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
//! MAC commands
//!
//! Work in progress

use crate::mac::{DecodeError, PanId, ShortAddress};
use crate::utils::OptionalFrom;

extended_enum!(
    /// MAC command identifiers
    CommandId, u8,
    /// Association request, request association to PAN
    AssociationRequest => 1,
    /// Association response
    AssociationResponse => 2,
    /// Dissassociation notification
    DisassociationNotification => 3,
    /// Data request
    DataRequest => 4,
    /// PAN identifier conflict notification, sent from coordinator to offending device
    PanIdConflictNotification => 5,
    /// Orphan notification, 
    OrphanNotification => 6,
    /// Beacon request, sent from a device which want to join a PAN
    BeaconRequest => 7,
    /// Coordinator re-alignment, the coordinator will change network parameters
    CoordinatorRealignment => 8,
    /// Guaranteed time slot request, request a guaranteed time slot
    GuaranteedTimeSlotRequest => 9,
);

const CAP_FFD: u8 = 0x02;
const CAP_MAINS_POWER: u8 = 0x04;
const CAP_IDLE_RECEIVE: u8 = 0x08;
const CAP_FRAME_PROTECTION: u8 = 0x40;
const CAP_ALLOCATE_ADDRESS: u8 = 0x80;

/// Association request capability information
///
/// Sent with association request to report the capabilities of the device.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CapabilityInformation {
    /// Full-function device (FFD) or a reduced-function device (RFD)
    /// RFD and FFD have different function sets.
    pub full_function_device: bool,
    /// Device is connected to a mains power source or not
    pub mains_power: bool,
    /// Receive is enabled while idle
    pub idle_receive: bool,
    /// Frames are cryptographically protected
    pub frame_protection: bool,
    /// Device wish to have an short address allocated by the coordinator
    pub allocate_address: bool,
}

impl From<u8> for CapabilityInformation {
    fn from(byte: u8) -> Self {
        let full_function_device = byte & CAP_FFD == CAP_FFD;
        let mains_power = byte & CAP_MAINS_POWER == CAP_MAINS_POWER;
        let idle_receive = byte & CAP_IDLE_RECEIVE == CAP_IDLE_RECEIVE;
        let frame_protection = byte & CAP_FRAME_PROTECTION == CAP_FRAME_PROTECTION;
        let allocate_address = byte & CAP_ALLOCATE_ADDRESS == CAP_ALLOCATE_ADDRESS;
        Self {
            full_function_device,
            mains_power,
            idle_receive,
            frame_protection,
            allocate_address,
        }
    }
}

impl From<CapabilityInformation> for u8 {
    fn from(ar: CapabilityInformation) -> Self {
        let mut byte = 0u8;
        if ar.full_function_device {
            byte = byte | CAP_FFD;
        }
        if ar.mains_power {
            byte = byte | CAP_MAINS_POWER;
        }
        if ar.idle_receive {
            byte = byte | CAP_IDLE_RECEIVE;
        }
        if ar.frame_protection {
            byte = byte | CAP_FRAME_PROTECTION;
        }
        if ar.allocate_address {
            byte = byte | CAP_ALLOCATE_ADDRESS;
        }
        byte
    }
}

extended_enum!(
    /// Association Status
    AssociationStatus, u8,
    /// Successful
    Successful => 0x00,
    /// Network (PAN) at capacity
    NetworkAtCapacity => 0x01,
    /// Access to PAN denied
    AccessDenied => 0x02,
    /// Duplicate hopping sequence offset
    HoppingSequenceOffsetDuplication => 0x03,
    /// Fast association was successful
    FastAssociationSuccesful => 0x80,
);

extended_enum!(
    /// Disassociation Reason
    DisassociationReason, u8,
    /// Coordinator requested device to leave
    CoordinatorLeave => 1,
    /// Device requested to leave
    DeviceLeave => 2,
);

/// Coordinator re-alignment data
///
/// Changes to the PAN sent by the coordinator.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CoordinatorRealignmentData {
    /// PAN id that the coordinator will use
    pub pan_id: PanId,
    /// Short address that the coordinator will use
    pub coordinator_address: ShortAddress,
    /// Channel that the coordinator will use
    pub channel: u8,
    /// Device address or broadcast
    pub device_address: ShortAddress,
    /// Channel page or channel number the coordinator will use
    pub channel_page: Option<u8>,
}

