ardop_interface 0.4.0

Interface to the Amateur Radio Digital Open Protocol (ARDOP)
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
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//! Status messages received from the TNC
//!
//! The main method, `Response::parse()`, invokes a `nom` parser which
//! attempts to parse a TNC output from the provided byte stream.
//! In the output,
//!
//! 0. Contains the remaining bytes which have not yet been parsed
//!    into a TNC message. Provide these bytes to future calls to
//!    `Response::parse()`.
//!
//! 1. If the parser matched anything contains `Some` `Response`,
//!    which is further enumerated.

use std::str;
use std::string::String;

use nom;
use nom::types::CompleteStr;
use nom::*;

use super::constants::{CommandID, FALSE, NEWLINE_STR, TRUE};

use crate::arq::{ConnectionFailedReason, ConnectionInfo};

custom_derive! {
    /// ARQ Connection States
    #[derive(Debug, PartialEq, Eq, EnumFromStr, EnumDisplay, Clone)]
    pub enum State {
        /// Codec stopped
        OFFLINE,

        /// ARQ has disconnected
        DISC,

        /// Information Sending Station
        ISS,

        /// Information Receiving Station
        IRS,

        /// Attempting to become the ISS
        IRStoISS,

        /// ARQ connected; link is idle
        IDLE,

        /// Sending FEC data
        FECSend,

        /// Receiving FEC data
        FECRcv,
    }
}

impl State {
    /// True if this state is a connected state
    ///
    /// Returns true if the TNC is connected to a remote
    /// peer when it is in the given state.
    pub fn is_connected(state: &Self) -> bool {
        match state {
            &State::ISS => true,
            &State::IRS => true,
            &State::IRStoISS => true,
            &State::IDLE => true,
            _ => false,
        }
    }
}

/// Announces a change in the ARQ connection state
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ConnectionStateChange {
    /// The busy detection state has changed
    ///
    /// If the payload is true, then the RF channel is now
    /// sensed as busy. If the payload is false, then the RF
    /// channel is now sensed as clear.
    Busy(bool),

    /// An open connection has been closed
    ///
    /// Connections may be closed by `DISCONNECT` tear-down
    /// or by an `ABORT`. The loss of connection reason is
    /// not enumerated here, however.
    Closed,

    /// Successfully connected
    ///
    /// Data is information about the connection
    Connected(ConnectionInfo),

    /// Identity frame received
    ///
    /// Reports the receipt of an identity frame (`IDF`) from
    /// a remote peer. Tuple contains:
    /// 0. Peer callsign
    /// 1. Peer Maidenhead grid square, if known
    IdentityFrame(String, Option<String>),

    /// Ping frame received
    ///
    /// Reports the receipt of an incoming `PING` request
    /// from a remote peer. The `PING` is not necessarily
    /// directed at your station. Tuple contains:
    /// 0. Sender callsign
    /// 1. Target callsign
    /// 2. SNR, in dB relative to 3 kHz
    /// 3. Constellation quality (unitless, higher→better)
    Ping(String, String, u16, u16),

    /// Ping ACK received
    ///
    /// A remote peer has responded to a `PING` request made
    /// by this station.
    ///
    /// Data fields are:
    /// 0. SNR, in dB relative to 3 kHz
    /// 1. Constellation quality (unitless, higher→better)
    PingAck(u16, u16),

    /// Failed to connect
    ///
    /// No connection was ever successfully made with
    /// the remote peer. Failure is not limited to
    /// `CONNECT` requests. Even when `LISTEN`ing, it
    /// is possible for the bandwidth negotiation to
    /// fail.
    Failed(ConnectionFailedReason),

    /// Reports on the progress of data transmission
    ///
    /// This message reports the current length of the
    /// TNC's outbound `BUFFER`. When the `SendBuffer`
    /// reaches zero, all enqueued data has been transmitted.
    /// If your application allows the buffer to empty, a
    /// link turnover is likely to occur.
    ///
    /// For ARQ connections, the `SendBuffer` counts the total
    /// length of all un-ACK'd data. A `SendBuffer(0)` event
    /// indicates that the peer has successfully received all
    /// outstanding data.
    SendBuffer(u64),

    /// Announces that this station has become the sender
    ///
    /// In the ARDOP protocol, at most one station is the
    /// sending side (`ISS`). This state indicates that
    /// the current station has become the sender.
    Sending,

    /// Announces that this station has become the receiver
    ///
    /// In the ARDOP protocol, at most one station is the
    /// sending side (`ISS`). This state indicates that
    /// the current station has become the sender.
    ///
    /// Even in the receiving state, a station will still
    /// transmit acknowledgement packets.
    Receiving,
}

