nmea_parser/ais/vdm_t16.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 16: Assignment Mode Command
22#[derive(Clone, Debug, PartialEq)]
23pub struct AssignmentModeCommand {
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 // When the message is 96 bits long it is interpreted as an assignment for a single station,
31 // When the message is 144 bits long it is interpreted as a channel assignled for two stations.
32 pub assigned_for_single_station: bool,
33
34 /// Source MMSI (30 bits)
35 pub mmsi: u32,
36
37 /// Destination A MMSI (30 bits)
38 pub mmsi1: u32,
39
40 /// Offset A
41 pub offset1: u16,
42
43 /// Increment A
44 pub increment1: u16,
45
46 /// Destination B MMSI (30 bits)
47 pub mmsi2: Option<u32>,
48
49 /// Offset B
50 pub offset2: Option<u16>,
51
52 /// Increment B
53 pub increment2: Option<u16>,
54}
55
56// -------------------------------------------------------------------------------------------------
57
58/// AIS VDM/VDO type 16: Assignment Mode Command
59pub(crate) fn handle(
60 bv: &BitVec,
61 station: Station,
62 own_vessel: bool,
63) -> Result<ParsedMessage, ParseError> {
64 let single = bv.len() < 144;
65 Ok(ParsedMessage::AssignmentModeCommand(
66 AssignmentModeCommand {
67 own_vessel: { own_vessel },
68 station: { station },
69 assigned_for_single_station: { single },
70 mmsi: { pick_u64(bv, 8, 30) as u32 },
71 mmsi1: { pick_u64(bv, 40, 30) as u32 },
72 offset1: { pick_u64(bv, 70, 12) as u16 },
73 increment1: { pick_u64(bv, 82, 10) as u16 },
74 mmsi2: {
75 if single {
76 None
77 } else {
78 Some(pick_u64(bv, 92, 30) as u32)
79 }
80 },
81 offset2: {
82 if single {
83 None
84 } else {
85 Some(pick_u64(bv, 122, 12) as u16)
86 }
87 },
88 increment2: {
89 if single {
90 None
91 } else {
92 Some(pick_u64(bv, 134, 10) as u16)
93 }
94 },
95 },
96 ))
97}
98
99// -------------------------------------------------------------------------------------------------
100
101#[cfg(test)]
102mod test {
103 use super::*;
104
105 #[test]
106 fn test_parse_vdm_type16() {
107 let mut p = NmeaParser::new();
108 match p.parse_sentence("!AIVDM,1,1,,A,@01uEO@mMk7P<P00,0*18") {
109 Ok(ps) => {
110 match ps {
111 // The expected result
112 ParsedMessage::AssignmentModeCommand(i) => {
113 assert_eq!(i.mmsi, 2053501);
114 assert_eq!(i.mmsi1, 224251000);
115 assert_eq!(i.offset1, 200);
116 assert_eq!(i.increment1, 0);
117 assert_eq!(i.mmsi2, None);
118 assert_eq!(i.offset2, None);
119 assert_eq!(i.increment2, None);
120 }
121 ParsedMessage::Incomplete => {
122 assert!(false);
123 }
124 _ => {
125 assert!(false);
126 }
127 }
128 }
129 Err(e) => {
130 assert_eq!(e.to_string(), "OK");
131 }
132 }
133 }
134}