dvb-ci 0.8.0

DVB Common Interface (EN 50221) — APDU/resource objects, session/transport PDUs, and a CA_PMT builder from a dvb-si PMT.
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
//! Low-Speed Communications objects — ETSI EN 50221 §8.7.1, Tables 52-56
//! (PDF pp. 51-54).
//!
//! - `comms_cmd` (`9F 8C 00`, Table 52) — connect / disconnect / set-params /
//!   enquire-status / get-next-buffer.
//! - `connection_descriptor` (`9F 8C 01`, Table 53) — telephone / cable-return
//!   connection info; also nested inside Connect_on_Channel.
//! - `comms_reply` (`9F 8C 02`, Table 54) — id + return_value.
//! - `comms_send` (`9F 8C 03` last / `9F 8C 04` more, Table 55) — phase + bytes.
//! - `comms_rcv` (`9F 8C 05` last / `9F 8C 06` more, Table 56) — phase + bytes.

use crate::error::{Error, Result};
use crate::length;
use crate::tag::{self, ApduTag};
use crate::traits::ApduDef;
use broadcast_common::{Parse, Serialize};

/// `comms_command_id` values (Table 52, p. 52).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum CommsCommandId {
    /// `01` — establish communication on the comms resource.
    ConnectOnChannel,
    /// `02` — terminate the connection.
    DisconnectOnChannel,
    /// `03` — set buffer size / timeout.
    SetParams,
    /// `04` — request the current connection status.
    EnquireStatus,
    /// `05` — receive-side flow control.
    GetNextBuffer,
    /// Any other value (reserved).
    Reserved(u8),
}

impl CommsCommandId {
    /// Decode a `comms_command_id` byte.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0x01 => Self::ConnectOnChannel,
            0x02 => Self::DisconnectOnChannel,
            0x03 => Self::SetParams,
            0x04 => Self::EnquireStatus,
            0x05 => Self::GetNextBuffer,
            other => Self::Reserved(other),
        }
    }
    /// Wire byte.
    #[must_use]
    pub const fn to_u8(self) -> u8 {
        match self {
            Self::ConnectOnChannel => 0x01,
            Self::DisconnectOnChannel => 0x02,
            Self::SetParams => 0x03,
            Self::EnquireStatus => 0x04,
            Self::GetNextBuffer => 0x05,
            Self::Reserved(v) => v,
        }
    }
    /// Spec token, or `"reserved"`.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::ConnectOnChannel => "Connect_on_Channel",
            Self::DisconnectOnChannel => "Disconnect_on_Channel",
            Self::SetParams => "Set_Params",
            Self::EnquireStatus => "Enquire_Status",
            Self::GetNextBuffer => "Get_Next_Buffer",
            Self::Reserved(_) => "reserved",
        }
    }
}
broadcast_common::impl_spec_display!(CommsCommandId, Reserved);

/// `connection_descriptor_type` values (Table 53, p. 52).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum ConnectionDescriptorType {
    /// `01` — an SI telephone_descriptor() follows.
    SiTelephoneDescriptor,
    /// `02` — a cable-return-channel `channel_id` follows.
    CableReturnChannelDescriptor,
    /// Any other value (reserved).
    Reserved(u8),
}

impl ConnectionDescriptorType {
    /// Decode a `connection_descriptor_type` byte.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0x01 => Self::SiTelephoneDescriptor,
            0x02 => Self::CableReturnChannelDescriptor,
            other => Self::Reserved(other),
        }
    }
    /// Wire byte.
    #[must_use]
    pub const fn to_u8(self) -> u8 {
        match self {
            Self::SiTelephoneDescriptor => 0x01,
            Self::CableReturnChannelDescriptor => 0x02,
            Self::Reserved(v) => v,
        }
    }
    /// Spec token, or `"reserved"`.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::SiTelephoneDescriptor => "SI_Telephone_Descriptor",
            Self::CableReturnChannelDescriptor => "Cable_Return_Channel_Descriptor",
            Self::Reserved(_) => "reserved",
        }
    }
}
broadcast_common::impl_spec_display!(ConnectionDescriptorType, Reserved);

