1use crate::descriptors::DescriptorLoop;
7use crate::error::{Error, Result};
8use alloc::vec::Vec;
9use dvb_common::{Parse, Serialize};
10
11pub const TABLE_ID: u8 = 0x74;
13pub const PID: u16 = 0x0000;
15
16const MIN_HEADER_LEN: usize = 3;
17const EXTENSION_HEADER_LEN: usize = 5;
18const COMMON_DESC_LEN_BYTES: usize = 2;
19const APP_LOOP_LEN_BYTES: usize = 2;
20const CRC_LEN: usize = 4;
21const APP_HEADER_LEN: usize = 9;
22const MIN_SECTION_LEN: usize =
23 MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES + APP_LOOP_LEN_BYTES + CRC_LEN;
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize))]
28#[non_exhaustive]
29pub enum ControlCode {
30 Reserved,
32 Autostart,
34 Present,
36 Destroy,
38 Kill,
40 Prefetch,
42 Remote,
44 Disabled,
46 PlaybackAutostart,
48 Unallocated(u8),
50}
51
52impl ControlCode {
53 #[must_use]
54 pub fn from_u8(v: u8) -> Self {
56 match v {
57 0x00 => Self::Reserved,
58 0x01 => Self::Autostart,
59 0x02 => Self::Present,
60 0x03 => Self::Destroy,
61 0x04 => Self::Kill,
62 0x05 => Self::Prefetch,
63 0x06 => Self::Remote,
64 0x07 => Self::Disabled,
65 0x08 => Self::PlaybackAutostart,
66 _ => Self::Unallocated(v),
67 }
68 }
69
70 #[must_use]
71 pub fn to_u8(self) -> u8 {
73 match self {
74 Self::Reserved => 0x00,
75 Self::Autostart => 0x01,
76 Self::Present => 0x02,
77 Self::Destroy => 0x03,
78 Self::Kill => 0x04,
79 Self::Prefetch => 0x05,
80 Self::Remote => 0x06,
81 Self::Disabled => 0x07,
82 Self::PlaybackAutostart => 0x08,
83 Self::Unallocated(v) => v,
84 }
85 }
86
87 #[must_use]
88 pub fn name(self) -> &'static str {
90 match self {
91 Self::Reserved => "Reserved",
92 Self::Autostart => "AUTOSTART",
93 Self::Present => "PRESENT",
94 Self::Destroy => "DESTROY",
95 Self::Kill => "KILL",
96 Self::Prefetch => "PREFETCH",
97 Self::Remote => "REMOTE",
98 Self::Disabled => "DISABLED",
99 Self::PlaybackAutostart => "PLAYBACK_AUTOSTART",
100 Self::Unallocated(_) => "Unallocated",
101 }
102 }
103}
104dvb_common::impl_spec_display!(ControlCode, Unallocated);
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111#[cfg_attr(feature = "serde", derive(serde::Serialize))]
112#[non_exhaustive]
113pub enum ApplicationType {
114 DvbJ,
116 DvbHtml,
118 HbbTv,
120 OipfDae,
122 Reserved(u16),
124 UserDefined(u16),
126}
127
128impl ApplicationType {
129 #[must_use]
130 pub fn from_u16(v: u16) -> Self {
132 match v {
133 0x0001 => Self::DvbJ,
134 0x0002 => Self::DvbHtml,
135 0x0010 => Self::HbbTv,
136 0x0011 => Self::OipfDae,
137 v if v < 0x8000 => Self::Reserved(v),
138 _ => Self::UserDefined(v),
139 }
140 }
141
142 #[must_use]
143 pub fn to_u16(self) -> u16 {
145 match self {
146 Self::DvbJ => 0x0001,
147 Self::DvbHtml => 0x0002,
148 Self::HbbTv => 0x0010,
149 Self::OipfDae => 0x0011,
150 Self::Reserved(v) | Self::UserDefined(v) => v,
151 }
152 }
153
154 #[must_use]
155 pub fn name(self) -> &'static str {
157 match self {
158 Self::DvbJ => "DVB-J",
159 Self::DvbHtml => "DVB-HTML",
160 Self::HbbTv => "HbbTV",
161 Self::OipfDae => "OIPF DAE",
162 Self::Reserved(_) => "Reserved",
163 Self::UserDefined(_) => "User Defined",
164 }
165 }
166}
167dvb_common::impl_spec_display!(ApplicationType, Reserved, UserDefined);
168
169#[derive(Debug, Clone, PartialEq, Eq)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize))]
172pub struct ApplicationIdentifier {
173 pub organisation_id: u32,
175 pub application_id: u16,
177}
178
179#[derive(Debug, Clone, PartialEq, Eq)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize))]
182#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
183pub struct AitApplication<'a> {
184 pub identifier: ApplicationIdentifier,
186 pub control_code: ControlCode,
188 pub descriptors: DescriptorLoop<'a>,
192}
193
194#[derive(Debug, Clone, PartialEq, Eq)]
196#[cfg_attr(feature = "serde", derive(serde::Serialize))]
197#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
198pub struct AitSection<'a> {
199 pub application_type: ApplicationType,
201 pub test_application_flag: bool,
203 pub version_number: u8,
205 pub current_next_indicator: bool,
207 pub section_number: u8,
209 pub last_section_number: u8,
211 pub common_descriptors: DescriptorLoop<'a>,
215 pub applications: Vec<AitApplication<'a>>,
217}
218
219impl<'a> Parse<'a> for AitSection<'a> {
220 type Error = crate::error::Error;
221 fn parse(bytes: &'a [u8]) -> Result<Self> {
222 let min_len = MIN_HEADER_LEN
223 + EXTENSION_HEADER_LEN
224 + COMMON_DESC_LEN_BYTES
225 + APP_LOOP_LEN_BYTES
226 + CRC_LEN;
227 if bytes.len() < min_len {
228 return Err(Error::BufferTooShort {
229 need: min_len,
230 have: bytes.len(),
231 what: "AitSection",
232 });
233 }
234
235 if bytes[0] != TABLE_ID {
236 return Err(Error::UnexpectedTableId {
237 table_id: bytes[0],
238 what: "AitSection",
239 expected: &[TABLE_ID],
240 });
241 }
242
243 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
244 let total = super::check_section_length(
245 bytes.len(),
246 MIN_HEADER_LEN,
247 section_length as usize,
248 MIN_SECTION_LEN,
249 )?;
250
251 let test_application_flag = (bytes[3] & 0x80) != 0;
252 let application_type_raw = (((bytes[3] & 0x7F) as u16) << 8) | (bytes[4] as u16);
253 let application_type = ApplicationType::from_u16(application_type_raw);
254 let version_number = (bytes[5] >> 1) & 0x1F;
255 let current_next_indicator = (bytes[5] & 0x01) != 0;
256 let section_number = bytes[6];
257 let last_section_number = bytes[7];
258
259 let common_descriptors_length = (((bytes[8] & 0x0F) as usize) << 8) | bytes[9] as usize;
260 let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
261 let common_desc_end = common_desc_start + common_descriptors_length;
262 let app_loop_end = total - CRC_LEN;
263 if common_desc_end > app_loop_end {
264 return Err(Error::SectionLengthOverflow {
265 declared: common_descriptors_length,
266 available: app_loop_end.saturating_sub(common_desc_start),
267 });
268 }
269 let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
270
271 let app_loop_length =
272 (((bytes[common_desc_end] & 0x0F) as usize) << 8) | bytes[common_desc_end + 1] as usize;
273 let app_loop_start = common_desc_end + APP_LOOP_LEN_BYTES;
274 let app_loop_actual_end = app_loop_start + app_loop_length;
275 if app_loop_actual_end > app_loop_end {
276 return Err(Error::SectionLengthOverflow {
277 declared: app_loop_length,
278 available: app_loop_end.saturating_sub(app_loop_start),
279 });
280 }
281
282 let mut applications = Vec::new();
283 let mut pos = app_loop_start;
284 while pos + APP_HEADER_LEN <= app_loop_actual_end {
285 let organisation_id = ((bytes[pos] as u32) << 24)
286 | ((bytes[pos + 1] as u32) << 16)
287 | ((bytes[pos + 2] as u32) << 8)
288 | (bytes[pos + 3] as u32);
289 let application_id = u16::from_be_bytes([bytes[pos + 4], bytes[pos + 5]]);
290 let control_code = ControlCode::from_u8(bytes[pos + 6]);
291 let app_desc_length =
292 (((bytes[pos + 7] & 0x0F) as usize) << 8) | bytes[pos + 8] as usize;
293 let app_desc_start = pos + APP_HEADER_LEN;
294 let app_desc_end = app_desc_start + app_desc_length;
295 if app_desc_end > app_loop_actual_end {
296 return Err(Error::SectionLengthOverflow {
297 declared: app_desc_length,
298 available: app_loop_actual_end.saturating_sub(app_desc_start),
299 });
300 }
301 applications.push(AitApplication {
302 identifier: ApplicationIdentifier {
303 organisation_id,
304 application_id,
305 },
306 control_code,
307 descriptors: DescriptorLoop::new(&bytes[app_desc_start..app_desc_end]),
308 });
309 pos = app_desc_end;
310 }
311
312 Ok(AitSection {
313 application_type,
314 test_application_flag,
315 version_number,
316 current_next_indicator,
317 section_number,
318 last_section_number,
319 common_descriptors,
320 applications,
321 })
322 }
323}
324
325impl Serialize for AitSection<'_> {
326 type Error = crate::error::Error;
327 fn serialized_len(&self) -> usize {
328 let app_bytes: usize = self
329 .applications
330 .iter()
331 .map(|a| APP_HEADER_LEN + a.descriptors.len())
332 .sum();
333 MIN_HEADER_LEN
334 + EXTENSION_HEADER_LEN
335 + COMMON_DESC_LEN_BYTES
336 + self.common_descriptors.len()
337 + APP_LOOP_LEN_BYTES
338 + app_bytes
339 + CRC_LEN
340 }
341
342 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
343 let len = self.serialized_len();
344 if buf.len() < len {
345 return Err(Error::OutputBufferTooSmall {
346 need: len,
347 have: buf.len(),
348 });
349 }
350
351 let section_length: u16 = (len - MIN_HEADER_LEN) as u16;
352 let app_type_raw = self.application_type.to_u16();
353 buf[0] = TABLE_ID;
354 buf[1] = super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F);
355 buf[2] = (section_length & 0xFF) as u8;
356 buf[3] = (u8::from(self.test_application_flag) << 7) | ((app_type_raw >> 8) as u8 & 0x7F);
357 buf[4] = (app_type_raw & 0xFF) as u8;
358 buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
359 buf[6] = self.section_number;
360 buf[7] = self.last_section_number;
361
362 let cdl = self.common_descriptors.len() as u16;
363 buf[8] = 0xF0 | ((cdl >> 8) as u8 & 0x0F);
364 buf[9] = (cdl & 0xFF) as u8;
365
366 let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
367 buf[common_desc_start..common_desc_start + self.common_descriptors.len()]
368 .copy_from_slice(self.common_descriptors.raw());
369
370 let app_loop_start = common_desc_start + self.common_descriptors.len();
371 let app_bytes: usize = self
372 .applications
373 .iter()
374 .map(|a| APP_HEADER_LEN + a.descriptors.len())
375 .sum();
376 let apl = app_bytes as u16;
377 buf[app_loop_start] = 0xF0 | ((apl >> 8) as u8 & 0x0F);
378 buf[app_loop_start + 1] = (apl & 0xFF) as u8;
379
380 let mut pos = app_loop_start + APP_LOOP_LEN_BYTES;
381 for app in &self.applications {
382 buf[pos..pos + 4].copy_from_slice(&app.identifier.organisation_id.to_be_bytes());
383 buf[pos + 4..pos + 6].copy_from_slice(&app.identifier.application_id.to_be_bytes());
384 buf[pos + 6] = app.control_code.to_u8();
385 let adl = app.descriptors.len() as u16;
386 buf[pos + 7] = 0xF0 | ((adl >> 8) as u8 & 0x0F);
387 buf[pos + 8] = (adl & 0xFF) as u8;
388 let desc_start = pos + APP_HEADER_LEN;
389 buf[desc_start..desc_start + app.descriptors.len()]
390 .copy_from_slice(app.descriptors.raw());
391 pos = desc_start + app.descriptors.len();
392 }
393
394 let crc_pos = len - CRC_LEN;
395 let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
396 buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
397 Ok(len)
398 }
399}
400impl<'a> crate::traits::TableDef<'a> for AitSection<'a> {
401 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
402 const NAME: &'static str = "APPLICATION_INFORMATION";
403}
404
405#[cfg(test)]
406mod tests {
407 use super::*;
408
409 fn build_ait(
410 application_type: u16,
411 test_flag: bool,
412 version: u8,
413 common_descriptors: &[u8],
414 applications: &[(u32, u16, u8, Vec<u8>)],
415 ) -> Vec<u8> {
416 let app_bytes: usize = applications
417 .iter()
418 .map(|(_, _, _, d)| APP_HEADER_LEN + d.len())
419 .sum();
420 let section_length: u16 = (EXTENSION_HEADER_LEN
421 + COMMON_DESC_LEN_BYTES
422 + common_descriptors.len()
423 + APP_LOOP_LEN_BYTES
424 + app_bytes
425 + CRC_LEN) as u16;
426 let mut v = vec![
427 TABLE_ID,
428 super::super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F),
429 (section_length & 0xFF) as u8,
430 (u8::from(test_flag) << 7) | ((application_type >> 8) as u8 & 0x7F),
431 (application_type & 0xFF) as u8,
432 0xC0 | ((version & 0x1F) << 1) | 0x01,
433 0,
434 0,
435 ];
436 let cdl = common_descriptors.len() as u16;
437 v.push(0xF0 | ((cdl >> 8) as u8 & 0x0F));
438 v.push((cdl & 0xFF) as u8);
439 v.extend_from_slice(common_descriptors);
440 let apl = app_bytes as u16;
441 v.push(0xF0 | ((apl >> 8) as u8 & 0x0F));
442 v.push((apl & 0xFF) as u8);
443 for &(org_id, app_id, cc, ref desc) in applications {
444 v.extend_from_slice(&org_id.to_be_bytes());
445 v.extend_from_slice(&app_id.to_be_bytes());
446 v.push(cc);
447 let adl = desc.len() as u16;
448 v.push(0xF0 | ((adl >> 8) as u8 & 0x0F));
449 v.push((adl & 0xFF) as u8);
450 v.extend_from_slice(desc);
451 }
452 v.extend_from_slice(&[0, 0, 0, 0]);
453 v
454 }
455
456 #[test]
457 fn parse_rejects_wrong_table_id() {
458 let mut bytes = build_ait(0x0010, false, 0, &[], &[]);
459 bytes[0] = 0x00;
460 let err = AitSection::parse(&bytes).unwrap_err();
461 assert!(matches!(
462 err,
463 Error::UnexpectedTableId { table_id: 0x00, .. }
464 ));
465 }
466
467 #[test]
468 fn parse_rejects_short_buffer() {
469 let err = AitSection::parse(&[0x74, 0x00]).unwrap_err();
470 assert!(matches!(err, Error::BufferTooShort { .. }));
471 }
472
473 #[test]
474 fn parse_empty_ait_no_applications() {
475 let bytes = build_ait(0x0010, false, 5, &[], &[]);
476 let ait = AitSection::parse(&bytes).expect("parse");
477 assert_eq!(ait.application_type, ApplicationType::HbbTv);
478 assert!(!ait.test_application_flag);
479 assert_eq!(ait.version_number, 5);
480 assert!(ait.current_next_indicator);
481 assert_eq!(ait.section_number, 0);
482 assert_eq!(ait.last_section_number, 0);
483 assert_eq!(ait.common_descriptors.len(), 0);
484 assert_eq!(ait.applications.len(), 0);
485 }
486
487 #[test]
488 fn parse_test_application_flag_extracted() {
489 let bytes = build_ait(0x0010, true, 0, &[], &[]);
490 let ait = AitSection::parse(&bytes).unwrap();
491 assert!(ait.test_application_flag);
492 }
493
494 #[test]
495 fn parse_common_descriptors_preserved() {
496 let desc = vec![0x00, 0x02, 0xAA, 0xBB];
497 let bytes = build_ait(0x0010, false, 0, &desc, &[]);
498 let ait = AitSection::parse(&bytes).unwrap();
499 assert_eq!(ait.common_descriptors.raw(), &desc[..]);
500 }
501
502 #[test]
503 fn parse_single_application() {
504 let desc = vec![0x02, 0x03, 0xCC, 0xDD, 0xEE];
505 let bytes = build_ait(
506 0x0010,
507 false,
508 0,
509 &[],
510 &[(0x12345678, 0xABCD, 0x01, desc.clone())],
511 );
512 let ait = AitSection::parse(&bytes).unwrap();
513 assert_eq!(ait.applications.len(), 1);
514 assert_eq!(ait.applications[0].identifier.organisation_id, 0x12345678);
515 assert_eq!(ait.applications[0].identifier.application_id, 0xABCD);
516 assert_eq!(ait.applications[0].control_code, ControlCode::Autostart);
517 assert_eq!(ait.applications[0].descriptors.raw(), &desc[..]);
518 }
519
520 #[test]
521 fn parse_multiple_applications_preserve_order() {
522 let bytes = build_ait(
523 0x0010,
524 false,
525 0,
526 &[],
527 &[
528 (0x00000001, 0x0001, 0x01, vec![]),
529 (0x00000002, 0x0002, 0x02, vec![0x01]),
530 (0x00000003, 0x0003, 0x03, vec![0x02, 0x03]),
531 ],
532 );
533 let ait = AitSection::parse(&bytes).unwrap();
534 assert_eq!(ait.applications.len(), 3);
535 assert_eq!(ait.applications[0].identifier.organisation_id, 1);
536 assert_eq!(ait.applications[1].identifier.organisation_id, 2);
537 assert_eq!(ait.applications[2].identifier.organisation_id, 3);
538 }
539
540 #[test]
541 fn serialize_round_trip_empty() {
542 let ait = AitSection {
543 application_type: ApplicationType::HbbTv,
544 test_application_flag: false,
545 version_number: 3,
546 current_next_indicator: true,
547 section_number: 0,
548 last_section_number: 0,
549 common_descriptors: DescriptorLoop::new(&[]),
550 applications: vec![],
551 };
552 let mut buf = vec![0u8; ait.serialized_len()];
553 ait.serialize_into(&mut buf).unwrap();
554 let reparsed = AitSection::parse(&buf).unwrap();
555 assert_eq!(ait, reparsed);
556 }
557
558 #[test]
559 fn serialize_round_trip_with_applications() {
560 let desc1: [u8; 2] = [0xAA, 0xBB];
561 let ait = AitSection {
562 application_type: ApplicationType::HbbTv,
563 test_application_flag: true,
564 version_number: 7,
565 current_next_indicator: true,
566 section_number: 1,
567 last_section_number: 2,
568 common_descriptors: DescriptorLoop::new(&[0x01, 0x00]),
569 applications: vec![
570 AitApplication {
571 identifier: ApplicationIdentifier {
572 organisation_id: 0x12345678,
573 application_id: 0xABCD,
574 },
575 control_code: ControlCode::Autostart,
576 descriptors: DescriptorLoop::new(&desc1),
577 },
578 AitApplication {
579 identifier: ApplicationIdentifier {
580 organisation_id: 0x87654321,
581 application_id: 0x00EF,
582 },
583 control_code: ControlCode::Present,
584 descriptors: DescriptorLoop::new(&[]),
585 },
586 ],
587 };
588 let mut buf = vec![0u8; ait.serialized_len()];
589 ait.serialize_into(&mut buf).unwrap();
590 let reparsed = AitSection::parse(&buf).unwrap();
591 assert_eq!(ait, reparsed);
592 }
593
594 #[test]
595 fn parse_rejects_zero_section_length() {
596 let mut buf = vec![0u8; 64];
597 buf[0] = TABLE_ID;
598 buf[1] = 0xF0;
599 buf[2] = 0x00;
600 for b in &mut buf[3..] {
601 *b = 0xFF;
602 }
603 assert!(matches!(
604 AitSection::parse(&buf).unwrap_err(),
605 Error::SectionLengthOverflow { .. }
606 ));
607 }
608
609 #[test]
610 fn control_code_full_range_round_trip() {
611 for byte in 0u8..=0xFF {
612 let cc = ControlCode::from_u8(byte);
613 assert_eq!(
614 cc.to_u8(),
615 byte,
616 "ControlCode round-trip failed for {byte:#04x}"
617 );
618 }
619 }
620
621 #[test]
622 fn control_code_named_values() {
623 assert_eq!(ControlCode::Autostart.to_u8(), 0x01);
624 assert_eq!(ControlCode::Kill.to_u8(), 0x04);
625 assert_eq!(ControlCode::Prefetch.to_u8(), 0x05);
626 assert_eq!(ControlCode::PlaybackAutostart.to_u8(), 0x08);
627 }
628
629 #[test]
630 fn control_code_wire_to_name() {
631 assert_eq!(ControlCode::from_u8(0x01).name(), "AUTOSTART");
632 assert_eq!(ControlCode::from_u8(0x04).name(), "KILL");
633 assert_eq!(ControlCode::from_u8(0x08).name(), "PLAYBACK_AUTOSTART");
634 assert_eq!(ControlCode::from_u8(0x00).name(), "Reserved");
635 }
636
637 #[test]
638 fn application_type_full_range_round_trip() {
639 for at in 0u16..=0xFFFF {
640 let app = ApplicationType::from_u16(at);
641 assert_eq!(
642 app.to_u16(),
643 at,
644 "ApplicationType round-trip failed for {at:#06x}"
645 );
646 }
647 }
648}