dvb-t2mi 10.0.0

ETSI TS 102 773 DVB-T2 Modulator Interface parser + builder.
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
433
434
435
436
437
438
439
440
//! Drift tests: assert that each `spec_tables/*.toml` mirror and its
//! code-backing enum/dispatch table are in sync.
//!
//! A byte-sweep over the relevant domain produces the "from code" set; the
//! TOML parser produces the "from spec" set.  Fails symmetrically if either
//! set has something the other lacks.

use dvb_t2mi::packet::PacketType;
use dvb_t2mi::payload::fef_null::S1Field;
use dvb_t2mi::payload::fef_subpart::SubpartVariety;
use dvb_t2mi::payload::individual_addressing::AddressingFunctionTag;
use dvb_t2mi::payload::l1_current::FrequencySource;
use dvb_t2mi::payload::timestamp::Bandwidth;
use dvb_t2mi::payload::{
    AuxStreamType, GuardInterval, L1CodeRate, L1FecType, L1Modulation, PilotPattern, PlpFecType,
    PlpMode, PlpModulation, PlpPayloadType, PlpType, T2Version, TxInputStreamType,
};
use std::collections::BTreeSet;

// ── tiny TOML parser ─────────────────────────────────────────────────────────

/// Parse the spec_tables TOML format, returning `(value_u16, variant, spec)`.
fn parse_entries(toml: &str) -> Vec<(u16, String, String)> {
    let mut results = Vec::new();
    let mut cur_value: Option<u16> = None;
    let mut cur_variant: Option<String> = None;
    let mut cur_spec: Option<String> = None;

    let flush = |v: &mut Option<u16>,
                 var: &mut Option<String>,
                 sp: &mut Option<String>,
                 out: &mut Vec<(u16, String, String)>| {
        if let (Some(value), Some(variant), Some(spec)) = (v.take(), var.take(), sp.take()) {
            out.push((value, variant, spec));
        }
    };

    for raw in toml.lines() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if line == "[[entry]]" {
            flush(
                &mut cur_value,
                &mut cur_variant,
                &mut cur_spec,
                &mut results,
            );
            continue;
        }
        if let Some(eq) = line.find('=') {
            let key = line[..eq].trim();
            let val = line[eq + 1..].trim();
            match key {
                "value" => {
                    let hex = val.trim_start_matches("0x").trim_start_matches("0X");
                    cur_value = Some(
                        u16::from_str_radix(hex, 16)
                            .unwrap_or_else(|_| panic!("bad hex in TOML: {val:?}")),
                    );
                }
                "variant" => {
                    cur_variant = Some(val.trim_matches('"').replace("\\\"", "\"").to_string());
                }
                "spec" => {
                    cur_spec = Some(val.trim_matches('"').replace("\\\"", "\"").to_string());
                }
                _ => {}
            }
        }
    }
    flush(
        &mut cur_value,
        &mut cur_variant,
        &mut cur_spec,
        &mut results,
    );
    results
}

// ── helpers ───────────────────────────────────────────────────────────────────

fn toml_set_u8(toml: &str) -> BTreeSet<(u8, String)> {
    parse_entries(toml)
        .iter()
        .map(|(v, var, _)| (*v as u8, var.clone()))
        .collect()
}

fn toml_set_u16(toml: &str) -> BTreeSet<(u16, String)> {
    parse_entries(toml)
        .iter()
        .map(|(v, var, _)| (*v, var.clone()))
        .collect()
}

// ── test: PacketType ─────────────────────────────────────────────────────────