/// `connection_descriptor()` object (Table 53): carries the connection info.
///
/// The body after the `connection_descriptor_type` byte is branch-dependent (an
/// SI `telephone_descriptor()` whose internal layout EN 50221 does not reproduce,
/// or a 1-byte `channel_id`); it is carried verbatim so it round-trips.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ConnectionDescriptor<'a> {
    /// `connection_descriptor_type`.
    pub descriptor_type: ConnectionDescriptorType,
    /// The branch-dependent payload (telephone_descriptor bytes, or the
    /// `channel_id` byte), verbatim.
    #[cfg_attr(feature = "serde", serde(borrow, with = "super::bytes_serde"))]
    pub payload: &'a [u8],
}

impl<'a> ConnectionDescriptor<'a> {
    /// Parse a nested `connection_descriptor()` at the front of `bytes`, returning
    /// the object and how many bytes it consumed (tag + length + body).
    pub(crate) fn parse_component(bytes: &'a [u8]) -> Result<(Self, usize)> {
        if bytes.len() < 3 {
            return Err(Error::BufferTooShort {
                need: 3,
                have: bytes.len(),
                what: "connection_descriptor tag",
            });
        }
        let t = ApduTag::from_bytes(bytes[0], bytes[1], bytes[2]);
        if t != tag::CONNECTION_DESCRIPTOR {
            return Err(Error::UnexpectedApduTag {
                got: t.as_u24(),
                expected: tag::CONNECTION_DESCRIPTOR.as_u24(),
                what: "connection_descriptor",
            });
        }
        let (len_value, len_hdr) = length::decode(&bytes[3..])?;
        let body_start = 3 + len_hdr;
        let body_end = body_start + len_value;
        if bytes.len() < body_end {
            return Err(Error::LengthMismatch {
                what: "connection_descriptor",
                declared: len_value,
                actual: bytes.len().saturating_sub(body_start),
            });
        }
        let body = &bytes[body_start..body_end];
        let type_byte = *body.first().ok_or(Error::BufferTooShort {
            need: 1,
            have: 0,
            what: "connection_descriptor type",
        })?;
        Ok((
            Self {
                descriptor_type: ConnectionDescriptorType::from_u8(type_byte),
                payload: &body[1..],
            },
            body_end,
        ))
    }

    pub(crate) fn component_len(&self) -> usize {
        super::apdu_len(1 + self.payload.len())
    }

    pub(crate) fn serialize_component(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = 1 + self.payload.len();
        let mut pos = super::write_apdu_header(tag::CONNECTION_DESCRIPTOR, body_len, buf)?;
        buf[pos] = self.descriptor_type.to_u8();
        pos += 1;
        buf[pos..pos + self.payload.len()].copy_from_slice(self.payload);
        pos += self.payload.len();
        Ok(pos)
    }
}

impl<'a> Parse<'a> for ConnectionDescriptor<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let (c, consumed) = Self::parse_component(bytes)?;
        if consumed != bytes.len() {
            return Err(Error::LengthMismatch {
                what: "connection_descriptor",
                declared: consumed,
                actual: bytes.len(),
            });
        }
        Ok(c)
    }
}

impl Serialize for ConnectionDescriptor<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        self.component_len()
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        self.serialize_component(buf)
    }
}

impl<'a> ApduDef<'a> for ConnectionDescriptor<'a> {
    const TAG: ApduTag = tag::CONNECTION_DESCRIPTOR;
    const NAME: &'static str = "CONNECTION_DESCRIPTOR";
}

/// The branch-dependent parameters of a `comms_cmd()` (Table 52).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum CommsCmdParams<'a> {
    /// `Connect_on_Channel`: connection descriptor + retry_count + timeout.
    Connect {
        /// The nested `connection_descriptor()`.
        #[cfg_attr(feature = "serde", serde(borrow))]
        connection_descriptor: ConnectionDescriptor<'a>,
        /// `retry_count`.
        retry_count: u8,
        /// `timeout` (seconds).
        timeout: u8,
    },
    /// `Set_Params`: buffer_size + timeout.
    SetParams {
        /// `buffer_size` (1..=254).
        buffer_size: u8,
        /// `timeout` (units of 10 ms).
        timeout: u8,
    },
    /// `Get_Next_Buffer`: comms_phase_id.
    GetNextBuffer {
        /// `comms_phase_id` (alternates 0,1,…).
        comms_phase_id: u8,
    },
    /// No branch parameters (`Disconnect_on_Channel`, `Enquire_Status`, or a
    /// reserved command id).
    None,
}

