aprs-decode 0.1.2

Robust APRS packet parsing — text (APRS-IS) and binary (AX.25) input
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use crate::callsign::Callsign;
use crate::capabilities::AprsCapabilities;
use crate::digipeater::{Digipeater, parse_via};
use crate::error::AprsError;
use crate::grid::AprsGridLocator;
use crate::item::AprsItem;
use crate::message::AprsMessage;
use crate::mic_e::AprsMicE;
use crate::nmea::AprsNmea;
use crate::object::AprsObject;
use crate::position::AprsPosition;
use crate::query::AprsQuery;
use crate::status::AprsStatus;
use crate::telemetry::AprsTelemetry;
use crate::third_party::AprsThirdParty;
use crate::user_defined::AprsUserDefined;
use crate::weather::AprsPositionlessWeather;

/// A fully decoded APRS packet.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AprsPacket {
    /// The source station (transmitter).
    pub from: Callsign,
    /// The destination callsign (AX.25 destination / APRS path destination).
    pub to: Callsign,
    /// The digipeater path.
    pub via: Vec<Digipeater>,
    /// The parsed packet content, discriminated by Data Type Indicator.
    pub data: AprsData,
}

/// The content of an APRS packet, dispatched by Data Type Indicator (DTI).
///
/// The DTI is the first byte of the AX.25 information field.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum AprsData {
    /// Position report. DTI: `!` `=` `/` `@`
    Position(AprsPosition),
    /// Message, bulletin, ACK/REJ, or telemetry metadata. DTI: `:`
    Message(AprsMessage),
    /// Status report. DTI: `>`
    Status(AprsStatus),
    /// MIC-E compressed position. DTI: `` ` `` `'` `\x1C` `\x1D`
    MicE(AprsMicE),
    /// Object report. DTI: `;`
    Object(AprsObject),
    /// Item report. DTI: `)`
    Item(AprsItem),
    /// Positionless weather report. DTI: `_`
    Weather(AprsPositionlessWeather),
    /// Telemetry data packet. DTI: `T` (followed by `#`)
    Telemetry(AprsTelemetry),
    /// Station capabilities. DTI: `<`
    Capabilities(AprsCapabilities),
    /// General query. DTI: `?`
    Query(AprsQuery),
    /// Maidenhead grid locator. DTI: `[`
    GridLocator(AprsGridLocator),
    /// Raw NMEA sentence. DTI: `$`
    Nmea(AprsNmea),
    /// Third-party forwarded packet. DTI: `}`
    ThirdParty(AprsThirdParty),
    /// User-defined / experimental packet. DTI: `{`
    UserDefined(AprsUserDefined),

    /// Packet type not yet implemented or not recognized.
    /// Preserves the DTI byte and the raw information field for caller inspection.
    Unknown { dti: u8, data: Vec<u8> },
}

impl AprsPacket {
    /// Decode a textual APRS packet (APRS-IS format).
    ///
    /// Expected format: `FROM>TO[,VIA...]:DATA`
    ///
    /// The input is `&[u8]` rather than `&str` because APRS information fields may
    /// contain arbitrary bytes (e.g. MIC-E uses bytes 0x1C and 0x1D).
    pub fn decode_textual(input: &[u8]) -> Result<Self, AprsError> {
        if input.is_empty() {
            return Err(AprsError::EmptyPacket);
        }

        let colon = input
            .iter()
            .position(|&b| b == b':')
            .ok_or(AprsError::MissingInfoDelimiter)?;

        let header = &input[..colon];
        let info = &input[colon + 1..];

        let arrow = header
            .iter()
            .position(|&b| b == b'>')
            .ok_or(AprsError::MissingDestinationDelimiter)?;

        let from_bytes = &header[..arrow];
        let dest_via = &header[arrow + 1..];

        let (to_bytes, via_bytes) = if let Some(comma) = dest_via.iter().position(|&b| b == b',') {
            (&dest_via[..comma], &dest_via[comma + 1..])
        } else {
            (dest_via, &b""[..])
        };

        let from = Callsign::decode_textual(from_bytes)?;
        let to = Callsign::decode_textual(to_bytes)?;
        let via = parse_via(via_bytes)?;
        let data = dispatch_data(info, &to)?;

        Ok(AprsPacket {
            from,
            to,
            via,
            data,
        })
    }

