nmea_parser/ais/vdm_t17.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 17: DGNSS Broadcast Binary Message.
22#[derive(Clone, Debug, PartialEq)]
23pub struct DgnssBroadcastBinaryMessage {
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 /// Latitude (17 bits)
34 pub latitude: Option<f64>,
35
36 /// Longitude (18 bits)
37 pub longitude: Option<f64>,
38
39 /// Payload (80-815 bits). Note that it appears to be tied to the now obsolete RTCM2 protocol.
40 pub payload: BitVec,
41}
42
43// -------------------------------------------------------------------------------------------------
44
45/// AIS VDM/VDO type 17: DGNSS Broadcast Binary Message
46pub(crate) fn handle(
47 bv: &BitVec,
48 station: Station,
49 own_vessel: bool,
50) -> Result<ParsedMessage, ParseError> {
51 Ok(ParsedMessage::DgnssBroadcastBinaryMessage(
52 DgnssBroadcastBinaryMessage {
53 own_vessel: { own_vessel },
54 station: { station },
55 mmsi: { pick_u64(bv, 8, 30) as u32 },
56 latitude: {
57 let lat_raw = pick_i64(bv, 58, 17) as i32;
58 if lat_raw != 0xd548 {
59 Some((lat_raw as f64) / 600.0)
60 } else {
61 None
62 }
63 },
64 longitude: {
65 let lon_raw = pick_i64(bv, 40, 18) as i32;
66 if lon_raw != 0x1a838 {
67 Some((lon_raw as f64) / 600.0)
68 } else {
69 None
70 }
71 },
72 payload: bv.iter().skip(80).collect(),
73 },
74 ))
75}
76
77// -------------------------------------------------------------------------------------------------
78
79#[cfg(test)]
80mod test {
81 use super::*;
82
83 #[test]
84 fn test_parse_vdm_type17() {
85 let mut p = NmeaParser::new();
86 match p.parse_sentence(
87 "!AIVDM,2,1,5,A,A02VqLPA4I6C07h5Ed1h<OrsuBTTwS?r:C?w`?la<gno1RTRwSP9:BcurA8a,0*3A",
88 ) {
89 Ok(ps) => match ps {
90 ParsedMessage::Incomplete => {
91 assert!(true);
92 }
93 _ => {
94 assert!(false);
95 }
96 },
97 Err(e) => {
98 assert_eq!(e.to_string(), "OK");
99 }
100 }
101 match p.parse_sentence("!AIVDM,2,2,5,A,:Oko02TSwu8<:Jbb,0*11") {
102 Ok(ps) => {
103 match ps {
104 // The expected result
105 ParsedMessage::DgnssBroadcastBinaryMessage(i) => {
106 assert_eq!(i.mmsi, 2734450);
107 assert::close(i.latitude.unwrap_or(0.0), 59.987, 0.001);
108 assert::close(i.longitude.unwrap_or(0.0), 29.130, 0.001);
109 assert_eq!(i.payload.len(), 376);
110 }
111 ParsedMessage::Incomplete => {
112 assert!(false);
113 }
114 _ => {
115 assert!(false);
116 }
117 }
118 }
119 Err(e) => {
120 assert_eq!(e.to_string(), "OK");
121 }
122 }
123 }
124}