aivdm 0.1.0

no_std, no_alloc parser and encoder for AIS (ITU-R M.1371) messages over NMEA 0183
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Field types shared across multiple ITU-R M.1371 message types.

use crate::bits::{BitReader, BitWriter};
use crate::error::BitError;

/// Undecoded application-specific binary data, as carried by the DAC/FI
/// binary messages (types 6, 8, 25, 26).
///
/// This crate does not attempt to decode every IMO/regional-registered
/// application message; callers who need to interpret the payload can read
/// [`BinaryPayload::bytes`] themselves against the relevant application
/// specification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BinaryPayload<const N: usize> {
    buf: [u8; N],
    bit_len: usize,
}

impl<const N: usize> BinaryPayload<N> {
    /// Number of meaningful bits stored.
    #[must_use]
    pub const fn bit_len(&self) -> usize {
        self.bit_len
    }

    /// The payload, packed MSB-first into bytes (the final byte is
    /// left-aligned if `bit_len` is not a multiple of 8).
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.buf[..self.bit_len.div_ceil(8)]
    }

    pub(crate) fn decode(r: &mut BitReader<'_>, bit_len: usize) -> Result<Self, BitError> {
        if bit_len.div_ceil(8) > N {
            return Err(BitError::OutOfRange {
                field: "BinaryPayload",
            });
        }
        let mut buf = [0u8; N];
        let mut remaining = bit_len;
        let mut i = 0;
        while remaining > 0 {
            let take = remaining.min(8);
            #[allow(
                clippy::cast_possible_truncation,
                reason = "take is clamped to 8 above, fits u32"
            )]
            let byte = r.read_u8(take as u32)?;
            buf[i] = byte << (8 - take);
            remaining -= take;
            i += 1;
        }
        Ok(Self { buf, bit_len })
    }

    pub(crate) fn encode(&self, w: &mut BitWriter<'_>) -> Result<(), BitError> {
        let mut remaining = self.bit_len;
        let mut i = 0;
        while remaining > 0 {
            let take = remaining.min(8);
            #[allow(
                clippy::cast_possible_truncation,
                reason = "take is clamped to 8 above, fits u32"
            )]
            let take_u32 = take as u32;
            let byte = self.buf[i] >> (8 - take);
            w.write_bits(u64::from(byte), take_u32)?;
            remaining -= take;
            i += 1;
        }
        Ok(())
    }

    /// Builds a payload directly from already MSB-first-packed bytes, for tests.
    #[cfg(test)]
    pub(crate) const fn test_from_raw(buf: [u8; N], bit_len: usize) -> Self {
        Self { buf, bit_len }
    }
}

/// A Maritime Mobile Service Identity: a 9-digit numeric station identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Mmsi(u32);

impl Mmsi {
    /// Wraps a raw 30-bit MMSI value.
    #[must_use]
    pub const fn from_raw(raw: u32) -> Self {
        Self(raw)
    }

    /// The raw numeric MMSI value.
    #[must_use]
    pub const fn raw(self) -> u32 {
        self.0
    }
}

impl core::fmt::Display for Mmsi {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:09}", self.0)
    }
}

/// Raw sentinel meaning "longitude not available" (181 degrees, in 1/10000-minute units).
pub const LONGITUDE_NOT_AVAILABLE_RAW: i32 = 181 * 60 * 10_000;
/// Raw sentinel meaning "latitude not available" (91 degrees, in 1/10000-minute units).
pub const LATITUDE_NOT_AVAILABLE_RAW: i32 = 91 * 60 * 10_000;

/// Longitude in 1/10000-minute units (signed 28-bit field in the wire format).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Longitude(i32);

impl Longitude {
    /// Wraps a raw signed longitude value in 1/10000-minute units.
    #[must_use]
    pub const fn from_raw(raw: i32) -> Self {
        Self(raw)
    }

    /// The raw signed value, in 1/10000-minute units.
    #[must_use]
    pub const fn raw(self) -> i32 {
        self.0
    }

    /// Whether this field carries an actual fix rather than the "not available" sentinel.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != LONGITUDE_NOT_AVAILABLE_RAW
    }

    /// The longitude in decimal degrees, or `None` if not available.
    #[must_use]
    pub fn as_degrees(self) -> Option<f64> {
        self.is_available().then(|| f64::from(self.0) / 600_000.0)
    }
}