    /// Decode a raw AX.25 UI frame.
    ///
    /// Frame structure:
    ///   Destination (7 bytes) + Source (7 bytes) + Repeaters (0–8 × 7 bytes)
    ///   + Control (0x03) + PID (0xF0) + Information field
    pub fn decode_ax25(input: &[u8]) -> Result<Self, AprsError> {
        if input.len() < 16 {
            return Err(AprsError::Ax25FrameTooShort { len: input.len() });
        }

        let (to, _) = Callsign::decode_ax25(&input[0..7])?;
        let (from, src_eoa) = Callsign::decode_ax25(&input[7..14])?;

        let mut pos = 14usize;
        let mut via = Vec::new();

        if !src_eoa {
            loop {
                if pos + 7 > input.len() {
                    return Err(AprsError::Ax25MissingEoa);
                }
                let (digi_call, eoa) = Callsign::decode_ax25(&input[pos..pos + 7])?;
                let heard = input[pos + 6] & 0x80 != 0;
                via.push(Digipeater::Callsign(digi_call, heard));
                pos += 7;
                if eoa {
                    break;
                }
                if pos >= input.len() {
                    return Err(AprsError::Ax25MissingEoa);
                }
            }
        }

        if pos >= input.len() {
            return Err(AprsError::TruncatedPacket {
                expected: pos + 2,
                got: input.len(),
            });
        }
        if input[pos] != 0x03 {
            return Err(AprsError::Ax25NotUiFrame { byte: input[pos] });
        }
        pos += 1;

        if pos >= input.len() {
            return Err(AprsError::TruncatedPacket {
                expected: pos + 1,
                got: input.len(),
            });
        }
        if input[pos] != 0xF0 {
            return Err(AprsError::Ax25NotAprsPid { byte: input[pos] });
        }
        pos += 1;

        let info = &input[pos..];
        let data = dispatch_data(info, &to)?;

        Ok(AprsPacket {
            from,
            to,
            via,
            data,
        })
    }

    /// Encode this packet to textual APRS-IS format.
    pub fn encode_textual(&self) -> Result<Vec<u8>, AprsError> {
        let mut out = Vec::new();
        self.from.encode_textual(&mut out);
        out.push(b'>');
        self.to.encode_textual(&mut out);
        for digi in &self.via {
            out.push(b',');
            digi.encode_textual(&mut out);
        }
        out.push(b':');
        self.encode_info(&mut out)?;
        Ok(out)
    }

    /// Encode this packet to a raw AX.25 UI frame.
    pub fn encode_ax25(&self) -> Result<Vec<u8>, AprsError> {
        let mut out = Vec::new();
        // Destination (EOA=0, more addresses follow)
        self.to.encode_ax25(&mut out, false);
        // Source (EOA=1 if no digipeaters, else 0)
        let src_eoa = self.via.is_empty();
        self.from.encode_ax25(&mut out, src_eoa);
        // Digipeaters
        for (i, digi) in self.via.iter().enumerate() {
            let is_last = i + 1 == self.via.len();
            match digi {
                Digipeater::Callsign(call, heard) => {
                    call.encode_ax25(&mut out, is_last);
                    // Restore the "has-been-repeated" H-bit (bit 7 of the SSID byte)
                    // so a heard digipeater (`*`) round-trips through AX.25.
                    if *heard && let Some(last) = out.last_mut() {
                        *last |= 0x80;
                    }
                }
                Digipeater::QConstruct(_, gw) => {
                    gw.encode_ax25(&mut out, is_last);
                }
            }
        }
        out.push(0x03); // Control: UI frame
        out.push(0xF0); // PID: no layer-3 (APRS)
        self.encode_info(&mut out)?;
        Ok(out)
    }

