Skip to main content

dvb_si/tables/
ait.rs

1//! Application Information Table — ETSI TS 102 809 §5.3.4.
2//!
3//! AIT carries application metadata for HbbTV / interactive-TV services.
4//! Carried on a per-service PID with table_id 0x74.
5
6use crate::descriptors::DescriptorLoop;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// AIT table_id (ETSI TS 102 809 §5.3.4).
11pub const TABLE_ID: u8 = 0x74;
12/// AIT has no well-known PID — it is service-specific.
13pub const PID: u16 = 0x0000;
14
15const MIN_HEADER_LEN: usize = 3;
16const EXTENSION_HEADER_LEN: usize = 5;
17const COMMON_DESC_LEN_BYTES: usize = 2;
18const APP_LOOP_LEN_BYTES: usize = 2;
19const CRC_LEN: usize = 4;
20const APP_HEADER_LEN: usize = 9;
21const MIN_SECTION_LEN: usize =
22    MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES + APP_LOOP_LEN_BYTES + CRC_LEN;
23
24/// 48-bit application identifier: organisation_id + application_id.
25#[derive(Debug, Clone, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize))]
27pub struct ApplicationIdentifier {
28    /// 32-bit organisation_id.
29    pub organisation_id: u32,
30    /// 16-bit application_id.
31    pub application_id: u16,
32}
33
34/// One application entry in the AIT application loop.
35#[derive(Debug, Clone, PartialEq, Eq)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize))]
37#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
38pub struct AitApplication<'a> {
39    /// Application identifier.
40    pub identifier: ApplicationIdentifier,
41    /// Application control code (1 = autostart, etc.).
42    pub control_code: u8,
43    /// Raw descriptor bytes for this application.
44    /// Per-application descriptor loop. Serializes as the typed descriptor
45    /// sequence; `.raw()` yields the wire bytes.
46    pub descriptors: DescriptorLoop<'a>,
47}
48
49/// Application Information Table.
50#[derive(Debug, Clone, PartialEq, Eq)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize))]
52#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
53pub struct AitSection<'a> {
54    /// 15-bit application_type (e.g. 0x0010 for HbbTV).
55    pub application_type: u16,
56    /// Test application flag (bit 15 of the extension field).
57    pub test_application_flag: bool,
58    /// 5-bit version_number.
59    pub version_number: u8,
60    /// current_next_indicator bit.
61    pub current_next_indicator: bool,
62    /// section_number in the sub-table sequence.
63    pub section_number: u8,
64    /// last_section_number in the sub-table sequence.
65    pub last_section_number: u8,
66    /// Raw common descriptor bytes.
67    /// Common descriptor loop. Serializes as the typed descriptor sequence;
68    /// `.raw()` yields the wire bytes.
69    pub common_descriptors: DescriptorLoop<'a>,
70    /// Applications in wire order.
71    pub applications: Vec<AitApplication<'a>>,
72}
73
74impl<'a> Parse<'a> for AitSection<'a> {
75    type Error = crate::error::Error;
76    fn parse(bytes: &'a [u8]) -> Result<Self> {
77        let min_len = MIN_HEADER_LEN
78            + EXTENSION_HEADER_LEN
79            + COMMON_DESC_LEN_BYTES
80            + APP_LOOP_LEN_BYTES
81            + CRC_LEN;
82        if bytes.len() < min_len {
83            return Err(Error::BufferTooShort {
84                need: min_len,
85                have: bytes.len(),
86                what: "AitSection",
87            });
88        }
89
90        if bytes[0] != TABLE_ID {
91            return Err(Error::UnexpectedTableId {
92                table_id: bytes[0],
93                what: "AitSection",
94                expected: &[TABLE_ID],
95            });
96        }
97
98        let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
99        let total = super::check_section_length(
100            bytes.len(),
101            MIN_HEADER_LEN,
102            section_length as usize,
103            MIN_SECTION_LEN,
104        )?;
105
106        let test_application_flag = (bytes[3] & 0x80) != 0;
107        let application_type = (((bytes[3] & 0x7F) as u16) << 8) | (bytes[4] as u16);
108        let version_number = (bytes[5] >> 1) & 0x1F;
109        let current_next_indicator = (bytes[5] & 0x01) != 0;
110        let section_number = bytes[6];
111        let last_section_number = bytes[7];
112
113        let common_descriptors_length = (((bytes[8] & 0x0F) as usize) << 8) | bytes[9] as usize;
114        let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
115        let common_desc_end = common_desc_start + common_descriptors_length;
116        let app_loop_end = total - CRC_LEN;
117        if common_desc_end > app_loop_end {
118            return Err(Error::SectionLengthOverflow {
119                declared: common_descriptors_length,
120                available: app_loop_end.saturating_sub(common_desc_start),
121            });
122        }
123        let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
124
125        let app_loop_length =
126            (((bytes[common_desc_end] & 0x0F) as usize) << 8) | bytes[common_desc_end + 1] as usize;
127        let app_loop_start = common_desc_end + APP_LOOP_LEN_BYTES;
128        let app_loop_actual_end = app_loop_start + app_loop_length;
129        if app_loop_actual_end > app_loop_end {
130            return Err(Error::SectionLengthOverflow {
131                declared: app_loop_length,
132                available: app_loop_end.saturating_sub(app_loop_start),
133            });
134        }
135
136        let mut applications = Vec::new();
137        let mut pos = app_loop_start;
138        while pos + APP_HEADER_LEN <= app_loop_actual_end {
139            let organisation_id = ((bytes[pos] as u32) << 24)
140                | ((bytes[pos + 1] as u32) << 16)
141                | ((bytes[pos + 2] as u32) << 8)
142                | (bytes[pos + 3] as u32);
143            let application_id = u16::from_be_bytes([bytes[pos + 4], bytes[pos + 5]]);
144            let control_code = bytes[pos + 6];
145            let app_desc_length =
146                (((bytes[pos + 7] & 0x0F) as usize) << 8) | bytes[pos + 8] as usize;
147            let app_desc_start = pos + APP_HEADER_LEN;
148            let app_desc_end = app_desc_start + app_desc_length;
149            if app_desc_end > app_loop_actual_end {
150                return Err(Error::SectionLengthOverflow {
151                    declared: app_desc_length,
152                    available: app_loop_actual_end.saturating_sub(app_desc_start),
153                });
154            }
155            applications.push(AitApplication {
156                identifier: ApplicationIdentifier {
157                    organisation_id,
158                    application_id,
159                },
160                control_code,
161                descriptors: DescriptorLoop::new(&bytes[app_desc_start..app_desc_end]),
162            });
163            pos = app_desc_end;
164        }
165
166        Ok(AitSection {
167            application_type,
168            test_application_flag,
169            version_number,
170            current_next_indicator,
171            section_number,
172            last_section_number,
173            common_descriptors,
174            applications,
175        })
176    }
177}
178
179impl Serialize for AitSection<'_> {
180    type Error = crate::error::Error;
181    fn serialized_len(&self) -> usize {
182        let app_bytes: usize = self
183            .applications
184            .iter()
185            .map(|a| APP_HEADER_LEN + a.descriptors.len())
186            .sum();
187        MIN_HEADER_LEN
188            + EXTENSION_HEADER_LEN
189            + COMMON_DESC_LEN_BYTES
190            + self.common_descriptors.len()
191            + APP_LOOP_LEN_BYTES
192            + app_bytes
193            + CRC_LEN
194    }
195
196    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
197        let len = self.serialized_len();
198        if buf.len() < len {
199            return Err(Error::OutputBufferTooSmall {
200                need: len,
201                have: buf.len(),
202            });
203        }
204
205        let section_length: u16 = (len - MIN_HEADER_LEN) as u16;
206        buf[0] = TABLE_ID;
207        buf[1] = super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F);
208        buf[2] = (section_length & 0xFF) as u8;
209        buf[3] = (u8::from(self.test_application_flag) << 7)
210            | ((self.application_type >> 8) as u8 & 0x7F);
211        buf[4] = (self.application_type & 0xFF) as u8;
212        buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
213        buf[6] = self.section_number;
214        buf[7] = self.last_section_number;
215
216        let cdl = self.common_descriptors.len() as u16;
217        buf[8] = 0xF0 | ((cdl >> 8) as u8 & 0x0F);
218        buf[9] = (cdl & 0xFF) as u8;
219
220        let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
221        buf[common_desc_start..common_desc_start + self.common_descriptors.len()]
222            .copy_from_slice(self.common_descriptors.raw());
223
224        let app_loop_start = common_desc_start + self.common_descriptors.len();
225        let app_bytes: usize = self
226            .applications
227            .iter()
228            .map(|a| APP_HEADER_LEN + a.descriptors.len())
229            .sum();
230        let apl = app_bytes as u16;
231        buf[app_loop_start] = 0xF0 | ((apl >> 8) as u8 & 0x0F);
232        buf[app_loop_start + 1] = (apl & 0xFF) as u8;
233
234        let mut pos = app_loop_start + APP_LOOP_LEN_BYTES;
235        for app in &self.applications {
236            buf[pos..pos + 4].copy_from_slice(&app.identifier.organisation_id.to_be_bytes());
237            buf[pos + 4..pos + 6].copy_from_slice(&app.identifier.application_id.to_be_bytes());
238            buf[pos + 6] = app.control_code;
239            let adl = app.descriptors.len() as u16;
240            buf[pos + 7] = 0xF0 | ((adl >> 8) as u8 & 0x0F);
241            buf[pos + 8] = (adl & 0xFF) as u8;
242            let desc_start = pos + APP_HEADER_LEN;
243            buf[desc_start..desc_start + app.descriptors.len()]
244                .copy_from_slice(app.descriptors.raw());
245            pos = desc_start + app.descriptors.len();
246        }
247
248        let crc_pos = len - CRC_LEN;
249        let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
250        buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
251        Ok(len)
252    }
253}
254impl<'a> crate::traits::TableDef<'a> for AitSection<'a> {
255    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
256    const NAME: &'static str = "APPLICATION_INFORMATION";
257}
258
259#[cfg(test)]
260mod tests {
261    use super::*;
262
263    fn build_ait(
264        application_type: u16,
265        test_flag: bool,
266        version: u8,
267        common_descriptors: &[u8],
268        applications: &[(u32, u16, u8, Vec<u8>)],
269    ) -> Vec<u8> {
270        let app_bytes: usize = applications
271            .iter()
272            .map(|(_, _, _, d)| APP_HEADER_LEN + d.len())
273            .sum();
274        let section_length: u16 = (EXTENSION_HEADER_LEN
275            + COMMON_DESC_LEN_BYTES
276            + common_descriptors.len()
277            + APP_LOOP_LEN_BYTES
278            + app_bytes
279            + CRC_LEN) as u16;
280        let mut v = vec![
281            TABLE_ID,
282            super::super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F),
283            (section_length & 0xFF) as u8,
284            (u8::from(test_flag) << 7) | ((application_type >> 8) as u8 & 0x7F),
285            (application_type & 0xFF) as u8,
286            0xC0 | ((version & 0x1F) << 1) | 0x01,
287            0,
288            0,
289        ];
290        let cdl = common_descriptors.len() as u16;
291        v.push(0xF0 | ((cdl >> 8) as u8 & 0x0F));
292        v.push((cdl & 0xFF) as u8);
293        v.extend_from_slice(common_descriptors);
294        let apl = app_bytes as u16;
295        v.push(0xF0 | ((apl >> 8) as u8 & 0x0F));
296        v.push((apl & 0xFF) as u8);
297        for &(org_id, app_id, cc, ref desc) in applications {
298            v.extend_from_slice(&org_id.to_be_bytes());
299            v.extend_from_slice(&app_id.to_be_bytes());
300            v.push(cc);
301            let adl = desc.len() as u16;
302            v.push(0xF0 | ((adl >> 8) as u8 & 0x0F));
303            v.push((adl & 0xFF) as u8);
304            v.extend_from_slice(desc);
305        }
306        v.extend_from_slice(&[0, 0, 0, 0]);
307        v
308    }
309
310    #[test]
311    fn parse_rejects_wrong_table_id() {
312        let mut bytes = build_ait(0x0010, false, 0, &[], &[]);
313        bytes[0] = 0x00;
314        let err = AitSection::parse(&bytes).unwrap_err();
315        assert!(matches!(
316            err,
317            Error::UnexpectedTableId { table_id: 0x00, .. }
318        ));
319    }
320
321    #[test]
322    fn parse_rejects_short_buffer() {
323        let err = AitSection::parse(&[0x74, 0x00]).unwrap_err();
324        assert!(matches!(err, Error::BufferTooShort { .. }));
325    }
326
327    #[test]
328    fn parse_empty_ait_no_applications() {
329        let bytes = build_ait(0x0010, false, 5, &[], &[]);
330        let ait = AitSection::parse(&bytes).expect("parse");
331        assert_eq!(ait.application_type, 0x0010);
332        assert!(!ait.test_application_flag);
333        assert_eq!(ait.version_number, 5);
334        assert!(ait.current_next_indicator);
335        assert_eq!(ait.section_number, 0);
336        assert_eq!(ait.last_section_number, 0);
337        assert_eq!(ait.common_descriptors.len(), 0);
338        assert_eq!(ait.applications.len(), 0);
339    }
340
341    #[test]
342    fn parse_test_application_flag_extracted() {
343        let bytes = build_ait(0x0010, true, 0, &[], &[]);
344        let ait = AitSection::parse(&bytes).unwrap();
345        assert!(ait.test_application_flag);
346    }
347
348    #[test]
349    fn parse_common_descriptors_preserved() {
350        let desc = vec![0x00, 0x02, 0xAA, 0xBB];
351        let bytes = build_ait(0x0010, false, 0, &desc, &[]);
352        let ait = AitSection::parse(&bytes).unwrap();
353        assert_eq!(ait.common_descriptors.raw(), &desc[..]);
354    }
355
356    #[test]
357    fn parse_single_application() {
358        let desc = vec![0x02, 0x03, 0xCC, 0xDD, 0xEE];
359        let bytes = build_ait(
360            0x0010,
361            false,
362            0,
363            &[],
364            &[(0x12345678, 0xABCD, 0x01, desc.clone())],
365        );
366        let ait = AitSection::parse(&bytes).unwrap();
367        assert_eq!(ait.applications.len(), 1);
368        assert_eq!(ait.applications[0].identifier.organisation_id, 0x12345678);
369        assert_eq!(ait.applications[0].identifier.application_id, 0xABCD);
370        assert_eq!(ait.applications[0].control_code, 0x01);
371        assert_eq!(ait.applications[0].descriptors.raw(), &desc[..]);
372    }
373
374    #[test]
375    fn parse_multiple_applications_preserve_order() {
376        let bytes = build_ait(
377            0x0010,
378            false,
379            0,
380            &[],
381            &[
382                (0x00000001, 0x0001, 0x01, vec![]),
383                (0x00000002, 0x0002, 0x02, vec![0x01]),
384                (0x00000003, 0x0003, 0x03, vec![0x02, 0x03]),
385            ],
386        );
387        let ait = AitSection::parse(&bytes).unwrap();
388        assert_eq!(ait.applications.len(), 3);
389        assert_eq!(ait.applications[0].identifier.organisation_id, 1);
390        assert_eq!(ait.applications[1].identifier.organisation_id, 2);
391        assert_eq!(ait.applications[2].identifier.organisation_id, 3);
392    }
393
394    #[test]
395    fn serialize_round_trip_empty() {
396        let ait = AitSection {
397            application_type: 0x0010,
398            test_application_flag: false,
399            version_number: 3,
400            current_next_indicator: true,
401            section_number: 0,
402            last_section_number: 0,
403            common_descriptors: DescriptorLoop::new(&[]),
404            applications: vec![],
405        };
406        let mut buf = vec![0u8; ait.serialized_len()];
407        ait.serialize_into(&mut buf).unwrap();
408        let reparsed = AitSection::parse(&buf).unwrap();
409        assert_eq!(ait, reparsed);
410    }
411
412    #[test]
413    fn serialize_round_trip_with_applications() {
414        let desc1: [u8; 2] = [0xAA, 0xBB];
415        let ait = AitSection {
416            application_type: 0x0010,
417            test_application_flag: true,
418            version_number: 7,
419            current_next_indicator: true,
420            section_number: 1,
421            last_section_number: 2,
422            common_descriptors: DescriptorLoop::new(&[0x01, 0x00]),
423            applications: vec![
424                AitApplication {
425                    identifier: ApplicationIdentifier {
426                        organisation_id: 0x12345678,
427                        application_id: 0xABCD,
428                    },
429                    control_code: 0x01,
430                    descriptors: DescriptorLoop::new(&desc1),
431                },
432                AitApplication {
433                    identifier: ApplicationIdentifier {
434                        organisation_id: 0x87654321,
435                        application_id: 0x00EF,
436                    },
437                    control_code: 0x02,
438                    descriptors: DescriptorLoop::new(&[]),
439                },
440            ],
441        };
442        let mut buf = vec![0u8; ait.serialized_len()];
443        ait.serialize_into(&mut buf).unwrap();
444        let reparsed = AitSection::parse(&buf).unwrap();
445        assert_eq!(ait, reparsed);
446    }
447
448    #[test]
449    fn parse_rejects_zero_section_length() {
450        let mut buf = vec![0u8; 64];
451        buf[0] = TABLE_ID;
452        buf[1] = 0xF0;
453        buf[2] = 0x00;
454        for b in &mut buf[3..] {
455            *b = 0xFF;
456        }
457        assert!(matches!(
458            AitSection::parse(&buf).unwrap_err(),
459            Error::SectionLengthOverflow { .. }
460        ));
461    }
462}