Skip to main content

dvb_si/descriptors/
application_signalling.rs

1//! Application Signalling Descriptor — ETSI TS 102 809 §5.3.5.2.1, Table 17
2//! (tag 0x6F).
3//!
4//! Carried in the PMT to point at the AIT and enumerate the application types
5//! present alongside the AIT version expected. Per ts_102_809_apps.md
6//! "Table 17 — Application signalling descriptor syntax" (PDF p. 37) each loop
7//! entry is 3 bytes: reserved_future_use(1) + application_type(15), then
8//! reserved_future_use(3) + AIT_version_number(5).
9
10use super::descriptor_body;
11use crate::error::{Error, Result};
12use crate::tables::ait::ApplicationType;
13use alloc::vec::Vec;
14use dvb_common::{Parse, Serialize};
15
16/// Descriptor tag for application_signalling_descriptor.
17pub const TAG: u8 = 0x6F;
18const HEADER_LEN: usize = 2;
19const ENTRY_LEN: usize = 3;
20
21/// Largest representable 15-bit application_type.
22const APPLICATION_TYPE_MAX: u16 = 0x7FFF;
23/// Largest representable 5-bit AIT_version_number.
24const AIT_VERSION_MAX: u8 = 0x1F;
25
26/// Returns the well-known name for an `application_type`, or `None` if the
27/// type is not recognised.
28///
29/// Delegates to [`crate::tables::ait::ApplicationType`] as the single source
30/// of truth for application-type naming.  Verified entries from the
31/// DVB Services registry.
32#[must_use]
33pub fn application_type_name(val: u16) -> Option<&'static str> {
34    match crate::tables::ait::ApplicationType::from_u16(val) {
35        crate::tables::ait::ApplicationType::DvbJ
36        | crate::tables::ait::ApplicationType::DvbHtml
37        | crate::tables::ait::ApplicationType::HbbTv
38        | crate::tables::ait::ApplicationType::OipfDae => {
39            Some(crate::tables::ait::ApplicationType::from_u16(val).name())
40        }
41        _ => None,
42    }
43}
44
45/// One application-signalling loop entry.
46#[derive(Debug, Clone, PartialEq, Eq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize))]
48pub struct ApplicationSignallingEntry {
49    /// 15-bit application_type — reuses [`crate::tables::ait::ApplicationType`]
50    /// (ETSI TS 102 809 §5.3.5.2.1 / DVB Services registry).
51    pub application_type: ApplicationType,
52    /// 5-bit AIT_version_number this entry refers to.
53    pub ait_version_number: u8,
54}
55
56/// Application Signalling Descriptor.
57#[derive(Debug, Clone, PartialEq, Eq)]
58#[cfg_attr(feature = "serde", derive(serde::Serialize))]
59pub struct ApplicationSignallingDescriptor {
60    /// Entries in wire order.
61    pub entries: Vec<ApplicationSignallingEntry>,
62}
63
64impl<'a> Parse<'a> for ApplicationSignallingDescriptor {
65    type Error = crate::error::Error;
66    fn parse(bytes: &'a [u8]) -> Result<Self> {
67        let body = descriptor_body(
68            bytes,
69            TAG,
70            "ApplicationSignallingDescriptor",
71            "unexpected tag for application_signalling_descriptor",
72        )?;
73        if body.len() % ENTRY_LEN != 0 {
74            return Err(Error::InvalidDescriptor {
75                tag: TAG,
76                reason: "application_signalling_descriptor length must be a multiple of 3",
77            });
78        }
79        let mut entries = Vec::with_capacity(body.len() / ENTRY_LEN);
80        for chunk in body.chunks_exact(ENTRY_LEN) {
81            // reserved_future_use(1) ignored on parse.
82            let application_type_raw =
83                u16::from_be_bytes([chunk[0], chunk[1]]) & APPLICATION_TYPE_MAX;
84            let application_type = ApplicationType::from_u16(application_type_raw);
85            // reserved_future_use(3) ignored on parse.
86            let ait_version_number = chunk[2] & AIT_VERSION_MAX;
87            entries.push(ApplicationSignallingEntry {
88                application_type,
89                ait_version_number,
90            });
91        }
92        Ok(Self { entries })
93    }
94}
95
96impl Serialize for ApplicationSignallingDescriptor {
97    type Error = crate::error::Error;
98    fn serialized_len(&self) -> usize {
99        HEADER_LEN + self.entries.len() * ENTRY_LEN
100    }
101
102    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
103        for e in &self.entries {
104            if e.application_type.to_u16() > APPLICATION_TYPE_MAX {
105                return Err(Error::InvalidDescriptor {
106                    tag: TAG,
107                    reason: "application_type exceeds 15 bits",
108                });
109            }
110            if e.ait_version_number > AIT_VERSION_MAX {
111                return Err(Error::InvalidDescriptor {
112                    tag: TAG,
113                    reason: "ait_version_number exceeds 5 bits",
114                });
115            }
116        }
117        if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
118            return Err(Error::InvalidDescriptor {
119                tag: TAG,
120                reason: "application_signalling_descriptor body exceeds 255 bytes",
121            });
122        }
123        let len = self.serialized_len();
124        if buf.len() < len {
125            return Err(Error::OutputBufferTooSmall {
126                need: len,
127                have: buf.len(),
128            });
129        }
130        buf[0] = TAG;
131        buf[1] = (self.entries.len() * ENTRY_LEN) as u8;
132        let mut pos = HEADER_LEN;
133        for e in &self.entries {
134            // reserved_future_use(1) emitted as 1.
135            let word = 0x8000 | (e.application_type.to_u16() & APPLICATION_TYPE_MAX);
136            buf[pos..pos + 2].copy_from_slice(&word.to_be_bytes());
137            // reserved_future_use(3) emitted as 1s.
138            buf[pos + 2] = 0xE0 | (e.ait_version_number & AIT_VERSION_MAX);
139            pos += ENTRY_LEN;
140        }
141        Ok(len)
142    }
143}
144impl<'a> crate::traits::DescriptorDef<'a> for ApplicationSignallingDescriptor {
145    const TAG: u8 = TAG;
146    const NAME: &'static str = "APPLICATION_SIGNALLING";
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn parse_single_entry() {
155        // application_type=0x0010 (HbbTV), AIT_version=3, reserved bits set.
156        let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
157        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
158        assert_eq!(d.entries.len(), 1);
159        assert_eq!(d.entries[0].application_type, ApplicationType::HbbTv);
160        assert_eq!(d.entries[0].ait_version_number, 3);
161    }
162
163    #[test]
164    fn parse_multiple_entries() {
165        let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
166        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
167        assert_eq!(d.entries.len(), 2);
168        assert_eq!(d.entries[0].application_type, ApplicationType::DvbJ);
169        assert_eq!(d.entries[0].ait_version_number, 5);
170        assert_eq!(
171            d.entries[1].application_type,
172            ApplicationType::Reserved(0x7FFF)
173        );
174        assert_eq!(d.entries[1].ait_version_number, 0x1F);
175    }
176
177    #[test]
178    fn parse_ignores_reserved_bits() {
179        // top bit of word + top 3 bits of version byte are reserved → masked out.
180        let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
181        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
182        assert_eq!(
183            d.entries[0].application_type,
184            ApplicationType::Reserved(0x7FFF)
185        );
186        assert_eq!(d.entries[0].ait_version_number, 0x1F);
187    }
188
189    #[test]
190    fn application_type_name_verified() {
191        assert_eq!(application_type_name(0x0001), Some("DVB-J"));
192        assert_eq!(application_type_name(0x0002), Some("DVB-HTML"));
193        assert_eq!(application_type_name(0x0010), Some("HbbTV"));
194        assert_eq!(application_type_name(0x0011), Some("OIPF DAE"));
195    }
196
197    #[test]
198    fn application_type_name_removed_entries_return_none() {
199        assert_eq!(application_type_name(0x0003), None);
200        assert_eq!(application_type_name(0x0004), None);
201        assert_eq!(application_type_name(0x0005), None);
202        assert_eq!(application_type_name(0x0012), None);
203        assert_eq!(application_type_name(0x0013), None);
204        assert_eq!(application_type_name(0x0020), None);
205    }
206
207    #[test]
208    fn application_type_name_unknown() {
209        assert_eq!(application_type_name(0x0000), None);
210        assert_eq!(application_type_name(0xFFFF), None);
211    }
212
213    #[test]
214    fn parse_rejects_wrong_tag() {
215        assert!(matches!(
216            ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
217            Error::InvalidDescriptor { tag: 0x70, .. }
218        ));
219    }
220
221    #[test]
222    fn parse_rejects_length_not_multiple_of_3() {
223        let bytes = [TAG, 4, 0, 0, 0, 0];
224        assert!(matches!(
225            ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
226            Error::InvalidDescriptor { .. }
227        ));
228    }
229
230    #[test]
231    fn empty_descriptor_valid() {
232        let bytes = [TAG, 0];
233        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
234        assert!(d.entries.is_empty());
235    }
236
237    #[test]
238    fn serialize_round_trip() {
239        let d = ApplicationSignallingDescriptor {
240            entries: vec![
241                ApplicationSignallingEntry {
242                    application_type: ApplicationType::DvbJ,
243                    ait_version_number: 7,
244                },
245                ApplicationSignallingEntry {
246                    application_type: ApplicationType::HbbTv,
247                    ait_version_number: 0,
248                },
249            ],
250        };
251        let mut buf = vec![0u8; d.serialized_len()];
252        d.serialize_into(&mut buf).unwrap();
253        assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
254    }
255
256    #[test]
257    fn serialize_rejects_application_type_over_range() {
258        let d = ApplicationSignallingDescriptor {
259            entries: vec![ApplicationSignallingEntry {
260                // UserDefined(0x8000) → to_u16() = 0x8000 > APPLICATION_TYPE_MAX
261                application_type: ApplicationType::UserDefined(0x8000),
262                ait_version_number: 0,
263            }],
264        };
265        let mut buf = vec![0u8; d.serialized_len()];
266        assert!(matches!(
267            d.serialize_into(&mut buf).unwrap_err(),
268            Error::InvalidDescriptor { .. }
269        ));
270    }
271
272    #[test]
273    fn serialize_rejects_ait_version_over_range() {
274        let d = ApplicationSignallingDescriptor {
275            entries: vec![ApplicationSignallingEntry {
276                application_type: ApplicationType::Reserved(0),
277                ait_version_number: 0x20,
278            }],
279        };
280        let mut buf = vec![0u8; d.serialized_len()];
281        assert!(matches!(
282            d.serialize_into(&mut buf).unwrap_err(),
283            Error::InvalidDescriptor { .. }
284        ));
285    }
286
287    #[cfg(feature = "serde")]
288    #[test]
289    fn serde_round_trip() {
290        let d = ApplicationSignallingDescriptor {
291            entries: vec![ApplicationSignallingEntry {
292                application_type: ApplicationType::HbbTv,
293                ait_version_number: 4,
294            }],
295        };
296        let j = serde_json::to_string(&d).unwrap();
297        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
298        let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
299    }
300
301    #[test]
302    fn application_type_known_values_round_trip() {
303        // Spot-check that well-known types survive a serialize→parse round-trip.
304        for at in [
305            ApplicationType::DvbJ,
306            ApplicationType::DvbHtml,
307            ApplicationType::HbbTv,
308            ApplicationType::OipfDae,
309            ApplicationType::Reserved(0x0005),
310        ] {
311            let d = ApplicationSignallingDescriptor {
312                entries: vec![ApplicationSignallingEntry {
313                    application_type: at,
314                    ait_version_number: 1,
315                }],
316            };
317            let mut buf = vec![0u8; d.serialized_len()];
318            d.serialize_into(&mut buf).unwrap();
319            let re = ApplicationSignallingDescriptor::parse(&buf).unwrap();
320            assert_eq!(re, d, "ApplicationType round-trip failed for {at:?}");
321        }
322    }
323}