Skip to main content

dvb_si/descriptors/
fmc.rs

1//! FMC Descriptor — ISO/IEC 13818-1 §2.6.44, Table 2-77 (tag 0x1F).
2//!
3//! A list of (ES_ID, FlexMuxChannel) pairs — 3 bytes each, consuming the
4//! entire descriptor body.
5
6use super::descriptor_body;
7use crate::error::{Error, Result};
8use alloc::vec::Vec;
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for FMC_descriptor.
12pub const TAG: u8 = 0x1F;
13const HEADER_LEN: usize = 2;
14
15/// FMC Descriptor.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18pub struct FmcDescriptor {
19    /// List of (ES_ID, FlexMuxChannel) pairs.
20    pub entries: Vec<(u16, u8)>,
21}
22
23impl<'a> Parse<'a> for FmcDescriptor {
24    type Error = crate::error::Error;
25
26    fn parse(bytes: &'a [u8]) -> Result<Self> {
27        let body = descriptor_body(
28            bytes,
29            TAG,
30            "FmcDescriptor",
31            "unexpected tag for FMC_descriptor",
32        )?;
33        if body.len() % 3 != 0 {
34            return Err(Error::InvalidDescriptor {
35                tag: TAG,
36                reason: "FMC_descriptor body length must be a multiple of 3",
37            });
38        }
39        let entries = body
40            .chunks_exact(3)
41            .map(|chunk| {
42                let es_id = u16::from_be_bytes([chunk[0], chunk[1]]);
43                (es_id, chunk[2])
44            })
45            .collect();
46        Ok(Self { entries })
47    }
48}
49
50impl Serialize for FmcDescriptor {
51    type Error = crate::error::Error;
52
53    fn serialized_len(&self) -> usize {
54        HEADER_LEN + self.entries.len() * 3
55    }
56
57    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
58        let len = self.serialized_len();
59        if buf.len() < len {
60            return Err(Error::OutputBufferTooSmall {
61                need: len,
62                have: buf.len(),
63            });
64        }
65        buf[0] = TAG;
66        buf[1] = (len - HEADER_LEN) as u8;
67        for (i, &(es_id, fmc)) in self.entries.iter().enumerate() {
68            let off = HEADER_LEN + i * 3;
69            buf[off..off + 2].copy_from_slice(&es_id.to_be_bytes());
70            buf[off + 2] = fmc;
71        }
72        Ok(len)
73    }
74}
75impl<'a> crate::traits::DescriptorDef<'a> for FmcDescriptor {
76    const TAG: u8 = TAG;
77    const NAME: &'static str = "FMC";
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn parse_empty() {
86        let d = FmcDescriptor::parse(&[TAG, 0]).unwrap();
87        assert!(d.entries.is_empty());
88    }
89
90    #[test]
91    fn parse_single_entry() {
92        let bytes = [TAG, 3, 0x00, 0x01, 0x42];
93        let d = FmcDescriptor::parse(&bytes).unwrap();
94        assert_eq!(d.entries, vec![(0x0001, 0x42)]);
95    }
96
97    #[test]
98    fn parse_multiple_entries() {
99        let bytes = [TAG, 6, 0x00, 0x0A, 0x01, 0x00, 0x0B, 0x02];
100        let d = FmcDescriptor::parse(&bytes).unwrap();
101        assert_eq!(d.entries, vec![(0x000A, 0x01), (0x000B, 0x02)]);
102    }
103
104    #[test]
105    fn parse_rejects_unaligned_length() {
106        let err = FmcDescriptor::parse(&[TAG, 1, 0x00]).unwrap_err();
107        assert!(matches!(err, Error::InvalidDescriptor { .. }));
108    }
109
110    #[test]
111    fn parse_rejects_wrong_tag() {
112        let err = FmcDescriptor::parse(&[0x02, 0, 0]).unwrap_err();
113        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x02, .. }));
114    }
115
116    #[test]
117    fn serialize_round_trip() {
118        let d = FmcDescriptor {
119            entries: vec![(0xDEAD, 0x42), (0xBEEF, 0x99)],
120        };
121        let mut buf = vec![0u8; d.serialized_len()];
122        d.serialize_into(&mut buf).unwrap();
123        let reparsed = FmcDescriptor::parse(&buf).unwrap();
124        assert_eq!(d, reparsed);
125    }
126
127    #[test]
128    fn serialize_rejects_small_buffer() {
129        let d = FmcDescriptor {
130            entries: vec![(0, 0)],
131        };
132        let mut tiny = vec![0u8; 3];
133        let err = d.serialize_into(&mut tiny).unwrap_err();
134        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
135    }
136}