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"
66 : m_log_callbacks(cb), m_legacy_input_filter_behaviour(filter_behaviour) {
71 auto n0183_msg = std::static_pointer_cast<const Nmea0183Msg>(ptr);
72 HandleN0183(n0183_msg);
75 InitN2KCommListeners();
78 if (g_GPS_Ident.IsEmpty()) g_GPS_Ident = wxT(
"Generic");
81Multiplexer::~Multiplexer() {}
83void Multiplexer::LogOutputMessage(
const std::shared_ptr<const NavMsg> &msg,
85 if (m_log_callbacks.log_is_active()) {
86 ns.direction = NavmsgStatus::Direction::kOutput;
88 m_log_callbacks.log_message(ll);
93 bool is_filtered,
bool is_error,
94 const wxString error_msg) {
95 if (m_log_callbacks.log_is_active()) {
97 ns.direction = NavmsgStatus::Direction::kReceived;
99 ns.status = NavmsgStatus::State::kChecksumError;
102 if (m_legacy_input_filter_behaviour) {
103 ns.accepted = NavmsgStatus::Accepted::kFilteredNoOutput;
105 ns.accepted = NavmsgStatus::Accepted::kFilteredDropped;
108 ns.accepted = NavmsgStatus::Accepted::kOk;
112 ll.error_msg = error_msg;
113 m_log_callbacks.log_message(ll);
117void Multiplexer::HandleN0183(std::shared_ptr<const Nmea0183Msg> n0183_msg) {
120 const auto &drivers = CommDriverRegistry::GetInstance().
GetDrivers();
121 auto &source_driver =
FindDriver(drivers, n0183_msg->source->iface);
124 bool bpass_input_filter =
true;
129 if (m_log_callbacks.log_is_active()) {
130 std::string str = n0183_msg->payload;
137 params = drv_serial->GetParams();
141 params = drv_net->GetParams();
149 params = drv_bluetooth->GetParams();
157 params.SentencePassesFilter(n0183_msg->payload.c_str(), FILTER_INPUT);
159 bool b_error =
false;
161 for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
166 bin_print.Printf(_T(
"<0x%02X>"), *it);
168 if ((*it != 0x0a) && (*it != 0x0d)) {
170 error_msg = _(
"Non-printable character in NMEA0183 message");
184 wxString port(n0183_msg->source->iface);
190 std::string source_iface;
192 source_iface = source_driver->iface;
195 for (
auto &driver : drivers) {
196 if (driver->bus == NavAddr::Bus::N0183) {
200 params = drv_serial->GetParams();
204 params = drv_net->GetParams();
211 params = drv_bluetooth->GetParams();
217 std::shared_ptr<const Nmea0183Msg> msg = n0183_msg;
218 if ((m_legacy_input_filter_behaviour && !bpass_input_filter) ||
219 bpass_input_filter) {
224 if ((!params.DisableEcho && params.Type == SERIAL) ||
225 driver->iface != source_iface) {
226 if (params.IOSelect == DS_TYPE_INPUT_OUTPUT ||
227 params.IOSelect == DS_TYPE_OUTPUT) {
228 bool bout_filter =
true;
229 bool bxmit_ok =
true;
230 std::string id(
"XXXXX");
231 size_t comma_pos = n0183_msg->payload.find(
",");
232 if (comma_pos != std::string::npos && comma_pos > 5)
233 id = n0183_msg->payload.substr(1, comma_pos - 1);
234 if (params.SentencePassesFilter(n0183_msg->payload.c_str(),
238 auto null_addr = std::make_shared<NavAddr>();
239 msg = std::make_shared<Nmea0183Msg>(
id, n0183_msg->payload,
241 bxmit_ok = driver->SendMessage(msg, null_addr);
246 ns.direction = NavmsgStatus::Direction::kOutput;
248 ns.accepted = NavmsgStatus::Accepted::kFilteredDropped;
250 if (!bxmit_ok) ns.status = NavmsgStatus::State::kTxError;
252 auto logaddr = std::make_shared<NavAddr0183>(driver->iface);
254 std::make_shared<Nmea0183Msg>(
id, n0183_msg->payload, logaddr);
255 LogOutputMessage(logmsg, ns);
263void Multiplexer::InitN2KCommListeners() {
270 listener_N2K_All.
Listen(n2k_msg_All,
this, EVT_N2K_ALL);
272 HandleN2K_Log(UnpackEvtPointer<Nmea2000Msg>(ev));
276bool Multiplexer::HandleN2K_Log(std::shared_ptr<const Nmea2000Msg> n2k_msg) {
277 if (!m_log_callbacks.log_is_active())
return false;
280 unsigned int pgn = 0;
281 pgn += n2k_msg.get()->payload.at(3);
282 pgn += n2k_msg.get()->payload.at(4) << 8;
283 pgn += n2k_msg.get()->payload.at(5) << 16;
286 std::string source = n2k_msg.get()->source->to_string();
289 unsigned char source_id = n2k_msg.get()->payload.at(7);
291 sprintf(ss,
"%d", source_id);
292 std::string ident = std::string(ss);
294 if (pgn == last_pgn_logged) {
299 wxString repeat_log_msg;
300 repeat_log_msg.Printf(
"...Repeated %d times\n", n_N2K_repeat);
307 log_msg.Printf(
"PGN: %d Source: %s ID: %s Desc: %s\n", pgn, source, ident,
308 N2K_LogMessage_Detail(pgn).c_str());
312 last_pgn_logged = pgn;
316std::string Multiplexer::N2K_LogMessage_Detail(
unsigned int pgn) {
317 std::string notused =
"Not used by OCPN, maybe by Plugins";
321 return "GNSS Position & DBoard: SAT System";
323 return "Position rapid";
325 return "COG/SOG rapid";
327 return "AIS Class A position report";
329 return "AIS Class B position report";
331 return "AIS Aids to Navigation (AtoN) Report";
333 return "AIS Base Station report";
335 return "AIS static data class A";
338 return "AIS static data class B part A";
340 return "AIS static data class B part B";
342 return "Heading rapid";
344 return "GNSS Sats & DBoard: SAT Status";
347 return "DBoard: Rudder data";
349 return "DBoard: Roll Pitch";
351 return "DBoard: Speed through water";
354 return "DBoard: Depth Data";
356 return "DBoard: Distance log";
358 return "DBoard: Wind data";
360 return "DBoard: Envorinment data";
363 return "System time. " + notused;
365 return "Man Overboard Notification. " + notused;
367 return "Heading/Track control. " + notused;
369 return "Rate of turn. " + notused;
371 return "Magnetic variation. " + notused;
373 return "Engine rapid param. " + notused;
375 return "Engine parameters dynamic. " + notused;
377 return "Transmission parameters dynamic. " + notused;
379 return "Trip Parameters, Engine. " + notused;
381 return "Binary status report. " + notused;
383 return "Fluid level. " + notused;
385 return "DC Detailed Status. " + notused;
387 return "Charger Status. " + notused;
389 return "Battery Status. " + notused;
391 return "Battery Configuration Status. " + notused;
393 return "Leeway. " + notused;
395 return "Windlass Control Status. " + notused;
397 return "Windlass Operating Status. " + notused;
399 return "Windlass Monitoring Status. " + notused;
401 return "Date,Time & Local offset. " + notused;
403 return "GNSS DOP data. " + notused;
405 return "Cross Track Error. " + notused;
407 return "Navigation info. " + notused;
409 return "Waypoint list. " + notused;
411 return "AIS Safety Related Broadcast Message. " + notused;
413 return "Waypoint list. " + notused;
415 return "Environmental parameters. " + notused;
417 return "Temperature. " + notused;
419 return "Humidity. " + notused;
421 return "Actual Pressure. " + notused;
423 return "Set Pressure. " + notused;
425 return "Temperature extended range. " + notused;
427 return "Meteorological Station Data. " + notused;
429 return "Trim Tab Position. " + notused;
431 return "Direction Data. " + notused;
433 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.