forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl.c
189 lines (181 loc) · 5.79 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
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Tomasz Lemiech <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rtl-sdr.h>
#include "dumpvdl2.h"
#include "rtl.h"
static rtlsdr_dev_t *rtl = NULL;
/* taken from librtlsdr-keenerd, (c) Kyle Keen */
static int nearest_gain(rtlsdr_dev_t *dev, int target_gain) {
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 -1;
}
gains = XCALLOC(count, sizeof(int));
count = rtlsdr_get_tuner_gains(dev, gains);
nearest = gains[0];
for (i = 0; i < count; i++) {
err1 = abs(target_gain - nearest);
err2 = abs(target_gain - gains[i]);
if (err2 < err1) {
nearest = gains[i];
}
}
XFREE(gains);
return nearest;
}
/* taken from librtlsdr-keenerd, (c) Kyle Keen */
static int rtl_verbose_device_search(char *s) {
int i, device_count, device, offset;
char *s2;
char vendor[256] = {0}, product[256] = {0}, serial[256] = {0};
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
return -1;
}
fprintf(stderr, "Found %d device(s):\n", device_count);
for (i = 0; i < device_count; i++) {
if (rtlsdr_get_device_usb_strings(i, vendor, product, serial) == 0) {
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
} else {
fprintf(stderr, " %d: %s\n", i, "Failed to query data");
}
}
fprintf(stderr, "\n");
/* does string look like raw id number */
device = (int)strtol(s, &s2, 0);
if (s2[0] == '\0' && device >= 0 && device < device_count) {
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
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;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
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;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
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;
fprintf(stderr, "Using device %d: %s\n",
device, rtlsdr_get_device_name((uint32_t)device));
return device;
}
fprintf(stderr, "No matching devices found.\n");
return -1;
}
void rtl_init(vdl2_state_t *ctx, char *dev, int freq, float gain, int correction) {
int r;
int device = rtl_verbose_device_search(dev);
if(device < 0)
_exit(1);
r = rtlsdr_open(&rtl, device);
if(rtl == NULL) {
fprintf(stderr, "Failed to open rtlsdr device #%u: error %d\n", device, r);
_exit(1);
}
r = rtlsdr_set_sample_rate(rtl, RTL_RATE);
if (r < 0) {
fprintf(stderr, "Failed to set sample rate for device #%d: error %d\n", device, r);
_exit(1);
}
r = rtlsdr_set_center_freq(rtl, freq);
if (r < 0) {
fprintf(stderr, "Failed to set frequency for device #%d: error %d\n", device, r);
_exit(1);
}
fprintf(stderr, "Center frequency set to %u Hz\n", freq);
r = rtlsdr_set_freq_correction(rtl, correction);
if (r < 0 && r != -2 ) {
fprintf(stderr, "Failed to set freq correction for device #%d error %d\n", device, r);
_exit(1);
}
if(gain == SDR_AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(rtl, 0);
if (r < 0) {
fprintf(stderr, "Failed to set automatic gain for device #%d: error %d\n", device, r);
_exit(1);
} else
fprintf(stderr, "Device #%d: gain set to automatic\n", device);
} else {
int ngain = nearest_gain(rtl, (int)(gain * 10.f));
if(ngain < 0) {
fprintf(stderr, "Failed to read supported gain list for device #%d: error %d\n", device, ngain);
_exit(1);
}
r = rtlsdr_set_tuner_gain_mode(rtl, 1);
r |= rtlsdr_set_tuner_gain(rtl, ngain);
if (r < 0) {
fprintf(stderr, "Failed to set gain to %0.2f for device #%d: error %d\n",
(float)ngain / 10.f, device, r);
_exit(1);
} else
fprintf(stderr, "Device #%d: gain set to %0.2f dB\n", device,
(float)rtlsdr_get_tuner_gain(rtl) / 10.f);
}
r = rtlsdr_set_agc_mode(rtl, 0);
if (r < 0) {
fprintf(stderr, "Failed to disable AGC for device #%d: error %d\n", device, r);
_exit(1);
}
rtlsdr_reset_buffer(rtl);
fprintf(stderr, "Device %d started\n", device);
sbuf = XCALLOC(RTL_BUFSIZE / sizeof(uint8_t), sizeof(float));
process_buf_uchar_init();
if(rtlsdr_read_async(rtl, process_buf_uchar, NULL, RTL_BUFCNT, RTL_BUFSIZE) < 0) {
fprintf(stderr, "Device #%d: async read failed\n", device);
_exit(1);
}
}
void rtl_cancel() {
if(rtl != NULL)
rtlsdr_cancel_async(rtl);
}