2012-03-12 00:30:54 +01:00
|
|
|
/*
|
2012-04-03 19:50:03 +02:00
|
|
|
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
|
2012-03-12 00:30:54 +01:00
|
|
|
* Copyright (C) 2012 by Steve Markgraf <steve@steve-m.de>
|
|
|
|
*
|
|
|
|
* 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 2 of the License, or
|
2012-04-03 19:40:05 +02:00
|
|
|
* (at your option) any later version.
|
2012-03-12 00:30:54 +01:00
|
|
|
*
|
|
|
|
* 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 <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-04-10 01:23:48 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-03-12 00:30:54 +01:00
|
|
|
#include <unistd.h>
|
2012-04-10 01:23:48 +02:00
|
|
|
#else
|
|
|
|
#include <Windows.h>
|
2012-10-26 23:26:53 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-29 01:47:44 +02:00
|
|
|
#include "getopt/getopt.h"
|
2012-04-10 01:23:48 +02:00
|
|
|
#endif
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-04-08 23:02:42 +02:00
|
|
|
#include "rtl-sdr.h"
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-04-09 14:46:22 +02:00
|
|
|
#define DEFAULT_SAMPLE_RATE 2048000
|
|
|
|
#define DEFAULT_ASYNC_BUF_NUMBER 32
|
|
|
|
#define DEFAULT_BUF_LENGTH (16 * 16384)
|
2012-04-09 15:09:01 +02:00
|
|
|
#define MINIMAL_BUF_LENGTH 512
|
2012-04-09 14:46:22 +02:00
|
|
|
#define MAXIMAL_BUF_LENGTH (256 * 16384)
|
2012-03-13 23:18:25 +01:00
|
|
|
|
2012-03-12 00:30:54 +01:00
|
|
|
static int do_exit = 0;
|
2012-10-20 19:43:13 +02:00
|
|
|
static uint32_t bytes_to_read = 0;
|
2012-04-02 23:09:14 +02:00
|
|
|
static rtlsdr_dev_t *dev = NULL;
|
2012-03-12 00:30:54 +01:00
|
|
|
|
|
|
|
void usage(void)
|
|
|
|
{
|
2012-04-08 23:02:42 +02:00
|
|
|
fprintf(stderr,
|
2012-05-25 20:45:47 +02:00
|
|
|
"rtl_sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n"
|
2012-04-09 14:46:22 +02:00
|
|
|
"Usage:\t -f frequency_to_tune_to [Hz]\n"
|
2012-03-13 23:18:25 +01:00
|
|
|
"\t[-s samplerate (default: 2048000 Hz)]\n"
|
2012-04-09 14:46:22 +02:00
|
|
|
"\t[-d device_index (default: 0)]\n"
|
2012-05-25 20:45:47 +02:00
|
|
|
"\t[-g gain (default: 0 for auto)]\n"
|
2012-04-09 14:46:22 +02:00
|
|
|
"\t[-b output_block_size (default: 16 * 16384)]\n"
|
2012-10-20 19:43:13 +02:00
|
|
|
"\t[-n number of samples to read (default: 0, infinite)]\n"
|
2012-04-09 15:05:09 +02:00
|
|
|
"\t[-S force sync output (default: async)]\n"
|
2012-05-06 00:34:13 +02:00
|
|
|
"\tfilename (a '-' dumps samples to stdout)\n\n");
|
2012-03-12 00:30:54 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-04-10 01:23:48 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
BOOL WINAPI
|
2012-04-10 15:12:32 +02:00
|
|
|
sighandler(int signum)
|
|
|
|
{
|
|
|
|
if (CTRL_C_EVENT == signum) {
|
|
|
|
fprintf(stderr, "Signal caught, exiting!\n");
|
|
|
|
do_exit = 1;
|
|
|
|
rtlsdr_cancel_async(dev);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-04-10 01:23:48 +02:00
|
|
|
#else
|
2012-04-10 15:12:32 +02:00
|
|
|
static void sighandler(int signum)
|
2012-03-12 00:30:54 +01:00
|
|
|
{
|
2012-04-10 01:23:48 +02:00
|
|
|
fprintf(stderr, "Signal caught, exiting!\n");
|
2012-03-12 00:30:54 +01:00
|
|
|
do_exit = 1;
|
2012-04-02 23:09:14 +02:00
|
|
|
rtlsdr_cancel_async(dev);
|
|
|
|
}
|
2012-04-10 15:12:32 +02:00
|
|
|
#endif
|
2012-04-02 23:09:14 +02:00
|
|
|
|
2012-04-08 23:02:42 +02:00
|
|
|
static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
|
2012-04-02 23:09:14 +02:00
|
|
|
{
|
2012-04-09 18:14:39 +02:00
|
|
|
if (ctx) {
|
2012-10-20 19:43:13 +02:00
|
|
|
if (do_exit)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((bytes_to_read > 0) && (bytes_to_read < len)) {
|
|
|
|
len = bytes_to_read;
|
|
|
|
do_exit = 1;
|
|
|
|
rtlsdr_cancel_async(dev);
|
|
|
|
}
|
|
|
|
|
2012-04-09 18:14:39 +02:00
|
|
|
if (fwrite(buf, 1, len, (FILE*)ctx) != len) {
|
|
|
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
|
|
|
rtlsdr_cancel_async(dev);
|
|
|
|
}
|
2012-10-20 19:43:13 +02:00
|
|
|
|
|
|
|
if (bytes_to_read > 0)
|
|
|
|
bytes_to_read -= len;
|
2012-04-09 18:14:39 +02:00
|
|
|
}
|
2012-03-12 00:30:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-04-10 01:23:48 +02:00
|
|
|
#ifndef _WIN32
|
2012-03-12 00:30:54 +01:00
|
|
|
struct sigaction sigact;
|
2012-04-10 01:23:48 +02:00
|
|
|
#endif
|
2012-03-27 20:53:49 +02:00
|
|
|
char *filename = NULL;
|
2012-04-02 23:09:14 +02:00
|
|
|
int n_read;
|
2012-04-09 14:46:22 +02:00
|
|
|
int r, opt;
|
2012-05-25 20:45:47 +02:00
|
|
|
int i, gain = 0;
|
2012-04-09 15:05:09 +02:00
|
|
|
int sync_mode = 0;
|
2012-03-12 00:30:54 +01:00
|
|
|
FILE *file;
|
2012-04-09 14:46:22 +02:00
|
|
|
uint8_t *buffer;
|
2012-04-08 23:02:42 +02:00
|
|
|
uint32_t dev_index = 0;
|
2012-05-07 22:59:34 +02:00
|
|
|
uint32_t frequency = 100000000;
|
2012-04-09 14:46:22 +02:00
|
|
|
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
|
|
|
|
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
|
2012-04-10 01:23:48 +02:00
|
|
|
int device_count;
|
2012-05-20 16:41:15 +02:00
|
|
|
char vendor[256], product[256], serial[256];
|
2012-05-29 01:47:44 +02:00
|
|
|
|
2012-10-20 19:43:13 +02:00
|
|
|
while ((opt = getopt(argc, argv, "d:f:g:s:b:n:S::")) != -1) {
|
2012-03-12 00:30:54 +01:00
|
|
|
switch (opt) {
|
2012-03-31 16:20:38 +02:00
|
|
|
case 'd':
|
|
|
|
dev_index = atoi(optarg);
|
|
|
|
break;
|
2012-03-12 00:30:54 +01:00
|
|
|
case 'f':
|
2012-04-07 01:02:08 +02:00
|
|
|
frequency = (uint32_t)atof(optarg);
|
2012-03-12 00:30:54 +01:00
|
|
|
break;
|
2012-04-01 01:36:49 +02:00
|
|
|
case 'g':
|
2012-05-25 20:45:47 +02:00
|
|
|
gain = (int)(atof(optarg) * 10); /* tenths of a dB */
|
2012-04-01 01:36:49 +02:00
|
|
|
break;
|
2012-03-13 23:18:25 +01:00
|
|
|
case 's':
|
2012-05-06 00:34:13 +02:00
|
|
|
samp_rate = (uint32_t)atof(optarg);
|
2012-03-13 23:18:25 +01:00
|
|
|
break;
|
2012-04-09 14:46:22 +02:00
|
|
|
case 'b':
|
|
|
|
out_block_size = (uint32_t)atof(optarg);
|
|
|
|
break;
|
2012-10-20 19:43:13 +02:00
|
|
|
case 'n':
|
|
|
|
bytes_to_read = (uint32_t)atof(optarg) * 2;
|
|
|
|
break;
|
2012-04-09 15:05:09 +02:00
|
|
|
case 'S':
|
|
|
|
sync_mode = 1;
|
|
|
|
break;
|
2012-03-12 00:30:54 +01:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc <= optind) {
|
|
|
|
usage();
|
|
|
|
} else {
|
|
|
|
filename = argv[optind];
|
|
|
|
}
|
2012-05-29 01:47:44 +02:00
|
|
|
|
2012-04-09 14:46:22 +02:00
|
|
|
if(out_block_size < MINIMAL_BUF_LENGTH ||
|
|
|
|
out_block_size > MAXIMAL_BUF_LENGTH ){
|
|
|
|
fprintf(stderr,
|
|
|
|
"Output block size wrong value, falling back to default\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
"Minimal length: %u\n", MINIMAL_BUF_LENGTH);
|
|
|
|
fprintf(stderr,
|
|
|
|
"Maximal length: %u\n", MAXIMAL_BUF_LENGTH);
|
|
|
|
out_block_size = DEFAULT_BUF_LENGTH;
|
|
|
|
}
|
2012-04-19 15:43:05 +02:00
|
|
|
|
2012-04-09 14:46:22 +02:00
|
|
|
buffer = malloc(out_block_size * sizeof(uint8_t));
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-04-10 01:23:48 +02:00
|
|
|
device_count = rtlsdr_get_device_count();
|
2012-03-31 14:32:18 +02:00
|
|
|
if (!device_count) {
|
|
|
|
fprintf(stderr, "No supported devices found.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-04-08 23:02:42 +02:00
|
|
|
fprintf(stderr, "Found %d device(s):\n", device_count);
|
2012-05-20 16:41:15 +02:00
|
|
|
for (i = 0; i < device_count; i++) {
|
|
|
|
rtlsdr_get_device_usb_strings(i, vendor, product, serial);
|
|
|
|
fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
|
|
|
|
}
|
2012-04-08 23:02:42 +02:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
|
|
|
fprintf(stderr, "Using device %d: %s\n",
|
2012-05-20 16:41:15 +02:00
|
|
|
dev_index, rtlsdr_get_device_name(dev_index));
|
2012-03-27 19:49:44 +02:00
|
|
|
|
2012-04-03 00:47:52 +02:00
|
|
|
r = rtlsdr_open(&dev, dev_index);
|
|
|
|
if (r < 0) {
|
|
|
|
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
|
2012-03-27 19:49:44 +02:00
|
|
|
exit(1);
|
2012-03-12 00:30:54 +01:00
|
|
|
}
|
2012-04-10 01:23:48 +02:00
|
|
|
#ifndef _WIN32
|
2012-03-12 00:30:54 +01:00
|
|
|
sigact.sa_handler = sighandler;
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = 0;
|
|
|
|
sigaction(SIGINT, &sigact, NULL);
|
|
|
|
sigaction(SIGTERM, &sigact, NULL);
|
|
|
|
sigaction(SIGQUIT, &sigact, NULL);
|
2012-04-09 18:14:39 +02:00
|
|
|
sigaction(SIGPIPE, &sigact, NULL);
|
2012-04-10 01:23:48 +02:00
|
|
|
#else
|
|
|
|
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
|
|
|
|
#endif
|
2012-03-27 19:49:44 +02:00
|
|
|
/* Set the sample rate */
|
2012-03-31 14:32:18 +02:00
|
|
|
r = rtlsdr_set_sample_rate(dev, samp_rate);
|
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-03-27 19:49:44 +02:00
|
|
|
/* Set the frequency */
|
|
|
|
r = rtlsdr_set_center_freq(dev, frequency);
|
2012-03-31 14:32:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to set center freq.\n");
|
2012-04-01 01:15:05 +02:00
|
|
|
else
|
2012-04-07 01:02:08 +02:00
|
|
|
fprintf(stderr, "Tuned to %u Hz.\n", frequency);
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-05-25 20:45:47 +02:00
|
|
|
if (0 == gain) {
|
|
|
|
/* Enable automatic gain */
|
|
|
|
r = rtlsdr_set_tuner_gain_mode(dev, 0);
|
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to enable automatic gain.\n");
|
|
|
|
} else {
|
|
|
|
/* Enable manual gain */
|
|
|
|
r = rtlsdr_set_tuner_gain_mode(dev, 1);
|
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
|
|
|
|
|
|
|
|
/* Set the tuner gain */
|
|
|
|
r = rtlsdr_set_tuner_gain(dev, gain);
|
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Tuner gain set to %f dB.\n", gain/10.0);
|
|
|
|
}
|
2012-04-01 01:36:49 +02:00
|
|
|
|
2012-04-09 18:14:39 +02:00
|
|
|
if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
|
|
|
|
file = stdout;
|
2012-10-26 23:26:53 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
#endif
|
2012-04-09 18:14:39 +02:00
|
|
|
} else {
|
2012-04-09 15:09:01 +02:00
|
|
|
file = fopen(filename, "wb");
|
|
|
|
if (!file) {
|
|
|
|
fprintf(stderr, "Failed to open %s\n", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-03-12 00:30:54 +01:00
|
|
|
}
|
|
|
|
|
2012-03-31 16:20:38 +02:00
|
|
|
/* Reset endpoint before we start reading from it (mandatory) */
|
2012-03-27 19:49:44 +02:00
|
|
|
r = rtlsdr_reset_buffer(dev);
|
2012-03-31 14:32:18 +02:00
|
|
|
if (r < 0)
|
|
|
|
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
|
|
|
|
|
2012-04-09 15:05:09 +02:00
|
|
|
if (sync_mode) {
|
2012-04-09 15:09:01 +02:00
|
|
|
fprintf(stderr, "Reading samples in sync mode...\n");
|
2012-04-09 15:05:09 +02:00
|
|
|
while (!do_exit) {
|
|
|
|
r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read);
|
2012-04-09 18:14:39 +02:00
|
|
|
if (r < 0) {
|
2012-04-09 15:05:09 +02:00
|
|
|
fprintf(stderr, "WARNING: sync read failed.\n");
|
2012-04-09 18:14:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-03-12 00:30:54 +01:00
|
|
|
|
2012-10-20 19:43:13 +02:00
|
|
|
if ((bytes_to_read > 0) && (bytes_to_read < (uint32_t)n_read)) {
|
|
|
|
n_read = bytes_to_read;
|
|
|
|
do_exit = 1;
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:32:51 +02:00
|
|
|
if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) {
|
2012-04-09 18:14:39 +02:00
|
|
|
fprintf(stderr, "Short write, samples lost, exiting!\n");
|
|
|
|
break;
|
|
|
|
}
|
2012-04-09 15:05:09 +02:00
|
|
|
|
2012-04-25 22:32:51 +02:00
|
|
|
if ((uint32_t)n_read < out_block_size) {
|
2012-04-09 15:05:09 +02:00
|
|
|
fprintf(stderr, "Short read, samples lost, exiting!\n");
|
|
|
|
break;
|
|
|
|
}
|
2012-10-20 19:43:13 +02:00
|
|
|
|
|
|
|
if (bytes_to_read > 0)
|
|
|
|
bytes_to_read -= n_read;
|
2012-03-12 00:30:54 +01:00
|
|
|
}
|
2012-04-09 15:05:09 +02:00
|
|
|
} else {
|
2012-04-09 18:14:39 +02:00
|
|
|
fprintf(stderr, "Reading samples in async mode...\n");
|
|
|
|
r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file,
|
|
|
|
DEFAULT_ASYNC_BUF_NUMBER, out_block_size);
|
2012-03-12 00:30:54 +01:00
|
|
|
}
|
2012-04-09 15:05:09 +02:00
|
|
|
|
2012-04-01 13:06:22 +02:00
|
|
|
if (do_exit)
|
2012-04-08 23:02:42 +02:00
|
|
|
fprintf(stderr, "\nUser cancel, exiting...\n");
|
2012-04-09 00:27:15 +02:00
|
|
|
else
|
2012-04-09 18:14:39 +02:00
|
|
|
fprintf(stderr, "\nLibrary error %d, exiting...\n", r);
|
2012-04-01 13:06:22 +02:00
|
|
|
|
2012-04-09 15:09:01 +02:00
|
|
|
if (file != stdout)
|
|
|
|
fclose(file);
|
|
|
|
|
2012-03-31 16:20:38 +02:00
|
|
|
rtlsdr_close(dev);
|
2012-04-09 14:46:22 +02:00
|
|
|
free (buffer);
|
2012-03-12 00:30:54 +01:00
|
|
|
out:
|
|
|
|
return r >= 0 ? r : -r;
|
|
|
|
}
|