dvb_si/descriptors/extension/
c2_bundle_delivery_system.rs1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize))]
14pub struct C2BundleEntry {
15 pub plp_id: u8,
17 pub data_slice_id: u8,
19 pub c2_system_tuning_frequency: u32,
21 pub c2_system_tuning_frequency_type: C2TuningFrequencyType,
23 pub active_ofdm_symbol_duration: ActiveOfdmSymbolDuration,
25 pub guard_interval: C2GuardInterval,
27 pub primary_channel: bool,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize))]
34pub struct C2BundleDeliverySystem {
35 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 let freq_bytes = chunk[2..]
51 .first_chunk::<4>()
52 .expect("chunks_exact guarantees length");
53 entries.push(C2BundleEntry {
54 plp_id: chunk[0],
55 data_slice_id: chunk[1],
56 c2_system_tuning_frequency: u32::from_be_bytes(*freq_bytes),
57 c2_system_tuning_frequency_type: C2TuningFrequencyType::from_u8(packed >> 6),
58 active_ofdm_symbol_duration: ActiveOfdmSymbolDuration::from_u8(
59 (packed >> 3) & 0x07,
60 ),
61 guard_interval: C2GuardInterval::from_u8(packed & 0x07),
62 primary_channel: (chunk[7] & 0x80) != 0,
63 });
64 }
65 Ok(C2BundleDeliverySystem { entries })
66 }
67}
68
69impl Serialize for C2BundleDeliverySystem {
70 type Error = crate::error::Error;
71 fn serialized_len(&self) -> usize {
72 self.entries.len() * C2_BUNDLE_ENTRY_LEN
73 }
74 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
75 let len = self.serialized_len();
76 if buf.len() < len {
77 return Err(Error::OutputBufferTooSmall {
78 need: len,
79 have: buf.len(),
80 });
81 }
82 let mut p = 0;
83 for e in &self.entries {
84 buf[p] = e.plp_id;
85 buf[p + 1] = e.data_slice_id;
86 buf[p + 2..p + 6].copy_from_slice(&e.c2_system_tuning_frequency.to_be_bytes());
87 buf[p + 6] = (e.c2_system_tuning_frequency_type.to_u8() << 6)
88 | ((e.active_ofdm_symbol_duration.to_u8() & 0x07) << 3)
89 | (e.guard_interval.to_u8() & 0x07);
90 buf[p + 7] = u8::from(e.primary_channel) << 7;
91 p += C2_BUNDLE_ENTRY_LEN;
92 }
93 Ok(len)
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use crate::descriptors::extension::test_support::*;
101 use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor};
102
103 #[test]
104 fn parse_c2_bundle_two_entries() {
105 let entry = |off: u8| {
106 let packed = (0x01u8 << 6) | 0x01;
107 [off, off + 1, 0x00, 0x00, 0x10, 0x00, packed, 0x80]
108 };
109 let mut sel = Vec::new();
110 sel.extend_from_slice(&entry(0x01));
111 sel.extend_from_slice(&entry(0x05));
112 let bytes = wrap(0x16, &sel);
113 let d = ExtensionDescriptor::parse(&bytes).unwrap();
114 match &d.body {
115 ExtensionBody::C2BundleDeliverySystem(b) => {
116 assert_eq!(b.entries.len(), 2);
117 assert_eq!(b.entries[0].plp_id, 0x01);
118 assert!(b.entries[0].primary_channel);
119 assert_eq!(
120 b.entries[0].c2_system_tuning_frequency_type,
121 C2TuningFrequencyType::C2SystemCentreFrequency
122 );
123 assert_eq!(b.entries[1].plp_id, 0x05);
124 assert_eq!(b.entries[1].guard_interval, C2GuardInterval::G1_64);
125 }
126 other => panic!("expected C2BundleDeliverySystem, got {other:?}"),
127 }
128 round_trip(&d);
129 }
130
131 #[test]
132 fn parse_c2_bundle_rejects_partial_entry() {
133 let sel = [0x01, 0x02, 0x03];
134 let bytes = wrap(0x16, &sel);
135 assert!(matches!(
136 ExtensionDescriptor::parse(&bytes).unwrap_err(),
137 crate::error::Error::InvalidDescriptor {
138 tag: super::TAG,
139 ..
140 }
141 ));
142 }
143}