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 std::string str = n0183_msg->payload;
135 params = drv_serial->GetParams();
139 params = drv_net->GetParams();
147 params = drv_bluetooth->GetParams();
155 params.SentencePassesFilter(n0183_msg->payload.c_str(), FILTER_INPUT);
157 bool b_error =
false;
159 for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
164 bin_print.Printf(_T(
"<0x%02X>"), *it);
166 if ((*it != 0x0a) && (*it != 0x0d)) {
168 error_msg = _(
"Non-printable character in NMEA0183 message");
182 wxString port(n0183_msg->source->iface);
187 std::string source_iface;
189 source_iface = source_driver->iface;
192 for (
auto &driver : drivers) {
193 if (driver->bus == NavAddr::Bus::N0183) {
197 params = drv_serial->GetParams();
201 params = drv_net->GetParams();
208 params = drv_bluetooth->GetParams();
214 std::shared_ptr<const Nmea0183Msg> msg = n0183_msg;
215 if ((m_legacy_input_filter_behaviour && !bpass_input_filter) ||
216 bpass_input_filter) {
221 if ((!params.DisableEcho && params.Type == SERIAL) ||
222 driver->iface != source_iface) {
223 if (params.IOSelect == DS_TYPE_INPUT_OUTPUT ||
224 params.IOSelect == DS_TYPE_OUTPUT) {
225 bool bout_filter =
true;
226 bool bxmit_ok =
true;
227 std::string id(
"XXXXX");
228 size_t comma_pos = n0183_msg->payload.find(
",");
229 if (comma_pos != std::string::npos && comma_pos > 5)
230 id = n0183_msg->payload.substr(1, comma_pos - 1);
231 if (params.SentencePassesFilter(n0183_msg->payload.c_str(),
235 auto null_addr = std::make_shared<NavAddr>();
236 msg = std::make_shared<Nmea0183Msg>(
id, n0183_msg->payload,
238 bxmit_ok = driver->SendMessage(msg, null_addr);
243 if (m_log_callbacks.log_is_active()) {
245 ns.direction = NavmsgStatus::Direction::kOutput;
247 ns.accepted = NavmsgStatus::Accepted::kFilteredDropped;
249 if (!bxmit_ok) ns.status = NavmsgStatus::State::kTxError;
251 auto logaddr = std::make_shared<NavAddr0183>(driver->iface);
252 auto logmsg = std::make_shared<Nmea0183Msg>(
253 id, n0183_msg->payload, logaddr);
254 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;
279 auto payload = n2k_msg.get()->payload;
281 unsigned int pgn = 0;
282 pgn += n2k_msg.get()->payload.at(3);
283 pgn += n2k_msg.get()->payload.at(4) << 8;
284 pgn += n2k_msg.get()->payload.at(5) << 16;
287 printf(
" %d: payload\n", pgn);
288 for(
size_t i=0; i< payload.size(); i++){
289 printf(
"%02X ", payload.at(i));
292 std::string pretty = n2k_msg->to_string();
293 printf(
"%s\n\n", pretty.c_str());
297 if (payload.at(0) == 0x94) {
299 ns.direction = NavmsgStatus::Direction::kOutput;
300 LogOutputMessage(n2k_msg, ns);
303 std::string source = n2k_msg.get()->source->to_string();
306 unsigned char source_id = n2k_msg.get()->payload.at(7);
308 sprintf(ss,
"%d", source_id);
309 std::string ident = std::string(ss);
311 if (pgn == last_pgn_logged) {
316 wxString repeat_log_msg;
317 repeat_log_msg.Printf(
"...Repeated %d times\n", n_N2K_repeat);
324 log_msg.Printf(
"PGN: %d Source: %s ID: %s Desc: %s\n", pgn, source, ident,
325 N2K_LogMessage_Detail(pgn).c_str());
329 last_pgn_logged = pgn;
334std::string Multiplexer::N2K_LogMessage_Detail(
unsigned int pgn) {
335 std::string notused =
"Not used by OCPN, maybe by Plugins";
339 return "GNSS Position & DBoard: SAT System";
341 return "Position rapid";
343 return "COG/SOG rapid";
345 return "AIS Class A position report";
347 return "AIS Class B position report";
349 return "AIS Aids to Navigation (AtoN) Report";
351 return "AIS Base Station report";
353 return "AIS static data class A";
356 return "AIS static data class B part A";
358 return "AIS static data class B part B";
360 return "Heading rapid";
362 return "GNSS Sats & DBoard: SAT Status";
365 return "DBoard: Rudder data";
367 return "DBoard: Roll Pitch";
369 return "DBoard: Speed through water";
372 return "DBoard: Depth Data";
374 return "DBoard: Distance log";
376 return "DBoard: Wind data";
378 return "DBoard: Envorinment data";
381 return "System time. " + notused;
383 return "Man Overboard Notification. " + notused;
385 return "Heading/Track control. " + notused;
387 return "Rate of turn. " + notused;
389 return "Magnetic variation. " + notused;
391 return "Engine rapid param. " + notused;
393 return "Engine parameters dynamic. " + notused;
395 return "Transmission parameters dynamic. " + notused;
397 return "Trip Parameters, Engine. " + notused;
399 return "Binary status report. " + notused;
401 return "Fluid level. " + notused;
403 return "DC Detailed Status. " + notused;
405 return "Charger Status. " + notused;
407 return "Battery Status. " + notused;
409 return "Battery Configuration Status. " + notused;
411 return "Leeway. " + notused;
413 return "Windlass Control Status. " + notused;
415 return "Windlass Operating Status. " + notused;
417 return "Windlass Monitoring Status. " + notused;
419 return "Date,Time & Local offset. " + notused;
421 return "GNSS DOP data. " + notused;
423 return "Cross Track Error. " + notused;
425 return "Navigation info. " + notused;
427 return "Waypoint list. " + notused;
429 return "AIS Safety Related Broadcast Message. " + notused;
431 return "Waypoint list. " + notused;
433 return "Environmental parameters. " + notused;
435 return "Temperature. " + notused;
437 return "Humidity. " + notused;
439 return "Actual Pressure. " + notused;
441 return "Set Pressure. " + notused;
443 return "Temperature extended range. " + notused;
445 return "Meteorological Station Data. " + notused;
447 return "Trim Tab Position. " + notused;
449 return "Direction Data. " + notused;
451 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.