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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use std::error::Error;
use std::str::FromStr;
use std::fmt;

/// Human-readable protocol identifiers. These are mostly from the AX.25 2.2 spec which has far more examples than 2.0.
#[derive(Debug, PartialEq)]
pub enum ProtocolIdentifier {
    Layer3Impl,
    X25Plp,
    CompressedTcpIp,
    UncompressedTcpIp,
    SegmentationFragment,
    TexnetDatagram,
    LinkQuality,
    Appletalk,
    AppletalkArp,
    ArpaIp,
    ArpaAddress,
    Flexnet,
    NetRom,
    None,
    Escape,
    Unknown(u8)
}

impl ProtocolIdentifier {
    fn from_byte(byte: &u8) -> ProtocolIdentifier {
        match *byte {
            pid if pid & 0b00110000 == 0b00010000
                || pid & 0b00110000 == 0b00100000 => ProtocolIdentifier::Layer3Impl,
            0x01 => ProtocolIdentifier::X25Plp,
            0x06 => ProtocolIdentifier::CompressedTcpIp,
            0x07 => ProtocolIdentifier::UncompressedTcpIp,
            0x08 => ProtocolIdentifier::SegmentationFragment,
            0xC3 => ProtocolIdentifier::TexnetDatagram,
            0xC4 => ProtocolIdentifier::LinkQuality,
            0xCA => ProtocolIdentifier::Appletalk,
            0xCB => ProtocolIdentifier::AppletalkArp,
            0xCC => ProtocolIdentifier::ArpaIp,
            0xCD => ProtocolIdentifier::ArpaAddress,
            0xCE => ProtocolIdentifier::Flexnet,
            0xCF => ProtocolIdentifier::NetRom,
            0xF0 => ProtocolIdentifier::None,
            0xFF => ProtocolIdentifier::Escape,
            pid => ProtocolIdentifier::Unknown(pid)
        }
    }

    fn to_byte(&self) -> u8 {
        match self {
            &ProtocolIdentifier::Layer3Impl => 0b00010000,
            &ProtocolIdentifier::X25Plp => 0x01,
            &ProtocolIdentifier::CompressedTcpIp => 0x06,
            &ProtocolIdentifier::UncompressedTcpIp => 0x07,
            &ProtocolIdentifier::SegmentationFragment => 0x08,
            &ProtocolIdentifier::TexnetDatagram => 0xC3,
            &ProtocolIdentifier::LinkQuality => 0xC4,
            &ProtocolIdentifier::Appletalk => 0xCA,
            &ProtocolIdentifier::AppletalkArp => 0xCB,
            &ProtocolIdentifier::ArpaIp => 0xCC,
            &ProtocolIdentifier::ArpaAddress => 0xCD,
            &ProtocolIdentifier::Flexnet => 0xCE,
            &ProtocolIdentifier::NetRom => 0xCF,
            &ProtocolIdentifier::None => 0xF0,
            &ProtocolIdentifier::Escape => 0xFF,
            &ProtocolIdentifier::Unknown(pid) => pid
        }
    }
}

/// Indicates whether a given frame is a Command or a Response.
#[derive(Debug, PartialEq)]
pub enum CommandResponse {
    Command,
    Response
}

/// Information (I) frame
#[derive(Debug, PartialEq)]
pub struct Information {
    pub pid: ProtocolIdentifier,
    pub info: Vec<u8>,
    pub receive_sequence: u8,
    pub send_sequence: u8,
    pub poll: bool
}

/// RR Supervisory (S) frame
#[derive(Debug, PartialEq)]
pub struct ReceiveReady {
    pub receive_sequence: u8,
    pub poll_or_final: bool
}

/// RNR Supervisory (S) frame
#[derive(Debug, PartialEq)]
pub struct ReceiveNotReady {
    pub receive_sequence: u8,
    pub poll_or_final: bool
}

/// REJ Supervisory (S) frame
#[derive(Debug, PartialEq)]
pub struct Reject {
    pub receive_sequence: u8,
    pub poll_or_final: bool
}

/// SABM Unnumbered (U) frame
#[derive(Debug, PartialEq)]
pub struct SetAsynchronousBalancedMode {
    pub poll: bool
}

