NMEA: detectio of the optional tag block in front of a sentence, ignoring it for...
[marnav.git] / src / marnav / ais / ais.cpp
blob239d44440e2c427295cdca957b2fa98ed912e9fd
1 #include "ais.hpp"
3 #include <algorithm>
5 #include <marnav/ais/message_01.hpp>
6 #include <marnav/ais/message_02.hpp>
7 #include <marnav/ais/message_03.hpp>
8 #include <marnav/ais/message_04.hpp>
9 #include <marnav/ais/message_05.hpp>
10 #include <marnav/ais/message_06.hpp>
11 #include <marnav/ais/message_07.hpp>
12 #include <marnav/ais/message_08.hpp>
13 #include <marnav/ais/message_09.hpp>
14 #include <marnav/ais/message_10.hpp>
15 #include <marnav/ais/message_11.hpp>
16 #include <marnav/ais/message_17.hpp>
17 #include <marnav/ais/message_18.hpp>
18 #include <marnav/ais/message_19.hpp>
19 #include <marnav/ais/message_20.hpp>
20 #include <marnav/ais/message_21.hpp>
21 #include <marnav/ais/message_22.hpp>
22 #include <marnav/ais/message_23.hpp>
23 #include <marnav/ais/message_24.hpp>
25 /// @example parse_ais.cpp
26 /// This example shows how to parse AIS messages from NMEA sentences.
28 /// @example read_ais.cpp
29 /// This is an example on how to parse and handle AIS messages while
30 /// receiving NMEA sentences.
32 /// @example create_nmea_from_ais.cpp
33 /// Shows how to create a NMEA sentence or sentences from AIS data.
35 namespace marnav
37 namespace ais
39 uint8_t decode_armoring(char c)
41 auto value = c - '0';
42 if (value > 40)
43 value -= 8;
44 return value & 0x3f;
47 char encode_armoring(uint8_t value)
49 value &= 0x3f; // ensure 6 bits
50 if (value > 39)
51 value += 8;
52 return value + '0';
55 /// @cond DEV
57 namespace
59 static raw collect(const std::vector<std::pair<std::string, uint32_t>> & v)
61 raw result;
62 result.reserve(64); // 64 bytes (512) are enough for AIS messages
64 for (auto const & item : v) {
65 const std::string & payload = item.first;
66 const uint32_t pad = item.second;
68 auto end = payload.cend();
69 auto last = end - 1;
70 for (auto i = payload.cbegin(); i != end; ++i) {
72 uint8_t value = decode_armoring(*i);
74 if (i == last) {
75 result.append(value >> pad, 6 - pad);
76 } else {
77 result.append(value, 6);
82 return result;
85 static std::function<std::unique_ptr<message>(const raw &)> instantiate_message(
86 message_id type, size_t size)
88 #define REGISTER_MESSAGE(m) \
89 { \
90 m::ID, detail::parse_##m \
93 struct entry {
94 const message_id id;
95 const std::function<std::unique_ptr<message>(const raw &)> parse;
98 static const std::vector<entry> known_messages = {
99 REGISTER_MESSAGE(message_01), REGISTER_MESSAGE(message_02),
100 REGISTER_MESSAGE(message_03), REGISTER_MESSAGE(message_04),
101 REGISTER_MESSAGE(message_05), REGISTER_MESSAGE(message_06),
102 REGISTER_MESSAGE(message_07), REGISTER_MESSAGE(message_08),
103 REGISTER_MESSAGE(message_09), REGISTER_MESSAGE(message_10),
104 REGISTER_MESSAGE(message_11), REGISTER_MESSAGE(message_17),
105 REGISTER_MESSAGE(message_18), REGISTER_MESSAGE(message_19),
106 REGISTER_MESSAGE(message_20), REGISTER_MESSAGE(message_21),
107 REGISTER_MESSAGE(message_22), REGISTER_MESSAGE(message_23),
108 REGISTER_MESSAGE(message_24),
111 #undef REGISTER_MESSAGE
113 using namespace std;
114 auto const & i = std::find_if(begin(known_messages), end(known_messages),
115 [type](const entry & e) { return e.id == type; });
117 if (i == end(known_messages))
118 throw unknown_message{"unknown message in ais/instantiate_message: "
119 + std::to_string(static_cast<uint8_t>(type)) + " (" + std::to_string(size)
120 + " bits)"};
122 return i->parse;
126 /// @endcond
128 /// Parses the specified data and creates corresponding AIS messages.
130 /// @param[in] v All NMEA payloads, necessary to build the AIS message.
131 /// This may be obtained using nmea::collect_payload.
132 /// @return The constructed AIS message.
133 /// @exception unknown_message Will be thrown if the AIS message is not supported.
134 /// @exception std::invalid_argument Error has been occurred during parsing of
135 /// the message.
136 std::unique_ptr<message> make_message(const std::vector<std::pair<std::string, uint32_t>> & v)
138 auto bits = collect(v);
139 message_id type = static_cast<message_id>(bits.get<uint8_t>(0, 6));
140 return instantiate_message(type, bits.size())(bits);
143 /// Encodes the specified message and returns a container with payload and padding
144 /// information. This payload container can be used directly with NMEA funcitons.
146 /// @param[in] msg The message to encode.
147 /// @return The container with payload/padding information
149 std::vector<std::pair<std::string, uint32_t>> encode_message(const message & msg)
151 auto bits = msg.get_data();
152 if (bits.size() == 0)
153 throw std::invalid_argument{"message not able to encode"};
155 std::vector<std::pair<std::string, uint32_t>> result;
157 std::pair<std::string, uint32_t> current{"", 0};
158 for (raw::size_type ofs = 0; ofs < bits.size(); ofs += 6) {
159 if (ofs + 6 < bits.size()) {
160 // normal case
162 uint8_t value = 0;
163 bits.get(value, ofs, 6);
164 current.first += encode_armoring(value);
166 // append to string, only 51 characters per string (happens to be NMEA restriction)
167 if (current.first.size() == 56) {
168 result.push_back(current);
169 current.first.clear();
170 current.second = 0;
172 } else {
173 // last, append remainder padded to the string
175 auto remainder = bits.size() - ofs;
176 current.second = 6 - remainder;
177 uint8_t value = 0;
178 bits.get(value, ofs, remainder);
179 value <<= current.second;
180 current.first += encode_armoring(value);
181 result.push_back(current);
185 return result;