General: reorganization of header files
[marnav.git] / src / marnav / ais / ais.cpp
blob6963f2c873422013b0e924dadede55dc1f2b907d
1 #include <marnav/ais/ais.hpp>
2 #include <marnav/ais/message_01.hpp>
3 #include <marnav/ais/message_02.hpp>
4 #include <marnav/ais/message_03.hpp>
5 #include <marnav/ais/message_04.hpp>
6 #include <marnav/ais/message_05.hpp>
7 #include <marnav/ais/message_06.hpp>
8 #include <marnav/ais/message_07.hpp>
9 #include <marnav/ais/message_08.hpp>
10 #include <marnav/ais/message_09.hpp>
11 #include <marnav/ais/message_10.hpp>
12 #include <marnav/ais/message_11.hpp>
13 #include <marnav/ais/message_12.hpp>
14 #include <marnav/ais/message_13.hpp>
15 #include <marnav/ais/message_14.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 #include <algorithm>
26 #include <functional>
28 /// @example parse_ais.cpp
29 /// This example shows how to parse AIS messages from NMEA sentences.
31 /// @example read_ais.cpp
32 /// This is an example on how to parse and handle AIS messages while
33 /// receiving NMEA sentences.
35 /// @example create_nmea_from_ais.cpp
36 /// Shows how to create a NMEA sentence or sentences from AIS data.
38 namespace marnav
40 namespace ais
42 uint8_t decode_armoring(char c)
44 auto value = c - '0';
45 if (value > 40)
46 value -= 8;
47 return value & 0x3f;
50 char encode_armoring(uint8_t value)
52 value &= 0x3f; // ensure 6 bits
53 if (value > 39)
54 value += 8;
55 return value + '0';
58 /// @cond DEV
60 namespace
62 static raw collect(const std::vector<std::pair<std::string, uint32_t>> & v)
64 raw result;
65 result.reserve(64); // 64 bytes (512) are enough for AIS messages
67 for (auto const & item : v) {
68 const std::string & payload = item.first;
69 const uint32_t pad = item.second;
71 auto end = payload.cend();
72 auto last = end - 1;
73 for (auto i = payload.cbegin(); i != end; ++i) {
75 uint8_t value = decode_armoring(*i);
77 if (i == last) {
78 result.append(value >> pad, 6 - pad);
79 } else {
80 result.append(value, 6);
85 return result;
88 static std::function<std::unique_ptr<message>(const raw &)> instantiate_message(
89 message_id type, size_t size)
91 #define REGISTER_MESSAGE(m) \
92 { \
93 m::ID, detail::factory::parse<m> \
96 struct entry {
97 const message_id id;
98 const std::function<std::unique_ptr<message>(const raw &)> parse;
101 static const std::vector<entry> known_messages = {
102 REGISTER_MESSAGE(message_01), REGISTER_MESSAGE(message_02),
103 REGISTER_MESSAGE(message_03), REGISTER_MESSAGE(message_04),
104 REGISTER_MESSAGE(message_05), REGISTER_MESSAGE(message_06),
105 REGISTER_MESSAGE(message_07), REGISTER_MESSAGE(message_08),
106 REGISTER_MESSAGE(message_09), REGISTER_MESSAGE(message_10),
107 REGISTER_MESSAGE(message_11), REGISTER_MESSAGE(message_12),
108 REGISTER_MESSAGE(message_13), REGISTER_MESSAGE(message_14),
109 REGISTER_MESSAGE(message_17), REGISTER_MESSAGE(message_18),
110 REGISTER_MESSAGE(message_19), REGISTER_MESSAGE(message_20),
111 REGISTER_MESSAGE(message_21), REGISTER_MESSAGE(message_22),
112 REGISTER_MESSAGE(message_23), REGISTER_MESSAGE(message_24),
115 #undef REGISTER_MESSAGE
117 using namespace std;
118 auto const & i = std::find_if(begin(known_messages), end(known_messages),
119 [type](const entry & e) { return e.id == type; });
121 if (i == end(known_messages))
122 throw unknown_message{"unknown message in ais/instantiate_message: "
123 + std::to_string(static_cast<uint8_t>(type)) + " (" + std::to_string(size)
124 + " bits)"};
126 return i->parse;
130 /// @endcond
132 /// Parses the specified data and creates corresponding AIS messages.
134 /// @param[in] v All NMEA payloads, necessary to build the AIS message.
135 /// This may be obtained using nmea::collect_payload.
136 /// @return The constructed AIS message.
137 /// @exception unknown_message Will be thrown if the AIS message is not supported.
138 /// @exception std::invalid_argument Error has been occurred during parsing of
139 /// the message.
140 std::unique_ptr<message> make_message(const std::vector<std::pair<std::string, uint32_t>> & v)
142 auto bits = collect(v);
143 message_id type = static_cast<message_id>(bits.get<uint8_t>(0, 6));
144 return instantiate_message(type, bits.size())(bits);
147 /// Encodes the specified message and returns a container with payload and padding
148 /// information. This payload container can be used directly with NMEA funcitons.
150 /// @param[in] msg The message to encode.
151 /// @return The container with payload/padding information
153 std::vector<std::pair<std::string, uint32_t>> encode_message(const message & msg)
155 auto bits = msg.get_data();
156 if (bits.size() == 0)
157 throw std::invalid_argument{"message not able to encode"};
159 std::vector<std::pair<std::string, uint32_t>> result;
161 std::pair<std::string, uint32_t> current{"", 0};
162 for (raw::size_type ofs = 0; ofs < bits.size(); ofs += 6) {
163 if (ofs + 6 < bits.size()) {
164 // normal case
166 uint8_t value = 0;
167 bits.get(value, ofs, 6);
168 current.first += encode_armoring(value);
170 // append to string, only 51 characters per string (happens to be NMEA restriction)
171 if (current.first.size() == 56) {
172 result.push_back(current);
173 current.first.clear();
174 current.second = 0;
176 } else {
177 // last, append remainder padded to the string
179 auto remainder = bits.size() - ofs;
180 current.second = 6 - remainder;
181 uint8_t value = 0;
182 bits.get(value, ofs, remainder);
183 value <<= current.second;
184 current.first += encode_armoring(value);
185 result.push_back(current);
189 return result;