Debian Codegraph @

src/libais/ais6.cpp

raw symbols SCIP
1 // Address Binary Message (ABM) 6
2
3 #include <cmath>
4 #include <iomanip>
5
6 #include "ais.h"
7
8 namespace libais {
9
10 Ais6::Ais6(const char *nmea_payload, const size_t pad)
11 : AisMsg(nmea_payload, pad), seq(0), mmsi_dest(0), retransmit(false),
12 spare(0), dac(0), fi(0) {
13
14 if (!CheckStatus()) {
15 return;
16 }
17
18 // TODO(olafsinram): 46 or rather 56??
19 const int payload_len = num_bits - 46; // in bits w/o DAC/FI
20 if (num_bits < 88 || payload_len < 0 || payload_len > 952) {
21 status = AIS_ERR_BAD_BIT_COUNT;
22 return;
23 }
24
25 assert(message_id == 6);
26
27 bits.SeekTo(38);
28 seq = bits.ToUnsignedInt(38, 2);
29 mmsi_dest = bits.ToUnsignedInt(40, 30);
30 retransmit = !bits[70];
31 spare = bits[71];
32 dac = bits.ToUnsignedInt(72, 10);
33 fi = bits.ToUnsignedInt(82, 6);
34 }
35
36 // http://www.e-navigation.nl/content/monitoring-aids-navigation
37 Ais6_0_0::Ais6_0_0(const char *nmea_payload, const size_t pad)
38 : Ais6(nmea_payload, pad),
39 sub_id(1),
40 voltage(0.0),
41 current(0.0),
42 dc_power_supply(true),
43 light_on(true),
44 battery_low(false),
45 off_position(false),
46 spare2(0) {
47 assert(dac == 0);
48 assert(fi == 0);
49
50 if (!CheckStatus()) {
51 return;
52 }
53
54 if (num_bits != 136) {
55 status = AIS_ERR_BAD_BIT_COUNT;
56 return;
57 }
58
59 bits.SeekTo(88);
60 sub_id = bits.ToUnsignedInt(88, 16);
61 voltage = bits.ToUnsignedInt(104, 12) / 10.0;
62 current = bits.ToUnsignedInt(116, 10) / 10.0;
63 dc_power_supply = bits[126];
64 light_on = bits[127];
65 battery_low = bits[128];
66 off_position = bits[129];
67
68 spare2 = bits.ToUnsignedInt(130, 6);
69
70 assert(bits.GetRemaining() == 0);
71 status = AIS_OK;
72 }
73
74 Ais6_1_0::Ais6_1_0(const char *nmea_payload, const size_t pad)
75 : Ais6(nmea_payload, pad), ack_required(false), msg_seq(0),
76 spare2(0) {
77 assert(dac == 1);
78 assert(fi == 0);
79
80 if (!CheckStatus()) {
81 return;
82 }
83
84 // ITU-1371-5 says 112 to 920 because there must be at least one character.
85 // TODO(schwehr): Are there any examples with no characters in the wild?
86 if (num_bits < 112 || num_bits > 920) {
87 status = AIS_ERR_BAD_BIT_COUNT;
88 return;
89 }
90
91 bits.SeekTo(88);
92 ack_required = bits[88];
93 msg_seq = bits.ToUnsignedInt(89, 11);
94
95 const size_t text_size = 6 * ((num_bits - 100) / 6);
96 const size_t spare2_size = num_bits - 100 - text_size;
97 text = bits.ToString(100, text_size);
98
99 if (!spare2_size)
100 spare2 = 0;
101 else
102 spare2 = bits.ToUnsignedInt(100 + text_size, spare2_size);
103
104 assert(bits.GetRemaining() == 0);
105 status = AIS_OK;
106 }
107
108 Ais6_1_1::Ais6_1_1(const char *nmea_payload, const size_t pad)
109 : Ais6(nmea_payload, pad), ack_dac(0), msg_seq(0), spare2(0) {
110 assert(dac == 1);
111 assert(fi == 1);
112
113 if (!CheckStatus()) {
114 return;
115 }
116
117 if (num_bits != 112) {
118 status = AIS_ERR_BAD_BIT_COUNT;
119 return;
120 }
121
122 bits.SeekTo(88);
123 ack_dac = bits.ToUnsignedInt(88, 10);
124 msg_seq = bits.ToUnsignedInt(98, 11);
125 spare2 = bits.ToUnsignedInt(109, 3);
126
127 assert(bits.GetRemaining() == 0);
128 status = AIS_OK;
129 }
130
131 Ais6_1_2::Ais6_1_2(const char *nmea_payload, const size_t pad)
132 : Ais6(nmea_payload, pad), req_dac(0), req_fi(0) {
133 assert(dac == 1);
134 assert(fi == 2);
135
136 if (!CheckStatus()) {
137 return;
138 }
139
140 if (num_bits != 104) {
141 status = AIS_ERR_BAD_BIT_COUNT;
142 return;
143 }
144
145 bits.SeekTo(88);
146 req_dac = bits.ToUnsignedInt(88, 10);
147 req_fi = bits.ToUnsignedInt(98, 6);
148
149 assert(bits.GetRemaining() == 0);
150 status = AIS_OK;
151 }
152
153 // IFM 3: Capability interrogation - OLD ITU 1371-1
154 Ais6_1_3::Ais6_1_3(const char *nmea_payload, const size_t pad)
155 : Ais6(nmea_payload, pad), req_dac(0), spare2(0), spare3(0), spare4(0) {
156 assert(dac == 1);
157 assert(fi == 3);
158
159 if (!CheckStatus()) {
160 return;
161 }
162
163 if (num_bits != 104 && num_bits != 168) {
164 status = AIS_ERR_BAD_BIT_COUNT;
165 return;
166 }
167
168 bits.SeekTo(88);
169 req_dac = bits.ToUnsignedInt(88, 10);
170 if (num_bits == 104) {
171 // Invalid but often used.
172 spare2 = bits.ToUnsignedInt(98, 6);
173 assert(bits.GetRemaining() == 0);
174 status = AIS_OK;
175 return;
176 }
177 spare2 = bits.ToUnsignedInt(98, 32);
178 spare3 = bits.ToUnsignedInt(130, 32);
179 spare4 = bits.ToUnsignedInt(162, 6);
180
181 assert(bits.GetRemaining() == 0);
182 status = AIS_OK;
183 }
184
185 // IFM 4: Capability reply - OLD ITU 1371-4
186 // TODO(schwehr): WTF? 10 + 128 + 6 == 80 Is this 168 or 232 bits?
187 Ais6_1_4::Ais6_1_4(const char *nmea_payload, const size_t pad)
188 : Ais6(nmea_payload, pad), ack_dac(0), capabilities(),
189 cap_reserved(), spare2(0), spare3(0), spare4(0), spare5(0) {
190 assert(dac == 1);
191 assert(fi == 4);
192
193 if (!CheckStatus()) {
194 return;
195 }
196
197 if (num_bits != 352) {
198 status = AIS_ERR_BAD_BIT_COUNT;
199 return;
200 }
201
202 bits.SeekTo(88);
203 ack_dac = bits.ToUnsignedInt(88, 10);
204 constexpr int kNumFI = 64;
205 for (size_t cap_num = 0; cap_num < kNumFI; cap_num++) {
206 size_t start = 98 + cap_num * 2;
207 capabilities[cap_num] = bits[start];
208 cap_reserved[cap_num] = bits[start + 1];
209 }
210 spare2 = bits.ToUnsignedInt(226, 32);
211 spare3 = bits.ToUnsignedInt(258, 32);
212 spare4 = bits.ToUnsignedInt(290, 32);
213 // This last one is not a full 32 bits to cover the 126 bits of spare.
214 spare5 = bits.ToUnsignedInt(322, 30);
215
216
217 assert(bits.GetRemaining() == 0);
218 status = AIS_OK;
219 }
220
221 // IMO 1371-5 Ack
222 Ais6_1_5::Ais6_1_5(const char *nmea_payload, const size_t pad)
223 : Ais6(nmea_payload, pad), ack_dac(0), ack_fi(0), seq_num(0),
224 ai_available(false), ai_response(0), spare(0), spare2(0) {
225 assert(dac == 1);
226 assert(fi == 5);
227
228 if (!CheckStatus()) {
229 return;
230 }
231
232 if (num_bits != 168) {
233 status = AIS_ERR_BAD_BIT_COUNT;
234 return;
235 }
236
237 bits.SeekTo(88);
238 ack_dac = bits.ToUnsignedInt(88, 10);
239 ack_fi = bits.ToUnsignedInt(98, 6);
240 seq_num = bits.ToUnsignedInt(104, 11);
241 ai_available = static_cast<bool>(bits[115]);
242 ai_response = bits.ToUnsignedInt(116, 3);
243 spare = bits.ToUnsignedInt(119, 32);
244 spare2 = bits.ToUnsignedInt(151, 17);
245
246 assert(bits.GetRemaining() == 0);
247
248 status = AIS_OK;
249 }
250
251 // IMO Circ 289 - Dangerous cargo
252 // See also Circ 236
253 Ais6_1_12::Ais6_1_12(const char *nmea_payload, const size_t pad)
254 : Ais6(nmea_payload, pad), utc_month_dep(0), utc_day_dep(0),
255 utc_hour_dep(0), utc_min_dep(0), utc_month_next(0),
256 utc_day_next(0), utc_hour_next(0), utc_min_next(0),
257 un(0), value(0), value_unit(0), spare2(0) {
258 assert(dac == 1);
259 assert(fi == 12);
260
261 if (!CheckStatus()) {
262 return;
263 }
264
265 if (num_bits != 360) {
266 status = AIS_ERR_BAD_BIT_COUNT;
267 return;
268 }
269
270 // TODO(schwehr): add in the offset of the dest mmsi
271
272 #if 0
273 bits.SeekTo(56);
274 last_port = bits.ToString(56, 30);
275 utc_month_dep = bits.ToUnsignedInt(86, 4);
276 utc_day_dep = bits.ToUnsignedInt(90, 5);
277 utc_hour_dep = bits.ToUnsignedInt(95, 5);
278 utc_min_dep = bits.ToUnsignedInt(100, 6);
279 next_port = bits.ToString(106, 30);
280 utc_month_next = bits.ToUnsignedInt(136, 4); // estimated arrival
281 utc_day_next = bits.ToUnsignedInt(140, 5);
282 utc_hour_next = bits.ToUnsignedInt(145, 5);
283 utc_min_next = bits.ToUnsignedInt(150, 6);
284 main_danger = bits.ToString(156, 120);
285 imo_cat = bits.ToString(276, 24);
286 un = bits.ToUnsignedInt(300, 13);
287 value = bits.ToUnsignedInt(313, 10); // TODO(schwehr): units
288 value_unit = bits.ToUnsignedInt(323, 2);
289 spare = bits.ToUnsignedInt(325, 3);
290 // 360
291 #endif
292
293 // TODO(schwehr): Add assert(bits.GetRemaining() == 0);
294 status = AIS_OK;
295 }
296
297 // 6_1_13 Does not exist
298
299 // IMO Circ 289 - Tidal Window
300 // See also Circ 236
301 Ais6_1_14::Ais6_1_14(const char *nmea_payload, const size_t pad)
302 : Ais6(nmea_payload, pad), utc_month(0), utc_day(0) {
303 // TODO(schwehr): untested - no sample of the correct length yet
304 assert(dac == 1);
305 assert(fi == 14);
306
307 if (!CheckStatus()) {
308 return;
309 }
310
311 if (num_bits != 376) {
312 status = AIS_ERR_BAD_BIT_COUNT;
313 return;
314 }
315
316 bits.SeekTo(88);
317 utc_month = bits.ToUnsignedInt(88, 4);
318 utc_day = bits.ToUnsignedInt(92, 5);
319
320 for (size_t window_num = 0; window_num < 3; window_num++) {
321 Ais6_1_14_Window w;
322 const size_t start = 97 + window_num * 93;
323 // Reversed order for lng/lat.
324 double y = bits.ToInt(start, 27) / 600000.0;
325 double x = bits.ToInt(start + 27, 28) / 600000.0;
326 w.position = AisPoint(x, y);
327 w.utc_hour_from = bits.ToUnsignedInt(start + 55, 5);
328 w.utc_min_from = bits.ToUnsignedInt(start + 60, 6);
329 w.utc_hour_to = bits.ToUnsignedInt(start + 66, 5);
330 w.utc_min_to = bits.ToUnsignedInt(start + 71, 6);
331 w.cur_dir = bits.ToUnsignedInt(start + 77, 9);
332 w.cur_speed = bits.ToUnsignedInt(start + 86, 7) / 10.;
333
334 windows.push_back(w);
335 }
336
337 assert(bits.GetRemaining() == 0);
338 status = AIS_OK;
339 }
340
341 // IMO Circ 289 - Clearance time to enter port
342 Ais6_1_18::Ais6_1_18(const char *nmea_payload, const size_t pad)
343 : Ais6(nmea_payload, pad), link_id(0), utc_month(0), utc_day(0),
344 utc_hour(0), utc_min(0), spare2() {
345 assert(dac == 1);
346 assert(fi == 18);
347
348 if (!CheckStatus()) {
349 return;
350 }
351
352 if (num_bits != 360) {
353 status = AIS_ERR_BAD_BIT_COUNT;
354 return;
355 }
356
357 link_id = bits.ToUnsignedInt(88, 10);
358 utc_month = bits.ToUnsignedInt(98, 4);
359 utc_day = bits.ToUnsignedInt(102, 5);
360 utc_hour = bits.ToUnsignedInt(107, 5);
361 utc_min = bits.ToUnsignedInt(112, 6);
362 port_berth = bits.ToString(118, 120);
363 dest = bits.ToString(238, 30);
364 position = bits.ToAisPoint(268, 49);
365 spare2[0] = bits.ToUnsignedInt(317, 32);
366 spare2[1] = bits.ToUnsignedInt(349, 11);
367
368 assert(bits.GetRemaining() == 0);
369 status = AIS_OK;
370 }
371
372 // IMO Circ 289 - Berthing data
373 Ais6_1_20::Ais6_1_20(const char *nmea_payload, const size_t pad)
374 : Ais6(nmea_payload, pad), link_id(0), length(0), depth(0.0),
375 mooring_position(0), utc_month(0), utc_day(0), utc_hour(0), utc_min(0),
376 services_known(false), services() {
377 assert(dac == 1);
378 assert(fi == 20);
379
380 if (!CheckStatus()) {
381 return;
382 }
383
384 if (num_bits != 360) {
385 status = AIS_ERR_BAD_BIT_COUNT;
386 return;
387 }
388
389 bits.SeekTo(88);
390 link_id = bits.ToUnsignedInt(88, 10);
391 length = bits.ToUnsignedInt(98, 9);
392 depth = bits.ToUnsignedInt(107, 8);
393 mooring_position = bits.ToUnsignedInt(115, 3);
394 utc_month = bits.ToUnsignedInt(118, 4);
395 utc_day = bits.ToUnsignedInt(122, 5);
396 utc_hour = bits.ToUnsignedInt(127, 5);
397 utc_min = bits.ToUnsignedInt(132, 6);
398 services_known = bits[138];
399 for (size_t serv_num = 0; serv_num < 26; serv_num++) {
400 // TODO(schwehr): const int val = bits.ToUnsignedInt(139 + 2*serv_num, 2);
401 services[serv_num]
402 = static_cast<int>(bits.ToUnsignedInt(139 + 2*serv_num, 2));
403 }
404 name = bits.ToString(191, 120);
405 position = bits.ToAisPoint(311, 49);
406
407 assert(bits.GetRemaining() == 0);
408 status = AIS_OK;
409 }
410
411 Ais6_1_25_Cargo::Ais6_1_25_Cargo()
412 : code_type(0), imdg_valid(false), imdg(0), spare_valid(false), spare(0),
413 un_valid(false), un(0), bc_valid(false), bc(0), marpol_oil_valid(false),
414 marpol_oil(0), marpol_cat_valid(false), marpol_cat(0) {}
415
416 // IMO Circ 289 - Dangerous cargo indication 2
417 // See also Circ 236
418 Ais6_1_25::Ais6_1_25(const char *nmea_payload, const size_t pad)
419 : Ais6(nmea_payload, pad), amount_unit(0), amount(0) {
420 assert(dac == 1);
421 assert(fi == 25);
422
423 if (!CheckStatus()) {
424 return;
425 }
426
427 // TODO(schwehr): verify multiple of the size of cargos + header
428 // or padded to a slot boundary
429 // Allowing a message with no payloads
430 // TODO(schwehr): (num_bits - 100) % 17 != 0) is okay
431 if (num_bits < 100 || num_bits > 576) {
432 status = AIS_ERR_BAD_BIT_COUNT;
433 return;
434 }
435 if ((num_bits - 100) % 17 != 0) {
436 status = AIS_ERR_BAD_BIT_COUNT;
437 return;
438 }
439
440 bits.SeekTo(88);
441 amount_unit = bits.ToUnsignedInt(88, 2);
442 amount = bits.ToUnsignedInt(90, 10);
443 const size_t total_cargos = static_cast<int>(floor((num_bits - 100) / 17.));
444 for (size_t cargo_num = 0; cargo_num < total_cargos; cargo_num++) {
445 Ais6_1_25_Cargo cargo;
446 const size_t start = 100 + 17*cargo_num;
447 cargo.code_type = bits.ToUnsignedInt(start, 4);
448
449 // TODO(schwehr): Is this the correct behavior?
450 switch (cargo.code_type) {
451 // No 0
452 case 1: // IMDG Code in packed form
453 cargo.imdg = bits.ToUnsignedInt(start + 4, 7);
454 cargo.imdg_valid = true;
455 cargo.spare = bits.ToUnsignedInt(start + 11, 6);
456 cargo.spare_valid = true;
457 break;
458 case 2: // IGC Code
459 cargo.un = bits.ToUnsignedInt(start + 4, 13);
460 cargo.un_valid = true;
461 break;
462 case 3: // BC Code
463 cargo.bc = bits.ToUnsignedInt(start + 4, 3);
464 cargo.bc_valid = true;
465 cargo.imdg = bits.ToUnsignedInt(start + 7, 7);
466 cargo.imdg_valid = true;
467 cargo.spare = bits.ToUnsignedInt(start + 14, 3);
468 cargo.spare_valid = true;
469 break;
470 case 4: // MARPOL Annex I
471 cargo.marpol_oil = bits.ToUnsignedInt(start + 4, 4);
472 cargo.marpol_oil_valid = true;
473 cargo.spare = bits.ToUnsignedInt(start + 8, 9);
474 cargo.spare_valid = true;
475 break;
476 case 5: // MARPOL Annex II IBC
477 cargo.marpol_cat = bits.ToUnsignedInt(start + 4, 3);
478 cargo.marpol_cat_valid = true;
479 cargo.spare = bits.ToUnsignedInt(start + 7, 10);
480 cargo.spare_valid = true;
481 break;
482 // 6: Regional use
483 // 7: 7-15 reserved for future
484 default:
485 break; // Just push in an all blank record?
486 }
487 cargos.push_back(cargo);
488 }
489
490 assert(bits.GetRemaining() == 0);
491 status = AIS_OK;
492 }
493
494 // TODO(schwehr): 6_1_28 - Modify 8_1_28 once that is debugged
495
496 Ais6_1_32_Window::Ais6_1_32_Window()
497 : from_utc_hour(0), from_utc_min(0), to_utc_hour(0), to_utc_min(0),
498 cur_dir(0), cur_speed(0.0) {}
499
500 // IMO Circ 289 - Tidal window
501 // See also Circ 236
502 Ais6_1_32::Ais6_1_32(const char *nmea_payload, const size_t pad)
503 : Ais6(nmea_payload, pad), utc_month(0), utc_day(0) {
504 assert(dac == 1);
505 assert(fi == 32);
506
507 if (!CheckStatus()) {
508 return;
509 }
510
511 // TODO(schwehr): might get messages with not all windows
512 if (num_bits != 350) {
513 status = AIS_ERR_BAD_BIT_COUNT;
514 return;
515 }
516
517 bits.SeekTo(88);
518 utc_month = bits.ToUnsignedInt(88, 4);
519 utc_day = bits.ToUnsignedInt(92, 5);
520
521 for (size_t window_num = 0; window_num < 3; window_num++) {
522 Ais6_1_32_Window w;
523 const size_t start = 97 + 88*window_num;
524 w.position = bits.ToAisPoint(start, 49);
525 w.from_utc_hour = bits.ToUnsignedInt(start + 49, 5);
526 w.from_utc_min = bits.ToUnsignedInt(start + 54, 6);
527 w.to_utc_hour = bits.ToUnsignedInt(start + 60, 5);
528 w.to_utc_min = bits.ToUnsignedInt(start + 65, 6);
529 w.cur_dir = bits.ToUnsignedInt(start + 71, 9);
530 w.cur_speed = bits.ToUnsignedInt(start + 80, 8) / 10.;
531 windows.push_back(w);
532 }
533
534 assert(bits.GetRemaining() == 0);
535 status = AIS_OK;
536 }
537
538 // IFM 40: people on board - OLD ITU 1371-4
539 Ais6_1_40::Ais6_1_40(const char *nmea_payload, const size_t pad)
540 : Ais6(nmea_payload, pad), persons(0), spare2(0) {
541 assert(dac == 1);
542 assert(fi == 40);
543
544 if (num_bits != 104) {
545 status = AIS_ERR_BAD_BIT_COUNT;
546 return;
547 }
548
549 AisBitset bs;
550 const AIS_STATUS r = bits.ParseNmeaPayload(nmea_payload, pad);
551 if (r != AIS_OK) {
552 status = r;
553 return;
554 }
555
556 bits.SeekTo(88);
557 persons = bits.ToUnsignedInt(88, 13);
558 spare2 = bits.ToUnsignedInt(101, 3);
559
560 assert(bits.GetRemaining() == 0);
561 status = AIS_OK;
562 }
563
564 } // namespace libais