2 * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
3 * Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "getopt/getopt.h"
36 #define DEFAULT_SAMPLE_RATE 2048000
37 #define DEFAULT_ASYNC_BUF_NUMBER 32
38 #define DEFAULT_BUF_LENGTH (16 * 16384)
39 #define MINIMAL_BUF_LENGTH 512
40 #define MAXIMAL_BUF_LENGTH (256 * 16384)
42 static int do_exit = 0;
43 static uint32_t bytes_to_read = 0;
44 static rtlsdr_dev_t *dev = NULL;
49 "rtl_sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
50 "Usage:\t -f frequency_to_tune_to [Hz]\n"
51 "\t[-s samplerate (default: 2048000 Hz)]\n"
52 "\t[-d device_index (default: 0)]\n"
53 "\t[-g gain (default: 0 for auto)]\n"
54 "\t[-b output_block_size (default: 16 * 16384)]\n"
55 "\t[-n number of samples to read (default: 0, infinite)]\n"
56 "\t[-S force sync output (default: async)]\n"
57 "\tfilename (a '-' dumps samples to stdout)\n\n");
63 sighandler(int signum)
65 if (CTRL_C_EVENT == signum) {
66 fprintf(stderr, "Signal caught, exiting!\n");
68 rtlsdr_cancel_async(dev);
74 static void sighandler(int signum)
76 fprintf(stderr, "Signal caught, exiting!\n");
78 rtlsdr_cancel_async(dev);
82 static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
88 if ((bytes_to_read > 0) && (bytes_to_read < len)) {
91 rtlsdr_cancel_async(dev);
94 if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
95 fprintf(stderr, "Short write, samples lost, exiting!\n");
96 rtlsdr_cancel_async(dev);
99 if (bytes_to_read > 0)
100 bytes_to_read -= len;
104 int main(int argc, char **argv)
107 struct sigaction sigact;
109 char *filename = NULL;
116 uint32_t dev_index = 0;
117 uint32_t frequency = 100000000;
118 uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
119 uint32_t out_block_size = DEFAULT_BUF_LENGTH;
121 char vendor[256], product[256], serial[256];
123 while ((opt = getopt(argc, argv, "d:f:g:s:b:n:S::")) != -1) {
126 dev_index = atoi(optarg);
129 frequency = (uint32_t)atof(optarg);
132 gain = (int)(atof(optarg) * 10); /* tenths of a dB */
135 samp_rate = (uint32_t)atof(optarg);
138 out_block_size = (uint32_t)atof(optarg);
141 bytes_to_read = (uint32_t)atof(optarg) * 2;
152 if (argc <= optind) {
155 filename = argv[optind];
158 if(out_block_size < MINIMAL_BUF_LENGTH ||
159 out_block_size > MAXIMAL_BUF_LENGTH ){
161 "Output block size wrong value, falling back to default\n");
163 "Minimal length: %u\n", MINIMAL_BUF_LENGTH);
165 "Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
166 out_block_size = DEFAULT_BUF_LENGTH;
169 buffer = malloc(out_block_size * sizeof(uint8_t));
171 device_count = rtlsdr_get_device_count();
173 fprintf(stderr, "No supported devices found.\n");
177 fprintf(stderr, "Found %d device(s):\n", device_count);
178 for (i = 0; i < device_count; i++) {
179 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
180 fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
182 fprintf(stderr, "\n");
184 fprintf(stderr, "Using device %d: %s\n",
185 dev_index, rtlsdr_get_device_name(dev_index));
187 r = rtlsdr_open(&dev, dev_index);
189 fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
193 sigact.sa_handler = sighandler;
194 sigemptyset(&sigact.sa_mask);
196 sigaction(SIGINT, &sigact, NULL);
197 sigaction(SIGTERM, &sigact, NULL);
198 sigaction(SIGQUIT, &sigact, NULL);
199 sigaction(SIGPIPE, &sigact, NULL);
201 SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
203 /* Set the sample rate */
204 r = rtlsdr_set_sample_rate(dev, samp_rate);
206 fprintf(stderr, "WARNING: Failed to set sample rate.\n");
208 /* Set the frequency */
209 r = rtlsdr_set_center_freq(dev, frequency);
211 fprintf(stderr, "WARNING: Failed to set center freq.\n");
213 fprintf(stderr, "Tuned to %u Hz.\n", frequency);
216 /* Enable automatic gain */
217 r = rtlsdr_set_tuner_gain_mode(dev, 0);
219 fprintf(stderr, "WARNING: Failed to enable automatic gain.\n");
221 /* Enable manual gain */
222 r = rtlsdr_set_tuner_gain_mode(dev, 1);
224 fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
226 /* Set the tuner gain */
227 r = rtlsdr_set_tuner_gain(dev, gain);
229 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
231 fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0);
234 if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
237 _setmode(_fileno(stdin), _O_BINARY);
240 file = fopen(filename, "wb");
242 fprintf(stderr, "Failed to open %s\n", filename);
247 /* Reset endpoint before we start reading from it (mandatory) */
248 r = rtlsdr_reset_buffer(dev);
250 fprintf(stderr, "WARNING: Failed to reset buffers.\n");
253 fprintf(stderr, "Reading samples in sync mode...\n");
255 r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
257 fprintf(stderr, "WARNING: sync read failed.\n");
261 if ((bytes_to_read > 0) && (bytes_to_read < (uint32_t)n_read)) {
262 n_read = bytes_to_read;
266 if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) {
267 fprintf(stderr, "Short write, samples lost, exiting!\n");
271 if ((uint32_t)n_read < out_block_size) {
272 fprintf(stderr, "Short read, samples lost, exiting!\n");
276 if (bytes_to_read > 0)
277 bytes_to_read -= n_read;
280 fprintf(stderr, "Reading samples in async mode...\n");
281 r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
282 DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
286 fprintf(stderr, "\nUser cancel, exiting...\n");
288 fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
296 return r >= 0 ? r : -r;