summaryrefslogtreecommitdiffstats
path: root/src/zmq2edi/zmq2edi.cpp
blob: cff16c7499bf3eecb9482984c1fcf40cd0abf3dc (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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
   2011, 2012 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)

   Copyright (C) 2020
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://www.opendigitalradio.org
   */
/*
   This file is part of ODR-DabMux.

   ODR-DabMux 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.

   ODR-DabMux 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 ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Log.h"
#include "zmq.hpp"
#include <getopt.h>
#include <cmath>
#include <cstring>
#include <chrono>
#include <iostream>
#include <iterator>
#include <thread>
#include <vector>

#include "EDISender.h"
#include "dabOutput/dabOutput.h"

constexpr size_t MAX_ERROR_COUNT = 10;
constexpr long ZMQ_TIMEOUT_MS = 1000;
constexpr long DEFAULT_BACKOFF = 5000;

static edi::configuration_t edi_conf;

static EDISender edisender;

static void usage()
{
    using namespace std;

    cerr << "Usage:" << endl;
    cerr << "odr-zmq2edi [options] <source>" << endl << endl;

    cerr << "Options:" << endl;
    cerr << "The following options can be given only once:" << endl;
    cerr << " <source> is a ZMQ URL that points to a ODR-DabMux ZMQ output." << endl;
    cerr << " -w <delay>            Keep every ETI frame until TIST is <delay> milliseconds after current system time." << endl;
    cerr << "                       Negative delay values are also allowed." << endl;
    cerr << " -C <path to script>   Before starting, run the given script, and only start if it returns 0." << endl;
    cerr << "                       This is useful for checking that NTP is properly synchronised" << endl;
    cerr << " -x                    Drop frames where for which the wait time would be negative, i.e. frames that arrived too late." << endl;
    cerr << " -P                    Disable PFT and send AFPackets." << endl;
    cerr << " -f <fec>              Set the FEC." << endl;
    cerr << " -i <spread>           Configure the UDP packet spread/interleaver with given percentage: 0% send all fragments at once, 100% spread over 24ms, >100% spread and interleave. Default 95%\n";
    cerr << " -D                    Dump the EDI to edi.debug file." << endl;
    cerr << " -v                    Enables verbose mode." << endl;
    cerr << " -a <alignement>       Set the alignment of the TAG Packet (default 8)." << endl;
    cerr << " -b <backoff>          Number of milliseconds to backoff after an input reset (default " << DEFAULT_BACKOFF << ")." << endl << endl;

    cerr << "The following options can be given several times, when more than UDP destination is desired:" << endl;
    cerr << " -d <destination ip>   Set the destination ip." << endl;
    cerr << " -p <destination port> Set the destination port." << endl;
    cerr << " -s <source port>      Set the source port." << endl;
    cerr << " -S <source ip>        Select the source IP in case we want to use multicast." << endl;
    cerr << " -t <ttl>              Set the packet's TTL." << endl << endl;

    cerr << "The input socket will be reset if no data is received for " <<
        (int)(MAX_ERROR_COUNT * ZMQ_TIMEOUT_MS / 1000.0) << " seconds." << endl;
    cerr << "It is best practice to run this tool under a process supervisor that will restart it automatically." << endl;
}

static metadata_t get_md_one_frame(uint8_t *buf, size_t size, size_t *consumed_bytes)
{
    size_t remaining = size;
    if (remaining < 3) {
        etiLog.level(warn) << "Insufficient data to parse metadata";
        throw std::runtime_error("Insufficient data");
    }

    metadata_t md;
    bool utc_offset_received = false;
    bool edi_time_received = false;
    bool dlfc_received = false;

    while (remaining) {
        uint8_t id = buf[0];
        uint16_t len = (((uint16_t)buf[1]) << 8) + buf[2];

        if (id == static_cast<uint8_t>(output_metadata_id_e::separation_marker)) {
            if (len != 0) {
                etiLog.level(warn) << "Invalid length " << len << " for metadata: separation_marker";
            }

            if (not utc_offset_received or not edi_time_received or not dlfc_received) {
                throw std::runtime_error("Incomplete metadata received");
            }

            remaining -= 3;
            *consumed_bytes = size - remaining;
            return md;
        }
        else if (id == static_cast<uint8_t>(output_metadata_id_e::utc_offset)) {
            if (len != 2) {
                etiLog.level(warn) << "Invalid length " << len << " for metadata: utc_offset";
            }
            if (remaining < 2) {
                throw std::runtime_error("Insufficient data for utc_offset");
            }
            uint16_t utco;
            std::memcpy(&utco, buf + 3, sizeof(utco));
            md.utc_offset = ntohs(utco);
            utc_offset_received = true;
            remaining -= 5;
            buf += 5;
        }
        else if (id == static_cast<uint8_t>(output_metadata_id_e::edi_time)) {
            if (len != 4) {
                etiLog.level(warn) << "Invalid length " << len << " for metadata: edi_time";
            }
            if (remaining < 4) {
                throw std::runtime_error("Insufficient data for edi_time");
            }
            uint32_t edi_time;
            std::memcpy(&edi_time, buf + 3, sizeof(edi_time));
            md.edi_time = ntohl(edi_time);
            edi_time_received = true;
            remaining -= 7;
            buf += 7;
        }
        else if (id == static_cast<uint8_t>(output_metadata_id_e::dlfc)) {
            if (len != 2) {
                etiLog.level(warn) << "Invalid length " << len << " for metadata: dlfc";
            }
            if (remaining < 2) {
                throw std::runtime_error("Insufficient data for dlfc");
            }
            uint16_t dlfc;
            std::memcpy(&dlfc, buf + 3, sizeof(dlfc));
            md.dlfc = ntohs(dlfc);
            dlfc_received = true;
            remaining -= 5;
            buf += 5;
        }
    }

    throw std::runtime_error("Insufficient data");
}

/* There is some state inside the parsing of destination arguments,
 * because several destinations can be given.  */

