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