    fn encode_info(&self, out: &mut Vec<u8>) -> Result<(), AprsError> {
        match &self.data {
            AprsData::Position(pos) => {
                out.extend_from_slice(&pos.encode());
            }
            AprsData::Message(msg) => {
                out.extend_from_slice(&msg.encode());
            }
            AprsData::Status(s) => {
                out.extend_from_slice(&s.encode());
            }
            AprsData::MicE(m) => {
                out.extend_from_slice(&m.encode());
            }
            AprsData::Object(o) => {
                out.extend_from_slice(&o.encode());
            }
            AprsData::Item(i) => {
                out.extend_from_slice(&i.encode());
            }
            AprsData::Weather(w) => {
                out.extend_from_slice(&w.encode());
            }
            AprsData::Telemetry(t) => {
                out.extend_from_slice(&t.encode());
            }
            AprsData::Capabilities(c) => {
                out.extend_from_slice(&c.encode());
            }
            AprsData::Query(q) => {
                out.extend_from_slice(&q.encode());
            }
            AprsData::GridLocator(g) => {
                out.extend_from_slice(&g.encode());
            }
            AprsData::Nmea(n) => {
                out.extend_from_slice(&n.encode());
            }
            AprsData::ThirdParty(tp) => {
                out.extend_from_slice(&tp.encode()?);
            }
            AprsData::UserDefined(ud) => {
                out.extend_from_slice(&ud.encode());
            }
            AprsData::Unknown { dti: _, data } => {
                out.extend_from_slice(data);
            }
        }
        Ok(())
    }
}

