dvb-si 6.1.0

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
//! Network Change Notify Descriptor — ETSI EN 300 468 §6.4.9 (tag_extension 0x07).
use super::*;

const CELL_HEADER_LEN: usize = 2; // cell_id(16)
const LOOP_LENGTH_LEN: usize = 1; // loop_length(8)
const CHANGE_BASE_LEN: usize = 12; // id(1)+ver(1)+start(5)+dur(3)+packed(1)+msg(1)
const INVARIANT_TS_LEN: usize = 4; // tsid(16)+onid(16)

/// network_change_notify body (Table 149, §6.4.9). The two-level cell/change loop is unfolded.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct NetworkChangeNotify {
    /// Per-cell change lists.
    pub cells: Vec<NetworkChangeCell>,
}

/// A cell in the network_change_notify outer loop.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct NetworkChangeCell {
    /// cell_id(16).
    pub cell_id: u16,
    /// The cell's change entries.
    pub changes: Vec<NetworkChange>,
}

/// A change entry in the network_change_notify inner loop.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct NetworkChange {
    /// network_change_id(8).
    pub network_change_id: u8,
    /// network_change_version(8).
    pub network_change_version: u8,
    /// start_time_of_change(40) — raw 40-bit value (MJD date + UTC BCD time), big-endian.
    pub start_time_of_change: u64,
    /// change_duration(24) — raw 24-bit BCD value, big-endian.
    pub change_duration: u32,
    /// receiver_category(3).
    pub receiver_category: u8,
    /// change_type(4).
    pub change_type: u8,
    /// message_id(8).
    pub message_id: u8,
    /// invariant_ts (tsid, onid), present iff invariant_ts_present==1.
    pub invariant_ts: Option<InvariantTs>,
}

/// Conditional invariant-TS fields in a network_change_notify entry.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct InvariantTs {
    /// invariant_ts_tsid(16).
    pub tsid: u16,
    /// invariant_ts_onid(16).
    pub onid: u16,
}

impl<'a> ExtensionBodyDef<'a> for NetworkChangeNotify {
    const TAG_EXTENSION: u8 = 0x07;
    const NAME: &'static str = "NETWORK_CHANGE_NOTIFY";
}

impl NetworkChange {
    /// Decode `start_time_of_change` (40-bit MJD+BCD UTC) to a
    /// [`chrono::DateTime<chrono::Utc>`]. `None` if the date/time fields
    /// are out of range. Reuses the same helper as EIT/TOT/TDT.
    #[cfg(feature = "chrono")]
    #[must_use]
    pub fn start_time_of_change_utc(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        let raw = self.start_time_of_change;
        let bytes = [
            (raw >> 32) as u8,
            (raw >> 24) as u8,
            (raw >> 16) as u8,
            (raw >> 8) as u8,
            raw as u8,
        ];
        dvb_common::time::decode_mjd_bcd_utc(bytes)
    }

    /// Decode `change_duration` (24-bit BCD `HHMMSS`) to seconds.
    /// `None` if the BCD nibbles are out of range.
    #[cfg(feature = "chrono")]
    #[must_use]
    pub fn change_duration_secs(&self) -> Option<u32> {
        let raw = self.change_duration;
        let bytes = [(raw >> 16) as u8, (raw >> 8) as u8, raw as u8];
        dvb_common::time::decode_bcd_duration(bytes).map(|d| d.as_secs() as u32)
    }
}

fn change_serialized_len(ch: &NetworkChange) -> usize {
    CHANGE_BASE_LEN
        + if ch.invariant_ts.is_some() {
            INVARIANT_TS_LEN
        } else {
            0
        }
}

