nmea_parser/ais/
vdm_t13.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 13: Safety-Related Acknowledgment
22#[derive(Default, Clone, Debug, PartialEq)]
23pub struct SafetyRelatedAcknowledgement {
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    /// Source MMSI (30 bits)
31    pub mmsi: u32,
32
33    /// MMSI number 1 (30 bits)
34    pub mmsi1: u32,
35
36    /// MMSI sequence
37    pub mmsi1_seq: u8,
38
39    /// MMSI number 2 (30 bits)
40    pub mmsi2: u32,
41
42    /// MMSI sequence
43    pub mmsi2_seq: u8,
44
45    /// MMSI number 3 (30 bits)
46    pub mmsi3: u32,
47
48    /// MMSI sequence
49    pub mmsi3_seq: u8,
50
51    /// MMSI number 4 (30 bits)
52    pub mmsi4: u32,
53
54    /// MMSI sequence
55    pub mmsi4_seq: u8,
56}
57
58// -------------------------------------------------------------------------------------------------
59
60/// AIS VDM/VDO type 13: Safety-Related Acknowledgment
61pub(crate) fn handle(
62    bv: &BitVec,
63    station: Station,
64    own_vessel: bool,
65) -> Result<ParsedMessage, ParseError> {
66    Ok(ParsedMessage::SafetyRelatedAcknowledgement(
67        SafetyRelatedAcknowledgement {
68            own_vessel: { own_vessel },
69            station: { station },
70            mmsi: { pick_u64(bv, 8, 30) as u32 },
71            mmsi1: { pick_u64(bv, 40, 30) as u32 },
72            mmsi1_seq: { pick_u64(bv, 70, 2) as u8 },
73            mmsi2: { pick_u64(bv, 72, 30) as u32 },
74            mmsi2_seq: { pick_u64(bv, 102, 2) as u8 },
75            mmsi3: { pick_u64(bv, 104, 30) as u32 },
76            mmsi3_seq: { pick_u64(bv, 134, 2) as u8 },
77            mmsi4: { pick_u64(bv, 136, 30) as u32 },
78            mmsi4_seq: { pick_u64(bv, 166, 2) as u8 },
79        },
80    ))
81}
82
83// -------------------------------------------------------------------------------------------------
84
85#[cfg(test)]
86mod test {
87    use super::*;
88
89    #[test]
90    fn test_parse_vdm_type13() {
91        let mut p = NmeaParser::new();
92        match p.parse_sentence("!AIVDM,1,1,,A,=39UOj0jFs9R,0*65") {
93            Ok(ps) => {
94                match ps {
95                    // The expected result
96                    ParsedMessage::SafetyRelatedAcknowledgement(sra) => {
97                        assert_eq!(sra.mmsi, 211378120);
98                        assert_eq!(sra.mmsi1, 211217560);
99                        assert_eq!(sra.mmsi1_seq, 2);
100                        assert_eq!(sra.mmsi2, 0);
101                        assert_eq!(sra.mmsi2_seq, 0);
102                        assert_eq!(sra.mmsi3, 0);
103                        assert_eq!(sra.mmsi3_seq, 0);
104                        assert_eq!(sra.mmsi4, 0);
105                        assert_eq!(sra.mmsi4_seq, 0);
106                    }
107                    ParsedMessage::Incomplete => {
108                        assert!(false);
109                    }
110                    _ => {
111                        assert!(false);
112                    }
113                }
114            }
115            Err(e) => {
116                assert_eq!(e.to_string(), "OK");
117            }
118        }
119    }
120}