dvb-si 12.0.1

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Transport Protocol Descriptor — ETSI TS 102 809 §5.3.6.1, Table 28
//! (AIT tag 0x02).
//!
//! Carried in the AIT descriptor loops. Identifies the transport mechanism
//! for an application via `protocol_id` + `transport_protocol_label` + selector
//! bytes. The selector bytes are interpreted per `protocol_id`:
//!
//! - 0x0001: Object Carousel (Table 31)
//! - 0x0003: Interaction / HTTP (Table 32)
//! - other: Unknown (raw bytes preserved)

use crate::descriptors::descriptor_body;
use crate::error::{Error, Result};
use alloc::vec::Vec;
use broadcast_common::{Parse, Serialize};

/// Descriptor tag for transport_protocol_descriptor (AIT namespace).
pub const TAG: u8 = 0x02;
const HEADER_LEN: usize = 2;
const PROTOCOL_ID_LEN: usize = 2;
const LABEL_LEN: usize = 1;

/// Protocol ID for Object Carousel — ETSI TS 102 809 Table 29.
pub const PROTOCOL_ID_OBJECT_CAROUSEL: u16 = 0x0001;
/// Protocol ID for HTTP interaction channel — ETSI TS 102 809 Table 29.
pub const PROTOCOL_ID_HTTP: u16 = 0x0003;

/// Decoded Object Carousel selector — Table 31.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct OcSelector {
    /// 1-bit remote_connection flag.
    pub remote_connection: bool,
    /// Present only when `remote_connection` is true.
    pub remote_connection_info: Option<OcRemoteConnection>,
    /// Component tag.
    pub component_tag: u8,
}

/// Remote connection fields inside the OC selector (Table 31).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct OcRemoteConnection {
    /// 16-bit original_network_id.
    pub original_network_id: u16,
    /// 16-bit transport_stream_id.
    pub transport_stream_id: u16,
    /// 16-bit service_id.
    pub service_id: u16,
}

/// One URL entry in the HTTP interaction selector — Table 32.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct HttpUrlEntry<'a> {
    /// URL base bytes.
    pub url_base: &'a [u8],
    /// URL extension strings.
    pub url_extensions: Vec<&'a [u8]>,
}

/// Decoded HTTP interaction selector — Table 32.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct HttpSelector<'a> {
    /// URL entries.
    pub urls: Vec<HttpUrlEntry<'a>>,
}

/// Typed selector decoded from the raw selector bytes by `protocol_id`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum SelectorKind<'a> {
    /// Object Carousel selector (protocol_id 0x0001).
    ObjectCarousel(OcSelector),
    /// HTTP interaction selector (protocol_id 0x0003).
    Http(HttpSelector<'a>),
    /// Unknown protocol — raw selector bytes.
    Unknown(&'a [u8]),
}

impl SelectorKind<'_> {
    /// Spec name for the selector variant.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::ObjectCarousel(_) => "OBJECT_CAROUSEL",
            Self::Http(_) => "HTTP",
            Self::Unknown(_) => "UNKNOWN",
        }
    }
}
broadcast_common::impl_spec_display!(SelectorKind<'_>);

/// Transport Protocol Descriptor (AIT tag 0x02).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct TransportProtocolDescriptor<'a> {
    /// 16-bit protocol_id (Table 29).
    pub protocol_id: u16,
    /// Transport protocol label.
    pub transport_protocol_label: u8,
    /// Raw selector bytes (after protocol_id + label). Use `selector()`
    /// for typed decoding.
    #[cfg_attr(feature = "serde", serde(borrow))]
    pub selector_bytes: &'a [u8],
}

