Skip to main content

dvb_si/descriptors/extension/
c2_bundle_delivery_system.rs

1//! C2 Bundle Delivery System Descriptor โ€” ETSI EN 300 468 ยง6.4.6.4 (tag_extension 0x16).
2use super::*;
3use alloc::vec::Vec;
4
5use super::c2_delivery_system::{ActiveOfdmSymbolDuration, C2GuardInterval, C2TuningFrequencyType};
6
7impl<'a> ExtensionBodyDef<'a> for C2BundleDeliverySystem {
8    const TAG_EXTENSION: u8 = 0x16;
9    const NAME: &'static str = "C2_BUNDLE_DELIVERY_SYSTEM";
10}
11/// One C2 bundle entry (Table 139 inner loop).
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize))]
14pub struct C2BundleEntry {
15    /// plp_id(8).
16    pub plp_id: u8,
17    /// data_slice_id(8).
18    pub data_slice_id: u8,
19    /// C2_System_tuning_frequency(32).
20    pub c2_system_tuning_frequency: u32,
21    /// C2_System_tuning_frequency_type(2) โ€” Table 116.
22    pub c2_system_tuning_frequency_type: C2TuningFrequencyType,
23    /// active_OFDM_symbol_duration(3).
24    pub active_ofdm_symbol_duration: ActiveOfdmSymbolDuration,
25    /// guard_interval(3).
26    pub guard_interval: C2GuardInterval,
27    /// primary_channel(1).
28    pub primary_channel: bool,
29}
30
31/// C2_bundle_delivery_system body (Table 139) โ€” fully typed.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize))]
34pub struct C2BundleDeliverySystem {
35    /// Bundle entries in wire order.
36    pub entries: Vec<C2BundleEntry>,
37}
38
39impl<'a> Parse<'a> for C2BundleDeliverySystem {
40    type Error = crate::error::Error;
41    fn parse(sel: &'a [u8]) -> Result<Self> {
42        if sel.len() % C2_BUNDLE_ENTRY_LEN != 0 {
43            return Err(invalid(
44                "C2_bundle_delivery_system: not a whole number of entries",
45            ));
46        }
47        let mut entries = Vec::with_capacity(sel.len() / C2_BUNDLE_ENTRY_LEN);
48        for chunk in sel.chunks_exact(C2_BUNDLE_ENTRY_LEN) {
49            let packed = chunk[6];
50            entries.push(C2BundleEntry {
51                plp_id: chunk[0],
52                data_slice_id: chunk[1],
53                c2_system_tuning_frequency: u32::from_be_bytes([
54                    chunk[2], chunk[3], chunk[4], chunk[5],
55                ]),
56                c2_system_tuning_frequency_type: C2TuningFrequencyType::from_u8(packed >> 6),
57                active_ofdm_symbol_duration: ActiveOfdmSymbolDuration::from_u8(
58                    (packed >> 3) & 0x07,
59                ),
60                guard_interval: C2GuardInterval::from_u8(packed & 0x07),
61                primary_channel: (chunk[7] & 0x80) != 0,
62            });
63        }
64        Ok(C2BundleDeliverySystem { entries })
65    }
66}
67
68impl Serialize for C2BundleDeliverySystem {
69    type Error = crate::error::Error;
70    fn serialized_len(&self) -> usize {
71        self.entries.len() * C2_BUNDLE_ENTRY_LEN
72    }
73    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
74        let len = self.serialized_len();
75        if buf.len() < len {
76            return Err(Error::OutputBufferTooSmall {
77                need: len,
78                have: buf.len(),
79            });
80        }
81        let mut p = 0;
82        for e in &self.entries {
83            buf[p] = e.plp_id;
84            buf[p + 1] = e.data_slice_id;
85            buf[p + 2..p + 6].copy_from_slice(&e.c2_system_tuning_frequency.to_be_bytes());
86            buf[p + 6] = (e.c2_system_tuning_frequency_type.to_u8() << 6)
87                | ((e.active_ofdm_symbol_duration.to_u8() & 0x07) << 3)
88                | (e.guard_interval.to_u8() & 0x07);
89            buf[p + 7] = u8::from(e.primary_channel) << 7;
90            p += C2_BUNDLE_ENTRY_LEN;
91        }
92        Ok(len)
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use crate::descriptors::extension::test_support::*;
100    use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor};
101
102    #[test]
103    fn parse_c2_bundle_two_entries() {
104        let entry = |off: u8| {
105            let packed = (0x01u8 << 6) | 0x01;
106            [off, off + 1, 0x00, 0x00, 0x10, 0x00, packed, 0x80]
107        };
108        let mut sel = Vec::new();
109        sel.extend_from_slice(&entry(0x01));
110        sel.extend_from_slice(&entry(0x05));
111        let bytes = wrap(0x16, &sel);
112        let d = ExtensionDescriptor::parse(&bytes).unwrap();
113        match &d.body {
114            ExtensionBody::C2BundleDeliverySystem(b) => {
115                assert_eq!(b.entries.len(), 2);
116                assert_eq!(b.entries[0].plp_id, 0x01);
117                assert!(b.entries[0].primary_channel);
118                assert_eq!(
119                    b.entries[0].c2_system_tuning_frequency_type,
120                    C2TuningFrequencyType::C2SystemCentreFrequency
121                );
122                assert_eq!(b.entries[1].plp_id, 0x05);
123                assert_eq!(b.entries[1].guard_interval, C2GuardInterval::G1_64);
124            }
125            other => panic!("expected C2BundleDeliverySystem, got {other:?}"),
126        }
127        round_trip(&d);
128    }
129
130    #[test]
131    fn parse_c2_bundle_rejects_partial_entry() {
132        let sel = [0x01, 0x02, 0x03];
133        let bytes = wrap(0x16, &sel);
134        assert!(matches!(
135            ExtensionDescriptor::parse(&bytes).unwrap_err(),
136            crate::error::Error::InvalidDescriptor {
137                tag: super::TAG,
138                ..
139            }
140        ));
141    }
142}