28#include <wx/msw/winundef.h>
37#if defined(HAVE_READLINK) && !defined(HAVE_LIBGEN_H)
38#error Using readlink(3) requires libgen.h which cannot be found.
43#include "model/multiplexer.h"
45#include "model/config_vars.h"
46#include "model/conn_params.h"
50#include "model/comm_drv_n0183_android_bt.h"
52#include "model/nmea_log.h"
65bool CheckSumCheck(
const std::string &sentence) {
66 size_t check_start = sentence.find(
'*');
67 if (check_start == wxString::npos || check_start > sentence.size() - 3)
70 std::string check_str = sentence.substr(check_start + 1, 2);
71 unsigned long checksum = strtol(check_str.c_str(), 0, 16);
72 if (checksum == 0L && check_str !=
"00")
return false;
74 unsigned char calculated_checksum = 0;
75 for (std::string::const_iterator i = sentence.begin() + 1;
76 i != sentence.end() && *i !=
'*'; ++i)
77 calculated_checksum ^=
static_cast<unsigned char>(*i);
79 return calculated_checksum == checksum;
83 : m_log_callbacks(cb), m_legacy_input_filter_behaviour(filter_behaviour) {
88 auto n0183_msg = std::static_pointer_cast<const Nmea0183Msg>(ptr);
89 HandleN0183(n0183_msg);
92 InitN2KCommListeners();
95 if (g_GPS_Ident.IsEmpty()) g_GPS_Ident = wxT(
"Generic");
98Multiplexer::~Multiplexer() {}
100void Multiplexer::LogOutputMessage(
const std::shared_ptr<const NavMsg> &msg,
102 if (m_log_callbacks.log_is_active()) {
103 ns.direction = NavmsgStatus::Direction::kOutput;
105 m_log_callbacks.log_message(ll);
110 bool is_filtered,
bool is_error,
111 const wxString error_msg) {
112 if (m_log_callbacks.log_is_active()) {
114 ns.direction = NavmsgStatus::Direction::kReceived;
116 ns.status = NavmsgStatus::State::kChecksumError;
119 if (m_legacy_input_filter_behaviour) {
120 ns.accepted = NavmsgStatus::Accepted::kFilteredNoOutput;
122 ns.accepted = NavmsgStatus::Accepted::kFilteredDropped;
125 ns.accepted = NavmsgStatus::Accepted::kOk;
129 ll.error_msg = error_msg;
130 m_log_callbacks.log_message(ll);
134void Multiplexer::HandleN0183(std::shared_ptr<const Nmea0183Msg> n0183_msg) {
137 const auto &drivers = CommDriverRegistry::GetInstance().
GetDrivers();
138 auto &source_driver =
FindDriver(drivers, n0183_msg->source->iface);
141 bool bpass_input_filter =
true;
146 std::string str = n0183_msg->payload;
152 params = drv_serial->GetParams();
156 params = drv_net->GetParams();
164 params = drv_bluetooth->GetParams();
172 params.SentencePassesFilter(n0183_msg->payload.c_str(), FILTER_INPUT);
174 bool b_error =
false;
176 for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
181 bin_print.Printf(_T(
"<0x%02X>"), *it);
183 if ((*it != 0x0a) && (*it != 0x0d)) {
185 error_msg = _(
"Non-printable character in NMEA0183 message");
191 bool checksumOK = CheckSumCheck(n0183_msg->payload);
194 error_msg = _(
"NMEA0183 checksum error");
197 wxString port(n0183_msg->source->iface);
202 std::string source_iface;
204 source_iface = source_driver->iface;
207 for (
auto &driver : drivers) {
208 if (driver->bus == NavAddr::Bus::N0183) {
212 params = drv_serial->GetParams();
216 params = drv_net->GetParams();
223 params = drv_bluetooth->GetParams();
229 std::shared_ptr<const Nmea0183Msg> msg = n0183_msg;
230 if ((m_legacy_input_filter_behaviour && !bpass_input_filter) ||
231 bpass_input_filter) {
236 if ((!params.DisableEcho && params.Type == SERIAL) ||
237 driver->iface != source_iface) {
238 if (params.IOSelect == DS_TYPE_INPUT_OUTPUT ||
239 params.IOSelect == DS_TYPE_OUTPUT) {
240 bool bout_filter =
true;
241 bool bxmit_ok =
true;
242 std::string id(
"XXXXX");
243 size_t comma_pos = n0183_msg->payload.find(
",");
244 if (comma_pos != std::string::npos && comma_pos > 5)
245 id = n0183_msg->payload.substr(1, comma_pos - 1);
246 if (params.SentencePassesFilter(n0183_msg->payload.c_str(),
250 auto null_addr = std::make_shared<NavAddr>();
251 msg = std::make_shared<Nmea0183Msg>(
id, n0183_msg->payload,
253 bxmit_ok = driver->SendMessage(msg, null_addr);
258 if (m_log_callbacks.log_is_active()) {
260 ns.direction = NavmsgStatus::Direction::kOutput;
262 ns.accepted = NavmsgStatus::Accepted::kFilteredDropped;
264 if (!bxmit_ok) ns.status = NavmsgStatus::State::kTxError;
266 auto logaddr = std::make_shared<NavAddr0183>(driver->iface);
267 auto logmsg = std::make_shared<Nmea0183Msg>(
268 id, n0183_msg->payload, logaddr);
269 LogOutputMessage(logmsg, ns);
278void Multiplexer::InitN2KCommListeners() {
285 listener_N2K_All.
Listen(n2k_msg_All,
this, EVT_N2K_ALL);
287 HandleN2K_Log(UnpackEvtPointer<Nmea2000Msg>(ev));
291bool Multiplexer::HandleN2K_Log(std::shared_ptr<const Nmea2000Msg> n2k_msg) {
292 if (!m_log_callbacks.log_is_active())
return false;
294 auto payload = n2k_msg.get()->payload;
296 unsigned int pgn = 0;
297 pgn += n2k_msg.get()->payload.at(3);
298 pgn += n2k_msg.get()->payload.at(4) << 8;
299 pgn += n2k_msg.get()->payload.at(5) << 16;
302 printf(
" %d: payload\n", pgn);
303 for(
size_t i=0; i< payload.size(); i++){
304 printf(
"%02X ", payload.at(i));
307 std::string pretty = n2k_msg->to_string();
308 printf(
"%s\n\n", pretty.c_str());
312 if (payload.at(0) == 0x94) {
314 ns.direction = NavmsgStatus::Direction::kOutput;
315 LogOutputMessage(n2k_msg, ns);
318 std::string source = n2k_msg.get()->source->to_string();
321 unsigned char source_id = n2k_msg.get()->payload.at(7);
323 sprintf(ss,
"%d", source_id);
324 std::string ident = std::string(ss);
326 if (pgn == last_pgn_logged) {
331 wxString repeat_log_msg;
332 repeat_log_msg.Printf(
"...Repeated %d times\n", n_N2K_repeat);
339 log_msg.Printf(
"PGN: %d Source: %s ID: %s Desc: %s\n", pgn, source, ident,
340 N2K_LogMessage_Detail(pgn).c_str());
344 last_pgn_logged = pgn;
349std::string Multiplexer::N2K_LogMessage_Detail(
unsigned int pgn) {
350 std::string notused =
"Not used by OCPN, maybe by Plugins";
354 return "GNSS Position & DBoard: SAT System";
356 return "Position rapid";
358 return "COG/SOG rapid";
360 return "AIS Class A position report";
362 return "AIS Class B position report";
364 return "AIS Aids to Navigation (AtoN) Report";
366 return "AIS Base Station report";
368 return "AIS static data class A";
371 return "AIS static data class B part A";
373 return "AIS static data class B part B";
375 return "Heading rapid";
377 return "GNSS Sats & DBoard: SAT Status";
380 return "DBoard: Rudder data";
382 return "DBoard: Roll Pitch";
384 return "DBoard: Speed through water";
387 return "DBoard: Depth Data";
389 return "DBoard: Distance log";
391 return "DBoard: Wind data";
393 return "DBoard: Envorinment data";
396 return "System time. " + notused;
398 return "Man Overboard Notification. " + notused;
400 return "Heading/Track control. " + notused;
402 return "Rate of turn. " + notused;
404 return "Magnetic variation. " + notused;
406 return "Engine rapid param. " + notused;
408 return "Engine parameters dynamic. " + notused;
410 return "Transmission parameters dynamic. " + notused;
412 return "Trip Parameters, Engine. " + notused;
414 return "Binary status report. " + notused;
416 return "Fluid level. " + notused;
418 return "DC Detailed Status. " + notused;
420 return "Charger Status. " + notused;
422 return "Battery Status. " + notused;
424 return "Battery Configuration Status. " + notused;
426 return "Leeway. " + notused;
428 return "Windlass Control Status. " + notused;
430 return "Windlass Operating Status. " + notused;
432 return "Windlass Monitoring Status. " + notused;
434 return "Date,Time & Local offset. " + notused;
436 return "GNSS DOP data. " + notused;
438 return "Cross Track Error. " + notused;
440 return "Navigation info. " + notused;
442 return "Waypoint list. " + notused;
444 return "AIS Safety Related Broadcast Message. " + notused;
446 return "Waypoint list. " + notused;
448 return "Environmental parameters. " + notused;
450 return "Temperature. " + notused;
452 return "Humidity. " + notused;
454 return "Actual Pressure. " + notused;
456 return "Set Pressure. " + notused;
458 return "Temperature extended range. " + notused;
460 return "Meteorological Station Data. " + notused;
462 return "Trim Tab Position. " + notused;
464 return "Direction Data. " + notused;
466 return "No description. Not used by OCPN, maybe passed to plugins";
const std::vector< DriverPtr > & GetDrivers() const
void LogInputMessage(const std::shared_ptr< const NavMsg > &msg, bool is_filtered, bool is_error, const wxString error_msg="")
Logs an input message with context information.
Representation of message status as determined by the multiplexer.
static std::string MessageKey(const char *type="ALL")
Return key which should be used to listen to given message type.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
void Listen(const std::string &key, wxEvtHandler *listener, wxEventType evt)
Set object to send wxEventType ev to listener on changes in key.
Custom event class for OpenCPN's notification system.
std::shared_ptr< const void > GetSharedPtr() const
Gets the event's payload data.
DriverPtr & FindDriver(const std::vector< DriverPtr > &drivers, const std::string &iface, const NavAddr::Bus _bus)
Search list of drivers for a driver with given interface string.
Driver registration container, a singleton.
Raw messages layer, supports sending and recieving navmsg messages.