nmea_parser/ais/vdm_t14.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 14: Safety-Related Broadcast Message
22#[derive(Default, Clone, Debug, PartialEq)]
23pub struct SafetyRelatedBroadcastMessage {
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 /// Text (1-161 ASCII chars)
34 pub text: String,
35}
36
37// -------------------------------------------------------------------------------------------------
38
39/// AIS VDM/VDO type 14: Safety-Related Broadcast Message
40pub(crate) fn handle(
41 bv: &BitVec,
42 station: Station,
43 own_vessel: bool,
44) -> Result<ParsedMessage, ParseError> {
45 Ok(ParsedMessage::SafetyRelatedBroadcastMessage(
46 SafetyRelatedBroadcastMessage {
47 own_vessel: { own_vessel },
48 station: { station },
49 mmsi: { pick_u64(bv, 8, 30) as u32 },
50 text: { pick_string(bv, 40, 161) },
51 },
52 ))
53}
54
55// -------------------------------------------------------------------------------------------------
56
57#[cfg(test)]
58mod test {
59 use super::*;
60
61 #[test]
62 fn test_parse_vdm_type14() {
63 let mut p = NmeaParser::new();
64
65 // First message
66 match p.parse_sentence("!AIVDM,1,1,,A,>5?Per18=HB1U:1@E=B0m<L,2*51") {
67 Ok(ps) => {
68 match ps {
69 // The expected result
70 ParsedMessage::SafetyRelatedBroadcastMessage(srbm) => {
71 assert_eq!(srbm.mmsi, 351809000);
72 assert_eq!(srbm.text, "RCVD YR TEST MSG");
73 }
74 ParsedMessage::Incomplete => {
75 assert!(false);
76 }
77 _ => {
78 assert!(false);
79 }
80 }
81 }
82 Err(e) => {
83 assert_eq!(e.to_string(), "OK");
84 }
85 }
86
87 // Second message
88 match p.parse_sentence("!AIVDM,1,1,,A,>3R1p10E3;;R0USCR0HO>0@gN10kGJp,2*7F") {
89 Ok(ps) => {
90 match ps {
91 // The expected result
92 ParsedMessage::SafetyRelatedBroadcastMessage(srbm) => {
93 assert_eq!(srbm.mmsi, 237008900);
94 assert_eq!(srbm.text, "EP228 IX48 FG3 DK7 PL56.");
95 }
96 ParsedMessage::Incomplete => {
97 assert!(false);
98 }
99 _ => {
100 assert!(false);
101 }
102 }
103 }
104 Err(e) => {
105 assert_eq!(e.to_string(), "OK");
106 }
107 }
108
109 // Third message
110 match p.parse_sentence("!AIVDM,1,1,,A,>4aDT81@E=@,2*2E") {
111 Ok(ps) => {
112 match ps {
113 // The expected result
114 ParsedMessage::SafetyRelatedBroadcastMessage(srbm) => {
115 assert_eq!(srbm.mmsi, 311764000);
116 assert_eq!(srbm.text, "TEST");
117 }
118 ParsedMessage::Incomplete => {
119 assert!(false);
120 }
121 _ => {
122 assert!(false);
123 }
124 }
125 }
126 Err(e) => {
127 assert_eq!(e.to_string(), "OK");
128 }
129 }
130 }
131}