40#include "nlohmann/json.hpp"
48using NavMsgPtr = shared_ptr<const NavMsg>;
50static string NextWord(std::string& line) {
51 if (line.empty())
return "";
52 size_t space_pos = line.find(
" ");
53 if (space_pos == string::npos) space_pos = line.size();
54 string word(line.substr(0, space_pos));
59static unsigned HexNibbleValue(
char c) {
61 if (c >=
'0' && c <=
'9')
return c -
'0';
62 if (c >=
'a' && c <=
'f')
return 10 + c -
'a';
66static unsigned char HexByteValue(
const string& text) {
68 if (text.size() == 1) {
69 value = HexNibbleValue(text[0]);
71 value = 16 * HexNibbleValue(text[0]) + HexNibbleValue(text[1]);
73 return static_cast<unsigned char>(value);
76static NavMsgPtr Parse2000(
const std::string& iface,
const string& type,
77 const std::string& msg) {
78 vector<unsigned char> payload;
80 for (
const auto&
byte : hexbytes) payload.push_back(HexByteValue(byte));
84 }
catch (invalid_argument&) {
87 return make_shared<Nmea2000Msg>(
88 pgn, payload, make_shared<NavAddr2000>(iface,
N2kName(pgn)));
91static NavMsgPtr Parse0183(
const string& iface,
const string& type,
93 return make_shared<Nmea0183Msg>(type, msg, make_shared<NavAddr0183>(iface));
96static NavMsgPtr ParseSignalk(
const string& iface,
const string& type,
100 nlohmann::json root = nlohmann::json::parse(msg);
101 if (root.contains(
"context")) context = root[
"context"].get<std::string>();
102 }
catch (nlohmann::json::exception&) {
104 return make_shared<SignalkMsg>(type, context, msg, iface);
108 int space_count = count(msg.begin(), msg.end(),
' ');
109 if (space_count < 3) {
112 std::string msg_(msg);
114 auto iface = NextWord(msg_);
115 auto type = NextWord(msg_);
117 if (protocol ==
"nmea2000")
return Parse2000(
iface, type, msg_);
118 if (protocol ==
"nmea0183")
return Parse0183(
iface, type, msg_);
119 if (protocol ==
"signalk")
return ParseSignalk(
iface, type, msg_);
const std::string iface
Physical device for 0183, else a unique string.
static std::shared_ptr< const NavMsg > ParsePluginMessage(const std::string &msg)
Parse a string as provided by plugin and convert to a navmsg.
Loopback driver, treat sent messages as received.
Raw, undecoded messages definitions.
std::string tolower(const std::string &input)
Return copy of s with all characters converted to lower case.
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
std::string ltrim(const std::string &arg)
Strip possibly leading space characters in s.
Miscellaneous utilities, many of which string related.
N2k uses CAN which defines the basic properties of messages.