-
Notifications
You must be signed in to change notification settings - Fork 5
/
rtl.c
263 lines (224 loc) · 5.99 KB
/
rtl.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
/*
* Copyright (c) 2018 Thierry Leconte
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <rtl-sdr.h>
#include <complex.h>
#include "adsbdec.h"
int gain = 493;
#define SDRINRATE 2500000
static rtlsdr_dev_t *dev = NULL;
#define IQBUFFSZ (128*1024)
#define APBUFFSZ (4096*PULSEW)
static float ampbuff[APBUFFSZ+5];
static uint32_t inidx = 0;
static void in_callback(unsigned char *r, uint32_t len, void *ctx)
{
static complex float prev=0;
const complex float zr = 128.5+128.5*I;
int rlen;
for (int i = 0; i < len; ) {
complex float curr;
complex float val;
curr=((float)r[i++]+(float)r[i++]*I) - zr;
val=0.8536*prev+0.1464*curr;
ampbuff[inidx++]=creal(val)*creal(val)+cimag(val)*cimag(val);
val=0.5*prev+0.5*curr;
ampbuff[inidx++]=creal(val)*creal(val)+cimag(val)*cimag(val);
val=0.1464*prev+0.8536*curr;
ampbuff[inidx++]=creal(val)*creal(val)+cimag(val)*cimag(val);
ampbuff[inidx++]=creal(curr)*creal(curr)+cimag(curr)*cimag(curr);
prev=curr;
if(inidx>=APBUFFSZ) {
rlen = deqframe(ampbuff, inidx);
if(rlen<inidx)
memcpy(ampbuff,&(ampbuff[rlen]),(inidx-rlen)*sizeof(float));
inidx=inidx-rlen;
}
}
rlen = deqframe(ampbuff, inidx);
if(rlen<inidx)
memcpy(ampbuff,&(ampbuff[rlen]),(inidx-rlen)*sizeof(float));
inidx=inidx-rlen;
}
/* function verbose_device_search by Kyle Keen
* from http://cgit.osmocom.org/rtl-sdr/tree/src/convenience/convenience.c
*/
static int verbose_device_search(char *s)
{
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256];
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
return -1;
}
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
}
/* does string look like raw id number */
device = (int)strtol(s, &s2, 0);
if (s2[0] == '\0' && device >= 0 && device < device_count) {
return device;
}
/* does string exact match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (strcmp(s, serial) != 0) {
continue;
}
device = i;
return device;
}
/* does string prefix match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (strncmp(s, serial, strlen(s)) != 0) {
continue;
}
device = i;
return device;
}
/* does string suffix match a serial */
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
offset = strlen(serial) - strlen(s);
if (offset < 0) {
continue;
}
if (strncmp(s, serial + offset, strlen(s)) != 0) {
continue;
}
device = i;
return device;
}
fprintf(stderr, "No matching devices found.\n");
return -1;
}
static int nearest_gain(int target_gain)
{
int i, err1, err2, count, close_gain;
int *gains;
count = rtlsdr_get_tuner_gains(dev, NULL);
if (count <= 0) {
return 0;
}
gains = malloc(sizeof(int) * count);
count = rtlsdr_get_tuner_gains(dev, gains);
close_gain = gains[0];
for (i = 0; i < count; i++) {
err1 = abs(target_gain - close_gain);
err2 = abs(target_gain - gains[i]);
if (err2 < err1) {
close_gain = gains[i];
}
}
free(gains);
fprintf(stderr, "Gain:%d\n",close_gain);
return close_gain;
}
int initSDR(void)
{
int r;
int dev_index;
/*dev_index = verbose_device_search(argv[optind]); */
dev_index = 0 ;
r = rtlsdr_open(&dev, dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device\n");
return r;
}
rtlsdr_set_tuner_gain_mode(dev, 1);
r = rtlsdr_set_tuner_gain(dev, nearest_gain(gain));
if (r < 0)
fprintf(stderr, "WARNING: Failed to set gain.\n");
r = rtlsdr_set_center_freq(dev, 1090000000);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set center freq.\n");
return 1;
}
r = rtlsdr_set_sample_rate(dev, SDRINRATE);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
return 1;
}
r = rtlsdr_reset_buffer(dev);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
return 1;
}
return 0;
}
static void *readthread(void *arg)
{
int r=-1;
if(dev) r = rtlsdr_read_async(dev, in_callback, NULL, 4, IQBUFFSZ);
if (r) {
fprintf(stderr, "Read async %d\n", r);
}
return NULL;
}
int startSDR(void)
{
pthread_t th;
fprintf(stderr, "StartSDR\n");
pthread_create(&th, NULL, readthread, NULL);
return 0;
}
void stopSDR(void)
{
fprintf(stderr, "StopSDR\n");
if(dev) rtlsdr_cancel_async(dev);
}
void closeSDR(void)
{
if (dev) {
rtlsdr_close(dev);
dev = NULL;
}
}
char *filename = NULL;
void *fileInput(void *arg)
{
int fd;
unsigned char *iqbuff;
fd = open(filename, O_RDONLY);
if (fd < 0) {
handlerExit(0);
return NULL;
}
iqbuff = (unsigned char *)malloc(IQBUFFSZ * sizeof(unsigned char));
do {
int n;
n = read(fd, iqbuff, IQBUFFSZ * sizeof(unsigned char));
if (n <= 0)
break;
in_callback(iqbuff, n, NULL);
} while (1);
close(fd);
handlerExit(0);
return NULL;
}