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