2017-01-30 20:15:26 +00:00
|
|
|
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
|
|
|
|
|
//
|
|
|
|
|
// sdr.c: generic SDR infrastructure
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2016-2017 Oliver Jowett <oliver@mutability.co.uk>
|
|
|
|
|
// Copyright (c) 2017 FlightAware LLC
|
|
|
|
|
//
|
2017-06-15 17:16:51 +00:00
|
|
|
// This file is free software: you may copy, redistribute and/or modify it
|
2017-01-30 20:15:26 +00:00
|
|
|
// under the terms of the GNU General Public License as published by the
|
2017-06-15 17:16:51 +00:00
|
|
|
// Free Software Foundation, either version 2 of the License, or (at your
|
|
|
|
|
// option) any later version.
|
2017-01-30 20:15:26 +00:00
|
|
|
//
|
2017-06-15 17:16:51 +00:00
|
|
|
// This file 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
|
2017-01-30 20:15:26 +00:00
|
|
|
// General Public License for more details.
|
|
|
|
|
//
|
2017-06-15 17:16:51 +00:00
|
|
|
// You should have received a copy of the GNU General Public License
|
2017-01-30 20:15:26 +00:00
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2017-01-27 17:30:40 +00:00
|
|
|
#include "dump1090.h"
|
|
|
|
|
|
|
|
|
|
#include "sdr_ifile.h"
|
|
|
|
|
#ifdef ENABLE_RTLSDR
|
|
|
|
|
# include "sdr_rtlsdr.h"
|
|
|
|
|
#endif
|
2017-01-27 17:44:42 +00:00
|
|
|
#ifdef ENABLE_BLADERF
|
|
|
|
|
# include "sdr_bladerf.h"
|
|
|
|
|
#endif
|
2019-12-09 21:55:40 +00:00
|
|
|
#ifdef ENABLE_HACKRF
|
|
|
|
|
# include "sdr_hackrf.h"
|
|
|
|
|
#endif
|
2020-07-26 13:52:48 +00:00
|
|
|
#ifdef ENABLE_LIMESDR
|
|
|
|
|
# include "sdr_limesdr.h"
|
|
|
|
|
#endif
|
2017-01-27 17:30:40 +00:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const char *name;
|
|
|
|
|
sdr_type_t sdr_type;
|
|
|
|
|
void (*initConfig)();
|
|
|
|
|
void (*showHelp)();
|
|
|
|
|
bool (*handleOption)(int, char**, int*);
|
|
|
|
|
bool (*open)();
|
|
|
|
|
void (*run)();
|
Don't hang on exit if rtlsdr hardware stops sending samples.
Give up and exit after 30 seconds of no sample data, rather than just warning and continuing.
background & discussion: https://discussions.flightaware.com/t/cpu-hikes-crash-dump1090-fa/74759
The scenario this addresses is:
* Hardware wedges, USB bulk endpoint stops providing data
* librtlsdr remains in rtlsdr_read_async() waiting for either USB data which never arrives,
or a cancellation via _a different thread_ calling rtlsdr_cancel_async().
* main thread notices the lack of SDR data and complains
* something external e.g. piaware tries a restart and sends SIGTERM
* the signal handler sets Modes.exit = 1; the main thread starts waiting for receive thread termination
* because we're never getting callbacks from rtlsdr_read_async(), we never call rtlsdr_cancel_async
* dump1090 hangs waiting on receive thread termination
To fix this, add a sdrStop() handler function where the general contract is "make the receive thread terminate".
In the rtlsdr case, this calls rtlsdr_cancel_async directly, which will make rtlsdr_read_async() return even
if the hardware is stuck.
The main thread then calls sdrStop() before waiting for receive thread termination.
Also, as discussed in the thread above, there's not really much point in continuing to run if the SDR
has wedged, so bail out after 30 seconds of no sample data.
Also, if pthread_timedjoin_np is available, use it in preference to pthread_join so that we do not wait
indefinitely for the receive thread on shutdown. If the join times out, give up and abort() as we can't
safely continue a clean shutdown while the receive thread is running.
2021-03-20 17:51:08 +00:00
|
|
|
void (*stop)();
|
2017-01-27 17:30:40 +00:00
|
|
|
void (*close)();
|
2021-06-29 12:11:13 +00:00
|
|
|
int (*getgain)();
|
|
|
|
|
int (*getmaxgain)();
|
|
|
|
|
double (*getgaindb)(int);
|
|
|
|
|
int (*setgain)(int);
|
2017-01-27 17:30:40 +00:00
|
|
|
} sdr_handler;
|
|
|
|
|
|
|
|
|
|
static void noInitConfig()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void noShowHelp()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool noHandleOption(int argc, char **argv, int *jptr)
|
|
|
|
|
{
|
|
|
|
|
MODES_NOTUSED(argc);
|
|
|
|
|
MODES_NOTUSED(argv);
|
|
|
|
|
MODES_NOTUSED(jptr);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool noOpen()
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Net-only mode, no SDR device or file open.\n");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void noRun()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
Don't hang on exit if rtlsdr hardware stops sending samples.
Give up and exit after 30 seconds of no sample data, rather than just warning and continuing.
background & discussion: https://discussions.flightaware.com/t/cpu-hikes-crash-dump1090-fa/74759
The scenario this addresses is:
* Hardware wedges, USB bulk endpoint stops providing data
* librtlsdr remains in rtlsdr_read_async() waiting for either USB data which never arrives,
or a cancellation via _a different thread_ calling rtlsdr_cancel_async().
* main thread notices the lack of SDR data and complains
* something external e.g. piaware tries a restart and sends SIGTERM
* the signal handler sets Modes.exit = 1; the main thread starts waiting for receive thread termination
* because we're never getting callbacks from rtlsdr_read_async(), we never call rtlsdr_cancel_async
* dump1090 hangs waiting on receive thread termination
To fix this, add a sdrStop() handler function where the general contract is "make the receive thread terminate".
In the rtlsdr case, this calls rtlsdr_cancel_async directly, which will make rtlsdr_read_async() return even
if the hardware is stuck.
The main thread then calls sdrStop() before waiting for receive thread termination.
Also, as discussed in the thread above, there's not really much point in continuing to run if the SDR
has wedged, so bail out after 30 seconds of no sample data.
Also, if pthread_timedjoin_np is available, use it in preference to pthread_join so that we do not wait
indefinitely for the receive thread on shutdown. If the join times out, give up and abort() as we can't
safely continue a clean shutdown while the receive thread is running.
2021-03-20 17:51:08 +00:00
|
|
|
static void noStop()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 17:30:40 +00:00
|
|
|
static void noClose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 12:11:13 +00:00
|
|
|
static int noGetGain()
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int noGetMaxGain()
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double noGetGainDb(int step)
|
|
|
|
|
{
|
|
|
|
|
MODES_NOTUSED(step);
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int noSetGain(int step)
|
|
|
|
|
{
|
|
|
|
|
MODES_NOTUSED(step);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 17:30:40 +00:00
|
|
|
static bool unsupportedOpen()
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Support for this SDR type was not enabled in this build.\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sdr_handler sdr_handlers[] = {
|
|
|
|
|
#ifdef ENABLE_RTLSDR
|
2021-06-29 12:11:13 +00:00
|
|
|
{ "rtlsdr", SDR_RTLSDR, rtlsdrInitConfig, rtlsdrShowHelp, rtlsdrHandleOption, rtlsdrOpen, rtlsdrRun, rtlsdrStop, rtlsdrClose, rtlsdrGetGain, rtlsdrGetMaxGain, rtlsdrGetGainDb, rtlsdrSetGain },
|
2017-01-27 17:30:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
2017-01-27 17:44:42 +00:00
|
|
|
#ifdef ENABLE_BLADERF
|
2021-06-29 12:11:13 +00:00
|
|
|
{ "bladerf", SDR_BLADERF, bladeRFInitConfig, bladeRFShowHelp, bladeRFHandleOption, bladeRFOpen, bladeRFRun, noStop, bladeRFClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain },
|
2017-01-27 17:44:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
2019-12-09 21:55:40 +00:00
|
|
|
#ifdef ENABLE_HACKRF
|
2021-06-29 12:11:13 +00:00
|
|
|
{ "hackrf", SDR_HACKRF, hackRFInitConfig, hackRFShowHelp, hackRFHandleOption, hackRFOpen, hackRFRun, noStop, hackRFClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain },
|
2019-12-09 21:55:40 +00:00
|
|
|
#endif
|
2020-07-26 13:52:48 +00:00
|
|
|
#ifdef ENABLE_LIMESDR
|
2021-06-29 12:11:13 +00:00
|
|
|
{ "limesdr", SDR_LIMESDR, limesdrInitConfig, limesdrShowHelp, limesdrHandleOption, limesdrOpen, limesdrRun, noStop, limesdrClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain },
|
2020-07-26 13:52:48 +00:00
|
|
|
#endif
|
2019-12-09 21:55:40 +00:00
|
|
|
|
2021-06-29 12:11:13 +00:00
|
|
|
{ "none", SDR_NONE, noInitConfig, noShowHelp, noHandleOption, noOpen, noRun, noStop, noClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain },
|
|
|
|
|
{ "ifile", SDR_IFILE, ifileInitConfig, ifileShowHelp, ifileHandleOption, ifileOpen, ifileRun, noStop, ifileClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain },
|
2017-01-27 17:30:40 +00:00
|
|
|
|
2021-06-29 12:11:13 +00:00
|
|
|
{ NULL, SDR_NONE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } /* must come last */
|
2017-01-27 17:30:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void sdrInitConfig()
|
|
|
|
|
{
|
|
|
|
|
// Default SDR is the first type available in the handlers array.
|
|
|
|
|
Modes.sdr_type = sdr_handlers[0].sdr_type;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
sdr_handlers[i].initConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sdrShowHelp()
|
|
|
|
|
{
|
|
|
|
|
printf("--device-type <type> Select SDR type (default: %s)\n", sdr_handlers[0].name);
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
sdr_handlers[i].showHelp();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool sdrHandleOption(int argc, char **argv, int *jptr)
|
|
|
|
|
{
|
|
|
|
|
int j = *jptr;
|
2020-08-05 12:12:06 +00:00
|
|
|
if (!strcmp(argv[j], "--device-type")) {
|
|
|
|
|
if ((j+1) < argc) {
|
|
|
|
|
++j;
|
|
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
if (!strcasecmp(sdr_handlers[i].name, argv[j])) {
|
|
|
|
|
Modes.sdr_type = sdr_handlers[i].sdr_type;
|
|
|
|
|
*jptr = j;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-01-27 17:30:40 +00:00
|
|
|
}
|
2020-08-05 12:12:06 +00:00
|
|
|
fprintf(stderr, "SDR type '%s' not recognized. ", argv[j]);
|
2017-01-27 17:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 12:12:06 +00:00
|
|
|
fprintf(stderr, "Supported SDR types:\n");
|
2017-01-27 17:30:40 +00:00
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
fprintf(stderr, " %s\n", sdr_handlers[i].name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
if (sdr_handlers[i].handleOption(argc, argv, jptr))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sdr_handler *current_handler()
|
|
|
|
|
{
|
2021-06-29 12:11:13 +00:00
|
|
|
static sdr_handler unsupported_handler = { "unsupported", SDR_NONE, noInitConfig, noShowHelp, noHandleOption, unsupportedOpen, noRun, noStop, noClose, noGetGain, noGetMaxGain, noGetGainDb, noSetGain };
|
2017-01-27 17:30:40 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; sdr_handlers[i].name; ++i) {
|
|
|
|
|
if (Modes.sdr_type == sdr_handlers[i].sdr_type) {
|
|
|
|
|
return &sdr_handlers[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &unsupported_handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool sdrOpen()
|
|
|
|
|
{
|
2020-08-05 04:00:50 +00:00
|
|
|
pthread_mutex_init(&Modes.reader_cpu_mutex, NULL);
|
2017-01-27 17:30:40 +00:00
|
|
|
return current_handler()->open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sdrRun()
|
|
|
|
|
{
|
2020-08-05 03:51:32 +00:00
|
|
|
set_thread_name("dump1090-sdr");
|
2020-08-05 04:00:50 +00:00
|
|
|
|
|
|
|
|
pthread_mutex_lock(&Modes.reader_cpu_mutex);
|
|
|
|
|
Modes.reader_cpu_accumulator.tv_sec = 0;
|
|
|
|
|
Modes.reader_cpu_accumulator.tv_nsec = 0;
|
|
|
|
|
start_cpu_timing(&Modes.reader_cpu_start);
|
|
|
|
|
pthread_mutex_unlock(&Modes.reader_cpu_mutex);
|
|
|
|
|
|
2020-08-05 03:51:32 +00:00
|
|
|
current_handler()->run();
|
2020-08-05 04:00:50 +00:00
|
|
|
|
|
|
|
|
pthread_mutex_lock(&Modes.reader_cpu_mutex);
|
|
|
|
|
end_cpu_timing(&Modes.reader_cpu_start, &Modes.reader_cpu_accumulator);
|
|
|
|
|
pthread_mutex_unlock(&Modes.reader_cpu_mutex);
|
2017-01-27 17:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
Don't hang on exit if rtlsdr hardware stops sending samples.
Give up and exit after 30 seconds of no sample data, rather than just warning and continuing.
background & discussion: https://discussions.flightaware.com/t/cpu-hikes-crash-dump1090-fa/74759
The scenario this addresses is:
* Hardware wedges, USB bulk endpoint stops providing data
* librtlsdr remains in rtlsdr_read_async() waiting for either USB data which never arrives,
or a cancellation via _a different thread_ calling rtlsdr_cancel_async().
* main thread notices the lack of SDR data and complains
* something external e.g. piaware tries a restart and sends SIGTERM
* the signal handler sets Modes.exit = 1; the main thread starts waiting for receive thread termination
* because we're never getting callbacks from rtlsdr_read_async(), we never call rtlsdr_cancel_async
* dump1090 hangs waiting on receive thread termination
To fix this, add a sdrStop() handler function where the general contract is "make the receive thread terminate".
In the rtlsdr case, this calls rtlsdr_cancel_async directly, which will make rtlsdr_read_async() return even
if the hardware is stuck.
The main thread then calls sdrStop() before waiting for receive thread termination.
Also, as discussed in the thread above, there's not really much point in continuing to run if the SDR
has wedged, so bail out after 30 seconds of no sample data.
Also, if pthread_timedjoin_np is available, use it in preference to pthread_join so that we do not wait
indefinitely for the receive thread on shutdown. If the join times out, give up and abort() as we can't
safely continue a clean shutdown while the receive thread is running.
2021-03-20 17:51:08 +00:00
|
|
|
void sdrStop()
|
|
|
|
|
{
|
|
|
|
|
current_handler()->stop();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 17:30:40 +00:00
|
|
|
void sdrClose()
|
|
|
|
|
{
|
2020-08-05 04:00:50 +00:00
|
|
|
pthread_mutex_destroy(&Modes.reader_cpu_mutex);
|
2017-01-27 17:30:40 +00:00
|
|
|
current_handler()->close();
|
|
|
|
|
}
|
2020-08-05 04:00:50 +00:00
|
|
|
|
|
|
|
|
void sdrMonitor()
|
|
|
|
|
{
|
|
|
|
|
pthread_mutex_lock(&Modes.reader_cpu_mutex);
|
|
|
|
|
update_cpu_timing(&Modes.reader_cpu_start, &Modes.reader_cpu_accumulator);
|
|
|
|
|
pthread_mutex_unlock(&Modes.reader_cpu_mutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sdrUpdateCPUTime(struct timespec *addTo)
|
|
|
|
|
{
|
|
|
|
|
pthread_mutex_lock(&Modes.reader_cpu_mutex);
|
|
|
|
|
add_timespecs(&Modes.reader_cpu_accumulator, addTo, addTo);
|
|
|
|
|
Modes.reader_cpu_accumulator.tv_sec = 0;
|
|
|
|
|
Modes.reader_cpu_accumulator.tv_nsec = 0;
|
|
|
|
|
pthread_mutex_unlock(&Modes.reader_cpu_mutex);
|
|
|
|
|
}
|
2021-06-29 12:11:13 +00:00
|
|
|
|
|
|
|
|
int sdrGetGain()
|
|
|
|
|
{
|
|
|
|
|
return current_handler()->getgain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sdrGetMaxGain()
|
|
|
|
|
{
|
|
|
|
|
return current_handler()->getmaxgain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double sdrGetGainDb(int step)
|
|
|
|
|
{
|
|
|
|
|
return current_handler()->getgaindb(step);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sdrSetGain(int step)
|
|
|
|
|
{
|
|
|
|
|
return current_handler()->setgain(step);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|