Skip to main content

nmea_kit/ais/messages/
binary_addressed.rs

1//! AIS Type 6 — Addressed Binary Message.
2
3use crate::ais::armor::extract_u32;
4
5/// AIS Type 6 — Addressed Binary Message.
6///
7/// Carries application-specific data identified by DAC (Designated Area Code)
8/// and FID (Functional ID). The `data` field contains raw bits (one byte per bit).
9#[derive(Debug, Clone, PartialEq)]
10pub struct BinaryAddressed {
11    pub mmsi: u32,
12    /// Sequence number (0-3).
13    pub sequence: u8,
14    /// Destination MMSI.
15    pub dest_mmsi: u32,
16    /// Retransmit flag.
17    pub retransmit: bool,
18    /// Designated Area Code.
19    pub dac: u16,
20    /// Functional ID.
21    pub fid: u8,
22    /// Application-specific binary data (raw bits).
23    pub data: Vec<u8>,
24}
25
26impl BinaryAddressed {
27    pub(crate) fn decode(bits: &[u8]) -> Option<Self> {
28        if bits.len() < 88 {
29            return None;
30        }
31        let mmsi = extract_u32(bits, 8, 30)?;
32        let sequence = extract_u32(bits, 38, 2)? as u8;
33        let dest_mmsi = extract_u32(bits, 40, 30)?;
34        let retransmit = extract_u32(bits, 70, 1)? == 1;
35        let dac = extract_u32(bits, 72, 10)? as u16;
36        let fid = extract_u32(bits, 82, 6)? as u8;
37        let data = if bits.len() > 88 {
38            bits[88..].to_vec()
39        } else {
40            Vec::new()
41        };
42        Some(Self {
43            mmsi,
44            sequence,
45            dest_mmsi,
46            retransmit,
47            dac,
48            fid,
49            data,
50        })
51    }
52}