aboutsummaryrefslogtreecommitdiffstats
path: root/src/Utils.cpp
blob: f947acd99898c49a671ad9702ec226c13bd98b85 (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
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   Her Majesty the Queen in Right of Canada (Communications Research
   Center Canada)

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

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

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

#include "Utils.h"

#include <ctime>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <pthread.h>
#if defined(HAVE_PRCTL)
#  include <sys/prctl.h>
#endif


static void printHeader()
{
    std::cerr << "ODR-DabMod version " <<
#if defined(GITVERSION)
            GITVERSION
#else
            VERSION
#endif
            ", compiled at " << __DATE__ << ", " << __TIME__ << std::endl;

    std::cerr << "Compiled with features: " <<
#if defined(HAVE_ZEROMQ)
        "zeromq " <<
#endif
#if defined(HAVE_OUTPUT_UHD)
        "output_uhd " <<
#endif
#if defined(HAVE_SOAPYSDR)
        "output_soapysdr " <<
#endif
#if defined(HAVE_LIMESDR)
        "output_limesdr " <<
#endif
#if defined(__FAST_MATH__)
        "fast-math " <<
#endif
#if defined(__SSE__)
        "SSE " <<
#endif
#if defined(__ARM_NEON)
        "NEON " <<
#endif
        "\n";
}

void printUsage(const char* progName)
{
    FILE* out = stderr;
    fprintf(out, "Usage with configuration file:\n");
    fprintf(out, "\t%s config_file.ini\n\n", progName);

    fprintf(out, "Usage with command line options:\n");
    fprintf(out, "\t%s"
            " input"
            " (-f filename -F format | -u uhddevice -F frequency)"
            " [-o offset]"
            "\n\t"
            " [-G txgain]"
            " [-T filter_taps_file]"
            " [-a gain]"
            " [-c clockrate]"
            "\n\t"
            " [-g gainMode]"
            " [-m dabMode]"
            " [-r samplingRate]"
            " [-l]"
            " [-h]"
            "\n", progName);
    fprintf(out, "Where:\n");
    fprintf(out, "input:         ETI input filename (default: stdin), or\n");
    fprintf(out, "                  tcp://source:port for ETI-over-TCP input, or\n");
    fprintf(out, "                  udp://:port for EDI input.\n");
    fprintf(out, "-f name:       Use file output with given filename. (use /dev/stdout for standard output)\n");
    fprintf(out, "-F format:     Set the output format (see doc/example.ini for formats) for the file output.\n");
    fprintf(out, "-o:            Set the timestamp offset added to the timestamp in the ETI. The offset is a double.\n");
    fprintf(out, "                  Specifying this option 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, "-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, "-T taps_file:  Enable filtering before the output, using the specified file containing the filter taps.\n");
    fprintf(out, "               Use 'default' as taps_file to use the internal 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 gainmode:   Set computation gain mode: fix, max or var\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\n");
    fprintf(out, "-l:            Loop file when reach end of file.\n");
    fprintf(out, "-h:            Print this help.\n");
}


void printVersion(void)
{
    FILE *out = stderr;
    fprintf(out,
            "    ODR-DabMod is copyright (C) Her Majesty the Queen in Right of Canada,\n"
            "    2005 -- 2012 Communications Research Centre (CRC),\n"
            "     and\n"
            "    Copyright (C) 2023 Matthias P. Braendli, matthias.braendli@mpb.li\n"
            "\n"
            "    http://opendigitalradio.org\n"
            "\n"
            "    ODR-DabMod is free software: you can redistribute it and/or modify it\n"
            "    under the terms of the GNU General Public License as published by the\n"
            "    Free Software Foundation, either version 3 of the License, or (at your\n"
            "    option) any later version.\n"
            "\n"
            "    ODR-DabMod is distributed in the hope that it will be useful, but\n"
            "    WITHOUT ANY WARRANTY; without even the implied warranty of\n"
            "    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
            "    General Public License for more details.\n"
            "\n"
            "    You should have received a copy of the GNU General Public License along\n"
            "    with ODR-DabMod.  If not, see <http://www.gnu.org/licenses/>.\n"
            "\n"
           );
}

void printStartupInfo()
{
    printHeader();
}

void printModSettings(const mod_settings_t& mod_settings)
{
    std::stringstream ss;
    // Print settings
    ss << "Input\n";
    ss << "  Type: " << mod_settings.inputTransport << "\n";
    ss << "  Source: " << mod_settings.inputName << "\n";

    ss << "Output\n";

    if (mod_settings.useFileOutput) {
        ss << "  Name: " << mod_settings.outputName << "\n";
    }
#if defined(HAVE_OUTPUT_UHD)
    else if (mod_settings.useUHDOutput) {
        ss << " UHD\n" <<
            "  Device: " << mod_settings.sdr_device_config.device << "\n" <<
            "  Subdevice: " <<
                mod_settings.sdr_device_config.subDevice << "\n" <<
            "  master_clock_rate: " <<
                mod_settings.sdr_device_config.masterClockRate << "\n" <<
            "  refclk: " <<
                mod_settings.sdr_device_config.refclk_src << "\n" <<
            "  pps source: " <<
                mod_settings.sdr_device_config.pps_src << "\n";
    }
#endif
#if defined(HAVE_SOAPYSDR)
    else if (mod_settings.useSoapyOutput) {
        ss << " SoapySDR\n"
            "  Device: " << mod_settings.sdr_device_config.device << "\n" <<
            "  master_clock_rate: " <<
                mod_settings.sdr_device_config.masterClockRate << "\n";
    }
#endif
#if defined(HAVE_DEXTER)
    else if (mod_settings.useDexterOutput) {
        ss << " PrecisionWave DEXTER\n";
    }
#endif
#if defined(HAVE_LIMESDR)
    else if (mod_settings.useLimeOutput) {
        ss << " LimeSDR\n"
            "  Device: " << mod_settings.sdr_device_config.device << "\n" <<
            "  master_clock_rate: " <<
                mod_settings.sdr_device_config.masterClockRate << "\n";
    }
#endif
#if defined(HAVE_BLADERF)
    else if (mod_settings.useBladeRFOutput) {
        ss << " BladeRF\n"
            "  Device: " << mod_settings.sdr_device_config.device << "\n" <<
            "  refclk: " << mod_settings.sdr_device_config.refclk_src << "\n";
    }
#endif
    else if (mod_settings.useZeroMQOutput) {
        ss << " ZeroMQ\n" <<
            "  Listening on: " << mod_settings.outputName << "\n" <<
            "  Socket type : " << mod_settings.zmqOutputSocketType << "\n";
    }

    ss << "  Sampling rate: ";
    if (mod_settings.outputRate > 1000) {
        if (mod_settings.outputRate > 1000000) {
            ss << std::fixed << std::setprecision(4) <<
                mod_settings.outputRate / 1000000.0 <<
                " MHz\n";
        }
        else {
            ss << std::fixed << std::setprecision(4) <<
                mod_settings.outputRate / 1000.0 <<
                " kHz\n";
        }
    }
    else {
        ss << std::fixed << std::setprecision(4) <<
            mod_settings.outputRate << " Hz\n";
    }
    fprintf(stderr, "%s", ss.str().c_str());
}

int set_realtime_prio(int prio)
{
    // Set thread priority to realtime
    const int policy = SCHED_RR;
    sched_param sp;
    sp.sched_priority = sched_get_priority_min(policy) + prio;
    int ret = pthread_setschedparam(pthread_self(), policy, &sp);
    return ret;
}

void set_thread_name(const char *name)
{
#if defined(HAVE_PRCTL)
    prctl(PR_SET_NAME,name,0,0,0);
#endif
}

double parse_channel(const std::string& chan)
{
    double freq;
    if      (chan == "5A") freq = 174928000;
    else if (chan == "5B") freq = 176640000;
    else if (chan == "5C") freq = 178352000;
    else if (chan == "5D") freq = 180064000;
    else if (chan == "6A") freq = 181936000;
    else if (chan == "6B") freq = 183648000;
    else if (chan == "6C") freq = 185360000;
    else if (chan == "6D") freq = 187072000;
    else if (chan == "7A") freq = 188928000;
    else if (chan == "7B") freq = 190640000;
    else if (chan == "7C") freq = 192352000;
    else if (chan == "7D") freq = 194064000;
    else if (chan == "8A") freq = 195936000;
    else if (chan == "8B") freq = 197648000;
    else if (chan == "8C") freq = 199360000;
    else if (chan == "8D") freq = 201072000;
    else if (chan == "9A") freq = 202928000;
    else if (chan == "9B") freq = 204640000;
    else if (chan == "9C") freq = 206352000;
    else if (chan == "9D") freq = 208064000;
    else if (chan == "10A") freq = 209936000;
    else if (chan == "10B") freq = 211648000;
    else if (chan == "10C") freq = 213360000;
    else if (chan == "10D") freq = 215072000;
    else if (chan == "11A") freq = 216928000;
    else if (chan == "11B") freq = 218640000;
    else if (chan == "11C") freq = 220352000;
    else if (chan == "11D") freq = 222064000;
    else if (chan == "12A") freq = 223936000;
    else if (chan == "12B") freq = 225648000;
    else if (chan == "12C") freq = 227360000;
    else if (chan == "12D") freq = 229072000;
    else if (chan == "13A") freq = 230784000;
    else if (chan == "13B") freq = 232496000;
    else if (chan == "13C") freq = 234208000;
    else if (chan == "13D") freq = 235776000;
    else if (chan == "13E") freq = 237488000;
    else if (chan == "13F") freq = 239200000;
    else {
        std::cerr << "Channel " << chan << " does not exist in table\n";
        throw std::out_of_range("channel out of range");
    }
    return freq;
}

std::optional<std::string> convert_frequency_to_channel(double frequency)
{
    const int freq = round(frequency);
    std::string chan;
    if      (freq == 174928000) chan = "5A";
    else if (freq == 176640000) chan = "5B";
    else if (freq == 178352000) chan = "5C";
    else if (freq == 180064000) chan = "5D";
    else if (freq == 181936000) chan = "6A";
    else if (freq == 183648000) chan = "6B";
    else if (freq == 185360000) chan = "6C";
    else if (freq == 187072000) chan = "6D";
    else if (freq == 188928000) chan = "7A";
    else if (freq == 190640000) chan = "7B";
    else if (freq == 192352000) chan = "7C";
    else if (freq == 194064000) chan = "7D";
    else if (freq == 195936000) chan = "8A";
    else if (freq == 197648000) chan = "8B";
    else if (freq == 199360000) chan = "8C";
    else if (freq == 201072000) chan = "8D";
    else if (freq == 202928000) chan = "9A";
    else if (freq == 204640000) chan = "9B";
    else if (freq == 206352000) chan = "9C";
    else if (freq == 208064000) chan = "9D";
    else if (freq == 209936000) chan = "10A";
    else if (freq == 211648000) chan = "10B";
    else if (freq == 213360000) chan = "10C";
    else if (freq == 215072000) chan = "10D";
    else if (freq == 216928000) chan = "11A";
    else if (freq == 218640000) chan = "11B";
    else if (freq == 220352000) chan = "11C";
    else if (freq == 222064000) chan = "11D";
    else if (freq == 223936000) chan = "12A";
    else if (freq == 225648000) chan = "12B";
    else if (freq == 227360000) chan = "12C";
    else if (freq == 229072000) chan = "12D";
    else if (freq == 230784000) chan = "13A";
    else if (freq == 232496000) chan = "13B";
    else if (freq == 234208000) chan = "13C";
    else if (freq == 235776000) chan = "13D";
    else if (freq == 237488000) chan = "13E";
    else if (freq == 239200000) chan = "13F";
    else { return std::nullopt; }

    return chan;
}

std::chrono::milliseconds transmission_frame_duration(unsigned int dabmode)
{
    using namespace std::chrono;
    switch (dabmode) {
        case 1: return milliseconds(96);
        case 2: return milliseconds(24);
        case 3: return milliseconds(24);
        case 4: return milliseconds(48);
        default:
            throw std::runtime_error("invalid DAB mode");
    }
}


time_t get_clock_realtime_seconds()
{
    struct timespec t;
    if (clock_gettime(CLOCK_REALTIME, &t) != 0) {
        throw std::runtime_error(std::string("Failed to retrieve CLOCK_REALTIME") + strerror(errno));
    }

    return t.tv_sec;
}