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 <chrono>
29#include <memory>
30#include <sstream>
31#include <vector>
32#include <string>
33
34#ifdef _MSC_VER
35#include <winsock2.h>
36#else
37#include <netinet/in.h>
38#endif
39
40#include "observable.h"
41
42using NavmsgTimePoint = std::chrono::time_point<std::chrono::steady_clock>;
43
44struct N2kPGN {
45 uint64_t pgn;
46
47 N2kPGN(uint64_t _pgn) { pgn = _pgn; }
48
49 friend bool operator<(const N2kPGN& lhs, const N2kPGN& rhs) {
50 return lhs.pgn < rhs.pgn;
51 }
52
53 std::string to_string() const {
54 std::stringstream ss;
55 ss << pgn;
56 return ss.str();
57 }
58};
59
69struct N2kName {
70 N2kName() {};
71 N2kName(uint64_t name) { value.Name = name; }
72
73 friend bool operator<(const N2kName& lhs, const N2kName& rhs) {
74 return lhs.value.Name < rhs.value.Name;
75 }
76
77 std::string to_string() const {
78 std::stringstream ss;
79 ss << value.Name;
80 return ss.str();
81 }
82
83 static uint64_t Parse(const std::string& s) {
84 std::stringstream ss;
85 uint64_t id;
86 ss << s;
87 ss >> id;
88 return id;
89 }
90
91 uint32_t GetNumber() const;
92 uint16_t GetManufacturer() const;
93 uint8_t GetDevInstanceLow() const;
94 uint8_t GetDevInstanceHigh() const;
95 uint8_t GetDevFunc() const;
96 uint8_t GetDevClass() const;
97 uint8_t GetSysInstance() const;
98 uint8_t GetIndustryGroup() const;
100 typedef union {
101 uint64_t Name;
102 struct {
103 uint32_t UnicNumberAndManCode; // ManufacturerCode 11 bits , UniqueNumber
104 // 21 bits
105 unsigned char DeviceInstance;
106 unsigned char DeviceFunction;
107 unsigned char DeviceClass;
108 unsigned char IndustryGroupAndSystemInstance; // 4 bits each
109 };
111
113
114 void SetUniqueNumber(uint32_t _UniqueNumber) {
115 value.UnicNumberAndManCode =
116 (value.UnicNumberAndManCode & 0xffe00000) | (_UniqueNumber & 0x1fffff);
117 }
118 void SetManufacturerCode(uint16_t _ManufacturerCode) {
119 value.UnicNumberAndManCode =
120 (value.UnicNumberAndManCode & 0x1fffff) |
121 (((unsigned long)(_ManufacturerCode & 0x7ff)) << 21);
122 }
123 void SetDeviceInstance(unsigned char _DeviceInstance) {
124 value.DeviceInstance = _DeviceInstance;
125 }
126 void SetDeviceFunction(unsigned char _DeviceFunction) {
127 value.DeviceFunction = _DeviceFunction;
128 }
129 void SetDeviceClass(unsigned char _DeviceClass) {
130 value.DeviceClass = ((_DeviceClass & 0x7f) << 1);
131 }
132 void SetIndustryGroup(unsigned char _IndustryGroup) {
133 value.IndustryGroupAndSystemInstance =
134 (value.IndustryGroupAndSystemInstance & 0x0f) | (_IndustryGroup << 4) |
135 0x80;
136 }
137 void SetSystemInstance(unsigned char _SystemInstance) {
138 value.IndustryGroupAndSystemInstance =
139 (value.IndustryGroupAndSystemInstance & 0xf0) |
140 (_SystemInstance & 0x0f);
141 }
142
143 uint64_t GetName() const { return value.Name; }
144};
145
147class NavAddr {
148public:
149 enum class Bus {
150 N0183,
151 Signalk,
152 N2000,
153 Onenet,
154 Plugin,
155 TestBus,
156 AppMsg,
157 Undef
158 };
159
160 NavAddr(Bus b, const std::string& i) : bus(b), iface(i) {};
161 NavAddr() : bus(Bus::Undef), iface("") {};
162
163 virtual ~NavAddr() = default;
164
165 virtual std::string to_string() const {
166 return NavAddr::BusToString(bus) + " " + iface;
167 }
168 static std::string BusToString(Bus b);
169 static Bus StringToBus(const std::string& s);
170
171 Bus bus;
172 const std::string iface;
174};
175
176class NavAddr0183 : public NavAddr {
177public:
178 NavAddr0183(const std::string iface) : NavAddr(NavAddr::Bus::N0183, iface) {};
179
180 // An empty, illegal N0183 address
181 NavAddr0183() : NavAddr() {}
182
183 std::string to_string() const { return iface; }
184};
185
186class NavAddr2000 : public NavAddr {
187public:
188 NavAddr2000(const std::string& iface, const N2kName& _name)
189 : NavAddr(NavAddr::Bus::N2000, iface), name(_name) {};
190
191 NavAddr2000(const std::string& iface, unsigned char _address)
192 : NavAddr(NavAddr::Bus::N2000, iface), name(0), address(_address) {};
193
194 // An empty, illegal N2000 address
195 NavAddr2000() : NavAddr() {}
196
197 std::string to_string() const { return name.to_string(); }
198
199 const N2kName name;
200 unsigned char address;
201};
202
203class NavAddrPlugin : public NavAddr {
204public:
205 const std::string id;
206 NavAddrPlugin(const std::string& _id)
207 : NavAddr(NavAddr::Bus::Plugin, "Internal"), id(_id) {}
208};
209
210class NavAddrSignalK : public NavAddr {
211public:
212 NavAddrSignalK(std::string iface) : NavAddr(NavAddr::Bus::Signalk, iface) {};
213
214 std::string to_string() const { return NavAddr::to_string(); }
215};
216
217class NavAddrTest : public NavAddr {
218public:
219 NavAddrTest(std::string output_path)
220 : NavAddr(NavAddr::Bus::TestBus, "Test"), name(output_path) {};
221
222 const std::string name;
223};
224
226class NavMsg : public KeyProvider {
227public:
228 NavMsg() = delete;
229 virtual ~NavMsg() = default;
230
232 virtual std::string key() const = 0;
233
235 virtual std::string to_string() const { return key(); }
236
241 virtual std::string to_vdr() const { return to_string(); }
242
244 std::string GetKey() const { return key(); }
245
246 const NavAddr::Bus bus;
247
252 std::shared_ptr<const NavAddr> source;
253
254 const NavmsgTimePoint created_at;
255
256protected:
257 NavMsg(const NavAddr::Bus& _bus, std::shared_ptr<const NavAddr> src)
258 : bus(_bus), source(src), created_at(std::chrono::steady_clock::now()) {};
259};
260
264class Nmea2000Msg : public NavMsg {
265public:
266 Nmea2000Msg(const uint64_t _pgn)
267 : NavMsg(NavAddr::Bus::N2000, std::make_shared<NavAddr>()), PGN(_pgn) {}
268
269 Nmea2000Msg(const uint64_t _pgn, std::shared_ptr<const NavAddr2000> src)
270 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn) {}
271
272 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
273 std::shared_ptr<const NavAddr2000> src)
274 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn), payload(_payload) {}
275
276 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
277 std::shared_ptr<const NavAddr2000> src, int _priority)
278 : NavMsg(NavAddr::Bus::N2000, src),
279 PGN(_pgn),
280 payload(_payload),
281 priority(_priority) {}
282
283 virtual ~Nmea2000Msg() = default;
284
285 std::string key() const override {
286 return std::string("n2000-") + PGN.to_string();
287 };
288
290 std::string to_string() const override;
291
292 std::string to_vdr() const override;
293
294 N2kPGN PGN; // For TX message, unparsed
295 std::vector<unsigned char> payload;
296 int priority;
297};
298
300class Nmea0183Msg : public NavMsg {
301public:
302 Nmea0183Msg(const std::string& id, const std::string& _payload,
303 std::shared_ptr<const NavAddr> src)
304 : NavMsg(NavAddr::Bus::N0183, src),
305 talker(id.substr(0, 2)),
306 type(id.substr(2)),
307 payload(_payload) {}
308
310 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
311
312 Nmea0183Msg(const std::string& id)
313 : Nmea0183Msg(id.size() <= 3 ? std::string("??") + id : id, "",
314 std::make_shared<const NavAddr>()) {}
315
316 Nmea0183Msg(const Nmea0183Msg& other, const std::string& t)
317 : NavMsg(NavAddr::Bus::N0183, other.source),
318 talker(other.talker),
319 type(t),
320 payload(other.payload) {}
321
322 virtual ~Nmea0183Msg() = default;
323
324 std::string key() const override {
325 return Nmea0183Msg::MessageKey(type.c_str());
326 };
327
328 std::string to_string() const override;
329
330 std::string to_vdr() const override;
331
333 static std::string MessageKey(const char* type = "ALL") {
334 static const char* const prefix = "n0183-";
335 return std::string(prefix) + type;
336 }
337
338 const std::string talker;
339 const std::string type;
340 const std::string payload;
341};
342
344class PluginMsg : public NavMsg {
345public:
346 PluginMsg()
347 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
348
349 PluginMsg(const std::string& _name, const std::string& _dest_host,
350 const std::string& msg)
351 : NavMsg(NavAddr::Bus::Plugin, std::make_shared<const NavAddr>(
352 NavAddr::Bus::Plugin, "Internal")),
353 name(_name),
354 message(msg),
355 dest_host(_dest_host) {}
356
357 PluginMsg(const std::string& _name, const std::string& msg)
358 : PluginMsg(_name, "localhost", msg) {}
359
360 virtual ~PluginMsg() = default;
361
362 std::string key() const { return std::string("plug.json-") + name; };
363
364 std::string to_string() const;
365
366 const std::string name;
367 const std::string message;
368 const std::string dest_host;
369};
370
372class SignalkMsg : public NavMsg {
373public:
374 SignalkMsg()
375 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
376
377 SignalkMsg(std::string _context_self, std::string _context,
378 std::string _raw_message, std::string _iface)
379 : NavMsg(NavAddr::Bus::Signalk,
380 std::make_shared<const NavAddr>(NavAddr::Bus::Signalk, _iface)),
381 context_self(_context_self),
382 context(_context),
383 raw_message(_raw_message) {};
384
385 virtual ~SignalkMsg() = default;
386
387 std::string key() const { return std::string("signalK"); };
388
389 std::string to_string() const { return raw_message; }
390
391 struct in_addr dest;
392 struct in_addr src;
393 std::string context_self;
394 std::string context;
395 std::string raw_message;
396};
397
399class NullNavMsg : public NavMsg {
400public:
401 NullNavMsg()
402 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
403
404 virtual ~NullNavMsg() = default;
405
406 std::string key() const { return "navmsg-undef"; }
407};
408
409#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.
const std::string type
For example 'GGA'.
std::string key() const override
Return unique key used by observable to notify/listen.
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:69
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