static std::shared_ptr<edi::udp_destination_t> edi_destination;
static bool dest_port_set = false;
static bool source_port_set = false;
static bool source_addr_set = false;
static bool ttl_set = false;
static bool dest_addr_set = false;

static void add_edi_destination(void)
{
    if (not dest_addr_set) {
        throw std::runtime_error("Destination address not specified for destination number " +
                std::to_string(edi_conf.destinations.size() + 1));
    }

    edi_conf.destinations.push_back(move(edi_destination));
    edi_destination = std::make_shared<edi::udp_destination_t>();

    dest_port_set = false;
    source_port_set = false;
    source_addr_set = false;
    ttl_set = false;
    dest_addr_set = false;
}

static void parse_destination_args(char option)
{
    if (not edi_destination) {
        edi_destination = std::make_shared<edi::udp_destination_t>();
    }

    switch (option) {
        case 'p':
            if (dest_port_set) {
                add_edi_destination();
            }
            edi_destination->dest_port = std::stoi(optarg);
            dest_port_set = true;
            break;
        case 's':
            if (source_port_set) {
                add_edi_destination();
            }
            edi_destination->source_port = std::stoi(optarg);
            source_port_set = true;
            break;
        case 'S':
            if (source_addr_set) {
                add_edi_destination();
            }
            edi_destination->source_addr = optarg;
            source_addr_set = true;
            break;
        case 't':
            if (ttl_set) {
                add_edi_destination();
            }
            edi_destination->ttl = std::stoi(optarg);
            ttl_set = true;
            break;
        case 'd':
            if (dest_addr_set) {
                add_edi_destination();
            }
            edi_destination->dest_addr = optarg;
            dest_addr_set = true;
            break;
        default:
            throw std::logic_error("parse_destination_args invalid");
    }
}

class FCTDiscontinuity { };

