dvb_si/descriptors/
ecm_repetition_rate.rs1use super::descriptor_body;
9use crate::error::{Error, Result};
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x78;
14const HEADER_LEN: usize = 2;
15const FIXED_LEN: usize = 4;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
21pub struct EcmRepetitionRateDescriptor<'a> {
22 pub ca_system_id: u16,
24 pub ecm_repetition_rate: u16,
26 pub private_data: &'a [u8],
28}
29
30impl<'a> Parse<'a> for EcmRepetitionRateDescriptor<'a> {
31 type Error = crate::error::Error;
32 fn parse(bytes: &'a [u8]) -> Result<Self> {
33 let body = descriptor_body(
34 bytes,
35 TAG,
36 "EcmRepetitionRateDescriptor",
37 "unexpected tag for ECM_repetition_rate_descriptor",
38 )?;
39 if body.len() < FIXED_LEN {
40 return Err(Error::InvalidDescriptor {
41 tag: TAG,
42 reason: "ECM_repetition_rate_descriptor body shorter than 4 bytes",
43 });
44 }
45 let ca_system_id = u16::from_be_bytes([body[0], body[1]]);
46 let ecm_repetition_rate = u16::from_be_bytes([body[2], body[3]]);
47 let private_data = &body[FIXED_LEN..];
48 Ok(Self {
49 ca_system_id,
50 ecm_repetition_rate,
51 private_data,
52 })
53 }
54}
55
56impl Serialize for EcmRepetitionRateDescriptor<'_> {
57 type Error = crate::error::Error;
58 fn serialized_len(&self) -> usize {
59 HEADER_LEN + FIXED_LEN + self.private_data.len()
60 }
61
62 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
63 if FIXED_LEN + self.private_data.len() > u8::MAX as usize {
64 return Err(Error::InvalidDescriptor {
65 tag: TAG,
66 reason: "ECM_repetition_rate_descriptor body exceeds 255 bytes",
67 });
68 }
69 let len = self.serialized_len();
70 if buf.len() < len {
71 return Err(Error::OutputBufferTooSmall {
72 need: len,
73 have: buf.len(),
74 });
75 }
76 buf[0] = TAG;
77 buf[1] = (FIXED_LEN + self.private_data.len()) as u8;
78 buf[2..4].copy_from_slice(&self.ca_system_id.to_be_bytes());
79 buf[4..6].copy_from_slice(&self.ecm_repetition_rate.to_be_bytes());
80 buf[HEADER_LEN + FIXED_LEN..len].copy_from_slice(self.private_data);
81 Ok(len)
82 }
83}
84impl<'a> crate::traits::DescriptorDef<'a> for EcmRepetitionRateDescriptor<'a> {
85 const TAG: u8 = TAG;
86 const NAME: &'static str = "ECM_REPETITION_RATE";
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn parse_no_private_data() {
95 let bytes = [TAG, 4, 0x06, 0x48, 0x01, 0xF4];
96 let d = EcmRepetitionRateDescriptor::parse(&bytes).unwrap();
97 assert_eq!(d.ca_system_id, 0x0648);
98 assert_eq!(d.ecm_repetition_rate, 500);
99 assert!(d.private_data.is_empty());
100 }
101
102 #[test]
103 fn parse_with_private_data() {
104 let bytes = [TAG, 6, 0x05, 0x00, 0x00, 0xC8, 0xAA, 0xBB];
105 let d = EcmRepetitionRateDescriptor::parse(&bytes).unwrap();
106 assert_eq!(d.ca_system_id, 0x0500);
107 assert_eq!(d.ecm_repetition_rate, 200);
108 assert_eq!(d.private_data, &[0xAA, 0xBB]);
109 }
110
111 #[test]
112 fn parse_rejects_wrong_tag() {
113 assert!(matches!(
114 EcmRepetitionRateDescriptor::parse(&[0x77, 4, 0, 0, 0, 0]).unwrap_err(),
115 Error::InvalidDescriptor { tag: 0x77, .. }
116 ));
117 }
118
119 #[test]
120 fn parse_rejects_body_too_short() {
121 let bytes = [TAG, 3, 0, 0, 0];
122 assert!(matches!(
123 EcmRepetitionRateDescriptor::parse(&bytes).unwrap_err(),
124 Error::InvalidDescriptor { .. }
125 ));
126 }
127
128 #[test]
129 fn parse_rejects_length_overrunning_buffer() {
130 let bytes = [TAG, 6, 0, 0, 0, 0];
131 assert!(matches!(
132 EcmRepetitionRateDescriptor::parse(&bytes).unwrap_err(),
133 Error::BufferTooShort { .. }
134 ));
135 }
136
137 #[test]
138 fn serialize_round_trip() {
139 let d = EcmRepetitionRateDescriptor {
140 ca_system_id: 0x0B00,
141 ecm_repetition_rate: 1000,
142 private_data: &[0x01, 0x02],
143 };
144 let mut buf = vec![0u8; d.serialized_len()];
145 d.serialize_into(&mut buf).unwrap();
146 assert_eq!(EcmRepetitionRateDescriptor::parse(&buf).unwrap(), d);
147 }
148
149 #[test]
150 fn serialize_rejects_too_small_buffer() {
151 let d = EcmRepetitionRateDescriptor {
152 ca_system_id: 0,
153 ecm_repetition_rate: 0,
154 private_data: &[],
155 };
156 let mut buf = vec![0u8; 3];
157 assert!(matches!(
158 d.serialize_into(&mut buf).unwrap_err(),
159 Error::OutputBufferTooSmall { .. }
160 ));
161 }
162
163 #[cfg(feature = "serde")]
164 #[test]
165 fn serde_serializes_to_stable_json() {
166 let d = EcmRepetitionRateDescriptor {
169 ca_system_id: 0x0648,
170 ecm_repetition_rate: 480,
171 private_data: &[0xFE],
172 };
173 let j = serde_json::to_string(&d).unwrap();
174 let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
177 assert!(j.contains("ca_system_id"));
178 }
179}