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
//! Receive Trunking Signalling Block (TSBK) packets and decode the various TSBK payloads.

use collect_slice::CollectSlice;

use bits::{Dibit, DibitBytes};
use buffer::{Buffer, DataPayloadStorage};
use coding::trellis;
use consts::{TSBK_DIBITS, TSBK_BYTES};
use data::{crc, interleave};
use error::{Result, P25Error};
use util::{slice_u16, slice_u24};

use trunking::fields::{Channel, TalkGroup, ServiceOptions, RegResponse};

/// State machine for receiving a TSBK packet.
///
/// The state machine consumes dibit symbols and performs the following steps:
///
/// 1. Buffer dibits until a full packet's worth are available
/// 2. Descramble symbols using the same deinterleaver as data packets
/// 3. Decode 1/2-rate convolutional code and attempt to correct any errors
/// 4. Group dibits into a buffer of bytes for further interpretation
pub struct TsbkReceiver {
    /// Current buffered dibits.
    dibits: Buffer<DataPayloadStorage>,
}

impl TsbkReceiver {
    /// Create a new `TsbkReceiver` in the initial state.
    pub fn new() -> TsbkReceiver {
        TsbkReceiver {
            dibits: Buffer::new(DataPayloadStorage::new()),
        }
    }

    /// Feed in a baseband symbol, possibly producing a complete TSBK packet. Return
    /// `Some(Ok(pkt))` if a packet was successfully received, `Some(Err(err))` if an
    /// error occurred, and `None` in the case of no event.
    pub fn feed(&mut self, dibit: Dibit) -> Option<Result<TsbkFields>> {
        let (count, dibits) = {
            let buf = match self.dibits.feed(dibit) {
                Some(buf) => buf,
                None => return None,
            };

            let mut dibits = [Dibit::default(); TSBK_DIBITS];
            let count = trellis::DibitDecoder::new(interleave::Deinterleaver::new(buf))
                .filter_map(|x| x.ok())
                .collect_slice_exhaust(&mut dibits[..]);

            (count, dibits)
        };

        if count != dibits.len() {
            return Some(Err(P25Error::ViterbiUnrecoverable));
        }

        let mut bytes = [0; TSBK_BYTES];
        DibitBytes::new(dibits.iter().cloned()).collect_slice_checked(&mut bytes[..]);

        Some(Ok(TsbkFields::new(bytes)))
    }
}

/// Type of a TSBK payload.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum TsbkOpcode {
    GroupVoiceGrant,
    GroupVoiceUpdate,
    GroupVoiceUpdateExplicit,
    UnitVoiceGrant,
    UnitCallRequest,
    UnitVoiceUpdate,
    PhoneGrant,
    PhoneAlert,
    UnitDataGrant,
    GroupDataGrant,
    GroupDataUpdate,
    GroupDataUpdateExplicit,
    UnitStatusUpdate,
    UnitStatusQuery,
    UnitShortMessage,
    UnitMonitor,
    UnitCallAlert,
    AckResponse,
    QueuedResponse,
    ExtendedFunctionResponse,
    DenyResponse,
    GroupAffiliationResponse,
    GroupAffiliationQuery,
    LocRegResponse,
    UnitRegResponse,
    UnitRegCommand,
    UnitAuthCommand,
    UnitDeregAck,
    RoamingAddrCommand,
    RoamingAddrUpdate,
    SystemServiceBroadcast,
    AltControlChannel,
    RfssStatusBroadcast,
    NetworkStatusBroadcast,
    AdjacentSite,
    ChannelParamsUpdate,
    ProtectionParamBroadcast,
    ProtectionParamUpdate,
    Reserved,
}