/// Latitude in 1/10000-minute units (signed 27-bit field in the wire format).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Latitude(i32);

impl Latitude {
    /// Wraps a raw signed latitude value in 1/10000-minute units.
    #[must_use]
    pub const fn from_raw(raw: i32) -> Self {
        Self(raw)
    }

    /// The raw signed value, in 1/10000-minute units.
    #[must_use]
    pub const fn raw(self) -> i32 {
        self.0
    }

    /// Whether this field carries an actual fix rather than the "not available" sentinel.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != LATITUDE_NOT_AVAILABLE_RAW
    }

    /// The latitude in decimal degrees, or `None` if not available.
    #[must_use]
    pub fn as_degrees(self) -> Option<f64> {
        self.is_available().then(|| f64::from(self.0) / 600_000.0)
    }
}

/// Speed over ground, in units of 0.1 knot (10-bit field).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Sog(u16);

impl Sog {
    /// Raw sentinel meaning "not available".
    pub const NOT_AVAILABLE_RAW: u16 = 1023;
    /// Raw sentinel meaning "102.2 knots or higher".
    pub const HIGH_SPEED_RAW: u16 = 1022;

    /// Wraps a raw tenths-of-a-knot value.
    #[must_use]
    pub const fn from_raw(raw: u16) -> Self {
        Self(raw)
    }

    /// The raw tenths-of-a-knot value.
    #[must_use]
    pub const fn raw(self) -> u16 {
        self.0
    }

    /// Whether this field carries an actual speed rather than the "not available" sentinel.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != Self::NOT_AVAILABLE_RAW
    }

    /// The speed in knots, or `None` if not available.
    #[must_use]
    pub fn knots(self) -> Option<f64> {
        self.is_available().then(|| f64::from(self.0) / 10.0)
    }
}

/// Course over ground, in units of 0.1 degree (12-bit field).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Cog(u16);

impl Cog {
    /// Raw sentinel meaning "not available".
    pub const NOT_AVAILABLE_RAW: u16 = 3600;

    /// Wraps a raw tenths-of-a-degree value.
    #[must_use]
    pub const fn from_raw(raw: u16) -> Self {
        Self(raw)
    }

    /// The raw tenths-of-a-degree value.
    #[must_use]
    pub const fn raw(self) -> u16 {
        self.0
    }

    /// Whether this field carries an actual course rather than the "not available" sentinel.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != Self::NOT_AVAILABLE_RAW
    }

    /// The course in decimal degrees, or `None` if not available.
    #[must_use]
    pub fn degrees(self) -> Option<f64> {
        self.is_available().then(|| f64::from(self.0) / 10.0)
    }
}

/// True heading, in whole degrees (9-bit field).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Heading(u16);

impl Heading {
    /// Raw sentinel meaning "not available".
    pub const NOT_AVAILABLE_RAW: u16 = 511;

    /// Wraps a raw whole-degree heading value.
    #[must_use]
    pub const fn from_raw(raw: u16) -> Self {
        Self(raw)
    }

    /// The raw whole-degree value.
    #[must_use]
    pub const fn raw(self) -> u16 {
        self.0
    }

    /// Whether this field carries an actual heading rather than the "not available" sentinel.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != Self::NOT_AVAILABLE_RAW
    }

    /// The heading in whole degrees, or `None` if not available.
    #[must_use]
    pub const fn degrees(self) -> Option<u16> {
        if self.is_available() {
            Some(self.0)
        } else {
            None
        }
    }
}

/// Rate of turn, ITU-R M.1371-encoded (signed 8-bit field).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RateOfTurn(i8);

impl RateOfTurn {
    /// Wraps a raw ITU-R M.1371-encoded rate-of-turn value.
    #[must_use]
    pub const fn from_raw(raw: i8) -> Self {
        Self(raw)
    }

    /// The raw encoded value.
    #[must_use]
    pub const fn raw(self) -> i8 {
        self.0
    }

    /// Whether a rate of turn indicator is present at all.
    #[must_use]
    pub const fn is_available(self) -> bool {
        self.0 != -128
    }

