1use crate::error::{Error, Result};
7use crate::traits::Descriptor;
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x43;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 11;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum Polarization {
19 LinearHorizontal,
21 LinearVertical,
23 CircularLeft,
25 CircularRight,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32pub enum ModulationSystem {
33 DvbS,
35 DvbS2,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42pub enum ModulationType {
43 Auto,
45 Qpsk,
47 Psk8,
49 Qam16,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
56pub enum RollOff {
57 Alpha035,
59 Alpha025,
61 Alpha020,
63 Reserved,
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70pub struct SatelliteDeliverySystemDescriptor {
71 pub frequency_bcd: u32,
73 pub orbital_position_bcd: u16,
75 pub east: bool,
77 pub polarization: Polarization,
79 pub roll_off: RollOff,
83 pub modulation_system: ModulationSystem,
85 pub modulation_type: ModulationType,
87 pub symbol_rate_bcd: u32,
89 pub fec_inner: u8,
91}
92
93impl<'a> Parse<'a> for SatelliteDeliverySystemDescriptor {
94 type Error = crate::error::Error;
95 fn parse(bytes: &'a [u8]) -> Result<Self> {
96 if bytes.len() < HEADER_LEN {
97 return Err(Error::BufferTooShort {
98 need: HEADER_LEN,
99 have: bytes.len(),
100 what: "satellite delivery system descriptor header",
101 });
102 }
103
104 let tag = bytes[0];
105 if tag != TAG {
106 return Err(Error::InvalidDescriptor {
107 tag,
108 reason: "expected tag 0x43",
109 });
110 }
111
112 let length = bytes[1] as usize;
113 let total = HEADER_LEN + length;
114
115 if bytes.len() < total {
116 return Err(Error::BufferTooShort {
117 need: total,
118 have: bytes.len(),
119 what: "satellite delivery system descriptor body",
120 });
121 }
122
123 if length != BODY_LEN as usize {
124 return Err(Error::InvalidDescriptor {
125 tag: TAG,
126 reason: "descriptor_length must equal 11",
127 });
128 }
129
130 let body = &bytes[HEADER_LEN..total];
131
132 let frequency_bcd = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
134
135 let orbital_position_bcd = u16::from_be_bytes([body[4], body[5]]);
137
138 let flags = body[6];
141 let east = (flags & 0x80) != 0;
142
143 let pol_raw = (flags >> 5) & 0x03;
144 let polarization = match pol_raw {
145 0 => Polarization::LinearHorizontal,
146 1 => Polarization::LinearVertical,
147 2 => Polarization::CircularLeft,
148 _ => Polarization::CircularRight,
149 };
150
151 let roll_raw = (flags >> 3) & 0x03;
152 let roll_off = match roll_raw {
153 0 => RollOff::Alpha035,
154 1 => RollOff::Alpha025,
155 2 => RollOff::Alpha020,
156 _ => RollOff::Reserved,
157 };
158
159 let mod_sys_raw = (flags >> 2) & 0x01;
160 let modulation_system = match mod_sys_raw {
161 0 => ModulationSystem::DvbS,
162 _ => ModulationSystem::DvbS2,
163 };
164
165 let mod_type_raw = flags & 0x03;
166 let modulation_type = match mod_type_raw {
167 0 => ModulationType::Auto,
168 1 => ModulationType::Qpsk,
169 2 => ModulationType::Psk8,
170 _ => ModulationType::Qam16,
171 };
172
173 let symbol_rate_and_fec = u32::from_be_bytes([body[7], body[8], body[9], body[10]]);
175 let symbol_rate_bcd = symbol_rate_and_fec >> 4;
176 let fec_inner = (symbol_rate_and_fec & 0x0F) as u8;
177
178 Ok(SatelliteDeliverySystemDescriptor {
179 frequency_bcd,
180 orbital_position_bcd,
181 east,
182 polarization,
183 roll_off,
184 modulation_system,
185 modulation_type,
186 symbol_rate_bcd,
187 fec_inner,
188 })
189 }
190}
191
192impl Serialize for SatelliteDeliverySystemDescriptor {
193 type Error = crate::error::Error;
194 fn serialized_len(&self) -> usize {
195 HEADER_LEN + BODY_LEN as usize
196 }
197
198 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
199 let len = self.serialized_len();
200 if buf.len() < len {
201 return Err(Error::OutputBufferTooSmall {
202 need: len,
203 have: buf.len(),
204 });
205 }
206
207 buf[0] = TAG;
208 buf[1] = BODY_LEN;
209
210 let freq_bytes = self.frequency_bcd.to_be_bytes();
212 buf[2..6].copy_from_slice(&freq_bytes);
213
214 let orb_bytes = self.orbital_position_bcd.to_be_bytes();
216 buf[6..8].copy_from_slice(&orb_bytes);
217
218 let mut flags: u8 = 0;
220 if self.east {
221 flags |= 0x80;
222 }
223 flags |= match self.polarization {
224 Polarization::LinearHorizontal => 0x00,
225 Polarization::LinearVertical => 0x20,
226 Polarization::CircularLeft => 0x40,
227 Polarization::CircularRight => 0x60,
228 };
229 if self.modulation_system == ModulationSystem::DvbS2 {
232 flags |= match self.roll_off {
233 RollOff::Alpha035 => 0x00,
234 RollOff::Alpha025 => 0x08,
235 RollOff::Alpha020 => 0x10,
236 RollOff::Reserved => 0x18,
237 };
238 }
239 flags |= match self.modulation_system {
240 ModulationSystem::DvbS => 0x00,
241 ModulationSystem::DvbS2 => 0x04,
242 };
243 flags |= match self.modulation_type {
244 ModulationType::Auto => 0x00,
245 ModulationType::Qpsk => 0x01,
246 ModulationType::Psk8 => 0x02,
247 ModulationType::Qam16 => 0x03,
248 };
249 buf[8] = flags;
250
251 let sym_freq =
254 ((self.symbol_rate_bcd & 0x0FFF_FFFF) << 4) | (u32::from(self.fec_inner) & 0x0F);
255 let sym_bytes = sym_freq.to_be_bytes();
256 buf[9..13].copy_from_slice(&sym_bytes);
257
258 Ok(len)
259 }
260}
261
262impl<'a> Descriptor<'a> for SatelliteDeliverySystemDescriptor {
263 const TAG: u8 = TAG;
264
265 fn descriptor_length(&self) -> u8 {
266 BODY_LEN
267 }
268}
269
270#[cfg(test)]
271mod tests {
272 use super::*;
273
274 #[test]
277 fn parse_extracts_frequency_and_orbital_position() {
278 let raw: Vec<u8> = vec![
281 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00, ];
286 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
287 assert_eq!(desc.frequency_bcd, 0x11725000);
288 assert_eq!(desc.orbital_position_bcd, 0x1920);
289 }
290
291 #[test]
293 fn parse_extracts_west_east_flag() {
294 let raw_east: Vec<u8> = vec![
296 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
297 0x80, 0x02, 0x75, 0x00, 0x00,
299 ];
300 let desc_east = SatelliteDeliverySystemDescriptor::parse(&raw_east).unwrap();
301 assert!(desc_east.east, "east should be true when bit 7 is set");
302
303 let raw_west: Vec<u8> = vec![
305 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
307 ];
308 let desc_west = SatelliteDeliverySystemDescriptor::parse(&raw_west).unwrap();
309 assert!(!desc_west.east, "east should be false when bit 7 is clear");
310 }
311
312 #[test]
314 fn parse_extracts_polarization_variants() {
315 let pol_pairs: [(u8, Polarization); 4] = [
316 (0x00, Polarization::LinearHorizontal),
317 (0x20, Polarization::LinearVertical),
318 (0x40, Polarization::CircularLeft),
319 (0x60, Polarization::CircularRight),
320 ];
321
322 for (offset, expected_pol) in pol_pairs {
323 let raw: Vec<u8> = vec![
324 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
325 offset, 0x02, 0x75, 0x00, 0x00,
327 ];
328 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
329 assert_eq!(
330 desc.polarization, expected_pol,
331 "polarization mismatch for offset 0x{:02x}",
332 offset
333 );
334 }
335 }
336
337 #[test]
339 fn parse_extracts_modulation_system_and_type() {
340 let raw: Vec<u8> = vec![
342 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x01, 0x02, 0x75, 0x00, 0x00,
344 ];
345 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
346 assert_eq!(desc.modulation_system, ModulationSystem::DvbS);
347 assert_eq!(desc.modulation_type, ModulationType::Qpsk);
348
349 let raw2: Vec<u8> = vec![
351 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
352 0x06, 0x02, 0x75, 0x00, 0x00,
354 ];
355 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
356 assert_eq!(desc2.modulation_system, ModulationSystem::DvbS2);
357 assert_eq!(desc2.modulation_type, ModulationType::Psk8);
358 }
359
360 #[test]
362 fn parse_extracts_roll_off() {
363 let roll_pairs: [(u8, RollOff); 4] = [
364 (0x00, RollOff::Alpha035),
365 (0x08, RollOff::Alpha025),
366 (0x10, RollOff::Alpha020),
367 (0x18, RollOff::Reserved),
368 ];
369
370 for (offset, expected_roll) in roll_pairs {
371 let raw: Vec<u8> = vec![
372 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, offset, 0x02, 0x75, 0x00, 0x00,
374 ];
375 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
376 assert_eq!(desc.roll_off, expected_roll);
377 }
378 }
379
380 #[test]
383 fn parse_extracts_symbol_rate_and_fec() {
384 let raw: Vec<u8> = vec![
386 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
387 0x05, ];
389 let desc = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap();
390 assert_eq!(desc.symbol_rate_bcd, 0x0275000);
391 assert_eq!(desc.fec_inner, 5);
392
393 let raw2: Vec<u8> = vec![
395 TAG, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00,
396 0x0F, ];
398 let desc2 = SatelliteDeliverySystemDescriptor::parse(&raw2).unwrap();
399 assert_eq!(desc2.fec_inner, 0x0F);
400 }
401
402 #[test]
404 fn parse_rejects_wrong_tag() {
405 let raw: Vec<u8> = vec![
406 0x44, BODY_LEN, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20, 0x00, 0x02, 0x75, 0x00, 0x00,
408 ];
409 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
410 assert!(
411 matches!(err, Error::InvalidDescriptor { tag: 0x44, .. }),
412 "expected InvalidDescriptor(tag=0x44), got {err:?}"
413 );
414 }
415
416 #[test]
418 fn parse_rejects_wrong_length() {
419 let raw: Vec<u8> = vec![
420 TAG, 0x05, 0x11, 0x72, 0x50, 0x00, 0x19, 0x20,
422 ];
423 let err = SatelliteDeliverySystemDescriptor::parse(&raw).unwrap_err();
424 assert!(
425 matches!(
426 err,
427 Error::InvalidDescriptor {
428 reason: "descriptor_length must equal 11",
429 ..
430 }
431 ),
432 "expected InvalidDescriptor about length, got {err:?}"
433 );
434 }
435
436 #[test]
439 fn serialize_round_trip() {
440 let desc = SatelliteDeliverySystemDescriptor {
441 frequency_bcd: 0x11725000,
442 orbital_position_bcd: 0x1920,
443 east: true,
444 polarization: Polarization::CircularRight,
445 roll_off: RollOff::Alpha025,
446 modulation_system: ModulationSystem::DvbS2,
447 modulation_type: ModulationType::Psk8,
448 symbol_rate_bcd: 0x027500,
449 fec_inner: 5,
450 };
451
452 let mut buf = vec![0u8; desc.serialized_len()];
453 let written = desc.serialize_into(&mut buf).unwrap();
454 assert_eq!(written, desc.serialized_len());
455
456 let reparsed = SatelliteDeliverySystemDescriptor::parse(&buf).unwrap();
457 assert_eq!(desc, reparsed);
458 }
459}