48#include "observable.h"
50using namespace std::literals::chrono_literals;
55 m_portstring(params->GetDSPort()),
56 m_baudrate(params->Baudrate),
58 [&](const std::vector<unsigned char>& v) { SendMessage(v); },
59 m_portstring, m_baudrate)),
62 m_stats_timer(*
this, 2s) {
63 m_garmin_handler =
nullptr;
64 this->attributes[
"commPort"] = params->Port.ToStdString();
65 this->attributes[
"userComment"] = params->UserComment.ToStdString();
66 this->attributes[
"ioDirection"] = DsPortTypeToString(params->IOSelect);
71CommDriverN0183Serial::~CommDriverN0183Serial() { Close(); }
73bool CommDriverN0183Serial::Open() {
75 comx = m_params.GetDSPort().AfterFirst(
':');
76 if (comx.IsEmpty())
return false;
78 wxString port_uc = m_params.GetDSPort().Upper();
80 auto send_func = [&](
const std::vector<unsigned char>& v) { SendMessage(v); };
81 if ((wxNOT_FOUND != port_uc.Find(
"USB")) &&
82 (wxNOT_FOUND != port_uc.Find(
"GARMIN"))) {
84 }
else if (m_params.Garmin) {
88 comx = comx.BeforeFirst(
' ');
97void CommDriverN0183Serial::Close() {
99 wxString::Format(
"Closing NMEA Driver %s", m_portstring.c_str()));
102 if (m_serial_io->IsRunning()) {
103 wxLogMessage(
"Stopping Secondary Thread");
104 m_serial_io->RequestStop();
105 std::chrono::milliseconds elapsed;
106 if (m_serial_io->WaitUntilStopped(10s, elapsed)) {
107 MESSAGE_LOG <<
"Stopped in " << elapsed.count() <<
" msec.";
109 MESSAGE_LOG <<
"Not stopped after 10 sec.";
114 if (m_garmin_handler) {
115 m_garmin_handler->Close();
116 delete m_garmin_handler;
117 m_garmin_handler =
nullptr;
121bool CommDriverN0183Serial::IsGarminThreadActive() {
122 if (m_garmin_handler) {
125 if (m_garmin_handler->m_usb_handle != INVALID_HANDLE_VALUE)
135void CommDriverN0183Serial::StopGarminUSBIOThread(
bool b_pause) {
136 if (m_garmin_handler) {
137 m_garmin_handler->StopIOThread(b_pause);
141bool CommDriverN0183Serial::SendMessage(std::shared_ptr<const NavMsg> msg,
142 std::shared_ptr<const NavAddr> addr) {
143 auto msg_0183 = std::dynamic_pointer_cast<const Nmea0183Msg>(msg);
144 wxString sentence(msg_0183->payload.c_str());
146 if (m_serial_io->IsRunning()) {
147 for (
int retries = 0; retries < 10; retries += 1) {
148 if (m_serial_io->SetOutMsg(sentence)) {
158void CommDriverN0183Serial::SendMessage(
const std::vector<unsigned char>& msg) {
161 if (m_params.IOSelect == DS_TYPE_OUTPUT)
return;
164 if (msg.size() < 6)
return;
165 if (msg[0] !=
'$' && msg[0] !=
'!')
return;
168 std::string identifier(msg.begin() + 1, msg.begin() + 6);
172 std::string payload(msg.begin(), msg.end());
174 std::make_shared<const Nmea0183Msg>(identifier, payload, GetAddress());
175 auto message_all = std::make_shared<const Nmea0183Msg>(*message,
"ALL");
177 if (m_params.SentencePassesFilter(payload, FILTER_INPUT))
178 m_listener.
Notify(std::move(message));
179 m_listener.
Notify(std::move(message_all));
NMEA0183 drivers common part.
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.
Nmea0183 serial communications wrapper, possibly running a thread.
Line-oriented input/output buffers.
Driver registration container, a singleton.
Communication statistics infrastructure.
Enhanced logging interface on top of wx/log.h.
Basic synchronization primitive.