impl TsbkOpcode {
    /// Try to parse an opcode from the given 6 bits.
    pub fn from_bits(bits: u8) -> Option<TsbkOpcode> {
        use self::TsbkOpcode::*;

        assert!(bits >> 6 == 0);

        match bits {
            0b000000 => Some(GroupVoiceGrant),
            0b000001 => Some(Reserved),
            0b000010 => Some(GroupVoiceUpdate),
            0b000011 => Some(GroupVoiceUpdateExplicit),
            0b000100 => Some(UnitVoiceGrant),
            0b000101 => Some(UnitCallRequest),
            0b000110 => Some(UnitVoiceUpdate),
            0b000111 => Some(Reserved),

            0b001000 => Some(PhoneGrant),
            0b001001 => Some(Reserved),
            0b001010 => Some(PhoneAlert),
            0b001011...0b001111 => Some(Reserved),

            0b010000 => Some(UnitDataGrant),
            0b010001 => Some(GroupDataGrant),
            0b010010 => Some(GroupDataUpdate),
            0b010011 => Some(GroupDataUpdateExplicit),
            0b010100...0b010111 => Some(Reserved),

            0b011000 => Some(UnitStatusUpdate),
            0b011001 => Some(Reserved),
            0b011010 => Some(UnitStatusQuery),
            0b011011 => Some(Reserved),
            0b011100 => Some(UnitShortMessage),
            0b011101 => Some(UnitMonitor),
            0b011110 => Some(Reserved),
            0b011111 => Some(UnitCallAlert),
            0b100000 => Some(AckResponse),
            0b100001 => Some(QueuedResponse),
            0b100010 => Some(Reserved),
            0b100011 => Some(Reserved),
            0b100100 => Some(ExtendedFunctionResponse),
            0b100101 => Some(Reserved),
            0b100110 => Some(Reserved),
            0b100111 => Some(DenyResponse),

            0b101000 => Some(GroupAffiliationResponse),
            0b101001 => Some(Reserved),
            0b101010 => Some(GroupAffiliationQuery),
            0b101011 => Some(LocRegResponse),
            0b101100 => Some(UnitRegResponse),
            0b101101 => Some(UnitRegCommand),
            0b101110 => Some(UnitAuthCommand),
            0b101111 => Some(UnitDeregAck),

            0b110000...0b110101 => Some(Reserved),
            0b110110 => Some(RoamingAddrCommand),
            0b110111 => Some(RoamingAddrUpdate),

            0b111000 => Some(SystemServiceBroadcast),
            0b111001 => Some(AltControlChannel),
            0b111010 => Some(RfssStatusBroadcast),
            0b111011 => Some(NetworkStatusBroadcast),
            0b111100 => Some(AdjacentSite),
            0b111101 => Some(ChannelParamsUpdate),
            0b111110 => Some(ProtectionParamBroadcast),
            0b111111 => Some(ProtectionParamUpdate),

            _ => None,
        }
    }
}

/// Buffer of bytes that represents a TSBK packet.
pub type Buf = [u8; TSBK_BYTES];

/// A Trunking Signalling Block packet.
#[derive(Copy, Clone)]
pub struct TsbkFields(Buf);

impl TsbkFields {
    /// Interpret the given bytes as a TSBK packet.
    pub fn new(buf: Buf) -> TsbkFields { TsbkFields(buf) }

    /// Whether this packet is the last one in the TSBK group.
    pub fn is_tail(&self) -> bool { self.0[0] >> 7 == 1 }
    /// Whether the packet is encrypted.
    pub fn protected(&self) -> bool { self.0[0] >> 6 & 1 == 1 }
    /// Type of data contained in the payload.
    pub fn opcode(&self) -> Option<TsbkOpcode> { TsbkOpcode::from_bits(self.0[0] & 0x3F) }
    /// Manufacturer ID, which determines if the packet is standardized.
    pub fn mfg(&self) -> u8 { self.0[1] }
    /// Transmitted CRC.
    pub fn crc(&self) -> u16 { slice_u16(&self.0[10..]) }

    /// Calculate 16-bit CRC over bytes in packet.
    pub fn calc_crc(&self) -> u16 {
        crc::CRC16::new()
            .feed_bytes((&self.0[..10]).iter().cloned())
            .finish() as u16
    }

