31#include <wx/jsonval.h>
32#include <wx/jsonreader.h>
34#include "model/base_platform.h"
35#include "model/comm_appmsg.h"
38#include "model/comm_drv_n2k.h"
46 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
51 auto msg = UnpackEvtPointer<Nmea2000Msg>(ev);
52 return msg->source->to_string();
56 auto msg = UnpackEvtPointer<Nmea0183Msg>(ev);
61 auto msg = UnpackEvtPointer<PluginMsg>(ev);
66 auto msg = UnpackEvtPointer<SignalkMsg>(ev);
69 reader.
Parse(wxString(msg->raw_message), &data);
77 for (
size_t i = 0; i < reader.
GetErrors().GetCount(); i++)
78 root[
"Errors"].Append(reader.
GetErrors().Item(i));
81 for (
size_t i = 0; i < reader.
GetWarnings().GetCount(); i++)
82 root[
"Warnings"].Append(reader.
GetWarnings().Item(i));
84 root[
"Context"] = msg->context;
85 root[
"ContextSelf"] = msg->context_self;
87 return static_pointer_cast<void>(std::make_shared<wxJSONValue>(root));
90shared_ptr<ObservableListener> GetListener(
NMEA2000Id id, wxEventType et,
92 return make_shared<ObservableListener>(
Nmea2000Msg(
id.
id), eh, et);
95std::shared_ptr<ObservableListener> GetListener(
NMEA0183Id id, wxEventType et,
97 return make_shared<ObservableListener>(
Nmea0183Msg(
id.
id), eh, et);
100shared_ptr<ObservableListener> GetListener(
SignalkId id, wxEventType et,
102 return make_shared<ObservableListener>(
SignalkMsg(), eh, et);
105shared_ptr<ObservableListener> GetListener(
NavDataId id, wxEventType et,
110std::shared_ptr<ObservableListener> GetListener(
PluginMsgId id, wxEventType et,
112 return make_shared<ObservableListener>(
PluginMsg(
id.
id,
""), eh, et);
116 auto msg = UnpackEvtPointer<BasicNavDataMsg>(ev);
119 data.lat = msg->pos.lat;
120 data.lon = msg->pos.lon;
125 data.time = msg->time;
132 std::vector<DriverHandle> result;
134 auto& registry = CommDriverRegistry::GetInstance();
135 const std::vector<DriverPtr>& drivers = registry.GetDrivers();
137 for (
auto& driver : drivers) result.push_back(driver->Key());
144 auto& registry = CommDriverRegistry::GetInstance();
145 auto& drivers = registry.GetDrivers();
146 auto func = [handle](
const DriverPtr d) {
return d->Key() == handle; };
148 for (
auto& d : drivers)
149 if (d->Key() == handle) found = d.get();
151 return found->GetAttributes();
157 DriverHandle handle,
const std::shared_ptr<std::vector<uint8_t>>& payload) {
159 auto& registry = CommDriverRegistry::GetInstance();
160 auto& drivers = registry.GetDrivers();
162 for (
auto& d : drivers)
163 if (d->Key() == handle) found = d.get();
165 return RESULT_COMM_INVALID_HANDLE;
169 std::unordered_map<std::string, std::string> attributes =
171 auto protocol_it = attributes.find(
"protocol");
172 if (protocol_it == attributes.end())
return RESULT_COMM_INVALID_PARMS;
173 std::string protocol = protocol_it->second;
175 if (protocol ==
"nmea0183") {
178 std::string msg(payload->begin(), payload->end());
179 std::string
id = msg.substr(1, 5);
180 auto address = std::make_shared<NavAddr>();
181 auto msg_out = std::make_shared<Nmea0183Msg>(
id, msg, address);
182 bool xmit_ok = d0183->SendMessage(msg_out, address);
183 return xmit_ok ? RESULT_COMM_NO_ERROR : RESULT_COMM_TX_ERROR;
184 }
else if (protocol ==
"internal") {
185 std::string msg(payload->begin(), payload->end());
186 size_t space_pos = msg.find(
" ");
187 if (space_pos == std::string::npos)
return RESULT_COMM_INVALID_PARMS;
188 auto plugin_msg = std::make_shared<PluginMsg>(msg.substr(0, space_pos),
189 msg.substr(space_pos + 1));
190 NavMsgBus::GetInstance().
Notify(static_pointer_cast<NavMsg>(plugin_msg));
191 return RESULT_COMM_NO_ERROR;
193 return RESULT_COMM_INVALID_PARMS;
198 DriverHandle handle,
int PGN,
int destinationCANAddress,
int priority,
199 const std::shared_ptr<std::vector<uint8_t>>& payload) {
204 auto& registry = CommDriverRegistry::GetInstance();
205 auto& drivers = registry.GetDrivers();
208 for (
auto& d : drivers)
209 if (d->Key() == handle) found = d.get();
211 return RESULT_COMM_INVALID_HANDLE;
214 std::make_shared<const NavAddr2000>(found->
iface, destinationCANAddress);
216 std::make_shared<const Nmea2000Msg>(_PGN, *payload, dest_addr, priority);
217 bool result = found->SendMessage(msg, dest_addr);
219 return RESULT_COMM_NO_ERROR;
223 std::vector<int>& pgn_list) {
224 if (!pgn_list.size())
return RESULT_COMM_INVALID_PARMS;
227 auto& registry = CommDriverRegistry::GetInstance();
228 auto& drivers = registry.GetDrivers();
230 for (
auto& d : drivers)
231 if (d->Key() == handle) found = d.get();
234 return RESULT_COMM_INVALID_HANDLE;
239 for (
size_t i = 0; i < pgn_list.size(); i++) {
243 while (nTry && iresult < 0) {
244 iresult = dn2k->SetTXPGN(pgn_list[i]);
249 return RESULT_COMM_REGISTER_PGN_ERROR;
252 return RESULT_COMM_NO_ERROR;
255wxString* GetpPrivateApplicationDataLocation(
void) {
Common interface for all drivers.
const std::string iface
Physical device for 0183, else a unique string.
NMEA0183 drivers common part.
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.
Adds a std::shared<void> element to wxCommandEvent.
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.
Driver registration container, a singleton.
Raw messages layer, supports sending and recieving navmsg messages.
PlugIn Object Definition/API.
CommDriverResult
Error return values
std::string DriverHandle
Plugin API supporting direct access to comm drivers for output purposes.
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.
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.
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...
Facade for BasicNavDataMsg.
Facade for NavAddrPluginMsg.
Available decoded data for plugins.
Facade for NavAddrSignalK.