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 { N0183, Signalk, N2000, Onenet, Plugin, TestBus, Undef };
147
148 NavAddr(Bus b, const std::string& i) : bus(b), iface(i) {};
149 NavAddr() : bus(Bus::Undef), iface("") {};
150
151 std::string to_string() const {
152 return NavAddr::BusToString(bus) + " " + iface;
153 }
154 static std::string BusToString(Bus b);
155 static Bus StringToBus(const std::string& s);
156
157 Bus bus;
158 const std::string iface;
160};
161
162class NavAddr0183 : public NavAddr {
163public:
164 NavAddr0183(const std::string iface) : NavAddr(NavAddr::Bus::N0183, iface) {};
165
166 // An empty, illegal N0183 address
167 NavAddr0183() : NavAddr() {}
168
169 std::string to_string() const { return iface; }
170};
171
172class NavAddr2000 : public NavAddr {
173public:
174 NavAddr2000(const std::string& iface, const N2kName& _name)
175 : NavAddr(NavAddr::Bus::N2000, iface), name(_name) {};
176
177 NavAddr2000(const std::string& iface, unsigned char _address)
178 : NavAddr(NavAddr::Bus::N2000, iface), name(0), address(_address) {};
179
180 // An empty, illegal N2000 address
181 NavAddr2000() : NavAddr() {}
182
183 std::string to_string() const { return name.to_string(); }
184
185 const N2kName name;
186 unsigned char address;
187};
188
189class NavAddrPlugin : public NavAddr {
190public:
191 const std::string id;
192 NavAddrPlugin(const std::string& _id)
193 : NavAddr(NavAddr::Bus::Plugin, "Plugin"), id(_id) {}
194};
195
196class NavAddrSignalK : public NavAddr {
197public:
198 NavAddrSignalK(std::string iface) : NavAddr(NavAddr::Bus::Signalk, iface) {};
199
200 std::string to_string() const { return NavAddr::to_string(); }
201};
202
203class NavAddrTest : public NavAddr {
204public:
205 NavAddrTest(std::string output_path)
206 : NavAddr(NavAddr::Bus::TestBus, "Test"), name(output_path) {};
207
208 const std::string name;
209};
210
212class NavMsg : public KeyProvider {
213public:
214 NavMsg() = delete;
215
217 virtual std::string key() const = 0;
218
220 virtual std::string to_string() const {
221 return NavAddr::BusToString(bus) + " " + key();
222 }
223
225 std::string GetKey() const { return key(); }
226
227 const NavAddr::Bus bus;
228
233 std::shared_ptr<const NavAddr> source;
234
235protected:
236 NavMsg(const NavAddr::Bus& _bus, std::shared_ptr<const NavAddr> src)
237 : bus(_bus), source(src) {};
238};
239
243class Nmea2000Msg : public NavMsg {
244public:
245 Nmea2000Msg(const uint64_t _pgn)
246 : NavMsg(NavAddr::Bus::N2000, std::make_shared<NavAddr>()), PGN(_pgn) {}
247
248 Nmea2000Msg(const uint64_t _pgn, std::shared_ptr<const NavAddr2000> src)
249 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn) {}
250
251 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
252 std::shared_ptr<const NavAddr2000> src)
253 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn), payload(_payload) {}
254
255 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
256 std::shared_ptr<const NavAddr2000> src, int _priority)
257 : NavMsg(NavAddr::Bus::N2000, src),
258 PGN(_pgn),
259 payload(_payload),
260 priority(_priority) {}
261
262 virtual ~Nmea2000Msg() = default;
263
264 std::string key() const { return std::string("n2000-") + PGN.to_string(); };
265
267 std::string to_string() const;
268
269 N2kPGN PGN; // For TX message, unparsed
270 std::vector<unsigned char> payload;
271 int priority;
272};
273
275class Nmea0183Msg : public NavMsg {
276public:
277 Nmea0183Msg(const std::string& id, const std::string& _payload,
278 std::shared_ptr<const NavAddr> src)
279 : NavMsg(NavAddr::Bus::N0183, src),
280 talker(id.substr(0, 2)),
281 type(id.substr(2)),
282 payload(_payload) {}
283
285 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
286
287 Nmea0183Msg(const std::string& id)
288 : Nmea0183Msg(id.size() <= 3 ? std::string("??") + id : id, "",
289 std::make_shared<const NavAddr>()) {}
290
291 Nmea0183Msg(const Nmea0183Msg& other, const std::string& t)
292 : NavMsg(NavAddr::Bus::N0183, other.source),
293 talker(other.talker),
294 type(t),
295 payload(other.payload) {}
296
297 virtual ~Nmea0183Msg() = default;
298
299 std::string key() const { return Nmea0183Msg::MessageKey(type.c_str()); };
300
301 std::string to_string() const;
302
304 static std::string MessageKey(const char* type = "ALL") {
305 static const char* const prefix = "n0183-";
306 return std::string(prefix) + type;
307 }
308
309 const std::string talker;
310 const std::string type;
311 const std::string payload;
312};
313
315class PluginMsg : public NavMsg {
316public:
317 PluginMsg()
318 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
319
320 PluginMsg(const std::string& _name, const std::string& _dest_host,
321 const std::string& msg)
322 : NavMsg(NavAddr::Bus::Plugin,
323 std::make_shared<const NavAddr>(NavAddr::Bus::Plugin, "")),
324 name(_name),
325 message(msg),
326 dest_host(_dest_host) {}
327
328 PluginMsg(const std::string& _name, const std::string& msg)
329 : PluginMsg(_name, "localhost", msg) {}
330
331 virtual ~PluginMsg() = default;
332
333 const std::string name;
334 const std::string message;
335 const std::string dest_host;
336
337 std::string key() const { return std::string("plug.json-") + name; };
338
339 std::string to_string() const { return name + ": " + message; }
340};
341
343class SignalkMsg : public NavMsg {
344public:
345 SignalkMsg()
346 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
347
348 SignalkMsg(std::string _context_self, std::string _context,
349 std::string _raw_message, std::string _iface)
350 : NavMsg(NavAddr::Bus::Signalk,
351 std::make_shared<const NavAddr>(NavAddr::Bus::Signalk, _iface)),
352 context_self(_context_self),
353 context(_context),
354 raw_message(_raw_message) {};
355
356 virtual ~SignalkMsg() = default;
357
358 struct in_addr dest;
359 struct in_addr src;
360 std::string context_self;
361 std::string context;
362 std::string raw_message;
363
364 std::string key() const { return std::string("signalK"); };
365};
366
368class NullNavMsg : public NavMsg {
369public:
370 NullNavMsg()
371 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
372
373 virtual ~NullNavMsg() = default;
374
375 std::string key() const { return "navmsg-undef"; }
376};
377
378#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 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_string() const
Return printable string for logging etc without trailing nl.
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
std::string to_string() const
Print "bus key id payload".
std::string key() const
Return unique key used by observable to notify/listen.
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.
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