/// Event messages
///
/// These events are always sent asynchronously—i.e., not at the
/// request of the host.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Event {
    /// Bytes of payload data that is "pending"
    ///
    /// Length of the data that is currently in the process of being
    /// transmitted. Includes data not yet acknowledged by the receiving
    /// station.
    BUFFER(u64),

    /// Indicates that the RF channel has become busy (or not)
    BUSY(bool),

    /// Pending connect or ping not for this station
    ///
    /// Indicates to the host that the prior `PENDING` Connect
    /// Request or `PING` was not to `MYCALL` or one of the `MYAUX`
    /// call signs) This allows the Host to resume scanning.
    CANCELPENDING,

    /// Indicates the successful opening of an ARQ connection
    ///
    /// Values
    /// - Remote call sign. This *should* be a proper callsign.
    /// - Connection bandwidth, in Hz
    /// - Maidenhead grid square of remote peer, if known. If
    ///   not provided, will be None.
    CONNECTED(String, u16, Option<String>),

    /// An existing ARQ link has been disconnected
    DISCONNECTED,

    /// Announces a state transition to the given State
    NEWSTATE(State),

    /// A connect request or ping frame has been detected
    ///
    /// Indicates to the host application a Connect Request
    /// or `PING` frame type has been detected (may not
    /// necessarily be to `MYCALL` or one of the `MYAUX` call
    /// signs).
    ///
    /// This provides an early warning to the host that a
    /// connection may be in process so it can hold any
    /// scanning activity.
    PENDING,

    /// Reports receipt of a ping
    ///
    /// The ping is not necessarily directed at your station.
    ///
    /// Values:
    /// - Sender call sign. This *should* be a proper callsign.
    /// - Remote/intended call sign. This may be a tactical call.
    /// - SNR (in dB relative to 3 kHz noise bandwidth). An SNR
    ///   of `21` indicates an SNR greater than 20 dB.
    /// - Decoded constellation quality (30 -- 100)
    PING(String, String, u16, u16),

    /// Reports receipt of a ping acknowledgement
    ///
    /// The `PINGACK` asynchronous reply to host is sent ONLY if
    /// a prior `PING` to this station was received within the
    /// last 5 seconds.
    ///
    /// Values:
    /// - SNR (in dB relative to 3 kHz noise bandwidth). An SNR
    ///   of `21` indicates an SNR greater than 20 dB.
    /// - Decoded constellation quality (30 -- 100)
    PINGACK(u16, u16),

    /// Reports transmission of a `PINGACK`
    ///
    /// Indicates to the host that a `PING` was received and
    /// that the TNC has automatically transmitted a
    /// `PINGACK` reply.
    PINGREPLY,

    /// Key transmitter
    ///
    /// Indicates that the control software should key or unkey the
    /// transmitter. To operate correctly the transmitter PTT should
    /// be activated within 50 ms of receipt of this response.
    PTT(bool),

    /// Connection rejected by peer
    REJECTED(ConnectionFailedReason),

    /// A textual status message for the user
    STATUS(String),

    /// Announces target of incoming call
    ///
    /// Sent to indicate the receipt of an incoming ARQ connection
    /// request. Value is the "dialed callsign" being called by the
    /// remote peer. This may be one of our "`MYAUX`" callsigns or
    /// our proper assigned `MYCALL` (plus SSID).
    TARGET(String),

    /// An unknown, unsolicited message from the TNC
    Unknown(String),
}

/// Command success
///
/// Reports a successful command execution. A tuple of:
/// 1. ID of the command that succeeded
/// 2. Optional response text. At present, this is only
///    populated for `VERSION` responses.
///
pub type CommandOk = (CommandID, Option<String>);

