Skip to main content

dvb_si/descriptors/
satellite_delivery_system.rs

1//! Satellite Delivery System Descriptor — ETSI EN 300 468 §6.2.13.2 (tag 0x43).
2//!
3//! Carried inside the NIT's `transport_stream_loop`'s second descriptor loop.
4//! Conveys carrier tuning parameters for a DVB-S / DVB-S2 transponder.
5
6use super::cable_delivery_system::FecInner;
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for satellite_delivery_system_descriptor.
12pub const TAG: u8 = 0x43;
13const HEADER_LEN: usize = 2;
14const BODY_LEN: u8 = 11;
15
16/// Polarization (§6.2.13.2 Table 38).
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize))]
19pub enum Polarization {
20    /// Linear horizontal.
21    LinearHorizontal,
22    /// Linear vertical.
23    LinearVertical,
24    /// Circular left.
25    CircularLeft,
26    /// Circular right.
27    CircularRight,
28}
29
30impl Polarization {
31    /// Human-readable spec label (ETSI EN 300 468 §6.2.13.2 Table 38).
32    #[must_use]
33    pub fn name(self) -> &'static str {
34        match self {
35            Self::LinearHorizontal => "linear horizontal",
36            Self::LinearVertical => "linear vertical",
37            Self::CircularLeft => "circular left",
38            Self::CircularRight => "circular right",
39        }
40    }
41
42    #[must_use]
43    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
44    pub fn from_u8(v: u8) -> Self {
45        match v & 0x03 {
46            0 => Polarization::LinearHorizontal,
47            1 => Polarization::LinearVertical,
48            2 => Polarization::CircularLeft,
49            _ => Polarization::CircularRight,
50        }
51    }
52
53    #[must_use]
54    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
55    pub fn to_u8(self) -> u8 {
56        match self {
57            Polarization::LinearHorizontal => 0,
58            Polarization::LinearVertical => 1,
59            Polarization::CircularLeft => 2,
60            Polarization::CircularRight => 3,
61        }
62    }
63}
64dvb_common::impl_spec_display!(Polarization);
65
66/// Modulation system (§6.2.13.2 Table 40: DVB-S or DVB-S2).
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize))]
69pub enum ModulationSystem {
70    /// DVB-S (first generation).
71    DvbS,
72    /// DVB-S2 (second generation).
73    DvbS2,
74}
75
76impl ModulationSystem {
77    /// Human-readable spec label (ETSI EN 300 468 §6.2.13.2 Table 40).
78    #[must_use]
79    pub fn name(self) -> &'static str {
80        match self {
81            Self::DvbS => "DVB-S",
82            Self::DvbS2 => "DVB-S2",
83        }
84    }
85}
86dvb_common::impl_spec_display!(ModulationSystem);
87
88/// Modulation type (§6.2.13.2 Table 41).
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90#[cfg_attr(feature = "serde", derive(serde::Serialize))]
91pub enum ModulationType {
92    /// Auto-detect.
93    Auto,
94    /// QPSK.
95    Qpsk,
96    /// 8PSK.
97    Psk8,
98    /// 16QAM.
99    Qam16,
100}
101
102impl ModulationType {
103    /// Human-readable spec label (ETSI EN 300 468 §6.2.13.2 Table 41).
104    #[must_use]
105    pub fn name(self) -> &'static str {
106        match self {
107            Self::Auto => "Auto",
108            Self::Qpsk => "QPSK",
109            Self::Psk8 => "8PSK",
110            Self::Qam16 => "16-QAM",
111        }
112    }
113}
114dvb_common::impl_spec_display!(ModulationType);
115
116/// Roll-off factor (§6.2.13.2 Table 39, DVB-S2 only; also used by S2X
117/// with extended values per Table 144).
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize))]
120#[non_exhaustive]
121pub enum RollOff {
122    /// 0.35 (DVB-S default).
123    Alpha035,
124    /// 0.25 (DVB-S2 common).
125    Alpha025,
126    /// 0.20 (DVB-S2 narrow).
127    Alpha020,
128    /// Reserved — carries the raw value for forward compatibility (2-bit for
129    /// DVB-S/S2, 3-bit for S2X).
130    Reserved(u8),
131}
132
133impl RollOff {
134    #[must_use]
135    /// Construct from a raw `u8`; every value maps to a variant (total, lossless).
136    pub fn from_u8(v: u8) -> Self {
137        match v {
138            0 => RollOff::Alpha035,
139            1 => RollOff::Alpha025,
140            2 => RollOff::Alpha020,
141            other => RollOff::Reserved(other),
142        }
143    }
144
145    #[must_use]
146    /// Inverse of `from_u8`; `Self::Reserved` emits its stored value.
147    pub fn to_u8(self) -> u8 {
148        match self {
149            RollOff::Alpha035 => 0,
150            RollOff::Alpha025 => 1,
151            RollOff::Alpha020 => 2,
152            RollOff::Reserved(v) => v,
153        }
154    }
155
156    /// Human-readable spec label (ETSI EN 300 468 §6.2.13.2 Table 39).
157    #[must_use]
158    pub fn name(self) -> &'static str {
159        match self {
160            Self::Alpha035 => "α=0.35",
161            Self::Alpha025 => "α=0.25",
162            Self::Alpha020 => "α=0.20",
163            Self::Reserved(_) => "reserved",
164        }
165    }
166}
167dvb_common::impl_spec_display!(RollOff, Reserved);
168
169/// Satellite Delivery System Descriptor.
170#[derive(Debug, Clone, PartialEq, Eq)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize))]
172pub struct SatelliteDeliverySystemDescriptor {
173    /// 32-bit BCD frequency in GHz (e.g. `0x01172500` = 11.72500 GHz = 11_725_000_000 Hz).
174    pub frequency_bcd: u32,
175    /// 16-bit BCD orbital position tenths of a degree (e.g. 0x1920 = 192.0°).
176    pub orbital_position_bcd: u16,
177    /// False = west, true = east.
178    pub east: bool,
179    /// Polarization.
180    pub polarization: Polarization,
181    /// DVB-S2 roll-off factor. Meaningful only when `modulation_system` is
182    /// DVB-S2 (Table 37); for DVB-S the bits are reserved_zero_future_use and
183    /// serialize emits them as 0 regardless of this field.
184    pub roll_off: RollOff,
185    /// Modulation system.
186    pub modulation_system: ModulationSystem,
187    /// Modulation type.
188    pub modulation_type: ModulationType,
189    /// 28-bit BCD symbol rate in Msym/s (e.g. 0x0275_000 = 27.500 Msym/s).
190    pub symbol_rate_bcd: u32,
191    /// 4-bit FEC inner code — ETSI EN 300 468 Table 36.
192    pub fec_inner: FecInner,
193}
194
195impl SatelliteDeliverySystemDescriptor {
196    /// Decode the 32-bit BCD `frequency` to Hz (10 kHz field resolution,
197    /// EN 300 468 §6.2.13.2). `None` if the BCD nibbles are out of range.
198    ///
199    /// e.g. `0x0117_2500` → `11_725_000_000` Hz (11.72500 GHz).
200    #[must_use]
201    pub fn frequency_hz(&self) -> Option<u64> {
202        dvb_common::bcd::bcd_to_decimal(u64::from(self.frequency_bcd), 8).map(|v| v * 10_000)
203    }
204
205    /// Set `frequency` from Hz, encoding to the 8-digit BCD field at the field's
206    /// 10 kHz resolution (sub-10 kHz precision is truncated).
207    ///
208    /// # Errors
209    /// [`ValueOutOfRange`](crate::Error::ValueOutOfRange) if the value
210    /// exceeds the 8-digit BCD field.
211    pub fn set_frequency_hz(&mut self, hz: u64) -> crate::Result<()> {
212        self.frequency_bcd = super::encode_bcd_field(
213            hz / 10_000,
214            8,
215            "SatelliteDeliverySystemDescriptor::frequency",
216        )? as u32;
217        Ok(())
218    }
219
220    /// Decode the 28-bit BCD `symbol_rate` to symbols/second (100 sym/s
221    /// resolution). `None` if the BCD nibbles are out of range.
222    ///
223    /// e.g. `0x027_5000` → `27_500_000` (27.5 Msym/s).
224    #[must_use]
225    pub fn symbol_rate_sps(&self) -> Option<u64> {
226        dvb_common::bcd::bcd_to_decimal(u64::from(self.symbol_rate_bcd), 7).map(|v| v * 100)
227    }
228
229    /// Set `symbol_rate` from symbols/second (100 sym/s field resolution).
230    ///
231    /// # Errors
232    /// [`ValueOutOfRange`](crate::Error::ValueOutOfRange) on overflow of
233    /// the 7-digit BCD field.
234    pub fn set_symbol_rate_sps(&mut self, sps: u64) -> crate::Result<()> {
235        self.symbol_rate_bcd = super::encode_bcd_field(
236            sps / 100,
237            7,
238            "SatelliteDeliverySystemDescriptor::symbol_rate",
239        )? as u32;
240        Ok(())
241    }
242
243    /// Decode the 16-bit BCD `orbital_position` to degrees (tenths resolution).
244    /// `None` if the BCD nibbles are out of range. e.g. `0x1920` → `192.0`.
245    #[must_use]
246    pub fn orbital_position_deg(&self) -> Option<f64> {
247        dvb_common::bcd::bcd_to_decimal(u64::from(self.orbital_position_bcd), 4)
248            .map(|tenths| tenths as f64 / 10.0)
249    }
250
251    /// Set `orbital_position` in degrees, rounded to the field's tenth-degree
252    /// resolution. The east/west `east` flag is a separate field.
253    ///
254    /// # Errors
255    /// [`ValueOutOfRange`](crate::Error::ValueOutOfRange) if negative or
256    /// beyond the 4-digit BCD field.
257    pub fn set_orbital_position_deg(&mut self, deg: f64) -> crate::Result<()> {
258        if !(0.0..=6_553.5).contains(&deg) {
259            return Err(crate::Error::ValueOutOfRange {
260                field: "SatelliteDeliverySystemDescriptor::orbital_position",
261                reason: "degrees must be in 0.0..=6553.5",
262            });
263        }
264        let tenths = (deg * 10.0).round() as u64;
265        self.orbital_position_bcd = super::encode_bcd_field(
266            tenths,
267            4,
268            "SatelliteDeliverySystemDescriptor::orbital_position",
269        )? as u16;
270        Ok(())
271    }
272}
273
274impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
275    type Error = crate::error::Error;
276    fn parse(bytes: &'a [u8]) -> Result<Self> {
277        let body = descriptor_body(
278            bytes,
279            TAG,
280            "SatelliteDeliverySystemDescriptor",
281            "expected tag 0x43",
282        )?;
283
284        if body.len() != BODY_LEN as usize {
285            return Err(Error::InvalidDescriptor {
286                tag: TAG,
287                reason: "descriptor_length must equal 11",
288            });
289        }
290
291        // Frequency: 4 bytes BCD (GHz.MMMM)
292        let frequency_bcd = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
293
294        // Orbital position: 2 bytes BCD (tenths of a degree)
295        let orbital_position_bcd = u16::from_be_bytes([body[4], body[5]]);
296
297        // Flags byte 6: bit 7 = west_east_flag, bits 5-6 = polarization,
298        // bits 3-4 = roll_off, bit 2 = modulation_system, bits 1-0 = modulation_type
299        let flags = body[6];
300        let east = (flags & 0x80) != 0;
301
302        let pol_raw = (flags >> 5) & 0x03;
303        let polarization = match pol_raw {
304            0 => Polarization::LinearHorizontal,
305            1 => Polarization::LinearVertical,
306            2 => Polarization::CircularLeft,
307            _ => Polarization::CircularRight,
308        };
309
310        let roll_raw = (flags >> 3) & 0x03;
311        let roll_off = match roll_raw {
312            0 => RollOff::Alpha035,
313            1 => RollOff::Alpha025,
314            2 => RollOff::Alpha020,
315            v => RollOff::Reserved(v),
316        };
317
318        let mod_sys_raw = (flags >> 2) & 0x01;
319        let modulation_system = match mod_sys_raw {
320            0 => ModulationSystem::DvbS,
321            _ => ModulationSystem::DvbS2,
322        };
323
324        let mod_type_raw = flags & 0x03;
325        let modulation_type = match mod_type_raw {
326            0 => ModulationType::Auto,
327            1 => ModulationType::Qpsk,
328            2 => ModulationType::Psk8,
329            _ => ModulationType::Qam16,
330        };
331
332        // Symbol rate: 28-bit BCD packed into 4 bytes (3.5 bytes + 4-bit FEC)
333        let symbol_rate_and_fec = u32::from_be_bytes([body[7], body[8], body[9], body[10]]);
334        let symbol_rate_bcd = symbol_rate_and_fec >> 4;
335        let fec_inner = FecInner::from_u8((symbol_rate_and_fec & 0x0F) as u8);
336
337        Ok(SatelliteDeliverySystemDescriptor {
338            frequency_bcd,
339            orbital_position_bcd,
340            east,
341            polarization,
342            roll_off,
343            modulation_system,
344            modulation_type,
345            symbol_rate_bcd,
346            fec_inner,
347        })
348    }
349}
350
351impl Serialize for SatelliteDeliverySystemDescriptor {
352    type Error = crate::error::Error;
353    fn serialized_len(&self) -> usize {
354        HEADER_LEN + BODY_LEN as usize
355    }
356
357    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
358        let len = self.serialized_len();
359        if buf.len() < len {
360            return Err(Error::OutputBufferTooSmall {
361                need: len,
362                have: buf.len(),
363            });
364        }
365
366        buf[0] = TAG;
367        buf[1] = BODY_LEN;
368
369        // Frequency: 4 bytes BCD
370        let freq_bytes = self.frequency_bcd.to_be_bytes();
371        buf[2..6].copy_from_slice(&freq_bytes);
372
373        // Orbital position: 2 bytes BCD
374        let orb_bytes = self.orbital_position_bcd.to_be_bytes();
375        buf[6..8].copy_from_slice(&orb_bytes);
376
377        // Flags byte: combine west_east, polarization, roll_off, modulation_system, modulation_type
378        let mut flags: u8 = 0;
379        if self.east {
380            flags |= 0x80;
381        }
382        flags |= match self.polarization {
383            Polarization::LinearHorizontal => 0x00,
384            Polarization::LinearVertical => 0x20,
385            Polarization::CircularLeft => 0x40,
386            Polarization::CircularRight => 0x60,
387        };
388        // Table 37: roll_off exists only when modulation_system == DVB-S2;
389        // for DVB-S those 2 bits are reserved_zero_future_use and SHALL be 0.
390        if self.modulation_system == ModulationSystem::DvbS2 {
391            flags |= match self.roll_off {
392                RollOff::Alpha035 => 0x00,
393                RollOff::Alpha025 => 0x08,
394                RollOff::Alpha020 => 0x10,
395                RollOff::Reserved(v) => (v & 0x03) << 3,
396            };
397        }
398        flags |= match self.modulation_system {
399            ModulationSystem::DvbS => 0x00,
400            ModulationSystem::DvbS2 => 0x04,
401        };
402        flags |= match self.modulation_type {
403            ModulationType::Auto => 0x00,
404            ModulationType::Qpsk => 0x01,
405            ModulationType::Psk8 => 0x02,
406            ModulationType::Qam16 => 0x03,
407        };
408        buf[8] = flags;
409
410        // Symbol rate + FEC_inner: 28-bit BCD shifted left 4 bits, then OR with FEC.
411        // Mask to 28 bits so an over-range value can't spill past the field.
412        let sym_freq = ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4)
413            | (u32::from(self.fec_inner.to_u8()) & 0x0F);
414        let sym_bytes = sym_freq.to_be_bytes();
415        buf[9..13].copy_from_slice(&sym_bytes);
416
417        Ok(len)
418    }
419}
420impl<'a> crate::traits::DescriptorDef<'a> for SatelliteDeliverySystemDescriptor {
421    const TAG: u8 = TAG;
422    const NAME: &'static str = "SATELLITE_DELIVERY_SYSTEM";
423}
424
425#[cfg(test)]
426mod tests {
427    use super::*;
428
429    /// Build a valid 13-byte descriptor (2 header + 11 body) and confirm
430    /// parse extracts frequency and orbital position correctly.
431    #[test]
432    fn parse_extracts_frequency_and_orbital_position() {
433        // frequency: 11.72500 GHz → BCD 0x01 0x17 0x25 0x00 (§6.2.13.2)
434        // orbital: 192.0° → BCD 0x19 0x20
435        let raw: Vec<u8> = vec![
436            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, // frequency
437            0x19, 0x20, // orbital position
438            0x00, // flags (all defaults: west, linear-h, alpha-035, DVB-S, auto)
439            0x02, 0x75, 0x00, 0x00, // symbol rate 27.500 Msym/s, FEC 0
440        ];
441        let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
442        assert_eq!(desc.frequency_bcd, 0x01172500);
443        assert_eq!(desc.orbital_position_bcd, 0x1920);
444    }
445
446    /// Flags byte bit 7 encodes west/east direction.
447    #[test]
448    fn parse_extracts_west_east_flag() {
449        // East: bit 7 = 1
450        let raw_east: Vec<u8> = vec![
451            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
452            0x80, // east flag set, everything else zero
453            0x02, 0x75, 0x00, 0x00,
454        ];
455        let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
456        assert!(desc_east.east, "east should be true when bit 7 is set");
457
458        // West: bit 7 = 0
459        let raw_west: Vec<u8> = vec![
460            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, // east flag clear
461            0x02, 0x75, 0x00, 0x00,
462        ];
463        let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
464        assert!(!desc_west.east, "east should be false when bit 7 is clear");
465    }
466
467    /// All four polarization values are extracted correctly from bits 5-6.
468    #[test]
469    fn parse_extracts_polarization_variants() {
470        let pol_pairs: [(u8, Polarization); 4] = [
471            (0x00, Polarization::LinearHorizontal),
472            (0x20, Polarization::LinearVertical),
473            (0x40, Polarization::CircularLeft),
474            (0x60, Polarization::CircularRight),
475        ];
476
477        for (offset, expected_pol) in pol_pairs {
478            let raw: Vec<u8> = vec![
479                TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
480                offset, // polarization bits
481                0x02, 0x75, 0x00, 0x00,
482            ];
483            let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
484            assert_eq!(
485                desc.polarization, expected_pol,
486                "polarization mismatch for offset 0x{:02x}",
487                offset
488            );
489        }
490    }
491
492    /// Modulation system (bit 2) and modulation type (bits 1-0) are extracted.
493    #[test]
494    fn parse_extracts_modulation_system_and_type() {
495        // DVB-S (bit 2 = 0), QPSK (bits 1-0 = 01)
496        let raw: Vec<u8> = vec![
497            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x01, // DVB-S, QPSK
498            0x02, 0x75, 0x00, 0x00,
499        ];
500        let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
501        assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
502        assert_eq!(desc.modulation_type, ModulationType::Qpsk);
503
504        // DVB-S2 (bit 2 = 1), 8PSK (bits 1-0 = 10)
505        let raw2: Vec<u8> = vec![
506            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20,
507            0x06, // DVB-S2 (0x04) + 8PSK (0x02)
508            0x02, 0x75, 0x00, 0x00,
509        ];
510        let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
511        assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
512        assert_eq!(desc2.modulation_type, ModulationType::Psk8);
513    }
514
515    /// Roll-off codes (bits 3-4) are extracted correctly.
516    #[test]
517    fn parse_extracts_roll_off() {
518        let roll_pairs: [(u8, RollOff); 4] = [
519            (0x00, RollOff::Alpha035),
520            (0x08, RollOff::Alpha025),
521            (0x10, RollOff::Alpha020),
522            (0x18, RollOff::Reserved(3)),
523        ];
524
525        for (offset, expected_roll) in roll_pairs {
526            let raw: Vec<u8> = vec![
527                TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, offset, // roll-off bits
528                0x02, 0x75, 0x00, 0x00,
529            ];
530            let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
531            assert_eq!(desc.roll_off, expected_roll);
532        }
533    }
534
535    /// Symbol rate (28-bit BCD) and FEC inner (4 bits) are extracted from
536    /// the last 4 bytes.
537    #[test]
538    fn parse_extracts_symbol_rate_and_fec() {
539        // symbol_rate: 27.500 Msym/s → BCD 0x027500, FEC: 5/6 → 0x4
540        let raw: Vec<u8> = vec![
541            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
542            0x04, // symbol_rate_bcd = 0x027500, fec_inner = 4 (Rate5_6)
543        ];
544        let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
545        assert_eq!(desc.symbol_rate_bcd, 0x0275000);
546        assert_eq!(desc.fec_inner, FecInner::Rate5_6);
547
548        // FEC = 0x0F (NoConvCoding)
549        let raw2: Vec<u8> = vec![
550            TAG, BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
551            0x0F, // FEC = 0x0F
552        ];
553        let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
554        assert_eq!(desc2.fec_inner, FecInner::NoConvCoding);
555    }
556
557    /// Wrong tag byte should return InvalidDescriptor.
558    #[test]
559    fn parse_rejects_wrong_tag() {
560        let raw: Vec<u8> = vec![
561            0x44, // wrong tag (cable delivery system)
562            BODY_LEN, 0x01, 0x17, 0x25, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
563        ];
564        let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
565        assert!(
566            matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
567            "expected InvalidDescriptor(tag=0x44), got {err:?}"
568        );
569    }
570
571    /// Body length must be exactly 11. Wrong length returns InvalidDescriptor.
572    #[test]
573    fn parse_rejects_wrong_length() {
574        let raw: Vec<u8> = vec![
575            TAG, 0x05, // wrong length (should be 11)
576            0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
577        ];
578        let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
579        assert!(
580            matches!(
581                err,
582                Error::InvalidDescriptor {
583                    reason: "descriptor_length must equal 11",
584                    ..
585                }
586            ),
587            "expected InvalidDescriptor about length, got {err:?}"
588        );
589    }
590
591    /// Parse → serialize → re-parse should yield an equal struct and
592    /// identical bytes.
593    #[test]
594    fn serialize_round_trip() {
595        let desc = SatelliteDeliverySystemDescriptor {
596            frequency_bcd: 0x01172500,
597            orbital_position_bcd: 0x1920,
598            east: true,
599            polarization: Polarization::CircularRight,
600            roll_off: RollOff::Alpha025,
601            modulation_system: ModulationSystem::DvbS2,
602            modulation_type: ModulationType::Psk8,
603            symbol_rate_bcd: 0x027500,
604            fec_inner: FecInner::Rate5_6,
605        };
606
607        let mut buf = vec![0u8; desc.serialized_len()];
608        let written = desc.serialize_into(&mut buf).unwrap();
609        assert_eq!(written, desc.serialized_len());
610
611        let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
612        assert_eq!(desc, reparsed);
613    }
614
615    /// Reserved roll-off value round-trips with its raw 2-bit value preserved.
616    #[test]
617    fn reserved_roll_off_round_trips() {
618        let desc = SatelliteDeliverySystemDescriptor {
619            frequency_bcd: 0x01172500,
620            orbital_position_bcd: 0x1920,
621            east: true,
622            polarization: Polarization::CircularRight,
623            roll_off: RollOff::Reserved(3),
624            modulation_system: ModulationSystem::DvbS2,
625            modulation_type: ModulationType::Psk8,
626            symbol_rate_bcd: 0x027500,
627            fec_inner: FecInner::Rate5_6,
628        };
629
630        let mut buf = vec![0u8; desc.serialized_len()];
631        desc.serialize_into(&mut buf).unwrap();
632        assert_eq!(buf[8] & 0x18, 0x18); // roll_off bits = 0b11
633
634        let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
635        assert_eq!(reparsed.roll_off, RollOff::Reserved(3));
636    }
637
638    #[test]
639    fn frequency_hz_round_trip() {
640        let mut desc = SatelliteDeliverySystemDescriptor {
641            frequency_bcd: 0,
642            orbital_position_bcd: 0x1920,
643            east: true,
644            polarization: Polarization::LinearHorizontal,
645            roll_off: RollOff::Alpha035,
646            modulation_system: ModulationSystem::DvbS,
647            modulation_type: ModulationType::Auto,
648            symbol_rate_bcd: 0x027500,
649            fec_inner: FecInner::Rate5_6,
650        };
651        desc.set_frequency_hz(11_725_000_000).unwrap();
652        assert_eq!(desc.frequency_hz(), Some(11_725_000_000));
653        assert_eq!(desc.frequency_bcd, 0x01172500);
654    }
655}