Skip to main content

dvb_t2mi/
packet.rs

1//! T2-MI packet header and type parsing.
2
3use num_enum::TryFromPrimitive;
4
5/// Packet types per ETSI TS 102 773 Table 1.
6///
7/// Reserved for future use: `0x22..=0x2F`, `0x34..=0xFF`.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, TryFromPrimitive)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize))]
10#[repr(u8)]
11pub enum PacketType {
12    /// Baseband Frame (BBFRAME) — §5.2.1
13    BasebandFrame = 0x00,
14    /// Auxiliary stream I/Q data — §5.2.2
15    AuxiliaryIqData = 0x01,
16    /// Arbitrary cell insertion — §5.2.3
17    ArbitraryCellInsertion = 0x02,
18    /// L1-current signalling — §5.2.4
19    L1Current = 0x10,
20    /// L1-future signalling — §5.2.5
21    L1Future = 0x11,
22    /// P2 bias balancing cells — §5.2.6
23    P2BiasBalancing = 0x12,
24    /// DVB-T2 timestamp — §5.2.7
25    Timestamp = 0x20,
26    /// Individual addressing — §5.2.8
27    IndividualAddressing = 0x21,
28    /// FEF part: Null — §5.2.9
29    FefPartNull = 0x30,
30    /// FEF part: I/Q data — §5.2.10
31    FefPartIqData = 0x31,
32    /// FEF part: composite — §5.2.11
33    FefPartComposite = 0x32,
34    /// FEF sub-part — §5.2.12
35    FefSubPart = 0x33,
36}
37
38impl From<PacketType> for u8 {
39    fn from(pt: PacketType) -> Self {
40        pt as u8
41    }
42}
43
44impl From<num_enum::TryFromPrimitiveError<PacketType>> for crate::error::Error {
45    fn from(e: num_enum::TryFromPrimitiveError<PacketType>) -> Self {
46        crate::error::Error::InvalidPacketType { found: e.number }
47    }
48}
49
50/// T2-MI packet header (6 bytes / 48 bits) per §5.1.
51///
52/// Layout:
53/// - byte 0: packet_type (8 bits) — Table 1
54/// - byte 1: packet_count (8 bits) — wraps 0xFF→0x00, arbitrary start
55/// - byte 2 `[7:4]`: superframe_idx (4 bits)
56/// - byte 2 `[3]`: rfu (1 bit) — must be 0
57/// - byte 2 `[2:0]`: t2mi_stream_id (3 bits)
58/// - byte 3: rfu (8 bits) — must be 0
59/// - byte 4-5: payload_len (16 bits, unit = bits)
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize))]
62pub struct Header {
63    /// Table 1 packet type.
64    pub packet_type: PacketType,
65    /// Wraps 0xFF→0x00; arbitrary start value.
66    pub packet_count: u8,
67    /// Super-frame index [0..=15].
68    pub superframe_idx: u8,
69    /// T2-MI stream ID [0..=7].
70    pub t2mi_stream_id: u8,
71    /// Payload length in bits (not bytes).
72    pub payload_len_bits: u16,
73}
74
75impl<'a> dvb_common::Parse<'a> for Header {
76    type Error = crate::error::Error;
77
78    fn parse(bytes: &'a [u8]) -> Result<Self, crate::error::Error> {
79        use super::error::Error;
80
81        let len = bytes.len();
82        if len < 6 {
83            return Err(Error::BufferTooShort {
84                need: 6,
85                have: len,
86                what: "T2MI Header",
87            });
88        }
89
90        let packet_type = PacketType::try_from(bytes[0])?;
91        let packet_count = bytes[1];
92
93        // byte 2 [7:4] = superframe_idx
94        let superframe_idx = (bytes[2] >> 4) & 0x0F;
95
96        // byte 2 [3] = rfu — spec says must be 0
97        if bytes[2] & 0x08 != 0 {
98            return Err(Error::ReservedBitsViolation {
99                field: "byte 2 bit 3",
100                reason: "RFU must be zero (ETSI TS 102 773 §5.1)",
101            });
102        }
103
104        // byte 2 [2:0] = t2mi_stream_id
105        let t2mi_stream_id = bytes[2] & 0x07;
106
107        // byte 3 = rfu — spec says must be 0
108        if bytes[3] != 0 {
109            return Err(Error::ReservedBitsViolation {
110                field: "byte 3",
111                reason: "All 8 RFU bits must be zero (ETSI TS 102 773 §5.1)",
112            });
113        }
114
115        let payload_len_bits = u16::from_be_bytes([bytes[4], bytes[5]]);
116
117        Ok(Header {
118            packet_type,
119            packet_count,
120            superframe_idx,
121            t2mi_stream_id,
122            payload_len_bits,
123        })
124    }
125}
126
127impl Header {
128    /// Compute payload length in bytes (ceil(bits / 8)).
129    #[must_use]
130    pub fn payload_len_bytes(&self) -> usize {
131        (self.payload_len_bits as usize).div_ceil(8)
132    }
133
134    /// Total packet size including header + payload + CRC-32 trailer, in bytes.
135    ///
136    /// = 6 (header) + ceil(payload_len_bits / 8) (payload + padding) + 4 (CRC).
137    #[must_use]
138    pub fn total_bytes(&self) -> usize {
139        6 + self.payload_len_bytes() + super::crc::CRC_LEN
140    }
141
142    /// Slice the payload bytes out of the full packet buffer this header was
143    /// parsed from (the 6 header bytes, then `payload_len_bytes()` of payload,
144    /// then the 4-byte CRC trailer).
145    ///
146    /// Errors with [`Error::PayloadLengthMismatch`](crate::error::Error::PayloadLengthMismatch) when the buffer is too
147    /// short for the declared `payload_len_bits`.
148    pub fn payload_bytes<'a>(&self, packet: &'a [u8]) -> Result<&'a [u8], crate::error::Error> {
149        let end = 6 + self.payload_len_bytes();
150        if packet.len() < end {
151            return Err(crate::error::Error::PayloadLengthMismatch {
152                declared_bits: self.payload_len_bits,
153                remaining_bytes: packet.len().saturating_sub(6),
154            });
155        }
156        Ok(&packet[6..end])
157    }
158}
159
160impl dvb_common::Serialize for Header {
161    type Error = crate::error::Error;
162
163    fn serialized_len(&self) -> usize {
164        6
165    }
166
167    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize, crate::error::Error> {
168        use super::error::Error;
169
170        if buf.len() < 6 {
171            return Err(Error::OutputBufferTooSmall {
172                need: 6,
173                have: buf.len(),
174            });
175        }
176
177        if self.t2mi_stream_id > 7 {
178            return Err(Error::ReservedBitsViolation {
179                field: "t2mi_stream_id",
180                reason: "Must be in range 0..=7 (3-bit field)",
181            });
182        }
183
184        buf[0] = self.packet_type.into();
185        buf[1] = self.packet_count;
186        buf[2] = (self.superframe_idx & 0x0F) << 4 | (self.t2mi_stream_id & 0x07);
187        buf[3] = 0; // RFU = 0
188        let len_be = self.payload_len_bits.to_be_bytes();
189        buf[4] = len_be[0];
190        buf[5] = len_be[1];
191
192        Ok(6)
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199    use dvb_common::{Parse, Serialize};
200
201    #[test]
202    fn packet_type_try_from_all_valid() {
203        // Table 1 — every defined type must round-trip
204        let valid_types = [
205            0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x20, 0x21, 0x30, 0x31, 0x32, 0x33,
206        ];
207        for v in valid_types {
208            let result = PacketType::try_from(v);
209            assert!(result.is_ok(), "PacketType::try_from({:#04x}) failed", v);
210        }
211    }
212
213    #[test]
214    fn packet_type_rejects_reserved() {
215        // 0x22..=0x2F and 0x34..=0xFF are RFU
216        for v in 0x22..=0x2F {
217            assert!(
218                PacketType::try_from(v).is_err(),
219                "0x{v:02x} should be rejected"
220            );
221        }
222        for v in 0x34..=0xFF {
223            assert!(
224                PacketType::try_from(v).is_err(),
225                "0x{v:02x} should be rejected"
226            );
227        }
228    }
229
230    #[test]
231    fn exhaustive_byte_sweep() {
232        let mut matched = 0u16;
233        for byte in 0u8..=0xFF {
234            if let Ok(v) = PacketType::try_from(byte) {
235                assert_eq!(v as u8, byte, "round-trip failed for {byte:#04x}");
236                matched += 1;
237            }
238        }
239        assert_eq!(matched, 12, "expected 12 matched variants");
240    }
241
242    #[test]
243    fn parse_rejects_buffer_shorter_than_6() {
244        let buf = [0x00u8; 5];
245        let result = Header::parse(&buf);
246        assert!(result.is_err());
247        let err = result.unwrap_err();
248        if let crate::Error::BufferTooShort { need, have, what } = err {
249            assert_eq!(need, 6);
250            assert_eq!(have, 5);
251            assert_eq!(what, "T2MI Header");
252        } else {
253            panic!("Expected BufferTooShort, got {:?}", err);
254        }
255    }
256
257    #[test]
258    fn parse_extracts_packet_type_and_count() {
259        let buf = [0x10u8, 0xAB, 0x00, 0x00, 0x00, 0x08];
260        let hdr = Header::parse(&buf).unwrap();
261        assert_eq!(hdr.packet_type, PacketType::L1Current);
262        assert_eq!(hdr.packet_count, 0xAB);
263    }
264
265    #[test]
266    fn parse_extracts_superframe_idx() {
267        let buf = [0x00u8, 0x00, 0x50, 0x00, 0x00, 0x08];
268        let hdr = Header::parse(&buf).unwrap();
269        assert_eq!(hdr.superframe_idx, 5);
270    }
271
272    #[test]
273    fn parse_accepts_all_defined_packet_types() {
274        let types = [
275            0x00u8, 0x01, 0x02, 0x10, 0x11, 0x12, 0x20, 0x21, 0x30, 0x31, 0x32, 0x33,
276        ];
277        for &t in &types {
278            let buf = [t, 0x00, 0x00, 0x00, 0x00, 0x08];
279            let result = Header::parse(&buf);
280            assert!(
281                result.is_ok(),
282                "parse failed for packet_type {:#04x}: {:?}",
283                t,
284                result
285            );
286            assert_eq!(
287                result.unwrap().packet_type,
288                PacketType::try_from(t).unwrap()
289            );
290        }
291    }
292
293    #[test]
294    fn parse_rejects_reserved_packet_type_0x22() {
295        let buf = [0x22u8, 0x00, 0x00, 0x00, 0x00, 0x08];
296        let result = Header::parse(&buf);
297        assert!(result.is_err());
298        assert!(matches!(
299            result.unwrap_err(),
300            crate::Error::InvalidPacketType { found: 0x22 }
301        ));
302    }
303
304    #[test]
305    fn parse_extracts_t2mi_stream_id_0_through_7() {
306        for id in 0..=7 {
307            let buf = [0x00u8, 0x00, id, 0x00, 0x00, 0x08];
308            let hdr = Header::parse(&buf).unwrap();
309            assert_eq!(hdr.t2mi_stream_id, id, "stream_id mismatch for id={}", id);
310        }
311    }
312
313    #[test]
314    fn parse_rejects_nonzero_rfu_bits_in_byte2() {
315        // byte 2 bit 3 (0x08) is rfu — must be 0
316        let buf = [0x00u8, 0x00, 0x08, 0x00, 0x00, 0x08];
317        let result = Header::parse(&buf);
318        assert!(result.is_err());
319        assert!(matches!(
320            result.unwrap_err(),
321            crate::Error::ReservedBitsViolation { .. }
322        ));
323    }
324
325    #[test]
326    fn parse_rejects_nonzero_byte3() {
327        let buf = [0x00u8, 0x00, 0x00, 0x01, 0x00, 0x08];
328        let result = Header::parse(&buf);
329        assert!(result.is_err());
330    }
331
332    #[test]
333    fn parse_extracts_payload_len_bits() {
334        let buf = [0x00u8, 0x00, 0x00, 0x00, 0x01, 0x00];
335        let hdr = Header::parse(&buf).unwrap();
336        assert_eq!(hdr.payload_len_bits, 0x0100);
337    }
338
339    #[test]
340    fn payload_len_bytes_rounds_up() {
341        let hdr = Header {
342            packet_type: PacketType::BasebandFrame,
343            packet_count: 0,
344            superframe_idx: 0,
345            t2mi_stream_id: 0,
346            payload_len_bits: 13,
347        };
348        assert_eq!(hdr.payload_len_bytes(), 2); // ceil(13/8) = 2
349
350        let hdr2 = Header {
351            payload_len_bits: 16,
352            ..hdr
353        };
354        assert_eq!(hdr2.payload_len_bytes(), 2);
355
356        let hdr3 = Header {
357            payload_len_bits: 0,
358            ..hdr
359        };
360        assert_eq!(hdr3.payload_len_bytes(), 0);
361    }
362
363    // ── Serialize tests ──
364
365    #[test]
366    fn serialize_writes_6_bytes() {
367        let hdr = Header {
368            packet_type: PacketType::BasebandFrame,
369            packet_count: 42,
370            superframe_idx: 7,
371            t2mi_stream_id: 3,
372            payload_len_bits: 128,
373        };
374        let mut buf = [0u8; 256];
375        let written = hdr.serialize_into(&mut buf).unwrap();
376        assert_eq!(written, 6);
377        assert_eq!(buf[0], 0x00);
378        assert_eq!(buf[1], 42);
379        assert_eq!(buf[2], (7 << 4) | 3);
380        assert_eq!(buf[3], 0);
381        assert_eq!(buf[4], 0);
382        assert_eq!(buf[5], 128);
383    }
384
385    #[test]
386    fn serialize_round_trip_identity_for_every_packet_type() {
387        let types = [
388            0x00u8, 0x01, 0x02, 0x10, 0x11, 0x12, 0x20, 0x21, 0x30, 0x31, 0x32, 0x33,
389        ];
390        for &t in &types {
391            let original = Header {
392                packet_type: PacketType::try_from(t).unwrap(),
393                packet_count: 13,
394                superframe_idx: 7,
395                t2mi_stream_id: 2,
396                payload_len_bits: 512,
397            };
398            let mut buf = [0u8; 6];
399            original.serialize_into(&mut buf).unwrap();
400            let parsed = Header::parse(&buf).unwrap();
401            assert_eq!(original, parsed, "Round-trip failed for type {:#04x}", t);
402        }
403    }
404
405    #[test]
406    fn serialize_rejects_too_small_buffer() {
407        let hdr = Header {
408            packet_type: PacketType::BasebandFrame,
409            packet_count: 0,
410            superframe_idx: 0,
411            t2mi_stream_id: 0,
412            payload_len_bits: 0,
413        };
414        let mut buf = [0u8; 5];
415        let result = hdr.serialize_into(&mut buf);
416        assert!(result.is_err());
417    }
418
419    #[test]
420    fn serialize_rejects_t2mi_stream_id_above_7() {
421        let hdr = Header {
422            packet_type: PacketType::BasebandFrame,
423            packet_count: 0,
424            superframe_idx: 0,
425            t2mi_stream_id: 8,
426            payload_len_bits: 0,
427        };
428        let mut buf = [0u8; 6];
429        let result = hdr.serialize_into(&mut buf);
430        assert!(result.is_err());
431    }
432
433    #[test]
434    fn payload_bytes_slices_declared_payload() {
435        // payload_len_bits = 24 → 3 payload bytes after the 6-byte header.
436        let buf = [
437            0x00, 0x01, 0x10, 0x00, 0x00, 0x18, 0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x00, 0x00,
438        ];
439        let hdr = Header::parse(&buf).unwrap();
440        assert_eq!(hdr.payload_bytes(&buf).unwrap(), &[0xAA, 0xBB, 0xCC]);
441    }
442
443    #[test]
444    fn payload_bytes_rejects_truncated_buffer() {
445        // Declares 3 payload bytes but only 1 follows the header.
446        let buf = [0x00, 0x01, 0x10, 0x00, 0x00, 0x18, 0xAA];
447        let hdr = Header::parse(&buf).unwrap();
448        assert!(matches!(
449            hdr.payload_bytes(&buf),
450            Err(crate::Error::PayloadLengthMismatch { .. })
451        ));
452    }
453}