/// DISC Unnumbered (U) frame
#[derive(Debug, PartialEq)]
pub struct Disconnect {
    pub poll: bool
}

/// DM Unnumbered (U) frame
#[derive(Debug, PartialEq)]
pub struct DisconnectedMode {
    pub final_bit: bool // 'final' is a rust keyword
}

/// UA Unnumbered (U) frame
#[derive(Debug, PartialEq)]
pub struct UnnumberedAcknowledge {
    pub final_bit: bool
}

/// FRMR Unnumbered (U) frame. Flags correspond to names in the AX.25 specification.
#[derive(Debug, PartialEq)]
pub struct FrameReject {
    pub final_bit: bool,
    /// A raw copy of the control field in the frame that was rejected
    pub rejected_control_field_raw: u8,
    /// The attached control field contained an invalid Receive Sequence Number
    pub z: bool,
    /// The information field of a received frame exceeded the maximum allowable length.
    pub y: bool,
    /// A U or S frame was received that contained an information field.
    pub x: bool,
    /// The received control field was invalid or not implemented.
    pub w: bool,
    pub receive_sequence: u8,
    pub send_sequence: u8,
    pub command_response: CommandResponse
}

/// UI Unnumbered Information frame
#[derive(Debug, PartialEq)]
pub struct UnnumberedInformation {
    pub pid: ProtocolIdentifier,
    pub info: Vec<u8>,
    pub poll_or_final: bool
}

/// Placeholder for when the Address part was parseable but not the control field
#[derive(Debug, PartialEq)]
pub struct UnknownContent {
    pub raw: Vec<u8>
}

/// The body of the frame after the end of the address field
#[derive(Debug, PartialEq)]
pub enum FrameContent {
    Information(Information),
    ReceiveReady(ReceiveReady),
    ReceiveNotReady(ReceiveNotReady),
    Reject(Reject),
    SetAsynchronousBalancedMode(SetAsynchronousBalancedMode),
    Disconnect(Disconnect),
    DisconnectedMode(DisconnectedMode),
    UnnumberedAcknowledge(UnnumberedAcknowledge),
    FrameReject(FrameReject),
    UnnumberedInformation(UnnumberedInformation),
    UnknownContent(UnknownContent)
}

impl FrameContent {
    fn encode(&self) -> Vec<u8> {
        let mut encoded = Vec::new();

        match *self {
            FrameContent::Information(ref i) => {
                let mut c: u8 = 0;
                c |= (i.receive_sequence & 0b0000_0111) << 5;
                c |= if i.poll { 1 << 4 } else { 0 };
                c |= (i.send_sequence & 0b0000_0111) << 1;
                encoded.push(c);
                encoded.push(i.pid.to_byte());
                encoded.extend(&i.info);
            },
            FrameContent::ReceiveReady(ref rr) => {
                let mut c: u8 = 0b0000_0001;
                c |= if rr.poll_or_final { 1 << 4 } else { 0 };
                c |= (rr.receive_sequence & 0b0000_0111) << 5;
                encoded.push(c);
            },
            FrameContent::ReceiveNotReady(ref rnr) => {
                let mut c: u8 = 0b0000_0101;
                c |= if rnr.poll_or_final { 1 << 4 } else { 0 };
                c |= (rnr.receive_sequence & 0b0000_0111) << 5;
                encoded.push(c);
            },
            FrameContent::Reject(ref rej) => {
                let mut c: u8 = 0b0000_1001;
                c |= if rej.poll_or_final { 1 << 4 } else { 0 };
                c |= (rej.receive_sequence & 0b0000_0111) << 5;
                encoded.push(c);
            },
            FrameContent::SetAsynchronousBalancedMode(ref sabm) => {
                let mut c: u8 = 0b0010_1111;
                c |= if sabm.poll { 1 << 4 } else { 0 };
                encoded.push(c);
            },
            FrameContent::Disconnect(ref disc) => {
                let mut c: u8 = 0b0100_0011;
                c |= if disc.poll { 1 << 4 } else { 0 };
                encoded.push(c);
            },
            FrameContent::DisconnectedMode(ref dm) => {
                let mut c: u8 = 0b0000_1111;
                c |= if dm.final_bit { 1 << 4 } else { 0 };
                encoded.push(c);
            },
            FrameContent::UnnumberedAcknowledge(ref ua) => {
                let mut c: u8 = 0b0110_0011;
                c |= if ua.final_bit { 1 << 4 } else { 0 };
                encoded.push(c);
            },
            FrameContent::FrameReject(ref fr) => {
                let mut c: u8 = 0b1000_0111;
                c |= if fr.final_bit { 1 << 4 } else { 0 };
                encoded.push(c);
                let mut frmr1: u8 = 0;
                frmr1 |= if fr.z { 1 << 3 } else { 0 };
                frmr1 |= if fr.y { 1 << 2 } else { 0 };
                frmr1 |= if fr.x { 1 << 1 } else { 0 };
                frmr1 |= if fr.w { 1 << 0 } else { 0 };
                encoded.push(frmr1);
                let mut frmr2: u8 = 0;
                frmr2 |= (fr.receive_sequence & 0b0000_0111) << 5;
                frmr2 |= if fr.command_response == CommandResponse::Response { 1 << 4 } else { 0 };
                frmr2 |= (fr.send_sequence & 0b0000_0111) << 1;
                encoded.push(frmr2);
                encoded.push(fr.rejected_control_field_raw);
            },
            FrameContent::UnnumberedInformation(ref ui) => {
                let mut c: u8 = 0b0000_0011;
                c |= if ui.poll_or_final { 1 << 4 } else { 0 };
                encoded.push(c);
                encoded.push(ui.pid.to_byte());
                encoded.extend(&ui.info);
            },
            FrameContent::UnknownContent(ref uc) => {
                encoded.extend(&uc.raw);
            }
        }

        encoded
    }
}

