forked from flightaware/dump1090
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdr_miri.c
319 lines (256 loc) · 8.14 KB
/
sdr_miri.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
// Part of dump1090, a Mode S message decoder for RTLSDR devices.
//
// sdr_miri.c: libmirisdr-4 support
//
// Copyright (c) 2014-2017 Oliver Jowett <oliver@mutability.co.uk>
// Copyright (c) 2017 FlightAware LLC
//
// This file is free software: you may copy, redistribute 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.
//
// 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 "dump1090.h"
#include "sdr_miri.h"
#include <unistd.h>
#include "mirisdr.h"
#define DEFAULT_ASYNC_BUF_NUMBER 32
#define DEFAULT_BUF_LENGTH (16 * 16384)
static struct {
mirisdr_dev_t *dev;
iq_convert_fn converter;
struct converter_state *converter_state;
mirisdr_hw_flavour_t hw_flavour;
bool verbose;
int gain;
int max_gain;
char *transfer_type;
uint32_t bandwidth;
} MIRI;
//
// =============================== miriSDR handling ==========================
//
void miriInitConfig()
{
MIRI.dev = NULL;
MIRI.converter = NULL;
MIRI.converter_state = NULL;
MIRI.gain = 0;
MIRI.max_gain = 0;
MIRI.bandwidth = 5e6;
MIRI.transfer_type = "ISOC";
MIRI.hw_flavour = MIRISDR_HW_DEFAULT;
}
void miriShowHelp()
{
printf(" Mirics SDR-specific options (use with --device-type miri)\n");
printf("\n");
printf("--bandwidth <hz> (default: 5000000)\n");
printf("--bulk-transfer (default: isochronous)\n");
printf("--hardware-flavour <string> (hardware specific: \"default\", \"sdrplay\")\n");
printf("\n");
}
bool miriHandleOption(int argc, char **argv, int *jptr)
{
int j = *jptr;
bool more = (j+1 < argc);
if (!strcmp(argv[j], "--bandwidth") && more) {
MIRI.bandwidth = atoi(argv[++j]);
} else if (!strcmp(argv[j], "--bulk-transfer")) {
MIRI.transfer_type = "BULK";
} else if (!strcmp(argv[j], "--hardware-flavour") && more) {
if (!strcasecmp(argv[++j], "sdrplay")) {
MIRI.hw_flavour = MIRISDR_HW_SDRPLAY;
}
} else {
return false;
}
*jptr = j;
return true;
}
bool miriOpen(void)
{
uint32_t dev_index = 0;
uint32_t freq = Modes.freq;
uint32_t sample_rate = Modes.sample_rate;
int r, i = 0;
int gain = Modes.gain;
char vendor[256] = { 0 }, product[256] = { 0 }, serial[256] = { 0 };
int device_count = mirisdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
return false;
}
printf("Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++) {
mirisdr_get_device_usb_strings(i, vendor, product, serial);
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
}
printf("\n");
r = mirisdr_open(&MIRI.dev, dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open mirisdr device #%d.\n", dev_index);
return false;
}
MIRI.max_gain = mirisdr_get_tuner_gains(MIRI.dev, NULL) -1;
if (MIRI.max_gain > 0) {
printf("Supported gain values: 0 - %d \n", MIRI.max_gain);
}
/* Set the hardware flavour */
mirisdr_set_hw_flavour(MIRI.dev, MIRI.hw_flavour);
/* Set the sample rate */
r = mirisdr_set_sample_rate(MIRI.dev, sample_rate);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set sample rate to %u.\n", sample_rate);
/* Set the frequency */
r = mirisdr_set_center_freq(MIRI.dev, freq);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set frequency to %u Hz.\n", freq);
/* Set sample format */
mirisdr_set_sample_format(MIRI.dev, "252_S16");
/* Set USB transfer type */
mirisdr_set_transfer(MIRI.dev, MIRI.transfer_type );
/* Set IF mode to ZERO*/
mirisdr_set_if_freq(MIRI.dev, 0);
/* Set bandwidth */
r = mirisdr_set_bandwidth(MIRI.dev, MIRI.bandwidth);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set bandwidth to %u Hz.\n", MIRI.bandwidth);
/* Set gain */
miriSetGain(gain);
/* Reset buffer before we start reading from it */
r = mirisdr_reset_buffer(MIRI.dev);
if (r < 0)
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
MIRI.converter = init_converter(INPUT_SC16,
Modes.sample_rate,
Modes.dc_filter,
&MIRI.converter_state);
if (!MIRI.converter) {
fprintf(stderr, "MIRI: can't initialize sample converter\n");
goto error;
}
return true;
error:
if (MIRI.dev != NULL) {
mirisdr_close(MIRI.dev);
MIRI.dev = NULL;
}
return false;
}
static void miriCallback(unsigned char *buf, uint32_t len, void *ctx)
{
static unsigned dropped = 0;
static uint64_t sampleCounter = 0;
MODES_NOTUSED(ctx);
sdrMonitor();
if (Modes.exit) {
mirisdr_cancel_async(MIRI.dev);
return;
}
unsigned samples_read = len/4;
if (len < DEFAULT_BUF_LENGTH) {
fprintf(stderr, "miri: len < DEFAULT_BUF_LENGTH, samples_read: %d len: %d buffer \n", samples_read, len);
return;
}
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;
if (dropped) {
// We previously dropped some samples due to no buffers being available
outbuf->flags |= MAGBUF_DISCONTINUOUS;
outbuf->dropped = dropped;
}
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;
}
MIRI.converter(buf, &outbuf->data[outbuf->overlap], to_convert, MIRI.converter_state, &outbuf->mean_level, &outbuf->mean_power);
outbuf->validLength = outbuf->overlap + to_convert;
// Push to the demodulation thread
fifo_enqueue(outbuf);
}
void miriRun()
{
if (!MIRI.dev) {
return;
}
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
fprintf(stderr, "miri: out_block_size: %d buffer size %ld \n", out_block_size, (long)(out_block_size * sizeof(uint8_t)));
int r = mirisdr_read_async(MIRI.dev, miriCallback, NULL,
DEFAULT_ASYNC_BUF_NUMBER,
out_block_size);
if (!Modes.exit)
fprintf(stderr, "miri: mirisdr_read_async returned unexpectedly. (error %d) \n", r);
}
void miriStop() {
if (MIRI.dev) {
mirisdr_cancel_async(MIRI.dev);
}
}
void miriClose()
{
if (MIRI.dev) {
mirisdr_close(MIRI.dev);
MIRI.dev = NULL;
}
if (MIRI.converter) {
cleanup_converter(MIRI.converter_state);
MIRI.converter = NULL;
MIRI.converter_state = NULL;
}
}
int miriGetGain()
{
return MIRI.gain;
}
int miriGetMaxGain()
{
return MIRI.max_gain;
}
double miriGetGainDb(int step)
{
if (step < 0)
step = 0;
if (step >= MIRI.max_gain)
step = MIRI.max_gain;
return step;
}
int miriSetGain(int step)
{
int r;
if (step < 0) {
step = 0;
}
if (step >= MIRI.max_gain) {
step = MIRI.max_gain;
}
r = mirisdr_set_tuner_gain(MIRI.dev, step);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
else
MIRI.gain = step;
return MIRI.gain;
}