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