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