forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
air.c
258 lines (214 loc) · 6.44 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
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
/*
* Copyright (c) 2015 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.
*
*/
#ifdef WITH_AIR
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <libairspy/airspy.h>
#include "acarsdec.h"
#define AIRMULT 400
#define AIRINRATE (INTRATE*AIRMULT)
static struct airspy_device* device = NULL;
extern void *compute_thread(void *arg);
static const unsigned int r820t_hf[]={1953050,1980748,2001344,2032592,2060291,2087988};
static const unsigned int r820t_lf[]={525548,656935,795424,898403,1186034,1502073,1715133,1853622};
static unsigned int chooseFc(unsigned int minF,unsigned int maxF)
{
unsigned int bw=maxF-minF+2*INTRATE;
unsigned int off;
int i,j;
for(i=7;i>=0;i--)
if((r820t_hf[5]-r820t_lf[i])>=bw) break;
if(i<0) return 0;
for(j=5;j>=0;j--)
if((r820t_hf[j]-r820t_lf[i])<=bw) break;
j++;
off=(r820t_hf[j]+r820t_lf[i])/2-AIRINRATE/4;
airspy_r820t_write(device, 10, 0xB0 | (15-j));
airspy_r820t_write(device, 11, 0xE0 | (15-i));
return(((maxF+minF)/2+off+INTRATE/2)/INTRATE*INTRATE);
}
int initAirspy(char **argv, int optind)
{
int n;
char *argF;
int Fc,minFc=140000000,maxFc=0;
int Fd[MAXNBCHANNELS];
int result;
uint32_t i,count;
uint32_t * supported_samplerates;
/* parse args */
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: Invalid frequency %d\n",
Fd[nbch]);
continue;
}
channel[nbch].chn = nbch;
channel[nbch].Fr = Fd[nbch];
if(Fd[nbch]<minFc) minFc= Fd[nbch];
if(Fd[nbch]>maxFc) maxFc= Fd[nbch];
nbch++;
};
if (nbch > MAXNBCHANNELS)
fprintf(stderr,
"WARNING: too many frequencies, taking only the first %d\n",
MAXNBCHANNELS);
if (nbch == 0) {
fprintf(stderr, "Need a least one frequency\n");
return 1;
}
/* init airspy */
result = airspy_open(&device);
if( result != AIRSPY_SUCCESS ) {
fprintf(stderr,"airspy_open() failed: %s (%d)\n", airspy_error_name(result), result);
airspy_exit();
return -1;
}
result = airspy_set_sample_type(device, AIRSPY_SAMPLE_FLOAT32_REAL);
if( result != AIRSPY_SUCCESS ) {
fprintf(stderr,"airspy_set_sample_type() failed: %s (%d)\n", airspy_error_name(result), result);
airspy_close(device);
airspy_exit();
return -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]==AIRINRATE) break;
if(i>=count) {
fprintf(stderr,"did not find needed sampling rate\n");
airspy_exit();
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);
airspy_exit();
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_vga_gain() failed: %s (%d)\n", airspy_error_name(result), result);
}
Fc=chooseFc(minFc,maxFc);
if(Fc==0) {
fprintf(stderr, "Frequencies too far apart\n");
return 1;
}
result = airspy_set_freq(device, Fc);
if( result != AIRSPY_SUCCESS ) {
fprintf(stderr,"airspy_set_freq() failed: %s (%d)\n", airspy_error_name(result), result);
airspy_close(device);
airspy_exit();
return -1;
}
if (verbose)
fprintf(stderr, "Set freq. to %d hz\n", Fc);
/* computes mixers osc. */
for (n = 0; n < nbch; n++) {
channel_t *ch = &(channel[n]);
int i;
double AMFreq,Ph;
ch->wf = malloc(AIRMULT * sizeof(float complex));
ch->dm_buffer = malloc(512 * sizeof(double));
ch->D=0;
AMFreq = 2.0*M_PI*(double)(Fc-ch->Fr+AIRINRATE/4)/(double)(AIRINRATE);
for (i = 0, Ph=0; i < AIRMULT; i++) {
ch->wf[i]=cexpf(Ph*-I)/AIRMULT;
Ph+=AMFreq;
if(Ph>2.0*M_PI) Ph-=2.0*M_PI;
if(Ph<-2.0*M_PI) Ph+=2.0*M_PI;
}
}
return 0;
}
int ind=0;
static int rx_callback(airspy_transfer_t* transfer)
{
float* pt_rx_buffer;
int n,i;
int bo,be,ben,nbk;
pt_rx_buffer = (float *)(transfer->samples);
bo=AIRMULT-ind;
nbk=(transfer->sample_count-bo)/AIRMULT;
be=nbk*AIRMULT+bo;
ben=transfer->sample_count-be;
for(n=0;n<nbch;n++) {
channel_t *ch = &(channel[n]);
float S;
int k,bn,m;
float complex D;
D=ch->D;
/* compute */
m=0;k=0;
for (i=ind; i < AIRMULT;i++,k++) {
S = pt_rx_buffer[k];
D += ch->wf[i] * S;
}
ch->dm_buffer[m++]=cabsf(D);
for (bn=0; bn<nbk;bn++) {
D=0;
for (i=0; i < AIRMULT;i++,k++) {
S = pt_rx_buffer[k];
D += ch->wf[i] * S;
}
ch->dm_buffer[m++]=cabsf(D);
}
D=0;
for (i=0; i<ben;i++,k++) {
S = pt_rx_buffer[k];
D += ch->wf[i] * S;
}
ch->D=D;
demodMSK(ch,m);
}
ind=ben;
return 0;
}
int runAirspySample(void)
{
int result ;
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);
airspy_exit();
return -1;
}
while(airspy_is_streaming(device) == AIRSPY_TRUE) {
sleep(2);
}
return 0;
}
#endif