/// `comms_cmd()` object (Table 52).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CommsCmd<'a> {
    /// `comms_command_id`.
    pub command_id: CommsCommandId,
    /// The branch-dependent parameters keyed by `command_id`.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub params: CommsCmdParams<'a>,
}

impl<'a> Parse<'a> for CommsCmd<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = super::parse_apdu_header(bytes, tag::COMMS_CMD, "comms_cmd")?;
        let id_byte = *body.first().ok_or(Error::BufferTooShort {
            need: 1,
            have: 0,
            what: "comms_cmd command_id",
        })?;
        let command_id = CommsCommandId::from_u8(id_byte);
        let rest = &body[1..];
        let params = match command_id {
            CommsCommandId::ConnectOnChannel => {
                let (cd, consumed) = ConnectionDescriptor::parse_component(rest)?;
                let tail = &rest[consumed..];
                if tail.len() < 2 {
                    return Err(Error::BufferTooShort {
                        need: 2,
                        have: tail.len(),
                        what: "comms_cmd connect retry/timeout",
                    });
                }
                CommsCmdParams::Connect {
                    connection_descriptor: cd,
                    retry_count: tail[0],
                    timeout: tail[1],
                }
            }
            CommsCommandId::SetParams => {
                if rest.len() < 2 {
                    return Err(Error::BufferTooShort {
                        need: 2,
                        have: rest.len(),
                        what: "comms_cmd set_params",
                    });
                }
                CommsCmdParams::SetParams {
                    buffer_size: rest[0],
                    timeout: rest[1],
                }
            }
            CommsCommandId::GetNextBuffer => {
                let comms_phase_id = *rest.first().ok_or(Error::BufferTooShort {
                    need: 1,
                    have: 0,
                    what: "comms_cmd get_next_buffer",
                })?;
                CommsCmdParams::GetNextBuffer { comms_phase_id }
            }
            _ => CommsCmdParams::None,
        };
        Ok(Self { command_id, params })
    }
}

impl CommsCmd<'_> {
    fn body_len(&self) -> usize {
        1 + match &self.params {
            CommsCmdParams::Connect {
                connection_descriptor,
                ..
            } => connection_descriptor.component_len() + 2,
            CommsCmdParams::SetParams { .. } => 2,
            CommsCmdParams::GetNextBuffer { .. } => 1,
            CommsCmdParams::None => 0,
        }
    }
}

impl Serialize for CommsCmd<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        super::apdu_len(self.body_len())
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = self.body_len();
        let mut pos = super::write_apdu_header(tag::COMMS_CMD, body_len, buf)?;
        buf[pos] = self.command_id.to_u8();
        pos += 1;
        match &self.params {
            CommsCmdParams::Connect {
                connection_descriptor,
                retry_count,
                timeout,
            } => {
                pos += connection_descriptor.serialize_component(&mut buf[pos..])?;
                buf[pos] = *retry_count;
                buf[pos + 1] = *timeout;
                pos += 2;
            }
            CommsCmdParams::SetParams {
                buffer_size,
                timeout,
            } => {
                buf[pos] = *buffer_size;
                buf[pos + 1] = *timeout;
                pos += 2;
            }
            CommsCmdParams::GetNextBuffer { comms_phase_id } => {
                buf[pos] = *comms_phase_id;
                pos += 1;
            }
            CommsCmdParams::None => {}
        }
        Ok(pos)
    }
}

impl<'a> ApduDef<'a> for CommsCmd<'a> {
    const TAG: ApduTag = tag::COMMS_CMD;
    const NAME: &'static str = "COMMS_CMD";
}

/// `comms_reply_id` values (Table 54, p. 53).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum CommsReplyId {
    /// `01` — Connect acknowledgement.
    ConnectAck,
    /// `02` — Disconnect acknowledgement.
    DisconnectAck,
    /// `03` — Set Params acknowledgement.
    SetParamsAck,
    /// `04` — Status reply (return_value 0 = Disconnected, 1 = Connected).
    StatusReply,
    /// `05` — Get Next Buffer acknowledgement.
    GetNextBufferAck,
    /// `06` — Send acknowledgement (return_value = acknowledged phase).
    SendAck,
    /// Any other value (reserved).
    Reserved(u8),
}