impl<'a> TransportProtocolDescriptor<'a> {
    /// Decode the selector bytes according to `protocol_id`.
    #[must_use]
    pub fn selector(&self) -> SelectorKind<'a> {
        match self.protocol_id {
            PROTOCOL_ID_OBJECT_CAROUSEL => self.decode_oc_selector(),
            PROTOCOL_ID_HTTP => self.decode_http_selector(),
            _ => SelectorKind::Unknown(self.selector_bytes),
        }
    }

    fn decode_oc_selector(&self) -> SelectorKind<'a> {
        let b = self.selector_bytes;
        if b.len() < 2 {
            return SelectorKind::Unknown(b);
        }
        let remote_connection = (b[0] & 0x80) != 0;
        let mut pos = 1;
        let remote_connection_info = if remote_connection {
            if pos + 6 > b.len() {
                return SelectorKind::Unknown(b);
            }
            let info = OcRemoteConnection {
                original_network_id: u16::from_be_bytes([b[pos], b[pos + 1]]),
                transport_stream_id: u16::from_be_bytes([b[pos + 2], b[pos + 3]]),
                service_id: u16::from_be_bytes([b[pos + 4], b[pos + 5]]),
            };
            pos += 6;
            Some(info)
        } else {
            None
        };
        // component_tag must still be present; a malformed selector that
        // declared remote_connection but stops before the component_tag
        // (e.g. exactly 7 bytes) falls back to Unknown rather than panicking.
        if pos >= b.len() {
            return SelectorKind::Unknown(b);
        }
        let component_tag = b[pos];
        SelectorKind::ObjectCarousel(OcSelector {
            remote_connection,
            remote_connection_info,
            component_tag,
        })
    }

    /// Decode an HTTP selector (EN 300 468 §6.4.7 `url_base_length` /
    /// `url_extension_count`).
    ///
    /// A malformed selector falls back to [`SelectorKind::Unknown`] — the same
    /// policy as [`Self::decode_oc_selector`]. It used to `break` out of the
    /// loop and still report `Http` with the URLs read so far, so a truncated
    /// selector was indistinguishable from a complete one (r03-W14).
    fn decode_http_selector(&self) -> SelectorKind<'a> {
        let b = self.selector_bytes;
        let mut urls = Vec::new();
        let mut pos = 0;
        while pos < b.len() {
            let url_base_length = b[pos] as usize;
            pos += 1;
            let Some(base_end) = pos.checked_add(url_base_length) else {
                return SelectorKind::Unknown(b);
            };
            if base_end > b.len() {
                return SelectorKind::Unknown(b);
            }
            let url_base = &b[pos..base_end];
            pos = base_end;
            if pos == b.len() {
                urls.push(HttpUrlEntry {
                    url_base,
                    url_extensions: Vec::new(),
                });
                break;
            }
            let url_extension_count = b[pos] as usize;
            pos += 1;
            let mut url_extensions = Vec::with_capacity(url_extension_count);
            for _ in 0..url_extension_count {
                let Some(ext_len) = b.get(pos).map(|&v| usize::from(v)) else {
                    return SelectorKind::Unknown(b);
                };
                pos += 1;
                let Some(ext_end) = pos.checked_add(ext_len) else {
                    return SelectorKind::Unknown(b);
                };
                if ext_end > b.len() {
                    return SelectorKind::Unknown(b);
                }
                url_extensions.push(&b[pos..ext_end]);
                pos = ext_end;
            }
            urls.push(HttpUrlEntry {
                url_base,
                url_extensions,
            });
        }
        SelectorKind::Http(HttpSelector { urls })
    }
}

impl<'a> Parse<'a> for TransportProtocolDescriptor<'a> {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let body = descriptor_body(
            bytes,
            TAG,
            "TransportProtocolDescriptor",
            "unexpected tag for transport_protocol_descriptor",
        )?;
        if body.len() < PROTOCOL_ID_LEN + LABEL_LEN {
            return Err(Error::InvalidDescriptor {
                tag: TAG,
                reason: "transport_protocol_descriptor body shorter than minimum 3 bytes",
            });
        }
        let protocol_id = u16::from_be_bytes([body[0], body[1]]);
        let transport_protocol_label = body[2];
        let selector_bytes = &body[PROTOCOL_ID_LEN + LABEL_LEN..];
        Ok(Self {
            protocol_id,
            transport_protocol_label,
            selector_bytes,
        })
    }
}

impl Serialize for TransportProtocolDescriptor<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        HEADER_LEN + PROTOCOL_ID_LEN + LABEL_LEN + self.selector_bytes.len()
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        let body_len = len - HEADER_LEN;
        if body_len > u8::MAX as usize {
            return Err(Error::InvalidDescriptor {
                tag: TAG,
                reason: "transport_protocol_descriptor body exceeds 255 bytes",
            });
        }
        if buf.len() < len {
            return Err(Error::OutputBufferTooSmall {
                need: len,
                have: buf.len(),
            });
        }
        crate::descriptors::write_descriptor_header(buf, TAG, body_len)?;
        buf[2..4].copy_from_slice(&self.protocol_id.to_be_bytes());
        buf[4] = self.transport_protocol_label;
        buf[5..5 + self.selector_bytes.len()].copy_from_slice(self.selector_bytes);
        Ok(len)
    }
}

