forked from baycom/tfrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdr.cpp
280 lines (262 loc) · 6.64 KB
/
sdr.cpp
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
/*
tfrec - Receiver for TFA IT+ (and compatible) sensors
(c) 2017 Georg Acher, Deti Fliegl {acher|fliegl}(at)baycom.de
#include <GPL-v2>
sdr.cpp -- wrapper around librtlsdr
Parts taken from librtlsdr (rtl_fm.c)
rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
Copyright (C) 2012 by Steve Markgraf <[email protected]>
Copyright (C) 2012 by Hoernchen <[email protected]>
Copyright (C) 2012 by Kyle Keen <[email protected]>
Copyright (C) 2013 by Elias Oenal <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "sdr.h"
#define READ_TIMEOUT 5
//-------------------------------------------------------------------------
sdr::sdr(int dev_index, int _dbg, int dumpmode, char *dumpfile)
{
buffer=0;
wr_ptr=0;
rd_ptr=0;
r_thread=NULL;
running=0;
dev=NULL;
dump_fd=NULL;
if (dumpmode>0 && dumpfile) {
dump_fd=fopen(dumpfile,"wb");
if (!dump_fd) {
perror(dumpfile);
exit(-1);
}
}
set_buffer_len(2*1024*1024);
vendor="";
product="";
serial="";
dbg=_dbg;
char vendorc[256], productc[256], serialc[256];
rtlsdr_get_device_usb_strings(dev_index, vendorc, productc, serialc);
vendor=vendorc;
product=productc;
serial=serialc;
int r;
while(1) {
r=rtlsdr_open(&dev, dev_index);
if (!r)
break;
fprintf(stderr, "RET OPEN %i, retry\n",r);
sleep(1);
}
set_ppm(0);
// set_gain(0,0);
pthread_cond_init(&ready, NULL);
pthread_mutex_init(&ready_m, NULL);
}
//-------------------------------------------------------------------------
sdr::~sdr(void)
{
stop();
}
//-------------------------------------------------------------------------
int sdr::search_device(char *substr)
{
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256],sum[1024];
device_count = rtlsdr_get_device_count();
if (!device_count) {
printf("No supported devices found.\n");
return -1;
}
if (!strcmp(substr,"?"))
printf("Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++) {
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
if (!strcmp(substr,"?"))
printf(" %d: %s, %s, SN%s\n", i, vendor, product, serial);
else {
snprintf(sum,sizeof(sum),"%s %s SN%s",vendor, product, serial);
if(strcasestr(sum,substr)) {
printf("match device index %i %s\n",i,sum);
return i;
}
}
}
return -1;
}
//-------------------------------------------------------------------------
int sdr::start(void)
{
if (!dev)
return -1;
int r;
r=rtlsdr_reset_buffer(dev);
running=1;
r_thread=new thread(&sdr::read_thread,this);
alarm(READ_TIMEOUT);
return 0;
}
//-------------------------------------------------------------------------
int sdr::stop(void)
{
if (r_thread) {
rtlsdr_cancel_async(dev);
r_thread->join();
delete r_thread;
r_thread=0;
}
running=0;
return 0;
}
//-------------------------------------------------------------------------
// l: number of samples
int sdr::set_buffer_len(int l)
{
if (running)
return 0;
if (buffer)
free(buffer);
buffer=(int16_t*)malloc(sizeof(int16_t) * l);
buffer_len=l;
wr_ptr=0;
rd_ptr=0;
return 0;
}
//-------------------------------------------------------------------------
int sdr::set_frequency(uint32_t f)
{
if (!dev)
return -1;
int r;
r=rtlsdr_set_center_freq(dev, f);
cur_frequ=f;
if (dbg>=1)
printf("Frequency %.4lfMHz\n",(float)(f/1e6));
return r;
}
//-------------------------------------------------------------------------
int sdr::nearest_gain(int g)
{
int i, r, err1, err2, count, nearest;
int* gains;
r = rtlsdr_set_tuner_gain_mode(dev, 1);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
return r;
}
count = rtlsdr_get_tuner_gains(dev, NULL);
if (count <= 0) {
return 0;
}
gains =(int*) malloc(sizeof(int) * count);
count = rtlsdr_get_tuner_gains(dev, gains);
nearest = gains[0];
for (i=0; i<count; i++) {
err1 = abs(g - nearest);
err2 = abs(g - gains[i]);
if (err2 < err1) {
nearest = gains[i];
}
}
free(gains);
return nearest;
}
//-------------------------------------------------------------------------
// mode=0 -> auto gain
int sdr::set_gain(int mode,float g)
{
if (!dev)
return -1;
int r;
if (mode==0) {
if (dbg)
printf("AUTO GAIN\n");
r=rtlsdr_set_tuner_gain_mode(dev,0);
cur_gain=0;
} else {
cur_gain=nearest_gain(g*10);
if (dbg)
printf("GAIN %.1f\n",cur_gain/10.0);
r=rtlsdr_set_tuner_gain_mode(dev, 1);
r=rtlsdr_set_tuner_gain(dev, cur_gain);
}
cur_gain_mode=mode;
return cur_gain;
}
//-------------------------------------------------------------------------
int sdr::set_ppm(int p)
{
if (!dev)
return -1;
int r=rtlsdr_set_freq_correction(dev, p);
cur_ppm=p;
return r;
}
//-------------------------------------------------------------------------
int sdr::set_samplerate(int s)
{
if (!dev)
return -1;
int r;
r=rtlsdr_set_sample_rate(dev, s);
if (dbg)
printf("Samplerate %i\n",s);
cur_sr=s;
return r;
}
//-------------------------------------------------------------------------
void sdr::read_data(unsigned char *buf, uint32_t len)
{
// printf("Got %i\n",len);
int w=wr_ptr;
if (dump_fd)
fwrite(buf,len,1,dump_fd);
for (uint32_t i=0;i<len;i++) {
buffer[w++] = ((int16_t)(buf[i]) - 128)<<6; // Scale to +-8192
if (w>=buffer_len)
w=0;
}
wr_ptr=w;
safe_cond_signal(&ready, &ready_m);
}
//-------------------------------------------------------------------------
static void sdr_read_callback(unsigned char *buf, uint32_t len, void *ctx)
{
if (!ctx)
return;
sdr *this_ptr=(sdr*)ctx;
this_ptr->read_data(buf,len);
alarm(READ_TIMEOUT);
}
//-------------------------------------------------------------------------
void sdr::read_thread(void)
{
if (dbg)
printf("START READ THREAD\n");
rtlsdr_read_async(dev, sdr_read_callback, this, 0, buffer_len/8);
}
//-------------------------------------------------------------------------
int sdr::wait(int16_t* &d, int &len)
{
safe_cond_wait(&ready, &ready_m);
d=buffer+rd_ptr;
if (rd_ptr<wr_ptr)
len=wr_ptr-rd_ptr;
else
len=buffer_len-rd_ptr; // ignore wraparound, defer to next read
return 0;
}
//-------------------------------------------------------------------------
void sdr::done(int len)
{
int r=rd_ptr+len;
if (r>=buffer_len)
r=r-buffer_len;
rd_ptr=r;
}
//-------------------------------------------------------------------------