    /// The decoded rate of turn in degrees/minute, or `None` if unavailable,
    /// or if the sensor only reported "turning right/left at more than
    /// 5deg/30s" without an exact rate (raw value `+127`/`-127`).
    #[must_use]
    pub fn degrees_per_minute(self) -> Option<f64> {
        if !self.is_available() || self.0 == 127 || self.0 == -127 {
            return None;
        }
        let x = f64::from(self.0) / 4.733;
        Some(x * x.abs())
    }
}

/// Navigational status (4-bit field), ITU-R M.1371 Table 45.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NavigationStatus {
    /// Under way using engine.
    UnderWayUsingEngine,
    /// At anchor.
    AtAnchor,
    /// Not under command.
    NotUnderCommand,
    /// Restricted maneuverability.
    RestrictedManoeuvrability,
    /// Constrained by her draught.
    ConstrainedByDraught,
    /// Moored.
    Moored,
    /// Aground.
    Aground,
    /// Engaged in fishing.
    EngagedInFishing,
    /// Under way sailing.
    UnderWaySailing,
    /// Reserved for high-speed craft.
    ReservedHsc,
    /// Reserved for wing-in-ground craft.
    ReservedWig,
    /// Reserved (regional: power-driven vessel towing astern).
    ReservedTowingAstern,
    /// Reserved (regional: power-driven vessel pushing ahead/towing alongside).
    ReservedPushingAhead,
    /// Reserved for future use.
    Reserved13,
    /// AIS-SART (active), AIS-MOB, or AIS-EPIRB.
    AisSartMobEpirb,
    /// Not defined (default).
    NotDefined,
}

impl NavigationStatus {
    /// Decodes a raw 4-bit navigational status value.
    #[must_use]
    pub const fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::UnderWayUsingEngine,
            1 => Self::AtAnchor,
            2 => Self::NotUnderCommand,
            3 => Self::RestrictedManoeuvrability,
            4 => Self::ConstrainedByDraught,
            5 => Self::Moored,
            6 => Self::Aground,
            7 => Self::EngagedInFishing,
            8 => Self::UnderWaySailing,
            9 => Self::ReservedHsc,
            10 => Self::ReservedWig,
            11 => Self::ReservedTowingAstern,
            12 => Self::ReservedPushingAhead,
            13 => Self::Reserved13,
            14 => Self::AisSartMobEpirb,
            _ => Self::NotDefined,
        }
    }

    /// Encodes back to the raw 4-bit navigational status value.
    #[must_use]
    pub const fn to_raw(self) -> u8 {
        match self {
            Self::UnderWayUsingEngine => 0,
            Self::AtAnchor => 1,
            Self::NotUnderCommand => 2,
            Self::RestrictedManoeuvrability => 3,
            Self::ConstrainedByDraught => 4,
            Self::Moored => 5,
            Self::Aground => 6,
            Self::EngagedInFishing => 7,
            Self::UnderWaySailing => 8,
            Self::ReservedHsc => 9,
            Self::ReservedWig => 10,
            Self::ReservedTowingAstern => 11,
            Self::ReservedPushingAhead => 12,
            Self::Reserved13 => 13,
            Self::AisSartMobEpirb => 14,
            Self::NotDefined => 15,
        }
    }
}

/// Special maneuver indicator (2-bit field).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ManeuverIndicator {
    /// Not available.
    NotAvailable,
    /// No special maneuver.
    NoSpecialManeuver,
    /// Special maneuver (e.g. regional passing arrangement).
    SpecialManeuver,
    /// Reserved value.
    Reserved,
}

impl ManeuverIndicator {
    /// Decodes a raw 2-bit maneuver indicator value.
    #[must_use]
    pub const fn from_raw(v: u8) -> Self {
        match v {
            0 => Self::NotAvailable,
            1 => Self::NoSpecialManeuver,
            2 => Self::SpecialManeuver,
            _ => Self::Reserved,
        }
    }

    /// Encodes back to the raw 2-bit maneuver indicator value.
    #[must_use]
    pub const fn to_raw(self) -> u8 {
        match self {
            Self::NotAvailable => 0,
            Self::NoSpecialManeuver => 1,
            Self::SpecialManeuver => 2,
            Self::Reserved => 3,
        }
    }
}