impl CommsReplyId {
    /// Decode a `comms_reply_id` byte.
    #[must_use]
    pub fn from_u8(v: u8) -> Self {
        match v {
            0x01 => Self::ConnectAck,
            0x02 => Self::DisconnectAck,
            0x03 => Self::SetParamsAck,
            0x04 => Self::StatusReply,
            0x05 => Self::GetNextBufferAck,
            0x06 => Self::SendAck,
            other => Self::Reserved(other),
        }
    }
    /// Wire byte.
    #[must_use]
    pub const fn to_u8(self) -> u8 {
        match self {
            Self::ConnectAck => 0x01,
            Self::DisconnectAck => 0x02,
            Self::SetParamsAck => 0x03,
            Self::StatusReply => 0x04,
            Self::GetNextBufferAck => 0x05,
            Self::SendAck => 0x06,
            Self::Reserved(v) => v,
        }
    }
    /// Spec token, or `"reserved"`.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::ConnectAck => "Connect_Ack",
            Self::DisconnectAck => "Disconnect_Ack",
            Self::SetParamsAck => "Set_Params_Ack",
            Self::StatusReply => "Status_Reply",
            Self::GetNextBufferAck => "Get_Next_Buffer_Ack",
            Self::SendAck => "Send_Ack",
            Self::Reserved(_) => "reserved",
        }
    }
}
broadcast_common::impl_spec_display!(CommsReplyId, Reserved);

/// `comms_reply()` object (Table 54): id + signed return_value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CommsReply {
    /// `comms_reply_id`.
    pub reply_id: CommsReplyId,
    /// `return_value` — positive/zero = OK, negative = error (carried as raw u8).
    pub return_value: u8,
}

// comms_reply_id(1) + return_value(1).
const COMMS_REPLY_BODY: usize = 2;

impl<'a> Parse<'a> for CommsReply {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = super::parse_apdu_header(bytes, tag::COMMS_REPLY, "comms_reply")?;
        if body.len() < COMMS_REPLY_BODY {
            return Err(Error::BufferTooShort {
                need: COMMS_REPLY_BODY,
                have: body.len(),
                what: "comms_reply",
            });
        }
        Ok(Self {
            reply_id: CommsReplyId::from_u8(body[0]),
            return_value: body[1],
        })
    }
}

impl Serialize for CommsReply {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        super::apdu_len(COMMS_REPLY_BODY)
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let mut pos = super::write_apdu_header(tag::COMMS_REPLY, COMMS_REPLY_BODY, buf)?;
        buf[pos] = self.reply_id.to_u8();
        buf[pos + 1] = self.return_value;
        pos += COMMS_REPLY_BODY;
        Ok(pos)
    }
}

impl ApduDef<'_> for CommsReply {
    const TAG: ApduTag = tag::COMMS_REPLY;
    const NAME: &'static str = "COMMS_REPLY";
}

/// `comms_send()` object (Table 55): a `comms_phase_id` + message bytes.
///
/// The `_last` (`9F 8C 03`) / `_more` (`9F 8C 04`) tags share this body;
/// [`CommsSend::more`] selects which tag is written and is set from the tag.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CommsSend<'a> {
    /// `true` = `comms_send_more`; `false` = `comms_send_last`.
    pub more: bool,
    /// `comms_phase_id` (0 or 1).
    pub comms_phase_id: u8,
    /// `message_byte`s (max 254).
    #[cfg_attr(feature = "serde", serde(borrow, with = "super::bytes_serde"))]
    pub message: &'a [u8],
}

impl CommsSend<'_> {
    /// The `apdu_tag` for this object given [`CommsSend::more`].
    #[must_use]
    pub fn tag(&self) -> ApduTag {
        if self.more {
            tag::COMMS_SEND_MORE
        } else {
            tag::COMMS_SEND_LAST
        }
    }
}

impl<'a> Parse<'a> for CommsSend<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        if bytes.len() < 3 {
            return Err(Error::BufferTooShort {
                need: 3,
                have: bytes.len(),
                what: "comms_send tag",
            });
        }
        let t = ApduTag::from_bytes(bytes[0], bytes[1], bytes[2]);
        let (expected, more) = match t {
            tag::COMMS_SEND_MORE => (tag::COMMS_SEND_MORE, true),
            _ => (tag::COMMS_SEND_LAST, false),
        };
        let body = super::parse_apdu_header(bytes, expected, "comms_send")?;
        let comms_phase_id = *body.first().ok_or(Error::BufferTooShort {
            need: 1,
            have: 0,
            what: "comms_send comms_phase_id",
        })?;
        Ok(Self {
            more,
            comms_phase_id,
            message: &body[1..],
        })
    }
}

