nmea_parser/ais/
vdm_t12.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 12: Addressed Safety-Related Message
22#[derive(Default, Clone, Debug, PartialEq)]
23pub struct AddressedSafetyRelatedMessage {
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 source_mmsi: u32,
32
33    /// Sequence number (2 bits)
34    pub sequence_number: u8,
35
36    /// Destination MMSI (30 bits)
37    pub destination_mmsi: u32,
38
39    /// Retransmit flag (1 bit)
40    pub retransmit_flag: bool,
41
42    /// Text (936 bits; 1-156 chars)
43    pub text: String,
44}
45
46// -------------------------------------------------------------------------------------------------
47
48/// AIS VDM/VDO type 12: Addressed Safety-Related Message
49pub(crate) fn handle(
50    bv: &BitVec,
51    station: Station,
52    own_vessel: bool,
53) -> Result<ParsedMessage, ParseError> {
54    Ok(ParsedMessage::AddressedSafetyRelatedMessage(
55        AddressedSafetyRelatedMessage {
56            own_vessel: { own_vessel },
57            station: { station },
58            source_mmsi: { pick_u64(bv, 8, 30) as u32 },
59            sequence_number: { pick_u64(bv, 38, 2) as u8 },
60            destination_mmsi: { pick_u64(bv, 40, 30) as u32 },
61            retransmit_flag: { pick_u64(bv, 70, 1) != 0 },
62            text: { pick_string(bv, 72, 156) },
63        },
64    ))
65}
66
67// -------------------------------------------------------------------------------------------------
68
69#[cfg(test)]
70mod test {
71    use super::*;
72
73    #[test]
74    fn test_parse_vdm_type12() {
75        // First message
76        let mut p = NmeaParser::new();
77        match p.parse_sentence(
78            "!AIVDM,1,1,,A,<02:oP0kKcv0@<51C5PB5@?BDPD?P:?2?EB7PDB16693P381>>5<PikP,0*37",
79        ) {
80            Ok(ps) => {
81                match ps {
82                    // The expected result
83                    ParsedMessage::AddressedSafetyRelatedMessage(asrm) => {
84                        assert_eq!(asrm.source_mmsi, 2275200);
85                        assert_eq!(asrm.sequence_number, 0);
86                        assert_eq!(asrm.destination_mmsi, 215724000);
87                        assert!(!asrm.retransmit_flag);
88                        assert_eq!(asrm.text, "PLEASE REPORT TO JOBOURG TRAFFIC CHANNEL 13");
89                    }
90                    ParsedMessage::Incomplete => {
91                        assert!(false);
92                    }
93                    _ => {
94                        assert!(false);
95                    }
96                }
97            }
98            Err(e) => {
99                assert_eq!(e.to_string(), "OK");
100            }
101        }
102
103        // Second message
104        match p.parse_sentence(
105            "!AIVDM,1,1,,A,<CR3B@<0TO3j5@PmkiP31BCPphPDB13;CPihkP=?D?PmP3B5GPpn,0*3A",
106        ) {
107            Ok(ps) => {
108                match ps {
109                    // The expected result
110                    ParsedMessage::AddressedSafetyRelatedMessage(asrm) => {
111                        assert_eq!(asrm.source_mmsi, 237032000);
112                        assert_eq!(asrm.sequence_number, 3);
113                        assert_eq!(asrm.destination_mmsi, 2391100);
114                        assert!(asrm.retransmit_flag);
115                        assert_eq!(asrm.text, "EP 531 CARS 80 TRACKS 103 MOTO 5 CREW 86");
116                    }
117                    ParsedMessage::Incomplete => {
118                        assert!(false);
119                    }
120                    _ => {
121                        assert!(false);
122                    }
123                }
124            }
125            Err(e) => {
126                assert_eq!(e.to_string(), "OK");
127            }
128        }
129    }
130}