OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_navmsg.h
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: navmsg -- Raw, undecoded messages definitions.
5 * Author: David Register, Alec Leamas
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David Register, Alec Leamas *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26#ifndef _DRIVER_NAVMSG_H
27#define _DRIVER_NAVMSG_H
28
29#include <memory>
30#include <sstream>
31#include <vector>
32#include <string>
33
34#ifndef _MSC_VER
35#include <netinet/in.h>
36#endif
37
38#include "observable.h"
39
41struct N2kPGN {
42 uint64_t pgn;
43
44 N2kPGN(uint64_t _pgn) { pgn = _pgn; }
45
46 std::string to_string() const {
47 std::stringstream ss;
48 ss << pgn;
49 return ss.str();
50 }
51};
52
62struct N2kName {
63 N2kName(){};
64 N2kName(uint64_t name) { value.Name = name; }
65
66 std::string to_string() const {
67 std::stringstream ss;
68 ss << value.Name;
69 return ss.str();
70 }
71
72 static uint64_t Parse(const std::string& s) {
73 std::stringstream ss;
74 uint64_t id;
75 ss << s;
76 ss >> id;
77 return id;
78 }
79
80 uint32_t GetNumber() const;
81 uint16_t GetManufacturer() const;
82 uint8_t GetDevInstanceLow() const;
83 uint8_t GetDevInstanceHigh() const;
84 uint8_t GetDevFunc() const;
85 uint8_t GetDevClass() const;
86 uint8_t GetSysInstance() const;
87 uint8_t GetIndustryGroup() const;
89 typedef union {
90 uint64_t Name;
91 struct {
92 uint32_t UnicNumberAndManCode; // ManufacturerCode 11 bits , UniqueNumber
93 // 21 bits
94 unsigned char DeviceInstance;
95 unsigned char DeviceFunction;
96 unsigned char DeviceClass;
97 unsigned char IndustryGroupAndSystemInstance; // 4 bits each
98 };
100
102
103 void SetUniqueNumber(uint32_t _UniqueNumber) {
104 value.UnicNumberAndManCode =
105 (value.UnicNumberAndManCode & 0xffe00000) | (_UniqueNumber & 0x1fffff);
106 }
107 void SetManufacturerCode(uint16_t _ManufacturerCode) {
108 value.UnicNumberAndManCode =
109 (value.UnicNumberAndManCode & 0x1fffff) |
110 (((unsigned long)(_ManufacturerCode & 0x7ff)) << 21);
111 }
112 void SetDeviceInstance(unsigned char _DeviceInstance) {
113 value.DeviceInstance = _DeviceInstance;
114 }
115 void SetDeviceFunction(unsigned char _DeviceFunction) {
116 value.DeviceFunction = _DeviceFunction;
117 }
118 void SetDeviceClass(unsigned char _DeviceClass) {
119 value.DeviceClass = ((_DeviceClass & 0x7f) << 1);
120 }
121 void SetIndustryGroup(unsigned char _IndustryGroup) {
122 value.IndustryGroupAndSystemInstance =
123 (value.IndustryGroupAndSystemInstance & 0x0f) | (_IndustryGroup << 4) |
124 0x80;
125 }
126 void SetSystemInstance(unsigned char _SystemInstance) {
127 value.IndustryGroupAndSystemInstance =
128 (value.IndustryGroupAndSystemInstance & 0xf0) |
129 (_SystemInstance & 0x0f);
130 }
131
132 uint64_t GetName() const { return value.Name; }
133};
134
136class NavAddr {
137public:
138 enum class Bus { N0183, Signalk, N2000, Onenet, TestBus, Undef };
139
140 NavAddr(Bus b, const std::string& i) : bus(b), iface(i){};
141 NavAddr() : bus(Bus::Undef), iface(""){};
142
143 std::string to_string() const {
144 return NavAddr::BusToString(bus) + " " + iface;
145 }
146 static std::string BusToString(Bus b);
147 static Bus StringToBus(const std::string& s);
148
149 Bus bus;
150 const std::string iface;
152};
153
155class NavAddr0183 : public NavAddr {
156public:
157 NavAddr0183(const std::string iface) : NavAddr(NavAddr::Bus::N0183, iface){};
158
159 std::string to_string() const { return iface; }
160};
161
163class NavAddr2000 : public NavAddr {
164public:
165 NavAddr2000(const std::string& iface, const N2kName& _name)
166 : NavAddr(NavAddr::Bus::N2000, iface), name(_name){};
167
168 NavAddr2000(const std::string& iface, unsigned char _address)
169 : NavAddr(NavAddr::Bus::N2000, iface), name(0), address(_address){};
170
171 std::string to_string() const { return name.to_string(); }
172
173 const N2kName name;
174 unsigned char address;
175};
176
178class NavAddrSignalK : public NavAddr {
179public:
180 NavAddrSignalK() : NavAddr(NavAddr::Bus::Signalk, "signalK"){};
181};
182
184class NavAddrTest : public NavAddr {
185public:
186 NavAddrTest(std::string output_path)
187 : NavAddr(NavAddr::Bus::TestBus, "Test"), name(output_path){};
188
189 const std::string name;
190};
191
193class NavMsg : public KeyProvider {
194public:
195 NavMsg() = delete;
196
197 virtual std::string key() const = 0;
198
199 virtual std::string to_string() const {
200 return NavAddr::BusToString(bus) + " " + key();
201 }
202
203 std::string GetKey() const { return key(); }
204
205 const NavAddr::Bus bus;
206
207 std::shared_ptr<const NavAddr> source;
208
209protected:
210 NavMsg(const NavAddr::Bus& _bus, std::shared_ptr<const NavAddr> src)
211 : bus(_bus), source(src){};
212};
213
217class Nmea2000Msg : public NavMsg {
218public:
219 Nmea2000Msg(const uint64_t _pgn)
220 : NavMsg(NavAddr::Bus::N2000, std::make_shared<NavAddr>()), PGN(_pgn) {}
221
222 Nmea2000Msg(const uint64_t _pgn, std::shared_ptr<const NavAddr> src)
223 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn) {}
224
225 Nmea2000Msg(const uint64_t _pgn, const std::vector<unsigned char>& _payload,
226 std::shared_ptr<const NavAddr> src)
227 : NavMsg(NavAddr::Bus::N2000, src), PGN(_pgn), payload(_payload) {}
228
229 virtual ~Nmea2000Msg() = default;
230
231 std::string key() const { return std::string("n2000-") + PGN.to_string(); };
232
234 std::string to_string() const;
235
236 N2kPGN PGN; // For TX message, unparsed
237 std::vector<unsigned char> payload;
238};
239
241class Nmea0183Msg : public NavMsg {
242public:
243 Nmea0183Msg(const std::string& id, const std::string& _payload,
244 std::shared_ptr<const NavAddr> src)
245 : NavMsg(NavAddr::Bus::N0183, src),
246 talker(id.substr(0, 2)),
247 type(id.substr(2)),
248 payload(_payload) {}
249
251 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
252
253 Nmea0183Msg(const std::string& id)
254 : Nmea0183Msg(id.size() <= 3 ? std::string("??") + id : id, "",
255 std::make_shared<const NavAddr>()) {}
256
257 Nmea0183Msg(const Nmea0183Msg& other, const std::string& t)
258 : NavMsg(NavAddr::Bus::N0183, other.source),
259 talker(other.talker),
260 type(t),
261 payload(other.payload) {}
262
263 virtual ~Nmea0183Msg() = default;
264
265 std::string key() const { return Nmea0183Msg::MessageKey(type.c_str()); };
266
267 std::string to_string() const {
268 return NavMsg::to_string() + " " + talker + type + " " + payload;
269 }
270
272 static std::string MessageKey(const char* type = "ALL") {
273 static const char* const prefix = "n0183-";
274 return std::string(prefix) + type;
275 }
276
277 const std::string talker;
278 const std::string type;
279 const std::string payload;
280};
281
283class SignalkMsg : public NavMsg {
284public:
285 SignalkMsg()
286 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
287
288 SignalkMsg(std::string _context_self, std::string _context,
289 std::string _raw_message)
290 : NavMsg(NavAddr::Bus::Signalk,
291 std::make_shared<const NavAddr>(NavAddr::Bus::Signalk, "")),
292 context_self(_context_self),
293 context(_context),
294 raw_message(_raw_message){};
295
296 virtual ~SignalkMsg() = default;
297
298 struct in_addr dest;
299 struct in_addr src;
300 std::string context_self;
301 std::string context;
302 std::string raw_message;
303
304 std::vector<std::string> errors;
305 std::vector<std::string> warnings;
306 std::string key() const { return std::string("signalK"); };
307};
308
310class NullNavMsg : public NavMsg {
311public:
312 NullNavMsg()
313 : NavMsg(NavAddr::Bus::Undef, std::make_shared<const NavAddr>()) {}
314
315 virtual ~NullNavMsg() = default;
316
317 std::string key() const { return "navmsg-undef"; }
318};
319
320#endif // DRIVER_NAVMSG_H
Interface implemented by classes which listens.
Definition: observable.h:54
NMEA0183 sent/received address, an interface.
Definition: comm_navmsg.h:155
NMEA2000 sent/received address, an address (possibly wildcard)
Definition: comm_navmsg.h:163
There is only support for a single signalK bus.
Definition: comm_navmsg.h:178
Dummy test address.
Definition: comm_navmsg.h:184
Where messages are sent to or received from.
Definition: comm_navmsg.h:136
const std::string iface
Physical device for 0183, else a unique string.
Definition: comm_navmsg.h:150
Actual data sent between application and transport layer.
Definition: comm_navmsg.h:193
std::string GetKey() const
Return key used to listen and notify.
Definition: comm_navmsg.h:203
A regular Nmea0183 message.
Definition: comm_navmsg.h:241
const std::string type
For example 'GGA'.
Definition: comm_navmsg.h:278
const std::string talker
For example 'GP'.
Definition: comm_navmsg.h:277
const std::string payload
Complete NMEA0183 sentence, also prefix.
Definition: comm_navmsg.h:279
static std::string MessageKey(const char *type="ALL")
Return key which should be used to listen to given message type.
Definition: comm_navmsg.h:272
See: https://github.com/OpenCPN/OpenCPN/issues/2729#issuecomment-1179506343.
Definition: comm_navmsg.h:217
std::string to_string() const
Print "bus key id payload".
Definition: comm_navmsg.cpp:79
An invalid message, error return value.
Definition: comm_navmsg.h:310
A parsed, raw SignalK message.
Definition: comm_navmsg.h:283
N2k uses CAN which defines the basic properties of messages.
Definition: comm_navmsg.h:62
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
NMEA2000 message discriminator.
Definition: comm_navmsg.h:41