nmea_parser/ais/
vdm_t6.rs

1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use super::*;
18
19// -------------------------------------------------------------------------------------------------
20
21/// Type 6: Binary Addressed Message
22#[derive(Default, Clone, Debug, PartialEq)]
23pub struct BinaryAddressedMessage {
24    /// True if the data is about own vessel, false if about other.
25    pub own_vessel: bool,
26
27    /// AIS station type.
28    pub station: Station,
29
30    /// User ID (30 bits)
31    pub mmsi: u32,
32
33    /// User ID (2 bits)
34    pub sequence_number: u8,
35
36    /// User ID (30 bits)
37    pub destination_mmsi: u32,
38
39    /// Retransmit flag
40    pub retransmit_flag: bool,
41
42    /// Designated area code, DAC (10 bits)
43    pub dac: u16,
44
45    /// Functional ID, FID (6 bits)
46    pub fid: u8,
47    // TODO: data (depending on DAC and FID
48}
49
50impl LatLon for BinaryAddressedMessage {
51    fn latitude(&self) -> Option<f64> {
52        None // TODO: depends on data
53    }
54
55    fn longitude(&self) -> Option<f64> {
56        None // TODO: depends on data
57    }
58}
59
60// -------------------------------------------------------------------------------------------------
61
62/// AIS VDM/VDO type 6: Binary Addressed Message. Implementation of the 920-bit data field is
63/// unimplemented currently.
64pub(crate) fn handle(
65    bv: &BitVec,
66    station: Station,
67    own_vessel: bool,
68) -> Result<ParsedMessage, ParseError> {
69    Ok(ParsedMessage::BinaryAddressedMessage(
70        BinaryAddressedMessage {
71            own_vessel: { own_vessel },
72            station: { station },
73            mmsi: { pick_u64(bv, 8, 30) as u32 },
74            sequence_number: { pick_u64(bv, 38, 2) as u8 },
75            destination_mmsi: { pick_u64(bv, 40, 30) as u32 },
76            retransmit_flag: { pick_u64(bv, 70, 1) != 0 },
77            dac: { pick_u64(bv, 72, 10) as u16 },
78            fid: { pick_u64(bv, 82, 6) as u8 }, // TODO: data (depending on DAC and FID
79        },
80    ))
81}
82
83// -------------------------------------------------------------------------------------------------
84
85#[cfg(test)]
86mod test {
87    use super::*;
88
89    #[test]
90    fn test_parse_vdm_type6() {
91        let mut p = NmeaParser::new();
92        match p.parse_sentence("!AIVDM,1,1,,B,6B?n;be:cbapalgc;i6?Ow4,2*4A") {
93            Ok(ps) => {
94                match ps {
95                    // The expected result
96                    ParsedMessage::BinaryAddressedMessage(bam) => {
97                        assert_eq!(bam.mmsi, 150834090);
98                        assert_eq!(bam.sequence_number, 3);
99                        assert_eq!(bam.destination_mmsi, 313240222);
100                        assert!(!bam.retransmit_flag);
101                        assert_eq!(bam.dac, 669);
102                        assert_eq!(bam.fid, 11);
103                        // TODO: check data
104                    }
105                    ParsedMessage::Incomplete => {
106                        assert!(false);
107                    }
108                    _ => {
109                        assert!(false);
110                    }
111                }
112            }
113            Err(e) => {
114                assert_eq!(e.to_string(), "OK");
115            }
116        }
117    }
118}