nmea_parser/ais/vdm_t26.rs
1/*
2Copyright 2020-2021 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 26: Multiple Slot Binary Message
22#[derive(Default, Clone, Debug, PartialEq)]
23pub struct MultipleSlotBinaryMessage {
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 /// When 'addressed' flag is on this field contains the parsed destination MMSI.
34 pub dest_mmsi: Option<u32>,
35
36 /// When 'addressed' flag is off and 'structured' flag on this field contains
37 /// application ID which consists of 10-bit DAC and 6-bit FID as in message types 6 and 8.
38 pub app_id: Option<u16>,
39
40 /// Data field of length 0-1004 bits.
41 pub data: BitVec,
42
43 /// Radio status
44 pub radio: u32,
45}
46
47// -------------------------------------------------------------------------------------------------
48
49/// AIS VDM/VDO type 26: Multiple Slot Binary Message
50#[allow(clippy::collapsible_if)]
51pub(crate) fn handle(
52 bv: &BitVec,
53 station: Station,
54 own_vessel: bool,
55) -> Result<ParsedMessage, ParseError> {
56 let addressed = pick_u64(bv, 38, 1) != 0;
57 let structured = pick_u64(bv, 39, 1) != 0;
58
59 Ok(ParsedMessage::MultipleSlotBinaryMessage(
60 MultipleSlotBinaryMessage {
61 own_vessel: { own_vessel },
62 station: { station },
63 mmsi: { pick_u64(bv, 8, 30) as u32 },
64 dest_mmsi: {
65 if addressed {
66 Some(pick_u64(bv, 40, 30) as u32)
67 } else {
68 None
69 }
70 },
71 app_id: {
72 if addressed {
73 None
74 } else if structured {
75 Some(pick_u64(bv, 70, 16) as u16)
76 } else {
77 None
78 }
79 },
80 data: {
81 if addressed {
82 BitVec::from_bitslice(&bv[70..max(70, bv.len() - 20)])
83 } else if structured {
84 BitVec::from_bitslice(&bv[86..max(86, bv.len() - 20)])
85 } else {
86 BitVec::from_bitslice(&bv[40..max(40, bv.len() - 20)])
87 }
88 },
89 radio: { pick_u64(bv, bv.len() - 20, 20) as u32 },
90 },
91 ))
92}
93
94// -------------------------------------------------------------------------------------------------
95
96#[cfg(test)]
97mod test {
98 use super::*;
99
100 #[test]
101 fn test_parse_vdm_type26() {
102 let mut p = NmeaParser::new();
103
104 // Valid message
105 match p.parse_sentence("!AIVDM,1,1,,A,JB3R0GO7p>vQL8tjw0b5hqpd0706kh9d3lR2vbl0400,2*40") {
106 Ok(ps) => {
107 match ps {
108 // The expected result
109 ParsedMessage::MultipleSlotBinaryMessage(msbm) => {
110 assert_eq!(msbm.mmsi, 137920605);
111 assert_eq!(msbm.dest_mmsi, Some(838351848));
112 assert_eq!(msbm.app_id, None);
113 }
114 ParsedMessage::Incomplete => {
115 assert!(false);
116 }
117 _ => {
118 assert!(false);
119 }
120 }
121 }
122 Err(e) => {
123 assert_eq!(e.to_string(), "OK");
124 }
125 }
126
127 // Too short payload
128 match p.parse_sentence("!AIVDM,1,1,,,Jl@bhbmCU`:lwOd0,0*48") {
129 Ok(ps) => {
130 match ps {
131 // The expected result
132 ParsedMessage::MultipleSlotBinaryMessage(msbm) => {
133 assert_eq!(msbm.mmsi, 285913259);
134 assert_eq!(msbm.dest_mmsi, None);
135 assert_eq!(msbm.app_id, Some(16254));
136 }
137 ParsedMessage::Incomplete => {
138 assert!(false);
139 }
140 _ => {
141 assert!(false);
142 }
143 }
144 }
145 Err(e) => {
146 assert_eq!(e.to_string(), "OK");
147 }
148 }
149 }
150}