    /// Verify if the calculated CRC matches the transmitted one.
    pub fn crc_valid(&self) -> bool {
        self.crc() == self.calc_crc()
    }

    /// Bytes that make up the payload of the packet.
    pub fn payload(&self) -> &[u8] { &self.0[2..=9] }
}

/// Response given to a location registration request.
pub struct LocRegResponse(Buf);

impl LocRegResponse {
    /// Create a new `LocRegResponse` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { LocRegResponse(tsbk.0) }

    /// System response to the registration request.
    pub fn response(&self) -> RegResponse { RegResponse::from_bits(self.0[2] & 0b11) }
    /// Talkgroup of requesting unit.
    pub fn talkgroup(&self)  -> TalkGroup { TalkGroup::new(&self.0[3..=4]) }
    /// RF Subsystem ID of site within System.
    pub fn rfss(&self) -> u8 { self.0[5] }
    /// Site ID of site within RFSS.
    pub fn site(&self) -> u8 { self.0[6] }
    /// Address of requesting unit.
    pub fn dest_unit(&self) -> u32 { slice_u24(&self.0[7..=9]) }
}

/// Response given to an attempted user registration.
pub struct UnitRegResponse(Buf);

impl UnitRegResponse {
    /// Create a new `UnitRegResponse` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { UnitRegResponse(tsbk.0) }

    /// System response to user registration request.
    pub fn response(&self) -> RegResponse {
        RegResponse::from_bits((self.0[2] >> 4) & 0b11)
    }

    /// System ID within WACN.
    pub fn system(&self) -> u16 { slice_u16(&self.0[2..=3]) & 0xFFF }
    /// Address of originating unit, which uniquely identifies the unit within the System.
    pub fn src_id(&self) -> u32 { slice_u24(&self.0[4..=6]) }
    /// ID of originating unit which, along with the WACN and System ID, uniquely
    /// identifies the unit.
    pub fn src_addr(&self) -> u32 { slice_u24(&self.0[7..=9]) }
}

/// Acknowledgement of successful user deregistration request.
pub struct UnitDeregAck(Buf);

impl UnitDeregAck {
    /// Create a new `UnitDeregAck` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { UnitDeregAck(tsbk.0) }

    /// WACN ID within the communication network.
    pub fn wacn(&self) -> u32 { slice_u24(&self.0[3..=5]) >> 4 }
    /// System ID within WACN.
    pub fn system(&self) -> u16 { slice_u16(&self.0[5..=6]) & 0xFFF }
    /// ID of affected unit.
    pub fn src_unit(&self) -> u32 { slice_u24(&self.0[7..=9]) }
}

/// Indicates a talkgroup has been granted a voice traffic channel.
pub struct GroupVoiceGrant(Buf);

impl GroupVoiceGrant {
    /// Create a new `GroupVoiceGrant` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { GroupVoiceGrant(tsbk.0) }

    /// Options requested/granted for the traffic channel.
    pub fn opts(&self) -> ServiceOptions { ServiceOptions::new(self.0[2]) }
    /// Parameters for tuning to the traffic channel.
    pub fn channel(&self) -> Channel { Channel::new(&self.0[3..=4]) }
    /// Talkgroup for the conversation.
    pub fn talkgroup(&self) -> TalkGroup { TalkGroup::new(&self.0[5..]) }
    /// Unit that initiated the conversation.
    pub fn src_unit(&self) -> u32 { slice_u24(&self.0[7..]) }
}

/// Indicates a pair of units have been granted a traffic channel.
///
/// Note that this decoder can be used with `UnitVoiceGrant`, `UnitVoiceUpdate`, and
/// `UnitDataGrant` packets.
pub struct UnitTrafficChannel(Buf);

impl UnitTrafficChannel {
    /// Create a new `UnitTrafficChannel` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { UnitTrafficChannel(tsbk.0) }