/// Dispatch the information field to the correct packet parser by DTI.
/// For MIC-E the destination callsign is also needed.
fn dispatch_data(info: &[u8], to: &Callsign) -> Result<AprsData, AprsError> {
    let dti = match info.first() {
        Some(&b) => b,
        None => {
            return Ok(AprsData::Unknown {
                dti: 0,
                data: Vec::new(),
            });
        }
    };

    match dti {
        b'!' | b'=' | b'/' | b'@' => AprsPosition::parse(info).map(AprsData::Position),
        b':' => AprsMessage::parse(info).map(AprsData::Message),
        b'>' => AprsStatus::parse(info).map(AprsData::Status),
        b'`' | b'\'' | 0x1C | 0x1D => AprsMicE::parse(info, to).map(AprsData::MicE),
        b';' => AprsObject::parse(info).map(AprsData::Object),
        b')' => AprsItem::parse(info).map(AprsData::Item),
        b'_' => AprsPositionlessWeather::parse(info).map(AprsData::Weather),
        // `T` is telemetry only when followed by `#`; otherwise preserve as Unknown
        // rather than failing the whole packet.
        b'T' if info.get(1) == Some(&b'#') => AprsTelemetry::parse(info).map(AprsData::Telemetry),
        b'<' => Ok(AprsData::Capabilities(AprsCapabilities::parse(info))),
        b'?' => Ok(AprsData::Query(AprsQuery::parse(info))),
        b'[' => AprsGridLocator::parse(info).map(AprsData::GridLocator),
        b'$' => Ok(AprsData::Nmea(AprsNmea::parse(info))),
        b'}' => AprsThirdParty::parse(info).map(AprsData::ThirdParty),
        b'{' => Ok(AprsData::UserDefined(AprsUserDefined::parse(info))),
        _ => Ok(AprsData::Unknown {
            dti,
            data: info.to_vec(),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const POSITION_PACKET: &[u8] = b"W1AW-9>APRS,WIDE1-1,WIDE2-2:!4903.50N/07201.75W-Test";

    const MSG_PACKET: &[u8] = b"KD9ABC>APDR15,qAR,KD9XYZ::W1AW-9   :Hello world{001";

    #[test]
    fn decode_position_full() {
        let pkt = AprsPacket::decode_textual(POSITION_PACKET).unwrap();
        assert_eq!(pkt.from.to_string(), "W1AW-9");
        assert_eq!(pkt.to.to_string(), "APRS");
        assert_eq!(pkt.via.len(), 2);
        assert!(matches!(pkt.data, AprsData::Position(_)));
    }

    #[test]
    fn decode_message_header() {
        let pkt = AprsPacket::decode_textual(MSG_PACKET).unwrap();
        assert_eq!(pkt.from.to_string(), "KD9ABC");
        assert_eq!(pkt.to.to_string(), "APDR15");
        assert_eq!(pkt.via.len(), 1);
        assert!(matches!(pkt.data, AprsData::Message(_)));
    }

    #[test]
    fn empty_input_error() {
        assert!(AprsPacket::decode_textual(b"").is_err());
    }

    #[test]
    fn missing_arrow_error() {
        assert!(AprsPacket::decode_textual(b"W1AW:!hello").is_err());
    }

    #[test]
    fn missing_colon_error() {
        assert!(AprsPacket::decode_textual(b"W1AW>APRS,WIDE1").is_err());
    }

    #[test]
    fn no_via_path() {
        let pkt = AprsPacket::decode_textual(b"W1AW>APRS:>Status text").unwrap();
        assert!(pkt.via.is_empty());
    }

    #[test]
    fn unknown_dti_preserved() {
        let pkt = AprsPacket::decode_textual(b"W1AW>APRS:~custom data").unwrap();
        #[allow(unreachable_patterns)]
        match &pkt.data {
            AprsData::Unknown { dti, data } => {
                assert_eq!(*dti, b'~');
                assert_eq!(data.as_slice(), b"~custom data");
            }
            _ => panic!("expected Unknown"),
        }
    }

    #[test]
    fn telemetry_data_dispatched() {
        let pkt = AprsPacket::decode_textual(b"W1AW>APRS:T#005,10,20,30,40,50,10101010").unwrap();
        assert!(matches!(pkt.data, AprsData::Telemetry(_)));
    }

    #[test]
    fn t_without_hash_is_unknown() {
        // A `T`-prefixed info field that isn't `T#...` must not fail the packet;
        // it falls back to Unknown.
        let pkt = AprsPacket::decode_textual(b"W1AW>APRS:Tno hash here").unwrap();
        match &pkt.data {
            AprsData::Unknown { dti, .. } => assert_eq!(*dti, b'T'),
            other => panic!("expected Unknown, got {other:?}"),
        }
    }

    #[test]
    fn encode_textual_round_trip() {
        let pkt = AprsPacket::decode_textual(POSITION_PACKET).unwrap();
        let encoded = pkt.encode_textual().unwrap();
        assert_eq!(encoded, POSITION_PACKET);
    }

    #[test]
    fn encode_ax25_round_trip() {
        let pkt = AprsPacket::decode_textual(POSITION_PACKET).unwrap();
        let ax25 = pkt.encode_ax25().unwrap();
        let decoded = AprsPacket::decode_ax25(&ax25).unwrap();
        assert_eq!(decoded.from.to_string(), "W1AW-9");
        assert_eq!(decoded.to.to_string(), "APRS");
    }

    #[test]
    fn ax25_struct_round_trip_preserves_heard_bit() {
        // A digipeated packet: WIDE1-1 has been heard (`*`). The H-bit must survive
        // an AX.25 encode→decode cycle so the structures compare equal.
        let pkt =
            AprsPacket::decode_textual(b"W1AW-9>APRS,W0OOD-2*,WIDE1-1:!4903.50N/07201.75W-Test")
                .unwrap();
        let ax25 = pkt.encode_ax25().unwrap();
        let redecoded = AprsPacket::decode_ax25(&ax25).unwrap();
        assert_eq!(pkt, redecoded);
        assert!(matches!(redecoded.via[0], Digipeater::Callsign(_, true)));
        assert!(matches!(redecoded.via[1], Digipeater::Callsign(_, false)));
    }

    // Real-world packet using all-'@' compressed null-position and space csT bytes.
    // The station has no GPS lock and uses '@' placeholders with a malformed (all-space)
    // csT block. The '!' DTI must still dispatch to Position, not Unknown.
    #[test]
    fn null_position_at_signs() {
        let raw = b"N0AMY-3>BEACON,W0OOD-2*,WIDE2*,WIDE1-1:!/@@@@@@@@@@    WWW.TONYTYLER.COM WHERE IS SHAMERFACE SHE IS WITH DOMO";
        let pkt = AprsPacket::decode_textual(raw).unwrap();
        assert_eq!(pkt.from.to_string(), "N0AMY-3");
        assert!(matches!(pkt.data, AprsData::Position(_)));
        if let AprsData::Position(ref pos) = pkt.data {
            assert!(!pos.messaging_supported);
            assert!(pos.timestamp.is_none());
            assert!(pos.comment.starts_with(b"  WWW.TONYTYLER.COM"));
        }
    }
}