/// UTC second timestamp (6-bit field) as used in position reports.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Timestamp {
    /// The UTC second (`0..=59`) at which the report was generated.
    Second(u8),
    /// Positioning system time stamp is not available.
    NotAvailable,
    /// Positioning system is in manual input mode.
    ManualInputMode,
    /// Positioning system is in dead reckoning mode.
    DeadReckoning,
    /// Positioning system is inoperative.
    Inoperative,
}

impl Timestamp {
    /// Decodes a raw 6-bit timestamp value.
    #[must_use]
    pub const fn from_raw(v: u8) -> Self {
        match v {
            0..=59 => Self::Second(v),
            61 => Self::ManualInputMode,
            62 => Self::DeadReckoning,
            63 => Self::Inoperative,
            _ => Self::NotAvailable,
        }
    }

    /// Encodes back to the raw 6-bit timestamp value.
    #[must_use]
    pub const fn to_raw(self) -> u8 {
        match self {
            Self::Second(s) => s,
            Self::NotAvailable => 60,
            Self::ManualInputMode => 61,
            Self::DeadReckoning => 62,
            Self::Inoperative => 63,
        }
    }
}

/// Electronic Position Fixing Device type (4-bit field), ITU-R M.1371 Table 47.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EpfdType {
    /// Not specified / undefined.
    Undefined,
    /// GPS.
    Gps,
    /// GLONASS.
    Glonass,
    /// Combined GPS/GLONASS.
    GpsGlonass,
    /// Loran-C.
    LoranC,
    /// Chayka.
    Chayka,
    /// Integrated navigation system.
    IntegratedNavigationSystem,
    /// Surveyed (fixed position, e.g. a base station).
    Surveyed,
    /// Galileo.
    Galileo,
    /// Reserved/unused value.
    Reserved(u8),
}

impl EpfdType {
    /// Decodes a raw 4-bit EPFD type value.
    #[must_use]
    pub const fn from_raw(v: u8) -> Self {
        match v {
            1 => Self::Gps,
            2 => Self::Glonass,
            3 => Self::GpsGlonass,
            4 => Self::LoranC,
            5 => Self::Chayka,
            6 => Self::IntegratedNavigationSystem,
            7 => Self::Surveyed,
            8 => Self::Galileo,
            0 => Self::Undefined,
            other => Self::Reserved(other),
        }
    }

    /// Encodes back to the raw 4-bit EPFD type value.
    #[must_use]
    pub const fn to_raw(self) -> u8 {
        match self {
            Self::Undefined => 0,
            Self::Gps => 1,
            Self::Glonass => 2,
            Self::GpsGlonass => 3,
            Self::LoranC => 4,
            Self::Chayka => 5,
            Self::IntegratedNavigationSystem => 6,
            Self::Surveyed => 7,
            Self::Galileo => 8,
            Self::Reserved(v) => v,
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    use super::*;
    use std::string::ToString;

    #[test]
    fn mmsi_displays_zero_padded() {
        assert_eq!(Mmsi::from_raw(123).to_string(), "000000123");
    }

    #[test]
    fn navigation_status_roundtrips() {
        for v in 0u8..16 {
            assert_eq!(NavigationStatus::from_raw(v).to_raw(), v);
        }
    }

    #[test]
    fn maneuver_indicator_roundtrips() {
        for v in 0u8..4 {
            assert_eq!(ManeuverIndicator::from_raw(v).to_raw(), v);
        }
    }

    #[test]
    fn timestamp_roundtrips() {
        for v in 0u8..64 {
            assert_eq!(Timestamp::from_raw(v).to_raw(), v);
        }
    }

    #[test]
    fn epfd_type_roundtrips() {
        for v in 0u8..16 {
            assert_eq!(EpfdType::from_raw(v).to_raw(), v);
        }
    }

    #[test]
    fn longitude_not_available_sentinel() {
        let lon = Longitude::from_raw(LONGITUDE_NOT_AVAILABLE_RAW);
        assert!(!lon.is_available());
        assert_eq!(lon.as_degrees(), None);
    }

    #[test]
    fn longitude_known_value() {
        // -73.5 degrees == -73.5 * 600_000
        let lon = Longitude::from_raw(-44_100_000);
        assert_eq!(lon.as_degrees(), Some(-73.5));
    }
}