impl<'a> crate::traits::DescriptorDef<'a> for TransportProtocolDescriptor<'a> {
    const TAG: u8 = TAG;
    const NAME: &'static str = "TRANSPORT_PROTOCOL";
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Body: protocol_id(2) + label(1) + remote_conn_rfu(1) + component_tag(1) = 5.
    fn build_oc_local() -> [u8; 7] {
        [TAG, 5, 0x00, 0x01, 0x01, 0x00, 0xB4]
    }

    /// Body: protocol_id(2) + label(1) + remote_conn(1) + org_id(2) + ts_id(2) + service_id(2) + component_tag(1) = 11.
    fn build_oc_remote() -> [u8; 13] {
        [
            TAG, 11, 0x00, 0x01, 0x01, 0x80, // remote_connection=1, rfu=0000000
            0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x04, // component_tag
        ]
    }

    /// Body: protocol_id(2) + label(1) + url_base_len(1) + "http"(4) + ext_count(1) + ext_len(1) + "/app"(4) = 14.
    fn build_http() -> [u8; 16] {
        [
            TAG, 14, 0x00, 0x03, 0x01, 4, b'h', b't', b't', b'p', 1, // url_extension_count
            4, b'/', b'a', b'p', b'p',
        ]
    }

    #[test]
    fn parse_oc_local_selector() {
        let bytes = build_oc_local();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.protocol_id, PROTOCOL_ID_OBJECT_CAROUSEL);
        assert_eq!(d.transport_protocol_label, 0x01);
        assert_eq!(d.selector_bytes, &[0x00, 0xB4]);
        match d.selector() {
            SelectorKind::ObjectCarousel(oc) => {
                assert!(!oc.remote_connection);
                assert!(oc.remote_connection_info.is_none());
                assert_eq!(oc.component_tag, 0xB4);
            }
            other => panic!("expected ObjectCarousel, got {other:?}"),
        }
    }

    #[test]
    fn parse_oc_remote_selector() {
        let bytes = build_oc_remote();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        match d.selector() {
            SelectorKind::ObjectCarousel(oc) => {
                assert!(oc.remote_connection);
                let rc = oc.remote_connection_info.unwrap();
                assert_eq!(rc.original_network_id, 1);
                assert_eq!(rc.transport_stream_id, 2);
                assert_eq!(rc.service_id, 3);
                assert_eq!(oc.component_tag, 0x04);
            }
            other => panic!("expected ObjectCarousel, got {other:?}"),
        }
    }

    #[test]
    fn parse_http_selector() {
        let bytes = build_http();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.protocol_id, PROTOCOL_ID_HTTP);
        match d.selector() {
            SelectorKind::Http(http) => {
                assert_eq!(http.urls.len(), 1);
                assert_eq!(http.urls[0].url_base, b"http");
                assert_eq!(http.urls[0].url_extensions.len(), 1);
                assert_eq!(http.urls[0].url_extensions[0], b"/app");
            }
            other => panic!("expected Http, got {other:?}"),
        }
    }

    #[test]
    fn oc_remote_selector_missing_component_tag_is_unknown_not_panic() {
        // protocol_id=0x0001, label, then a 7-byte selector that declares
        // remote_connection=1 (+6 bytes of triplet) but omits component_tag.
        // selector() must return Unknown, not panic.
        let bytes = [
            TAG, 10, // body = 10
            0x00, 0x01, // protocol_id = 0x0001 (OC)
            0x01, // transport_protocol_label
            0x80, // remote_connection=1
            0x00, 0x01, 0x00, 0x02, 0x00, 0x03, // 6-byte triplet, then NO component_tag
        ];
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.selector_bytes.len(), 7); // flag(1) + triplet(6), component_tag missing
        assert!(d.selector_bytes[0] & 0x80 != 0); // remote_connection set
        assert!(matches!(d.selector(), SelectorKind::Unknown(_)));
    }

    #[test]
    fn parse_unknown_protocol() {
        let bytes = [TAG, 5, 0x01, 0x00, 0x02, 0xCA, 0xFE];
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.protocol_id, 0x0100);
        assert!(matches!(d.selector(), SelectorKind::Unknown(_)));
    }

    #[test]
    fn serialize_round_trip_oc() {
        let bytes = build_oc_local();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        assert_eq!(buf.as_slice(), &bytes[..]);
        let re = TransportProtocolDescriptor::parse(&buf).unwrap();
        assert_eq!(d, re);
    }

    #[test]
    fn serialize_round_trip_http() {
        let bytes = build_http();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        assert_eq!(buf.as_slice(), &bytes[..]);
        let re = TransportProtocolDescriptor::parse(&buf).unwrap();
        assert_eq!(d, re);
    }

    #[test]
    fn serialize_round_trip_oc_remote() {
        let bytes = build_oc_remote();
        let d = TransportProtocolDescriptor::parse(&bytes).unwrap();
        let mut buf = vec![0u8; d.serialized_len()];
        d.serialize_into(&mut buf).unwrap();
        assert_eq!(buf.as_slice(), &bytes[..]);
    }

    #[test]
    fn malformed_http_selector_falls_back_to_unknown() {
        // A truncated url_base used to `break` out of the loop and still
        // report `Http` with the URLs read so far (r03-W14).
        // url_base_length = 20 but only 4 bytes follow.
        bytes_fall_back_to_unknown(&[TAG, 6, 0x00, 0x03, 0x01, 20, b'h', b't', b't', b'p']);
    }

    #[test]
    fn truncated_http_extension_falls_back_to_unknown() {
        // url_extension_count = 2 but the selector ends after one extension's
        // length byte (no ext_len byte at all).
        bytes_fall_back_to_unknown(&[
            TAG, 10, 0x00, 0x03, 0x01, 4, b'h', b't', b't', b'p', 2, 4, b'/', b'a',
        ]);
        // ext_len = 4 but only 1 extension byte follows.
        bytes_fall_back_to_unknown(&[
            TAG, 9, 0x00, 0x03, 0x01, 4, b'h', b't', b't', b'p', 2, 4, b'/',
        ]);
    }

    fn bytes_fall_back_to_unknown(bytes: &[u8]) {
        let d = TransportProtocolDescriptor::parse(bytes).unwrap();
        assert!(
            matches!(d.selector(), SelectorKind::Unknown(_)),
            "expected Unknown for a truncated HTTP selector, got {:?}",
            d.selector()
        );
    }
}