impl Serialize for CommsSend<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        super::apdu_len(1 + self.message.len())
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = 1 + self.message.len();
        let mut pos = super::write_apdu_header(self.tag(), body_len, buf)?;
        buf[pos] = self.comms_phase_id;
        pos += 1;
        buf[pos..pos + self.message.len()].copy_from_slice(self.message);
        pos += self.message.len();
        Ok(pos)
    }
}

impl<'a> ApduDef<'a> for CommsSend<'a> {
    const TAG: ApduTag = tag::COMMS_SEND_LAST;
    const NAME: &'static str = "COMMS_SEND";
}

/// `comms_rcv()` object (Table 56): a `comms_phase_id` + message bytes.
///
/// The `_last` (`9F 8C 05`) / `_more` (`9F 8C 06`) tags share this body;
/// [`CommsRcv::more`] selects which tag is written and is set from the tag.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CommsRcv<'a> {
    /// `true` = `comms_rcv_more`; `false` = `comms_rcv_last`.
    pub more: bool,
    /// `comms_phase_id`.
    pub comms_phase_id: u8,
    /// `message_byte`s.
    #[cfg_attr(feature = "serde", serde(borrow, with = "super::bytes_serde"))]
    pub message: &'a [u8],
}

impl CommsRcv<'_> {
    /// The `apdu_tag` for this object given [`CommsRcv::more`].
    #[must_use]
    pub fn tag(&self) -> ApduTag {
        if self.more {
            tag::COMMS_RCV_MORE
        } else {
            tag::COMMS_RCV_LAST
        }
    }
}

impl<'a> Parse<'a> for CommsRcv<'a> {
    type Error = Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        if bytes.len() < 3 {
            return Err(Error::BufferTooShort {
                need: 3,
                have: bytes.len(),
                what: "comms_rcv tag",
            });
        }
        let t = ApduTag::from_bytes(bytes[0], bytes[1], bytes[2]);
        let (expected, more) = match t {
            tag::COMMS_RCV_MORE => (tag::COMMS_RCV_MORE, true),
            _ => (tag::COMMS_RCV_LAST, false),
        };
        let body = super::parse_apdu_header(bytes, expected, "comms_rcv")?;
        let comms_phase_id = *body.first().ok_or(Error::BufferTooShort {
            need: 1,
            have: 0,
            what: "comms_rcv comms_phase_id",
        })?;
        Ok(Self {
            more,
            comms_phase_id,
            message: &body[1..],
        })
    }
}

impl Serialize for CommsRcv<'_> {
    type Error = Error;
    fn serialized_len(&self) -> usize {
        super::apdu_len(1 + self.message.len())
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let body_len = 1 + self.message.len();
        let mut pos = super::write_apdu_header(self.tag(), body_len, buf)?;
        buf[pos] = self.comms_phase_id;
        pos += 1;
        buf[pos..pos + self.message.len()].copy_from_slice(self.message);
        pos += self.message.len();
        Ok(pos)
    }
}

