aboutsummaryrefslogtreecommitdiffstats
path: root/src/fig0_21.cpp
blob: fbd1b1325893644ced465e1078522faea80aa7b9 (plain)
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
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)
    Copyright (C) 2018 Matthias P. Braendli (http://www.opendigitalradio.org)
    Copyright (C) 2015 Data Path

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Authors:
         Sergio Sagliocco <sergio.sagliocco@csp.it>
         Matthias P. Braendli <matthias@mpb.li>
                   / |  |-  ')|)  |-|_ _ (|,_   .|  _  ,_ \
         Data Path \(|(||_(|/_| (||_||(a)_||||(|||.(_()|||/

*/

#include "figs.hpp"
#include <cstdio>
#include <cstring>
#include <map>
#include <unordered_set>

static std::unordered_set<int> regions_seen;

bool fig0_21_is_complete(int region_id)
{
    bool complete = regions_seen.count(region_id);

    if (complete) {
        regions_seen.clear();
    }
    else {
        regions_seen.insert(region_id);
    }

    return complete;
}


// FIG 0/21 Frequency Information
// ETSI EN 300 401 8.1.8
fig_result_t fig0_21(fig0_common_t& fig0, const display_settings_t &disp)
{
    uint8_t* f = fig0.f;
    fig_result_t r;

    int i = 1;
    while (i < fig0.figlen) {
        const uint16_t RegionId = (f[i] << 3) | (f[i+1] >> 5);
        r.complete |= fig0_21_is_complete(RegionId);
        const uint8_t Length_FI_list = f[i+1] & 0x1F; // in bytes
        r.msgs.emplace_back("-");
        r.msgs.emplace_back(1, strprintf("RegionId=0x%03x", RegionId));
        r.msgs.emplace_back(1, strprintf("Len=%d Bytes", Length_FI_list));
        i += 2;
        const int FI_start_ix = i;

        r.msgs.emplace_back(1, "FIs:");
        for (size_t FI_ix = 0; i < FI_start_ix + Length_FI_list; FI_ix++) {
            if (i + 3 > fig0.figlen) {
                r.errors.push_back("FIG0/21 too small!");
                break;
            }

            const uint16_t Id_field = (f[i] << 8) | f[i+1];
            const uint8_t RandM = f[i+2] >> 4;
            const bool Continuity_flag = (f[i+2] >> 3) & 0x01;
            const uint8_t Length_Freq_list = f[i+2] & 0x07; // in bytes
            r.msgs.emplace_back(2, "-");
            r.msgs.emplace_back(3, strprintf("Length Freq list=%d", Length_Freq_list));
            i += 3;

            std::string idfield;
            switch (RandM) {
                case 0x0:
                case 0x1: idfield = "EId"; break;
                case 0x6: idfield = "DRM Service Id"; break;
                case 0x8: idfield = "RDS PI"; break;
                case 0x9:
                case 0xa:
                case 0xc: idfield = "Dummy"; break;
                case 0xe: idfield = "AMSS Service Id"; break;
                default:
                          idfield = "invalid";
                          r.errors.emplace_back("R&M invalid");
                          break;
            }
            r.msgs.emplace_back(3,
                    strprintf("ID field=0x%X ", Id_field) + idfield);

            std::string rm_str;
            switch (RandM) {
                case 0x0: rm_str = "DAB ensemble, no local windows"; break;
                case 0x6: rm_str = "DRM"; break;
                case 0x8: rm_str = "FM with RDS"; break;
                case 0x9: rm_str = "FM without RDS"; break;
                case 0xa: rm_str = "AM (MW in 9 kHz steps & LW)"; break;
                case 0xc: rm_str = "AM (MW in 5 kHz steps & SW)"; break;
                case 0xe: rm_str = "AMSS"; break;
                default:
                          rm_str = "Rfu";
                          r.errors.emplace_back("R&M is Rfu");
                          break;
            }
            r.msgs.emplace_back(3,
                    strprintf("R&M=0x%1x ", RandM) + rm_str);

            std::string continuity_str;
            if ((fig0.oe() == 0) || ((fig0.oe() == 1) && (RandM != 0x6) &&
                        ((RandM < 0x8) || (RandM > 0xa)) && (RandM != 0xc) && (RandM != 0xe))) {
                if (Continuity_flag == 0) {
                    switch (RandM) {
                        case 0x0:
                        case 0x1:
                            continuity_str = ", continuous output not expected";
                            break;
                        case 0x6:
                            continuity_str = ", no compensating time delay on DRM audio signal";
                            break;
                        case 0x8:
                        case 0x9:
                            continuity_str = ", no compensating time delay on FM audio signal";
                            break;
                        case 0xa:
                        case 0xc:
                        case 0xe:
                            continuity_str = ", no compensating time delay on AM audio signal";
                            break;
                        default:
                            continuity_str = ", Rfu";
                            break;
                    }
                }
                else {  // Continuity_flag == 1
                    switch (RandM) {
                        case 0x0:
                        case 0x1:
                            continuity_str = ", continuous output possible";
                            break;
                        case 0x6:
                            continuity_str = ", compensating time delay on DRM audio signal";
                            break;
                        case 0x8:
                        case 0x9:
                            continuity_str = ", compensating time delay on FM audio signal";
                            break;
                        case 0xa:
                        case 0xc:
                        case 0xe:
                            continuity_str = ", compensating time delay on AM audio signal";
                            break;
                        default:
                            continuity_str = ", Rfu";
                            break;
                    }
                }
            }
            else {  // fig0.oe() == 1
                continuity_str = ", reserved for future addition";
                r.errors.emplace_back("Rfu");
            }

            r.msgs.emplace_back(3,
                    strprintf("Continuity flag=%d ", Continuity_flag) +
                    continuity_str);

            const uint64_t key =
                ((uint64_t)fig0.oe() << 32) | ((uint64_t)fig0.pd() << 31) |
                ((uint64_t)RegionId << 20) | ((uint64_t)Id_field << 4) |
                (uint64_t)RandM;
            r.msgs.emplace_back(3,
                    strprintf("database key=0x%09" PRId64, key));

            // CEI Change Event Indication
            if (Length_Freq_list == 0) {
                r.msgs.emplace_back(3, "CEI=true");
            }

            r.msgs.emplace_back(3, "Frequency Information:");
            // Iterate over the frequency infos
            switch (RandM) {
                case 0x0:
                case 0x1:
                    {
                        // Each entry is 24 bits (5 control + 19 freq)
                        const size_t bytes_per_entry = 3;
                        const int num_freqs = Length_Freq_list / bytes_per_entry;

                        if (Length_Freq_list % bytes_per_entry != 0) {
                            r.errors.push_back("Length of freq list incorrect size");
                        }

                        for (int freq_ix = 0; freq_ix < num_freqs; freq_ix++) {
                            r.msgs.emplace_back(4, "-");
                            if (i + bytes_per_entry > fig0.figlen) {
                                r.errors.push_back(strprintf(
                                            "FIG 0/21 too small for"
                                            " FI %d, freq %d",
                                            FI_ix, freq_ix));
                                break;
                            }

                            const uint8_t Control_field = (f[i] >> 3);
                            const uint32_t freq = 16 *
                                (((uint32_t)(f[i] & 0x07) << 16) |
                                 ((uint32_t)f[i+1] << 8) |
                                 (uint32_t)f[i+2]);
                            i += bytes_per_entry;
                            if (freq == 0) {
                                r.errors.emplace_back(strprintf(
                                            "Frequency not to be used (0) in"
                                            " FI %d, freq %d",
                                            FI_ix, freq_ix));
                                continue;
                            }
                            const uint8_t Control_field_trans_mode = (Control_field >> 1) & 0x07;
                            if ((Control_field & 0x10) == 0) {
                                r.msgs.emplace_back(5,
                                        strprintf("%d kHz", freq));
                                if ((Control_field & 0x01) == 0) {
                                    r.msgs.emplace_back(5,
                                            "geographically adjacent area");
                                }
                                else {  // (Control_field & 0x01) == 1
                                    r.msgs.emplace_back(5,
                                            "no geographically adjacent area");
                                }
                                if (Control_field_trans_mode == 0) {
                                    r.msgs.emplace_back(5,
                                            "no transmission mode signalled");
                                }
                                else if (Control_field_trans_mode <= 4) {
                                    r.msgs.emplace_back(5,
                                            strprintf("transmission mode %d",
                                                Control_field_trans_mode));
                                }
                                else {  // Control_field_trans_mode > 4
                                    r.msgs.emplace_back(5,
                                            strprintf("invalid transmission mode 0x%x",
                                                Control_field_trans_mode));
                                }
                            }
                            else {  // (Control_field & 0x10) == 0x10
                                r.msgs.emplace_back(5,
                                        strprintf("%d kHz,"
                                            "invalid Control field b23 0x%x",
                                            freq, Control_field));
                            }
                        }
                    }
                    break;
                case 0x8:
                case 0x9:
                case 0xA:
                    {
                        // entries are 8-bit freq
                        const size_t bytes_per_entry = 1;
                        const int num_freqs = Length_Freq_list / bytes_per_entry;

                        for (int freq_ix = 0; freq_ix < num_freqs; freq_ix++) {
                            r.msgs.emplace_back(4, "-");
                            if (i + bytes_per_entry > fig0.figlen) {
                                r.errors.push_back(strprintf(
                                            "FIG 0/21 too small for"
                                            " FI %d, freq %d",
                                            FI_ix, freq_ix));
                            }

                            const uint8_t freq = f[i];
                            i++;
                            if (freq == 0) {
                                r.errors.emplace_back(
                                        "Frequency not to be used (0)");
                                continue;
                            }

                            if (RandM == 0xA) {
                                if (freq < 16) {
                                    r.msgs.emplace_back(5,
                                            strprintf("%d kHz",
                                                144 + ((uint32_t)freq * 9)));
                                }
                                else {  // f[k] >= 16
                                    r.msgs.emplace_back(5,
                                            strprintf("%d kHz",
                                                387 + ((uint32_t)freq * 9)));
                                }
                            }
                            else {  // RandM == 8 or 9
                                r.msgs.emplace_back(5,
                                        strprintf("%.1f MHz",
                                            87.5 + ((float)freq * 0.1)));
                            }
                        }
                    }
                    break;
                case 0xC:
                    {
                        // freqs are 16-bit
                        const size_t bytes_per_entry = 2;
                        const int num_freqs = Length_Freq_list / bytes_per_entry;

                        if (Length_Freq_list % bytes_per_entry != 0) {
                            r.errors.push_back("Length of freq list incorrect size");
                        }

                        for (int freq_ix = 0; freq_ix < num_freqs; freq_ix++) {
                            r.msgs.emplace_back(4, "-");
                            if (i + bytes_per_entry > fig0.figlen) {
                                r.errors.push_back(strprintf(
                                            "FIG 0/21 too small for"
                                            " FI %d, freq %d",
                                            FI_ix, freq_ix));
                            }

                            // freqs are 16-bit
                            const uint16_t freq = 5 *
                                (((uint16_t)f[i] << 8) |
                                 (uint32_t)f[i+1]);
                            i += bytes_per_entry;
                            if (freq != 0) {
                                r.msgs.emplace_back(5,
                                        strprintf("%d kHz", freq));
                            }
                            else {
                                r.errors.emplace_back(
                                        "Frequency not to be used (0)");
                            }
                        }
                    }
                    break;
                case 0x6:
                case 0xE:
                    {
                        // There is a first 8-bit entry, and the
                        // list contains 16-bit freqs
                        if (i + 1 > fig0.figlen) {
                            r.errors.push_back(strprintf(
                                        "FIG 0/21 too small for"
                                        " control of"
                                        " FI %d",
                                        FI_ix));
                        }

                        const size_t bytes_per_entry = 2;
                        const int num_freqs = (Length_Freq_list-1) / bytes_per_entry;

                        if ((Length_Freq_list-1) % bytes_per_entry != 0) {
                            r.errors.push_back("Length of freq list incorrect size");
                        }
                        const uint32_t Id_field2 = f[i];
                        i++;

                        for (int freq_ix = 0; freq_ix < num_freqs; freq_ix++) {
                            r.msgs.emplace_back(4, "-");
                            if (i + bytes_per_entry > fig0.figlen) {
                                r.errors.push_back(strprintf(
                                            "FIG 0/21 too small for"
                                            " FI %d, freq %d",
                                            FI_ix, freq_ix));
                            }
                            // entries are 16bit freq
                            const uint16_t freq =
                                ((((uint16_t)f[i] & 0x7f) << 8) |
                                 (uint16_t)f[i+1]);
                            i += bytes_per_entry;

                            if (freq != 0) {
                                r.msgs.emplace_back(5,
                                        strprintf("%d kHz", freq));
                            }
                            else {
                                r.errors.emplace_back(
                                        "Frequency not to be used (0)");
                            }

                            const uint32_t srv_id = (Id_field2 << 16) | Id_field;
                            if (RandM == 0x6) {
                                r.msgs.emplace_back(5,
                                        strprintf("DRM Service Id 0x%X", srv_id));
                            }
                            else if (RandM == 0xE) {
                                r.msgs.emplace_back(5,
                                        strprintf("AMSS Service Id 0x%X", srv_id));
                            }
                        }
                    }
                    break;
                default:
                    r.errors.push_back(strprintf("Invalid R&M"));
                    break;
            }
        }
    }

    return r;
}