Skip to main content

dvb_si/tables/
ait.rs

1//! Application Information Table — ETSI TS 102 809 §5.3.4.
2//!
3//! AIT carries application metadata for HbbTV / interactive-TV services.
4//! Carried on a per-service PID with table_id 0x74.
5
6use crate::descriptors::DescriptorLoop;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// AIT table_id (ETSI TS 102 809 §5.3.4).
11pub const TABLE_ID: u8 = 0x74;
12/// AIT has no well-known PID — it is service-specific.
13pub const PID: u16 = 0x0000;
14
15const MIN_HEADER_LEN: usize = 3;
16const EXTENSION_HEADER_LEN: usize = 5;
17const COMMON_DESC_LEN_BYTES: usize = 2;
18const APP_LOOP_LEN_BYTES: usize = 2;
19const CRC_LEN: usize = 4;
20const APP_HEADER_LEN: usize = 9;
21const MIN_SECTION_LEN: usize =
22    MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES + APP_LOOP_LEN_BYTES + CRC_LEN;
23
24/// Application control code — ETSI TS 102 809 §5.2.4.1 Table 3.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27#[non_exhaustive]
28pub enum ControlCode {
29    /// 0x00 — reserved for future use.
30    Reserved,
31    /// 0x01 — AUTOSTART: started on service selection unless already running.
32    Autostart,
33    /// 0x02 — PRESENT: allowed to run but not auto-started.
34    Present,
35    /// 0x03 — DESTROY: stopped gracefully, no restart.
36    Destroy,
37    /// 0x04 — KILL: stopped immediately, no restart.
38    Kill,
39    /// 0x05 — PREFETCH: files cached, app not started.
40    Prefetch,
41    /// 0x06 — REMOTE: application not hosted by the current service.
42    Remote,
43    /// 0x07 — DISABLED: application shall not be available to the user.
44    Disabled,
45    /// 0x08 — PLAYBACK_AUTOSTART: autostart for playback services.
46    PlaybackAutostart,
47    /// Catch-all for reserved / unallocated wire values.
48    Unallocated(u8),
49}
50
51impl ControlCode {
52    #[must_use]
53    /// Decode from the wire value.  Every value maps (lossless).
54    pub fn from_u8(v: u8) -> Self {
55        match v {
56            0x00 => Self::Reserved,
57            0x01 => Self::Autostart,
58            0x02 => Self::Present,
59            0x03 => Self::Destroy,
60            0x04 => Self::Kill,
61            0x05 => Self::Prefetch,
62            0x06 => Self::Remote,
63            0x07 => Self::Disabled,
64            0x08 => Self::PlaybackAutostart,
65            _ => Self::Unallocated(v),
66        }
67    }
68
69    #[must_use]
70    /// Encode to the wire value.  Inverse of `from_u8` / `from_u16`.
71    pub fn to_u8(self) -> u8 {
72        match self {
73            Self::Reserved => 0x00,
74            Self::Autostart => 0x01,
75            Self::Present => 0x02,
76            Self::Destroy => 0x03,
77            Self::Kill => 0x04,
78            Self::Prefetch => 0x05,
79            Self::Remote => 0x06,
80            Self::Disabled => 0x07,
81            Self::PlaybackAutostart => 0x08,
82            Self::Unallocated(v) => v,
83        }
84    }
85
86    #[must_use]
87    /// Human-readable spec display name.
88    pub fn name(self) -> &'static str {
89        match self {
90            Self::Reserved => "Reserved",
91            Self::Autostart => "AUTOSTART",
92            Self::Present => "PRESENT",
93            Self::Destroy => "DESTROY",
94            Self::Kill => "KILL",
95            Self::Prefetch => "PREFETCH",
96            Self::Remote => "REMOTE",
97            Self::Disabled => "DISABLED",
98            Self::PlaybackAutostart => "PLAYBACK_AUTOSTART",
99            Self::Unallocated(_) => "Unallocated",
100        }
101    }
102}
103dvb_common::impl_spec_display!(ControlCode, Unallocated);
104
105/// Application type — ETSI TS 102 809 §5.2.4.2 Tables 2-3 (application_type).
106///
107/// 15-bit field identifying the application environment.
108/// Verified entries from the DVB Services registry.
109#[derive(Debug, Clone, Copy, PartialEq, Eq)]
110#[cfg_attr(feature = "serde", derive(serde::Serialize))]
111#[non_exhaustive]
112pub enum ApplicationType {
113    /// 0x0001 — DVB-J.
114    DvbJ,
115    /// 0x0002 — DVB-HTML.
116    DvbHtml,
117    /// 0x0010 — HbbTV.
118    HbbTv,
119    /// 0x0011 — OIPF DAE.
120    OipfDae,
121    /// Other values below `0x8000` — reserved for DVB use.
122    Reserved(u16),
123    /// `0x8000`..`0xFFFF` — user defined.
124    UserDefined(u16),
125}
126
127impl ApplicationType {
128    #[must_use]
129    /// Decode from the wire value.  Every value maps (lossless).
130    pub fn from_u16(v: u16) -> Self {
131        match v {
132            0x0001 => Self::DvbJ,
133            0x0002 => Self::DvbHtml,
134            0x0010 => Self::HbbTv,
135            0x0011 => Self::OipfDae,
136            v if v < 0x8000 => Self::Reserved(v),
137            _ => Self::UserDefined(v),
138        }
139    }
140
141    #[must_use]
142    /// Encode to the wire value.  Inverse of `from_u16`.
143    pub fn to_u16(self) -> u16 {
144        match self {
145            Self::DvbJ => 0x0001,
146            Self::DvbHtml => 0x0002,
147            Self::HbbTv => 0x0010,
148            Self::OipfDae => 0x0011,
149            Self::Reserved(v) | Self::UserDefined(v) => v,
150        }
151    }
152
153    #[must_use]
154    /// Human-readable spec display name.
155    pub fn name(self) -> &'static str {
156        match self {
157            Self::DvbJ => "DVB-J",
158            Self::DvbHtml => "DVB-HTML",
159            Self::HbbTv => "HbbTV",
160            Self::OipfDae => "OIPF DAE",
161            Self::Reserved(_) => "Reserved",
162            Self::UserDefined(_) => "User Defined",
163        }
164    }
165}
166dvb_common::impl_spec_display!(ApplicationType, Reserved, UserDefined);
167
168/// 48-bit application identifier: organisation_id + application_id.
169#[derive(Debug, Clone, PartialEq, Eq)]
170#[cfg_attr(feature = "serde", derive(serde::Serialize))]
171pub struct ApplicationIdentifier {
172    /// 32-bit organisation_id.
173    pub organisation_id: u32,
174    /// 16-bit application_id.
175    pub application_id: u16,
176}
177
178/// One application entry in the AIT application loop.
179#[derive(Debug, Clone, PartialEq, Eq)]
180#[cfg_attr(feature = "serde", derive(serde::Serialize))]
181#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
182pub struct AitApplication<'a> {
183    /// Application identifier.
184    pub identifier: ApplicationIdentifier,
185    /// Application control code (1 = autostart, etc.).
186    pub control_code: ControlCode,
187    /// Raw descriptor bytes for this application.
188    /// Per-application descriptor loop. Serializes as the typed descriptor
189    /// sequence; `.raw()` yields the wire bytes.
190    pub descriptors: DescriptorLoop<'a>,
191}
192
193/// Application Information Table.
194#[derive(Debug, Clone, PartialEq, Eq)]
195#[cfg_attr(feature = "serde", derive(serde::Serialize))]
196#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
197pub struct AitSection<'a> {
198    /// 15-bit application_type (e.g. 0x0010 for HbbTV).
199    pub application_type: ApplicationType,
200    /// Test application flag (bit 15 of the extension field).
201    pub test_application_flag: bool,
202    /// 5-bit version_number.
203    pub version_number: u8,
204    /// current_next_indicator bit.
205    pub current_next_indicator: bool,
206    /// section_number in the sub-table sequence.
207    pub section_number: u8,
208    /// last_section_number in the sub-table sequence.
209    pub last_section_number: u8,
210    /// Raw common descriptor bytes.
211    /// Common descriptor loop. Serializes as the typed descriptor sequence;
212    /// `.raw()` yields the wire bytes.
213    pub common_descriptors: DescriptorLoop<'a>,
214    /// Applications in wire order.
215    pub applications: Vec<AitApplication<'a>>,
216}
217
218impl<'a> Parse<'a> for AitSection<'a> {
219    type Error = crate::error::Error;
220    fn parse(bytes: &'a [u8]) -> Result<Self> {
221        let min_len = MIN_HEADER_LEN
222            + EXTENSION_HEADER_LEN
223            + COMMON_DESC_LEN_BYTES
224            + APP_LOOP_LEN_BYTES
225            + CRC_LEN;
226        if bytes.len() < min_len {
227            return Err(Error::BufferTooShort {
228                need: min_len,
229                have: bytes.len(),
230                what: "AitSection",
231            });
232        }
233
234        if bytes[0] != TABLE_ID {
235            return Err(Error::UnexpectedTableId {
236                table_id: bytes[0],
237                what: "AitSection",
238                expected: &[TABLE_ID],
239            });
240        }
241
242        let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
243        let total = super::check_section_length(
244            bytes.len(),
245            MIN_HEADER_LEN,
246            section_length as usize,
247            MIN_SECTION_LEN,
248        )?;
249
250        let test_application_flag = (bytes[3] & 0x80) != 0;
251        let application_type_raw = (((bytes[3] & 0x7F) as u16) << 8) | (bytes[4] as u16);
252        let application_type = ApplicationType::from_u16(application_type_raw);
253        let version_number = (bytes[5] >> 1) & 0x1F;
254        let current_next_indicator = (bytes[5] & 0x01) != 0;
255        let section_number = bytes[6];
256        let last_section_number = bytes[7];
257
258        let common_descriptors_length = (((bytes[8] & 0x0F) as usize) << 8) | bytes[9] as usize;
259        let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
260        let common_desc_end = common_desc_start + common_descriptors_length;
261        let app_loop_end = total - CRC_LEN;
262        if common_desc_end > app_loop_end {
263            return Err(Error::SectionLengthOverflow {
264                declared: common_descriptors_length,
265                available: app_loop_end.saturating_sub(common_desc_start),
266            });
267        }
268        let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
269
270        let app_loop_length =
271            (((bytes[common_desc_end] & 0x0F) as usize) << 8) | bytes[common_desc_end + 1] as usize;
272        let app_loop_start = common_desc_end + APP_LOOP_LEN_BYTES;
273        let app_loop_actual_end = app_loop_start + app_loop_length;
274        if app_loop_actual_end > app_loop_end {
275            return Err(Error::SectionLengthOverflow {
276                declared: app_loop_length,
277                available: app_loop_end.saturating_sub(app_loop_start),
278            });
279        }
280
281        let mut applications = Vec::new();
282        let mut pos = app_loop_start;
283        while pos + APP_HEADER_LEN <= app_loop_actual_end {
284            let organisation_id = ((bytes[pos] as u32) << 24)
285                | ((bytes[pos + 1] as u32) << 16)
286                | ((bytes[pos + 2] as u32) << 8)
287                | (bytes[pos + 3] as u32);
288            let application_id = u16::from_be_bytes([bytes[pos + 4], bytes[pos + 5]]);
289            let control_code = ControlCode::from_u8(bytes[pos + 6]);
290            let app_desc_length =
291                (((bytes[pos + 7] & 0x0F) as usize) << 8) | bytes[pos + 8] as usize;
292            let app_desc_start = pos + APP_HEADER_LEN;
293            let app_desc_end = app_desc_start + app_desc_length;
294            if app_desc_end > app_loop_actual_end {
295                return Err(Error::SectionLengthOverflow {
296                    declared: app_desc_length,
297                    available: app_loop_actual_end.saturating_sub(app_desc_start),
298                });
299            }
300            applications.push(AitApplication {
301                identifier: ApplicationIdentifier {
302                    organisation_id,
303                    application_id,
304                },
305                control_code,
306                descriptors: DescriptorLoop::new(&bytes[app_desc_start..app_desc_end]),
307            });
308            pos = app_desc_end;
309        }
310
311        Ok(AitSection {
312            application_type,
313            test_application_flag,
314            version_number,
315            current_next_indicator,
316            section_number,
317            last_section_number,
318            common_descriptors,
319            applications,
320        })
321    }
322}
323
324impl Serialize for AitSection<'_> {
325    type Error = crate::error::Error;
326    fn serialized_len(&self) -> usize {
327        let app_bytes: usize = self
328            .applications
329            .iter()
330            .map(|a| APP_HEADER_LEN + a.descriptors.len())
331            .sum();
332        MIN_HEADER_LEN
333            + EXTENSION_HEADER_LEN
334            + COMMON_DESC_LEN_BYTES
335            + self.common_descriptors.len()
336            + APP_LOOP_LEN_BYTES
337            + app_bytes
338            + CRC_LEN
339    }
340
341    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
342        let len = self.serialized_len();
343        if buf.len() < len {
344            return Err(Error::OutputBufferTooSmall {
345                need: len,
346                have: buf.len(),
347            });
348        }
349
350        let section_length: u16 = (len - MIN_HEADER_LEN) as u16;
351        let app_type_raw = self.application_type.to_u16();
352        buf[0] = TABLE_ID;
353        buf[1] = super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F);
354        buf[2] = (section_length & 0xFF) as u8;
355        buf[3] = (u8::from(self.test_application_flag) << 7) | ((app_type_raw >> 8) as u8 & 0x7F);
356        buf[4] = (app_type_raw & 0xFF) as u8;
357        buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
358        buf[6] = self.section_number;
359        buf[7] = self.last_section_number;
360
361        let cdl = self.common_descriptors.len() as u16;
362        buf[8] = 0xF0 | ((cdl >> 8) as u8 & 0x0F);
363        buf[9] = (cdl & 0xFF) as u8;
364
365        let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
366        buf[common_desc_start..common_desc_start + self.common_descriptors.len()]
367            .copy_from_slice(self.common_descriptors.raw());
368
369        let app_loop_start = common_desc_start + self.common_descriptors.len();
370        let app_bytes: usize = self
371            .applications
372            .iter()
373            .map(|a| APP_HEADER_LEN + a.descriptors.len())
374            .sum();
375        let apl = app_bytes as u16;
376        buf[app_loop_start] = 0xF0 | ((apl >> 8) as u8 & 0x0F);
377        buf[app_loop_start + 1] = (apl & 0xFF) as u8;
378
379        let mut pos = app_loop_start + APP_LOOP_LEN_BYTES;
380        for app in &self.applications {
381            buf[pos..pos + 4].copy_from_slice(&app.identifier.organisation_id.to_be_bytes());
382            buf[pos + 4..pos + 6].copy_from_slice(&app.identifier.application_id.to_be_bytes());
383            buf[pos + 6] = app.control_code.to_u8();
384            let adl = app.descriptors.len() as u16;
385            buf[pos + 7] = 0xF0 | ((adl >> 8) as u8 & 0x0F);
386            buf[pos + 8] = (adl & 0xFF) as u8;
387            let desc_start = pos + APP_HEADER_LEN;
388            buf[desc_start..desc_start + app.descriptors.len()]
389                .copy_from_slice(app.descriptors.raw());
390            pos = desc_start + app.descriptors.len();
391        }
392
393        let crc_pos = len - CRC_LEN;
394        let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
395        buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
396        Ok(len)
397    }
398}
399impl<'a> crate::traits::TableDef<'a> for AitSection<'a> {
400    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
401    const NAME: &'static str = "APPLICATION_INFORMATION";
402}
403
404#[cfg(test)]
405mod tests {
406    use super::*;
407
408    fn build_ait(
409        application_type: u16,
410        test_flag: bool,
411        version: u8,
412        common_descriptors: &[u8],
413        applications: &[(u32, u16, u8, Vec<u8>)],
414    ) -> Vec<u8> {
415        let app_bytes: usize = applications
416            .iter()
417            .map(|(_, _, _, d)| APP_HEADER_LEN + d.len())
418            .sum();
419        let section_length: u16 = (EXTENSION_HEADER_LEN
420            + COMMON_DESC_LEN_BYTES
421            + common_descriptors.len()
422            + APP_LOOP_LEN_BYTES
423            + app_bytes
424            + CRC_LEN) as u16;
425        let mut v = vec![
426            TABLE_ID,
427            super::super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F),
428            (section_length & 0xFF) as u8,
429            (u8::from(test_flag) << 7) | ((application_type >> 8) as u8 & 0x7F),
430            (application_type & 0xFF) as u8,
431            0xC0 | ((version & 0x1F) << 1) | 0x01,
432            0,
433            0,
434        ];
435        let cdl = common_descriptors.len() as u16;
436        v.push(0xF0 | ((cdl >> 8) as u8 & 0x0F));
437        v.push((cdl & 0xFF) as u8);
438        v.extend_from_slice(common_descriptors);
439        let apl = app_bytes as u16;
440        v.push(0xF0 | ((apl >> 8) as u8 & 0x0F));
441        v.push((apl & 0xFF) as u8);
442        for &(org_id, app_id, cc, ref desc) in applications {
443            v.extend_from_slice(&org_id.to_be_bytes());
444            v.extend_from_slice(&app_id.to_be_bytes());
445            v.push(cc);
446            let adl = desc.len() as u16;
447            v.push(0xF0 | ((adl >> 8) as u8 & 0x0F));
448            v.push((adl & 0xFF) as u8);
449            v.extend_from_slice(desc);
450        }
451        v.extend_from_slice(&[0, 0, 0, 0]);
452        v
453    }
454
455    #[test]
456    fn parse_rejects_wrong_table_id() {
457        let mut bytes = build_ait(0x0010, false, 0, &[], &[]);
458        bytes[0] = 0x00;
459        let err = AitSection::parse(&bytes).unwrap_err();
460        assert!(matches!(
461            err,
462            Error::UnexpectedTableId { table_id: 0x00, .. }
463        ));
464    }
465
466    #[test]
467    fn parse_rejects_short_buffer() {
468        let err = AitSection::parse(&[0x74, 0x00]).unwrap_err();
469        assert!(matches!(err, Error::BufferTooShort { .. }));
470    }
471
472    #[test]
473    fn parse_empty_ait_no_applications() {
474        let bytes = build_ait(0x0010, false, 5, &[], &[]);
475        let ait = AitSection::parse(&bytes).expect("parse");
476        assert_eq!(ait.application_type, ApplicationType::HbbTv);
477        assert!(!ait.test_application_flag);
478        assert_eq!(ait.version_number, 5);
479        assert!(ait.current_next_indicator);
480        assert_eq!(ait.section_number, 0);
481        assert_eq!(ait.last_section_number, 0);
482        assert_eq!(ait.common_descriptors.len(), 0);
483        assert_eq!(ait.applications.len(), 0);
484    }
485
486    #[test]
487    fn parse_test_application_flag_extracted() {
488        let bytes = build_ait(0x0010, true, 0, &[], &[]);
489        let ait = AitSection::parse(&bytes).unwrap();
490        assert!(ait.test_application_flag);
491    }
492
493    #[test]
494    fn parse_common_descriptors_preserved() {
495        let desc = vec![0x00, 0x02, 0xAA, 0xBB];
496        let bytes = build_ait(0x0010, false, 0, &desc, &[]);
497        let ait = AitSection::parse(&bytes).unwrap();
498        assert_eq!(ait.common_descriptors.raw(), &desc[..]);
499    }
500
501    #[test]
502    fn parse_single_application() {
503        let desc = vec![0x02, 0x03, 0xCC, 0xDD, 0xEE];
504        let bytes = build_ait(
505            0x0010,
506            false,
507            0,
508            &[],
509            &[(0x12345678, 0xABCD, 0x01, desc.clone())],
510        );
511        let ait = AitSection::parse(&bytes).unwrap();
512        assert_eq!(ait.applications.len(), 1);
513        assert_eq!(ait.applications[0].identifier.organisation_id, 0x12345678);
514        assert_eq!(ait.applications[0].identifier.application_id, 0xABCD);
515        assert_eq!(ait.applications[0].control_code, ControlCode::Autostart);
516        assert_eq!(ait.applications[0].descriptors.raw(), &desc[..]);
517    }
518
519    #[test]
520    fn parse_multiple_applications_preserve_order() {
521        let bytes = build_ait(
522            0x0010,
523            false,
524            0,
525            &[],
526            &[
527                (0x00000001, 0x0001, 0x01, vec![]),
528                (0x00000002, 0x0002, 0x02, vec![0x01]),
529                (0x00000003, 0x0003, 0x03, vec![0x02, 0x03]),
530            ],
531        );
532        let ait = AitSection::parse(&bytes).unwrap();
533        assert_eq!(ait.applications.len(), 3);
534        assert_eq!(ait.applications[0].identifier.organisation_id, 1);
535        assert_eq!(ait.applications[1].identifier.organisation_id, 2);
536        assert_eq!(ait.applications[2].identifier.organisation_id, 3);
537    }
538
539    #[test]
540    fn serialize_round_trip_empty() {
541        let ait = AitSection {
542            application_type: ApplicationType::HbbTv,
543            test_application_flag: false,
544            version_number: 3,
545            current_next_indicator: true,
546            section_number: 0,
547            last_section_number: 0,
548            common_descriptors: DescriptorLoop::new(&[]),
549            applications: vec![],
550        };
551        let mut buf = vec![0u8; ait.serialized_len()];
552        ait.serialize_into(&mut buf).unwrap();
553        let reparsed = AitSection::parse(&buf).unwrap();
554        assert_eq!(ait, reparsed);
555    }
556
557    #[test]
558    fn serialize_round_trip_with_applications() {
559        let desc1: [u8; 2] = [0xAA, 0xBB];
560        let ait = AitSection {
561            application_type: ApplicationType::HbbTv,
562            test_application_flag: true,
563            version_number: 7,
564            current_next_indicator: true,
565            section_number: 1,
566            last_section_number: 2,
567            common_descriptors: DescriptorLoop::new(&[0x01, 0x00]),
568            applications: vec![
569                AitApplication {
570                    identifier: ApplicationIdentifier {
571                        organisation_id: 0x12345678,
572                        application_id: 0xABCD,
573                    },
574                    control_code: ControlCode::Autostart,
575                    descriptors: DescriptorLoop::new(&desc1),
576                },
577                AitApplication {
578                    identifier: ApplicationIdentifier {
579                        organisation_id: 0x87654321,
580                        application_id: 0x00EF,
581                    },
582                    control_code: ControlCode::Present,
583                    descriptors: DescriptorLoop::new(&[]),
584                },
585            ],
586        };
587        let mut buf = vec![0u8; ait.serialized_len()];
588        ait.serialize_into(&mut buf).unwrap();
589        let reparsed = AitSection::parse(&buf).unwrap();
590        assert_eq!(ait, reparsed);
591    }
592
593    #[test]
594    fn parse_rejects_zero_section_length() {
595        let mut buf = vec![0u8; 64];
596        buf[0] = TABLE_ID;
597        buf[1] = 0xF0;
598        buf[2] = 0x00;
599        for b in &mut buf[3..] {
600            *b = 0xFF;
601        }
602        assert!(matches!(
603            AitSection::parse(&buf).unwrap_err(),
604            Error::SectionLengthOverflow { .. }
605        ));
606    }
607
608    #[test]
609    fn control_code_full_range_round_trip() {
610        for byte in 0u8..=0xFF {
611            let cc = ControlCode::from_u8(byte);
612            assert_eq!(
613                cc.to_u8(),
614                byte,
615                "ControlCode round-trip failed for {byte:#04x}"
616            );
617        }
618    }
619
620    #[test]
621    fn control_code_named_values() {
622        assert_eq!(ControlCode::Autostart.to_u8(), 0x01);
623        assert_eq!(ControlCode::Kill.to_u8(), 0x04);
624        assert_eq!(ControlCode::Prefetch.to_u8(), 0x05);
625        assert_eq!(ControlCode::PlaybackAutostart.to_u8(), 0x08);
626    }
627
628    #[test]
629    fn control_code_wire_to_name() {
630        assert_eq!(ControlCode::from_u8(0x01).name(), "AUTOSTART");
631        assert_eq!(ControlCode::from_u8(0x04).name(), "KILL");
632        assert_eq!(ControlCode::from_u8(0x08).name(), "PLAYBACK_AUTOSTART");
633        assert_eq!(ControlCode::from_u8(0x00).name(), "Reserved");
634    }
635
636    #[test]
637    fn application_type_full_range_round_trip() {
638        for at in 0u16..=0xFFFF {
639            let app = ApplicationType::from_u16(at);
640            assert_eq!(
641                app.to_u16(),
642                at,
643                "ApplicationType round-trip failed for {at:#06x}"
644            );
645        }
646    }
647}