int start(int argc, char **argv)
{
    edi_conf.enable_pft = true;

    if (argc == 0) {
        usage();
        return 1;
    }

    int delay_ms = 500;
    bool drop_late_packets = false;
    uint32_t backoff_after_reset_ms = DEFAULT_BACKOFF;
    std::string startupcheck;

    int ch = 0;
    while (ch != -1) {
        ch = getopt(argc, argv, "C:d:p:s:S:t:Pf:i:Dva:b:w:xh");
        switch (ch) {
            case -1:
                break;
            case 'C':
                startupcheck = optarg;
                break;
            case 'd':
            case 's':
            case 'S':
            case 't':
            case 'p':
                parse_destination_args(ch);
                break;
            case 'P':
                edi_conf.enable_pft = false;
                break;
            case 'f':
                edi_conf.fec = std::stoi(optarg);
                break;
            case 'i':
                {
                    int spread_percent = std::stoi(optarg);
                    if (spread_percent < 0) {
                        throw std::runtime_error("EDI output: negative spread value is invalid.");
                    }

                    edi_conf.fragment_spreading_factor = (double)spread_percent / 100.0;
                    if (edi_conf.fragment_spreading_factor > 30000) {
                        throw std::runtime_error("EDI output: interleaving set for more than 30 seconds!");
                    }
                }
                break;
            case 'D':
                edi_conf.dump = true;
                break;
            case 'v':
                edi_conf.verbose = true;
                break;
            case 'a':
                edi_conf.tagpacket_alignment = std::stoi(optarg);
                break;
            case 'b':
                backoff_after_reset_ms = std::stoi(optarg);
                break;
            case 'w':
                delay_ms = std::stoi(optarg);
                break;
            case 'x':
                drop_late_packets = true;
                break;
            case 'h':
            default:
                usage();
                return 1;
        }
    }

    if (not startupcheck.empty()) {
        etiLog.level(info) << "Running startup check '" << startupcheck << "'";
        int wstatus = system(startupcheck.c_str());

        if (WIFEXITED(wstatus)) {
            if (WEXITSTATUS(wstatus) == 0) {
                etiLog.level(info) << "Startup check ok";
            }
            else {
                etiLog.level(error) << "Startup check failed, returned " << WEXITSTATUS(wstatus);
                return 1;
            }
        }
        else {
            etiLog.level(error) << "Startup check failed, child didn't terminate normally";
            return 1;
        }
    }

    add_edi_destination();

    if (optind >= argc) {
        etiLog.level(error) << "source option is missing";
        return 1;
    }

    if (edi_conf.destinations.empty()) {
        etiLog.level(error) << "No EDI destinations set";
        return 1;
    }

    etiLog.level(info) << "Setting up EDI Sender with delay " << delay_ms << " ms. " <<
        (drop_late_packets ? "Will" : "Will not") << " drop late packets";
    edisender.start(edi_conf, delay_ms, drop_late_packets);
    edisender.print_configuration();

    const char* source_url = argv[optind];

    zmq::context_t zmq_ctx(1);
    etiLog.level(info) << "Opening ZMQ input: " << source_url;

    while (true) {
        zmq::socket_t zmq_sock(zmq_ctx, ZMQ_SUB);
        zmq_sock.connect(source_url);
        zmq_sock.setsockopt(ZMQ_SUBSCRIBE, NULL, 0); // subscribe to all messages

        size_t error_count = 0;
        int previous_fct = -1;

        try {
            while (error_count < MAX_ERROR_COUNT) {
                zmq::message_t incoming;
                zmq::pollitem_t items[1];
                items[0].socket = zmq_sock;
                items[0].events = ZMQ_POLLIN;
                const int num_events = zmq::poll(items, 1, ZMQ_TIMEOUT_MS);
                if (num_events == 0) { // timeout
                    error_count++;
                }
                else {
                    // Event received: recv will not block
                    zmq_sock.recv(incoming, zmq::recv_flags::none);
                    const auto received_at = std::chrono::steady_clock::now();

                    zmq_dab_message_t* dab_msg = (zmq_dab_message_t*)incoming.data();

                    if (dab_msg->version != 1) {
                        etiLog.level(error) << "ZeroMQ wrong packet version " << dab_msg->version;
                        error_count++;
                    }

                    int offset = sizeof(dab_msg->version) +
                        NUM_FRAMES_PER_ZMQ_MESSAGE * sizeof(*dab_msg->buflen);

                    std::vector<frame_t> all_frames;
                    all_frames.reserve(NUM_FRAMES_PER_ZMQ_MESSAGE);

                    for (int i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
                        if (dab_msg->buflen[i] <= 0 or dab_msg->buflen[i] > 6144) {
                            etiLog.level(error) << "ZeroMQ buffer " << i <<
                                " has invalid length " << dab_msg->buflen[i];
                            error_count++;
                        }
                        else {
                            frame_t frame;
                            frame.data.resize(6144, 0x55);
                            frame.received_at = received_at;

                            const int framesize = dab_msg->buflen[i];

                            memcpy(frame.data.data(),
                                    ((uint8_t*)incoming.data()) + offset,
                                    framesize);

                            const int fct = frame.data[4];

                            const int expected_fct = (previous_fct + 1) % 250;
                            if (previous_fct != -1 and expected_fct != fct) {
                                etiLog.level(error) << "ETI wrong frame counter FCT=" << fct << " expected " << expected_fct;
                                throw FCTDiscontinuity();
                            }
                            previous_fct = fct;

                            all_frames.push_back(std::move(frame));

                            offset += framesize;
                        }
                    }

                    for (auto &f : all_frames) {
                        size_t consumed_bytes = 0;

                        f.metadata = get_md_one_frame(
                                static_cast<uint8_t*>(incoming.data()) + offset,
                                incoming.size() - offset,
                                &consumed_bytes);

                        offset += consumed_bytes;
                    }

                    for (auto &f : all_frames) {
                        edisender.push_frame(std::move(f));
                    }
                }
            }

            etiLog.level(info) << "Backoff " << backoff_after_reset_ms <<
                "ms due to ZMQ input (" << source_url << ") timeout";
        }
        catch (const FCTDiscontinuity&) {
            etiLog.level(info) << "Backoff " << backoff_after_reset_ms << "ms FCT discontinuity";
        }

        zmq_sock.close();
        std::this_thread::sleep_for(std::chrono::milliseconds(backoff_after_reset_ms));
    }

    return 0;
}

int main(int argc, char **argv)
{
    // Version handling is done very early to ensure nothing else but the version gets printed out
    if (argc == 2 and strcmp(argv[1], "--version") == 0) {
        fprintf(stdout, "%s\n",
#if defined(GITVERSION)
                GITVERSION
#else
                PACKAGE_VERSION
#endif
               );
        return 0;
    }

    etiLog.level(info) << "ZMQ2EDI converter from " <<
        PACKAGE_NAME << " " <<
#if defined(GITVERSION)
        GITVERSION <<
#else
        PACKAGE_VERSION <<
#endif
        " starting up";

    int ret = 1;

    try {
        ret = start(argc, argv);

        // To make sure things get printed to stderr
        std::this_thread::sleep_for(std::chrono::milliseconds(300));
    }
    catch (const std::runtime_error &e) {
        etiLog.level(error) << "Runtime error: " << e.what();
    }
    catch (const std::logic_error &e) {
        etiLog.level(error) << "Logic error! " << e.what();
    }

    return ret;
}