forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soapy.c
323 lines (274 loc) · 8.23 KB
/
soapy.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
#ifdef WITH_SOAPY
#define _GNU_SOURCE
#include <complex.h>
#include <math.h>
#include <pthread.h>
#include <signal.h>
#include <SoapySDR/Device.h>
#include <SoapySDR/Formats.h>
#include <SoapySDR/Types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "acarsdec.h"
static SoapySDRDevice *dev = NULL;
static SoapySDRStream *stream = NULL;
static int16_t* soapyInBuf = NULL;
static int soapyInBufSize = 0;
static int soapyInRate = 0;
static int watchdogCounter = 50;
static int current_index = 0;
static pthread_mutex_t cbMutex = PTHREAD_MUTEX_INITIALIZER;
#define SOAPYOUTBUFSZ 1024
static unsigned int chooseFc(unsigned int *Fd, unsigned int nbch)
{
int n;
int ne;
int Fc;
do {
ne = 0;
for (n = 0; n < nbch - 1; n++) {
if (Fd[n] > Fd[n + 1]) {
unsigned int t;
t = Fd[n + 1];
Fd[n + 1] = Fd[n];
Fd[n] = t;
ne = 1;
}
}
} while (ne);
if ((Fd[nbch - 1] - Fd[0]) > soapyInRate - 4 * INTRATE) {
fprintf(stderr, "Frequencies too far apart\n");
return 0;
}
for (Fc = Fd[nbch - 1] + 2 * INTRATE; Fc > Fd[0] - 2 * INTRATE; Fc--) {
for (n = 0; n < nbch; n++) {
if (abs(Fc - Fd[n]) > soapyInRate / 2 - 2 * INTRATE)
break;
if (abs(Fc - Fd[n]) < 2 * INTRATE)
break;
if (n > 0 && Fc - Fd[n - 1] == Fd[n] - Fc)
break;
}
if (n == nbch)
break;
}
return Fc;
}
int initSoapy(char **argv, int optind)
{
int r, n;
char *argF;
unsigned int Fc;
unsigned int Fd[MAXNBCHANNELS];
if (argv[optind] == NULL) {
fprintf(stderr, "Need device string (ex: driver=rtltcp,rtltcp=127.0.0.1) after -d\n");
exit(1);
}
dev = SoapySDRDevice_makeStrArgs(argv[optind]);
if(dev == NULL) {
fprintf(stderr, "Error opening SoapySDR device using string \"%s\": %s", argv[optind], SoapySDRDevice_lastError());
return -1;
}
optind++;
soapyInBufSize = SOAPYOUTBUFSZ * rateMult * 2;
soapyInRate = INTRATE * rateMult;
soapyInBuf = malloc(sizeof(int16_t) * soapyInBufSize);
if (gain == -10.0) {
if (verbose)
fprintf(stderr, "Tuner gain: AGC\n");
r = SoapySDRDevice_setGainMode(dev, SOAPY_SDR_RX, 0, 1);
if (r != 0)
fprintf(stderr, "WARNING: Failed to turn on AGC: %s\n", SoapySDRDevice_lastError());
} else {
r = SoapySDRDevice_setGainMode(dev, SOAPY_SDR_RX, 0, 0);
if (r != 0)
fprintf(stderr, "WARNING: Failed to turn off AGC: %s\n", SoapySDRDevice_lastError());
if (verbose)
fprintf(stderr, "Setting gain to: %f\n", gain);
r = SoapySDRDevice_setGain(dev, SOAPY_SDR_RX, 0, gain);
if (r != 0)
fprintf(stderr, "WARNING: Failed to set gain: %s\n", SoapySDRDevice_lastError());
}
if (ppm != 0) {
r = SoapySDRDevice_setFrequencyCorrection(dev, SOAPY_SDR_RX, 0, ppm);
if (r != 0)
fprintf(stderr, "WARNING: Failed to set freq correction: %s\n", SoapySDRDevice_lastError());
}
nbch = 0;
while ((argF = argv[optind]) && nbch < MAXNBCHANNELS) {
Fd[nbch] = ((int)(1000000 * atof(argF) + INTRATE / 2) / INTRATE) * INTRATE;
optind++;
if (Fd[nbch] < 118000000 || Fd[nbch] > 138000000) {
fprintf(stderr, "WARNING: frequency not in range 118-138 MHz: %d\n",
Fd[nbch]);
continue;
}
channel[nbch].chn = nbch;
channel[nbch].Fr = (float)Fd[nbch];
nbch++;
};
if (nbch > MAXNBCHANNELS)
fprintf(stderr,
"WARNING: too many frequencies, using only the first %d\n",
MAXNBCHANNELS);
if (nbch == 0) {
fprintf(stderr, "Need a least one frequency\n");
return 1;
}
if(freq == 0)
freq = chooseFc(Fd, nbch);
if(freq == 0)
return 1;
for (n = 0; n < nbch; n++) {
if (Fd[n] < freq - soapyInRate/2 || Fd[n] > freq + soapyInRate/2) {
fprintf(stderr, "WARNING: frequency not in tuned range %d-%d: %d\n",
freq - soapyInRate/2, freq + soapyInRate/2, Fd[n]);
continue;
}
}
for (n = 0; n < nbch; n++) {
channel_t *ch = &(channel[n]);
int ind;
float AMFreq;
ch->counter = 0;
ch->D = 0;
ch->oscillator = malloc(rateMult * sizeof(float complex));
ch->dm_buffer = malloc(SOAPYOUTBUFSZ*sizeof(float));
AMFreq = (ch->Fr - (float)freq) / (float)(soapyInRate) * 2.0 * M_PI;
for (ind = 0; ind < rateMult; ind++) {
ch->oscillator[ind] = cexpf(AMFreq*ind*-I)/rateMult;
}
}
if (verbose)
fprintf(stderr, "Set center freq. to %dHz\n", (int)freq);
r = SoapySDRDevice_setFrequency(dev, SOAPY_SDR_RX, 0, freq, NULL);
if (r != 0)
fprintf(stderr, "WARNING: Failed to set frequency: %s\n", SoapySDRDevice_lastError());
if (verbose)
fprintf(stderr, "Setting sample rate: %.4f MS/s\n", soapyInRate / 1e6);
r = SoapySDRDevice_setSampleRate(dev, SOAPY_SDR_RX, 0, soapyInRate);
if (r != 0)
fprintf(stderr, "WARNING: Failed to set sample rate: %s\n", SoapySDRDevice_lastError());
stream = SoapySDRDevice_setupStream(dev, SOAPY_SDR_RX, SOAPY_SDR_CS16, NULL, 0, NULL);
return 0;
}
int soapySetAntenna(const char *antenna) {
if (dev == NULL) {
fprintf(stderr, "soapySetAntenna: SoapySDR not init'd\n");
return 1;
}
if (antenna == NULL) {
fprintf(stderr, "soapySetAntenna: antenna is NULL\n");
return 1;
}
if (SoapySDRDevice_setAntenna(dev, SOAPY_SDR_RX, 0, antenna) != 0) {
fprintf(stderr, "soapySetAntenna: SoapySDRDevice_setAntenna failed (check antenna validity)\n");
return 1;
}
return 0;
}
static void *readThreadEntryPoint(void *arg) {
int n;
int res = 0;
int flags = 0;
long long timens = 0;
void* bufs[] = { soapyInBuf };
SoapySDRDevice_activateStream(dev, stream, 0, 0, 0);
while(!signalExit) {
pthread_mutex_lock(&cbMutex);
watchdogCounter = 50;
pthread_mutex_unlock(&cbMutex);
flags = 0;
res = SoapySDRDevice_readStream(dev, stream, bufs, soapyInBufSize/2, &flags, &timens, 10000000);
if(res <= 0) {
fprintf(stderr, "WARNING: Failed to read SoapySDR stream (%d): %s\n", res, SoapySDRDevice_lastError());
pthread_mutex_lock(&cbMutex);
signalExit = 1;
pthread_mutex_unlock(&cbMutex);
return NULL;
}
int n, i;
int local_ind;
for (n = 0; n < nbch; n++) {
local_ind = current_index;
channel_t *ch = &(channel[n]);
float complex D = ch->D;
for (i = 0; i < res*2; i+=2) {
float r = (float)soapyInBuf[i];
float g = (float)soapyInBuf[i+1];
float complex v = r + g*I;
D += v * ch->oscillator[local_ind++] / 32768.0;
if (local_ind >= rateMult) {
ch->dm_buffer[ch->counter++] = cabsf(D);
local_ind = 0;
D = 0;
if (ch->counter >= SOAPYOUTBUFSZ) {
demodMSK(ch, SOAPYOUTBUFSZ);
ch->counter = 0;
}
}
}
ch->D = D;
}
current_index = (current_index + res) % rateMult;
}
pthread_mutex_lock(&cbMutex);
signalExit = 1;
pthread_mutex_unlock(&cbMutex);
return NULL;
}
int runSoapySample(void)
{
pthread_t readThread;
pthread_create(&readThread, NULL, readThreadEntryPoint, NULL);
pthread_mutex_lock(&cbMutex);
while (!signalExit) {
if (--watchdogCounter <= 0) {
fprintf(stderr, "No data from SoapySDR for 5 seconds, exiting ...\n");
break;
}
pthread_mutex_unlock(&cbMutex);
usleep(100 * 1000); // 0.1 seconds
pthread_mutex_lock(&cbMutex);
}
pthread_mutex_unlock(&cbMutex);
int count = 100; // 10 seconds
int err = 0;
// Wait on reader thread exit
while (count-- > 0 && (err = pthread_tryjoin_np(readThread, NULL))) {
usleep(100 * 1000); // 0.1 seconds
}
if (err) {
fprintf(stderr, "Receive thread termination failed, will raise SIGKILL to ensure we die!\n");
raise(SIGKILL);
return 1;
}
return 0;
}
int runSoapyClose(void) {
int res = 0;
if (soapyInBuf) {
free(soapyInBuf);
soapyInBuf = NULL;
}
if (stream) {
res = SoapySDRDevice_closeStream(dev, stream);
stream = NULL;
if (res != 0)
fprintf(stderr, "WARNING: Failed to close SoapySDR stream: %s\n", SoapySDRDevice_lastError());
res = SoapySDRDevice_deactivateStream(dev, stream, 0, 0);
stream = NULL;
if (res != 0)
fprintf(stderr, "WARNING: Failed to deactivate SoapySDR stream: %s\n", SoapySDRDevice_lastError());
}
if (dev) {
res = SoapySDRDevice_unmake(dev);
dev = NULL;
if (res != 0)
fprintf(stderr, "WARNING: Failed to close SoapySDR device: %s\n", SoapySDRDevice_lastError());
}
return res;
}
#endif