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::Undef:
61 return "??";
62 break;
63 }
64 return "????";
65}
66
67NavAddr::Bus NavAddr::StringToBus(const std::string& s) {
68 if (s == "nmea0183") return NavAddr::Bus::N0183;
69 if (s == "nmea2000") return NavAddr::Bus::N2000;
70 if (s == "SignalK") return NavAddr::Bus::Signalk;
71 if (s == "Onenet") return NavAddr::Bus::Onenet;
72 if (s == "Plugin") return NavAddr::Bus::Plugin;
73 if (s == "TestBus") return NavAddr::Bus::TestBus;
74 return NavAddr::Bus::Undef;
75}
76
77static std::string CharToString(unsigned char c) {
78 using namespace std;
79 stringstream ss;
80 ss << setfill('0') << hex << setw(2) << (c & 0x00ff);
81 return ss.str();
82}
83
84std::string Nmea2000Msg::to_string() const {
85 std::string s;
86 std::for_each(payload.begin(), payload.end(),
87 [&s](unsigned char c) { s.append(CharToString(c)); });
88
89 return NavMsg::to_string() + " " + PGN.to_string() + " " + s;
90}
91
92std::string Nmea0183Msg::to_string() const {
93 std::stringstream ss;
94 ss << NavAddr::BusToString(bus) << " " << key() << " " << talker << type
95 << " " << ocpn::printable(payload);
96 return ss.str();
97}
virtual 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 type
For example 'GGA'.
const std::string talker
For example 'GP'.
const std::string payload
Complete NMEA0183 sentence, also prefix.
std::string to_string() const
Return printable string for logging etc without trailing nl.
std::string to_string() const
Print "bus key id payload".
Communication driver layer.
std::string printable(const std::string str)
Return copy of str with non-printable chars replaced by hex like "<0D>".