OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_navmsg.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 David Register *
3 * Copyright (C) 2022 - 2024 Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 **************************************************************************/
20
26// For compilers that support precompilation, includes "wx.h".
27#include <wx/wxprec.h>
28
29#ifndef WX_PRECOMP
30#include <wx/wx.h>
31#endif // precompiled headers
32
33#include <algorithm>
34#include <string>
35#include <iomanip>
36
37#include "model/comm_driver.h"
38#include "model/ocpn_utils.h"
39
40std::string NavAddr::BusToString(NavAddr::Bus b) {
41 switch (b) {
42 case NavAddr::Bus::N0183:
43 return "nmea0183";
44 break;
45 case NavAddr::Bus::N2000:
46 return "nmea2000";
47 break;
48 case NavAddr::Bus::Signalk:
49 return "SignalK";
50 break;
51 case NavAddr::Bus::Onenet:
52 return "Onenet";
53 break;
54 case NavAddr::Bus::Plugin:
55 return "Plugin";
56 break;
57 case NavAddr::Bus::TestBus:
58 return "TestBus";
59 break;
60 case NavAddr::Bus::AppMsg:
61 return "AppMsg";
62 break;
63 case NavAddr::Bus::Undef:
64 return "??";
65 break;
66 }
67 return "????";
68}
69
70NavAddr::Bus NavAddr::StringToBus(const std::string& s) {
71 if (s == "nmea0183") return NavAddr::Bus::N0183;
72 if (s == "nmea2000") return NavAddr::Bus::N2000;
73 if (s == "SignalK") return NavAddr::Bus::Signalk;
74 if (s == "Onenet") return NavAddr::Bus::Onenet;
75 if (s == "Plugin") return NavAddr::Bus::Plugin;
76 if (s == "TestBus") return NavAddr::Bus::TestBus;
77 if (s == "AppMsg") return NavAddr::Bus::AppMsg;
78 return NavAddr::Bus::Undef;
79}
80
81static std::string CharToString(unsigned char c) {
82 using namespace std;
83 stringstream ss;
84 ss << setfill('0') << hex << setw(2) << (c & 0x00ff);
85 return ss.str();
86}
87
88static std::string CharToString(char c) {
89 return CharToString(static_cast<unsigned char>(c));
90}
91
92std::string Nmea2000Msg::to_string() const {
93 // Arrange to "pretty-print" the N2K message payload
94
95 // extract PGN from payload
96 uint64_t pgn = 0;
97 unsigned char* c = (unsigned char*)&pgn;
98 *c++ = payload.at(3);
99 *c++ = payload.at(4);
100 *c++ = payload.at(5);
101
102 std::string st;
103 size_t data_start = 12;
104 if (payload.at(0) == 0x94) data_start = 7;
105 size_t i = 0;
106 while (i < payload.size() - 1) {
107 if (i > data_start) {
108 for (int j = 0; j < 8; j++) {
109 st.append(CharToString(payload[i]));
110 st.append(" ");
111 i++;
112 if (i >= payload.size() - 1) {
113 break;
114 }
115 }
116 st.append(" ");
117 } else
118 i++;
119 }
120
121 std::stringstream ss1;
122 std::string spgn = " ";
123 if (PGN.pgn == 1) spgn = "ALL ";
124 ss1 << "n2000-" << spgn << "PGN: " << pgn << " [ " << st << " ]";
125 return ss1.str();
126}
127
128std::string Nmea2000Msg::to_vdr() const {
129 std::string s;
130 for (auto c : payload) s.append(CharToString(c) + " ");
131 return s;
132}
133
134std::string Nmea0183Msg::to_string() const {
135 std::stringstream ss;
136 ss << key() << " " << talker << type << " " << ocpn::printable(payload);
137 return ss.str();
138}
139
140std::string Nmea0183Msg::to_vdr() const { return ocpn::printable(payload); }
141
142std::string PluginMsg::to_string() const {
144 if (name == "position-fix") return name + ":" + ocpn::rtrim(message);
145
146 std::stringstream ss;
147 for (char c : ocpn::rtrim(message)) {
148 if (c >= ' ' && c <= '~')
149 ss << c;
150 else
151 ss << CharToString(c);
152 }
153 return name + ": " + ss.str();
154}
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.
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.
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....
std::string to_string() const
Return printable string for logging etc without trailing nl.
Communication driver layer.
std::string printable(const std::string &str)
Return copy of str with non-printable chars replaced by hex like "<0D>".
std::string rtrim(const std::string &arg)
Strip possibly trailing space characters in s.
Miscellaneous utilities, many of which string related.