/// Error type when parsing fails due to a malformed frame
#[derive(Debug, Default)]
pub struct ParseError {
    msg: String
}
impl ParseError {
    fn new() -> ParseError {
        ParseError { msg: "Parse error".to_string() }
    }
}
impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}
impl Error for ParseError {
    fn description(&self) -> &str {
        return &self.msg;
    }
}

fn parse_err<T>(msg: &str) -> Result<T,Box<Error>> {
    Err(Box::new(ParseError { msg: msg.to_string() }))
}

/// A source or destination of an AX.25 frame, combining a callsign with an SSID.
#[derive(Debug, PartialEq)]
pub struct Address {
    // An alphanumeric ASCII callsign of maximum length 6, e.g. "VK7NTK"
    pub callsign: String,
    /// Secondary Station Identifier, from 0 to 15
    pub ssid: u8,
    c_bit: bool
}

impl Address {
    fn to_bytes(&self, high_bit: bool, final_in_address: bool) -> Vec<u8> {
        let mut encoded = Vec::new();
        // Shift by one bit as required for AX.25 address encoding
        for b in self.callsign.as_bytes() {
            encoded.push(b << 1);
        }
        // Pad with spaces up to length 6
        while encoded.len() != 6 {
            encoded.push(b' ' << 1);
        }
        // Now do the SSID byte
        let high = if high_bit { 0b1000_0000 } else { 0 };
        let low = if final_in_address { 0b0000_0001 } else { 0 };
        let ssid_byte = (self.ssid << 1) | 0b0110_0000 | high | low; 
        encoded.push(ssid_byte);

        encoded
    }
}

impl Default for Address {
    fn default() -> Address {
        Address { callsign: "NOCALL".to_string(), ssid: 0, c_bit: false }
    }
}

impl fmt::Display for Address {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let ssid_str = match self.ssid {
            0 => "".to_string(),
            ssid => format!("-{}", ssid)
        };
        write!(f, "{}{}", self.callsign, ssid_str)
    }
}

impl FromStr for Address {
    type Err = Box<Error>;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split("-").collect();
        if parts.len() != 2 {
            return parse_err("Address must be of the form CALL-#");
        }