#[test]
fn packet_type_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/packet_type.toml");
    let toml_set = toml_set_u8(toml);

    // byte sweep: TryFromPrimitive — skip bytes that don't map to a variant
    let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
    for b in 0u8..=255 {
        if let Ok(pt) = PacketType::try_from(b) {
            code_set.insert((pt as u8, format!("{pt:?}")));
        }
    }

    // tripwire: 12 named variants (Table 1, verified from source)
    assert_eq!(
        code_set.len(),
        12,
        "PacketType sweep produced {} named variants, expected 12",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "PacketType drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── test: AddressingFunctionTag ───────────────────────────────────────────────

#[test]
fn addressing_function_tag_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/addressing_function_tag.toml");
    let toml_set = toml_set_u8(toml);

    // byte sweep: TryFromPrimitive — skip bytes that don't map to a variant
    let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
    for b in 0u8..=255 {
        if let Ok(tag) = AddressingFunctionTag::try_from(b) {
            code_set.insert((tag as u8, format!("{tag:?}")));
        }
    }

    // tripwire: 14 named variants (Tables 5 & 6, verified from source)
    assert_eq!(
        code_set.len(),
        14,
        "AddressingFunctionTag sweep produced {} named variants, expected 14",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "AddressingFunctionTag drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── test: Bandwidth ───────────────────────────────────────────────────────────

#[test]
fn bandwidth_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/bandwidth.toml");
    let toml_set = toml_set_u8(toml);

    // byte sweep: TryFromPrimitive — skip bytes that don't map to a variant
    let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
    for b in 0u8..=255 {
        if let Ok(bw) = Bandwidth::try_from(b) {
            code_set.insert((bw as u8, format!("{bw:?}")));
        }
    }

    // tripwire: 6 named variants (Table 3, verified from source)
    assert_eq!(
        code_set.len(),
        6,
        "Bandwidth sweep produced {} named variants, expected 6",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "Bandwidth drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── test: FrequencySource ─────────────────────────────────────────────────────

#[test]
fn frequency_source_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/frequency_source.toml");
    let toml_set = toml_set_u8(toml);

    // byte sweep: TryFromPrimitive — skip bytes that don't map to a variant
    let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
    for b in 0u8..=255 {
        if let Ok(fs) = FrequencySource::try_from(b) {
            code_set.insert((fs as u8, format!("{fs:?}")));
        }
    }

    // tripwire: 3 named variants (Table 2, verified from source)
    assert_eq!(
        code_set.len(),
        3,
        "FrequencySource sweep produced {} named variants, expected 3",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "FrequencySource drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── test: S1Field ─────────────────────────────────────────────────────────────

#[test]
fn s1_field_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/s1_field.toml");
    let toml_set = toml_set_u8(toml);

    // byte sweep: S1Field is exhaustive over 3-bit domain; TryFromPrimitive
    // accepts 0..=7, rejects 8..=255
    let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
    for b in 0u8..=255 {
        if let Ok(s1) = S1Field::try_from(b) {
            code_set.insert((s1 as u8, format!("{s1:?}")));
        }
    }

    // tripwire: 8 named variants (Table 18, 3-bit field exhaustive)
    assert_eq!(
        code_set.len(),
        8,
        "S1Field sweep produced {} named variants, expected 8",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "S1Field drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── test: SubpartVariety ──────────────────────────────────────────────────────

#[test]
fn subpart_variety_toml_matches_enum() {
    let toml = include_str!("../docs/enums/ts_102_773/subpart_variety.toml");
    let toml_set = toml_set_u16(toml);

    // u16 sweep over 0..=255 (all defined values fit in this range);
    // SubpartVariety is TryFromPrimitive on u16; values 4..=0xFFFF return Err.
    let mut code_set: BTreeSet<(u16, String)> = BTreeSet::new();
    for b in 0u16..=255 {
        if let Ok(sv) = SubpartVariety::try_from(b) {
            code_set.insert((sv as u16, format!("{sv:?}")));
        }
    }

    // tripwire: 4 named variants (Table 13, verified from source)
    assert_eq!(
        code_set.len(),
        4,
        "SubpartVariety sweep produced {} named variants, expected 4",
        code_set.len()
    );

    let only_in_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
    let only_in_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
    assert!(
        only_in_toml.is_empty() && only_in_code.is_empty(),
        "SubpartVariety drift detected!\n  only in TOML: {only_in_toml:?}\n  only in code: {only_in_code:?}"
    );
}

// ── L1 signalling enum drift tests ───────────────────────────────────────────

macro_rules! l1_drift_test {
    ($test_name:ident, $toml_file:literal, $enum_ty:ident, $domain:expr, $expected:expr, $reserved_pat:pat) => {
        #[test]
        fn $test_name() {
            let toml = include_str!(concat!("../docs/enums/en_302_755/", $toml_file));
            let toml_set: BTreeSet<(u8, String)> = parse_entries(toml)
                .iter()
                .map(|(v, var, _)| (*v as u8, var.clone()))
                .collect();

            let mut code_set: BTreeSet<(u8, String)> = BTreeSet::new();
            for b in $domain {
                let e = $enum_ty::from_u8(b);
                if !matches!(e, $reserved_pat) {
                    code_set.insert((e.to_u8(), format!("{e:?}")));
                }
            }

            assert_eq!(
                code_set.len(),
                $expected,
                "{}: expected {} named variants, got {}",
                stringify!($enum_ty),
                $expected,
                code_set.len()
            );
            let only_toml: BTreeSet<_> = toml_set.difference(&code_set).collect();
            let only_code: BTreeSet<_> = code_set.difference(&toml_set).collect();
            assert!(
                only_toml.is_empty() && only_code.is_empty(),
                "{} drift!\n  only in TOML: {only_toml:?}\n  only in code: {only_code:?}",
                stringify!($enum_ty)
            );
        }
    };
}

l1_drift_test!(
    tx_input_stream_type_drift,
    "tx_input_stream_type.toml",
    TxInputStreamType,
    0u8..=0xFF,
    3,
    TxInputStreamType::Reserved(_)
);

l1_drift_test!(
    guard_interval_drift,
    "guard_interval.toml",
    GuardInterval,
    0u8..=7,
    7,
    GuardInterval::Reserved(_)
);

l1_drift_test!(
    l1_modulation_drift,
    "l1_modulation.toml",
    L1Modulation,
    0u8..=0x0F,
    4,
    L1Modulation::Reserved(_)
);

l1_drift_test!(
    l1_code_rate_drift,
    "l1_code_rate.toml",
    L1CodeRate,
    0u8..=3,
    1,
    L1CodeRate::Reserved(_)
);

l1_drift_test!(
    l1_fec_type_drift,
    "l1_fec_type.toml",
    L1FecType,
    0u8..=3,
    1,
    L1FecType::Reserved(_)
);

l1_drift_test!(
    pilot_pattern_drift,
    "pilot_pattern.toml",
    PilotPattern,
    0u8..=0x0F,
    8,
    PilotPattern::Reserved(_)
);

l1_drift_test!(
    t2_version_drift,
    "t2_version.toml",
    T2Version,
    0u8..=0x0F,
    3,
    T2Version::Reserved(_)
);

l1_drift_test!(
    plp_type_drift,
    "plp_type.toml",
    PlpType,
    0u8..=7,
    3,
    PlpType::Reserved(_)
);

l1_drift_test!(
    plp_payload_type_drift,
    "plp_payload_type.toml",
    PlpPayloadType,
    0u8..=0x1F,
    4,
    PlpPayloadType::Reserved(_)
);

l1_drift_test!(
    plp_modulation_drift,
    "plp_modulation.toml",
    PlpModulation,
    0u8..=7,
    4,
    PlpModulation::Reserved(_)
);

l1_drift_test!(
    plp_fec_type_drift,
    "plp_fec_type.toml",
    PlpFecType,
    0u8..=3,
    2,
    PlpFecType::Reserved(_)
);

l1_drift_test!(
    plp_mode_drift,
    "plp_mode.toml",
    PlpMode,
    0u8..=3,
    3,
    PlpMode::Reserved(_)
);

l1_drift_test!(
    aux_stream_type_drift,
    "aux_stream_type.toml",
    AuxStreamType,
    0u8..=0x0F,
    1,
    AuxStreamType::Reserved(_)
);