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