        let callsign = parts[0].to_uppercase();
        if callsign.len() == 0 || callsign.len() > 6 {
            return parse_err("Callsign must be 1-6 letters/numbers");
        }
        for c in callsign.chars() {
            if !c.is_alphanumeric() {
                return parse_err("Callsign must be alphanumeric only (space padding is handled internally)");
            }
        }
        
        let ssid = parts[1].parse::<u8>()?;
        if ssid > 15 {
            return parse_err("SSID must be from 0 to 15");
        }

        // c_bit will be set on transmit
        Ok(Address { callsign: callsign, ssid: ssid, c_bit: false })
    }
}

/// A single hop in the frame's route
#[derive(Debug)]
pub struct RouteEntry {
    pub repeater: Address,
    pub has_repeated: bool
}

/// A strongly-typed representation of a single AX.25 frame.
#[derive(Debug)]
pub struct Ax25Frame {
    pub source: Address,
    pub destination: Address,
    /// The route the frame has taken/will take according to repeater entries in the address field
    pub route: Vec<RouteEntry>,
    /// AX.25 2.0-compliant stations will indicate in every frame whether it is a command
    /// or a response, as part of the address field.
    pub command_or_response: Option<CommandResponse>,
    pub content: FrameContent
}

impl Ax25Frame {
    /// Returns a UTF-8 string that is a "best effort" at displaying the information
    /// content of this frame. Returns None if there is no information field present.
    /// Most applications will need to work with the Vec<u8> info directly.
    pub fn info_string_lossy(&self) -> Option<String> {
        match self.content {
            FrameContent::Information(ref i)
                => Some(String::from_utf8_lossy(&i.info).into_owned()),
            FrameContent::UnnumberedInformation(ref ui)
                => Some(String::from_utf8_lossy(&ui.info).into_owned()),
            _ => None
        }
    }

    /// Parse raw bytes into an Ax25Frame if possible.
    pub fn from_bytes(bytes: &[u8]) -> Result<Ax25Frame, Box<Error>> {
        // Skip over leading null bytes
        // Linux AF_PACKET has oen of these - we will strip it out in the linux module
        // but also keep the protection here
        let addr_start = bytes.iter().position(|&c| c != 0).ok_or(ParseError::new())?;
        let addr_end = bytes.iter().position(|&c| c & 0x01 == 0x01).ok_or(ParseError::new())?;
        let control = addr_end + 1;
        if addr_end - addr_start + 1 < 14 { // +1 because the "terminator" is actually within the last byte
            return parse_err(&format!("Address field too short: {} {}", addr_start, addr_end));
        }
        if control >= bytes.len() {
            return parse_err(&format!("Packet is unreasonably short: {} bytes", bytes.len() ));
        }
        
        let dest = parse_address(&bytes[addr_start..addr_start+7])?;
        let src = parse_address(&bytes[addr_start+7..addr_start+14])?;
        let rpt_count = (addr_end + 1 - addr_start - 14) / 7;
        let mut route: Vec<RouteEntry> = Vec::new();
        for i in 0..rpt_count {
            let repeater = parse_address(&bytes[addr_start + 14 + i * 7 .. addr_start + 14 + (i+1) * 7])?;
            let entry = RouteEntry {
                has_repeated: repeater.c_bit, // The "C" bit in an address happens to be the repeated bit for a repeater
                repeater: repeater,
            };
            route.push(entry);
        }

        let content = parse_content(&bytes[control..])?;
        let command_or_response = match (dest.c_bit, src.c_bit) {
            (true, false) => Some(CommandResponse::Command),
            (false, true) => Some(CommandResponse::Response),
            _ => None
        };

        Ok(Ax25Frame {
            source: src,
            destination: dest,
            route: route,
            content: content,
            command_or_response: command_or_response
        })
    }

    /// Encode an Ax25Frame struct as raw bytes for transmission
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut frame = Vec::new();
        let (dest_c_bit, src_c_bit) = match self.command_or_response {
            Some(CommandResponse::Command) => (true, false),
            Some(CommandResponse::Response) => (false, true),
            _ => (true, false) // assume Command
        };
        frame.extend(self.destination.to_bytes(dest_c_bit, false));
        frame.extend(self.source.to_bytes(src_c_bit, self.route.is_empty()));