impl<'a> Parse<'a> for NetworkChangeNotify {
    type Error = crate::error::Error;
    fn parse(sel: &'a [u8]) -> Result<Self> {
        let mut cells = Vec::new();
        let mut pos = 0;
        while pos < sel.len() {
            if pos + CELL_HEADER_LEN + LOOP_LENGTH_LEN > sel.len() {
                return Err(Error::BufferTooShort {
                    need: pos + CELL_HEADER_LEN + LOOP_LENGTH_LEN,
                    have: sel.len(),
                    what: "network_change_notify body",
                });
            }
            let cell_id = u16::from_be_bytes([sel[pos], sel[pos + 1]]);
            let loop_length = sel[pos + CELL_HEADER_LEN] as usize;
            pos += CELL_HEADER_LEN + LOOP_LENGTH_LEN;

            if pos + loop_length > sel.len() {
                return Err(Error::BufferTooShort {
                    need: pos + loop_length,
                    have: sel.len(),
                    what: "network_change_notify body",
                });
            }

            let inner_end = pos + loop_length;
            let mut changes = Vec::new();
            while pos < inner_end {
                let remaining = inner_end - pos;
                // At minimum we need CHANGE_BASE_LEN bytes for a basic entry.
                if remaining < CHANGE_BASE_LEN {
                    return Err(Error::BufferTooShort {
                        need: inner_end - remaining + CHANGE_BASE_LEN,
                        have: sel.len(),
                        what: "network_change_notify body",
                    });
                }
                let network_change_id = sel[pos];
                let network_change_version = sel[pos + 1];
                let start_time_of_change = (u64::from(sel[pos + 2]) << 32)
                    | (u64::from(sel[pos + 3]) << 24)
                    | (u64::from(sel[pos + 4]) << 16)
                    | (u64::from(sel[pos + 5]) << 8)
                    | u64::from(sel[pos + 6]);
                let change_duration = (u32::from(sel[pos + 7]) << 16)
                    | (u32::from(sel[pos + 8]) << 8)
                    | u32::from(sel[pos + 9]);
                let packed = sel[pos + 10];
                let receiver_category = packed >> 5;
                let invariant_ts_present = (packed >> 4) & 1;
                let change_type = packed & 0x0F;
                let message_id = sel[pos + 11];
                pos += CHANGE_BASE_LEN;

                let invariant_ts = if invariant_ts_present == 1 {
                    if pos + INVARIANT_TS_LEN > inner_end {
                        return Err(Error::BufferTooShort {
                            need: pos + INVARIANT_TS_LEN,
                            have: sel.len(),
                            what: "network_change_notify body",
                        });
                    }
                    let ts = InvariantTs {
                        tsid: u16::from_be_bytes([sel[pos], sel[pos + 1]]),
                        onid: u16::from_be_bytes([sel[pos + 2], sel[pos + 3]]),
                    };
                    pos += INVARIANT_TS_LEN;
                    Some(ts)
                } else {
                    None
                };

                changes.push(NetworkChange {
                    network_change_id,
                    network_change_version,
                    start_time_of_change,
                    change_duration,
                    receiver_category,
                    change_type,
                    message_id,
                    invariant_ts,
                });
            }

            if pos != inner_end {
                return Err(invalid("network_change_notify: change entry overruns loop"));
            }

            cells.push(NetworkChangeCell { cell_id, changes });
        }
        Ok(NetworkChangeNotify { cells })
    }
}

