1use crate::descriptors::DescriptorLoop;
13use crate::error::{Error, Result};
14use crate::traits::Table;
15use dvb_common::{Parse, Serialize};
16
17pub const TABLE_ID: u8 = 0x73;
19pub const PID: u16 = 0x0014;
21
22const HEADER_LEN: usize = 3;
23const UTC_TIME_LEN: usize = 5;
24const DESC_LOOP_LEN_FIELD: usize = 2;
25const CRC_LEN: usize = 4;
26
27#[derive(Debug, Clone, PartialEq, Eq)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize))]
30#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
31pub struct Tot<'a> {
32 pub utc_time_raw: [u8; 5],
34 pub descriptors: DescriptorLoop<'a>,
38}
39
40impl<'a> Parse<'a> for Tot<'a> {
41 type Error = crate::error::Error;
42 fn parse(bytes: &'a [u8]) -> Result<Self> {
43 let min_len = HEADER_LEN + UTC_TIME_LEN + DESC_LOOP_LEN_FIELD + CRC_LEN;
44 if bytes.len() < min_len {
45 return Err(Error::BufferTooShort {
46 need: min_len,
47 have: bytes.len(),
48 what: "Tot",
49 });
50 }
51 if bytes[0] != TABLE_ID {
52 return Err(Error::UnexpectedTableId {
53 table_id: bytes[0],
54 what: "Tot",
55 expected: &[TABLE_ID],
56 });
57 }
58 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
59 let total = HEADER_LEN + section_length as usize;
60 if bytes.len() < total {
61 return Err(Error::SectionLengthOverflow {
62 declared: section_length as usize,
63 available: bytes.len() - HEADER_LEN,
64 });
65 }
66 let utc_time_raw = [bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]];
67 let dl_pos = HEADER_LEN + UTC_TIME_LEN;
68 let dl = (((bytes[dl_pos] & 0x0F) as usize) << 8) | bytes[dl_pos + 1] as usize;
69 let d_start = dl_pos + DESC_LOOP_LEN_FIELD;
70 let d_end = d_start + dl;
71 if d_end > total - CRC_LEN {
72 return Err(Error::SectionLengthOverflow {
73 declared: dl,
74 available: total - CRC_LEN - d_start,
75 });
76 }
77 Ok(Tot {
78 utc_time_raw,
79 descriptors: DescriptorLoop::new(&bytes[d_start..d_end]),
80 })
81 }
82}
83
84impl Serialize for Tot<'_> {
85 type Error = crate::error::Error;
86 fn serialized_len(&self) -> usize {
87 HEADER_LEN + UTC_TIME_LEN + DESC_LOOP_LEN_FIELD + self.descriptors.len() + CRC_LEN
88 }
89 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
90 let len = self.serialized_len();
91 if buf.len() < len {
92 return Err(Error::OutputBufferTooSmall {
93 need: len,
94 have: buf.len(),
95 });
96 }
97 let section_length = (len - HEADER_LEN) as u16;
98 buf[0] = TABLE_ID;
99 buf[1] = 0x70 | ((section_length >> 8) as u8 & 0x0F);
102 buf[2] = (section_length & 0xFF) as u8;
103 buf[3..8].copy_from_slice(&self.utc_time_raw);
104 let dl = self.descriptors.len() as u16;
105 buf[8] = 0xF0 | ((dl >> 8) as u8 & 0x0F);
106 buf[9] = (dl & 0xFF) as u8;
107 let d_end = 10 + self.descriptors.len();
108 buf[10..d_end].copy_from_slice(self.descriptors.raw());
109 let crc_pos = len - CRC_LEN;
110 let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
111 buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
112 Ok(len)
113 }
114}
115
116impl<'a> Table<'a> for Tot<'a> {
117 const TABLE_ID: u8 = TABLE_ID;
118 const PID: u16 = PID;
119}
120
121impl<'a> crate::traits::TableDef<'a> for Tot<'a> {
122 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
123 const NAME: &'static str = "TIME_OFFSET";
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 fn build_tot(desc: &[u8]) -> Vec<u8> {
131 let section_length = (UTC_TIME_LEN + DESC_LOOP_LEN_FIELD + desc.len() + CRC_LEN) as u16;
132 let mut v = Vec::new();
133 v.push(TABLE_ID);
134 v.push(0x70 | ((section_length >> 8) as u8 & 0x0F));
136 v.push((section_length & 0xFF) as u8);
137 v.extend_from_slice(&[0xE4, 0x09, 0x12, 0x34, 0x56]);
138 let dl = desc.len() as u16;
139 v.push(0xF0 | ((dl >> 8) as u8 & 0x0F));
140 v.push((dl & 0xFF) as u8);
141 v.extend_from_slice(desc);
142 v.extend_from_slice(&[0, 0, 0, 0]);
143 v
144 }
145
146 #[test]
147 fn parse_with_no_descriptors() {
148 let bytes = build_tot(&[]);
149 let tot = Tot::parse(&bytes).unwrap();
150 assert_eq!(tot.utc_time_raw, [0xE4, 0x09, 0x12, 0x34, 0x56]);
151 assert_eq!(tot.descriptors.raw(), &[] as &[u8]);
152 }
153
154 #[test]
155 fn parse_with_local_time_offset_descriptor() {
156 let lto = [
157 0x58u8, 13, b'G', b'B', b'R', 0x02, 0x00, 0x00, 0xE4, 0x09, 0x12, 0x34, 0x56, 0x01,
158 0x00,
159 ];
160 let bytes = build_tot(<o);
161 let tot = Tot::parse(&bytes).unwrap();
162 assert_eq!(tot.descriptors.raw(), <o[..]);
163 }
164
165 #[test]
166 fn parse_rejects_wrong_tag() {
167 let mut bytes = build_tot(&[]);
168 bytes[0] = 0x70;
169 assert!(matches!(
170 Tot::parse(&bytes).unwrap_err(),
171 Error::UnexpectedTableId { table_id: 0x70, .. }
172 ));
173 }
174
175 #[test]
176 fn serialize_round_trip() {
177 let lto = [0x58u8, 0];
178 let bytes = build_tot(<o);
179 let tot = Tot::parse(&bytes).unwrap();
180 let mut buf = vec![0u8; tot.serialized_len()];
181 tot.serialize_into(&mut buf).unwrap();
182 let re = Tot::parse(&buf).unwrap();
183 assert_eq!(tot, re);
184 }
185}