30#include <wx/fileconf.h>
31#include <wx/jsonval.h>
32#include <wx/jsonreader.h>
33#include <wx/tokenzr.h>
52 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
57 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
58 return msg->source->to_string();
62 auto msg = UnpackEvtPointer<Nmea0183Msg>(ev);
67 auto msg = UnpackEvtPointer<PluginMsg>(ev);
72 auto msg = UnpackEvtPointer<SignalkMsg>(ev);
75 reader.
Parse(wxString(msg->raw_message), &data);
83 for (
size_t i = 0; i < reader.
GetErrors().GetCount(); i++)
84 root[
"Errors"].Append(reader.
GetErrors().Item(i));
87 for (
size_t i = 0; i < reader.
GetWarnings().GetCount(); i++)
88 root[
"Warnings"].Append(reader.
GetWarnings().Item(i));
90 root[
"Context"] = msg->context;
91 root[
"ContextSelf"] = msg->context_self;
93 return static_pointer_cast<void>(std::make_shared<wxJSONValue>(root));
98 auto msg = UnpackEvtPointer<NotificationMsg>(ev);
99 auto note = msg->notification;
100 auto rv = std::make_shared<PI_Notification>(
102 note->GetTimeoutStart(), note->GetTimeoutLeft(), note->GetGuid());
103 rv->action_verb = msg->action_verb;
109 return make_shared<ObservableListener>(
Nmea2000Msg(
id.
id), eh, et);
114 return make_shared<ObservableListener>(
Nmea0183Msg(
id.
id), eh, et);
119 return make_shared<ObservableListener>(
SignalkMsg(), eh, et);
129 return make_shared<ObservableListener>(
PluginMsg(
id.
id,
""), eh, et);
139 auto msg = UnpackEvtPointer<BasicNavDataMsg>(ev);
142 data.
lat = msg->pos.lat;
143 data.lon = msg->pos.lon;
148 data.time = msg->time;
155 std::vector<DriverHandle> result;
157 auto& registry = CommDriverRegistry::GetInstance();
158 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
160 for (
auto& driver : drivers) result.push_back(driver->Key());
167 auto& registry = CommDriverRegistry::GetInstance();
168 auto& drivers = registry.GetDrivers();
169 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
171 for (
auto& d : drivers)
172 if (d->Key() == handle) found = d.get();
174 return found->GetAttributes();
180 DriverHandle handle,
const std::shared_ptr<std::vector<uint8_t>>& payload) {
182 auto& registry = CommDriverRegistry::GetInstance();
183 auto& drivers = registry.GetDrivers();
185 for (
auto& d : drivers)
186 if (d->Key() == handle) found = d.get();
192 std::unordered_map<std::string, std::string> attributes =
194 auto protocol_it = attributes.find(
"protocol");
196 std::string protocol = protocol_it->second;
202 if (protocol ==
"nmea0183") {
205 std::string msg(payload->begin(), payload->end());
206 std::string
id = msg.substr(1, 5);
207 auto address = std::make_shared<NavAddr>();
208 auto msg_out = std::make_shared<Nmea0183Msg>(
id, msg, address);
209 bool xmit_ok = d0183->SendMessage(msg_out, address);
211 }
else if (protocol ==
"internal") {
212 std::string msg(payload->begin(), payload->end());
213 size_t space_pos = msg.find(
" ");
215 auto plugin_msg = std::make_shared<PluginMsg>(msg.substr(0, space_pos),
216 msg.substr(space_pos + 1));
217 NavMsgBus::GetInstance().
Notify(static_pointer_cast<NavMsg>(plugin_msg));
219 }
else if (protocol ==
"loopback") {
220 std::string msg(payload->begin(), payload->end());
223 bool send_ok = found->SendMessage(navmsg,
nullptr);
231 DriverHandle handle,
int PGN,
int destinationCANAddress,
int priority,
232 const std::shared_ptr<std::vector<uint8_t>>& payload) {
237 auto& registry = CommDriverRegistry::GetInstance();
238 auto& drivers = registry.GetDrivers();
241 for (
auto& d : drivers)
242 if (d->Key() == handle) found = d.get();
247 std::make_shared<const NavAddr2000>(found->
iface, destinationCANAddress);
249 std::make_shared<const Nmea2000Msg>(_PGN, *payload, dest_addr, priority);
250 bool result = found->SendMessage(msg, dest_addr);
256 std::vector<int>& pgn_list) {
260 auto& registry = CommDriverRegistry::GetInstance();
261 auto& drivers = registry.GetDrivers();
263 for (
auto& d : drivers)
264 if (d->Key() == handle) found = d.get();
272 for (
size_t i = 0; i < pgn_list.size(); i++) {
276 while (nTry && iresult < 0) {
277 iresult = dn2k->SetTXPGN(pgn_list[i]);
292void ReloadConfigConnections() {
294 auto& registry = CommDriverRegistry::GetInstance();
295 registry.CloseAllDrivers();
300 TheConnectionParams().clear();
301 pConf->SetPath(
"/Settings/NMEADataSource");
303 wxString connectionconfigs;
304 pConf->Read(
"DataConnections", &connectionconfigs);
305 if (!connectionconfigs.IsEmpty()) {
306 wxArrayString confs = wxStringTokenize(connectionconfigs,
"|");
307 for (
size_t i = 0; i < confs.Count(); i++) {
309 if (!prm->Valid)
continue;
310 TheConnectionParams().push_back(prm);
316 for (
auto* cp : TheConnectionParams()) {
319 cp->b_IsSetup = TRUE;
329 const std::string& _message,
330 int _timeout_start,
int _timeout_left,
332 severity = _severity;
334 auto_timeout_start = _timeout_start;
335 auto_timeout_left = _timeout_left;
339int GetActiveNotificationCount() {
340 auto& noteman = NotificationManager::GetInstance();
341 return noteman.GetNotificationCount();
345 auto& noteman = NotificationManager::GetInstance();
346 if (noteman.GetNotificationCount())
353 const std::string& _message,
int timeout_secs) {
354 auto& noteman = NotificationManager::GetInstance();
355 auto notification = std::make_shared<Notification>(
356 (NotificationSeverity)_severity, _message, timeout_secs);
357 return noteman.AddNotification(notification);
360bool AcknowledgeNotification(
const std::string& guid) {
361 auto& noteman = NotificationManager::GetInstance();
362 return noteman.AcknowledgeNotification(guid);
365std::vector<std::shared_ptr<PI_Notification>> GetActiveNotifications() {
366 auto& noteman = NotificationManager::GetInstance();
367 std::vector<std::shared_ptr<PI_Notification>> pi_notes;
368 for (
auto note : noteman.GetNotifications()) {
369 auto pi_note = std::make_shared<PI_Notification>(
371 note->GetTimeoutStart(), note->GetTimeoutLeft(), note->GetGuid());
372 pi_notes.push_back(pi_note);
383 NavAddr::Bus ibus = NavAddr::Bus::Undef;
385 case PI_Conn_Bus::N0183:
386 ibus = NavAddr::Bus::N0183;
389 case PI_Conn_Bus::Signalk:
390 ibus = NavAddr::Bus::Signalk;
393 case PI_Conn_Bus::N2000:
394 ibus = NavAddr::Bus::N2000;
402 if (ibus != NavAddr::Bus::Undef) {
403 auto& registry = CommDriverRegistry::GetInstance();
404 auto& drivers = registry.GetDrivers();
405 auto& found_driver =
FindDriver(drivers, iface, ibus);
407 auto stats_provider =
409 if (stats_provider) {
410 stats = stats_provider->GetDriverStats();
416 if (stats.available) {
418 rv.state = PI_Comm_State::Ok;
420 rv.state = PI_Comm_State::NoData;
422 rv.state = PI_Comm_State::Unavailable;
Common interface for all drivers.
const std::string iface
Physical device for 0183, else a unique string.
NMEA0183 basic parsing common parts:
Driver interface providing driver statistics.
static std::shared_ptr< const NavMsg > ParsePluginMessage(const std::string &msg)
Parse a string as provided by plugin and convert to a navmsg.
void Notify(std::shared_ptr< const NavMsg > message)
Accept message received by driver, make it available for upper layers.
A regular Nmea0183 message.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
Custom event class for OpenCPN's notification system.
unsigned error_count
Number of detected errors since program start.
unsigned tx_count
Number of bytes sent since program start.
unsigned rx_count
Number of bytes received since program start.
PI_Notification(PI_NotificationSeverity _severity, const std::string &_message, int _timeout_start, int _timeout_left, std::string _guid)
Plugin Notification Framework support.
A plugin to plugin json message over the REST interface.
A parsed SignalK message over ipv4.
const wxArrayString & GetWarnings() const
Return a reference to the warning message's array.
const wxArrayString & GetErrors() const
Return a reference to the error message's array.
int GetErrorCount() const
Return the size of the error message's array.
int GetWarningCount() const
Return the size of the warning message's array.
int Parse(const wxString &doc, wxJSONValue *val)
Parse the JSON document.
The JSON value class implementation.
Decoded messages definitions.
void MakeCommDriver(const ConnectionParams *params)
Create and register a driver for given connection.
Communication drivers factory and support.
Loopback driver, treat sent messages as received.
Nmea2000 IP network driver.
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.
User notifications manager.
PlugIn Object Definition/API.
CommDriverResult
Error return values
@ RESULT_COMM_INVALID_PARMS
Invalid parameters provided to operation.
@ RESULT_COMM_TX_ERROR
Error occurred during transmission.
@ RESULT_COMM_REGISTER_PGN_ERROR
Failed to register PGN parameters.
@ RESULT_COMM_NO_ERROR
Operation completed successfully.
@ RESULT_COMM_INVALID_HANDLE
Invalid or unknown driver handle specified.
PI_NotificationSeverity
Plugin Notification Framework support.
std::string DriverHandle
Plugin API supporting direct access to comm drivers for output purposes.
wxFileConfig * GetOCPNConfigObject()
Gets OpenCPN's configuration object.
PI_Comm_Status GetConnState(const std::string &iface, PI_Conn_Bus _bus)
Plugin polled Comm Status support.
std::string GetPluginMsgPayload(PluginMsgId id, ObservedEvt ev)
Retrieve the string in a plugin message, internal or received on the REST insterface.
std::string GetN0183Payload(NMEA0183Id id, ObservedEvt ev)
Return payload in a received n0183 message of type id in ev.
PluginNavdata GetEventNavdata(ObservedEvt ev)
Return BasicNavDataMsg decoded data available in ev.
std::shared_ptr< PI_Notification > GetNotificationMsgPayload(NotificationMsgId id, ObservedEvt ev)
Retrieve the Notification Event in a Notification message.
CommDriverResult WriteCommDriverN2K(DriverHandle handle, int PGN, int destinationCANAddress, int priority, const std::shared_ptr< std::vector< uint8_t > > &payload)
Send a PGN message to an NMEA2000 address.
wxString * GetpPrivateApplicationDataLocation()
Gets private application data directory.
shared_ptr< ObservableListener > GetListener(NMEA2000Id id, wxEventType et, wxEvtHandler *eh)
Gets listener for NMEA 2000 messages.
std::vector< DriverHandle > GetActiveDrivers()
Comm port plugin TX support methods
const std::unordered_map< std::string, std::string > GetAttributes(DriverHandle handle)
Query a specific driver for attributes.
std::string GetN2000Source(NMEA2000Id id, ObservedEvt ev)
Return source identifier (iface) of a received n2000 message of type id in ev.
vector< uint8_t > GetN2000Payload(NMEA2000Id id, ObservedEvt ev)
Return N2K payload for a received n2000 message of type id in ev.
std::shared_ptr< void > GetSignalkPayload(ObservedEvt ev)
Get SignalK status payload after receiving a message.
CommDriverResult WriteCommDriver(DriverHandle handle, const std::shared_ptr< std::vector< uint8_t > > &payload)
Send a non-NMEA2000 message.
CommDriverResult RegisterTXPGNs(DriverHandle handle, std::vector< int > &pgn_list)
Register PGNs that this application intends to transmit for some NMEA 2000 adapters like Actisense NG...
Driver statistics report.
unsigned tx_count
Number of bytes sent since program start.
unsigned rx_count
Number of bytes received since program start.
unsigned error_count
Number of detected errors since program start.
Identifier for NMEA 0183 sentence types.
Identifier for NMEA 2000 message types.
Navigation data message identifier.
Facade for NotificationMsg.
Facade for NavAddrPluginMsg.
Basic navigation data structure.
double lat
Latitude in decimal degrees.
Identifier for Signal K paths.