| 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 | // Handles a sequence of AIS VDM NMEA messages. Messages are composed of |
| 18 | // 1 to 10 lines. These lines are connected by a sequence number if there |
| 19 | // are more than 1 line required to make a message. NMEA AIS VDM lines are of |
| 20 | // the following form as a pseudo regular expression, where each line has 7 |
| 21 | // comma separated fields: |
| 22 | // |
| 23 | // ![A-Z]{5},[1-9],[1-9],[0-9]?,[AB],armored_data,[0-5]\*[0-9A-F][0-9A-F] |
| 24 | // |
| 25 | // The fields are: |
| 26 | // |
| 27 | // 0. Start bang, 2 character talker and 3 character sentence. |
| 28 | // 1. Total number of sentences. |
| 29 | // 2. Current sentence number. |
| 30 | // 3. Sequence number. |
| 31 | // 4. VHF radio channel. |
| 32 | // 5. 6-bit per character encoded binary data (not Base64 encoding). The |
| 33 | // first character maps to the AIS message type. |
| 34 | // 6. The number of fill bits included in the last character, a star, and |
| 35 | // a 2 character hex xor checksum. |
| 36 | // |
| 37 | // An example single line message: |
| 38 | // |
| 39 | // !AIVDM,1,1,,A,14VIk0002sMM04vE>V9jGimn08RP,0*0D |
| 40 | // |
| 41 | // A multi-line message: |
| 42 | // |
| 43 | // clang-format off |
| 44 | // !SAVDM,2,1,4,B,55NGlfP00001L@GO??0lU=>0hUaaV2222222220O1p>454wV07PhE82Dk0CQ,0*64 |
| 45 | // // NOLINT |
| 46 | // !SAVDM,2,2,4,B,84i@H0SmFH0,2*54 |
| 47 | // clang-format on |
| 48 | // |
| 49 | // The library is structured around NmeaSentence, which manages each line of |
| 50 | // text. It validates the checksum and separates the field components. |
| 51 | // |
| 52 | // VdmStream takes a series of sentences, converts them to NmeaScentences, and |
| 53 | // then assembles the parts into complete messages. When it has all of the |
| 54 | // constituent sentences, it uses libais to decode the armored 6-bit payload |
| 55 | // into AisMsg instances. |
| 56 | // |
| 57 | // The VdmStream is not thread safe. |
| 58 | // |
| 59 | // See Also: |
| 60 | // http://catb.org/gpsd/AIVDM.html |
| 61 | // http://www.itu.int/rec/R-REC-M.1371/en |
| 62 | |
| 63 | #ifndef LIBAIS_VDM_H_ |
| 64 | #define LIBAIS_VDM_H_ |
| 65 | |
| 66 | #include <deque> |
| 67 | #include <memory> |
| 68 | #include <string> |
| 69 | #include <vector> |
| 70 | |
| 71 | // #include "base/logging.h" |
| 72 | #include "ais.h" |
| 73 | |
| 74 | namespace libais { |
| 75 | |
| 76 | // No more than 10 sentences per message. The total is [1..9]. |
| 77 | // The most observed in real data has been 4. |
| 78 | static const int kMaxSentences = 10; |
| 79 | |
| 80 | // AIS receivers use the sequence to group multi-line messages. |
| 81 | static const int kNumSequenceChannels = 10; |
| 82 | |
| 83 | // Computes the xor checksum of a string. |
| 84 | uint8_t Checksum(const string &content); |
| 85 | // Convert a number to a two character hex string. |
| 86 | string ToHex2(int32_t val); |
| 87 | // Returns the 2 upper case character xor checksum of a string. |
| 88 | string ChecksumHexString(const string &base); |
| 89 | bool ValidateChecksum(const string &line); |
| 90 | |
| 91 | // Manages single lines of NMEA AIS VDM text. |
| 92 | class NmeaSentence { |
| 93 | public: |
| 94 | NmeaSentence(const string &talker, const string &sentence_type, |
| 95 | int sentence_total, int sentence_number, int sequence_number, |
| 96 | char channel, const string &body, int fill_bits, |
| 97 | int64_t line_number) |
| 98 | : talker_(talker), |
| 99 | sentence_type_(sentence_type), |
| 100 | sentence_total_(sentence_total), |
| 101 | sentence_number_(sentence_number), |
| 102 | sequence_number_(sequence_number), |
| 103 | channel_(channel), |
| 104 | body_(body), |
| 105 | fill_bits_(fill_bits), |
| 106 | line_number_(line_number) {} |
| 107 | |
| 108 | // Returns a smart pointer for an instance of a NmeaSentence created from a |
| 109 | // line of text. Returns nullptr on failure. The line_number argument |
| 110 | // tracks the source location in a file or the count of lines pushed through |
| 111 | // a channel. |
| 112 | static std::unique_ptr<NmeaSentence> Create(const string &line, |
| 113 | int64_t line_number); |
| 114 | |
| 115 | // Returns a composite NmeaSentence from multiple prior sentences that make up |
| 116 | // a multi-line message. The resulting sentence is such that the new payload |
| 117 | // can be decoded as one 6-bit encoded unit. Sets the sentence total and |
| 118 | // sentence number both to 1 so that it looks like a single line sentence. |
| 119 | // Returns a nullptr if the merge failed. |
| 120 | // Merging sentences from different messages or out of order messages will |
| 121 | // fail. |
| 122 | std::unique_ptr<NmeaSentence> Merge( |
| 123 | const vector<std::unique_ptr<NmeaSentence>> &prior_sentences) const; |
| 124 | |
| 125 | // Reconstructs the NMEA AIS VDM sentence representation of this instance. |
| 126 | string ToString() const; |
| 127 | // Returns the hex digest of the 6-bit encoded body. Used for detection of |
| 128 | // multiple instances of a line in an input stream. |
| 129 | // TODO(schwehr): Consider mixing in the VHF channel into the text. |
| 130 | string ToMd5Digest() const; |
| 131 | |
| 132 | // Returns true if another sentence is a part of the same multiline message. |
| 133 | // The given sentence must have the same channel, sequence number and total |
| 134 | // number of sentences as the instance sentence to be a part of the same AIS |
| 135 | // NMEA multi-line message. |
| 136 | // |
| 137 | // In the same message: |
| 138 | // !SAVDM,2,1,1,A,54a... |
| 139 | // !SAVDM,2,2,1,A,888... |
| 140 | // |
| 141 | // Different channels: |
| 142 | // !SAVDM,2,1,1,B,54a... |
| 143 | // !SAVDM,2,2,1,A,888... |
| 144 | // |
| 145 | // Different sequence: |
| 146 | // !SAVDM,2,1,8,A,54a... |
| 147 | // !SAVDM,2,2,2,A,888... |
| 148 | // |
| 149 | // Different totals: |
| 150 | // !SAVDM,2,1,1,A,54a... |
| 151 | // !SAVDM,3,2,1,A,888... |
| 152 | // |
| 153 | // TODO(schwehr): What should this return when called with the same sentence? |
| 154 | // !SAVDM,3,2,1,A,54a... |
| 155 | // !SAVDM,3,3,1,A,54a... |
| 156 | bool VerifyInSameMessage(const NmeaSentence &sentence) const; |
| 157 | |
| 158 | // Accessors to get each of the fields or sub-fields of the sentence. |
| 159 | string talker() const { return talker_; } |
| 160 | string sentence_type() const { return sentence_type_; } |
| 161 | int sentence_total() const { return sentence_total_; } |
| 162 | int sentence_number() const { return sentence_number_; } |
| 163 | int sequence_number() const { return sequence_number_; } |
| 164 | char channel() const { return channel_; } |
| 165 | string body() const { return body_; } |
| 166 | int fill_bits() const { return fill_bits_; } |
| 167 | |
| 168 | int64_t line_number() const { return line_number_; } |
| 169 | |
| 170 | private: |
| 171 | // Which device type sent the message. AI is an AIS transceiver. |
| 172 | const string talker_; |
| 173 | // This will be VDM for an AIS message received and VDO for "own ship." |
| 174 | const string sentence_type_; |
| 175 | // Total number of lines that compose the message this is a part of. |
| 176 | const int sentence_total_; |
| 177 | // Which sentence is this? Ranges from 1 to sentence_total_. |
| 178 | const int sentence_number_; |
| 179 | // Logical channel for the AIS receiver. A receiver will put a multi-line |
| 180 | // message on a single channel and then move to the next channel. Ranges from |
| 181 | // 0 to 9, but most receivers will not use 0. |
| 182 | const int sequence_number_; // -1 if not valid. |
| 183 | // VHF channel. Will be either A or B. Some receivers do not record this. |
| 184 | const char channel_; |
| 185 | // The VDM 6-bit encoded/armored payload of the message. |
| 186 | const string body_; |
| 187 | // The number of padding. Should be zero for all messages except the last. |
| 188 | // Must be 0, 2, or 4. |
| 189 | const int fill_bits_; |
| 190 | |
| 191 | const int64_t line_number_; |
| 192 | }; |
| 193 | |
| 194 | // This class processes a sequence of lines to find the AIS messages across |
| 195 | // lines. AIS messages come in groups of 1 or more lines. Its job is |
| 196 | // to return decoded AIS messages as libais::AisMsg instances as they are found |
| 197 | // in the stream. |
| 198 | // Sentences are assembled into messages and output in FIFO order. |
| 199 | class VdmStream { |
| 200 | public: |
| 201 | VdmStream() : line_number_(0), incoming_sentences_(kNumSequenceChannels) {} |
| 202 | |
| 203 | // Returns true if the sentence was used or false if the line was ignored. |
| 204 | // A line will be ignored if it is not a valid VDM line or if it is a later |
| 205 | // part of a multi-line message but missing one or more initial lines. |
| 206 | bool AddLine(const string &line); // Was push |
| 207 | // Returns nullptr if there are not decoded messages currently available. |
| 208 | std::unique_ptr<libais::AisMsg> PopOldestMessage(); |
| 209 | |
| 210 | int size() const { return messages_.size(); } |
| 211 | bool empty() const { return messages_.empty(); } |
| 212 | |
| 213 | private: |
| 214 | // Line number starts at 0 and is incremented to 1 with the first line. |
| 215 | int64_t line_number_; |
| 216 | |
| 217 | // Decoded messages ready for pickup. |
| 218 | std::deque<std::unique_ptr<libais::AisMsg>> messages_; |
| 219 | // Sentences for each sequence number that have yet to get all the required |
| 220 | // parts to be complete. |
| 221 | vector<vector<std::unique_ptr<NmeaSentence>>> incoming_sentences_; |
| 222 | }; |
| 223 | |
| 224 | } // namespace libais |
| 225 | |
| 226 | #endif // LIBAIS_VDM_H_ |