-
Notifications
You must be signed in to change notification settings - Fork 25
/
sdr_rtlsdr.c
355 lines (303 loc) · 11.1 KB
/
sdr_rtlsdr.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
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
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// sdr_rtlsdr.c: rtlsdr dongle support
//
// Copyright (c) 2020 Michael Wolf <[email protected]>
//
// This code is based on a detached fork of dump1090-fa.
//
// Copyright (c) 2014-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/>.
//
// This file incorporates work covered by the following copyright and
// license:
//
// Copyright (C) 2012 by Salvatore Sanfilippo <[email protected]>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "readsb.h"
#include "sdr_rtlsdr.h"
#include <rtl-sdr.h>
#if defined(__arm__) || defined(__aarch64__)
// Assume we need to use a bounce buffer to avoid performance problems on Pis running kernel 5.x and using zerocopy
#define USE_BOUNCE_BUFFER
#endif
static struct {
iq_convert_fn converter;
struct converter_state *converter_state;
rtlsdr_dev_t *dev;
int ppm_error;
bool digital_agc;
uint8_t *bounce_buffer;
} RTLSDR;
//
// =============================== RTLSDR handling ==========================
//
void rtlsdrInitConfig() {
RTLSDR.dev = NULL;
RTLSDR.digital_agc = false;
RTLSDR.ppm_error = 0;
RTLSDR.converter = NULL;
RTLSDR.converter_state = NULL;
RTLSDR.bounce_buffer = NULL;
}
static void show_rtlsdr_devices() {
int device_count = rtlsdr_get_device_count();
fprintf(stderr, "rtlsdr: found %d device(s):\n", device_count);
for (int i = 0; i < device_count; i++) {
char vendor[256], product[256], serial[256];
if (rtlsdr_get_device_usb_strings(i, vendor, product, serial) != 0) {
fprintf(stderr, " %d: unable to read device details\n", i);
} else {
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
}
}
}
static int find_device_index(char *s) {
int device_count = rtlsdr_get_device_count();
if (!device_count) {
return -1;
}
/* does string look like raw id number */
if (!strcmp(s, "0")) {
return 0;
} else if (s[0] != '0') {
char *s2;
int device = (int) strtol(s, &s2, 10);
if (s2[0] == '\0' && device >= 0 && device < device_count) {
return device;
}
}
/* does string exact match a serial */
for (int i = 0; i < device_count; i++) {
char serial[256];
if (rtlsdr_get_device_usb_strings(i, NULL, NULL, serial) == 0 && !strcmp(s, serial)) {
return i;
}
}
/* does string prefix match a serial */
for (int i = 0; i < device_count; i++) {
char serial[256];
if (rtlsdr_get_device_usb_strings(i, NULL, NULL, serial) == 0 && !strncmp(s, serial, strlen(s))) {
return i;
}
}
/* does string suffix match a serial */
for (int i = 0; i < device_count; i++) {
char serial[256];
if (rtlsdr_get_device_usb_strings(i, NULL, NULL, serial) == 0 && strlen(s) < strlen(serial) && !strcmp(serial + strlen(serial) - strlen(s), s)) {
return i;
}
}
return -1;
}
bool rtlsdrHandleOption(int argc, char *argv) {
switch (argc) {
case OptRtlSdrEnableAgc:
RTLSDR.digital_agc = true;
break;
case OptRtlSdrPpm:
RTLSDR.ppm_error = atoi(argv);
break;
}
return true;
}
bool rtlsdrOpen(void) {
if (!rtlsdr_get_device_count()) {
fprintf(stderr, "rtlsdr: no supported devices found.\n");
return false;
}
int dev_index = 0;
if (Modes.dev_name) {
if ((dev_index = find_device_index(Modes.dev_name)) < 0) {
fprintf(stderr, "rtlsdr: no device matching '%s' found.\n", Modes.dev_name);
show_rtlsdr_devices();
return false;
}
}
char manufacturer[256];
char product[256];
char serial[256];
if (rtlsdr_get_device_usb_strings(dev_index, manufacturer, product, serial) < 0) {
fprintf(stderr, "rtlsdr: error querying device #%d: %s\n", dev_index, strerror(errno));
return false;
}
fprintf(stderr, "rtlsdr: using device #%d: %s (%s, %s, SN %s)\n",
dev_index, rtlsdr_get_device_name(dev_index),
manufacturer, product, serial);
if (rtlsdr_open(&RTLSDR.dev, dev_index) < 0) {
fprintf(stderr, "rtlsdr: error opening the RTLSDR device: %s\n",
strerror(errno));
return false;
}
// Set gain, frequency, sample rate, and reset the device
if (Modes.gain == MODES_AUTO_GAIN) {
fprintf(stderr, "rtlsdr: enabling tuner AGC\n");
rtlsdr_set_tuner_gain_mode(RTLSDR.dev, 0);
} else {
int *gains;
int numgains;
numgains = rtlsdr_get_tuner_gains(RTLSDR.dev, NULL);
if (numgains <= 0) {
fprintf(stderr, "rtlsdr: error getting tuner gains\n");
return false;
}
gains = malloc(numgains * sizeof (int));
if (rtlsdr_get_tuner_gains(RTLSDR.dev, gains) != numgains) {
fprintf(stderr, "rtlsdr: error getting tuner gains\n");
free(gains);
return false;
}
int target = (Modes.gain == MODES_MAX_GAIN ? 9999 : Modes.gain);
int closest = -1;
for (int i = 0; i < numgains; ++i) {
if (closest == -1 || abs(gains[i] - target) < abs(gains[closest] - target))
closest = i;
}
rtlsdr_set_tuner_gain(RTLSDR.dev, gains[closest]);
free(gains);
fprintf(stderr, "rtlsdr: tuner gain set to %.1f dB\n",
rtlsdr_get_tuner_gain(RTLSDR.dev) / 10.0);
}
if (RTLSDR.digital_agc) {
fprintf(stderr, "rtlsdr: enabling digital AGC\n");
rtlsdr_set_agc_mode(RTLSDR.dev, 1);
}
rtlsdr_set_freq_correction(RTLSDR.dev, RTLSDR.ppm_error);
rtlsdr_set_center_freq(RTLSDR.dev, Modes.freq);
rtlsdr_set_sample_rate(RTLSDR.dev, (unsigned) Modes.sample_rate);
#ifdef ENABLE_RTLSDR_BIASTEE
// Enable or disable bias tee on GPIO pin 0. (Works only for rtl-sdr.com v3 dongles)
rtlsdr_set_bias_tee(RTLSDR.dev, Modes.biastee);
#endif
rtlsdr_reset_buffer(RTLSDR.dev);
RTLSDR.converter = init_converter(INPUT_UC8,
Modes.sample_rate,
Modes.dc_filter,
&RTLSDR.converter_state);
if (!RTLSDR.converter) {
fprintf(stderr, "rtlsdr: can't initialize sample converter\n");
rtlsdrClose();
return false;
}
#ifdef USE_BOUNCE_BUFFER
if (!(RTLSDR.bounce_buffer = malloc(MODES_RTL_BUF_SIZE))) {
fprintf(stderr, "rtlsdr: can't allocate bounce buffer\n");
rtlsdrClose();
return false;
}
#endif
return true;
}
static void rtlsdrCallback(unsigned char *buf, uint32_t len, void *ctx) {
static unsigned dropped = 0;
static uint64_t sampleCounter = 0;
MODES_NOTUSED(ctx);
sdrMonitor();
if (Modes.exit) {
rtlsdr_cancel_async(RTLSDR.dev); // ask our caller to exit
return;
}
unsigned samples_read = len / 2; // Drops any trailing odd sample, not much else we can do there
if (!samples_read) {
return; // that wasn't useful
}
struct mag_buf *outbuf = fifo_acquire(0 /* don't wait */);
if (!outbuf) {
// FIFO is full. Drop this block.
dropped += samples_read;
sampleCounter += samples_read;
return;
}
outbuf->flags = 0;
outbuf->dropped = 0;
if (dropped) {
// We previously dropped some samples due to no buffers being available
outbuf->flags |= MAGBUF_DISCONTINUOUS;
outbuf->dropped = dropped;
// reset dropped counter
dropped = 0;
}
// Compute the sample timestamp and system timestamp for the start of the block
outbuf->sampleTimestamp = sampleCounter * 12e6 / Modes.sample_rate;
sampleCounter += samples_read;
// Get the approx system time for the start of this block
uint64_t block_duration = 1e3 * samples_read / Modes.sample_rate;
outbuf->sysTimestamp = mstime() - block_duration;
// Convert the new data
unsigned to_convert = samples_read;
if (to_convert + outbuf->overlap > outbuf->totalLength) {
// how did that happen?
to_convert = outbuf->totalLength - outbuf->overlap;
dropped = samples_read - to_convert;
}
#ifdef USE_BOUNCE_BUFFER
// Work around zero-copy slowness on Pis with 5.x kernels
memcpy(RTLSDR.bounce_buffer, buf, to_convert * 2);
buf = RTLSDR.bounce_buffer;
#endif
RTLSDR.converter(buf, &outbuf->data[outbuf->overlap], to_convert, RTLSDR.converter_state, &outbuf->mean_level, &outbuf->mean_power);
outbuf->validLength = outbuf->overlap + to_convert;
// Push to the demodulation thread
fifo_enqueue(outbuf);
}
void rtlsdrRun() {
if (!RTLSDR.dev) {
return;
}
rtlsdr_read_async(RTLSDR.dev, rtlsdrCallback, NULL, MODES_RTL_BUFFERS, MODES_RTL_BUF_SIZE);
if (!Modes.exit) {
fprintf(stderr, "rtlsdr_read_async returned unexpectedly, probably lost the USB device, bailing out");
}
}
void rtlsdrClose() {
if (RTLSDR.dev) {
rtlsdr_close(RTLSDR.dev);
RTLSDR.dev = NULL;
}
if (RTLSDR.converter) {
cleanup_converter(RTLSDR.converter_state);
RTLSDR.converter = NULL;
RTLSDR.converter_state = NULL;
}
if (RTLSDR.bounce_buffer) {
free(RTLSDR.bounce_buffer);
RTLSDR.bounce_buffer = NULL;
}
}