        for (i, entry) in self.route.iter().enumerate() {
            frame.extend(entry.repeater.to_bytes(entry.has_repeated, i+1 == self.route.len()));
        }

        frame.extend(self.content.encode());
        frame
    }
}

impl fmt::Display for Ax25Frame {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let info_display = match self.info_string_lossy() {
            Some(ref info) => info.clone(),
            None => "-".to_string()
        };
        write!(f, "Source\t\t{}\nDestination\t{}\n\
            Data\t\t\"{}\"",
            self.source, self.destination, info_display)
    }
}

fn parse_address(bytes: &[u8]) -> Result<Address, Box<Error>> {
    let mut dest_utf8: Vec<u8> = bytes[0..6].iter()
        .rev()
        .map(|&c| c >> 1)
        .skip_while(|&c| c == b' ')
        .collect::<Vec<u8>>();
    dest_utf8.reverse();
    Ok(Address {
        callsign: String::from_utf8(dest_utf8)?,
        ssid: (bytes[6] >> 1) & 0x0f,
        c_bit: bytes[6] & 0b1000_0000 > 0
    })
}

fn parse_i_frame(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    if bytes.len() < 2 {
        return parse_err("Missing PID field");
    }
    let c = bytes[0]; // control octet
    Ok(FrameContent::Information( Information {
        receive_sequence: (c & 0b1110_0000) >> 5,
        send_sequence: (c & 0b0000_1110) >> 1,
        poll: (c & 0b0001_0000) > 0,
        pid: ProtocolIdentifier::from_byte(&bytes[1]),
        info: bytes[2..].iter().cloned().collect() // could be empty vec
    }))
}

fn parse_s_frame(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    // These all have the same general layout
    // There should be no PID or info following this control byte
    let c = bytes[0];
    let n_r = (c & 0b1110_0000) >> 5;
    let poll_or_final = (c & 0b0001_0000) > 0;

    match c & 0b0000_1111 {
        0b0000_0001 => Ok(FrameContent::ReceiveReady( ReceiveReady {
            receive_sequence: n_r,
            poll_or_final: poll_or_final
        })),
        0b0000_0101 => Ok(FrameContent::ReceiveNotReady( ReceiveNotReady {
            receive_sequence: n_r,
            poll_or_final: poll_or_final
        })),
        0b0000_1001 => Ok(FrameContent::Reject( Reject {
            receive_sequence: n_r,
            poll_or_final: poll_or_final
        })),
        _ => parse_err("Unrecognised S field type")
    }
}

fn parse_u_frame(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    // The only moving part in control for U frames is the P/F bit
    // Two special cases to handle:
    // FRMR is followed by a 3-byte information field that must be parsed specially
    // UI is followed by PID and variable length information field
    let c = bytes[0];
    let poll_or_final = c & 0b0001_0000 > 0;

    // Ignore the P/F bit for identifying the command or response
    match c & 0b1110_1111 {
        0b0010_1111 => Ok(FrameContent::SetAsynchronousBalancedMode( SetAsynchronousBalancedMode { poll: poll_or_final })),
        0b0100_0011 => Ok(FrameContent::Disconnect( Disconnect { poll: poll_or_final })),
        0b0000_1111 => Ok(FrameContent::DisconnectedMode( DisconnectedMode { final_bit: poll_or_final })),
        0b0110_0011 => Ok(FrameContent::UnnumberedAcknowledge( UnnumberedAcknowledge { final_bit: poll_or_final })),
        0b1000_0111 => parse_frmr_frame(bytes),
        0b0000_0011 => parse_ui_frame(bytes),
        _ => parse_err("Unrecognised U field type")
    }
}

fn parse_ui_frame(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    if bytes.len() < 2 {
        return parse_err("Missing PID field");
    }
    // Control, then PID, then Info
    Ok(FrameContent::UnnumberedInformation( UnnumberedInformation {
        poll_or_final: bytes[0] & 0b0001_0000 > 0,
        pid: ProtocolIdentifier::from_byte(&bytes[1]),
        info: bytes[2..].iter().cloned().collect()
    }))
}

