dvb_si/descriptors/
service_availability.rs1use crate::error::{Error, Result};
10use crate::traits::Descriptor;
11use dvb_common::{Parse, Serialize};
12
13pub const TAG: u8 = 0x72;
15pub const HEADER_LEN: usize = 2;
17pub const FLAGS_LEN: usize = 1;
19pub const CELL_ID_LEN: usize = 2;
21
22const AVAILABILITY_FLAG_MASK: u8 = 0b1000_0000;
23const RESERVED_MASK: u8 = 0b0111_1111;
24
25#[derive(Debug, Clone, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct ServiceAvailabilityDescriptor {
29 pub availability_flag: bool,
31 pub cell_ids: Vec<u16>,
33}
34
35impl<'a> Parse<'a> for ServiceAvailabilityDescriptor {
36 type Error = crate::error::Error;
37 fn parse(bytes: &'a [u8]) -> Result<Self> {
38 if bytes.len() < HEADER_LEN {
39 return Err(Error::BufferTooShort {
40 need: HEADER_LEN,
41 have: bytes.len(),
42 what: "ServiceAvailabilityDescriptor header",
43 });
44 }
45 if bytes[0] != TAG {
46 return Err(Error::InvalidDescriptor {
47 tag: bytes[0],
48 reason: "unexpected tag for service_availability_descriptor",
49 });
50 }
51 let length = bytes[1] as usize;
52 if length < FLAGS_LEN {
53 return Err(Error::InvalidDescriptor {
54 tag: TAG,
55 reason: "service_availability_descriptor body too short (need flags byte)",
56 });
57 }
58 if (length - FLAGS_LEN) % CELL_ID_LEN != 0 {
59 return Err(Error::InvalidDescriptor {
60 tag: TAG,
61 reason: "service_availability cell_id loop must be a multiple of 2 bytes",
62 });
63 }
64 let end = HEADER_LEN + length;
65 if bytes.len() < end {
66 return Err(Error::BufferTooShort {
67 need: end,
68 have: bytes.len(),
69 what: "ServiceAvailabilityDescriptor body",
70 });
71 }
72 let flags = bytes[HEADER_LEN];
73 let availability_flag = flags & AVAILABILITY_FLAG_MASK != 0;
75 let count = (length - FLAGS_LEN) / CELL_ID_LEN;
76 let mut cell_ids = Vec::with_capacity(count);
77 let mut pos = HEADER_LEN + FLAGS_LEN;
78 for _ in 0..count {
79 cell_ids.push(u16::from_be_bytes([bytes[pos], bytes[pos + 1]]));
80 pos += CELL_ID_LEN;
81 }
82 Ok(Self {
83 availability_flag,
84 cell_ids,
85 })
86 }
87}
88
89impl Serialize for ServiceAvailabilityDescriptor {
90 type Error = crate::error::Error;
91 fn serialized_len(&self) -> usize {
92 HEADER_LEN + FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN
93 }
94
95 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
96 let body_len = FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN;
97 if body_len > u8::MAX as usize {
98 return Err(Error::InvalidDescriptor {
99 tag: TAG,
100 reason: "service_availability_descriptor body exceeds 255 bytes",
101 });
102 }
103 let len = self.serialized_len();
104 if buf.len() < len {
105 return Err(Error::OutputBufferTooSmall {
106 need: len,
107 have: buf.len(),
108 });
109 }
110 buf[0] = TAG;
111 buf[1] = body_len as u8;
112 let mut flags = RESERVED_MASK;
114 if self.availability_flag {
115 flags |= AVAILABILITY_FLAG_MASK;
116 }
117 buf[HEADER_LEN] = flags;
118 let mut pos = HEADER_LEN + FLAGS_LEN;
119 for cid in &self.cell_ids {
120 buf[pos..pos + CELL_ID_LEN].copy_from_slice(&cid.to_be_bytes());
121 pos += CELL_ID_LEN;
122 }
123 Ok(len)
124 }
125}
126
127impl<'a> Descriptor<'a> for ServiceAvailabilityDescriptor {
128 const TAG: u8 = TAG;
129 fn descriptor_length(&self) -> u8 {
130 (FLAGS_LEN + self.cell_ids.len() * CELL_ID_LEN) as u8
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 #[test]
139 fn parse_extracts_flag_and_cells() {
140 let bytes = [TAG, 5, 0x80, 0x00, 0x01, 0x00, 0x02];
142 let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
143 assert!(d.availability_flag);
144 assert_eq!(d.cell_ids, vec![0x0001, 0x0002]);
145 }
146
147 #[test]
148 fn parse_flag_false() {
149 let bytes = [TAG, 1, 0x00];
150 let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
151 assert!(!d.availability_flag);
152 assert!(d.cell_ids.is_empty());
153 }
154
155 #[test]
156 fn parse_ignores_reserved_bits() {
157 let bytes = [TAG, 1, 0x7F];
159 let d = ServiceAvailabilityDescriptor::parse(&bytes).unwrap();
160 assert!(!d.availability_flag);
161 }
162
163 #[test]
164 fn parse_rejects_wrong_tag() {
165 assert!(matches!(
166 ServiceAvailabilityDescriptor::parse(&[0x73, 1, 0]).unwrap_err(),
167 Error::InvalidDescriptor { tag: 0x73, .. }
168 ));
169 }
170
171 #[test]
172 fn parse_rejects_zero_length_body() {
173 assert!(matches!(
174 ServiceAvailabilityDescriptor::parse(&[TAG, 0]).unwrap_err(),
175 Error::InvalidDescriptor { tag: TAG, .. }
176 ));
177 }
178
179 #[test]
180 fn parse_rejects_odd_cell_loop() {
181 let bytes = [TAG, 2, 0x80, 0x00];
183 assert!(matches!(
184 ServiceAvailabilityDescriptor::parse(&bytes).unwrap_err(),
185 Error::InvalidDescriptor { tag: TAG, .. }
186 ));
187 }
188
189 #[test]
190 fn parse_rejects_truncated_buffer() {
191 let bytes = [TAG, 5, 0x80, 0x00, 0x01];
192 assert!(matches!(
193 ServiceAvailabilityDescriptor::parse(&bytes).unwrap_err(),
194 Error::BufferTooShort { .. }
195 ));
196 }
197
198 #[test]
199 fn serialize_round_trip() {
200 let d = ServiceAvailabilityDescriptor {
201 availability_flag: true,
202 cell_ids: vec![0x00AB, 0x00CD, 0x00EF],
203 };
204 let mut buf = vec![0u8; d.serialized_len()];
205 d.serialize_into(&mut buf).unwrap();
206 assert_eq!(ServiceAvailabilityDescriptor::parse(&buf).unwrap(), d);
207 }
208
209 #[test]
210 fn serialize_rejects_too_small_buffer() {
211 let d = ServiceAvailabilityDescriptor {
212 availability_flag: false,
213 cell_ids: vec![0x0001],
214 };
215 let mut buf = vec![0u8; 2];
216 assert!(matches!(
217 d.serialize_into(&mut buf).unwrap_err(),
218 Error::OutputBufferTooSmall { .. }
219 ));
220 }
221
222 #[test]
223 fn serialize_rejects_over_range_count() {
224 let d = ServiceAvailabilityDescriptor {
226 availability_flag: true,
227 cell_ids: vec![0u16; 128],
228 };
229 let mut buf = vec![0u8; d.serialized_len()];
230 assert!(matches!(
231 d.serialize_into(&mut buf).unwrap_err(),
232 Error::InvalidDescriptor { tag: TAG, .. }
233 ));
234 }
235
236 #[cfg(feature = "serde")]
237 #[test]
238 fn serde_round_trip() {
239 let d = ServiceAvailabilityDescriptor {
240 availability_flag: true,
241 cell_ids: vec![0x0001, 0x0002],
242 };
243 let json = serde_json::to_string(&d).unwrap();
244 let back: ServiceAvailabilityDescriptor = serde_json::from_str(&json).unwrap();
245 assert_eq!(back, d);
246 }
247}