    /// Parameters for tuning to the traffic channel.
    pub fn channel(&self) -> Channel { Channel::new(&self.0[2..]) }
    /// Destination unit of the call.
    pub fn dest_unit(&self) -> u32 { slice_u24(&self.0[4..]) }
    /// Originating unit of the call.
    pub fn src_unit(&self) -> u32 { slice_u24(&self.0[7..]) }
}

/// Indicates a unit has been granted a traffic channel for a phone call.
pub struct PhoneGrant(Buf);

impl PhoneGrant {
    /// Create a new `PhoneGrant` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { PhoneGrant(tsbk.0) }

    /// Options requested/granted for the traffic channel.
    pub fn opts(&self) -> ServiceOptions { ServiceOptions::new(self.0[2]) }
    /// Parameters for tuning to the traffic channel.
    pub fn channel(&self) -> Channel { Channel::new(&self.0[3..]) }
    /// Maximum amount of time (in units of 100ms) that the phone call can occupy the
    /// traffic channel.
    pub fn call_timer(&self) -> u16 { slice_u16(&self.0[5..]) }
    /// Unit assigned to the call.
    pub fn unit(&self) -> u32 { slice_u24(&self.0[7..]) }
}

/// Indicates a talkgroup has been granted a data traffic channel.
pub struct GroupDataGrant(Buf);

impl GroupDataGrant {
    /// Create a new `GroupDataGrant` decoder from the base TSBK decoder.
    pub fn new(tsbk: TsbkFields) -> Self { GroupDataGrant(tsbk.0) }

