-
Notifications
You must be signed in to change notification settings - Fork 5
/
air.c
231 lines (184 loc) · 4.95 KB
/
air.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
/*
* 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 <libairspy/airspy.h>
#include <complex.h>
#include "adsbdec.h"
#define AIR_SAMPLE_RATE 20000000
int gain = 18;
#define APBUFFSZ (8196*PULSEW)
static float ampbuff[APBUFFSZ+2];
static uint32_t inidx = 0;
static uint32_t fidx = 0 ;
static complex int rbuff[PULSEW]={0};
static complex int fval=0;
extern void handlerExit(int sig);
static void decodeiq(const unsigned short *r, const int len)
{
int rlen;
for (int i = 0; i < len; ) {
complex int val;
int a,b;
a = (int)r[i]-0x800;i++;
b = (int)r[i]-0x800;i++;
val=(a+b)+(-a+b)*I;
fval=fval+val-rbuff[fidx%PULSEW];
rbuff[fidx%PULSEW] = val ;
fidx++;
ampbuff[inidx]=creal(fval)*creal(fval)+cimag(fval)*cimag(fval);
inidx++;
a = (int)r[i]-0x800;i++;
b = (int)r[i]-0x800;i++;
val=-(a+b)+(a-b)*I;
fval=fval+val-rbuff[fidx%PULSEW];
rbuff[fidx%PULSEW] = val ;
fidx++;
ampbuff[inidx]=creal(fval)*creal(fval)+cimag(fval)*cimag(fval);
inidx++;
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;
}
static struct airspy_device *device = NULL;
int initSDR(void)
{
int result;
uint32_t i, count;
uint32_t *supported_samplerates;
/* init airspy */
result = airspy_open(&device);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_open() failed: %s (%d)\n", airspy_error_name(result),
result);
return -1;
}
result = airspy_set_sample_type(device, AIRSPY_SAMPLE_RAW);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_set_sample_type() failed: %s (%d)\n",
airspy_error_name(result), result);
airspy_close(device);
return -1;
}
result = airspy_set_packing(device, 1);
airspy_get_samplerates(device, &count, 0);
supported_samplerates = (uint32_t *) malloc(count * sizeof(uint32_t));
airspy_get_samplerates(device, supported_samplerates, count);
for (i = 0; i < count; i++) {
if (supported_samplerates[i] == AIR_SAMPLE_RATE)
break;
}
if (i >= count) {
fprintf(stderr, "did not find needed sampling rate\n");
airspy_close(device);
return -1;
}
free(supported_samplerates);
result = airspy_set_samplerate(device, i);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_set_samplerate() failed: %s (%d)\n",
airspy_error_name(result), result);
airspy_close(device);
return -1;
}
/* had problem with packing , disable it */
airspy_set_packing(device, 0);
result = airspy_set_linearity_gain(device, gain);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_set_linearity_gain() failed: %s (%d)\n",
airspy_error_name(result), result);
airspy_close(device);
return -1;
}
result = airspy_set_freq(device, 1090000000);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_set_freq() failed: %s (%d)\n", airspy_error_name(result),
result);
airspy_close(device);
return -1;
}
return 0;
}
static int rx_callback(airspy_transfer_t * transfer)
{
decodeiq((unsigned short *)(transfer->samples), transfer->sample_count);
return 0;
}
int startSDR(void)
{
int result;
if(device == NULL) return 0;
if(airspy_is_streaming(device) == AIRSPY_TRUE)
return 0;
result = airspy_start_rx(device, rx_callback, NULL);
if (result != AIRSPY_SUCCESS) {
fprintf(stderr, "airspy_start_rx() failed: %s (%d)\n", airspy_error_name(result),
result);
airspy_close(device);
return -1;
}
return 0;
}
void stopSDR(void)
{
if(device == NULL) return ;
airspy_stop_rx(device);
}
void closeSDR(void)
{
if(device == NULL) return ;
airspy_stop_rx(device);
airspy_close(device);
}
char *filename = NULL;
#define IQBUFFSZ (1024*1024)
void *fileInput(void *arg)
{
int fd;
unsigned short *iqbuff;
fd = open(filename, O_RDONLY);
if (fd < 0) {
handlerExit(0);
return NULL;
}
iqbuff = (unsigned short *)malloc(IQBUFFSZ * sizeof(short));
do {
int n;
n = read(fd, iqbuff, IQBUFFSZ * sizeof(short));
if (n <= 0)
break;
decodeiq(iqbuff, n / 2);
} while (1);
close(fd);
handlerExit(0);
return NULL;
}