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