OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_navmsg.h
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 - 2024 by David Register, Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
25#ifndef _DRIVER_NAVMSG_H
26#define _DRIVER_NAVMSG_H
27
28#include <memory>
29#include <sstream>
30#include <vector>
31#include <string>
32
33#ifdef _MSC_VER
34#include <winsock2.h>
35#else
36#include <netinet/in.h>
37#endif
38
39#include "observable.h"
40
41struct N2kPGN {
42 uint64_t pgn;
43
44 N2kPGN(uint64_t _pgn) { pgn = _pgn; }
45
46 friend bool operator<(const N2kPGN& lhs, const N2kPGN& rhs) {
47 return lhs.pgn < rhs.pgn;
48 }
49
50 std::string to_string() const {
51 std::stringstream ss;
52 ss << pgn;
53 return ss.str();
54 }
55};
56
66struct N2kName {
67 N2kName() {};
68 N2kName(uint64_t name) { value.Name = name; }
69
70 friend bool operator<(const N2kName& lhs, const N2kName& rhs) {
71 return lhs.value.Name < rhs.value.Name;
72 }
73
74 std::string to_string() const {
75 std::stringstream ss;
76 ss << value.Name;
77 return ss.str();
78 }
79
80 static uint64_t Parse(const std::string& s) {
81 std::stringstream ss;
82 uint64_t id;
83 ss << s;
84 ss >> id;
85 return id;
86 }
87
88 uint32_t GetNumber() const;
89 uint16_t GetManufacturer() const;
90 uint8_t GetDevInstanceLow() const;
91 uint8_t GetDevInstanceHigh() const;
92 uint8_t GetDevFunc() const;
93 uint8_t GetDevClass() const;
94 uint8_t GetSysInstance() const;
95 uint8_t GetIndustryGroup() const;
97 typedef union {
98 uint64_t Name;
99 struct {
100 uint32_t UnicNumberAndManCode; // ManufacturerCode 11 bits , UniqueNumber
101 // 21 bits
102 unsigned char DeviceInstance;
103 unsigned char DeviceFunction;
104 unsigned char DeviceClass;
105 unsigned char IndustryGroupAndSystemInstance; // 4 bits each
106 };
108
110
111 void SetUniqueNumber(uint32_t _UniqueNumber) {
112 value.UnicNumberAndManCode =
113 (value.UnicNumberAndManCode & 0xffe00000) | (_UniqueNumber & 0x1fffff);
114 }
115 void SetManufacturerCode(uint16_t _ManufacturerCode) {
116 value.UnicNumberAndManCode =
117 (value.UnicNumberAndManCode & 0x1fffff) |
118 (((unsigned long)(_ManufacturerCode & 0x7ff)) << 21);
119 }
120 void SetDeviceInstance(unsigned char _DeviceInstance) {
121 value.DeviceInstance = _DeviceInstance;
122 }
123 void SetDeviceFunction(unsigned char _DeviceFunction) {
124 value.DeviceFunction = _DeviceFunction;
125 }
126 void SetDeviceClass(unsigned char _DeviceClass) {
127 value.DeviceClass = ((_DeviceClass & 0x7f) << 1);
128 }
129 void SetIndustryGroup(unsigned char _IndustryGroup) {
130 value.IndustryGroupAndSystemInstance =
131 (value.IndustryGroupAndSystemInstance & 0x0f) | (_IndustryGroup << 4) |
132 0x80;
133 }
134 void SetSystemInstance(unsigned char _SystemInstance) {
135 value.IndustryGroupAndSystemInstance =
136 (value.IndustryGroupAndSystemInstance & 0xf0) |
137 (_SystemInstance & 0x0f);
138 }
139
140 uint64_t GetName() const { return value.Name; }
141};
142
144class NavAddr {
145public:
146 enum class Bus {
147 N0183,
148 Signalk,
149 N2000,
150 Onenet,
151 Plugin,
152 TestBus,
153 AppMsg,
154 Undef
155 };
156
157 NavAddr(Bus b, const std::string& i) : bus(b), iface(i) {};
158 NavAddr() : bus(Bus::Undef), iface("") {};
159
160 virtual ~NavAddr() = default;
161
162 virtual std::string to_string() const {
163 return NavAddr::BusToString(bus) + " " + iface;
164 }
165 static std::string BusToString(Bus b);
166 static Bus StringToBus(const std::string& s);
167
168 Bus bus;
169 const std::string iface;
171};
172
173class NavAddr0183 : public NavAddr {
174public:
175 NavAddr0183(const std::string iface) : NavAddr(NavAddr::Bus::N0183, iface) {};
176
177 // An empty, illegal N0183 address
178 NavAddr0183() : NavAddr() {}
179
180 std::string to_string() const { return iface; }
181};
182
183class NavAddr2000 : public NavAddr {
184public:
185 NavAddr2000(const std::string& iface, const N2kName& _name)
186 : NavAddr(NavAddr::Bus::N2000, iface), name(_name) {};
187
188 NavAddr2000(const std::string& iface, unsigned char _address)
189 : NavAddr(NavAddr::Bus::N2000, iface), name(0), address(_address) {};
190
191 // An empty, illegal N2000 address
192 NavAddr2000() : NavAddr() {}
193
194 std::string to_string() const { return name.to_string(); }
195
196 const N2kName name;
197 unsigned char address;
198};
199
200class NavAddrPlugin : public NavAddr {
201public:
202 const std::string id;
203 NavAddrPlugin(const std::string& _id)
204 : NavAddr(NavAddr::Bus::Plugin, "Internal"), id(_id) {}
205};
206
207class NavAddrSignalK : public NavAddr {
208public:
209 NavAddrSignalK(std::string iface) : NavAddr(NavAddr::Bus::Signalk, iface) {};
210
211 std::string to_string() const { return NavAddr::to_string(); }
212};
213
214class NavAddrTest : public NavAddr {
215public:
216 NavAddrTest(std::string output_path)
217 : NavAddr(NavAddr::Bus::TestBus, "Test"), name(output_path) {};
218
219 const std::string name;
220};
221
223class NavMsg : public KeyProvider {
224public:
225 NavMsg() = delete;
226
228 virtual std::string key() const = 0;
229
231 virtual std::string to_string() const { return key(); }
232
237 virtual std::string to_vdr() const { return to_string(); }
238
240 std::string GetKey() const { return key(); }
241
242 const NavAddr::Bus bus;
243
248 std::shared_ptr<const NavAddr> source;
249
250protected:
251 NavMsg(const NavAddr::Bus& _bus, std::shared_ptr<const NavAddr> src)
252 : bus(_bus), source(src) {};
253};
254
258class Nmea2000Msg : public NavMsg {
259public:
260 Nmea2000Msg(const uint64_t _pgn)
261 : NavMsg(NavAddr::Bus::N2000, std::make_shared<NavAddr>()), PGN(_pgn) {}
262
263 Nmea2000Msg(const uint64_t _pgn, std::shared_ptr<const NavAddr2000> src)
264 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn) {}
265
266 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
267 std::shared_ptr<const NavAddr2000> src)
268 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn), payload(_payload) {}
269
270 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
271 std::shared_ptr<const NavAddr2000> src, int _priority)
272 : NavMsg(NavAddr::Bus::N2000, src),
273 PGN(_pgn),
274 payload(_payload),
275 priority(_priority) {}
276
277 virtual ~Nmea2000Msg() = default;
278
279 std::string key() const override {
280 return std::string("n2000-") + PGN.to_string();
281 };
282
284 std::string to_string() const override;
285
286 std::string to_vdr() const override;
287
288 N2kPGN PGN; // For TX message, unparsed
289 std::vector<unsigned char> payload;
290 int priority;
291};
292
294class Nmea0183Msg : public NavMsg {
295public:
296 Nmea0183Msg(const std::string& id, const std::string& _payload,
297 std::shared_ptr<const NavAddr> src)
298 : NavMsg(NavAddr::Bus::N0183, src),
299 talker(id.substr(0, 2)),
300 type(id.substr(2)),
301 payload(_payload) {}
302
304 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
305
306 Nmea0183Msg(const std::string& id)
307 : Nmea0183Msg(id.size() <= 3 ? std::string("??") + id : id, "",
308 std::make_shared<const NavAddr>()) {}
309
310 Nmea0183Msg(const Nmea0183Msg& other, const std::string& t)
311 : NavMsg(NavAddr::Bus::N0183, other.source),
312 talker(other.talker),
313 type(t),
314 payload(other.payload) {}
315
316 virtual ~Nmea0183Msg() = default;
317
318 std::string key() const { return Nmea0183Msg::MessageKey(type.c_str()); };
319
320 std::string to_string() const override;
321
322 std::string to_vdr() const override;
323
325 static std::string MessageKey(const char* type = "ALL") {
326 static const char* const prefix = "n0183-";
327 return std::string(prefix) + type;
328 }
329
330 const std::string talker;
331 const std::string type;
332 const std::string payload;
333};
334
336class PluginMsg : public NavMsg {
337public:
338 PluginMsg()
339 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
340
341 PluginMsg(const std::string& _name, const std::string& _dest_host,
342 const std::string& msg)
343 : NavMsg(NavAddr::Bus::Plugin, std::make_shared<const NavAddr>(
344 NavAddr::Bus::Plugin, "Internal")),
345 name(_name),
346 message(msg),
347 dest_host(_dest_host) {}
348
349 PluginMsg(const std::string& _name, const std::string& msg)
350 : PluginMsg(_name, "localhost", msg) {}
351
352 virtual ~PluginMsg() = default;
353
354 std::string key() const { return std::string("plug.json-") + name; };
355
356 std::string to_string() const;
357
358 const std::string name;
359 const std::string message;
360 const std::string dest_host;
361};
362
364class SignalkMsg : public NavMsg {
365public:
366 SignalkMsg()
367 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
368
369 SignalkMsg(std::string _context_self, std::string _context,
370 std::string _raw_message, std::string _iface)
371 : NavMsg(NavAddr::Bus::Signalk,
372 std::make_shared<const NavAddr>(NavAddr::Bus::Signalk, _iface)),
373 context_self(_context_self),
374 context(_context),
375 raw_message(_raw_message) {};
376
377 virtual ~SignalkMsg() = default;
378
379 std::string key() const { return std::string("signalK"); };
380
381 std::string to_string() const { return raw_message; }
382
383 struct in_addr dest;
384 struct in_addr src;
385 std::string context_self;
386 std::string context;
387 std::string raw_message;
388};
389
391class NullNavMsg : public NavMsg {
392public:
393 NullNavMsg()
394 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
395
396 virtual ~NullNavMsg() = default;
397
398 std::string key() const { return "navmsg-undef"; }
399};
400
401#endif // DRIVER_NAVMSG_H
Interface implemented by classes which listens.
Definition observable.h:55
Where messages are sent to or received from.
const std::string iface
Physical device for 0183, else a unique string.
Actual data sent between application and transport layer.
std::string GetKey() const
Alias for key().
virtual std::string to_vdr() const
Return message in unquoted format used by VDR plugin, see https://opencpn-manuals....
virtual std::string key() const =0
Return unique key used by observable to notify/listen.
std::shared_ptr< const NavAddr > source
Source address is set by drivers when receiving, unused and should be empty when sending.
virtual std::string to_string() const
Return printable string for logging etc without trailing nl.
A regular Nmea0183 message.
std::string key() const
Return unique key used by observable to notify/listen.
const std::string type
For example 'GGA'.
const std::string talker
For example 'GP'.
const std::string payload
Complete NMEA0183 sentence, also prefix.
static std::string MessageKey(const char *type="ALL")
Return key which should be used to listen to given message type.
std::string to_vdr() const override
Return message in unquoted format used by VDR plugin, see https://opencpn-manuals....
std::string to_string() const override
Return printable string for logging etc without trailing nl.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
std::string key() const override
Return unique key used by observable to notify/listen.
std::string to_string() const override
Print "bus key id payload".
std::string to_vdr() const override
Return message in unquoted format used by VDR plugin, see https://opencpn-manuals....
An invalid message, error return value.
std::string key() const
Return unique key used by observable to notify/listen.
A plugin to plugin json message over the REST interface.
std::string to_string() const
Return printable string for logging etc without trailing nl.
std::string key() const
Return unique key used by observable to notify/listen.
const std::string dest_host
hostname, ip address or 'localhost'
Plugin ABI encapsulation.
A parsed SignalK message over ipv4.
std::string key() const
Return unique key used by observable to notify/listen.
std::string to_string() const
Return printable string for logging etc without trailing nl.
N2k uses CAN which defines the basic properties of messages.
Definition comm_navmsg.h:66
uint32_t GetNumber() const
21 bits
uint16_t GetManufacturer() const
9 bits
uint8_t GetDevClass() const
7 bits
uint8_t GetDevFunc() const
8 bits
uint8_t GetIndustryGroup() const
4 bits
uint8_t GetDevInstanceHigh() const
5 bits
uint8_t GetDevInstanceLow() const
3 bits
uint8_t GetSysInstance() const
4 bits