fn parse_frmr_frame(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    // Expect 24 bits following the control
    if bytes.len() != 4 {
        return parse_err("Wrong size for FRMR info");
    }
    Ok(FrameContent::FrameReject( FrameReject {
        final_bit: bytes[0] & 0b0001_0000 > 0,
        rejected_control_field_raw: bytes[3],
        z: bytes[1] & 0b0000_1000 > 0,
        y: bytes[1] & 0b0000_0100 > 0,
        x: bytes[1] & 0b0000_0010 > 0,
        w: bytes[1] & 0b0000_0001 > 0,
        receive_sequence: (bytes[2] & 0b1110_0000) >> 5,
        command_response: match bytes[2] & 0b0001_0000 > 0 {
            true => CommandResponse::Response,
            false => CommandResponse::Command
        },
        send_sequence: (bytes[2] & 0b0000_1110) >> 1
    }))
}

/// Parse the content of the frame starting from the control field
fn parse_content(bytes: &[u8]) -> Result<FrameContent, Box<Error>> {
    if bytes.len() == 0 {
        return parse_err("Zero content length");
    }
    match bytes[0] {
        c if c & 0x01 == 0x00 => parse_i_frame(bytes),
        c if c & 0x03 == 0x01 => parse_s_frame(bytes),
        c if c & 0x03 == 0x03 => parse_u_frame(bytes),
        _ => Ok(FrameContent::UnknownContent( UnknownContent {
            raw: bytes.to_vec()
        }))
    }
}


#[test]
fn pid_test() {
    assert_eq!(ProtocolIdentifier::from_byte(&0x01), ProtocolIdentifier::X25Plp);
    assert_eq!(ProtocolIdentifier::from_byte(&0xCA), ProtocolIdentifier::Appletalk);
    assert_eq!(ProtocolIdentifier::from_byte(&0xFF), ProtocolIdentifier::Escape);
    assert_eq!(ProtocolIdentifier::from_byte(&0x45), ProtocolIdentifier::Unknown(0x45));
    assert_eq!(ProtocolIdentifier::from_byte(&0x10), ProtocolIdentifier::Layer3Impl);
    assert_eq!(ProtocolIdentifier::from_byte(&0x20), ProtocolIdentifier::Layer3Impl);
    assert_eq!(ProtocolIdentifier::from_byte(&0xA5), ProtocolIdentifier::Layer3Impl);
}

#[test]
fn test_address_fromstr() {
    assert_eq!(Address::from_str("VK7NTK-1").unwrap(), Address { callsign: "VK7NTK".to_string(), ssid: 1, c_bit: false });
    assert_eq!(Address::from_str("ID-15").unwrap(), Address { callsign: "ID".to_string(), ssid: 15, c_bit: false });
    assert!(Address::from_str("vk7ntk-5").is_ok());

    assert!(Address::from_str("-1").is_err());
    assert!(Address::from_str("VK7NTK").is_err());
    assert!(Address::from_str("VK7N -5").is_err());
    assert!(Address::from_str("VK7NTK-16").is_err());
    assert!(Address::from_str("8").is_err());
    assert!(Address::from_str("vk7n--1").is_err());
}

#[test]
fn test_round_trips() {
    use std::fs::{read_dir, File};
    use std::io::Read;

    let mut paths: Vec<_> = read_dir("testdata/linux-ax0").unwrap()
                                            .map(|r| r.unwrap())
                                            .collect();
    paths.sort_by_key(|dir| dir.path());
    for entry in paths {
        let entry_path = entry.path();
        let filename = entry_path.to_str().unwrap();
        let mut file = File::open(filename).unwrap();
        let mut frame_data: Vec<u8> = Vec::new();
        let _ = file.read_to_end(&mut frame_data);
        // Skip the leading null byte. A quirk as they came from Linux AF_PACKET.
        let frame_data_fixed = &frame_data[1..];
        
        match Ax25Frame::from_bytes(frame_data_fixed) {
            Ok(parsed) => {
                // Should be identical when re-encoded
                assert_eq!(frame_data_fixed, &parsed.to_bytes()[..])
            },
            Err(e) => println!("Could not parse! {}", e)
        };
    }
}