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
|
/*
EDI output.
This defines a few TAG items as defined ETSI TS 102 821 and
ETSI TS 102 693
Copyright (C) 2019
Matthias P. Braendli, matthias.braendli@mpb.li
http://www.opendigitalradio.org
*/
/*
This file is part of the ODR-mmbTools.
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/>.
*/
#include "config.h"
#include "TagItems.h"
#include <vector>
#include <iostream>
#include <string>
#include <cstdint>
#include <stdexcept>
namespace edi {
TagStarPTR::TagStarPTR(const std::string& protocol)
: m_protocol(protocol)
{
if (m_protocol.size() != 4) {
throw std::runtime_error("TagStarPTR protocol invalid length");
}
}
std::vector<uint8_t> TagStarPTR::Assemble()
{
//std::cerr << "TagItem *ptr" << std::endl;
std::string pack_data("*ptr");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
packet.push_back(0x40);
packet.insert(packet.end(), m_protocol.begin(), m_protocol.end());
// Major
packet.push_back(0);
packet.push_back(0);
// Minor
packet.push_back(0);
packet.push_back(0);
return packet;
}
std::vector<uint8_t> TagDETI::Assemble()
{
std::string pack_data("deti");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.reserve(256);
// Placeholder for length
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
uint8_t fct = dlfc % 250;
uint8_t fcth = dlfc / 250;
uint16_t detiHeader = fct | (fcth << 8) | (rfudf << 13) | (ficf << 14) | (atstf << 15);
packet.push_back(detiHeader >> 8);
packet.push_back(detiHeader & 0xFF);
uint32_t etiHeader = mnsc | (rfu << 16) | (rfa << 17) |
(fp << 19) | (mid << 22) | (stat << 24);
packet.push_back((etiHeader >> 24) & 0xFF);
packet.push_back((etiHeader >> 16) & 0xFF);
packet.push_back((etiHeader >> 8) & 0xFF);
packet.push_back(etiHeader & 0xFF);
if (atstf) {
packet.push_back(utco);
packet.push_back((seconds >> 24) & 0xFF);
packet.push_back((seconds >> 16) & 0xFF);
packet.push_back((seconds >> 8) & 0xFF);
packet.push_back(seconds & 0xFF);
packet.push_back((tsta >> 16) & 0xFF);
packet.push_back((tsta >> 8) & 0xFF);
packet.push_back(tsta & 0xFF);
}
if (ficf) {
for (size_t i = 0; i < fic_length; i++) {
packet.push_back(fic_data[i]);
}
}
if (rfudf) {
packet.push_back((rfud >> 16) & 0xFF);
packet.push_back((rfud >> 8) & 0xFF);
packet.push_back(rfud & 0xFF);
}
// calculate and update size
// remove TAG name and TAG length fields and convert to bits
uint32_t taglength = (packet.size() - 8) * 8;
// write length into packet
packet[4] = (taglength >> 24) & 0xFF;
packet[5] = (taglength >> 16) & 0xFF;
packet[6] = (taglength >> 8) & 0xFF;
packet[7] = taglength & 0xFF;
dlfc = (dlfc+1) % 5000;
/*
std::cerr << "TagItem deti, packet.size " << packet.size() << std::endl;
std::cerr << " fic length " << fic_length << std::endl;
std::cerr << " length " << taglength / 8 << std::endl;
*/
return packet;
}
void TagDETI::set_edi_time(const std::time_t t, int tai_utc_offset)
{
utco = tai_utc_offset - 32;
const std::time_t posix_timestamp_1_jan_2000 = 946684800;
seconds = t - posix_timestamp_1_jan_2000 + utco;
}
std::vector<uint8_t> TagESTn::Assemble()
{
std::string pack_data("est");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.reserve(mst_length*8 + 16);
packet.push_back(id);
// Placeholder for length
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
if (tpl > 0x3F) {
throw std::runtime_error("TagESTn: invalid TPL value");
}
if (sad > 0x3FF) {
throw std::runtime_error("TagESTn: invalid SAD value");
}
if (scid > 0x3F) {
throw std::runtime_error("TagESTn: invalid SCID value");
}
uint32_t sstc = (scid << 18) | (sad << 8) | (tpl << 2) | rfa;
packet.push_back((sstc >> 16) & 0xFF);
packet.push_back((sstc >> 8) & 0xFF);
packet.push_back(sstc & 0xFF);
for (size_t i = 0; i < mst_length * 8; i++) {
packet.push_back(mst_data[i]);
}
// calculate and update size
// remove TAG name and TAG length fields and convert to bits
uint32_t taglength = (packet.size() - 8) * 8;
// write length into packet
packet[4] = (taglength >> 24) & 0xFF;
packet[5] = (taglength >> 16) & 0xFF;
packet[6] = (taglength >> 8) & 0xFF;
packet[7] = taglength & 0xFF;
/*
std::cerr << "TagItem ESTn, length " << packet.size() << std::endl;
std::cerr << " mst_length " << mst_length << std::endl;
*/
return packet;
}
std::vector<uint8_t> TagDSTI::Assemble()
{
std::string pack_data("dsti");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.reserve(256);
// Placeholder for length
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
uint8_t dfctl = dflc % 250;
uint8_t dfcth = dflc / 250;
uint16_t dstiHeader = dfctl | (dfcth << 8) | (rfadf << 13) | (atstf << 14) | (stihf << 15);
packet.push_back(dstiHeader >> 8);
packet.push_back(dstiHeader & 0xFF);
if (stihf) {
packet.push_back(stat);
packet.push_back((spid >> 8) & 0xFF);
packet.push_back(spid & 0xFF);
}
if (atstf) {
packet.push_back(utco);
packet.push_back((seconds >> 24) & 0xFF);
packet.push_back((seconds >> 16) & 0xFF);
packet.push_back((seconds >> 8) & 0xFF);
packet.push_back(seconds & 0xFF);
packet.push_back((tsta >> 16) & 0xFF);
packet.push_back((tsta >> 8) & 0xFF);
packet.push_back(tsta & 0xFF);
}
if (rfadf) {
for (size_t i = 0; i < rfad.size(); i++) {
packet.push_back(rfad[i]);
}
}
// calculate and update size
// remove TAG name and TAG length fields and convert to bits
uint32_t taglength = (packet.size() - 8) * 8;
// write length into packet
packet[4] = (taglength >> 24) & 0xFF;
packet[5] = (taglength >> 16) & 0xFF;
packet[6] = (taglength >> 8) & 0xFF;
packet[7] = taglength & 0xFF;
dflc = (dflc+1) % 5000;
/*
std::cerr << "TagItem dsti, packet.size " << packet.size() << std::endl;
std::cerr << " length " << taglength / 8 << std::endl;
*/
return packet;
}
void TagDSTI::set_edi_time(const std::time_t t, int tai_utc_offset)
{
utco = tai_utc_offset - 32;
const std::time_t posix_timestamp_1_jan_2000 = 946684800;
seconds = t - posix_timestamp_1_jan_2000 + utco;
}
#if 0
/* Update the EDI time. t is in UTC, TAI offset is requested from adjtimex */
void TagDSTI::set_edi_time(const std::time_t t)
{
if (tai_offset_cache_updated_at == 0 or tai_offset_cache_updated_at + 3600 < t) {
struct timex timex_request;
timex_request.modes = 0;
int err = adjtimex(&timex_request);
if (err == -1) {
throw std::runtime_error("adjtimex failed");
}
if (timex_request.tai == 0) {
throw std::runtime_error("CLOCK_TAI is not properly set up");
}
tai_offset_cache = timex_request.tai;
tai_offset_cache_updated_at = t;
fprintf(stderr, "adjtimex: %d, tai %d\n", err, timex_request.tai);
}
utco = tai_offset_cache - 32;
const std::time_t posix_timestamp_1_jan_2000 = 946684800;
seconds = t - posix_timestamp_1_jan_2000 + utco;
}
#endif
std::vector<uint8_t> TagSSm::Assemble()
{
std::string pack_data("ss");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.reserve(istd_length + 16);
packet.push_back((id >> 8) & 0xFF);
packet.push_back(id & 0xFF);
// Placeholder for length
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
packet.push_back(0);
if (rfa > 0x1F) {
throw std::runtime_error("TagSSm: invalid RFA value");
}
if (tid > 0x7) {
throw std::runtime_error("TagSSm: invalid tid value");
}
if (tidext > 0x7) {
throw std::runtime_error("TagSSm: invalid tidext value");
}
if (stid > 0x0FFF) {
throw std::runtime_error("TagSSm: invalid stid value");
}
uint32_t istc = (rfa << 19) | (tid << 16) | (tidext << 13) | ((crcstf ? 1 : 0) << 12) | stid;
packet.push_back((istc >> 16) & 0xFF);
packet.push_back((istc >> 8) & 0xFF);
packet.push_back(istc & 0xFF);
for (size_t i = 0; i < istd_length; i++) {
packet.push_back(istd_data[i]);
}
// calculate and update size
// remove TAG name and TAG length fields and convert to bits
uint32_t taglength = (packet.size() - 8) * 8;
// write length into packet
packet[4] = (taglength >> 24) & 0xFF;
packet[5] = (taglength >> 16) & 0xFF;
packet[6] = (taglength >> 8) & 0xFF;
packet[7] = taglength & 0xFF;
/*
std::cerr << "TagItem SSm, length " << packet.size() << std::endl;
std::cerr << " istd_length " << istd_length << std::endl;
*/
return packet;
}
std::vector<uint8_t> TagStarDMY::Assemble()
{
std::string pack_data("*dmy");
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
packet.resize(4 + 4 + length_);
const uint32_t length_bits = length_ * 8;
packet[4] = (length_bits >> 24) & 0xFF;
packet[5] = (length_bits >> 16) & 0xFF;
packet[6] = (length_bits >> 8) & 0xFF;
packet[7] = length_bits & 0xFF;
// The remaining bytes in the packet are "undefined data"
return packet;
}
}
|