forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirisdr.c
206 lines (196 loc) · 6.26 KB
/
mirisdr.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
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Tomasz Lemiech <szpajder@gmail.com>
*
* 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 <mirisdr.h>
#include "dumpvdl2.h"
#include "mirisdr.h"
static mirisdr_dev_t *mirisdr = NULL;
/* taken from librtlsdr-keenerd, (c) Kyle Keen */
static int mirisdr_nearest_gain(mirisdr_dev_t *dev, int target_gain) {
int i, r, err1, err2, count, nearest;
int* gains;
r = mirisdr_set_tuner_gain_mode(dev, 1);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
return r;
}
count = mirisdr_get_tuner_gains(dev, NULL);
if (count <= 0) {
return -1;
}
gains = XCALLOC(count, sizeof(int));
count = mirisdr_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;
}
static int mirisdr_verbose_device_search(char *s) {
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256];
device_count = mirisdr_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++) {
mirisdr_get_device_usb_strings(i, vendor, product, serial);
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
}
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, mirisdr_get_device_name((uint32_t)device));
return device;
}
/* does string exact match a serial */
for (i = 0; i < device_count; i++) {
mirisdr_get_device_usb_strings(i, vendor, product, serial);
if (strcmp(s, serial) != 0) {
continue;}
device = i;
fprintf(stderr, "Using device %d: %s\n",
device, mirisdr_get_device_name((uint32_t)device));
return device;
}
/* does string prefix match a serial */
for (i = 0; i < device_count; i++) {
mirisdr_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, mirisdr_get_device_name((uint32_t)device));
return device;
}
/* does string suffix match a serial */
for (i = 0; i < device_count; i++) {
mirisdr_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, mirisdr_get_device_name((uint32_t)device));
return device;
}
fprintf(stderr, "No matching devices found.\n");
return -1;
}
void mirisdr_init(vdl2_state_t *ctx, char *dev, int flavour, uint32_t freq, float gain, int freq_offset, int usb_xfer_mode) {
int r;
mirisdr_hw_flavour_t hw_flavour;
switch(flavour) {
case 0:
hw_flavour = MIRISDR_HW_DEFAULT;
break;
case 1:
hw_flavour = MIRISDR_HW_SDRPLAY;
break;
default:
fprintf(stderr, "Unknown device variant %u\n", flavour);
_exit(1);
}
int device = mirisdr_verbose_device_search(dev);
if(device < 0)
_exit(1);
r = mirisdr_open(&mirisdr, hw_flavour, device);
if(mirisdr == NULL) {
fprintf(stderr, "Failed to open mirisdr device #%u: error %d\n", device, r);
_exit(1);
}
if(usb_xfer_mode == 0)
r = mirisdr_set_transfer(mirisdr, "ISOC");
else if(usb_xfer_mode == 1)
r = mirisdr_set_transfer(mirisdr, "BULK");
else {
fprintf(stderr, "Invalid USB transfer mode\n");
_exit(1);
}
if (r < 0) {
fprintf(stderr, "Failed to set transfer mode for device #%d: error %d\n", device, r);
_exit(1);
}
fprintf(stderr, "Using USB transfer mode %s\n", mirisdr_get_transfer(mirisdr));
r = mirisdr_set_sample_rate(mirisdr, MIRISDR_RATE);
if (r < 0) {
fprintf(stderr, "Failed to set sample rate for device #%d: error %d\n", device, r);
_exit(1);
}
r = mirisdr_set_center_freq(mirisdr, freq - freq_offset);
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 - freq_offset);
if(gain == SDR_AUTO_GAIN) {
r = mirisdr_set_tuner_gain_mode(mirisdr, 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 = mirisdr_nearest_gain(mirisdr, (int)gain);
if(ngain < 0) {
fprintf(stderr, "Failed to read supported gain list for device #%d: error %d\n", device, ngain);
_exit(1);
}
r = mirisdr_set_tuner_gain_mode(mirisdr, 1);
r |= mirisdr_set_tuner_gain(mirisdr, ngain);
if (r < 0) {
fprintf(stderr, "Failed to set gain to %d for device #%d: error %d\n",
ngain, device, r);
_exit(1);
} else
fprintf(stderr, "Device #%d: gain set to %d dB\n", device,
mirisdr_get_tuner_gain(mirisdr));
}
r = mirisdr_set_sample_format(mirisdr, "252_S16");
if (r < 0) {
fprintf(stderr, "Failed to set sample format for device #%d: error %d\n", device, r);
_exit(1);
}
mirisdr_reset_buffer(mirisdr);
fprintf(stderr, "Device %d started\n", device);
ctx->sbuf = XCALLOC(MIRISDR_BUFSIZE / sizeof(int16_t), sizeof(float));
if(mirisdr_read_async(mirisdr, process_buf_short, ctx, MIRISDR_BUFCNT, MIRISDR_BUFSIZE) < 0) {
fprintf(stderr, "Device #%d: async read failed\n", device);
_exit(1);
}
}
void mirisdr_cancel() {
if(mirisdr != NULL)
mirisdr_cancel_async(mirisdr);
}