aboutsummaryrefslogtreecommitdiffstats
path: root/fig0_21.cpp
blob: 69f18b9f4f9e65aed273204fef04036875a35d51 (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
/*
    Copyright (C) 2014 CSP Innovazione nelle ICT s.c.a r.l. (http://www.csp.it/)
    Copyright (C) 2016 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
bool fig0_21(fig0_common_t& fig0, int indent)
{
    float freq;
    uint32_t ifreq;
    uint64_t key;
    uint16_t RegionId, Id_field;
    uint8_t i = 1, j, k, Length_FI_list, RandM, Length_Freq_list, Control_field;
    uint8_t Control_field_trans_mode, Id_field2;
    char tmpbuf[256];
    char desc[256];
    bool Continuity_flag;
    uint8_t* f = fig0.f;
    bool complete = false;

    while (i < (fig0.figlen - 1)) {
        // iterate over frequency information
        // decode RegionId, Length of FI list
        RegionId  =  (f[i] << 3) | (f[i+1] >> 5);
        complete |= fig0_21_is_complete(RegionId);
        Length_FI_list  = (f[i+1] & 0x1F);
        sprintf(desc, "RegionId=0x%03x", RegionId);
        printbuf(desc, indent+1, NULL, 0);
        i += 2;
        if ((i + Length_FI_list) <= fig0.figlen) {
            j = i;
            while ((j + 2) < (i + Length_FI_list)) {
                // iterate over FI list x
                // decode Id field, R&M, Continuity flag, Length of Freq list
                Id_field = (f[j] << 8) | f[j+1];
                RandM = f[j+2] >> 4;
                Continuity_flag = (f[j+2] >> 3) & 0x01;
                Length_Freq_list = f[j+2] & 0x07;
                sprintf(desc, "Id_field=");
                switch (RandM) {
                    case 0x0:
                    case 0x1:
                        strcat(desc, "EId");
                        break;
                    case 0x6:
                        strcat(desc, "DRM Service Id");
                        break;
                    case 0x8:
                        strcat(desc, "RDS PI");
                        break;
                    case 0x9:
                    case 0xa:
                    case 0xc:
                        strcat(desc, "Dummy");
                        break;
                    case 0xe:
                        strcat(desc, "AMSS Service Id");
                        break;
                    default:
                        strcat(desc, "invalid");
                        break;
                }
                sprintf(tmpbuf, "=0x%X, R&M=0x%1x", Id_field, RandM);
                strcat(desc, tmpbuf);
                switch (RandM) {
                    case 0x0:
                        strcat(desc, "=DAB ensemble, no local windows");
                        break;
                    case 0x6:
                        strcat(desc, "=DRM");
                        break;
                    case 0x8:
                        strcat(desc, "=FM with RDS");
                        break;
                    case 0x9:
                        strcat(desc, "=FM without RDS");
                        break;
                    case 0xa:
                        strcat(desc, "=AM (MW in 9 kHz steps & LW)");
                        break;
                    case 0xc:
                        strcat(desc, "=AM (MW in 5 kHz steps & SW)");
                        break;
                    case 0xe:
                        strcat(desc, "=AMSS");
                        break;
                    default:
                        strcat(desc, "=Rfu");
                        break;
                }
                sprintf(tmpbuf, ", Continuity flag=%d", Continuity_flag);
                strcat(desc, tmpbuf);
                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:
                                strcat(desc, "=continuous output not expected");
                                break;
                            case 0x6:
                                strcat(desc, "=no compensating time delay on DRM audio signal");
                                break;
                            case 0x8:
                            case 0x9:
                                strcat(desc, "=no compensating time delay on FM audio signal");
                                break;
                            case 0xa:
                            case 0xc:
                            case 0xe:
                                strcat(desc, "=no compensating time delay on AM audio signal");
                                break;
                            default:
                                strcat(desc, "=Rfu");
                                break;
                        }
                    }
                    else {  // Continuity_flag == 1
                        switch (RandM) {
                            case 0x0:
                            case 0x1:
                                strcat(desc, "=continuous output possible");
                                break;
                            case 0x6:
                                strcat(desc, "=compensating time delay on DRM audio signal");
                                break;
                            case 0x8:
                            case 0x9:
                                strcat(desc, "=compensating time delay on FM audio signal");
                                break;
                            case 0xa:
                            case 0xc:
                            case 0xe:
                                strcat(desc, "=compensating time delay on AM audio signal");
                                break;
                            default:
                                strcat(desc, "=Rfu");
                                break;
                        }
                    }
                }
                else {  // fig0.oe() == 1
                    strcat(desc, "=reserved for future addition");
                }
                key = ((uint64_t)fig0.oe() << 32) | ((uint64_t)fig0.pd() << 31) | \
                      ((uint64_t)RegionId << 20) | ((uint64_t)Id_field << 4) | \
                      (uint64_t)RandM;
                sprintf(tmpbuf, ", database key=0x%09" PRId64, key);
                // CEI Change Event Indication
                if (Length_Freq_list == 0) {
                    strcat(tmpbuf, ", CEI");
                }
                strcat(desc, tmpbuf);
                printbuf(desc, indent+2, NULL, 0);
                j += 3; // add header

                k = j;
                switch (RandM) {
                    case 0x0:
                    case 0x1:
                        while((k + 2) < (j + Length_Freq_list)) {
                            // iteration over Freq list
                            ifreq = (((uint32_t)(f[k] & 0x07) << 16) | ((uint32_t)f[k+1] << 8) | (uint32_t)f[k+2]) * 16;
                            if (ifreq != 0) {
                                Control_field = (f[k] >> 3);
                                Control_field_trans_mode = (Control_field >> 1) & 0x07;
                                if ((Control_field & 0x10) == 0) {
                                    sprintf(desc, "%d KHz, ", ifreq);
                                    if ((Control_field & 0x01) == 0) {
                                        strcat(desc, "geographically adjacent area, ");
                                    }
                                    else {  // (Control_field & 0x01) == 1
                                        strcat(desc, "no geographically adjacent area, ");
                                    }
                                    if (Control_field_trans_mode == 0) {
                                        strcat(desc, "no transmission mode signalled");
                                    }
                                    else if (Control_field_trans_mode <= 4) {
                                        sprintf(tmpbuf, "transmission mode %d", Control_field_trans_mode);
                                        strcat(desc, tmpbuf);
                                    }
                                    else {  // Control_field_trans_mode > 4
                                        sprintf(tmpbuf, "invalid transmission mode 0x%x", Control_field_trans_mode);
                                        strcat(desc, tmpbuf);
                                    }
                                }
                                else {  // (Control_field & 0x10) == 0x10
                                    sprintf(desc, "%d KHz, invalid Control field b23 0x%x", ifreq, Control_field);
                                }
                            }
                            else {
                                sprintf(desc, "Frequency not to be used (0)");
                            }
                            printbuf(desc, indent+3, NULL, 0);
                            k += 3;
                        }
                        break;
                    case 0x8:
                    case 0x9:
                    case 0xa:
                        while(k < (j + Length_Freq_list)) {
                            // iteration over Freq list
                            if (f[k] != 0) {    // freq != 0
                                if (RandM == 0xa) {
                                    if (f[k] < 16) {
                                        ifreq = (144 + ((uint32_t)f[k] * 9));
                                    }
                                    else {  // f[k] >= 16
                                        ifreq = (387 + ((uint32_t)f[k] * 9));
                                    }
                                    sprintf(desc, "%d KHz", ifreq);
                                }
                                else {  // RandM == 8 or 9
                                    freq = (87.5 + ((float)f[k] * 0.1));
                                    sprintf(desc, "%.1f MHz", freq);
                                }
                            }
                            else {
                                sprintf(desc, "Frequency not to be used (0)");
                            }
                            printbuf(desc, indent+3, NULL, 0);
                            k++;
                        }
                        break;
                    case 0xc:
                        while((k + 1) < (j + Length_Freq_list)) {
                            // iteration over Freq list
                            ifreq = (((uint32_t)f[k] << 8) | (uint32_t)f[k+1]) * 5;
                            if (ifreq != 0) {
                                sprintf(desc, "%d KHz", ifreq);
                            }
                            else {
                                sprintf(desc, "Frequency not to be used (0)");
                            }
                            printbuf(desc, indent+3, NULL, 0);
                            k += 2;
                        }
                        break;
                    case 0x6:
                    case 0xe:
                        while((k + 2) < (j + Length_Freq_list)) {
                            // iteration over Freq list
                            Id_field2 = f[k];
                            ifreq = ((((uint32_t)f[k+1] & 0x7f) << 8) | (uint32_t)f[k+2]);
                            if (ifreq != 0) {
                                sprintf(desc, "%d KHz", ifreq);
                            }
                            else {
                                sprintf(desc, "Frequency not to be used (0)");
                            }
                            if (RandM == 0x6) {
                                sprintf(tmpbuf, ", DRM Service Id 0x%X", Id_field2);
                                strcat(desc, tmpbuf);
                            }
                            else if (RandM == 0xe) {
                                sprintf(tmpbuf, ", AMSS Service Id 0x%X", Id_field2);
                                strcat(desc, tmpbuf);
                            }
                            if ((f[k+1] & 0x80) == 0x80) {
                                sprintf(tmpbuf, ", invalid Rfu b15 set to 1 instead of 0");
                                strcat(desc, tmpbuf);
                            }
                            printbuf(desc, indent+3, NULL, 0);
                            k += 3;
                        }
                        break;
                    default:
                        break;
                }
                j += Length_Freq_list;
            }
            i += Length_FI_list;
        }
    }

    return complete;
}