27 #include <wx/wxprec.h>
39 #include <wx/string.h>
43 #include "model/comm_drv_n0183_android_bt.h"
44 #include "model/comm_navmsg_bus.h"
45 #include "model/comm_drv_registry.h"
48 #include "androidUTIL.h"
51 typedef enum DS_ENUM_BUFFER_STATE {
54 } _DS_ENUM_BUFFER_STATE;
58 #define MAX_OUT_QUEUE_MESSAGE_LENGTH 100
64 std::lock_guard<std::mutex> lock(m_mutex);
65 return m_queque.size();
69 std::lock_guard<std::mutex> lock(m_mutex);
70 return m_queque.empty();
74 std::lock_guard<std::mutex> lock(m_mutex);
75 return m_queque.front();
78 void push(
const T& value) {
79 std::lock_guard<std::mutex> lock(m_mutex);
84 std::lock_guard<std::mutex> lock(m_mutex);
89 std::queue<T> m_queque;
90 mutable std::mutex m_mutex;
93 #define OUT_QUEUE_LENGTH 20
94 #define MAX_OUT_QUEUE_MESSAGE_LENGTH 100
98 CommDriverN0183AndroidBTEvent::CommDriverN0183AndroidBTEvent( wxEventType commandType,
int id = 0)
99 : wxEvent(id, commandType){};
101 CommDriverN0183AndroidBTEvent::~CommDriverN0183AndroidBTEvent(){};
103 void CommDriverN0183AndroidBTEvent::SetPayload(std::shared_ptr<std::vector<unsigned char>> data) {
106 std::shared_ptr<std::vector<unsigned char>> CommDriverN0183AndroidBTEvent::GetPayload() {
return m_payload; }
109 wxEvent* CommDriverN0183AndroidBTEvent::Clone()
const {
112 newevent->m_payload = this->m_payload;
121 : buf_(std::unique_ptr<T[]>(
new T[size])), max_size_(size) {}
124 size_t capacity()
const;
129 return (!full_ && (head_ == tail_));
138 std::lock_guard<std::mutex> lock(mutex_);
140 if (full_) tail_ = (tail_ + 1) % max_size_;
142 head_ = (head_ + 1) % max_size_;
144 full_ = head_ == tail_;
148 std::lock_guard<std::mutex> lock(mutex_);
150 if (empty())
return T();
153 auto val = buf_[tail_];
155 tail_ = (tail_ + 1) % max_size_;
162 std::unique_ptr<T[]> buf_;
165 const size_t max_size_;
169 CommDriverN0183AndroidBT::CommDriverN0183AndroidBT(
const ConnectionParams* params,
174 m_portstring(params->GetDSPort()),
176 m_listener(listener) {
178 this->attributes[
"commPort"] = params->Port.ToStdString();
179 this->attributes[
"userComment"] = params->UserComment.ToStdString();
180 dsPortType iosel = params->IOSelect;
181 std::string s_iosel = std::string(
"IN");
182 if (iosel == DS_TYPE_INPUT_OUTPUT) {s_iosel =
"OUT";}
183 else if (iosel == DS_TYPE_INPUT_OUTPUT) {s_iosel =
"IN/OUT";}
184 this->attributes[
"ioDirection"] = s_iosel;
187 Bind(wxEVT_COMMDRIVER_N0183_ANDROID_BT, &CommDriverN0183AndroidBT::handle_N0183_MSG,
193 CommDriverN0183AndroidBT::~CommDriverN0183AndroidBT() { Close(); }
195 bool CommDriverN0183AndroidBT::Open() {
197 comx = m_params.GetDSPort().AfterFirst(
':');
199 wxString port_uc = m_params.GetDSPort().Upper();
201 androidStartBT(
this, port_uc );
205 void CommDriverN0183AndroidBT::Close() {
207 wxString::Format(_T(
"Closing NMEA BT Driver %s"), m_portstring.c_str()));
211 Unbind(wxEVT_COMMDRIVER_N0183_ANDROID_BT, &CommDriverN0183AndroidBT::handle_N0183_MSG,
217 CommDriverRegistry::GetInstance().
Activate(shared_from_this());
220 bool CommDriverN0183AndroidBT::SendMessage(std::shared_ptr<const NavMsg> msg,
221 std::shared_ptr<const NavAddr> addr) {
223 auto msg_0183 = std::dynamic_pointer_cast<const Nmea0183Msg>(msg);
224 wxString sentence(msg_0183->payload.c_str());
226 wxString payload = sentence;
227 if( !sentence.EndsWith(_T(
"\r\n")) )
228 payload += _T(
"\r\n");
230 androidSendBTMessage(payload);
236 void CommDriverN0183AndroidBT::handle_N0183_MSG(
238 auto p =
event.GetPayload();
239 std::vector<unsigned char>* payload = p.get();
242 std::string full_sentence = std::string(payload->begin(), payload->end());
244 if ((full_sentence[0] ==
'$') || (full_sentence[0] ==
'!')) {
245 std::string identifier;
247 identifier = full_sentence.substr(1, 5);
251 auto msg = std::make_shared<const Nmea0183Msg>(identifier, full_sentence,
253 auto msg_all = std::make_shared<const Nmea0183Msg>(*msg,
"ALL");
255 if (m_params.SentencePassesFilter(full_sentence, FILTER_INPUT))
256 m_listener.
Notify(std::move(msg));
258 m_listener.
Notify(std::move(msg_all));
void Activate() override
Register driver and possibly do other post-ctor steps.
void Activate(DriverPtr driver)
Add driver to list of active drivers.
Interface implemented by transport layer and possible other parties like test code which should handl...
virtual void Notify(std::shared_ptr< const NavMsg > message)=0
Handle a received message.
Where messages are sent to or received from.
wxDEFINE_EVENT(REST_IO_EVT, ObservedEvt)
Event from IO thread to main.