impl Serialize for NetworkChangeNotify {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        self.cells
            .iter()
            .map(|cell| {
                CELL_HEADER_LEN
                    + LOOP_LENGTH_LEN
                    + cell
                        .changes
                        .iter()
                        .map(change_serialized_len)
                        .sum::<usize>()
            })
            .sum()
    }
    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        if buf.len() < len {
            return Err(Error::OutputBufferTooSmall {
                need: len,
                have: buf.len(),
            });
        }
        let mut pos = 0;
        for cell in &self.cells {
            buf[pos..pos + 2].copy_from_slice(&cell.cell_id.to_be_bytes());
            pos += 2;
            let loop_length: usize = cell.changes.iter().map(change_serialized_len).sum();
            buf[pos] = loop_length as u8;
            pos += 1;
            for ch in &cell.changes {
                buf[pos] = ch.network_change_id;
                buf[pos + 1] = ch.network_change_version;
                let st = ch.start_time_of_change;
                buf[pos + 2] = (st >> 32) as u8;
                buf[pos + 3] = (st >> 24) as u8;
                buf[pos + 4] = (st >> 16) as u8;
                buf[pos + 5] = (st >> 8) as u8;
                buf[pos + 6] = st as u8;
                let dur = ch.change_duration;
                buf[pos + 7] = (dur >> 16) as u8;
                buf[pos + 8] = (dur >> 8) as u8;
                buf[pos + 9] = dur as u8;
                let packed = ((ch.receiver_category & 0x07) << 5)
                    | ((ch.invariant_ts.is_some() as u8) << 4)
                    | (ch.change_type & 0x0F);
                buf[pos + 10] = packed;
                buf[pos + 11] = ch.message_id;
                pos += CHANGE_BASE_LEN;
                if let Some(ref inv) = ch.invariant_ts {
                    buf[pos..pos + 2].copy_from_slice(&inv.tsid.to_be_bytes());
                    buf[pos + 2..pos + 4].copy_from_slice(&inv.onid.to_be_bytes());
                    pos += INVARIANT_TS_LEN;
                }
            }
        }
        Ok(len)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::descriptors::extension::test_support::*;
    use crate::descriptors::extension::{ExtensionBody, ExtensionDescriptor, ExtensionTag};

    #[test]
    fn parse_network_change_notify_structured() {
        // 2 cells: one with 0 changes, one with 2 changes (one without
        // invariant_ts, one with).
        let sel = [
            // cell 1: cell_id=0x0001, loop_length=0
            0x00, 0x01, 0x00, // cell 2: cell_id=0x0002, loop_length=28 (0x1C)
            0x00, 0x02, 0x1C,
            //   change 1 (no invariant_ts): 12 bytes
            0x10, // network_change_id
            0x20, // network_change_version
            0x00, 0x00, 0x00, 0x00, 0x01, // start_time_of_change = 1
            0x00, 0x00, 0x01, // change_duration = 1
            0x23, // packed: rec_cat=1, inv_ts=0, change_type=3
            0x40, // message_id
            //   change 2 (with invariant_ts): 16 bytes
            0x30, // network_change_id
            0x40, // network_change_version
            0x00, 0x00, 0x00, 0x00, 0x02, // start_time_of_change = 2
            0x00, 0x00, 0x02, // change_duration = 2
            0x54, // packed: rec_cat=2, inv_ts=1, change_type=4
            0x50, // message_id
            0xAA, 0xAA, // tsid = 0xAAAA
            0xBB, 0xBB, // onid = 0xBBBB
        ];
        let bytes = wrap(0x07, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::NetworkChangeNotify(b) => {
                assert_eq!(b.cells.len(), 2);

                assert_eq!(b.cells[0].cell_id, 0x0001);
                assert!(b.cells[0].changes.is_empty());

                assert_eq!(b.cells[1].cell_id, 0x0002);
                assert_eq!(b.cells[1].changes.len(), 2);

                let ch0 = &b.cells[1].changes[0];
                assert_eq!(ch0.network_change_id, 0x10);
                assert_eq!(ch0.network_change_version, 0x20);
                assert_eq!(ch0.start_time_of_change, 1);
                assert_eq!(ch0.change_duration, 1);
                assert_eq!(ch0.receiver_category, 1);
                assert_eq!(ch0.change_type, 3);
                assert_eq!(ch0.message_id, 0x40);
                assert!(ch0.invariant_ts.is_none());

                let ch1 = &b.cells[1].changes[1];
                assert_eq!(ch1.network_change_id, 0x30);
                assert_eq!(ch1.network_change_version, 0x40);
                assert_eq!(ch1.start_time_of_change, 2);
                assert_eq!(ch1.change_duration, 2);
                assert_eq!(ch1.receiver_category, 2);
                assert_eq!(ch1.change_type, 4);
                assert_eq!(ch1.message_id, 0x50);
                let inv = ch1.invariant_ts.as_ref().unwrap();
                assert_eq!(inv.tsid, 0xAAAA);
                assert_eq!(inv.onid, 0xBBBB);
            }
            other => panic!("expected NetworkChangeNotify, got {other:?}"),
        }
        round_trip(&d);
    }

    /// Byte-exact cross-check against a TSDuck-compiled descriptor (tsduck-test
    /// test-015). Parse, assert decoded fields, assert kind(), then re-serialize
    /// and verify byte-exact match.
    #[test]
    fn network_change_notify_tsduck_byte_exact() {
        let bytes =
            from_hex("7f230712340056781cabcde5cc2312340852030281ef67e5e20234561132453b83deadbeef");
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        assert_eq!(d.kind(), Some(ExtensionTag::NetworkChangeNotify));

        match &d.body {
            ExtensionBody::NetworkChangeNotify(b) => {
                assert_eq!(b.cells.len(), 2);

                // cell 0: cell_id=0x1234, no changes
                assert_eq!(b.cells[0].cell_id, 0x1234);
                assert!(b.cells[0].changes.is_empty());

                // cell 1: cell_id=0x5678, 2 changes
                assert_eq!(b.cells[1].cell_id, 0x5678);
                assert_eq!(b.cells[1].changes.len(), 2);

                // change 0: no invariant_ts
                let ch0 = &b.cells[1].changes[0];
                assert_eq!(ch0.network_change_id, 0xAB);
                assert_eq!(ch0.network_change_version, 0xCD);
                assert_eq!(ch0.start_time_of_change, 0xE5CC231234);
                assert_eq!(ch0.change_duration, 0x085203);
                assert_eq!(ch0.receiver_category, 0);
                assert_eq!(ch0.change_type, 2);
                assert_eq!(ch0.message_id, 0x81);
                assert!(ch0.invariant_ts.is_none());

                // change 1: with invariant_ts
                let ch1 = &b.cells[1].changes[1];
                assert_eq!(ch1.network_change_id, 0xEF);
                assert_eq!(ch1.network_change_version, 0x67);
                assert_eq!(ch1.start_time_of_change, 0xE5E2023456);
                assert_eq!(ch1.change_duration, 0x113245);
                assert_eq!(ch1.receiver_category, 1);
                assert_eq!(ch1.change_type, 0xB);
                assert_eq!(ch1.message_id, 0x83);
                let inv = ch1.invariant_ts.as_ref().unwrap();
                assert_eq!(inv.tsid, 0xDEAD);
                assert_eq!(inv.onid, 0xBEEF);
            }
            other => panic!("expected NetworkChangeNotify, got {other:?}"),
        }

        let mut out = vec![0u8; d.serialized_len()];
        let n = d.serialize_into(&mut out).unwrap();
        assert_eq!(
            out[..n],
            bytes[..],
            "byte-exact re-serialize for TSDuck vector"
        );
    }

    #[cfg(feature = "chrono")]
    #[test]
    fn chrono_accessors_decode_start_time_and_duration() {
        use chrono::{Datelike, Timelike};
        // One cell with one change entry, no invariant_ts.
        // cell_id=0x0001, loop_length=12, then one 12-byte change entry.
        // MJD 0xE409 = 2018-01-01, BCD 12:34:56
        // duration BCD 01:30:45
        let sel: Vec<u8> = vec![
            0x00, 0x01, // cell_id
            12,   // loop_length
            0x10, // network_change_id
            0x20, // network_change_version
            0xE4, 0x09, 0x12, 0x34, 0x56, // start_time_of_change
            0x01, 0x30, 0x45, // change_duration
            0x23, // packed: rec_cat=1, inv_ts=0, change_type=3
            0x40, // message_id
        ];
        let bytes = wrap(0x07, &sel);
        let d = ExtensionDescriptor::parse(&bytes).unwrap();
        match &d.body {
            ExtensionBody::NetworkChangeNotify(b) => {
                assert_eq!(b.cells.len(), 1);
                let ch = &b.cells[0].changes[0];
                let utc = ch.start_time_of_change_utc().expect("should decode");
                assert_eq!(utc.year(), 2018);
                assert_eq!((utc.hour(), utc.minute(), utc.second()), (12, 34, 56));
                let secs = ch.change_duration_secs().expect("should decode");
                assert_eq!(secs, 5445); // 1h30m45s
            }
            other => panic!("expected NetworkChangeNotify, got {other:?}"),
        }
    }
}