impl CoordinatorRealignmentData {
    /// Decode coordinator re-alignment data from byte buffer
    ///
    /// # Returns
    ///
    /// Returns [`CoordinatorRealignmentData`] and the number of bytes used are returned
    ///
    /// # Errors
    ///
    /// This function returns an error, if there aren't enough bytes or
    /// dont't contain valid data. Please refer to [`DecodeError`] for details.
    ///
    /// [`DecodeError`]: ../enum.DecodeError.html
    /// [`CoordinatorRealignmentData`]: struct.CoordinatorRealignmentData.html
    pub fn decode(buf: &[u8]) -> Result<(Self, usize), DecodeError> {
        if buf.len() < 7 {
            return Err(DecodeError::NotEnoughBytes);
        }
        let mut offset = 0;
        let (pan_id, size) = PanId::decode(buf)?;
        offset += size;
        let (coordinator_address, size) = ShortAddress::decode(&buf[offset..])?;
        offset += size;
        let channel = buf[offset];
        offset += 1;
        let (device_address, size) = ShortAddress::decode(&buf[offset..])?;
        offset += size;
        let channel_page = if buf.len() > 7 {
            offset += 1;
            Some(buf[offset - 1])
        } else {
            None
        };
        Ok((
            CoordinatorRealignmentData {
                pan_id,
                coordinator_address,
                channel,
                device_address,
                channel_page,
            },
            offset,
        ))
    }
    /// Encode coordinator re-alignment data into a byte buffer
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written to the buffer
    ///
    /// # Panics
    ///
    /// Panics if the buffer is not long enough to hold the frame.
    ///
    pub fn encode(&self, buf: &mut [u8]) -> usize {
        let mut offset = 0;
        let size = self.pan_id.encode(buf);
        offset += size;
        let size = self.coordinator_address.encode(&mut buf[offset..]);
        offset += size;
        buf[offset] = self.channel;
        offset += 1;
        let size = self.device_address.encode(&mut buf[offset..]);
        offset += size;
        if let Some(channel_page) = self.channel_page {
            buf[offset] = channel_page;
            offset += 1;
        }
        offset
    }
}

const GTSC_RECEIVE_ONLY: u8 = 0x10;
const GTSC_ALLOCATION: u8 = 0x20;

/// Guaranteed time slot characteristics
///
/// GTS configuration requested with the guaranteed time slot request command.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct GuaranteedTimeSlotCharacteristics {
    /// Number of slots requested
    pub count: u8,
    /// Receive only slots, otherwise transmit
    pub receive_only: bool,
    /// Request type, Allocate or de-allocate
    pub allocation: bool,
}

impl From<u8> for GuaranteedTimeSlotCharacteristics {
    fn from(byte: u8) -> Self {
        let receive_only = byte & GTSC_RECEIVE_ONLY == GTSC_RECEIVE_ONLY;
        let allocation = byte & GTSC_ALLOCATION == GTSC_ALLOCATION;
        Self {
            count: (byte & 0x0f),
            receive_only,
            allocation,
        }
    }
}

impl From<GuaranteedTimeSlotCharacteristics> for u8 {
    fn from(gtsc: GuaranteedTimeSlotCharacteristics) -> Self {
        let mut byte = gtsc.count & 0x0f;
        if gtsc.receive_only {
            byte = byte | GTSC_RECEIVE_ONLY;
        }
        if gtsc.allocation {
            byte = byte | GTSC_ALLOCATION;
        }
        byte
    }
}

/// MAC commands
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Command {
    /// Association request, request association to a PAN
    AssociationRequest(CapabilityInformation),
    /// Association response, response to a association request
    AssociationResponse(ShortAddress, AssociationStatus),
    /// Notification of disassociation from the PAN
    DisassociationNotification(DisassociationReason),
    /// Request for data
    DataRequest,
    /// Notification of PAN idetifier conflict
    PanIdConflictNotification,
    /// Notification of orphan
    OrphanNotification,
    /// Request a beacon
    BeaconRequest,
    /// Coordinator re-alignment, the coordinator will change PAN parameters
    CoordinatorRealignment(CoordinatorRealignmentData),
    /// Request a guaranteed time slot (GTS)
    GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics),
}

