Skip to main content

dvb_si/descriptors/extension/
t2_delivery_system.rs

1//! T2 Delivery System Descriptor — ETSI EN 300 468 §6.4.6.3 (tag_extension 0x04).
2use super::*;
3use alloc::vec;
4use alloc::vec::Vec;
5
6impl<'a> ExtensionBodyDef<'a> for T2DeliverySystem {
7    const TAG_EXTENSION: u8 = 0x04;
8    const NAME: &'static str = "T2_DELIVERY_SYSTEM";
9}
10
11// ---------------------------------------------------------------------------
12//  T2-specific enums (Tables 134-137)
13// ---------------------------------------------------------------------------
14
15/// SISO/MISO mode — ETSI EN 300 468 Table 134.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18#[non_exhaustive]
19pub enum T2SisoMiso {
20    /// Single Input, Single Output (SISO).
21    Siso,
22    /// Multiple Input, Single Output (MISO).
23    Miso,
24    /// Reserved / future use.
25    Reserved(u8),
26}
27
28impl T2SisoMiso {
29    #[must_use]
30    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
31    pub fn from_u8(v: u8) -> Self {
32        match v {
33            0 => T2SisoMiso::Siso,
34            1 => T2SisoMiso::Miso,
35            other => T2SisoMiso::Reserved(other),
36        }
37    }
38
39    #[must_use]
40    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
41    pub fn to_u8(self) -> u8 {
42        match self {
43            T2SisoMiso::Siso => 0,
44            T2SisoMiso::Miso => 1,
45            T2SisoMiso::Reserved(v) => v,
46        }
47    }
48
49    #[must_use]
50    /// Human-readable spec name per the governing Table.
51    pub fn name(self) -> &'static str {
52        match self {
53            T2SisoMiso::Siso => "SISO",
54            T2SisoMiso::Miso => "MISO",
55            T2SisoMiso::Reserved(_) => "reserved",
56        }
57    }
58}
59dvb_common::impl_spec_display!(T2SisoMiso, Reserved);
60
61/// Bandwidth — ETSI EN 300 468 Table 135.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63#[cfg_attr(feature = "serde", derive(serde::Serialize))]
64#[non_exhaustive]
65pub enum T2Bandwidth {
66    /// 8 MHz.
67    Mhz8,
68    /// 7 MHz.
69    Mhz7,
70    /// 6 MHz.
71    Mhz6,
72    /// 5 MHz.
73    Mhz5,
74    /// 10 MHz.
75    Mhz10,
76    /// 1.712 MHz.
77    Mhz1_712,
78    /// Reserved / future use.
79    Reserved(u8),
80}
81
82impl T2Bandwidth {
83    #[must_use]
84    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
85    pub fn from_u8(v: u8) -> Self {
86        match v {
87            0 => T2Bandwidth::Mhz8,
88            1 => T2Bandwidth::Mhz7,
89            2 => T2Bandwidth::Mhz6,
90            3 => T2Bandwidth::Mhz5,
91            4 => T2Bandwidth::Mhz10,
92            5 => T2Bandwidth::Mhz1_712,
93            other => T2Bandwidth::Reserved(other),
94        }
95    }
96
97    #[must_use]
98    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
99    pub fn to_u8(self) -> u8 {
100        match self {
101            T2Bandwidth::Mhz8 => 0,
102            T2Bandwidth::Mhz7 => 1,
103            T2Bandwidth::Mhz6 => 2,
104            T2Bandwidth::Mhz5 => 3,
105            T2Bandwidth::Mhz10 => 4,
106            T2Bandwidth::Mhz1_712 => 5,
107            T2Bandwidth::Reserved(v) => v,
108        }
109    }
110
111    #[must_use]
112    /// Human-readable spec name per the governing Table.
113    pub fn name(self) -> &'static str {
114        match self {
115            T2Bandwidth::Mhz8 => "8 MHz",
116            T2Bandwidth::Mhz7 => "7 MHz",
117            T2Bandwidth::Mhz6 => "6 MHz",
118            T2Bandwidth::Mhz5 => "5 MHz",
119            T2Bandwidth::Mhz10 => "10 MHz",
120            T2Bandwidth::Mhz1_712 => "1.712 MHz",
121            T2Bandwidth::Reserved(_) => "reserved",
122        }
123    }
124}
125dvb_common::impl_spec_display!(T2Bandwidth, Reserved);
126
127/// Guard interval — ETSI EN 300 468 Table 136.
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize))]
130#[non_exhaustive]
131pub enum T2GuardInterval {
132    /// 1/32.
133    G1_32,
134    /// 1/16.
135    G1_16,
136    /// 1/8.
137    G1_8,
138    /// 1/4.
139    G1_4,
140    /// 1/128.
141    G1_128,
142    /// 19/128.
143    G19_128,
144    /// 19/256.
145    G19_256,
146    /// Reserved / future use.
147    Reserved(u8),
148}
149
150impl T2GuardInterval {
151    #[must_use]
152    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
153    pub fn from_u8(v: u8) -> Self {
154        match v {
155            0 => T2GuardInterval::G1_32,
156            1 => T2GuardInterval::G1_16,
157            2 => T2GuardInterval::G1_8,
158            3 => T2GuardInterval::G1_4,
159            4 => T2GuardInterval::G1_128,
160            5 => T2GuardInterval::G19_128,
161            6 => T2GuardInterval::G19_256,
162            other => T2GuardInterval::Reserved(other),
163        }
164    }
165
166    #[must_use]
167    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
168    pub fn to_u8(self) -> u8 {
169        match self {
170            T2GuardInterval::G1_32 => 0,
171            T2GuardInterval::G1_16 => 1,
172            T2GuardInterval::G1_8 => 2,
173            T2GuardInterval::G1_4 => 3,
174            T2GuardInterval::G1_128 => 4,
175            T2GuardInterval::G19_128 => 5,
176            T2GuardInterval::G19_256 => 6,
177            T2GuardInterval::Reserved(v) => v,
178        }
179    }
180
181    #[must_use]
182    /// Human-readable spec name per the governing Table.
183    pub fn name(self) -> &'static str {
184        match self {
185            T2GuardInterval::G1_32 => "1/32",
186            T2GuardInterval::G1_16 => "1/16",
187            T2GuardInterval::G1_8 => "1/8",
188            T2GuardInterval::G1_4 => "1/4",
189            T2GuardInterval::G1_128 => "1/128",
190            T2GuardInterval::G19_128 => "19/128",
191            T2GuardInterval::G19_256 => "19/256",
192            T2GuardInterval::Reserved(_) => "reserved",
193        }
194    }
195}
196dvb_common::impl_spec_display!(T2GuardInterval, Reserved);
197
198/// Transmission mode — ETSI EN 300 468 Table 137.
199#[derive(Debug, Clone, Copy, PartialEq, Eq)]
200#[cfg_attr(feature = "serde", derive(serde::Serialize))]
201#[non_exhaustive]
202pub enum T2TransmissionMode {
203    /// 2k mode.
204    Mode2k,
205    /// 8k mode.
206    Mode8k,
207    /// 4k mode.
208    Mode4k,
209    /// 1k mode.
210    Mode1k,
211    /// 16k mode.
212    Mode16k,
213    /// 32k mode.
214    Mode32k,
215    /// Reserved / future use.
216    Reserved(u8),
217}
218
219impl T2TransmissionMode {
220    #[must_use]
221    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
222    pub fn from_u8(v: u8) -> Self {
223        match v {
224            0 => T2TransmissionMode::Mode2k,
225            1 => T2TransmissionMode::Mode8k,
226            2 => T2TransmissionMode::Mode4k,
227            3 => T2TransmissionMode::Mode1k,
228            4 => T2TransmissionMode::Mode16k,
229            5 => T2TransmissionMode::Mode32k,
230            other => T2TransmissionMode::Reserved(other),
231        }
232    }
233
234    #[must_use]
235    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
236    pub fn to_u8(self) -> u8 {
237        match self {
238            T2TransmissionMode::Mode2k => 0,
239            T2TransmissionMode::Mode8k => 1,
240            T2TransmissionMode::Mode4k => 2,
241            T2TransmissionMode::Mode1k => 3,
242            T2TransmissionMode::Mode16k => 4,
243            T2TransmissionMode::Mode32k => 5,
244            T2TransmissionMode::Reserved(v) => v,
245        }
246    }
247
248    #[must_use]
249    /// Human-readable spec name per the governing Table.
250    pub fn name(self) -> &'static str {
251        match self {
252            T2TransmissionMode::Mode2k => "2k",
253            T2TransmissionMode::Mode8k => "8k",
254            T2TransmissionMode::Mode4k => "4k",
255            T2TransmissionMode::Mode1k => "1k",
256            T2TransmissionMode::Mode16k => "16k",
257            T2TransmissionMode::Mode32k => "32k",
258            T2TransmissionMode::Reserved(_) => "reserved",
259        }
260    }
261}
262dvb_common::impl_spec_display!(T2TransmissionMode, Reserved);
263
264// ---------------------------------------------------------------------------
265//  Structs
266// ---------------------------------------------------------------------------
267
268/// One T2 cell (Table 133 inner `for`).
269#[derive(Debug, Clone, PartialEq, Eq)]
270#[cfg_attr(feature = "serde", derive(serde::Serialize))]
271pub struct T2Cell {
272    /// cell_id(16).
273    pub cell_id: u16,
274    /// centre_frequency list. When tfs_flag, the length-prefixed loop;
275    /// otherwise exactly one frequency.
276    pub centre_frequencies: Vec<u32>,
277    /// subcell entries.
278    pub subcells: Vec<T2Subcell>,
279}
280
281impl T2Cell {
282    /// Decode all centre_frequencies to Hz (×10 Hz field resolution).
283    #[must_use]
284    pub fn centre_frequencies_hz(&self) -> Vec<u64> {
285        self.centre_frequencies
286            .iter()
287            .map(|&f| u64::from(f) * 10)
288            .collect()
289    }
290}
291
292/// One T2 subcell (Table 133 innermost `for`).
293#[derive(Debug, Clone, Copy, PartialEq, Eq)]
294#[cfg_attr(feature = "serde", derive(serde::Serialize))]
295pub struct T2Subcell {
296    /// cell_id_extension(8).
297    pub cell_id_extension: u8,
298    /// transposer_frequency(32) — ×10 Hz units.
299    pub transposer_frequency: u32,
300}
301
302impl T2Subcell {
303    /// Decode transposer_frequency to Hz (×10 Hz field resolution).
304    #[must_use]
305    pub fn transposer_frequency_hz(&self) -> u64 {
306        u64::from(self.transposer_frequency) * 10
307    }
308}
309
310/// T2_delivery_system body (Table 133). The cell loop is unfolded.
311#[derive(Debug, Clone, PartialEq, Eq)]
312#[cfg_attr(feature = "serde", derive(serde::Serialize))]
313pub struct T2DeliverySystem {
314    /// PLP identifier.
315    pub plp_id: u8,
316    /// T2 system identifier.
317    pub t2_system_id: u16,
318    /// SISO_MISO, present iff `descriptor_length > 4` (flags block present).
319    pub siso_miso: Option<T2SisoMiso>,
320    /// bandwidth, present with `siso_miso`.
321    pub bandwidth: Option<T2Bandwidth>,
322    /// guard_interval, present with `siso_miso`.
323    pub guard_interval: Option<T2GuardInterval>,
324    /// transmission_mode, present with `siso_miso`.
325    pub transmission_mode: Option<T2TransmissionMode>,
326    /// other_frequency_flag, present with `siso_miso`.
327    pub other_frequency_flag: Option<bool>,
328    /// tfs_flag, present with `siso_miso`.
329    pub tfs_flag: Option<bool>,
330    /// Cell loop entries (present only when flags block is present).
331    pub cells: Vec<T2Cell>,
332}
333
334impl<'a> Parse<'a> for T2DeliverySystem {
335    type Error = crate::error::Error;
336    fn parse(sel: &'a [u8]) -> Result<Self> {
337        if sel.len() < T2_FIXED_PREFIX_LEN {
338            return Err(Error::BufferTooShort {
339                need: T2_FIXED_PREFIX_LEN,
340                have: sel.len(),
341                what: "T2_delivery_system body",
342            });
343        }
344        let plp_id = sel[0];
345        let t2_system_id = u16::from_be_bytes([sel[1], sel[2]]);
346        let mut pos = T2_FIXED_PREFIX_LEN;
347        let (
348            siso_miso,
349            bandwidth,
350            guard_interval,
351            transmission_mode,
352            other_frequency_flag,
353            tfs_flag,
354        ) = if sel.len() > T2_FIXED_PREFIX_LEN {
355            if sel.len() < T2_FIXED_PREFIX_LEN + T2_FLAGS_BLOCK_LEN {
356                return Err(Error::BufferTooShort {
357                    need: T2_FIXED_PREFIX_LEN + T2_FLAGS_BLOCK_LEN,
358                    have: sel.len(),
359                    what: "T2_delivery_system body",
360                });
361            }
362            let b0 = sel[pos];
363            let b1 = sel[pos + 1];
364            pos += T2_FLAGS_BLOCK_LEN;
365            (
366                Some(T2SisoMiso::from_u8(b0 >> 6)),
367                Some(T2Bandwidth::from_u8((b0 >> 2) & 0x0F)),
368                Some(T2GuardInterval::from_u8(b1 >> 5)),
369                Some(T2TransmissionMode::from_u8((b1 >> 2) & 0x07)),
370                Some((b1 & 0x02) != 0),
371                Some((b1 & 0x01) != 0),
372            )
373        } else {
374            (None, None, None, None, None, None)
375        };
376        let cells = if siso_miso.is_some() {
377            let tfs = tfs_flag.unwrap();
378            let mut cells = Vec::new();
379            while pos < sel.len() {
380                if pos + 2 > sel.len() {
381                    return Err(Error::BufferTooShort {
382                        need: pos + 2,
383                        have: sel.len(),
384                        what: "T2_delivery_system body",
385                    });
386                }
387                let cell_id = u16::from_be_bytes([sel[pos], sel[pos + 1]]);
388                pos += 2;
389                let centre_frequencies = if tfs {
390                    if pos >= sel.len() {
391                        return Err(Error::BufferTooShort {
392                            need: pos + 1,
393                            have: sel.len(),
394                            what: "T2_delivery_system body",
395                        });
396                    }
397                    let freq_loop_len = sel[pos] as usize;
398                    pos += 1;
399                    if freq_loop_len % 4 != 0 {
400                        return Err(invalid(
401                            "T2_delivery_system: frequency_loop_length not a multiple of 4",
402                        ));
403                    }
404                    if pos + freq_loop_len > sel.len() {
405                        return Err(Error::BufferTooShort {
406                            need: pos + freq_loop_len,
407                            have: sel.len(),
408                            what: "T2_delivery_system body",
409                        });
410                    }
411                    let end = pos + freq_loop_len;
412                    let mut freqs = Vec::with_capacity(freq_loop_len / 4);
413                    while pos < end {
414                        freqs.push(u32::from_be_bytes([
415                            sel[pos],
416                            sel[pos + 1],
417                            sel[pos + 2],
418                            sel[pos + 3],
419                        ]));
420                        pos += 4;
421                    }
422                    freqs
423                } else {
424                    if pos + 4 > sel.len() {
425                        return Err(Error::BufferTooShort {
426                            need: pos + 4,
427                            have: sel.len(),
428                            what: "T2_delivery_system body",
429                        });
430                    }
431                    let freq =
432                        u32::from_be_bytes([sel[pos], sel[pos + 1], sel[pos + 2], sel[pos + 3]]);
433                    pos += 4;
434                    vec![freq]
435                };
436                if pos >= sel.len() {
437                    return Err(Error::BufferTooShort {
438                        need: pos + 1,
439                        have: sel.len(),
440                        what: "T2_delivery_system body",
441                    });
442                }
443                let subcell_loop_len = sel[pos] as usize;
444                pos += 1;
445                if subcell_loop_len % 5 != 0 {
446                    return Err(invalid(
447                        "T2_delivery_system: subcell_info_loop_length not a multiple of 5",
448                    ));
449                }
450                if pos + subcell_loop_len > sel.len() {
451                    return Err(Error::BufferTooShort {
452                        need: pos + subcell_loop_len,
453                        have: sel.len(),
454                        what: "T2_delivery_system body",
455                    });
456                }
457                let end = pos + subcell_loop_len;
458                let mut subcells = Vec::with_capacity(subcell_loop_len / 5);
459                while pos < end {
460                    subcells.push(T2Subcell {
461                        cell_id_extension: sel[pos],
462                        transposer_frequency: u32::from_be_bytes([
463                            sel[pos + 1],
464                            sel[pos + 2],
465                            sel[pos + 3],
466                            sel[pos + 4],
467                        ]),
468                    });
469                    pos += 5;
470                }
471                cells.push(T2Cell {
472                    cell_id,
473                    centre_frequencies,
474                    subcells,
475                });
476            }
477            cells
478        } else {
479            Vec::new()
480        };
481        Ok(T2DeliverySystem {
482            plp_id,
483            t2_system_id,
484            siso_miso,
485            bandwidth,
486            guard_interval,
487            transmission_mode,
488            other_frequency_flag,
489            tfs_flag,
490            cells,
491        })
492    }
493}
494
495impl Serialize for T2DeliverySystem {
496    type Error = crate::error::Error;
497    fn serialized_len(&self) -> usize {
498        let mut len = T2_FIXED_PREFIX_LEN;
499        if self.siso_miso.is_some() {
500            len += T2_FLAGS_BLOCK_LEN;
501            let tfs = self.tfs_flag.unwrap_or(false);
502            for cell in &self.cells {
503                len += 2; // cell_id
504                if tfs {
505                    len += 1 + cell.centre_frequencies.len() * 4;
506                } else {
507                    len += 4;
508                }
509                len += 1 + cell.subcells.len() * 5;
510            }
511        }
512        len
513    }
514    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
515        let len = self.serialized_len();
516        if buf.len() < len {
517            return Err(Error::OutputBufferTooSmall {
518                need: len,
519                have: buf.len(),
520            });
521        }
522        buf[0] = self.plp_id;
523        buf[1..3].copy_from_slice(&self.t2_system_id.to_be_bytes());
524        let mut p = T2_FIXED_PREFIX_LEN;
525        if let (Some(sm), Some(bw), Some(gi), Some(tm), Some(off), Some(tfs)) = (
526            self.siso_miso,
527            self.bandwidth,
528            self.guard_interval,
529            self.transmission_mode,
530            self.other_frequency_flag,
531            self.tfs_flag,
532        ) {
533            buf[p] = (sm.to_u8() << 6) | ((bw.to_u8() & 0x0F) << 2) | 0x03;
534            buf[p + 1] = (gi.to_u8() << 5)
535                | ((tm.to_u8() & 0x07) << 2)
536                | (u8::from(off) << 1)
537                | u8::from(tfs);
538            p += T2_FLAGS_BLOCK_LEN;
539            for cell in &self.cells {
540                buf[p..p + 2].copy_from_slice(&cell.cell_id.to_be_bytes());
541                p += 2;
542                if tfs {
543                    let freq_len = (cell.centre_frequencies.len() * 4) as u8;
544                    buf[p] = freq_len;
545                    p += 1;
546                    for &freq in &cell.centre_frequencies {
547                        buf[p..p + 4].copy_from_slice(&freq.to_be_bytes());
548                        p += 4;
549                    }
550                } else {
551                    let freq = cell.centre_frequencies.first().copied().unwrap_or(0);
552                    buf[p..p + 4].copy_from_slice(&freq.to_be_bytes());
553                    p += 4;
554                }
555                let subcell_len = (cell.subcells.len() * 5) as u8;
556                buf[p] = subcell_len;
557                p += 1;
558                for sc in &cell.subcells {
559                    buf[p] = sc.cell_id_extension;
560                    buf[p + 1..p + 5].copy_from_slice(&sc.transposer_frequency.to_be_bytes());
561                    p += 5;
562                }
563            }
564        }
565        Ok(len)
566    }
567}
568
569#[cfg(test)]
570mod tests {
571    use super::*;
572    use crate::descriptors::extension::test_support::*;
573    use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor};
574
575    #[test]
576    fn t2_bandwidth_roundtrip() {
577        for b in 0..=0xFFu8 {
578            assert_eq!(T2Bandwidth::from_u8(b).to_u8(), b);
579        }
580    }
581
582    #[test]
583    fn t2_guard_interval_roundtrip() {
584        for b in 0..=0xFFu8 {
585            assert_eq!(T2GuardInterval::from_u8(b).to_u8(), b);
586        }
587    }
588
589    #[test]
590    fn t2_transmission_mode_roundtrip() {
591        for b in 0..=0xFFu8 {
592            assert_eq!(T2TransmissionMode::from_u8(b).to_u8(), b);
593        }
594    }
595
596    #[test]
597    fn parse_t2_minimal() {
598        // body = plp + system_id = 3 bytes => no flags block
599        let sel = [0x07, 0x12, 0x34];
600        let bytes = wrap(0x04, &sel);
601        let d = ExtensionDescriptor::parse(&bytes).unwrap();
602        match &d.body {
603            ExtensionBody::T2DeliverySystem(b) => {
604                assert_eq!(b.plp_id, 0x07);
605                assert_eq!(b.t2_system_id, 0x1234);
606                assert_eq!(b.siso_miso, None);
607                assert!(b.cells.is_empty());
608            }
609            other => panic!("expected T2DeliverySystem, got {other:?}"),
610        }
611        round_trip(&d);
612    }
613
614    #[test]
615    fn parse_t2_structured_flags_and_cells() {
616        // prefix + flags block (siso=0, bw=4(10MHz), gi=6(19/256), tm=3(1k), off=0, tfs=1)
617        // + 2 cells: one empty, one with 3 freqs + 2 subcells
618        let b0: u8 = ((0x04 & 0x0F) << 2) | 0x03; // siso_miso=0, bandwidth=4, reserved=11
619        let b1: u8 = (0x06 << 5) | ((0x03 & 0x07) << 2) | (u8::from(false) << 1) | u8::from(true);
620        // cell 1: cell_id=0x1234, freq_len=0, subcell_len=0
621        let cell1 = [0x12, 0x34, 0x00, 0x00];
622        // cell 2: cell_id=0x5678, freq_len=12 (3 freqs), three freqs, subcell_len=10 (2 subcells), two subcells
623        let f1 = 0x01020304u32;
624        let f2 = 0x05060708u32;
625        let f3 = 0x090A0B0Cu32;
626        let sc1_id = 0x10u8;
627        let sc1_freq = 0x11121314u32;
628        let sc2_id = 0x20u8;
629        let sc2_freq = 0x21222324u32;
630        let mut cell2 = Vec::new();
631        cell2.extend_from_slice(&0x5678u16.to_be_bytes());
632        cell2.push(12);
633        cell2.extend_from_slice(&f1.to_be_bytes());
634        cell2.extend_from_slice(&f2.to_be_bytes());
635        cell2.extend_from_slice(&f3.to_be_bytes());
636        cell2.push(10);
637        cell2.push(sc1_id);
638        cell2.extend_from_slice(&sc1_freq.to_be_bytes());
639        cell2.push(sc2_id);
640        cell2.extend_from_slice(&sc2_freq.to_be_bytes());
641        let mut sel = vec![0x07, 0x12, 0x34, b0, b1];
642        sel.extend_from_slice(&cell1);
643        sel.extend_from_slice(&cell2);
644        let bytes = wrap(0x04, &sel);
645        let d = ExtensionDescriptor::parse(&bytes).unwrap();
646        match &d.body {
647            ExtensionBody::T2DeliverySystem(b) => {
648                assert_eq!(b.plp_id, 0x07);
649                assert_eq!(b.t2_system_id, 0x1234);
650                assert_eq!(b.siso_miso, Some(T2SisoMiso::Siso));
651                assert_eq!(b.bandwidth, Some(T2Bandwidth::Mhz10));
652                assert_eq!(b.guard_interval, Some(T2GuardInterval::G19_256));
653                assert_eq!(b.transmission_mode, Some(T2TransmissionMode::Mode1k));
654                assert_eq!(b.other_frequency_flag, Some(false));
655                assert_eq!(b.tfs_flag, Some(true));
656                assert_eq!(b.cells.len(), 2);
657                // cell 0: empty
658                assert_eq!(b.cells[0].cell_id, 0x1234);
659                assert!(b.cells[0].centre_frequencies.is_empty());
660                assert!(b.cells[0].subcells.is_empty());
661                // cell 1: 3 freqs + 2 subcells
662                assert_eq!(b.cells[1].cell_id, 0x5678);
663                assert_eq!(b.cells[1].centre_frequencies, vec![f1, f2, f3]);
664                assert_eq!(b.cells[1].subcells.len(), 2);
665                assert_eq!(b.cells[1].subcells[0].cell_id_extension, sc1_id);
666                assert_eq!(b.cells[1].subcells[0].transposer_frequency, sc1_freq);
667                assert_eq!(b.cells[1].subcells[1].cell_id_extension, sc2_id);
668                assert_eq!(b.cells[1].subcells[1].transposer_frequency, sc2_freq);
669
670                // Accessor tests
671                assert_eq!(
672                    b.cells[1].subcells[0].transposer_frequency_hz(),
673                    u64::from(sc1_freq) * 10
674                );
675                assert_eq!(
676                    b.cells[1].centre_frequencies_hz(),
677                    vec![u64::from(f1) * 10, u64::from(f2) * 10, u64::from(f3) * 10]
678                );
679            }
680            other => panic!("expected T2DeliverySystem, got {other:?}"),
681        }
682        round_trip(&d);
683    }
684
685    #[test]
686    fn tsduck_t2_reference() {
687        let bytes = from_hex(
688            "7f240456789a13cd12340000678a0c075bcd1505e30a780fd22c320a1217ea6406fa0aa9fc59",
689        );
690        let d = ExtensionDescriptor::parse(&bytes).unwrap();
691        match &d.body {
692            ExtensionBody::T2DeliverySystem(b) => {
693                assert_eq!(b.plp_id, 0x56);
694                assert_eq!(b.t2_system_id, 0x789A);
695                assert_eq!(b.siso_miso, Some(T2SisoMiso::Siso));
696                assert_eq!(b.bandwidth, Some(T2Bandwidth::Mhz10));
697                assert_eq!(b.guard_interval, Some(T2GuardInterval::G19_256));
698                assert_eq!(b.transmission_mode, Some(T2TransmissionMode::Mode1k));
699                assert_eq!(b.other_frequency_flag, Some(false));
700                assert_eq!(b.tfs_flag, Some(true));
701                assert_eq!(b.cells.len(), 2);
702
703                assert_eq!(b.cells[0].cell_id, 0x1234);
704                assert!(b.cells[0].centre_frequencies.is_empty());
705                assert!(b.cells[0].subcells.is_empty());
706
707                assert_eq!(b.cells[1].cell_id, 0x678A);
708                assert_eq!(
709                    b.cells[1].centre_frequencies,
710                    vec![0x075BCD15, 0x05E30A78, 0x0FD22C32]
711                );
712                assert_eq!(b.cells[1].subcells.len(), 2);
713                assert_eq!(b.cells[1].subcells[0].cell_id_extension, 0x12);
714                assert_eq!(b.cells[1].subcells[0].transposer_frequency, 0x17EA6406);
715                assert_eq!(b.cells[1].subcells[1].cell_id_extension, 0xFA);
716                assert_eq!(b.cells[1].subcells[1].transposer_frequency, 0x0AA9FC59);
717            }
718            other => panic!("expected T2DeliverySystem, got {other:?}"),
719        }
720        let mut out = vec![0u8; d.serialized_len()];
721        let n = d.serialize_into(&mut out).unwrap();
722        assert_eq!(
723            out[..n],
724            bytes[..],
725            "byte-exact re-serialize for tsduck T2 reference"
726        );
727    }
728}