-
Notifications
You must be signed in to change notification settings - Fork 41
/
aam.rs
185 lines (163 loc) · 5.28 KB
/
aam.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::parse::TEXT_PARAMETER_MAX_LEN;
use arrayvec::ArrayString;
use nom::{
bytes::complete::is_not,
character::complete::{char, one_of},
combinator::opt,
number::complete::float,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{parse::NmeaSentence, sentences::utils::array_string, Error, SentenceType};
/// AAM - Waypoint Arrival Alarm
///
/// <https://gpsd.gitlab.io/gpsd/NMEA.html#_aam_waypoint_arrival_alarm>
///
/// ```text
/// 1 2 3 4 5 6
/// | | | | | |
/// $--AAM,A,A,x.x,N,c--c*hh<CR><LF>
///
/// Field Number:
/// 1. Status, BOOLEAN, A = Arrival circle entered, V = not passed
/// 2. Status, BOOLEAN, A = perpendicular passed at waypoint, V = not passed
/// 3. Arrival circle radius
/// 4. Units of radius, nautical miles
/// 5. Waypoint ID
/// 6. Checksum
///
/// Example: $GPAAM,A,A,0.10,N,WPTNME*43
/// WPTNME is the waypoint name.
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[derive(Debug, PartialEq)]
pub struct AamData {
pub arrival_circle_entered: Option<bool>,
pub perpendicular_passed: Option<bool>,
pub arrival_circle_radius: Option<f32>,
pub radius_units: Option<char>,
#[cfg_attr(feature = "defmt-03", defmt(Debug2Format))]
pub waypoint_id: Option<ArrayString<TEXT_PARAMETER_MAX_LEN>>,
}
/// Parse AAM message
pub fn parse_aam(sentence: NmeaSentence) -> Result<AamData, Error> {
if sentence.message_id != SentenceType::AAM {
Err(Error::WrongSentenceHeader {
expected: SentenceType::AAM,
found: sentence.message_id,
})
} else {
Ok(do_parse_aam(sentence.data)?)
}
}
fn do_parse_aam(i: &str) -> Result<AamData, Error> {
let (i, arrival_circle_entered) = one_of("AV")(i)?;
let arrival_circle_entered = match arrival_circle_entered {
'A' => Some(true),
'V' => Some(false),
_ => unreachable!(),
};
let (i, _) = char(',')(i)?;
let (i, perpendicular_passed) = one_of("AV")(i)?;
let perpendicular_passed = match perpendicular_passed {
'A' => Some(true),
'V' => Some(false),
_ => unreachable!(),
};
let (i, _) = char(',')(i)?;
let (i, arrival_circle_radius) = opt(float)(i)?;
let (i, _) = char(',')(i)?;
let (i, radius_units) = opt(char('N'))(i)?;
let (i, _) = char(',')(i)?;
let (_i, waypoint_id) = opt(is_not("*"))(i)?;
Ok(AamData {
arrival_circle_entered,
perpendicular_passed,
arrival_circle_radius,
radius_units,
waypoint_id: waypoint_id
.map(array_string::<TEXT_PARAMETER_MAX_LEN>)
.transpose()?,
})
}
#[cfg(test)]
mod tests {
use approx::assert_relative_eq;
use super::*;
use crate::{parse::parse_nmea_sentence, SentenceType};
#[test]
fn parse_aam_with_nmea_sentence_struct() {
let data = parse_aam(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::AAM,
data: "A,V,0.10,N,WPTNME",
checksum: 0x0,
})
.unwrap();
assert!(data.arrival_circle_entered.unwrap());
assert!(!data.perpendicular_passed.unwrap());
assert_relative_eq!(data.arrival_circle_radius.unwrap(), 0.10);
assert_eq!(data.radius_units.unwrap(), 'N');
assert_eq!(&data.waypoint_id.unwrap(), "WPTNME");
}
#[test]
#[should_panic]
fn parse_aam_with_invalid_arrival_circle_entered_value() {
parse_aam(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::AAM,
data: "G,V,0.10,N,WPTNME",
checksum: 0x0,
})
.unwrap();
}
#[test]
#[should_panic]
fn parse_aam_with_invalid_perpendicular_passed_value() {
parse_aam(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::AAM,
data: "V,X,0.10,N,WPTNME",
checksum: 0x0,
})
.unwrap();
}
#[test]
#[should_panic]
fn parse_aam_with_invalid_radius_units_value() {
parse_aam(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::AAM,
data: "V,A,0.10,P,WPTNME",
checksum: 0x0,
})
.unwrap();
}
#[test]
fn parse_aam_full_sentence() {
let sentence = parse_nmea_sentence("$GPAAM,A,A,0.10,N,WPTNME*32").unwrap();
assert_eq!(sentence.checksum, 0x32);
assert_eq!(sentence.calc_checksum(), 0x32);
let data = parse_aam(sentence).unwrap();
assert!(data.arrival_circle_entered.unwrap());
assert!(data.perpendicular_passed.unwrap());
assert_relative_eq!(data.arrival_circle_radius.unwrap(), 0.10);
assert_eq!(data.radius_units.unwrap(), 'N');
assert_eq!(&data.waypoint_id.unwrap(), "WPTNME");
}
#[test]
fn parse_aam_with_wrong_message_id() {
let error = parse_aam(NmeaSentence {
talker_id: "GP",
message_id: SentenceType::ABK,
data: "A,V,0.10,N,WPTNME",
checksum: 0x43,
})
.unwrap_err();
if let Error::WrongSentenceHeader { expected, found } = error {
assert_eq!(expected, SentenceType::AAM);
assert_eq!(found, SentenceType::ABK);
}
}
}