impl Command {
    /// Decode MAC command from byte buffer
    ///
    /// # Returns
    ///
    /// Returns [`Command`] and the number of bytes used are returned
    ///
    /// # Errors
    ///
    /// This function returns an error, if the bytes either don't are enough or
    /// dont't contain valid data. Please refer to [`DecodeError`] for details.
    ///
    /// [`DecodeError`]: ../enum.DecodeError.html
    /// [`Command`]: enum.Command.html
    pub fn decode(buf: &[u8]) -> Result<(Self, usize), DecodeError> {
        if buf.len() == 0 {
            return Err(DecodeError::NotEnoughBytes);
        }
        let cmd = CommandId::optional_from(buf[0]);
        if cmd.is_none() {
            return Err(DecodeError::InvalidValue);
        }
        match cmd.unwrap() {
            CommandId::AssociationRequest => {
                if buf.len() < 2 {
                    return Err(DecodeError::NotEnoughBytes);
                }
                let capability = CapabilityInformation::from(buf[1]);
                Ok((Command::AssociationRequest(capability), 2))
            }
            CommandId::AssociationResponse => {
                if buf.len() < 4 {
                    return Err(DecodeError::NotEnoughBytes);
                }
                let (address, _) = ShortAddress::decode(&buf[1..])?;
                if let Some(status) = AssociationStatus::optional_from(buf[3]) {
                    Ok((Command::AssociationResponse(address, status), 4))
                } else {
                    return Err(DecodeError::InvalidValue);
                }
            }
            CommandId::DisassociationNotification => {
                if buf.len() < 2 {
                    return Err(DecodeError::NotEnoughBytes);
                }
                if let Some(reason) = DisassociationReason::optional_from(buf[1]) {
                    Ok((Command::DisassociationNotification(reason), 2))
                } else {
                    return Err(DecodeError::InvalidValue);
                }
            }
            CommandId::DataRequest => Ok((Command::DataRequest, 1)),
            CommandId::PanIdConflictNotification => Ok((Command::PanIdConflictNotification, 1)),
            CommandId::OrphanNotification => Ok((Command::OrphanNotification, 1)),
            CommandId::BeaconRequest => Ok((Command::BeaconRequest, 1)),
            CommandId::CoordinatorRealignment => {
                let (data, size) = CoordinatorRealignmentData::decode(&buf[1..])?;
                Ok((Command::CoordinatorRealignment(data), size + 1))
            }
            CommandId::GuaranteedTimeSlotRequest => {
                if buf.len() < 2 {
                    return Err(DecodeError::NotEnoughBytes);
                }
                let characteristics = GuaranteedTimeSlotCharacteristics::from(buf[1]);
                Ok((Command::GuaranteedTimeSlotRequest(characteristics), 2))
            }
        }
    }
    /// Encode the Command into a byte buffer
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written to the buffer
    ///
    /// # Panics
    ///
    /// Panics if the buffer is not long enough to hold the frame.
    ///
    pub fn encode(&self, buf: &mut [u8]) -> usize {
        match *self {
            Command::AssociationRequest(capability) => {
                buf[0] = u8::from(CommandId::AssociationRequest);
                buf[1] = u8::from(capability);
                2
            }
            Command::AssociationResponse(address, status) => {
                buf[0] = u8::from(CommandId::AssociationResponse);
                let size = address.encode(&mut buf[1..3]);
                buf[size + 1] = u8::from(status);
                size + 2
            }
            Command::DisassociationNotification(reason) => {
                buf[0] = u8::from(CommandId::DisassociationNotification);
                buf[1] = u8::from(reason);
                2
            }
            Command::DataRequest => {
                buf[0] = u8::from(CommandId::DataRequest);
                1
            }
            Command::PanIdConflictNotification => {
                buf[0] = u8::from(CommandId::PanIdConflictNotification);
                1
            }
            Command::OrphanNotification => {
                buf[0] = u8::from(CommandId::OrphanNotification);
                1
            }
            Command::BeaconRequest => {
                buf[0] = u8::from(CommandId::BeaconRequest);
                1
            }
            Command::CoordinatorRealignment(data) => {
                buf[0] = u8::from(CommandId::CoordinatorRealignment);
                let size = data.encode(&mut buf[1..]);
                size + 1
            }
            Command::GuaranteedTimeSlotRequest(characteristics) => {
                buf[0] = u8::from(CommandId::GuaranteedTimeSlotRequest);
                buf[1] = u8::from(characteristics);
                2
            }
        }
    }
}

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

    #[test]
    fn decode_association_request() {
        let data = [0x01, 0x8e];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::AssociationRequest(CapabilityInformation {
                full_function_device: true,
                mains_power: true,
                idle_receive: true,
                frame_protection: false,
                allocate_address: true,
            })
        );
    }

    #[test]
    fn encode_association_request() {
        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: false,
            mains_power: false,
            idle_receive: false,
            frame_protection: false,
            allocate_address: false,
        });
        let mut data = [0u8; 32];
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x00]);

        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: true,
            mains_power: false,
            idle_receive: false,
            frame_protection: false,
            allocate_address: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x02]);

        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: false,
            mains_power: true,
            idle_receive: false,
            frame_protection: false,
            allocate_address: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x04]);

        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: false,
            mains_power: false,
            idle_receive: true,
            frame_protection: false,
            allocate_address: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x08]);

        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: false,
            mains_power: false,
            idle_receive: false,
            frame_protection: true,
            allocate_address: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x40]);

        let command = Command::AssociationRequest(CapabilityInformation {
            full_function_device: false,
            mains_power: false,
            idle_receive: false,
            frame_protection: false,
            allocate_address: true,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x01, 0x80]);
    }

    #[test]
    fn decode_association_response() {
        let data = [0x02, 0x40, 0x77, 0x00];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 4);
        assert_eq!(
            command,
            Command::AssociationResponse(ShortAddress(0x7740), AssociationStatus::Successful)
        );

        let data = [0x02, 0xaa, 0x55, 0x01];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 4);
        assert_eq!(
            command,
            Command::AssociationResponse(
                ShortAddress(0x55aa),
                AssociationStatus::NetworkAtCapacity
            )
        );

        let data = [0x02, 0x00, 0x00, 0x02];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 4);
        assert_eq!(
            command,
            Command::AssociationResponse(ShortAddress(0x0000), AssociationStatus::AccessDenied)
        );

        let data = [0x02, 0x00, 0x00, 0x03];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 4);
        assert_eq!(
            command,
            Command::AssociationResponse(
                ShortAddress(0x0000),
                AssociationStatus::HoppingSequenceOffsetDuplication
            )
        );

        let data = [0x02, 0x00, 0x00, 0x80];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 4);
        assert_eq!(
            command,
            Command::AssociationResponse(
                ShortAddress(0x0000),
                AssociationStatus::FastAssociationSuccesful
            )
        );

        let data = [0x02, 0x00, 0x00, 0x04];
        let result = Command::decode(&data);
        assert!(result.is_err());

        let data = [0x02, 0x00, 0x00, 0x7f];
        let result = Command::decode(&data);
        assert!(result.is_err());

        let data = [0x02, 0x00, 0x00, 0x81];
        let result = Command::decode(&data);
        assert!(result.is_err());
    }

    #[test]
    fn encode_association_response() {
        let mut data = [0u8; 32];

        let command =
            Command::AssociationResponse(ShortAddress(0x55aa), AssociationStatus::Successful);
        let size = command.encode(&mut data);
        assert_eq!(size, 4);
        assert_eq!(data[..size], [0x02, 0xaa, 0x55, 0x00]);

        let command = Command::AssociationResponse(
            ShortAddress(0x1234),
            AssociationStatus::NetworkAtCapacity,
        );
        let size = command.encode(&mut data);
        assert_eq!(size, 4);
        assert_eq!(data[..size], [0x02, 0x34, 0x12, 0x01]);

        let command =
            Command::AssociationResponse(ShortAddress(0xcffe), AssociationStatus::AccessDenied);
        let size = command.encode(&mut data);
        assert_eq!(size, 4);
        assert_eq!(data[..size], [0x02, 0xfe, 0xcf, 0x02]);

        let command = Command::AssociationResponse(
            ShortAddress(0xfedc),
            AssociationStatus::HoppingSequenceOffsetDuplication,
        );
        let size = command.encode(&mut data);
        assert_eq!(size, 4);
        assert_eq!(data[..size], [0x02, 0xdc, 0xfe, 0x03]);

        let command = Command::AssociationResponse(
            ShortAddress(0x0ff0),
            AssociationStatus::FastAssociationSuccesful,
        );
        let size = command.encode(&mut data);
        assert_eq!(size, 4);
        assert_eq!(data[..size], [0x02, 0xf0, 0x0f, 0x80]);
    }

    #[test]
    fn decode_disassociation_notification() {
        let data = [0x03, 0x01];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::DisassociationNotification(DisassociationReason::CoordinatorLeave)
        );

        let data = [0x03, 0x02];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::DisassociationNotification(DisassociationReason::DeviceLeave)
        );

        let data = [0x03, 0x00];
        let result = Command::decode(&data);
        assert!(result.is_err());

        let data = [0x03, 0x03];
        let result = Command::decode(&data);
        assert!(result.is_err());
    }

    #[test]
    fn encode_disassociation_notification() {
        let mut data = [0u8; 32];

        let command = Command::DisassociationNotification(DisassociationReason::CoordinatorLeave);
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x03, 0x01]);

        let command = Command::DisassociationNotification(DisassociationReason::DeviceLeave);
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x03, 0x02]);
    }

    #[test]
    fn decode_coordinator_realignment() {
        let data = [0x08, 0x23, 0x11, 0x01, 0x00, 0x0f, 0x34, 0x12];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 8);
        assert_eq!(
            command,
            Command::CoordinatorRealignment(CoordinatorRealignmentData {
                pan_id: PanId(0x1123),
                coordinator_address: ShortAddress(0x0001),
                channel: 15,
                device_address: ShortAddress(0x1234),
                channel_page: None,
            })
        );

        let data = [0x08, 0x34, 0x12, 0x21, 0x43, 0x0b, 0xcd, 0xab, 0x01];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 9);
        assert_eq!(
            command,
            Command::CoordinatorRealignment(CoordinatorRealignmentData {
                pan_id: PanId(0x1234),
                coordinator_address: ShortAddress(0x4321),
                channel: 11,
                device_address: ShortAddress(0xabcd),
                channel_page: Some(1),
            })
        );
    }

    #[test]
    fn encode_coordinator_realignment() {
        let mut data = [0u8; 32];

        let command = Command::CoordinatorRealignment(CoordinatorRealignmentData {
            pan_id: PanId(0x1123),
            coordinator_address: ShortAddress(0x0001),
            channel: 15,
            device_address: ShortAddress(0x1234),
            channel_page: None,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 8);
        assert_eq!(
            data[..size],
            [0x08, 0x23, 0x11, 0x01, 0x00, 0x0f, 0x34, 0x12]
        );

        let command = Command::CoordinatorRealignment(CoordinatorRealignmentData {
            pan_id: PanId(0xbeef),
            coordinator_address: ShortAddress(0xfeed),
            channel: 26,
            device_address: ShortAddress(0x1234),
            channel_page: Some(15),
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 9);
        assert_eq!(
            data[..size],
            [0x08, 0xef, 0xbe, 0xed, 0xfe, 0x1a, 0x34, 0x12, 0x0f]
        );
    }

    #[test]
    fn decode_guaranteed_time_slot_request() {
        let data = [0x09, 0x01];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
                count: 1,
                receive_only: false,
                allocation: false,
            })
        );

        let data = [0x09, 0x12];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
                count: 2,
                receive_only: true,
                allocation: false,
            })
        );

        let data = [0x09, 0x23];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 2);
        assert_eq!(
            command,
            Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
                count: 3,
                receive_only: false,
                allocation: true,
            })
        );
    }

    #[test]
    fn encode_guaranteed_time_slot_request() {
        let mut data = [0u8; 32];

        let command = Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
            count: 1,
            receive_only: false,
            allocation: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x09, 0x01]);

        let command = Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
            count: 15,
            receive_only: true,
            allocation: false,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x09, 0x1f]);

        let command = Command::GuaranteedTimeSlotRequest(GuaranteedTimeSlotCharacteristics {
            count: 15,
            receive_only: false,
            allocation: true,
        });
        let size = command.encode(&mut data);
        assert_eq!(size, 2);
        assert_eq!(data[..size], [0x09, 0x2f]);
    }

    #[test]
    fn decode_other_commands() {
        let data = [0x04];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 1);
        assert_eq!(command, Command::DataRequest);

        let data = [0x05];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 1);
        assert_eq!(command, Command::PanIdConflictNotification);

        let data = [0x06];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 1);
        assert_eq!(command, Command::OrphanNotification);

        let data = [0x07];
        let (command, size) = Command::decode(&data).unwrap();
        assert_eq!(size, 1);
        assert_eq!(command, Command::BeaconRequest);
    }

    #[test]
    fn encode_other_commands() {
        let mut data = [0u8; 32];

        let command = Command::DataRequest;
        let size = command.encode(&mut data);
        assert_eq!(size, 1);
        assert_eq!(data[..size], [0x04]);

        let command = Command::PanIdConflictNotification;
        let size = command.encode(&mut data);
        assert_eq!(size, 1);
        assert_eq!(data[..size], [0x05]);

        let command = Command::OrphanNotification;
        let size = command.encode(&mut data);
        assert_eq!(size, 1);
        assert_eq!(data[..size], [0x06]);

        let command = Command::BeaconRequest;
        let size = command.encode(&mut data);
        assert_eq!(size, 1);
        assert_eq!(data[..size], [0x07]);
    }
}