/// Command success or failure
///
/// Reports that a command initiated by the host has either
/// succeeded or failed.
///
/// - For successful commands, returns the Command ID and,
///   for some commands, a descriptive string. At present,
///   the string is only populated for `VERSION` responses.
///
/// - For erroneous commands, returns the error message.
///   There is no standard way to determine the ID of a
///   failed command.
pub type CommandResult = Result<CommandOk, String>;

/// Response messages
///
/// The TNC has two types of output messages:
/// 1. A response to a command (request-reply pattern)
/// 2. An asynchronous reporting on some event (publish-subscribe pattern)
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Response {
    /// Command success or failure
    ///
    /// Reports that a command initiated by the host has either
    /// succeeded or failed.
    ///
    /// - For successful commands, returns the Command ID and,
    ///   for some commands, a descriptive string. At present,
    ///   the string is only populated for `VERSION` responses.
    ///
    /// - For erroneous commands, returns the error message.
    ///   There is no standard way to determine the ID of a
    ///   failed command.
    CommandResult(CommandResult),

    /// Asynchronous event message
    ///
    /// `Event`s report `CONNECTED`, `DISCONNECTED`, and many other
    /// potential state transitions.
    Event(Event),
}

impl Response {
    /// Parse zero or one TNC `Response` from raw bytes
    ///
    /// Scans the provided byte stream for the next TNC message and
    /// returns it. The remaining bytes, which are not yet parsed,
    /// are also returned.
    ///
    /// A catch-all Response, `Response::Event(Event::Unknown(String))`
    /// exists to parse messages which are otherwise not parseable.
    ///
    /// Parameters
    /// - `inp`: Raw bytes received from the TNC
    ///
    /// Return Value
    /// 0. Number of bytes consumed
    /// 1. A `Response` parsed from `inp`, if any.
    pub fn parse(inp: &[u8]) -> (usize, Option<Response>) {
        let inp_len = inp.len();
        match parse_line(inp) {
            Err(e) => {
                if e.is_incomplete() {
                    (0usize, None)
                } else {
                    panic!("Unexpected parse error: {:?}", e);
                }
            }
            Ok(res) => {
                if res.1.len() == 0 {
                    (inp_len - res.0.len(), None)
                } else {
                    let out = parse_response(res.1).unwrap();
                    (inp_len - res.0.len(), Some(out.1))
                }
            }
        }
    }
}

// Reads a complete line of text
named!(
    parse_line<&[u8], CompleteStr>,
    do_parse!(
        lin: map_res!(
            take_until_and_consume!(NEWLINE_STR),
            std::str::from_utf8
        ) >>
        (CompleteStr(lin))
    )
);

// Parse all Responses
named!(
    parse_response<CompleteStr, Response>,
    do_parse!(
        aa: alt!(
            parse_buffer |
            parse_busy |
            parse_cancelpending |
            parse_connected |
            parse_disconnected |
            parse_fault |
            parse_newstate |
            parse_pending |
            parse_ping |
            parse_pingack |
            parse_pingreply |
            parse_ptt |
            parse_rejected |
            parse_status |
            parse_target |
            parse_version |
            parse_command_ok |
            parse_anything
        ) >>
        (aa)
    )
);

// Parses any line
//
// Always run this parser LAST
named!(
    parse_anything<CompleteStr, Response>,
    do_parse!(
        aa: map!(
            take_while!(|_x| true),
            |s: CompleteStr| (*s).to_owned()
        ) >>
        (Response::Event(Event::Unknown(aa)))
    )
);

// Tries to parse positive acknowledgements to commands
//
// Always run this parser after you have tried all "unsolicited
// message" responses first.
named!(
    parse_command_ok<CompleteStr, Response>,
    do_parse!(
        cmd: map_res!(
            take_while!(is_alpha),
            |s: CompleteStr| (*s).parse::<CommandID>()
        ) >>
        take_while!(|_x| true) >>
        (Response::CommandResult(Ok((cmd, None))))
    )
);

named!(
    parse_buffer<CompleteStr, Response>,
    do_parse!(
        tag!(r"BUFFER") >>
        take_while!(is_space) >>
        aa: parse_u64 >>
        (Response::Event(Event::BUFFER(aa)))
    )
);

