31#include <wx/fileconf.h>
32#include <wx/jsonval.h>
33#include <wx/jsonreader.h>
34#include <wx/tokenzr.h>
36#include "model/base_platform.h"
37#include "model/comm_appmsg.h"
40#include "model/comm_drv_n2k.h"
45#include "model/comm_drv_factory.h"
46#include "model/comm_drv_n2k_net.h"
47#include "model/comm_drv_n2k_serial.h"
51 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
56 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
57 return msg->source->to_string();
61 auto msg = UnpackEvtPointer<Nmea0183Msg>(ev);
66 auto msg = UnpackEvtPointer<PluginMsg>(ev);
71 auto msg = UnpackEvtPointer<SignalkMsg>(ev);
74 reader.
Parse(wxString(msg->raw_message), &data);
82 for (
size_t i = 0; i < reader.
GetErrors().GetCount(); i++)
83 root[
"Errors"].Append(reader.
GetErrors().Item(i));
86 for (
size_t i = 0; i < reader.
GetWarnings().GetCount(); i++)
87 root[
"Warnings"].Append(reader.
GetWarnings().Item(i));
89 root[
"Context"] = msg->context;
90 root[
"ContextSelf"] = msg->context_self;
92 return static_pointer_cast<void>(std::make_shared<wxJSONValue>(root));
97 auto msg = UnpackEvtPointer<NotificationMsg>(ev);
98 auto note = msg->notification;
99 auto rv = std::make_shared<PI_Notification>(
101 note->GetTimeoutStart(), note->GetTimeoutLeft(), note->GetGuid());
102 rv->action_verb = msg->action_verb;
108 return make_shared<ObservableListener>(
Nmea2000Msg(
id.
id), eh, et);
113 return make_shared<ObservableListener>(
Nmea0183Msg(
id.
id), eh, et);
118 return make_shared<ObservableListener>(
SignalkMsg(), eh, et);
128 return make_shared<ObservableListener>(
PluginMsg(
id.
id,
""), eh, et);
138 auto msg = UnpackEvtPointer<BasicNavDataMsg>(ev);
141 data.
lat = msg->pos.lat;
142 data.lon = msg->pos.lon;
147 data.time = msg->time;
154 std::vector<DriverHandle> result;
156 auto& registry = CommDriverRegistry::GetInstance();
157 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
159 for (
auto& driver : drivers) result.push_back(driver->Key());
166 auto& registry = CommDriverRegistry::GetInstance();
167 auto& drivers = registry.GetDrivers();
168 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
170 for (
auto& d : drivers)
171 if (d->Key() == handle) found = d.get();
173 return found->GetAttributes();
179 DriverHandle handle,
const std::shared_ptr<std::vector<uint8_t>>& payload) {
181 auto& registry = CommDriverRegistry::GetInstance();
182 auto& drivers = registry.GetDrivers();
184 for (
auto& d : drivers)
185 if (d->Key() == handle) found = d.get();
191 std::unordered_map<std::string, std::string> attributes =
193 auto protocol_it = attributes.find(
"protocol");
195 std::string protocol = protocol_it->second;
197 if (protocol ==
"nmea0183") {
200 std::string msg(payload->begin(), payload->end());
201 std::string
id = msg.substr(1, 5);
202 auto address = std::make_shared<NavAddr>();
203 auto msg_out = std::make_shared<Nmea0183Msg>(
id, msg, address);
204 bool xmit_ok = d0183->SendMessage(msg_out, address);
206 }
else if (protocol ==
"internal") {
207 std::string msg(payload->begin(), payload->end());
208 size_t space_pos = msg.find(
" ");
210 auto plugin_msg = std::make_shared<PluginMsg>(msg.substr(0, space_pos),
211 msg.substr(space_pos + 1));
212 NavMsgBus::GetInstance().
Notify(static_pointer_cast<NavMsg>(plugin_msg));
220 DriverHandle handle,
int PGN,
int destinationCANAddress,
int priority,
221 const std::shared_ptr<std::vector<uint8_t>>& payload) {
226 auto& registry = CommDriverRegistry::GetInstance();
227 auto& drivers = registry.GetDrivers();
230 for (
auto& d : drivers)
231 if (d->Key() == handle) found = d.get();
236 std::make_shared<const NavAddr2000>(found->
iface, destinationCANAddress);
238 std::make_shared<const Nmea2000Msg>(_PGN, *payload, dest_addr, priority);
239 bool result = found->SendMessage(msg, dest_addr);
245 std::vector<int>& pgn_list) {
249 auto& registry = CommDriverRegistry::GetInstance();
250 auto& drivers = registry.GetDrivers();
252 for (
auto& d : drivers)
253 if (d->Key() == handle) found = d.get();
261 for (
size_t i = 0; i < pgn_list.size(); i++) {
265 while (nTry && iresult < 0) {
266 iresult = dn2k->SetTXPGN(pgn_list[i]);
281void ReloadConfigConnections() {
283 auto& registry = CommDriverRegistry::GetInstance();
284 registry.CloseAllDrivers();
289 TheConnectionParams().clear();
290 pConf->SetPath(_T (
"/Settings/NMEADataSource" ));
292 wxString connectionconfigs;
293 pConf->Read(_T(
"DataConnections" ), &connectionconfigs);
294 if (!connectionconfigs.IsEmpty()) {
295 wxArrayString confs = wxStringTokenize(connectionconfigs, _T(
"|"));
296 for (
size_t i = 0; i < confs.Count(); i++) {
298 if (!prm->Valid)
continue;
299 TheConnectionParams().push_back(prm);
305 for (
auto* cp : TheConnectionParams()) {
308 cp->b_IsSetup = TRUE;
318 const std::string& _message,
319 int _timeout_start,
int _timeout_left,
321 severity = _severity;
323 auto_timeout_start = _timeout_start;
324 auto_timeout_left = _timeout_left;
328int GetActiveNotificationCount() {
329 auto& noteman = NotificationManager::GetInstance();
330 return noteman.GetNotificationCount();
334 auto& noteman = NotificationManager::GetInstance();
339 const std::string& _message,
int timeout_secs) {
340 auto& noteman = NotificationManager::GetInstance();
341 auto notification = std::make_shared<Notification>(
342 (NotificationSeverity)_severity, _message, timeout_secs);
343 return noteman.AddNotification(notification);
346bool AcknowledgeNotification(
const std::string& guid) {
347 auto& noteman = NotificationManager::GetInstance();
348 return noteman.AcknowledgeNotification(guid);
351std::vector<std::shared_ptr<PI_Notification>> GetActiveNotifications() {
352 auto& noteman = NotificationManager::GetInstance();
353 std::vector<std::shared_ptr<PI_Notification>> pi_notes;
354 for (
auto note : noteman.GetNotifications()) {
355 auto pi_note = std::make_shared<PI_Notification>(
357 note->GetTimeoutStart(), note->GetTimeoutLeft(), note->GetGuid());
358 pi_notes.push_back(pi_note);
369 NavAddr::Bus ibus = NavAddr::Bus::Undef;
371 case PI_Conn_Bus::N0183:
372 ibus = NavAddr::Bus::N0183;
375 case PI_Conn_Bus::Signalk:
376 ibus = NavAddr::Bus::Signalk;
379 case PI_Conn_Bus::N2000:
380 ibus = NavAddr::Bus::N2000;
388 if (ibus != NavAddr::Bus::Undef) {
389 auto& registry = CommDriverRegistry::GetInstance();
390 auto& drivers = registry.GetDrivers();
391 auto& found_driver =
FindDriver(drivers, iface, ibus);
393 auto stats_provider =
395 if (stats_provider) {
396 stats = stats_provider->GetDriverStats();
402 if (stats.available) {
404 rv.state = PI_Comm_State::Ok;
406 rv.state = PI_Comm_State::NoData;
408 rv.state = PI_Comm_State::Unavailable;
Common interface for all drivers.
const std::string iface
Physical device for 0183, else a unique string.
NMEA0183 drivers common part.
Driver interface providing driver statistics.
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.
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.
Class NotificationManager.
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(void)
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.
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.
wxString * GetpPrivateApplicationDataLocation(void)
Gets private application data directory.
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.