forked from Mictronics/readsb-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdr.c
185 lines (153 loc) · 5.48 KB
/
sdr.c
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
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// sdr.c: generic SDR infrastructure
//
// Copyright (c) 2020 Michael Wolf <[email protected]>
//
// This code is based on a detached fork of dump1090-fa.
//
// Copyright (c) 2016-2017 Oliver Jowett <[email protected]>
// Copyright (c) 2017 FlightAware LLC
//
// This file 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
// any later version.
//
// 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "readsb.h"
#include "sdr_ifile.h"
#ifdef ENABLE_RTLSDR
#include "sdr_rtlsdr.h"
#endif
#ifdef ENABLE_BLADERF
#include "sdr_bladerf.h"
#include "sdr_ubladerf.h"
#endif
#ifdef ENABLE_PLUTOSDR
#include "sdr_plutosdr.h"
#endif
#include "sdr_beast.h"
typedef struct {
void (*initConfig)();
bool(*handleOption)(int, char*);
bool(*open)();
void (*run)();
void (*close)();
const char *name;
sdr_type_t sdr_type;
uint32_t padding;
} sdr_handler;
static void noInitConfig() {
}
static bool noHandleOption(int argc, char *argv) {
MODES_NOTUSED(argc);
MODES_NOTUSED(argv);
return false;
}
static bool noOpen() {
fprintf(stderr, "Net-only mode, no SDR device or file open.\n");
return true;
}
static void noRun() {
}
static void noClose() {
}
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
{ rtlsdrInitConfig, rtlsdrHandleOption, rtlsdrOpen, rtlsdrRun, rtlsdrClose, "rtlsdr", SDR_RTLSDR, 0},
#endif
#ifdef ENABLE_BLADERF
{ bladeRFInitConfig, bladeRFHandleOption, bladeRFOpen, bladeRFRun, bladeRFClose, "bladerf", SDR_BLADERF, 0},
{ ubladeRFInitConfig, ubladeRFHandleOption, ubladeRFOpen, ubladeRFRun, ubladeRFClose, "ubladerf", SDR_MICROBLADERF, 0},
#endif
#ifdef ENABLE_PLUTOSDR
{ plutosdrInitConfig, plutosdrHandleOption, plutosdrOpen, plutosdrRun, plutosdrClose, "plutosdr", SDR_PLUTOSDR, 0},
#endif
{ beastInitConfig, beastHandleOption, beastOpen, noRun, noClose, "modesbeast", SDR_MODESBEAST, 0},
{ beastInitConfig, beastHandleOption, beastOpen, noRun, noClose, "gnshulc", SDR_GNS, 0},
{ ifileInitConfig, ifileHandleOption, ifileOpen, ifileRun, ifileClose, "ifile", SDR_IFILE, 0},
{ noInitConfig, noHandleOption, noOpen, noRun, noClose, "none", SDR_NONE, 0},
{ NULL, NULL, NULL, NULL, NULL, NULL, SDR_NONE, 0} /* must come last */
};
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();
}
}
bool sdrHandleOption(int argc, char *argv) {
switch (argc) {
case OptDeviceType:
for (int i = 0; sdr_handlers[i].name; ++i) {
if (!strcasecmp(sdr_handlers[i].name, argv)) {
Modes.sdr_type = sdr_handlers[i].sdr_type;
return true;
}
}
break;
default:
for (int i = 0; sdr_handlers[i].sdr_type; ++i) {
if (Modes.sdr_type == sdr_handlers[i].sdr_type) {
return sdr_handlers[i].handleOption(argc, argv);
}
}
}
fprintf(stderr, "SDR type '%s' not recognized; supported SDR types are:\n", argv);
for (int i = 0; sdr_handlers[i].name; ++i) {
fprintf(stderr, " %s\n", sdr_handlers[i].name);
}
return false;
}
static sdr_handler *current_handler() {
static sdr_handler unsupported_handler = {noInitConfig, noHandleOption, unsupportedOpen, noRun, noClose, "unsupported", SDR_NONE, 0};
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() {
pthread_mutex_init(&Modes.reader_cpu_mutex, NULL);
return current_handler()->open();
}
void sdrRun() {
set_thread_name("readsb-sdr");
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);
current_handler()->run();
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);
}
void sdrClose() {
pthread_mutex_destroy(&Modes.reader_cpu_mutex);
current_handler()->close();
}
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);
}