named!(
    parse_busy<CompleteStr, Response>,
    do_parse!(
        tag!(r"BUSY") >>
        take_while!(is_space) >>
        aa: parse_boolstr_like >>
        (Response::Event(Event::BUSY(aa)))
    )
);

named!(
    parse_cancelpending<CompleteStr, Response>,
    do_parse!(
        tag!(r"CANCELPENDING") >>
        (Response::Event(Event::CANCELPENDING))
    )
);

named!(
    parse_connected<CompleteStr, Response>,
    do_parse!(
        tag!(r"CONNECTED") >>
        take_while!(is_space) >>
        aa: take_while!(is_call_letters) >>
        take_while!(is_space) >>
        bw: parse_u16 >>
        gs: opt!(
            do_parse!(
                take_while1!(is_space) >>
                gs: take_while!(is_alphanumeric) >>
                ((*gs).to_owned())
            )
        ) >>
        (Response::Event(Event::CONNECTED((*aa).to_owned(), bw, gs)))
    )
);

named!(
    parse_disconnected<CompleteStr, Response>,
    do_parse!(
        tag!(r"DISCONNECTED") >>
        (Response::Event(Event::DISCONNECTED))
    )
);

named!(
    parse_newstate<CompleteStr, Response>,
    do_parse!(
        tag!(r"NEWSTATE") >>
        take_while!(is_space) >>
        aa: map_res!(
            take_while!(is_alphanumeric),
            |s: CompleteStr| (*s).parse::<State>()
        ) >>
        (Response::Event(Event::NEWSTATE(aa)))
    )
);

named!(
    parse_fault<CompleteStr, Response>,
    do_parse!(
        tag!(r"FAULT") >>
        take_while!(is_space) >>
        aa: take_while!(|_x| true) >>
        (Response::CommandResult(Err((*aa).to_owned())))
    )
);

named!(
    parse_pending<CompleteStr, Response>,
    do_parse!(
        tag!(r"PENDING") >>
        (Response::Event(Event::PENDING))
    )
);

named!(
    parse_ping<CompleteStr, Response>,
    do_parse!(
        tag!(r"PING") >>
        take_while!(is_space) >>
        tx: take_while!(is_call_letters) >>
        tag!(">") >>
        rem: take_while!(is_call_letters) >>
        take_while!(is_space) >>
        snr: parse_u16 >>
        take_while!(is_space) >>
        qual: parse_u16 >>
        (Response::Event(Event::PING((*tx).to_owned(), (*rem).to_owned(), snr, qual)))
    )
);

named!(
    parse_pingack<CompleteStr, Response>,
    do_parse!(
        tag!(r"PINGACK") >>
        take_while!(is_space) >>
        snr: parse_u16 >>
        take_while!(is_space) >>
        qual: parse_u16 >>
        (Response::Event(Event::PINGACK(snr, qual)))
    )
);

named!(
    parse_pingreply<CompleteStr, Response>,
    do_parse!(
        tag!(r"PINGREPLY") >>
        (Response::Event(Event::PINGREPLY))
    )
);

named!(
    parse_ptt<CompleteStr, Response>,
    do_parse!(
        tag!(r"PTT") >>
        take_while!(is_space) >>
        aa: parse_boolstr_like >>
        (Response::Event(Event::PTT(aa)))
    )
);

named!(
    parse_status<CompleteStr, Response>,
    do_parse!(
        tag!(r"STATUS") >>
        take_while!(is_space) >>
        aa: take_while!(|_x| true) >>
        (Response::Event(Event::STATUS((*aa).to_owned())))
    )
);

named!(
    parse_version<CompleteStr, Response>,
    do_parse!(
        tag!(r"VERSION") >>
        take_while!(is_space) >>
        aa: take_while!(|_x| true) >>
        (Response::CommandResult(Ok((CommandID::VERSION, Some((*aa).to_owned())))))
    )
);

named!(
    parse_state<CompleteStr, State>,
    map_res!(
        nom::alpha,
        |inp: CompleteStr| str::parse::<State>(*inp)
    )
);

named!(
    parse_target<CompleteStr, Response>,
    do_parse!(
        tag!(r"TARGET") >>
        take_while!(is_space) >>
        aa: take_while!(is_call_letters) >>
        (Response::Event(Event::TARGET((*aa).to_owned())))
    )
);

