| 1 | // Defined sort of by Nav55, but being tweaked by the USCG and |
| 2 | // will hopefully become a RTCM standard. Also hoping that this |
| 3 | // will be harmonized with the IMO Circ 289. |
| 4 | |
| 5 | #include <cmath> |
| 6 | |
| 7 | #include "ais.h" |
| 8 | |
| 9 | namespace libais { |
| 10 | |
| 11 | const char *shape_names[8] = {"Circle/Pt", "Rect", "Sector", "Polyline", |
| 12 | "Polygon", "Text", "Reserved_6", "Reserved_7"}; |
| 13 | |
| 14 | const char *ais8_366_22_notice_names[AIS8_366_22_NUM_NAMES] = { |
| 15 | "Caution Area: Marine mammals habitat (no whales obs)", // 0 |
| 16 | "Caution Area: Marine mammals in area – reduce speed", // 1 |
| 17 | "Caution Area: Marine mammals in area – stay clear", // 2 |
| 18 | "Caution Area: Marine mammals in area – report sightings", // 3 |
| 19 | "Caution Area: Protected habitat – reduce speed", // 4 |
| 20 | "Caution Area: Protected habitat – stay clear", // 5 |
| 21 | "Caution Area: Protected habitat – no fishing or anchoring", // 6 |
| 22 | "Caution Area: Derelicts (drifting objects)", // 7 |
| 23 | "Caution Area: Traffic congestion", // 8 |
| 24 | "Caution Area: Marine event", // 9 |
| 25 | "Caution Area: Divers down", // 10 |
| 26 | "Caution Area: Swim area", // 11 |
| 27 | "Caution Area: Dredge operations", // 12 |
| 28 | "Caution Area: Survey operations", // 13 |
| 29 | "Caution Area: Underwater operation", // 14 |
| 30 | "Caution Area: Seaplane operations", // 15 |
| 31 | "Caution Area: Fishery – nets in water", // 16 |
| 32 | "Caution Area: Cluster of fishing vessels", // 17 |
| 33 | "Caution Area: Fairway closed", // 18 |
| 34 | "Caution Area: Harbour closed", // 19 |
| 35 | "Caution Area: Risk (define in Associated text field)", // 20 |
| 36 | "Caution Area: Underwater vehicle operation", // 21 |
| 37 | "(reserved for future use)", // 22 |
| 38 | "Environmental Caution Area: Storm front (line squall)", // 23 |
| 39 | "Environmental Caution Area: Hazardous sea ice", // 24 |
| 40 | "Environmental Caution Area: Storm warning (cell or line of storms)", // 25 |
| 41 | "Environmental Caution Area: High wind", // 26 |
| 42 | "Environmental Caution Area: High waves", // 27 |
| 43 | "Environmental Caution Area: Restricted visibility (fog, rain, etc.)", // 28 |
| 44 | "Environmental Caution Area: Strong currents", // 29 |
| 45 | "Environmental Caution Area: Heavy icing", // 30 |
| 46 | "(reserved for future use)", // 31 |
| 47 | "Restricted Area: Fishing prohibited", // 32 |
| 48 | "Restricted Area: No anchoring.", // 33 |
| 49 | "Restricted Area: Entry approval required prior to transit", // 34 |
| 50 | "Restricted Area: Entry prohibited", // 35 |
| 51 | "Restricted Area: Active military OPAREA", // 36 |
| 52 | "Restricted Area: Firing – danger area.", // 37 |
| 53 | "Restricted Area: Drifting Mines", // 38 |
| 54 | "(reserved for future use)", // 39 |
| 55 | "Anchorage Area: Anchorage open", // 40 |
| 56 | "Anchorage Area: Anchorage closed", // 41 |
| 57 | "Anchorage Area: Anchoring prohibited", // 42 |
| 58 | "Anchorage Area: Deep draft anchorage", // 43 |
| 59 | "Anchorage Area: Shallow draft anchorage", // 44 |
| 60 | "Anchorage Area: Vessel transfer operations", // 45 |
| 61 | "(reserved for future use)", // 46 |
| 62 | "(reserved for future use)", // 47 |
| 63 | "(reserved for future use)", // 48 |
| 64 | "(reserved for future use)", // 49 |
| 65 | "(reserved for future use)", // 50 |
| 66 | "(reserved for future use)", // 51 |
| 67 | "(reserved for future use)", // 52 |
| 68 | "(reserved for future use)", // 53 |
| 69 | "(reserved for future use)", // 54 |
| 70 | "(reserved for future use)", // 55 |
| 71 | "Security Alert – Level 1", // 56 |
| 72 | "Security Alert – Level 2", // 57 |
| 73 | "Security Alert – Level 3", // 58 |
| 74 | "(reserved for future use)", // 59 |
| 75 | "(reserved for future use)", // 60 |
| 76 | "(reserved for future use)", // 61 |
| 77 | "(reserved for future use)", // 62 |
| 78 | "(reserved for future use)", // 63 |
| 79 | "Distress Area: Vessel disabled and adrift", // 64 |
| 80 | "Distress Area: Vessel sinking", // 65 |
| 81 | "Distress Area: Vessel abandoning ship", // 66 |
| 82 | "Distress Area: Vessel requests medical assistance", // 67 |
| 83 | "Distress Area: Vessel flooding", // 68 |
| 84 | "Distress Area: Vessel fire/explosion", // 69 |
| 85 | "Distress Area: Vessel grounding", // 70 |
| 86 | "Distress Area: Vessel collision", // 71 |
| 87 | "Distress Area: Vessel listing/capsizing", // 72 |
| 88 | "Distress Area: Vessel under assault", // 73 |
| 89 | "Distress Area: Person overboard", // 74 |
| 90 | "Distress Area: SAR area", // 75 |
| 91 | "Distress Area: Pollution response area", // 76 |
| 92 | "(reserved for future use)", // 77 |
| 93 | "(reserved for future use)", // 78 |
| 94 | "(reserved for future use)", // 79 |
| 95 | "Instruction: Contact VTS at this point/juncture", // 80 |
| 96 | "Instruction: Contact Port Administration at this point/juncture", // 81 |
| 97 | "Instruction: Do not proceed beyond this point/juncture", // 82 |
| 98 | "Instruction: Await instructions prior to proceeding beyond " |
| 99 | "this point/juncture", // 83 |
| 100 | "Proceed to this location – await instructions", // 84 |
| 101 | "Clearance granted – proceed to berth", // 85 |
| 102 | "(reserved for future use)", // 86 |
| 103 | "(reserved for future use)", // 87 |
| 104 | "Information: Pilot boarding position", // 88 |
| 105 | "Information: Icebreaker waiting area", // 89 |
| 106 | "Information: Places of refuge", // 90 |
| 107 | "Information: Position of icebreakers", // 91 |
| 108 | "Information: Location of response units", // 92 |
| 109 | "VTS active target", // 93 |
| 110 | "Rogue or suspicious vessel", // 94 |
| 111 | "Vessel requesting non-distress assistance", // 95 |
| 112 | "Chart Feature: Sunken vessel", // 96 |
| 113 | "Chart Feature: Submerged object", // 97 |
| 114 | "Chart Feature: Semi-submerged object", // 98 |
| 115 | "Chart Feature: Shoal area", // 99 |
| 116 | "Chart Feature: Shoal area due north", // 100 |
| 117 | "Chart Feature: Shoal area due east", // 101 |
| 118 | "Chart Feature: Shoal area due south", // 102 |
| 119 | "Chart Feature: Shoal area due west", // 103 |
| 120 | "Chart Feature: Channel obstruction", // 104 |
| 121 | "Chart Feature: Reduced vertical clearance", // 105 |
| 122 | "Chart Feature: Bridge closed", // 106 |
| 123 | "Chart Feature: Bridge partially open", // 107 |
| 124 | "Chart Feature: Bridge fully open", // 108 |
| 125 | "(reserved for future use)", // 109 |
| 126 | "(reserved for future use)", // 110 |
| 127 | "(reserved for future use)", // 111 |
| 128 | "Report from ship: Icing info", // 112 |
| 129 | "(reserved for future use)", // 113 |
| 130 | "Report from ship: Misc information - see associated text field", // 114 |
| 131 | "(reserved for future use)", // 115 |
| 132 | "(reserved for future use)", // 116 |
| 133 | "(reserved for future use)", // 117 |
| 134 | "(reserved for future use)", // 118 |
| 135 | "(reserved for future use)", // 119 |
| 136 | "Route: Recommended route", // 120 |
| 137 | "Route: Alternative route", // 121 |
| 138 | "Route: Recommended route through ice", // 122 |
| 139 | "(reserved for future use)", // 123 |
| 140 | "(reserved for future use)", // 124 |
| 141 | "Other – Define in associated text field", // 125 |
| 142 | "Cancellation – cancel area as identified by Message Linkage ID", // 126 |
| 143 | "Undefined (default)" // 127 |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | Ais8_366_22::Ais8_366_22(const char *nmea_payload, const size_t pad) |
| 148 | : Ais8(nmea_payload, pad), link_id(0), notice_type(0), month(0), day(0), |
| 149 | utc_hour(0), utc_minute(0), duration_minutes(0) { |
| 150 | assert(dac == 366); |
| 151 | assert(fi == 22); |
| 152 | |
| 153 | if (!CheckStatus()) { |
| 154 | return; |
| 155 | } |
| 156 | if (num_bits <= 208 || num_bits >= 1020) { |
| 157 | status = AIS_ERR_BAD_BIT_COUNT; |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | link_id = bits.ToUnsignedInt(56, 10); |
| 162 | notice_type = bits.ToUnsignedInt(66, 7); |
| 163 | month = bits.ToUnsignedInt(73, 4); |
| 164 | day = bits.ToUnsignedInt(77, 5); |
| 165 | utc_hour = bits.ToUnsignedInt(82, 5); |
| 166 | utc_minute = bits.ToUnsignedInt(87, 6); |
| 167 | |
| 168 | duration_minutes = bits.ToUnsignedInt(93, 18); |
| 169 | |
| 170 | const int num_sub_areas = static_cast<int>(floor((num_bits - 111)/90.)); |
| 171 | for (int area_idx = 0; area_idx < num_sub_areas; area_idx++) { |
| 172 | Ais8_366_22_SubArea *area = |
| 173 | ais8_366_22_subarea_factory(bits, 111 + 90*area_idx); |
| 174 | if (area) { |
| 175 | sub_areas.push_back(area); |
| 176 | } else { |
| 177 | status = AIS_ERR_BAD_SUB_SUB_MSG; |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | assert(bits.GetRemaining() == 0); |
| 183 | status = AIS_OK; |
| 184 | } |
| 185 | |
| 186 | Ais8_366_22::~Ais8_366_22() { |
| 187 | // Switch to unique_ptr. |
| 188 | for (size_t i = 0; i < sub_areas.size(); i++) { |
| 189 | delete sub_areas[i]; |
| 190 | #ifndef NDEBUG |
| 191 | sub_areas[i] = NULL; |
| 192 | #endif |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Lookup table for the scale factors to decode the length / distance fields. |
| 197 | // The index is the "Scale Factor" |
| 198 | static int scale_multipliers[4] = {1, 10, 100, 1000}; |
| 199 | |
| 200 | Ais8_366_22_Circle::Ais8_366_22_Circle(const AisBitset &bits, |
| 201 | const size_t offset) { |
| 202 | const int scale_factor = bits.ToUnsignedInt(offset + 3, 2); |
| 203 | position = bits.ToAisPoint(offset + 5, 55); |
| 204 | // TODO(schwehr): precision? And bit counts for radius and spare? |
| 205 | // TODO(schwehr): collapse these numbers |
| 206 | radius_m = |
| 207 | bits.ToUnsignedInt(offset + 60, 12) * scale_multipliers[scale_factor]; |
| 208 | spare = bits.ToUnsignedInt(offset + 72, 16); |
| 209 | } |
| 210 | |
| 211 | Ais8_366_22_Rect::Ais8_366_22_Rect(const AisBitset &bits, |
| 212 | const size_t offset) { |
| 213 | const int scale_factor = bits.ToUnsignedInt(offset + 3, 2); |
| 214 | position = bits.ToAisPoint(offset + 5, 55); |
| 215 | e_dim_m = |
| 216 | bits.ToUnsignedInt(offset + 60, 8) * scale_multipliers[scale_factor]; |
| 217 | n_dim_m = |
| 218 | bits.ToUnsignedInt(offset + 68, 8) * scale_multipliers[scale_factor]; |
| 219 | orient_deg = bits.ToUnsignedInt(offset + 76, 9); |
| 220 | spare = bits.ToUnsignedInt(offset + 85, 5); |
| 221 | } |
| 222 | |
| 223 | Ais8_366_22_Sector::Ais8_366_22_Sector(const AisBitset &bits, |
| 224 | const size_t offset) { |
| 225 | const int scale_factor = bits.ToUnsignedInt(offset + 3, 2); |
| 226 | position = bits.ToAisPoint(offset + 5, 55); |
| 227 | radius_m = |
| 228 | bits.ToUnsignedInt(offset + 60, 12) * scale_multipliers[scale_factor]; |
| 229 | left_bound_deg = bits.ToUnsignedInt(offset + 72, 9); |
| 230 | right_bound_deg = bits.ToUnsignedInt(offset + 81, 9); |
| 231 | } |
| 232 | |
| 233 | Ais8_366_22_Polyline::Ais8_366_22_Polyline(const AisBitset &bits, |
| 234 | const size_t offset) { |
| 235 | const int scale_factor = bits.ToUnsignedInt(offset + 3, 2); |
| 236 | for (size_t i = 0; i < 4; i++) { |
| 237 | const int angle = bits.ToUnsignedInt(offset + 5 + (i*21), 10); |
| 238 | const int dist = bits.ToUnsignedInt(offset + 15 + (i*21), 11) * |
| 239 | scale_multipliers[scale_factor]; |
| 240 | if (dist == 0) { |
| 241 | break; |
| 242 | } |
| 243 | angles.push_back(angle); |
| 244 | dists_m.push_back(dist); |
| 245 | } |
| 246 | spare = bits[offset + 89]; |
| 247 | } |
| 248 | |
| 249 | // TODO(schwehr): Merge with Polyline. |
| 250 | Ais8_366_22_Polygon::Ais8_366_22_Polygon(const AisBitset &bits, |
| 251 | const size_t offset) { |
| 252 | const int scale_factor = bits.ToUnsignedInt(offset + 3, 2); |
| 253 | for (size_t i = 0; i < 4; i++) { |
| 254 | const int angle = bits.ToUnsignedInt(offset + 5 + (i*21), 10); |
| 255 | const int dist = bits.ToUnsignedInt(offset + 15 + (i*21), 11) * |
| 256 | scale_multipliers[scale_factor]; |
| 257 | if (dist == 0) { |
| 258 | break; |
| 259 | } |
| 260 | angles.push_back(angle); |
| 261 | dists_m.push_back(dist); |
| 262 | } |
| 263 | spare = bits[offset + 89]; |
| 264 | } |
| 265 | |
| 266 | Ais8_366_22_Text::Ais8_366_22_Text(const AisBitset &bits, |
| 267 | const size_t offset) { |
| 268 | text = string(bits.ToString(offset + 3, 84)); |
| 269 | spare = bits.ToUnsignedInt(offset + 87, 3); |
| 270 | } |
| 271 | |
| 272 | // Call the appropriate constructor |
| 273 | Ais8_366_22_SubArea * |
| 274 | ais8_366_22_subarea_factory(const AisBitset &bits, |
| 275 | const size_t offset) { |
| 276 | const Ais8_366_22_AreaShapeEnum area_shape = |
| 277 | (Ais8_366_22_AreaShapeEnum)bits.ToUnsignedInt(offset, 3); |
| 278 | |
| 279 | switch (area_shape) { |
| 280 | case AIS8_366_22_SHAPE_CIRCLE: |
| 281 | return new Ais8_366_22_Circle(bits, offset); |
| 282 | case AIS8_366_22_SHAPE_RECT: |
| 283 | return new Ais8_366_22_Rect(bits, offset); |
| 284 | case AIS8_366_22_SHAPE_SECTOR: |
| 285 | return new Ais8_366_22_Sector(bits, offset); |
| 286 | case AIS8_366_22_SHAPE_POLYLINE: |
| 287 | return new Ais8_366_22_Polyline(bits, offset); |
| 288 | case AIS8_366_22_SHAPE_POLYGON: |
| 289 | return new Ais8_366_22_Polygon(bits, offset); |
| 290 | case AIS8_366_22_SHAPE_TEXT: |
| 291 | return new Ais8_366_22_Text(bits, offset); |
| 292 | case AIS8_366_22_SHAPE_RESERVED_6: // FALLTHROUGH |
| 293 | case AIS8_366_22_SHAPE_RESERVED_7: // FALLTHROUGH |
| 294 | // Leave area as 0 to indicate error |
| 295 | break; |
| 296 | case AIS8_366_22_SHAPE_ERROR: |
| 297 | break; |
| 298 | default: |
| 299 | assert(false); |
| 300 | } |
| 301 | return nullptr; |
| 302 | } |
| 303 | |
| 304 | } // namespace libais |