1
|
/*
|
2
|
* sysmocom OsmoSDR
|
3
|
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
|
4
|
* Copyright (C) 2012 by Dimitri Stolnikov <horiz0n@gmx.net>
|
5
|
*
|
6
|
* This program is free software: you can redistribute it and/or modify
|
7
|
* it under the terms of the GNU General Public License as published by
|
8
|
* the Free Software Foundation, either version 2 of the License, or
|
9
|
* (at your option) any later version.
|
10
|
*
|
11
|
* This program is distributed in the hope that it will be useful,
|
12
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
* GNU General Public License for more details.
|
15
|
*
|
16
|
* You should have received a copy of the GNU General Public License
|
17
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
*/
|
19
|
|
20
|
#include <errno.h>
|
21
|
#include <signal.h>
|
22
|
#include <string.h>
|
23
|
#include <stdio.h>
|
24
|
#include <stdlib.h>
|
25
|
|
26
|
#ifndef _WIN32
|
27
|
#include <unistd.h>
|
28
|
#else
|
29
|
#include <Windows.h>
|
30
|
#endif
|
31
|
|
32
|
#include "osmosdr.h"
|
33
|
|
34
|
#define DEFAULT_SAMPLE_RATE 500000
|
35
|
#define DEFAULT_ASYNC_BUF_NUMBER 32
|
36
|
#define DEFAULT_BUF_LENGTH (16 * 16384)
|
37
|
#define MINIMAL_BUF_LENGTH 512
|
38
|
#define MAXIMAL_BUF_LENGTH (256 * 16384)
|
39
|
|
40
|
static int do_exit = 0;
|
41
|
static osmosdr_dev_t *dev = NULL;
|
42
|
|
43
|
void usage(void)
|
44
|
{
|
45
|
#ifdef _WIN32
|
46
|
fprintf(stderr,
|
47
|
"Usage:\t osmo_sdr.exe [device_index] [samplerate in kHz] "
|
48
|
"[gain] [frequency in Hz] [filename]\n");
|
49
|
#else
|
50
|
fprintf(stderr,
|
51
|
"Usage:\t -f frequency_to_tune_to [Hz]\n"
|
52
|
"\t[-s samplerate (default: 2048000 Hz)]\n"
|
53
|
"\t[-d device_index (default: 0)]\n"
|
54
|
"\t[-g gain (default: 0 for auto)]\n"
|
55
|
"\t[-b output_block_size (default: 16 * 16384)]\n"
|
56
|
"\t[-S force sync output (default: async)]\n"
|
57
|
"\tfilename (a '-' dumps samples to stdout)\n\n");
|
58
|
#endif
|
59
|
exit(1);
|
60
|
}
|
61
|
|
62
|
#ifdef _WIN32
|
63
|
BOOL WINAPI
|
64
|
sighandler(int signum)
|
65
|
{
|
66
|
if (CTRL_C_EVENT == signum) {
|
67
|
fprintf(stderr, "Signal caught, exiting!\n");
|
68
|
do_exit = 1;
|
69
|
osmosdr_cancel_async(dev);
|
70
|
return TRUE;
|
71
|
}
|
72
|
return FALSE;
|
73
|
}
|
74
|
#else
|
75
|
static void sighandler(int signum)
|
76
|
{
|
77
|
fprintf(stderr, "Signal caught, exiting!\n");
|
78
|
do_exit = 1;
|
79
|
osmosdr_cancel_async(dev);
|
80
|
}
|
81
|
#endif
|
82
|
|
83
|
static void osmosdr_callback(unsigned char *buf, uint32_t len, void *ctx)
|
84
|
{
|
85
|
if (ctx) {
|
86
|
if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
|
87
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
88
|
osmosdr_cancel_async(dev);
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
|
93
|
int main(int argc, char **argv)
|
94
|
{
|
95
|
#ifndef _WIN32
|
96
|
struct sigaction sigact;
|
97
|
#endif
|
98
|
char *filename = NULL;
|
99
|
int n_read;
|
100
|
int r, opt;
|
101
|
int i, gain = 0;
|
102
|
int sync_mode = 0;
|
103
|
FILE *file;
|
104
|
uint8_t *buffer;
|
105
|
uint32_t dev_index = 0;
|
106
|
uint32_t frequency = 100000000;
|
107
|
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
|
108
|
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
|
109
|
int device_count;
|
110
|
char vendor[256] = { 0 }, product[256] = { 0 }, serial[256] = { 0 };
|
111
|
int count;
|
112
|
int gains[100];
|
113
|
uint32_t rates[100];
|
114
|
|
115
|
#ifndef _WIN32
|
116
|
while ((opt = getopt(argc, argv, "d:f:g:s:b:S::")) != -1) {
|
117
|
switch (opt) {
|
118
|
case 'd':
|
119
|
dev_index = atoi(optarg);
|
120
|
break;
|
121
|
case 'f':
|
122
|
frequency = (uint32_t)atof(optarg);
|
123
|
break;
|
124
|
case 'g':
|
125
|
gain = (int)(atof(optarg) * 10); /* tenths of a dB */
|
126
|
break;
|
127
|
case 's':
|
128
|
samp_rate = (uint32_t)atof(optarg);
|
129
|
break;
|
130
|
case 'b':
|
131
|
out_block_size = (uint32_t)atof(optarg);
|
132
|
break;
|
133
|
case 'S':
|
134
|
sync_mode = 1;
|
135
|
break;
|
136
|
default:
|
137
|
usage();
|
138
|
break;
|
139
|
}
|
140
|
}
|
141
|
|
142
|
if (argc <= optind) {
|
143
|
usage();
|
144
|
} else {
|
145
|
filename = argv[optind];
|
146
|
}
|
147
|
#else
|
148
|
if(argc <6)
|
149
|
usage();
|
150
|
dev_index = atoi(argv[1]);
|
151
|
samp_rate = atoi(argv[2])*1000;
|
152
|
gain=(int)(atof(argv[3]) * 10); /* tenths of a dB */
|
153
|
frequency = atoi(argv[4]);
|
154
|
filename = argv[5];
|
155
|
#endif
|
156
|
if(out_block_size < MINIMAL_BUF_LENGTH ||
|
157
|
out_block_size > MAXIMAL_BUF_LENGTH ){
|
158
|
fprintf(stderr,
|
159
|
"Output block size wrong value, falling back to default\n");
|
160
|
fprintf(stderr,
|
161
|
"Minimal length: %u\n", MINIMAL_BUF_LENGTH);
|
162
|
fprintf(stderr,
|
163
|
"Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
|
164
|
out_block_size = DEFAULT_BUF_LENGTH;
|
165
|
}
|
166
|
|
167
|
buffer = malloc(out_block_size * sizeof(uint8_t));
|
168
|
|
169
|
device_count = osmosdr_get_device_count();
|
170
|
if (!device_count) {
|
171
|
fprintf(stderr, "No supported devices found.\n");
|
172
|
exit(1);
|
173
|
}
|
174
|
|
175
|
fprintf(stderr, "Found %d device(s):\n", device_count);
|
176
|
for (i = 0; i < device_count; i++) {
|
177
|
osmosdr_get_device_usb_strings(i, vendor, product, serial);
|
178
|
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
|
179
|
}
|
180
|
fprintf(stderr, "\n");
|
181
|
|
182
|
fprintf(stderr, "Using device %d: %s\n",
|
183
|
dev_index, osmosdr_get_device_name(dev_index));
|
184
|
|
185
|
r = osmosdr_open(&dev, dev_index);
|
186
|
if (r < 0) {
|
187
|
fprintf(stderr, "Failed to open osmosdr device #%d.\n", dev_index);
|
188
|
exit(1);
|
189
|
}
|
190
|
#ifndef _WIN32
|
191
|
sigact.sa_handler = sighandler;
|
192
|
sigemptyset(&sigact.sa_mask);
|
193
|
sigact.sa_flags = 0;
|
194
|
sigaction(SIGINT, &sigact, NULL);
|
195
|
sigaction(SIGTERM, &sigact, NULL);
|
196
|
sigaction(SIGQUIT, &sigact, NULL);
|
197
|
sigaction(SIGPIPE, &sigact, NULL);
|
198
|
#else
|
199
|
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
|
200
|
#endif
|
201
|
count = osmosdr_get_tuner_gains(dev, NULL);
|
202
|
fprintf(stderr, "Supported gain values (%d): ", count);
|
203
|
|
204
|
count = osmosdr_get_tuner_gains(dev, gains);
|
205
|
for (i = 0; i < count; i++)
|
206
|
fprintf(stderr, "%.1f ", gains[i] / 10.0);
|
207
|
fprintf(stderr, "\n");
|
208
|
|
209
|
count = osmosdr_get_sample_rates(dev, NULL);
|
210
|
fprintf(stderr, "Supported sample rates (%d): ", count);
|
211
|
|
212
|
count = osmosdr_get_sample_rates(dev, rates);
|
213
|
for (i = 0; i < count; i++)
|
214
|
fprintf(stderr, "%u ", rates[i]);
|
215
|
fprintf(stderr, "\n");
|
216
|
|
217
|
r = osmosdr_get_usb_strings(dev, vendor, product, serial);
|
218
|
if (r < 0)
|
219
|
fprintf(stderr, "WARNING: Failed to read usb strings.\n");
|
220
|
else
|
221
|
fprintf(stderr, "%s, %s: SN: %s\n", vendor, product, serial);
|
222
|
|
223
|
/* Set the sample rate */
|
224
|
r = osmosdr_set_sample_rate(dev, samp_rate);
|
225
|
if (r < 0)
|
226
|
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
|
227
|
else {
|
228
|
samp_rate = osmosdr_get_sample_rate(dev);
|
229
|
fprintf(stderr, "Sample rate is set to %u Hz.\n", samp_rate);
|
230
|
}
|
231
|
|
232
|
/* Set the frequency */
|
233
|
r = osmosdr_set_center_freq(dev, frequency);
|
234
|
if (r < 0)
|
235
|
fprintf(stderr, "WARNING: Failed to set center freq.\n");
|
236
|
else
|
237
|
fprintf(stderr, "Tuned to %u Hz.\n", frequency);
|
238
|
|
239
|
if (0 == gain) {
|
240
|
/* Enable automatic gain */
|
241
|
r = osmosdr_set_tuner_gain_mode(dev, 0);
|
242
|
if (r < 0)
|
243
|
fprintf(stderr, "WARNING: Failed to enable automatic gain.\n");
|
244
|
} else {
|
245
|
/* Enable manual gain */
|
246
|
r = osmosdr_set_tuner_gain_mode(dev, 1);
|
247
|
if (r < 0)
|
248
|
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
|
249
|
|
250
|
/* Set the tuner gain */
|
251
|
r = osmosdr_set_tuner_gain(dev, gain);
|
252
|
if (r < 0)
|
253
|
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
|
254
|
else
|
255
|
fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0);
|
256
|
}
|
257
|
|
258
|
if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
|
259
|
file = stdout;
|
260
|
} else {
|
261
|
file = fopen(filename, "wb");
|
262
|
if (!file) {
|
263
|
fprintf(stderr, "Failed to open %s\n", filename);
|
264
|
goto out;
|
265
|
}
|
266
|
}
|
267
|
|
268
|
/* Reset endpoint before we start reading from it (mandatory) */
|
269
|
r = osmosdr_reset_buffer(dev);
|
270
|
if (r < 0)
|
271
|
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
|
272
|
|
273
|
if (sync_mode) {
|
274
|
fprintf(stderr, "Reading samples in sync mode...\n");
|
275
|
while (!do_exit) {
|
276
|
r = osmosdr_read_sync(dev, buffer, out_block_size, &n_read);
|
277
|
if (r < 0) {
|
278
|
fprintf(stderr, "WARNING: sync read failed.\n");
|
279
|
break;
|
280
|
}
|
281
|
|
282
|
if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) {
|
283
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
284
|
break;
|
285
|
}
|
286
|
|
287
|
if ((uint32_t)n_read < out_block_size) {
|
288
|
fprintf(stderr, "Short read, samples lost, exiting!\n");
|
289
|
break;
|
290
|
}
|
291
|
}
|
292
|
} else {
|
293
|
fprintf(stderr, "Reading samples in async mode...\n");
|
294
|
r = osmosdr_read_async(dev, osmosdr_callback, (void *)file,
|
295
|
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
|
296
|
}
|
297
|
|
298
|
if (do_exit)
|
299
|
fprintf(stderr, "\nUser cancel, exiting...\n");
|
300
|
else
|
301
|
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
|
302
|
|
303
|
if (file != stdout)
|
304
|
fclose(file);
|
305
|
|
306
|
osmosdr_close(dev);
|
307
|
free (buffer);
|
308
|
out:
|
309
|
return r >= 0 ? r : -r;
|
310
|
}
|