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