| 1 | #include "ais.h" |
| 2 | |
| 3 | namespace libais { |
| 4 | |
| 5 | AisBitset::AisBitset() : num_bits(0), num_chars(0), current_position(0) {} |
| 6 | |
| 7 | AIS_STATUS AisBitset::ParseNmeaPayload(const char *nmea_payload, int pad) { |
| 8 | assert(nmea_payload); |
| 9 | assert(pad >= 0 && pad < 6); |
| 10 | |
| 11 | InitNmeaOrd(); |
| 12 | |
| 13 | num_bits = 0; |
| 14 | current_position = 0; |
| 15 | reset(); |
| 16 | |
| 17 | num_chars = strlen(nmea_payload); |
| 18 | |
| 19 | const size_t max_chars = size()/6; |
| 20 | if (static_cast<size_t>(num_chars) > max_chars) { |
| 21 | #ifdef LIBAIS_DEBUG |
| 22 | std::cerr << "ERROR: message longer than max allowed size (" << max_chars |
| 23 | << "): found " << num_chars << " characters in " |
| 24 | << nmea_payload << std::endl; |
| 25 | #endif |
| 26 | num_chars = 0; // Don't leave an impression that there are any valid chars. |
| 27 | return AIS_ERR_MSG_TOO_LONG; |
| 28 | } |
| 29 | |
| 30 | size_t bit = 0; |
| 31 | for (size_t idx = 0; nmea_payload[idx] != '\0' && idx < max_chars; idx++) { |
| 32 | int c = static_cast<int>(nmea_payload[idx]); |
| 33 | if (c < 48 || c > 119 || (c >= 88 && c <= 95)) { |
| 34 | // Make it clear that nothing valuable is in here. |
| 35 | reset(); |
| 36 | num_chars = 0; |
| 37 | return AIS_ERR_BAD_NMEA_CHR; |
| 38 | } |
| 39 | for (size_t offset = 0; offset < 6; offset++) { |
| 40 | set(bit++, nmea_ord_[c].test(offset)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | num_bits = num_chars * 6 - pad; |
| 45 | |
| 46 | return AIS_OK; |
| 47 | } |
| 48 | |
| 49 | const AisBitset& AisBitset::SeekRelative(int d) const { |
| 50 | assert((current_position + d) >= 0 && (current_position + d) < num_bits); |
| 51 | current_position += d; |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | const AisBitset& AisBitset::SeekTo(size_t pos) const { |
| 56 | assert(pos < num_bits); |
| 57 | current_position = pos; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | bool AisBitset::operator[](size_t pos) const { |
| 62 | // TODO(schwehr): Prefer to use num_bits (includes pad) for checking bounds. |
| 63 | assert(pos < num_chars * 6); |
| 64 | |
| 65 | assert(current_position == pos); |
| 66 | |
| 67 | current_position = pos + 1; |
| 68 | return bitset<MAX_BITS>::operator[](pos); |
| 69 | } |
| 70 | |
| 71 | unsigned int AisBitset::ToUnsignedInt(const size_t start, |
| 72 | const size_t len) const { |
| 73 | assert(len <= 32); |
| 74 | // TODO(schwehr): Prefer to use num_bits (includes pad) for checking bounds. |
| 75 | assert(start + len <= num_chars * 6); |
| 76 | |
| 77 | assert(current_position == start); |
| 78 | |
| 79 | unsigned int result = 0; |
| 80 | size_t end = start + len; |
| 81 | for (size_t i = start; i < end; ++i) { |
| 82 | result <<= 1; |
| 83 | if (test(i)) |
| 84 | result |= 1; |
| 85 | } |
| 86 | |
| 87 | current_position = end; |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | int AisBitset::ToInt(const size_t start, const size_t len) const { |
| 92 | assert(len <= 32); |
| 93 | // TODO(schwehr): Prefer to use num_bits (includes pad) for checking bounds. |
| 94 | assert(start + len <= num_chars * 6); |
| 95 | |
| 96 | assert(current_position == start); |
| 97 | |
| 98 | // Converting the sub-bitset to a signed number, per "Two's complement": |
| 99 | // - If negative, invert all the bits, then add 1. |
| 100 | bool is_positive = (len == 32 || !test(start)); |
| 101 | int result = 0; |
| 102 | size_t end = start + len; |
| 103 | for (size_t i = start; i < end; ++i) { |
| 104 | result <<= 1; |
| 105 | if (test(i) == is_positive) |
| 106 | result |= 1; |
| 107 | } |
| 108 | current_position = end; |
| 109 | return is_positive ? result : -(result + 1); |
| 110 | } |
| 111 | |
| 112 | string AisBitset::ToString(const size_t start, const size_t len) const { |
| 113 | assert(len % 6 == 0); |
| 114 | // TODO(schwehr): Prefer to use num_bits (includes pad) for checking bounds. |
| 115 | assert(start + len <= num_chars * 6); |
| 116 | |
| 117 | assert(current_position == start); |
| 118 | |
| 119 | const size_t num_char = len / 6; |
| 120 | string result(num_char, '@'); |
| 121 | for (size_t char_idx = 0; char_idx < num_char; char_idx++) { |
| 122 | const int char_num = ToUnsignedInt(start + char_idx*6, 6); |
| 123 | result[char_idx] = bits_to_char_tbl_[char_num]; |
| 124 | } |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | const AisPoint AisBitset::ToAisPoint(const size_t start, |
| 129 | const size_t point_size) const { |
| 130 | int lng_bits; |
| 131 | int lat_bits; |
| 132 | double divisor; |
| 133 | switch (point_size) { |
| 134 | case 35: |
| 135 | lng_bits = 18; |
| 136 | lat_bits = 17; |
| 137 | divisor = 600.0; |
| 138 | break; |
| 139 | case 49: |
| 140 | lng_bits = 25; |
| 141 | lat_bits = 24; |
| 142 | divisor = 60000.0; // 1/1000th minute |
| 143 | break; |
| 144 | case 55: |
| 145 | lng_bits = 28; |
| 146 | lat_bits = 27; |
| 147 | divisor = 600000.; |
| 148 | break; |
| 149 | default: |
| 150 | std::cerr << "Unsupported point AIS size: " << point_size << std::endl; |
| 151 | assert(false); |
| 152 | return AisPoint(-1, -1); |
| 153 | } |
| 154 | double lng_deg = ToInt(start, lng_bits); |
| 155 | double lat_deg = ToInt(start + lng_bits, lat_bits); |
| 156 | return AisPoint(lng_deg / divisor, lat_deg / divisor); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | // static private |
| 161 | |
| 162 | void AisBitset::InitNmeaOrd() { |
| 163 | if (nmea_ord_initialized_) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | for (int c = 48; c < 88; c++) { |
| 168 | int val = c - 48; |
| 169 | nmea_ord_[c] = Reverse(bitset<6>(val)); |
| 170 | } |
| 171 | for (int c = 88; c < 128; c++) { |
| 172 | int val = c - 48; |
| 173 | val -= 8; |
| 174 | nmea_ord_[c] = Reverse(bitset<6>(val)); |
| 175 | } |
| 176 | nmea_ord_initialized_ = true; |
| 177 | } |
| 178 | |
| 179 | bitset<6> AisBitset::Reverse(const bitset<6> &bits) { |
| 180 | bitset<6> out; |
| 181 | for (size_t i = 0; i < 6; i++) |
| 182 | out[5 - i] = bits[i]; |
| 183 | return out; |
| 184 | } |
| 185 | |
| 186 | bool AisBitset::nmea_ord_initialized_ = false; |
| 187 | bitset<6> AisBitset::nmea_ord_[128]; |
| 188 | // For decoding str bits inside of a binary message. |
| 189 | const char AisBitset::bits_to_char_tbl_[] = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 190 | "[\\]^- !\"#$%&`()*+,-./0123456789:;<=>?"; |
| 191 | |
| 192 | } // namespace libais |