aboutsummaryrefslogtreecommitdiffstats
path: root/src/DabMod.cpp
blob: 64e557c7b7ab1b8d6677ee043a3d3a2da6c31cb2 (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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   Her Majesty the Queen in Right of Canada (Communications Research
   Center Canada)

   Includes modifications for which no copyright is claimed
   2012, Matthias P. Braendli, matthias.braendli@mpb.li
 */
/*
   This file is part of CRC-DADMOD.

   CRC-DADMOD 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.

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

#ifdef HAVE_CONFIG_H
#   include "config.h"
#endif

#include "porting.h"

#include "Log.h"
#include "DabModulator.h"
#include "InputMemory.h"
#include "OutputFile.h"
#include "OutputUHD.h"
#include "InputReader.h"
#include "PcDebug.h"
#include "TimestampDecoder.h"
#include "FIRFilter.h"
#include "RemoteControl.h"

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <complex>
#include <string>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdexcept>
#include <signal.h>

#ifdef HAVE_NETINET_IN_H
#   include <netinet/in.h>
#endif

#ifdef HAVE_DECL__MM_MALLOC
#   include <mm_malloc.h>
#else
#   define memalign(a, b)   malloc(b)
#endif


typedef std::complex<float> complexf;


bool running = true;

void signalHandler(int signalNb)
{
    PDEBUG("signalHandler(%i)\n", signalNb);

    running = false;
}


void printUsage(char* progName, FILE* out = stderr)
{
    fprintf(out, "Welcome to %s %s%s, compiled at %s, %s\n\n",
            PACKAGE, VERSION, HGVERSION, __DATE__, __TIME__);
    fprintf(out, "Usage with configuration file:\n");
    fprintf(out, "\t%s -C config_file.ini\n\n", progName);

    fprintf(out, "Usage with command line options:\n");
    fprintf(out, "\t%s"
            " [input]"
            " (-f filename | -u uhddevice -F frequency) "
            " [-G txgain]"
            " [-o offset]"
            " [-O offsetfile]"
            " [-T filter_taps_file]"
            " [-a amplitude]"
            " [-c clockrate]"
            " [-g gainMode]"
            " [-h]"
            " [-l]"
            " [-m dabMode]"
            " [-r samplingRate]"
            "\n", progName);
    fprintf(out, "Where:\n");
    fprintf(out, "input:         ETI input filename (default: stdin).\n");
    fprintf(out, "-f name:       Use file output with given filename. (use /dev/stdout for standard output)\n");
    fprintf(out, "-u device:     Use UHD output with given device string. (use "" for default device)\n");
    fprintf(out, "-F frequency:  Set the transmit frequency when using UHD output. (mandatory option when using UHD)\n");
    fprintf(out, "-G txgain:     Set the transmit gain for the UHD driver (default: 0)\n");
    fprintf(out, "-o:            (UHD only) Set the timestamp offset added to the timestamp in the ETI. The offset is a double.\n");
    fprintf(out, "-O:            (UHD only) Set the file containing the timestamp offset added to the timestamp in the ETI.\n"
                                 "The file is read every six seconds, and must contain a double value.\n");
    fprintf(out, "                  Specifying either -o or -O has two implications: It enables synchronous transmission,\n"
                 "                  requiring an external REFCLK and PPS signal and frames that do not contain a valid timestamp\n"
                 "                  get muted.\n\n");
    fprintf(out, "-T taps_file:  Enable filtering before the output, using the specified file containing the filter taps.\n");
    fprintf(out, "-a gain:       Apply digital amplitude gain.\n");
    fprintf(out, "-c rate:       Set the DAC clock rate and enable Cic Equalisation.\n");
    fprintf(out, "-g:            Set computation gain mode: "
            "%u FIX, %u MAX, %u VAR\n", GAIN_FIX, GAIN_MAX, GAIN_VAR);
    fprintf(out, "-h:            Print this help.\n");
    fprintf(out, "-l:            Loop file when reach end of file.\n");
    fprintf(out, "-m mode:       Set DAB mode: (0: auto, 1-4: force).\n");
    fprintf(out, "-r rate:       Set output sampling rate (default: 2048000).\n");
}


void printVersion(FILE *out = stderr)
{
    fprintf(out, "Welcome to %s %s, compiled at %s, %s\n\n",
            PACKAGE, VERSION, __DATE__, __TIME__);
    fprintf(out, "CRC-DABMOD is copyright (C) Her Majesty the Queen in Right of Canada,\n"
            "    2009, 2010, 2011, 2012 Communications Research Centre (CRC).\n"
            "\n"
            "    This program is available free of charge and is licensed to you on a\n"
            "    non-exclusive basis; you may not redistribute it.\n"
            "\n"
            "    This program is provided \"AS IS\" in the hope that it will be useful, but\n"
            "    WITHOUT ANY WARRANTY with respect to its accurancy or usefulness; witout\n"
            "    even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR\n"
            "    PURPOSE and NONINFRINGEMENT.\n"
            "\n"
            "    In no event shall CRC be LIABLE for any LOSS, DAMAGE or COST that may be\n"
            "    incurred in connection with the use of this software.\n"
            "\n"
            "CRC-DABMOD makes use of the following open source packages:\n"
            "    Kiss FFT v1.2.9 (Revised BSD) - http://kissfft.sourceforge.net/\n"
           );

}


int main(int argc, char* argv[])
{
    int ret = 0;
    bool loop = false;
    std::string inputName = "";

    const char* outputName;
    int useFileOutput = 0;
    int useUHDOutput = 0;

    uint64_t frame = 0;
    size_t outputRate = 2048000;
    size_t clockRate = 0;
    unsigned dabMode = 0;
    float amplitude = 1.0f;
    GainMode gainMode = GAIN_VAR;
    Buffer data;

    std::string filterTapsFilename = "";

    // Two configuration sources exist: command line and (new) INI file
    bool use_configuration_cmdline = false;
    bool use_configuration_file = false;
    std::string configuration_file;

    OutputUHDConfig outputuhd_conf;

    // To handle the timestamp offset of the modulator
    struct modulator_offset_config modconf;
    modconf.use_offset_file = false;
    modconf.use_offset_fixed = false;
    modconf.delay_calculation_pipeline_stages = 0;

    Flowgraph* flowgraph = NULL;
    DabModulator* modulator = NULL;
    InputMemory* input = NULL;
    ModOutput* output = NULL;

    BaseRemoteController* rc = NULL;

    Logger logger;
    InputFileReader inputFileReader(logger);

    signal(SIGINT, signalHandler);

    // Set timezone to UTC
    setenv("TZ", "", 1);
    tzset();

    while (true) {
        int c = getopt(argc, argv, "a:C:c:f:F:g:G:hlm:o:O:r:T:u:V");
        if (c == -1) {
            break;
        }

        if (c != 'C') {
            use_configuration_cmdline = true;
        }
            
        switch (c) {
        case 'C':
            use_configuration_file = true;
            configuration_file = optarg;
            break;

        case 'a':
            amplitude = strtof(optarg, NULL);
            break;
        case 'c':
            clockRate = strtol(optarg, NULL, 0);
            break;
        case 'f':
            if (useUHDOutput) {
                fprintf(stderr, "Options -u and -f are mutually exclusive\n");
                goto END_MAIN;
            }
            outputName = optarg;
            useFileOutput = 1;
            break;
        case 'F':
            outputuhd_conf.frequency = strtof(optarg, NULL);
            break;
        case 'g':
            gainMode = (GainMode)strtol(optarg, NULL, 0);
            break;
        case 'G':
            outputuhd_conf.txgain = (int)strtol(optarg, NULL, 10);
            break;
        case 'l':
            loop = true;
            break;
        case 'o':
            if (modconf.use_offset_file)
            {
                fprintf(stderr, "Options -o and -O are mutually exclusive\n");
                goto END_MAIN;
            }
            modconf.use_offset_fixed = true;
            modconf.offset_fixed = strtod(optarg, NULL);
            outputuhd_conf.enableSync = true;
            break;
        case 'O':
            if (modconf.use_offset_fixed)
            {
                fprintf(stderr, "Options -o and -O are mutually exclusive\n");
                goto END_MAIN;
            }
            modconf.use_offset_file = true;
            modconf.offset_filename = std::string(optarg);
            outputuhd_conf.enableSync = true;
            break;
        case 'm':
            dabMode = strtol(optarg, NULL, 0);
            break;
        case 'r':
            outputRate = strtol(optarg, NULL, 0);
            break;
        case 'T':
            filterTapsFilename = optarg;
            break;
        case 'u':
            if (useFileOutput) {
                fprintf(stderr, "Options -u and -f are mutually exclusive\n");
                goto END_MAIN;
            }
            outputuhd_conf.device = optarg;
            useUHDOutput = 1;
            break;
        case 'V':
            printVersion();
            goto END_MAIN;
            break;
        case '?':
        case 'h':
            printUsage(argv[0]);
            goto END_MAIN;
            break;
        default:
            fprintf(stderr, "Option '%c' not coded yet!\n", c);
            ret = -1;
            goto END_MAIN;
        }
    }

    if (use_configuration_file && use_configuration_cmdline) {
        fprintf(stderr, "Warning: configuration file and command line parameters are defined:\n\t"
                        "Command line parameters override settings in the configuration file !\n");
    }

    if (use_configuration_file) {
        // First read parameters from the file
        using boost::property_tree::ptree;
        ptree pt;

        try {
            read_ini(configuration_file, pt);
        }
        catch (boost::property_tree::ini_parser::ini_parser_error &e)
        {
            fprintf(stderr, "Error, cannot read configuration file '%s'\n", configuration_file.c_str());
            goto END_MAIN;
        }

        // remote controller:
        if (pt.get("remotecontrol.telnet", 0) == 1) {
            try {
                int telnetport = pt.get<int>("remotecontrol.telnetport");
                RemoteControllerTelnet* telnetrc = new RemoteControllerTelnet(telnetport);
                telnetrc->start();
                rc = telnetrc;
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       telnet remote control enabled, but no telnetport defined.\n";
                goto END_MAIN;
            }
        }
        else {
            rc = new RemoteControllerDummy();
        }


        // input params:
        if (pt.get("input.loop", 0) == 1) {
            loop = true;
        }

        inputName = pt.get("input.filename", "/dev/stdin");

        // log parameters:
        if (pt.get("log.syslog", 0) == 1) {
            LogToSyslog* log_syslog = new LogToSyslog();
            logger.register_backend(log_syslog);
        }

        if (pt.get("log.filelog", 0) == 1) {
            std::string logfilename;
            try {
                 logfilename = pt.get<std::string>("log.filename");
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       Configuration enables file log, but does not specify log filename\n";
                goto END_MAIN;
            }

            LogToFile* log_file = new LogToFile(logfilename);
            logger.register_backend(log_file);
        }


        // modulator parameters:
        gainMode = (GainMode)pt.get("modulator.gainmode", 0);
        dabMode = pt.get("modulator.mode", dabMode);
        clockRate = pt.get("modulator.dac_clk_rate", (size_t)0);
        amplitude = pt.get("modulator.digital_gain", amplitude);
        outputRate = pt.get("modulator.rate", outputRate);
        
        // FIR Filter parameters:
        if (pt.get("firfilter.enabled", 0) == 1) {
            try {
                filterTapsFilename = pt.get<std::string>("firfilter.filtertapsfile");
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       Configuration enables firfilter, but does not specify filter taps file\n";
                goto END_MAIN;
            }
        }

        // Output options
        std::string output_selected;
        try {
             output_selected = pt.get<std::string>("output.output");
        }
        catch (std::exception &e) {
            std::cerr << "Error: " << e.what() << "\n";
            std::cerr << "       Configuration does not specify output\n";
            goto END_MAIN;
        }

        if (output_selected == "file") {
            try {
                outputName = pt.get<std::string>("fileoutput.filename").c_str();
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       Configuration does not specify file name for file output\n";
                goto END_MAIN;
            }
            useFileOutput = 1;
        }
        else if (output_selected == "uhd") {
            outputuhd_conf.device = pt.get("uhdoutput.device", "").c_str();
            outputuhd_conf.txgain = pt.get("uhdoutput.txgain", 0);
            try {
                outputuhd_conf.frequency = pt.get<double>("uhdoutput.frequency");
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       UHD output enabled, but no frequency defined.\n";
                goto END_MAIN;
            }

            outputuhd_conf.refclk_src = pt.get("uhdoutput.refclk_source", "int");
            outputuhd_conf.pps_src = pt.get("uhdoutput.pps_source", "int");
            outputuhd_conf.pps_polarity = pt.get("uhdoutput.pps_polarity", "pos");

            string behave = pt.get("uhdoutput.behaviour_refclk_lock_lost", "ignore");

            if (behave == "crash") {
                outputuhd_conf.refclk_lock_loss_behaviour = CRASH;
            }
            else if (behave == "ignore") {
                outputuhd_conf.refclk_lock_loss_behaviour = IGNORE;
            }
            else {
                std::cerr << "Error: UHD output: behaviour_refclk_lock_lost invalid." << std::endl;
                goto END_MAIN;
            }

            useUHDOutput = 1;
        }
        else {
            std::cerr << "Error: Invalid output defined.\n";
            goto END_MAIN;
        }

        outputuhd_conf.enableSync = (pt.get("delaymanagement.synchronous", 0) == 1);
        if (outputuhd_conf.enableSync) {
            try {
                std::string delay_mgmt = pt.get<std::string>("delaymanagement.management");
                if (delay_mgmt == "fixed") {
                    modconf.offset_fixed = pt.get<double>("delaymanagement.fixedoffset");
                    modconf.use_offset_fixed = true;
                }
                else if (delay_mgmt == "dynamic") {
                    modconf.offset_filename = pt.get<std::string>("delaymanagement.dynamicoffsetfile");
                    modconf.use_offset_file = true;
                }
                else {
                    throw std::runtime_error("invalid management value");
                }
            }
            catch (std::exception &e) {
                std::cerr << "Error: " << e.what() << "\n";
                std::cerr << "       Synchronised transmission enabled, but delay management specification is incomplete.\n";
                goto END_MAIN;
            }
        }

        outputuhd_conf.muteNoTimestamps = (pt.get("delaymanagement.mutenotimestamps", 0) == 1);
    }

    logger.level(info) << "starting up";

    if (!(modconf.use_offset_file || modconf.use_offset_fixed)) {
        fprintf(stderr, "No Modulator offset defined, setting to 0\n");
        logger.level(debug) << "No Modulator offset defined, setting to 0";
        modconf.use_offset_fixed = true;
        modconf.offset_fixed = 0;
    }

    // When using the FIRFilter, increase the modulator offset pipelining delay
    // by the correct amount
    if (filterTapsFilename != "") {
        modconf.delay_calculation_pipeline_stages += FIRFILTER_PIPELINE_DELAY;
    }


    // Setting ETI input filename
    if (inputName == "") {
        if (optind < argc) {
            inputName = argv[optind++];
        } else {
            inputName = "/dev/stdin";
        }
    }
    // Checking unused arguments
    if (optind != argc) {
        fprintf(stderr, "Invalid arguments:");
        while (optind != argc) {
            fprintf(stderr, " %s", argv[optind++]);
        }
        fprintf(stderr, "\n");
        printUsage(argv[0]);
        ret = -1;
        logger.level(error) << "Received invalid command line arguments";
        goto END_MAIN;
    }

    if (!useFileOutput && !useUHDOutput) {
        logger.level(error) << "Output not specified";
        fprintf(stderr, "Must specify output !");
        goto END_MAIN;
    }

    // Print settings
    fprintf(stderr, "Input\n");
    fprintf(stderr, "  Name: %s\n", inputName.c_str());
    fprintf(stderr, "Output\n");
    if (useUHDOutput) {
        fprintf(stderr, " UHD, Device: %s\n", outputuhd_conf.device);
    }
    else if (useFileOutput) {
        fprintf(stderr, "  Name: %s\n", outputName);
    }
    fprintf(stderr, "  Sampling rate: ");
    if (outputRate > 1000) {
        if (outputRate > 1000000) {
            fprintf(stderr, "%.4g MHz\n", outputRate / 1000000.0f);
        } else {
            fprintf(stderr, "%.4g kHz\n", outputRate / 1000.0f);
        }
    } else {
        fprintf(stderr, "%zu Hz\n", outputRate);
    }

    // Opening ETI input file
    if (inputFileReader.Open(inputName) == -1) {
        fprintf(stderr, "Unable to open input file!\n");
        logger.level(error) << "Unable to open input file!";
        ret = -1;
        goto END_MAIN;
    }

    if (useFileOutput) {
        // Opening COFDM output file
        if (outputName != NULL) {
            fprintf(stderr, "Using file output\n");
            output = new OutputFile(outputName);
        }
    }
    else if (useUHDOutput) {
        fprintf(stderr, "Using UHD output\n");
        amplitude /= 32000.0f;
        outputuhd_conf.sampleRate = outputRate;
        try {
            output = new OutputUHD(outputuhd_conf, logger);
            ((OutputUHD*)output)->enrol_at(*rc);
        }
        catch (std::exception& e) {
            logger.level(error) << "UHD initialisation failed:" << e.what();
            goto END_MAIN;
        }
        
    }

    flowgraph = new Flowgraph();
    data.setLength(6144);
    input = new InputMemory(&data);
    modulator = new DabModulator(modconf, rc, logger, outputRate, clockRate,
            dabMode, gainMode, amplitude, filterTapsFilename);
    flowgraph->connect(input, modulator);
    flowgraph->connect(modulator, output);

    if (useUHDOutput) {
        ((OutputUHD*)output)->setETIReader(modulator->getEtiReader());
    }

    inputFileReader.PrintInfo();

    try {
        while (running) {

            int framesize;

            PDEBUG("*****************************************\n");
            PDEBUG("* Starting main loop\n");
            PDEBUG("*****************************************\n");
            while ((framesize = inputFileReader.GetNextFrame(data.getData())) > 0) {
                if (!running) {
                    break;
                }

                frame++;

                PDEBUG("*****************************************\n");
                PDEBUG("* Read frame %lu\n", frame);
                PDEBUG("*****************************************\n");
                fprintf(stderr, "Reading frame %lu\n", frame);

                ////////////////////////////////////////////////////////////////
                // Proccessing data
                ////////////////////////////////////////////////////////////////
                flowgraph->run();
            }
            if (framesize == 0) {
                fprintf(stderr, "End of file reached.\n");
                if (!loop) {
                    running = false;
                } else {
                    fprintf(stderr, "Rewinding file.\n");
                    inputFileReader.Rewind();
                }
            }
            else {
                fprintf(stderr, "Input read error.\n");
                running = false;
            }
        }
    } catch (std::exception& e) {
        fprintf(stderr, "EXCEPTION: %s\n", e.what());
        ret = -1;
    }

END_MAIN:
    ////////////////////////////////////////////////////////////////////////
    // Cleaning things
    ////////////////////////////////////////////////////////////////////////
    fprintf(stderr, "\n\n");
    fprintf(stderr, "%lu DAB frames encoded\n", frame);
    fprintf(stderr, "%f seconds encoded\n", (float)frame * 0.024f);

    fprintf(stderr, "\nCleaning flowgraph...\n");
    delete flowgraph;

    // Cif
    fprintf(stderr, "\nCleaning buffers...\n");

    logger.level(info) << "Terminating";

    return ret;
}