    /// Options requested/granted for the traffic channel.
    pub fn opts(&self) -> ServiceOptions { ServiceOptions::new(self.0[2]) }
    /// Parameters for tuning to the traffic channel.
    pub fn channel(&self) -> Channel { Channel::new(&self.0[3..=4]) }
    /// Talkgroup assigned to the channel.
    pub fn talkgroup(&self) -> TalkGroup { TalkGroup::new(&self.0[5..=6]) }
    /// Originating unit for the data traffic.
    pub fn src_unit(&self) -> u32 { slice_u24(&self.0[7..=9]) }
}

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

    #[test]
    fn test_tsbk_fields() {
        let t = TsbkFields::new([
            0b10111001,
            0b00000001,
            0b11110000,
            0b00001111,
            0b10101010,
            0b01010101,
            0b00000000,
            0b11111111,
            0b11001100,
            0b00110011,
            0b11010111,
            0b11010111,
        ]);

        assert!(t.is_tail());
        assert!(!t.protected());
        assert_eq!(t.opcode(), Some(TsbkOpcode::AltControlChannel));
        assert_eq!(t.mfg(), 0b00000001);
        assert_eq!(t.crc(), 0b1101011111010111);
        assert_eq!(t.calc_crc(), 0b0111010000111100);
        assert!(!t.crc_valid());
        assert_eq!(t.payload(), &[
            0b11110000,
            0b00001111,
            0b10101010,
            0b01010101,
            0b00000000,
            0b11111111,
            0b11001100,
            0b00110011,
        ]);
    }

    #[test]
    fn test_adjacent_site() {
        let t = TsbkFields::new([
            0b00111100,
            0b00000000,
            0b11001100,
            0b11011111,
            0b00111100,
            0b10101010,
            0b01010101,
            0b00110110,
            0b01111110,
            0b01010001,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::AdjacentSite));
        let a = AdjacentSite::new(t.payload());

        assert_eq!(a.area(), 0b11001100);
        assert!(a.opts().conventional());
        assert!(a.opts().failing());
        assert!(!a.opts().current());
        assert!(a.opts().networked());
        assert_eq!(a.system(), 0b111100111100);
        assert_eq!(a.rfss(), 0b10101010);
        assert_eq!(a.site(), 0b01010101);
        assert_eq!(a.channel().id(), 0b0011);
        assert_eq!(a.channel().number(), 0b011001111110);
        let s = a.services();
        assert!(s.is_composite());
        assert!(!s.updates_only());
        assert!(!s.is_backup());
        assert!(s.has_data());
        assert!(!s.has_voice());
        assert!(s.has_registration());
        assert!(!s.has_auth());
    }

    #[test]
    fn test_channel_params_update() {
        let t = TsbkFields::new([
            0b00111101,
            0b00000000,
            0b0110_0011,
            0b00100_010,
            0b110100_00,
            0b00110010,
            0b00001010,
            0b00100101,
            0b00010000,
            0b10100010,
            0b11111111,
            0b11111111,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::ChannelParamsUpdate));
        let p = ChannelParamsUpdate::new(t.payload());

        assert_eq!(p.id(), 0b0110);
        assert_eq!(p.params().bandwidth, 12_500);
        assert_eq!(p.params().rx_freq(0b1001), 851_062_500);
    }

    #[test]
    fn test_group_traffic_update() {
        let t = TsbkFields::new([
            0b00000010,
            0b00000000,
            0b01101111,
            0b01010101,
            0b11111111,
            0b11111111,
            0b10011010,
            0b10101010,
            0b00110011,
            0b11001100,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::GroupVoiceUpdate));
        let u = GroupTrafficUpdate::new(t.payload()).updates();

        assert_eq!(u[0].0.id(), 0b0110);
        assert_eq!(u[0].0.number(), 0b111101010101);
        assert_eq!(u[0].1, TalkGroup::Everbody);
        assert_eq!(u[1].0.id(), 0b1001);
        assert_eq!(u[1].0.number(), 0b101010101010);
        assert_eq!(u[1].1, TalkGroup::Other(0b0011001111001100));
    }

    #[test]
    fn test_alt_control_channel() {
        let t = TsbkFields::new([
            0b00111001,
            0b00000000,
            0b11100011,
            0b01010101,
            0b10110110,
            0b10101111,
            0b01010001,
            0b11101010,
            0b10101010,
            0b10101110,
            0b00000000,
            0b11111111,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::AltControlChannel));
        let a = AltControlChannel::new(t.payload());
        assert_eq!(a.rfss(), 0b11100011);
        assert_eq!(a.site(), 0b01010101);
        let c = a.alts();
        assert_eq!(c[0].0.id(), 0b1011);
        assert_eq!(c[0].0.number(), 0b011010101111);
        let s = c[0].1;
        assert!(s.is_composite());
        assert!(!s.updates_only());
        assert!(!s.is_backup());
        assert!(s.has_data());
        assert!(!s.has_voice());
        assert!(s.has_registration());
        assert!(!s.has_auth());
        assert_eq!(c[1].0.id(), 0b1110);
        assert_eq!(c[1].0.number(), 0b101010101010);
        let s = c[1].1;
        assert!(!s.is_composite());
        assert!(s.updates_only());
        assert!(s.is_backup());
        assert!(!s.has_data());
        assert!(s.has_voice());
        assert!(!s.has_registration());
        assert!(s.has_auth());
    }

    #[test]
    fn test_rfss_status_broadcast() {
        let t = TsbkFields::new([
            0b00111010,
            0b00000000,
            0b11001100,
            0b00010000,
            0b10101010,
            0b11100111,
            0b00011000,
            0b11010101,
            0b01110011,
            0b01010001,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::RfssStatusBroadcast));
        let a = RfssStatusBroadcast::new(t.payload());
        assert_eq!(a.area(), 0b11001100);
        assert!(a.networked());
        assert_eq!(a.system(), 0b000010101010);
        assert_eq!(a.rfss(), 0b11100111);
        assert_eq!(a.site(), 0b00011000);
        assert_eq!(a.channel().id(), 0b1101);
        assert_eq!(a.channel().number(), 0b010101110011);
        let s = a.services();
        assert!(s.is_composite());
        assert!(!s.updates_only());
        assert!(!s.is_backup());
        assert!(s.has_data());
        assert!(!s.has_voice());
        assert!(s.has_registration());
        assert!(!s.has_auth());
    }

    #[test]
    fn test_network_status_broadcast() {
        let t = TsbkFields::new([
            0b00111011,
            0b00000000,
            0b11001010,
            0b11111100,
            0b00101011,
            0b11001111,
            0b01011011,
            0b11011100,
            0b11100111,
            0b01010001,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::NetworkStatusBroadcast));
        let n = NetworkStatusBroadcast::new(t.payload());
        assert_eq!(n.area(), 0b11001010);
        assert_eq!(n.wacn(), 0b11111100001010111100);
        assert_eq!(n.system(), 0b111101011011);
        assert_eq!(n.channel().id(), 0b1101);
        assert_eq!(n.channel().number(), 0b110011100111);
        let s = n.services();
        assert!(s.is_composite());
        assert!(!s.updates_only());
        assert!(!s.is_backup());
        assert!(s.has_data());
        assert!(!s.has_voice());
        assert!(s.has_registration());
        assert!(!s.has_auth());
    }

    #[test]
    fn test_unit_reg_response() {
        let t = TsbkFields::new([
            0b00101100,
            0b00000000,
            0b11011010,
            0b11100111,
            0b10101010,
            0b01010101,
            0b00110011,
            0b11111000,
            0b00111111,
            0b11001100,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::UnitRegResponse));
        let r = UnitRegResponse::new(t);
        assert_eq!(r.response(), RegResponse::Fail);
        assert_eq!(r.system(), 0b101011100111);
        assert_eq!(r.src_id(), 0b101010100101010100110011);
        assert_eq!(r.src_addr(), 0b111110000011111111001100);
    }

    #[test]
    fn test_unit_dereg_ack() {
        let t = TsbkFields::new([
            0b00101111,
            0b00000000,
            0b11111111,
            0b11001100,
            0b00110011,
            0b10100001,
            0b11110011,
            0b11111100,
            0b00000011,
            0b11100111,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::UnitDeregAck));
        let a = UnitDeregAck::new(t);
        assert_eq!(a.wacn(), 0b11001100001100111010);
        assert_eq!(a.system(), 0b000111110011);
        assert_eq!(a.src_unit(), 0b111111000000001111100111);
    }

    #[test]
    fn test_loc_reg_response() {
        let t = TsbkFields::new([
            0b00101011,
            0b00000000,
            0b01010011,
            0b11111000,
            0b00011100,
            0b11011010,
            0b10101010,
            0b11110000,
            0b00001111,
            0b00110011,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::LocRegResponse));
        let r = LocRegResponse::new(t);
        assert_eq!(r.response(), RegResponse::Refuse);
        assert_eq!(r.talkgroup(), TalkGroup::Other(0b1111100000011100));
        assert_eq!(r.rfss(), 0b11011010);
        assert_eq!(r.site(), 0b10101010);
        assert_eq!(r.dest_unit(), 0b111100000000111100110011);
    }

    #[test]
    fn test_call_alert() {
        let t = TsbkFields::new([
            0b00011111,
            0b00000000,
            0b11111111,
            0b11111111,
            0b01010101,
            0b10101010,
            0b11001100,
            0b00110011,
            0b11100111,
            0b00011000,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::UnitCallAlert));
        let c = UnitCallAlert::new(t.payload());
        assert_eq!(c.dest_unit(), 0b010101011010101011001100);
        assert_eq!(c.src_unit(), 0b001100111110011100011000);
    }

    #[test]
    fn test_call_request() {
        let t = TsbkFields::new([
            0b00000101,
            0b00000000,
            0b01010101,
            0b11111111,
            0b00111001,
            0b11000110,
            0b01010101,
            0b11101010,
            0b00010101,
            0b11110000,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::UnitCallRequest));
        let r = UnitCallRequest::new(t.payload());
        let o = r.opts();
        assert!(!o.emergency());
        assert!(o.protected());
        assert!(!o.full_duplex());
        assert!(o.packet_switched());
        assert_eq!(o.prio(), 0b101);
        assert_eq!(r.dest_unit(), 0b001110011100011001010101);
        assert_eq!(r.src_unit(), 0b111010100001010111110000);
    }

    #[test]
    fn test_group_voice_grant() {
        let t = TsbkFields::new([
            0b00000000,
            0b11111111,
            0b10100011,
            0b11100101,
            0b11001100,
            0b00011000,
            0b11100111,
            0b11110000,
            0b01111000,
            0b00111100,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::GroupVoiceGrant));
        let g = GroupVoiceGrant::new(t);
        let o = g.opts();
        assert!(o.emergency());
        assert!(!o.protected());
        assert!(o.full_duplex());
        assert!(!o.packet_switched());
        assert_eq!(o.prio(), 0b011);
        assert_eq!(g.channel().id(), 0b1110);
        assert_eq!(g.channel().number(), 0b010111001100);
        assert_eq!(g.talkgroup(), TalkGroup::Other(0b0001100011100111));
        assert_eq!(g.src_unit(), 0b111100000111100000111100);
    }

    #[test]
    fn test_unit_traffic_channel() {
        let t = TsbkFields::new([
            0b00000100,
            0b11111111,
            0b11001110,
            0b10101010,
            0b11100111,
            0b00011000,
            0b11111001,
            0b00000110,
            0b11100111,
            0b11001010,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::UnitVoiceGrant));
        let g = UnitTrafficChannel::new(t);
        assert_eq!(g.channel().id(), 0b1100);
        assert_eq!(g.channel().number(), 0b111010101010);
        assert_eq!(g.dest_unit(), 0b111001110001100011111001);
        assert_eq!(g.src_unit(), 0b000001101110011111001010);
    }

    #[test]
    fn test_phone_alert() {
        let t = TsbkFields::new([
            0b00001010,
            0b00000000,
            0b11110011,
            0b00111100,
            0b01011010,
            0b11100111,
            0b01101110,
            0b11111100,
            0b01111110,
            0b00111111,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::PhoneAlert));
        let a = PhoneAlert::new(t.payload());
        assert_eq!(a.digits(), &[
            0b11110011,
            0b00111100,
            0b01011010,
            0b11100111,
            0b01101110,
        ]);
        assert_eq!(a.dest_unit(), 0b111111000111111000111111);
    }

    #[test]
    fn test_phone_grant() {
        let t = TsbkFields::new([
            0b00001000,
            0b00000000,
            0b10100011,
            0b11100101,
            0b00110100,
            0b00000000,
            0b00000010,
            0b11111100,
            0b01111110,
            0b00000011,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::PhoneGrant));
        let g = PhoneGrant::new(t);
        let o = g.opts();
        assert!(o.emergency());
        assert!(!o.protected());
        assert!(o.full_duplex());
        assert!(!o.packet_switched());
        assert_eq!(o.prio(), 0b011);
        assert_eq!(g.channel().id(), 0b1110);
        assert_eq!(g.channel().number(), 0b010100110100);
        assert_eq!(g.call_timer(), 2);
        assert_eq!(g.unit(), 0b111111000111111000000011);
    }

    #[test]
    fn test_group_data_grant() {
        let t = TsbkFields::new([
            0b00010001,
            0b00000000,
            0b10100011,
            0b10010111,
            0b11111100,
            0b11110000,
            0b10001001,
            0b11100011,
            0b01000100,
            0b11101010,
            0b00000000,
            0b00000000,
        ]);
        assert_eq!(t.opcode(), Some(TsbkOpcode::GroupDataGrant));
        let g = GroupDataGrant::new(t);
        let o = g.opts();
        assert!(o.emergency());
        assert!(!o.protected());
        assert!(o.full_duplex());
        assert!(!o.packet_switched());
        assert_eq!(o.prio(), 0b011);
        assert_eq!(g.channel().id(), 0b1001);
        assert_eq!(g.channel().number(), 0b011111111100);
        assert_eq!(g.talkgroup(), TalkGroup::Other(0b1111000010001001));
        assert_eq!(g.src_unit(), 0b111000110100010011101010);
    }
}