named!(
    parse_rejected<CompleteStr, Response>,
    do_parse!(
        tag!(r"REJECTED") >>
        aa: parse_rejection_reason >>
        (Response::Event(Event::REJECTED(aa)))
    )
);

named!(
    parse_rejection_reason<CompleteStr, ConnectionFailedReason>,
    map_res!(
        nom::alpha,
        |inp: CompleteStr| match *inp {
            r"BW" => Ok(ConnectionFailedReason::IncompatibleBandwidth),
            r"BUSY" => Ok(ConnectionFailedReason::Busy),
            _ => Err(())
        }
    )
);

named!(
    parse_u16<CompleteStr, u16>,
    map_res!(
      take_while!(is_numeric),
      |s: CompleteStr| (*s).parse::<u16>()
    )
);

named!(
    parse_u64<CompleteStr, u64>,
    map_res!(
      take_while!(is_numeric),
      |s: CompleteStr| (*s).parse::<u64>()
    )
);

named!(
    parse_boolstr_like<CompleteStr, bool>,
    alt!(
        do_parse!(
            tag!(TRUE) >> (true)
        ) |
        do_parse!(
            tag!(FALSE) >> (false)
        )
    )
);

#[allow(dead_code)]
#[inline]
fn is_space(c: char) -> bool {
    c == ' '
}

#[allow(dead_code)]
#[inline]
fn is_alpha(c: char) -> bool {
    c.is_ascii_alphabetic()
}

#[allow(dead_code)]
#[inline]
fn is_alphanumeric(c: char) -> bool {
    c.is_ascii_alphanumeric()
}

#[allow(dead_code)]
#[inline]
fn is_call_letters(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '-'
}

