| 1 | #include <cmath> |
| 2 | #include <iomanip> |
| 3 | |
| 4 | #include "ais.h" |
| 5 | |
| 6 | namespace libais { |
| 7 | |
| 8 | // TODO(schwehr): field on class |
| 9 | const char *ais8_1_22_shape_names[8] = {"Circle/Pt", "Rect", "Sector", |
| 10 | "Polyline", "Polygon", "Text", |
| 11 | "Reserved_6", "Reserved_7"}; |
| 12 | |
| 13 | const char *ais8_1_22_notice_names[AIS8_1_22_NUM_NAMES] = { |
| 14 | // 0 has extra text compared to the spec |
| 15 | "Caution Area: Marine mammals habitat (implies whales NOT observed)", // 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 | static int scale_multipliers[4] = {1, 10, 100, 1000}; |
| 147 | // For the Scale factor so that we don't have to do a power of 10 calculation |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////// |
| 150 | // Sub-Areas for the Area Notice class |
| 151 | ////////////////////////////////////////////////////////////////////// |
| 152 | |
| 153 | Ais8_1_22_Circle::Ais8_1_22_Circle(const AisBitset &bits, |
| 154 | const size_t offset) { |
| 155 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 156 | position = bits.ToAisPoint(offset + 2, 49); |
| 157 | |
| 158 | precision = bits.ToUnsignedInt(offset + 51, 3); // useless |
| 159 | radius_m = |
| 160 | bits.ToUnsignedInt(offset + 54, 12) * scale_multipliers[scale_factor]; |
| 161 | spare = bits.ToUnsignedInt(offset + 66, 18); |
| 162 | } |
| 163 | |
| 164 | Ais8_1_22_Rect::Ais8_1_22_Rect(const AisBitset &bits, |
| 165 | const size_t offset) { |
| 166 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 167 | position = bits.ToAisPoint(offset + 2, 49); |
| 168 | |
| 169 | precision = bits.ToUnsignedInt(offset + 51, 3); // useless |
| 170 | e_dim_m = |
| 171 | bits.ToUnsignedInt(offset + 54, 8) * scale_multipliers[scale_factor]; |
| 172 | n_dim_m = |
| 173 | bits.ToUnsignedInt(offset + 62, 8) * scale_multipliers[scale_factor]; |
| 174 | orient_deg = bits.ToUnsignedInt(offset + 70, 9); |
| 175 | spare = bits.ToUnsignedInt(offset + 79, 5); |
| 176 | } |
| 177 | |
| 178 | Ais8_1_22_Sector::Ais8_1_22_Sector(const AisBitset &bits, |
| 179 | const size_t offset) { |
| 180 | const int scale = bits.ToUnsignedInt(offset, 2); |
| 181 | position = bits.ToAisPoint(offset + 2, 49); |
| 182 | |
| 183 | precision = bits.ToUnsignedInt(offset + 51, 3); |
| 184 | radius_m = bits.ToUnsignedInt(offset + 54, 12) * scale_multipliers[scale]; |
| 185 | left_bound_deg = bits.ToUnsignedInt(offset + 66, 9); |
| 186 | right_bound_deg = bits.ToUnsignedInt(offset + 75, 9); |
| 187 | } |
| 188 | |
| 189 | // Size of one point angle and distance |
| 190 | static const size_t PT_AD_SIZE = 10 + 10; |
| 191 | |
| 192 | Ais8_1_22_Polyline::Ais8_1_22_Polyline(const AisBitset &bits, |
| 193 | const size_t offset) { |
| 194 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 195 | const int multiplier = scale_multipliers[scale_factor]; |
| 196 | for (size_t i = 0; i < 4; i++) { |
| 197 | const int angle = bits.ToUnsignedInt(offset + 2 + (i*PT_AD_SIZE), 10); |
| 198 | const int dist = |
| 199 | bits.ToUnsignedInt(offset + 12 + (i*PT_AD_SIZE), 10) * multiplier; |
| 200 | if (0 == dist) |
| 201 | break; |
| 202 | angles.push_back(angle); |
| 203 | dists_m.push_back(dist); |
| 204 | } |
| 205 | const int spare_start = offset + AIS8_1_22_SUBAREA_SIZE - 5; |
| 206 | bits.SeekTo(spare_start); |
| 207 | spare = bits.ToUnsignedInt(spare_start, 2); |
| 208 | } |
| 209 | |
| 210 | // TODO(schwehr): fold into polyline |
| 211 | Ais8_1_22_Polygon::Ais8_1_22_Polygon(const AisBitset &bits, |
| 212 | const size_t offset) { |
| 213 | const int scale_factor = bits.ToUnsignedInt(offset, 2); |
| 214 | const int multiplier = scale_multipliers[scale_factor]; |
| 215 | for (size_t i = 0; i < 4; i++) { |
| 216 | const int angle = bits.ToUnsignedInt(offset + 2 + (i*PT_AD_SIZE), 10); |
| 217 | const int dist = |
| 218 | bits.ToUnsignedInt(offset + 12 + (i*PT_AD_SIZE), 10) * multiplier; |
| 219 | if (0 == dist) |
| 220 | break; |
| 221 | angles.push_back(angle); |
| 222 | dists_m.push_back(dist); |
| 223 | } |
| 224 | const int spare_start = offset + AIS8_1_22_SUBAREA_SIZE - 5; |
| 225 | bits.SeekTo(spare_start); |
| 226 | spare = bits.ToUnsignedInt(spare_start, 2); |
| 227 | } |
| 228 | |
| 229 | Ais8_1_22_Text::Ais8_1_22_Text(const AisBitset &bits, |
| 230 | const size_t offset) { |
| 231 | text = string(bits.ToString(offset, 84)); |
| 232 | // TODO(schwehr): spare? |
| 233 | } |
| 234 | |
| 235 | // Call the appropriate constructor |
| 236 | Ais8_1_22_SubArea* |
| 237 | ais8_1_22_subarea_factory(const AisBitset &bits, |
| 238 | const size_t offset) { |
| 239 | const Ais8_1_22_AreaShapeEnum area_shape = |
| 240 | (Ais8_1_22_AreaShapeEnum)bits.ToUnsignedInt(offset, 3); |
| 241 | |
| 242 | switch (area_shape) { |
| 243 | case AIS8_1_22_SHAPE_CIRCLE: |
| 244 | return new Ais8_1_22_Circle(bits, offset + 3); |
| 245 | case AIS8_1_22_SHAPE_RECT: |
| 246 | return new Ais8_1_22_Rect(bits, offset + 3); |
| 247 | case AIS8_1_22_SHAPE_SECTOR: |
| 248 | return new Ais8_1_22_Sector(bits, offset + 3); |
| 249 | case AIS8_1_22_SHAPE_POLYLINE: |
| 250 | return new Ais8_1_22_Polyline(bits, offset + 3); |
| 251 | case AIS8_1_22_SHAPE_POLYGON: |
| 252 | return new Ais8_1_22_Polygon(bits, offset + 3); |
| 253 | case AIS8_1_22_SHAPE_TEXT: |
| 254 | return new Ais8_1_22_Text(bits, offset + 3); |
| 255 | case AIS8_1_22_SHAPE_RESERVED_6: // FALLTHROUGH |
| 256 | case AIS8_1_22_SHAPE_RESERVED_7: // FALLTHROUGH |
| 257 | // Keep area==0 to indicate error. |
| 258 | break; |
| 259 | case AIS8_1_22_SHAPE_ERROR: |
| 260 | break; |
| 261 | default: |
| 262 | assert(false); |
| 263 | } |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | ////////////////////////////////////////////////////////////////////// |
| 269 | // Area Notice class |
| 270 | ////////////////////////////////////////////////////////////////////// |
| 271 | |
| 272 | Ais8_1_22::Ais8_1_22(const char *nmea_payload, const size_t pad) |
| 273 | : Ais8(nmea_payload, pad), link_id(0), notice_type(0), month(0), day(0), |
| 274 | hour(0), minute(0), duration_minutes(0) { |
| 275 | assert(dac == 1); |
| 276 | assert(fi == 22); |
| 277 | |
| 278 | if (!CheckStatus()) { |
| 279 | return; |
| 280 | } |
| 281 | // TODO(schwehr): Make checks more exact. Table 11.3, Circ 289 Annex, page 41 |
| 282 | // Spec is not byte aligned. BAD! |
| 283 | if (num_bits < 198 || num_bits > 984) { |
| 284 | status = AIS_ERR_BAD_BIT_COUNT; |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | bits.SeekTo(56); |
| 289 | link_id = bits.ToUnsignedInt(56, 10); |
| 290 | notice_type = bits.ToUnsignedInt(66, 7); |
| 291 | month = bits.ToUnsignedInt(73, 4); |
| 292 | day = bits.ToUnsignedInt(77, 5); |
| 293 | hour = bits.ToUnsignedInt(82, 5); |
| 294 | minute = bits.ToUnsignedInt(87, 6); |
| 295 | |
| 296 | duration_minutes = bits.ToUnsignedInt(93, 18); |
| 297 | |
| 298 | // Use floor to be able to ignore any spare bits |
| 299 | const int num_sub_areas = static_cast<int>(floor((num_bits - 111)/87.)); |
| 300 | for (int sub_area_idx = 0; sub_area_idx < num_sub_areas; sub_area_idx++) { |
| 301 | const size_t start = 111 + AIS8_1_22_SUBAREA_SIZE*sub_area_idx; |
| 302 | Ais8_1_22_SubArea *sub_area = ais8_1_22_subarea_factory(bits, start); |
| 303 | if (sub_area) { |
| 304 | sub_areas.push_back(sub_area); |
| 305 | } else { |
| 306 | status = AIS_ERR_BAD_SUB_SUB_MSG; |
| 307 | } |
| 308 | } |
| 309 | /* TODO(schwehr): inspect the subareas to make sure they are sane. |
| 310 | - polyline/polygon have a point first |
| 311 | - text has geometry to go through it all |
| 312 | */ |
| 313 | // TODO(schwehr): watch out for mandatory spare bits to byte align payload |
| 314 | // TODO(schwehr): Add assert(bits.GetRemaining() == 0); |
| 315 | if (status == AIS_UNINITIALIZED) |
| 316 | status = AIS_OK; |
| 317 | } |
| 318 | |
| 319 | // TODO(schwehr): Use unique_ptr to manage memory. |
| 320 | Ais8_1_22::~Ais8_1_22() { |
| 321 | for (size_t i = 0; i < sub_areas.size(); i++) { |
| 322 | delete sub_areas[i]; |
| 323 | sub_areas[i] = nullptr; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | } // namespace libais |