impl<'a> ApduDef<'a> for CommsRcv<'a> {
    const TAG: ApduTag = tag::COMMS_RCV_LAST;
    const NAME: &'static str = "COMMS_RCV";
}

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

    #[test]
    fn comms_cmd_connect_round_trips_and_bites() {
        let cmd = CommsCmd {
            command_id: CommsCommandId::ConnectOnChannel,
            params: CommsCmdParams::Connect {
                connection_descriptor: ConnectionDescriptor {
                    descriptor_type: ConnectionDescriptorType::CableReturnChannelDescriptor,
                    payload: &[0x07], // channel_id
                },
                retry_count: 3,
                timeout: 30,
            },
        };
        let bytes = cmd.to_bytes();
        // 9F8C00, body: id(01) + CD[9F8C01 len2 type02 chan07] + retry03 + to1E
        assert_eq!(
            bytes,
            [
                0x9F, 0x8C, 0x00, 0x09, 0x01, 0x9F, 0x8C, 0x01, 0x02, 0x02, 0x07, 0x03, 0x1E
            ]
        );
        assert_eq!(CommsCmd::parse(&bytes).unwrap(), cmd);

        let mut other = cmd.clone();
        other.params = CommsCmdParams::Connect {
            connection_descriptor: ConnectionDescriptor {
                descriptor_type: ConnectionDescriptorType::CableReturnChannelDescriptor,
                payload: &[0x08],
            },
            retry_count: 3,
            timeout: 30,
        };
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn comms_cmd_set_params_and_get_next() {
        let sp = CommsCmd {
            command_id: CommsCommandId::SetParams,
            params: CommsCmdParams::SetParams {
                buffer_size: 254,
                timeout: 10,
            },
        };
        let bytes = sp.to_bytes();
        assert_eq!(bytes, [0x9F, 0x8C, 0x00, 0x03, 0x03, 0xFE, 0x0A]);
        assert_eq!(CommsCmd::parse(&bytes).unwrap(), sp);
        assert_eq!(sp.command_id.name(), "Set_Params");

        let gnb = CommsCmd {
            command_id: CommsCommandId::GetNextBuffer,
            params: CommsCmdParams::GetNextBuffer { comms_phase_id: 1 },
        };
        let gb = gnb.to_bytes();
        assert_eq!(gb, [0x9F, 0x8C, 0x00, 0x02, 0x05, 0x01]);
        assert_eq!(CommsCmd::parse(&gb).unwrap(), gnb);
    }

    #[test]
    fn comms_cmd_disconnect_has_no_params() {
        let d = CommsCmd {
            command_id: CommsCommandId::DisconnectOnChannel,
            params: CommsCmdParams::None,
        };
        let bytes = d.to_bytes();
        assert_eq!(bytes, [0x9F, 0x8C, 0x00, 0x01, 0x02]);
        assert_eq!(CommsCmd::parse(&bytes).unwrap(), d);
    }

    #[test]
    fn connection_descriptor_standalone_round_trips() {
        let cd = ConnectionDescriptor {
            descriptor_type: ConnectionDescriptorType::SiTelephoneDescriptor,
            payload: &[0xAA, 0xBB, 0xCC],
        };
        let bytes = cd.to_bytes();
        assert_eq!(bytes, [0x9F, 0x8C, 0x01, 0x04, 0x01, 0xAA, 0xBB, 0xCC]);
        assert_eq!(ConnectionDescriptor::parse(&bytes).unwrap(), cd);
        let mut other = cd.clone();
        other.descriptor_type = ConnectionDescriptorType::CableReturnChannelDescriptor;
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn comms_reply_round_trips_and_bites() {
        let r = CommsReply {
            reply_id: CommsReplyId::StatusReply,
            return_value: 0x01, // Connected
        };
        let bytes = r.to_bytes();
        assert_eq!(bytes, [0x9F, 0x8C, 0x02, 0x02, 0x04, 0x01]);
        assert_eq!(CommsReply::parse(&bytes).unwrap(), r);
        assert_eq!(r.reply_id.name(), "Status_Reply");
        let mut other = r;
        other.return_value = 0x00;
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn comms_send_multibyte_round_trips_and_more_bites() {
        let s = CommsSend {
            more: false,
            comms_phase_id: 0,
            message: b"AT&F\r",
        };
        let bytes = s.to_bytes();
        assert_eq!(
            bytes,
            [0x9F, 0x8C, 0x03, 0x06, 0x00, b'A', b'T', b'&', b'F', b'\r']
        );
        assert_eq!(CommsSend::parse(&bytes).unwrap(), s);

        // more bite: flipping `more` flips the tag.
        let mut more = s.clone();
        more.more = true;
        let mb = more.to_bytes();
        assert_eq!(mb[2], 0x04);
        assert_ne!(bytes, mb);
        assert_eq!(CommsSend::parse(&mb).unwrap(), more);

        // field bite.
        let mut other = s.clone();
        other.comms_phase_id = 1;
        assert_ne!(bytes, other.to_bytes());
    }

    #[test]
    fn comms_rcv_round_trips_and_more_bites() {
        let r = CommsRcv {
            more: true,
            comms_phase_id: 1,
            message: b"OK\r\n",
        };
        let bytes = r.to_bytes();
        assert_eq!(bytes[2], 0x06); // rcv_more
        assert_eq!(CommsRcv::parse(&bytes).unwrap(), r);

        let mut last = r.clone();
        last.more = false;
        let lb = last.to_bytes();
        assert_eq!(lb[2], 0x05); // rcv_last
        assert_ne!(bytes, lb);
        assert_eq!(CommsRcv::parse(&lb).unwrap(), last);
    }
}