#[allow(dead_code)]
#[inline]
fn is_numeric(c: char) -> bool {
    c.is_numeric()
}

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

    #[test]
    fn test_state() {
        assert_eq!(b"IDLE", format!("{}", State::IDLE).as_bytes());
        assert_eq!(State::DISC, str::parse("DISC").unwrap());
        assert_eq!(true, State::is_connected(&State::IRStoISS));
        assert_eq!(false, State::is_connected(&State::DISC));
    }

    #[test]
    fn test_parse_line() {
        let res = parse_line("HELO WORLD\r".as_bytes());
        let r = res.unwrap().1;
        assert_eq!(CompleteStr("HELO WORLD"), r);

        let res = parse_line("\r".as_bytes());
        assert_eq!(CompleteStr(""), res.unwrap().1);
    }

    #[test]
    fn test_parse_anything() {
        let res = parse_anything(CompleteStr("HELO WORLD"));
        assert_eq!(
            Response::Event(Event::Unknown("HELO WORLD".to_owned())),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_u16() {
        let res = parse_u16(CompleteStr("5455"));
        assert_eq!(5455u16, res.unwrap().1);
    }

    #[test]
    fn test_parse_boolstr_like() {
        let res = parse_boolstr_like(CompleteStr("TRUE\r"));
        assert_eq!(true, res.unwrap().1);

        let res = parse_boolstr_like(CompleteStr("FALSE"));
        assert_eq!(false, res.unwrap().1);
    }

    #[test]
    fn test_parse_rejected() {
        assert_eq!(
            Response::Event(Event::REJECTED(
                ConnectionFailedReason::IncompatibleBandwidth
            )),
            parse_rejected(CompleteStr("REJECTEDBW")).unwrap().1
        );
    }

    #[test]
    fn test_parse_buffer() {
        let res = parse_buffer(CompleteStr("BUFFER 160"));
        assert_eq!(Response::Event(Event::BUFFER(160)), res.unwrap().1);
    }

    #[test]
    fn test_parse_busy() {
        let res = parse_busy(CompleteStr("BUSY TRUE"));
        assert_eq!(Response::Event(Event::BUSY(true)), res.unwrap().1);

        let res = parse_busy(CompleteStr("BUSY FALSE"));
        assert_eq!(Response::Event(Event::BUSY(false)), res.unwrap().1);
    }

    #[test]
    fn test_parse_connected() {
        let res = parse_connected(CompleteStr("CONNECTED W1AW-Z 500 EM00"));
        assert_eq!(
            Response::Event(Event::CONNECTED(
                "W1AW-Z".to_owned(),
                500,
                Some("EM00".to_owned())
            )),
            res.unwrap().1
        );

        let res = parse_connected(CompleteStr("CONNECTED W1AW-Z 500"));
        assert_eq!(
            Response::Event(Event::CONNECTED("W1AW-Z".to_owned(), 500, None)),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_fault() {
        let res = parse_fault(CompleteStr("FAULT it isn't working"));
        assert_eq!(
            Response::CommandResult(Err("it isn't working".to_owned())),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_newstate() {
        let res = parse_newstate(CompleteStr("NEWSTATE DISC"));
        assert_eq!(
            Response::Event(Event::NEWSTATE(State::DISC)),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_ping() {
        let res = parse_ping(CompleteStr("PING W1AW>CQ 10 80"));
        assert_eq!(
            Response::Event(Event::PING("W1AW".to_owned(), "CQ".to_owned(), 10, 80)),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_pingack() {
        let res = parse_pingack(CompleteStr("PINGACK 10 80"));
        assert_eq!(Response::Event(Event::PINGACK(10, 80)), res.unwrap().1);
    }

    #[test]
    fn test_parse_pingreply() {
        let res = parse_pingreply(CompleteStr("PINGREPLY"));
        assert_eq!(Response::Event(Event::PINGREPLY), res.unwrap().1);
    }

    #[test]
    fn test_parse_ptt() {
        let res = parse_ptt(CompleteStr("PTT TRUE"));
        assert_eq!(Response::Event(Event::PTT(true)), res.unwrap().1);
    }

    #[test]
    fn test_parse_status() {
        let res = parse_status(CompleteStr("STATUS everything alright"));
        assert_eq!(
            Response::Event(Event::STATUS("everything alright".to_owned())),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_target() {
        let res = parse_target(CompleteStr("TARGET W1AW-Z"));
        assert_eq!(
            Response::Event(Event::TARGET("W1AW-Z".to_owned())),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_version() {
        let res = parse_version(CompleteStr("VERSION 1.0.4"));
        assert_eq!(
            Response::CommandResult(Ok((CommandID::VERSION, Some("1.0.4".to_owned())))),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_command_ok() {
        let res = parse_command_ok(CompleteStr("MYAUX"));
        assert_eq!(
            Response::CommandResult(Ok((CommandID::MYAUX, None))),
            res.unwrap().1
        );

        let res = parse_command_ok(CompleteStr("ARQBW now 2500"));
        assert_eq!(
            Response::CommandResult(Ok((CommandID::ARQBW, None))),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse_response() {
        let res = parse_response(CompleteStr("VERSION 1.0.4-b4"));
        assert_eq!(
            Response::CommandResult(Ok((CommandID::VERSION, Some("1.0.4-b4".to_owned())))),
            res.unwrap().1
        );

        let res = parse_response(CompleteStr("blah blah"));
        assert_eq!(
            Response::Event(Event::Unknown("blah blah".to_owned())),
            res.unwrap().1
        );

        let res = parse_response(CompleteStr("CONNECTED W1AW 500"));
        assert_eq!(
            Response::Event(Event::CONNECTED("W1AW".to_owned(), 500, None)),
            res.unwrap().1
        );
    }

    #[test]
    fn test_parse() {
        let data = "PENDING\rCANCELPENDING\r";
        let res = Response::parse(data.as_bytes());
        assert_eq!(8, res.0);
        assert_eq!(Some(Response::Event(Event::PENDING)), res.1);
        let res = Response::parse(&data.as_bytes()[8..]);
        assert_eq!(14, res.0);
        assert_eq!(Some(Response::Event(Event::CANCELPENDING)), res.1);
        let res = Response::parse(&data.as_bytes()[22..]);
        assert_eq!(0, res.0);
        assert_eq!(None, res.1);

        let res = Response::parse("\r\r\r".as_bytes());
        assert_eq!(1, res.0);
        assert_eq!(None, res.1);

        let res = Response::parse("NEWSTATE IRS\r".as_bytes());
        assert_eq!(Some(Response::Event(Event::NEWSTATE(State::IRS))), res.1);
    }
}