| 1 | // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Parse NMEA AIS VDM strings without extra metadata. |
| 16 | // |
| 17 | // TODO(schwehr): Convert the catch (...) blanket exception catches to the |
| 18 | // expected exception. |
| 19 | // TODO(schwehr): Enable or remove logging messages. |
| 20 | |
| 21 | #include "vdm.h" |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <cctype> |
| 25 | #include <deque> |
| 26 | #include <functional> |
| 27 | #include <iomanip> |
| 28 | #include <locale> |
| 29 | #include <memory> |
| 30 | #include <numeric> |
| 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | #include <tuple> |
| 34 | |
| 35 | #include "decode_body.h" |
| 36 | #include "ais.h" |
| 37 | |
| 38 | using libais::AisMsg; |
| 39 | using std::ostringstream; |
| 40 | using std::unique_ptr; |
| 41 | |
| 42 | namespace libais { |
| 43 | |
| 44 | uint8_t Checksum(const string &line) { |
| 45 | return std::accumulate(line.begin(), line.end(), 0, std::bit_xor<uint8_t>()); |
| 46 | } |
| 47 | |
| 48 | string ToHex2(int32_t val) { |
| 49 | std::ostringstream out; |
| 50 | out << std::hex << std::setfill('0') << std::setw(2) << val; |
| 51 | return out.str(); |
| 52 | } |
| 53 | |
| 54 | string ChecksumHexString(const string &base) { |
| 55 | string checksum = ToHex2(Checksum(base)); |
| 56 | std::transform(checksum.begin(), checksum.end(), checksum.begin(), ::toupper); |
| 57 | return checksum; |
| 58 | } |
| 59 | |
| 60 | vector<string> Split(const string &line, char delim) { |
| 61 | vector<string> result; |
| 62 | std::stringstream ss(line); |
| 63 | while (!ss.eof()) { |
| 64 | string next; |
| 65 | std::getline(ss, next, delim); |
| 66 | result.push_back(next); |
| 67 | } |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | bool ValidateChecksum(const string &line) { |
| 72 | vector<string> fields = Split(line, '*'); |
| 73 | |
| 74 | if (fields.size() != 2 || fields[1].size() != 2) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | const string checksum_str(fields[1]); |
| 80 | int32_t checksum = std::stoi(checksum_str, nullptr, 16); |
| 81 | |
| 82 | int32_t checksum_calculated = Checksum(line.substr(1, line.size() - 4)); |
| 83 | if (checksum_calculated != checksum) { |
| 84 | return false; |
| 85 | } |
| 86 | } catch (...) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | string ReportErrorLine(const string &msg, const string &line, |
| 94 | int64_t line_number) { |
| 95 | return "Error on line:" + std::to_string(line_number) + ": " + msg + "\n " + |
| 96 | line; |
| 97 | } |
| 98 | |
| 99 | bool GetSentenceSequenceNumbers(const string & /* line */, |
| 100 | const int64_t /* line_number */, |
| 101 | const vector<string> &fields, |
| 102 | int32_t *sentence_total, |
| 103 | int32_t *sentence_number, |
| 104 | int32_t *sequence_number, char *channel) { |
| 105 | try { |
| 106 | *sentence_total = std::stoi(fields[1]); |
| 107 | } catch (...) { |
| 108 | return false; |
| 109 | } |
| 110 | if (*sentence_total < 0 || *sentence_total >= kMaxSentences) { |
| 111 | // TODO(schwehr): Test this code path. |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | if (fields[2].size() != 1) { |
| 116 | return false; |
| 117 | } |
| 118 | try { |
| 119 | *sentence_number = std::stoi(fields[2]); |
| 120 | } catch (...) { |
| 121 | // TODO(schwehr): Test this code path. |
| 122 | return false; |
| 123 | } |
| 124 | if (*sentence_number < 1 || *sentence_number > *sentence_total) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (fields[3].empty()) { |
| 129 | *sequence_number = -1; |
| 130 | } else { |
| 131 | try { |
| 132 | *sequence_number = std::stoi(fields[3]); |
| 133 | } catch (...) { |
| 134 | return false; |
| 135 | } |
| 136 | if (*sequence_number < 0 || *sequence_number >= kNumSequenceChannels) { |
| 137 | // TODO(schwehr): Simplify this block to just return false. |
| 138 | if (fields[3] != "") { |
| 139 | return false; |
| 140 | } |
| 141 | *sequence_number = -1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (fields[4].size() != 1 || (fields[4][0] != 'A' && fields[4][0] != 'B')) { |
| 146 | return false; |
| 147 | } |
| 148 | *channel = fields[4][0]; |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | // TODO(schwehr): Switch to make_unique when C++14 is available on Travis-CI. |
| 154 | template <typename T, typename... Args> |
| 155 | std::unique_ptr<T> MakeUnique(Args &&... args) { |
| 156 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 157 | } |
| 158 | |
| 159 | // static |
| 160 | unique_ptr<NmeaSentence> NmeaSentence::Create(const string &line, |
| 161 | int64_t line_number) { |
| 162 | if (line.size() < 9 || line[0] != '!') { |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | vector<string> fields = Split(line, ','); |
| 167 | if (fields.size() != 7) { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | if (!ValidateChecksum(line)) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | if (fields[0].size() != 6) { |
| 176 | // TODO(schwehr): Test this code path. |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | const string talker(fields[0].substr(1, 2)); |
| 181 | const string sentence_type(fields[0].substr(3)); |
| 182 | |
| 183 | int32_t sentence_total; |
| 184 | int32_t sentence_number; |
| 185 | int32_t sequence_number; |
| 186 | char channel; |
| 187 | if (!GetSentenceSequenceNumbers(line, line_number, fields, &sentence_total, |
| 188 | &sentence_number, &sequence_number, |
| 189 | &channel)) { |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | const string body(fields[5]); |
| 194 | if (body.size() < 1 || body.size() > 199) { |
| 195 | // TODO(schwehr): Test this code path. |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | int32_t fill_bits; |
| 200 | try { |
| 201 | fill_bits = std::stoi(fields[6].substr(0, 1)); |
| 202 | } catch (...) { |
| 203 | // TODO(schwehr): Test this code path. |
| 204 | return nullptr; |
| 205 | } |
| 206 | if (fill_bits < 0 || fill_bits > 5) { |
| 207 | return nullptr; |
| 208 | } |
| 209 | |
| 210 | return MakeUnique<NmeaSentence>(talker, sentence_type, sentence_total, |
| 211 | sentence_number, sequence_number, channel, |
| 212 | body, fill_bits, line_number); |
| 213 | } |
| 214 | |
| 215 | // TODO(schwehr): Is if faster to just use string.append? |
| 216 | string Join(const vector<string> &parts, char delim) { |
| 217 | ostringstream out; |
| 218 | for (const auto &part : parts) { |
| 219 | if (&part != &parts[0]) { |
| 220 | out << delim; |
| 221 | } |
| 222 | } |
| 223 | return out.str(); |
| 224 | } |
| 225 | |
| 226 | unique_ptr<NmeaSentence> NmeaSentence::Merge( |
| 227 | const vector<unique_ptr<NmeaSentence>> &prior_sentences) const { |
| 228 | if (prior_sentences.size() != sentence_total_ - 1) { |
| 229 | return nullptr; |
| 230 | } |
| 231 | |
| 232 | // Make sure these sentences really go together. |
| 233 | for (const auto &prior_sentence : prior_sentences) { |
| 234 | if (!VerifyInSameMessage(*prior_sentence)) { |
| 235 | return nullptr; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Check the order. |
| 240 | for (int i = 0; i < prior_sentences.size(); ++i) { |
| 241 | if (prior_sentences[i]->sentence_number() != i + 1) { |
| 242 | return nullptr; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | string body; |
| 247 | for (const auto &sentence : prior_sentences) { |
| 248 | body.append(sentence->body()); |
| 249 | } |
| 250 | body.append(body_); |
| 251 | |
| 252 | return MakeUnique<NmeaSentence>(talker_, sentence_type_, 1, 1, |
| 253 | sequence_number_, channel_, body, fill_bits_, |
| 254 | line_number_); |
| 255 | } |
| 256 | |
| 257 | string NmeaSentence::ToString() const { |
| 258 | string channel; |
| 259 | channel += channel_; |
| 260 | string sequence((sequence_number_ != -1) ? std::to_string(sequence_number_) |
| 261 | : ""); |
| 262 | |
| 263 | string result = ""; |
| 264 | result.append(talker_ + sentence_type_); |
| 265 | result.append(","); |
| 266 | result.append(std::to_string(sentence_total_)); |
| 267 | result.append(","); |
| 268 | result.append(std::to_string(sentence_number_)); |
| 269 | result.append(","); |
| 270 | result.append(sequence); |
| 271 | result.append(","); |
| 272 | result.append(channel); |
| 273 | result.append(","); |
| 274 | result.append(body_); |
| 275 | result.append(","); |
| 276 | result.append(std::to_string(fill_bits_)); |
| 277 | string checksum_str = ChecksumHexString(result); |
| 278 | result.append("*"); |
| 279 | result.append(checksum_str); |
| 280 | return "!" + result; |
| 281 | } |
| 282 | |
| 283 | string NmeaSentence::ToMd5Digest() const { |
| 284 | // TODO(schwehr): md5 digest. |
| 285 | return ""; |
| 286 | } |
| 287 | |
| 288 | bool NmeaSentence::VerifyInSameMessage(const NmeaSentence &sentence) const { |
| 289 | if (sentence.talker() != talker_) { |
| 290 | return false; |
| 291 | } |
| 292 | if (sentence.sentence_type() != sentence_type_) { |
| 293 | return false; |
| 294 | } |
| 295 | if (sentence.sentence_number() == sentence_number_) { |
| 296 | return false; |
| 297 | } |
| 298 | if (sentence.sentence_total() != sentence_total_) { |
| 299 | return false; |
| 300 | } |
| 301 | if (sentence.sequence_number() != sequence_number_) { |
| 302 | return false; |
| 303 | } |
| 304 | if (sentence.channel() != channel_) { |
| 305 | return false; |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | bool VdmStream::AddLine(const string &line) { |
| 311 | line_number_++; |
| 312 | auto sentence = NmeaSentence::Create(line, line_number_); |
| 313 | if (sentence == nullptr) { |
| 314 | return false; |
| 315 | } |
| 316 | int seq = sentence->sequence_number(); |
| 317 | int tot = sentence->sentence_total(); |
| 318 | |
| 319 | // These are enforced by NmeaSentence, so only check wehen debugging. |
| 320 | assert(seq < kNumSequenceChannels); |
| 321 | assert(tot < kMaxSentences); |
| 322 | |
| 323 | // Convert multi-line message to single line. |
| 324 | if (tot != 1) { |
| 325 | int cnt = sentence->sentence_number(); |
| 326 | |
| 327 | // Beginning of a message. |
| 328 | if (cnt == 1) { |
| 329 | incoming_sentences_[seq].clear(); |
| 330 | incoming_sentences_[seq].emplace_back(std::move(sentence)); |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | // Middle sentences of a message. |
| 336 | if (cnt != tot) { |
| 337 | if (incoming_sentences_[seq].size() + 1 != cnt) { |
| 338 | return false; |
| 339 | } |
| 340 | incoming_sentences_[seq].emplace_back(std::move(sentence)); |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | // Got final sentence in a multi-line message. |
| 345 | if (incoming_sentences_[seq].size() != tot - 1) { |
| 346 | incoming_sentences_[seq].clear(); |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | sentence = sentence->Merge(incoming_sentences_[seq]); |
| 351 | incoming_sentences_[seq].clear(); |
| 352 | if (sentence == nullptr) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // Ready to process multi-line message. Do not return here. |
| 357 | } |
| 358 | |
| 359 | if (sentence->body().size() < 2) { |
| 360 | return false; |
| 361 | } |
| 362 | unique_ptr<AisMsg> msg = |
| 363 | CreateAisMsg(sentence->body(), sentence->fill_bits()); |
| 364 | if (msg == nullptr) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | messages_.emplace_front(std::move(msg)); |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | unique_ptr<AisMsg> VdmStream::PopOldestMessage() { |
| 373 | if (messages_.empty()) { |
| 374 | return nullptr; |
| 375 | } |
| 376 | unique_ptr<AisMsg> msg(std::move(messages_.back())); |
| 377 | messages_.pop_back(); |
| 378 | return msg; |
| 379 | } |
| 380 | |
| 381 | } // namespace libais |