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, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25#include <algorithm>
26#include <string>
27#include <iomanip>
28
29// For compilers that support precompilation, includes "wx.h".
30#include <wx/wxprec.h>
31#ifndef WX_PRECOMP
32#include <wx/wx.h>
33#endif
34
35#include "model/comm_driver.h"
36#include "model/ocpn_utils.h"
37
38std::string NavAddr::BusToString(NavAddr::Bus b) {
39 switch (b) {
40 case NavAddr::Bus::N0183:
41 return "nmea0183";
42 break;
43 case NavAddr::Bus::N2000:
44 return "nmea2000";
45 break;
46 case NavAddr::Bus::Signalk:
47 return "SignalK";
48 break;
49 case NavAddr::Bus::Onenet:
50 return "Onenet";
51 break;
52 case NavAddr::Bus::Plugin:
53 return "Plugin";
54 break;
55 case NavAddr::Bus::TestBus:
56 return "TestBus";
57 break;
58 case NavAddr::Bus::AppMsg:
59 return "AppMsg";
60 break;
61 case NavAddr::Bus::Undef:
62 return "??";
63 break;
64 }
65 return "????";
66}
67
68NavAddr::Bus NavAddr::StringToBus(const std::string& s) {
69 if (s == "nmea0183") return NavAddr::Bus::N0183;
70 if (s == "nmea2000") return NavAddr::Bus::N2000;
71 if (s == "SignalK") return NavAddr::Bus::Signalk;
72 if (s == "Onenet") return NavAddr::Bus::Onenet;
73 if (s == "Plugin") return NavAddr::Bus::Plugin;
74 if (s == "TestBus") return NavAddr::Bus::TestBus;
75 if (s == "AppMsg") return NavAddr::Bus::AppMsg;
76 return NavAddr::Bus::Undef;
77}
78
79static std::string CharToString(unsigned char c) {
80 using namespace std;
81 stringstream ss;
82 ss << setfill('0') << hex << setw(2) << (c & 0x00ff);
83 return ss.str();
84}
85
86static std::string CharToString(char c) {
87 return CharToString(static_cast<unsigned char>(c));
88}
89
90std::string Nmea2000Msg::to_string() const {
91 // Arrange to "pretty-print" the N2K message payload
92
93 // extract PGN from payload
94 uint64_t pgn = 0;
95 unsigned char* c = (unsigned char*)&pgn;
96 *c++ = payload.at(3);
97 *c++ = payload.at(4);
98 *c++ = payload.at(5);
99
100 std::string st;
101 size_t data_start = 12;
102 if (payload.at(0) == 0x94) data_start = 7;
103 size_t i = 0;
104 while (i < payload.size() - 1) {
105 if (i > data_start) {
106 for (int j = 0; j < 8; j++) {
107 st.append(CharToString(payload[i]));
108 st.append(" ");
109 i++;
110 if (i >= payload.size() - 1) {
111 break;
112 }
113 }
114 st.append(" ");
115 } else
116 i++;
117 }
118
119 std::stringstream ss1;
120 std::string spgn = " ";
121 if (PGN.pgn == 1) spgn = "ALL ";
122 ss1 << "n2000-" << spgn << "PGN: " << pgn << " [ " << st << " ]";
123 return ss1.str();
124}
125
126std::string Nmea2000Msg::to_vdr() const {
127 std::string s;
128 for (auto c : payload) s.append(CharToString(c) + " ");
129 return s;
130}
131
132std::string Nmea0183Msg::to_string() const {
133 std::stringstream ss;
134 ss << key() << " " << talker << type << " " << ocpn::printable(payload);
135 return ss.str();
136}
137
138std::string Nmea0183Msg::to_vdr() const { return ocpn::printable(payload); }
139
140std::string PluginMsg::to_string() const {
142 if (name == "position-fix") return name + ":" + ocpn::rtrim(message);
143
144 std::stringstream ss;
145 for (char c : ocpn::rtrim(message)) {
146 if (c >= ' ' && c <= '~')
147 ss << c;
148 else
149 ss << CharToString(c);
150 }
151 return name + ": " + ss.str();
152}
153
154NavAddr::Bus NavMsg::GetBusByKey(const std::string& key) {
155 if (key.find("::") == std::string::npos) return NavAddr::Bus::Undef;
156 auto key_parts = ocpn::split(key, "::");
157 return NavAddr::StringToBus(key_parts[0]);
158}
virtual std::string key() const =0
Return unique key used by observable to notify/listen.
static NavAddr::Bus GetBusByKey(const std::string &key)
Return bus corresponding to given key.
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::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
std::string rtrim(const std::string &arg)
Strip possibly trailing space characters in s.
Miscellaneous utilities, many of which string related.