1
0
mirror of https://github.com/JvanKatwijk/dab-cmdline synced 2025-10-06 08:02:52 +02:00
Files

636 lines
18 KiB
C++
Raw Permalink Normal View History

2020-09-06 19:41:21 +02:00
#
2018-08-26 17:47:28 +02:00
/*
2025-07-01 18:38:24 +02:00
* Copyright (C) 2015, 2016, 2025
2018-08-26 17:47:28 +02:00
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of the DAB-library
*
* DAB-library 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 2 of the License, or
* (at your option) any later version.
*
* DAB-library 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 DAB-library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* E X A M P L E P R O G R A M
* for the DAB-library
*/
#include <stdlib.h>
2018-08-26 17:47:28 +02:00
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <cstdio>
#include <vector>
2020-04-18 13:16:54 +02:00
#include <iostream>
2018-08-26 17:47:28 +02:00
#include "dab-api.h"
#include "includes/support/band-handler.h"
2025-06-25 20:04:48 +02:00
#include "tii-handler.h"
2018-08-26 17:47:28 +02:00
#include "service-printer.h"
#ifdef HAVE_SDRPLAY
#include "sdrplay-handler.h"
#elif HAVE_AIRSPY
#include "airspy-handler.h"
#elif HAVE_RTLSDR
#include "rtlsdr-handler.h"
2018-08-28 09:36:39 +02:00
#elif HAVE_HACKRF
#include "hackrf-handler.h"
2020-06-17 19:53:45 +02:00
#elif HAVE_LIME
#include "lime-handler.h"
#elif HAVE_SDRPLAY_V3
#include "sdrplay-handler-v3.h"
2018-08-26 17:47:28 +02:00
#endif
#include <atomic>
#include <thread>
using std::cerr;
using std::endl;
void printOptions (); // forward declaration
2018-08-26 17:47:28 +02:00
// we deal with some callbacks, so we have some data that needs
// to be accessed from global contexts
static
std::atomic<bool> run;
static
void *theRadio = nullptr;
2018-08-26 17:47:28 +02:00
2025-06-25 20:04:48 +02:00
static
tiiHandler the_tiiHandler;
2018-08-26 17:47:28 +02:00
static
std::atomic<bool>timeSynced;
static
std::atomic<bool>timesyncSet;
static
std::atomic<bool>ensembleRecognized;
static
2018-10-26 09:22:17 +02:00
FILE *outFile = stdout;
2018-08-26 17:47:28 +02:00
static void sighandler (int signum) {
fprintf (stderr, "Signal caught, terminating!\n");
run. store (false);
}
static
void syncsignalHandler (bool b, void *userData) {
timeSynced. store (b);
timesyncSet. store (true);
(void)userData;
}
//
std::string ensembleName;
uint32_t ensembleId;
static
2025-06-25 20:04:48 +02:00
void name_of_ensemble (const std::string &name, int Id, void *userData) {
2018-08-26 17:47:28 +02:00
fprintf (stderr, "ensemble %s is (%X) recognized\n",
2025-06-25 20:04:48 +02:00
name. c_str (), (uint32_t)Id);
2018-08-26 17:47:28 +02:00
ensembleRecognized. store (true);
ensembleName = name;
ensembleId = Id;
}
std::vector<std::string> programNames;
std::vector<int> programSIds;
static
2025-06-25 20:04:48 +02:00
void serviceName (const std::string &s, int SId,
uint16_t subChId, void *userdata) {
2018-08-26 17:47:28 +02:00
for (std::vector<std::string>::iterator it = programNames.begin();
it != programNames. end(); ++it)
2025-06-25 20:04:48 +02:00
if (*it == s)
2018-08-26 17:47:28 +02:00
return;
2025-06-25 20:04:48 +02:00
programNames. push_back (s);
2018-08-26 17:47:28 +02:00
programSIds . push_back (SId);
2025-06-25 20:04:48 +02:00
// fprintf (stderr, "program %s is part of the ensemble\n", s. c_str ());
}
static
void tii_data_Handler (tiiData *theData, void *ctx) {
the_tiiHandler. add (*theData);
(void)ctx;
2018-08-26 17:47:28 +02:00
}
static
2021-04-26 19:23:13 +02:00
void programdata_Handler (audiodata *d, void *ctx) {
2018-08-26 17:47:28 +02:00
(void) *d;
(void)ctx;
}
//
// The function is called from within the library with
// a string, the so-called dynamic label
static
2021-04-29 16:06:56 +02:00
void dataOut_Handler (const char *dynamicLabel, void *ctx) {
2018-08-26 17:47:28 +02:00
(void)ctx;
2021-04-29 16:06:56 +02:00
// fprintf (stderr, "%s\n", dynamicLabel);
2018-08-26 17:47:28 +02:00
}
//
// Note: the function is called from the tdcHandler with a
// frame, either frame 0 or frame 1.
// The frames are packed bytes, here an additional header
// is added, a header of 8 bytes:
// the first 4 bytes for a pattern 0xFF 0x00 0xFF 0x00 0xFF
// the length of the contents, i.e. framelength without header
// is stored in bytes 5 (high byte) and byte 6.
// byte 7 contains 0x00, byte 8 contains 0x00 for frametype 0
// and 0xFF for frametype 1
// Note that the callback function is executed in the thread
// that executes the tdcHandler code.
static
void bytesOut_Handler (uint8_t *data, int16_t amount,
uint8_t type, void *ctx) {
(void)data;
(void)amount; (void)type;
2018-08-26 17:47:28 +02:00
(void)ctx;
}
//
static
void pcmHandler (int16_t *buffer, int size, int rate,
bool isStereo, void *ctx) {
(void)buffer;
(void)size;
(void)rate;
(void)ctx;
}
static
void systemData (bool flag, int16_t snr, int32_t freqOff, void *ctx) {
// fprintf (stderr, "synced = %s, snr = %d, offset = %d\n",
// flag? "on":"off", snr, freqOff);
}
static
void fibQuality (int16_t q, void *ctx) {
// fprintf (stderr, "fic quality = %d\n", q);
}
static
void mscQuality (int16_t fe, int16_t rsE, int16_t aacE, void *ctx) {
// fprintf (stderr, "msc quality = %d %d %d\n", fe, rsE, aacE);
}
int main (int argc, char **argv) {
// Default values
std::string startChannel = "5A";
2020-06-17 19:53:45 +02:00
uint8_t theMode = 1;
std::string theChannel = "5C";
2018-08-26 17:47:28 +02:00
uint8_t theBand = BAND_III;
2018-08-28 09:36:39 +02:00
#ifdef HAVE_HACKRF
int lnaGain = 40;
int vgaGain = 40;
2020-06-17 19:53:45 +02:00
int ppmOffset = 0;
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C::G:g:p:";
2020-06-17 19:53:45 +02:00
#elif HAVE_LIME
int16_t gain = 70;
std::string antenna = "Auto";
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C:G:g:X:";
2020-06-17 19:53:45 +02:00
#elif HAVE_SDRPLAY
int16_t GRdB = 30;
int16_t lnaState = 4;
bool autogain = true;
int16_t ppmOffset = 0;
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C:G:L:Qp:";
2020-06-17 19:53:45 +02:00
#elif HAVE_SDRPLAY_V3
int16_t GRdB = 30;
int16_t lnaState = 2;
2025-06-25 20:04:48 +02:00
bool autogain = true;
2020-06-17 19:53:45 +02:00
int16_t ppmOffset = 0;
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C:G:L:Qp:";
2020-06-17 19:53:45 +02:00
#elif HAVE_AIRSPY
int16_t gain = 20;
2018-08-26 17:47:28 +02:00
bool autogain = false;
2020-06-17 19:53:45 +02:00
bool rf_bias = false;
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C:G:bp:";
2020-06-17 19:53:45 +02:00
#elif HAVE_RTLSDR
int16_t gain = 50;
bool autogain = false;
int16_t ppmOffset = 0;
2020-09-04 19:13:09 +02:00
int dumpDuration = 1;
bool rawDump = false;
2025-07-01 18:38:24 +02:00
const char *optionsString = "I:F:jD:M:B:C:G:p:QR:T:";
2020-06-17 19:53:45 +02:00
#endif
2018-08-26 17:47:28 +02:00
int opt;
2025-06-29 12:43:09 +02:00
int freqSyncTime = 8;
2025-06-26 10:15:52 +02:00
int tiiSyncTime = 10;
2020-06-17 19:53:45 +02:00
bool jsonOutput = false;
2018-08-26 17:47:28 +02:00
struct sigaction sigact;
bandHandler dabBand;
deviceHandler *theDevice;
bool firstEnsemble = true;
2018-08-26 17:47:28 +02:00
2025-06-25 20:04:48 +02:00
fprintf (stderr, "dab_scanner V 2.0alfa,\n"
2020-09-06 19:41:21 +02:00
"Copyright 2018 J van Katwijk, Lazy Chair Computing\n" "2018 Hayati Ayguen\n"
"2019 J van Katwijk\n"
2025-06-25 20:04:48 +02:00
"2020 J van Katwijk\n"
"2025 J van Katwijk\n");
2018-08-26 17:47:28 +02:00
timeSynced. store (false);
timesyncSet. store (false);
run. store (false);
if (argc == 1) {
printOptions ();
exit (1);
}
2020-06-17 19:53:45 +02:00
while ((opt = getopt (argc, argv, optionsString)) != -1) {
2018-08-26 17:47:28 +02:00
switch (opt) {
case 'F':
outFile = fopen (optarg, "w");
if (outFile == nullptr)
2018-08-26 17:47:28 +02:00
outFile = stderr;
break;
case 'j':
jsonOutput = true;
break;
2018-08-26 17:47:28 +02:00
case 'D':
freqSyncTime = atoi (optarg);
break;
2025-06-26 11:45:57 +02:00
case 'I':
2025-06-26 10:15:52 +02:00
tiiSyncTime = atoi (optarg);
break;
2018-08-26 17:47:28 +02:00
case 'M':
theMode = atoi (optarg);
if (!((theMode == 1) || (theMode == 2) || (theMode == 4)))
theMode = 1;
break;
case 'B':
theBand = std::string (optarg) == std::string ("L_BAND") ?
L_BAND : BAND_III;
break;
2020-06-17 19:53:45 +02:00
case 'C':
startChannel = std::string (optarg);
break;
2020-09-04 19:13:09 +02:00
2020-06-17 19:53:45 +02:00
// device specific options
#ifdef HAVE_HACKRF
case 'G':
lnaGain = atoi (optarg);
break;
case 'g':
vgaGain = atoi (optarg);
break;
2018-08-26 17:47:28 +02:00
case 'p':
2020-06-17 19:53:45 +02:00
ppmOffset = 0;
2018-08-26 17:47:28 +02:00
break;
2020-06-17 19:53:45 +02:00
#elif HAVE_LIME
case 'G':
case 'g':
gain = atoi (optarg);
break;
case 'X':
antenna = std::string (optarg);
break;
#elif HAVE_SDRPLAY
2018-08-26 17:47:28 +02:00
case 'G':
2020-06-17 19:53:45 +02:00
GRdB = atoi (optarg);
break;
case 'L':
lnaState = atoi (optarg);
2018-08-26 17:47:28 +02:00
break;
case 'Q':
autogain = true;
break;
2020-06-17 19:53:45 +02:00
case 'p':
ppmOffset = atoi (optarg);
break;
#elif HAVE_SDRPLAY_V3
2018-08-28 09:36:39 +02:00
case 'G':
2020-06-17 19:53:45 +02:00
GRdB = atoi (optarg);
2018-08-28 09:36:39 +02:00
break;
2018-08-26 17:47:28 +02:00
2020-06-17 19:53:45 +02:00
case 'L':
lnaState = atoi (optarg);
2018-08-28 09:36:39 +02:00
break;
2020-06-17 19:53:45 +02:00
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
break;
#elif HAVE_AIRSPY
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
2018-08-26 17:47:28 +02:00
break;
2020-06-17 19:53:45 +02:00
#elif HAVE_RTLSDR
case 'G':
gain = atoi (optarg);
break;
case 'Q':
autogain = true;
break;
case 'p':
ppmOffset = atoi (optarg);
break;
2020-09-04 19:13:09 +02:00
case 'R':
rawDump = true;
break;
case 'T':
2020-09-06 19:41:21 +02:00
dumpDuration = atoi (optarg);
2020-09-04 19:13:09 +02:00
break;
2020-06-17 19:53:45 +02:00
#endif
2018-08-26 17:47:28 +02:00
default:
printOptions ();
exit (1);
}
}
//
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
2020-06-17 19:53:45 +02:00
theChannel = startChannel;
2018-08-26 17:47:28 +02:00
int32_t frequency = dabBand. Frequency (theBand, theChannel);
try {
#ifdef HAVE_SDRPLAY
theDevice = new sdrplayHandler (frequency,
2020-06-17 19:53:45 +02:00
ppmOffset,
GRdB,
lnaState,
2018-08-26 17:47:28 +02:00
autogain,
0,
0);
2020-06-17 19:53:45 +02:00
#elif HAVE_SDRPLAY_V3
theDevice = new sdrplayHandler_v3 (frequency,
ppmOffset,
GRdB,
lnaState,
autogain,
0,
0);
2018-08-26 17:47:28 +02:00
#elif HAVE_AIRSPY
theDevice = new airspyHandler (frequency,
2020-06-17 19:53:45 +02:00
ppmOffset,
gain,
rf_bias);
2018-08-26 17:47:28 +02:00
#elif HAVE_RTLSDR
theDevice = new rtlsdrHandler (frequency,
2020-06-17 19:53:45 +02:00
ppmOffset,
gain,
2018-08-26 17:47:28 +02:00
autogain);
2018-08-28 09:36:39 +02:00
#elif HAVE_HACKRF
theDevice = new hackrfHandler (frequency,
2020-06-17 19:53:45 +02:00
ppmOffset,
2018-08-28 09:36:39 +02:00
lnaGain,
vgaGain);
2018-08-26 17:47:28 +02:00
#endif
}
catch (int e) {
fprintf (stderr, "allocating device failed (%d), fatal\n", e);
exit (32);
}
2021-04-26 19:23:13 +02:00
// and with a sound device we now can create a "backend"
API_struct interface;
interface. dabMode = theMode;
2025-06-25 20:04:48 +02:00
interface. thresholdValue = 6;
2021-04-26 19:23:13 +02:00
interface. syncsignal_Handler = syncsignalHandler;
interface. systemdata_Handler = systemData;
2025-06-25 20:04:48 +02:00
interface. name_of_ensemble = name_of_ensemble;
interface. serviceName = serviceName;
2021-04-26 19:23:13 +02:00
interface. fib_quality_Handler = fibQuality;
interface. audioOut_Handler = pcmHandler;
interface. dataOut_Handler = dataOut_Handler;
interface. bytesOut_Handler = bytesOut_Handler;
interface. programdata_Handler = programdata_Handler;
interface. program_quality_Handler = mscQuality;
interface. motdata_Handler = nullptr;
2025-06-25 20:04:48 +02:00
interface. tii_data_Handler = tii_data_Handler;
2021-04-26 19:23:13 +02:00
interface. timeHandler = nullptr;
2018-08-26 17:47:28 +02:00
//
// and with a sound device we can create a "backend"
theRadio = dabInit (theDevice,
2021-04-26 19:23:13 +02:00
&interface,
nullptr, // no spectrum shown
nullptr, // no constellations
nullptr
2018-08-26 17:47:28 +02:00
);
if (theRadio == nullptr) {
2025-06-25 20:04:48 +02:00
fprintf (stderr, "sorry, no radio device available, fatal\n");
2018-08-26 17:47:28 +02:00
exit (4);
}
2020-06-17 19:53:45 +02:00
// theDevice -> setGain (theGain);
2018-08-26 17:47:28 +02:00
if (autogain)
theDevice -> set_autogain (autogain);
print_fileHeader (outFile, jsonOutput);
2018-08-26 17:47:28 +02:00
while (true) {
bool firstTime = true;
2020-06-17 19:53:45 +02:00
bool firstService = true;
2025-07-01 18:38:24 +02:00
int the_timeSyncTime = 5;
2025-06-26 10:15:52 +02:00
int the_freqSyncTime = freqSyncTime;
int the_tiiSyncTime = tiiSyncTime;
2018-08-26 17:47:28 +02:00
theDevice -> stopReader ();
2025-06-25 20:04:48 +02:00
the_tiiHandler. stop ();
2018-08-26 17:47:28 +02:00
int32_t frequency =
dabBand. Frequency (theBand, theChannel);
theDevice -> restartReader (frequency);
2018-08-26 17:47:28 +02:00
ensembleRecognized. store (false);
dabReset (theRadio);
// The device should be working right now
fprintf (stderr, "checking data in channel %s\n",
theChannel. c_str ());
timesyncSet. store (false);
timeSynced. store (false);
2025-06-29 12:43:09 +02:00
while (!timesyncSet. load () && (--the_timeSyncTime >= 0)) {
fprintf (stderr, "Waiting for time sync %d\r", the_timeSyncTime);
sleep (1);
}
2018-08-26 17:47:28 +02:00
if (!timeSynced. load ()) {
theChannel = dabBand. nextChannel (theBand, theChannel);
if (theChannel == startChannel)
break;
else
continue;
}
2018-10-26 09:22:17 +02:00
//
// we might have data here, not sure yet
2018-08-26 17:47:28 +02:00
while (!ensembleRecognized. load () &&
2025-06-26 10:15:52 +02:00
(--the_freqSyncTime >= 0)) {
2025-06-29 12:43:09 +02:00
fprintf (stderr, "waiting for frequency sync %d\r", the_freqSyncTime);
2018-08-26 17:47:28 +02:00
sleep (1);
}
fprintf (stderr, "\n");
if (!ensembleRecognized. load ()) {
theChannel = dabBand. nextChannel (theBand, theChannel);
if (theChannel == startChannel)
break;
else
continue;
}
2025-06-25 20:04:48 +02:00
the_tiiHandler. start (ensembleId);
2020-09-04 19:13:09 +02:00
#ifdef HAVE_RTLSDR
if (rawDump) {
2020-09-06 19:41:21 +02:00
((rtlsdrHandler *)theDevice) -> startDumping (theChannel,
ensembleId);
for (int i = 0; i < dumpDuration; i ++) {
sleep (1);
fprintf (stderr, "%d\r", dumpDuration - i);
}
2020-09-04 19:13:09 +02:00
}
2025-06-27 15:56:03 +02:00
#endif
2025-06-27 09:11:04 +02:00
while (--the_tiiSyncTime > 0) {
2025-06-29 12:43:09 +02:00
fprintf (stderr, "Looking for TII signals %d\r", the_tiiSyncTime);
2025-06-26 10:15:52 +02:00
sleep (1);
2025-06-27 09:11:04 +02:00
}
2018-08-26 17:47:28 +02:00
// print ensemble data here
print_ensembleData (outFile,
2020-06-17 19:53:45 +02:00
jsonOutput,
2018-08-26 17:47:28 +02:00
theRadio,
theChannel,
ensembleName,
ensembleId,
2020-06-17 19:53:45 +02:00
&firstEnsemble);
2018-08-26 17:47:28 +02:00
print_audioheader (outFile, jsonOutput);
2020-06-17 19:53:45 +02:00
for (int i = 0; i < (int)(programNames. size ()); i ++) {
2025-06-25 20:04:48 +02:00
if (is_audioService (theRadio, programNames [i])) {
2018-08-26 17:47:28 +02:00
audiodata ad;
dataforAudioService (theRadio,
2025-06-25 20:04:48 +02:00
programNames [i],
ad, 0);
2018-08-26 17:47:28 +02:00
print_audioService (outFile,
2020-06-16 19:55:12 +02:00
jsonOutput,
2018-08-26 17:47:28 +02:00
theRadio,
2025-06-25 20:04:48 +02:00
programNames [i],
theChannel,
&ad,
&firstService);
2018-08-26 17:47:28 +02:00
}
}
2020-06-17 19:53:45 +02:00
for (int i = 0; i < (int)(programNames. size ()); i ++) {
2025-06-25 20:04:48 +02:00
if (is_dataService (theRadio, programNames [i])) {
2018-08-26 17:47:28 +02:00
if (firstTime)
print_dataHeader (outFile, jsonOutput);
2018-08-26 17:47:28 +02:00
firstTime = false;
2025-06-25 20:04:48 +02:00
packetdata pd;
dataforDataService (theRadio,
programNames [i],
2025-06-26 10:15:52 +02:00
pd, 0);
2025-06-25 20:04:48 +02:00
if (pd. defined)
print_dataService (outFile,
jsonOutput,
theRadio,
programNames [i],
theChannel,
0,
&pd,
&firstService);
2018-08-26 17:47:28 +02:00
}
}
print_ensembleFooter (outFile, jsonOutput);
2020-09-04 19:13:09 +02:00
2025-06-25 20:04:48 +02:00
the_tiiHandler. print ();
2020-09-04 19:13:09 +02:00
#ifdef HAVE_RTLSDR
if (rawDump)
((rtlsdrHandler *)theDevice) -> stopDumping ();
#endif
2018-08-26 17:47:28 +02:00
theDevice -> stopReader ();
2025-06-25 20:04:48 +02:00
the_tiiHandler. stop ();
2018-08-26 17:47:28 +02:00
dabStop (theRadio);
programNames. resize (0);
programSIds. resize (0);
theChannel = dabBand. nextChannel (theBand, theChannel);
}
print_fileFooter (outFile, jsonOutput);
2018-08-26 17:47:28 +02:00
fclose (outFile);
theDevice -> stopReader ();
dabStop (theRadio);
dabExit (theRadio);
delete theDevice;
}
2025-06-25 20:04:48 +02:00
void printOptions () {
fprintf (stderr,
" dab-scanner options are\n\
2020-06-17 19:53:45 +02:00
-F filename in case the output is to a file\n\
-j output data in json format\n\
2025-07-01 18:38:24 +02:00
-t number threshold for tii detection\n\
-D number amount of time to look for full sync\n\
-M Mode Mode is 1, 2 or 4. Default is Mode 1\n\
-B Band Band is either L_BAND or BAND_III (default)\n\
2025-07-01 18:38:24 +02:00
-I number amount of time used to gather TII data\n\
2020-09-04 19:13:09 +02:00
-C start channel the start channel, default: 5A\n\
-R filename raw dump of the input data\n"
2020-06-17 19:53:45 +02:00
" for hackrf:\n"
" -v vgaGain\n"
" -l lnaGain\n"
" -p number\tppm offset\n"
" for SDRplay:\n"
" -G Gain reduction in dB (range 20 .. 59)\n"
" -L lnaState (depends on model chosen)\n"
" -Q autogain (default off)\n"
" -p number\t ppm offset\n"
" for rtlsdr:\n"
" -G number\t gain, range 0 .. 100\n"
" -Q autogain (default off)\n"
" -p number\tppm offset\n"
" for airspy:\n"
" -G number\t gain, range 1 .. 21\n"
" -b set rf bias\n"
" -p number\t ppm Correction\n"
" for limesdr:\n"
" -G number\t gain\n"
" -X antenna selection\n"
" -C channel\n");
2018-08-26 17:47:28 +02:00
}