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            // chunks_exact(3) guarantees 3 bytes; unwrap is safe.
82            let (type_bytes, rest) = chunk.split_first_chunk::<2>().unwrap();
83            // reserved_future_use(1) ignored on parse.
84            let application_type_raw = u16::from_be_bytes(*type_bytes) & APPLICATION_TYPE_MAX;
85            let application_type = ApplicationType::from_u16(application_type_raw);
86            // reserved_future_use(3) ignored on parse.
87            let ait_version_number = rest[0] & AIT_VERSION_MAX;
88            entries.push(ApplicationSignallingEntry {
89                application_type,
90                ait_version_number,
91            });
92        }
93        Ok(Self { entries })
94    }
95}
96
97impl Serialize for ApplicationSignallingDescriptor {
98    type Error = crate::error::Error;
99    fn serialized_len(&self) -> usize {
100        HEADER_LEN + self.entries.len() * ENTRY_LEN
101    }
102
103    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
104        for e in &self.entries {
105            if e.application_type.to_u16() > APPLICATION_TYPE_MAX {
106                return Err(Error::InvalidDescriptor {
107                    tag: TAG,
108                    reason: "application_type exceeds 15 bits",
109                });
110            }
111            if e.ait_version_number > AIT_VERSION_MAX {
112                return Err(Error::InvalidDescriptor {
113                    tag: TAG,
114                    reason: "ait_version_number exceeds 5 bits",
115                });
116            }
117        }
118        if self.entries.len() * ENTRY_LEN > u8::MAX as usize {
119            return Err(Error::InvalidDescriptor {
120                tag: TAG,
121                reason: "application_signalling_descriptor body exceeds 255 bytes",
122            });
123        }
124        let len = self.serialized_len();
125        if buf.len() < len {
126            return Err(Error::OutputBufferTooSmall {
127                need: len,
128                have: buf.len(),
129            });
130        }
131        buf[0] = TAG;
132        buf[1] = (self.entries.len() * ENTRY_LEN) as u8;
133        let mut pos = HEADER_LEN;
134        for e in &self.entries {
135            // reserved_future_use(1) emitted as 1.
136            let word = 0x8000 | (e.application_type.to_u16() & APPLICATION_TYPE_MAX);
137            buf[pos..pos + 2].copy_from_slice(&word.to_be_bytes());
138            // reserved_future_use(3) emitted as 1s.
139            buf[pos + 2] = 0xE0 | (e.ait_version_number & AIT_VERSION_MAX);
140            pos += ENTRY_LEN;
141        }
142        Ok(len)
143    }
144}
145impl<'a> crate::traits::DescriptorDef<'a> for ApplicationSignallingDescriptor {
146    const TAG: u8 = TAG;
147    const NAME: &'static str = "APPLICATION_SIGNALLING";
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    #[test]
155    fn parse_single_entry() {
156        // application_type=0x0010 (HbbTV), AIT_version=3, reserved bits set.
157        let bytes = [TAG, 3, 0x80, 0x10, 0xE3];
158        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
159        assert_eq!(d.entries.len(), 1);
160        assert_eq!(d.entries[0].application_type, ApplicationType::HbbTv);
161        assert_eq!(d.entries[0].ait_version_number, 3);
162    }
163
164    #[test]
165    fn parse_multiple_entries() {
166        let bytes = [TAG, 6, 0x00, 0x01, 0x05, 0x7F, 0xFF, 0x1F];
167        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
168        assert_eq!(d.entries.len(), 2);
169        assert_eq!(d.entries[0].application_type, ApplicationType::DvbJ);
170        assert_eq!(d.entries[0].ait_version_number, 5);
171        assert_eq!(
172            d.entries[1].application_type,
173            ApplicationType::Reserved(0x7FFF)
174        );
175        assert_eq!(d.entries[1].ait_version_number, 0x1F);
176    }
177
178    #[test]
179    fn parse_ignores_reserved_bits() {
180        // top bit of word + top 3 bits of version byte are reserved → masked out.
181        let bytes = [TAG, 3, 0xFF, 0xFF, 0xFF];
182        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
183        assert_eq!(
184            d.entries[0].application_type,
185            ApplicationType::Reserved(0x7FFF)
186        );
187        assert_eq!(d.entries[0].ait_version_number, 0x1F);
188    }
189
190    #[test]
191    fn application_type_name_verified() {
192        assert_eq!(application_type_name(0x0001), Some("DVB-J"));
193        assert_eq!(application_type_name(0x0002), Some("DVB-HTML"));
194        assert_eq!(application_type_name(0x0010), Some("HbbTV"));
195        assert_eq!(application_type_name(0x0011), Some("OIPF DAE"));
196    }
197
198    #[test]
199    fn application_type_name_removed_entries_return_none() {
200        assert_eq!(application_type_name(0x0003), None);
201        assert_eq!(application_type_name(0x0004), None);
202        assert_eq!(application_type_name(0x0005), None);
203        assert_eq!(application_type_name(0x0012), None);
204        assert_eq!(application_type_name(0x0013), None);
205        assert_eq!(application_type_name(0x0020), None);
206    }
207
208    #[test]
209    fn application_type_name_unknown() {
210        assert_eq!(application_type_name(0x0000), None);
211        assert_eq!(application_type_name(0xFFFF), None);
212    }
213
214    #[test]
215    fn parse_rejects_wrong_tag() {
216        assert!(matches!(
217            ApplicationSignallingDescriptor::parse(&[0x70, 0]).unwrap_err(),
218            Error::InvalidDescriptor { tag: 0x70, .. }
219        ));
220    }
221
222    #[test]
223    fn parse_rejects_length_not_multiple_of_3() {
224        let bytes = [TAG, 4, 0, 0, 0, 0];
225        assert!(matches!(
226            ApplicationSignallingDescriptor::parse(&bytes).unwrap_err(),
227            Error::InvalidDescriptor { .. }
228        ));
229    }
230
231    #[test]
232    fn empty_descriptor_valid() {
233        let bytes = [TAG, 0];
234        let d = ApplicationSignallingDescriptor::parse(&bytes).unwrap();
235        assert!(d.entries.is_empty());
236    }
237
238    #[test]
239    fn serialize_round_trip() {
240        let d = ApplicationSignallingDescriptor {
241            entries: vec![
242                ApplicationSignallingEntry {
243                    application_type: ApplicationType::DvbJ,
244                    ait_version_number: 7,
245                },
246                ApplicationSignallingEntry {
247                    application_type: ApplicationType::HbbTv,
248                    ait_version_number: 0,
249                },
250            ],
251        };
252        let mut buf = vec![0u8; d.serialized_len()];
253        d.serialize_into(&mut buf).unwrap();
254        assert_eq!(ApplicationSignallingDescriptor::parse(&buf).unwrap(), d);
255    }
256
257    #[test]
258    fn serialize_rejects_application_type_over_range() {
259        let d = ApplicationSignallingDescriptor {
260            entries: vec![ApplicationSignallingEntry {
261                // UserDefined(0x8000) → to_u16() = 0x8000 > APPLICATION_TYPE_MAX
262                application_type: ApplicationType::UserDefined(0x8000),
263                ait_version_number: 0,
264            }],
265        };
266        let mut buf = vec![0u8; d.serialized_len()];
267        assert!(matches!(
268            d.serialize_into(&mut buf).unwrap_err(),
269            Error::InvalidDescriptor { .. }
270        ));
271    }
272
273    #[test]
274    fn serialize_rejects_ait_version_over_range() {
275        let d = ApplicationSignallingDescriptor {
276            entries: vec![ApplicationSignallingEntry {
277                application_type: ApplicationType::Reserved(0),
278                ait_version_number: 0x20,
279            }],
280        };
281        let mut buf = vec![0u8; d.serialized_len()];
282        assert!(matches!(
283            d.serialize_into(&mut buf).unwrap_err(),
284            Error::InvalidDescriptor { .. }
285        ));
286    }
287
288    #[cfg(feature = "serde")]
289    #[test]
290    fn serde_round_trip() {
291        let d = ApplicationSignallingDescriptor {
292            entries: vec![ApplicationSignallingEntry {
293                application_type: ApplicationType::HbbTv,
294                ait_version_number: 4,
295            }],
296        };
297        let j = serde_json::to_string(&d).unwrap();
298        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
299        let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
300    }
301
302    #[test]
303    fn application_type_known_values_round_trip() {
304        // Spot-check that well-known types survive a serialize→parse round-trip.
305        for at in [
306            ApplicationType::DvbJ,
307            ApplicationType::DvbHtml,
308            ApplicationType::HbbTv,
309            ApplicationType::OipfDae,
310            ApplicationType::Reserved(0x0005),
311        ] {
312            let d = ApplicationSignallingDescriptor {
313                entries: vec![ApplicationSignallingEntry {
314                    application_type: at,
315                    ait_version_number: 1,
316                }],
317            };
318            let mut buf = vec![0u8; d.serialized_len()];
319            d.serialize_into(&mut buf).unwrap();
320            let re = ApplicationSignallingDescriptor::parse(&buf).unwrap();
321            assert_eq!(re, d, "ApplicationType round-trip failed for {at:?}");
322        }
323    }
324}