aboutsummaryrefslogtreecommitdiffstats
path: root/src/rtl_sdr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rtl_sdr.c')
-rw-r--r--src/rtl_sdr.c70
1 files changed, 26 insertions, 44 deletions
diff --git a/src/rtl_sdr.c b/src/rtl_sdr.c
index 730aa9e..dbb5b16 100644
--- a/src/rtl_sdr.c
+++ b/src/rtl_sdr.c
@@ -32,6 +32,7 @@
#endif
#include "rtl-sdr.h"
+#include "convenience/convenience.h"
#define DEFAULT_SAMPLE_RATE 2048000
#define DEFAULT_ASYNC_BUF_NUMBER 32
@@ -51,6 +52,7 @@ void usage(void)
"\t[-s samplerate (default: 2048000 Hz)]\n"
"\t[-d device_index (default: 0)]\n"
"\t[-g gain (default: 0 for auto)]\n"
+ "\t[-p ppm_error (default: 0)]\n"
"\t[-b output_block_size (default: 16 * 16384)]\n"
"\t[-n number of samples to read (default: 0, infinite)]\n"
"\t[-S force sync output (default: async)]\n"
@@ -110,29 +112,33 @@ int main(int argc, char **argv)
int n_read;
int r, opt;
int i, gain = 0;
+ int ppm_error = 0;
int sync_mode = 0;
FILE *file;
uint8_t *buffer;
- uint32_t dev_index = 0;
+ int dev_index = 0;
+ int dev_given = 0;
uint32_t frequency = 100000000;
uint32_t samp_rate = DEFAULT_SAMPLE_RATE;
uint32_t out_block_size = DEFAULT_BUF_LENGTH;
- int device_count;
- char vendor[256], product[256], serial[256];
- while ((opt = getopt(argc, argv, "d:f:g:s:b:n:S::")) != -1) {
+ while ((opt = getopt(argc, argv, "d:f:g:s:b:n:p:S::")) != -1) {
switch (opt) {
case 'd':
- dev_index = atoi(optarg);
+ dev_index = verbose_device_search(optarg);
+ dev_given = 1;
break;
case 'f':
- frequency = (uint32_t)atof(optarg);
+ frequency = (uint32_t)atofs(optarg);
break;
case 'g':
gain = (int)(atof(optarg) * 10); /* tenths of a dB */
break;
case 's':
- samp_rate = (uint32_t)atof(optarg);
+ samp_rate = (uint32_t)atofs(optarg);
+ break;
+ case 'p':
+ ppm_error = atoi(optarg);
break;
case 'b':
out_block_size = (uint32_t)atof(optarg);
@@ -168,23 +174,15 @@ int main(int argc, char **argv)
buffer = malloc(out_block_size * sizeof(uint8_t));
- device_count = rtlsdr_get_device_count();
- if (!device_count) {
- fprintf(stderr, "No supported devices found.\n");
- exit(1);
+ if (!dev_given) {
+ dev_index = verbose_device_search("0");
}
- fprintf(stderr, "Found %d device(s):\n", device_count);
- 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);
+ if (dev_index < 0) {
+ exit(1);
}
- fprintf(stderr, "\n");
- fprintf(stderr, "Using device %d: %s\n",
- dev_index, rtlsdr_get_device_name(dev_index));
-
- r = rtlsdr_open(&dev, dev_index);
+ r = rtlsdr_open(&dev, (uint32_t)dev_index);
if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1);
@@ -201,36 +199,22 @@ int main(int argc, char **argv)
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif
/* Set the sample rate */
- r = rtlsdr_set_sample_rate(dev, samp_rate);
- if (r < 0)
- fprintf(stderr, "WARNING: Failed to set sample rate.\n");
+ verbose_set_sample_rate(dev, samp_rate);
/* Set the frequency */
- r = rtlsdr_set_center_freq(dev, frequency);
- if (r < 0)
- fprintf(stderr, "WARNING: Failed to set center freq.\n");
- else
- fprintf(stderr, "Tuned to %u Hz.\n", frequency);
+ verbose_set_frequency(dev, frequency);
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");
+ verbose_auto_gain(dev);
} 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);
+ gain = nearest_gain(dev, gain);
+ verbose_gain_set(dev, gain);
}
+ verbose_ppm_set(dev, ppm_error);
+
if(strcmp(filename, "-") == 0) { /* Write samples to stdout */
file = stdout;
#ifdef _WIN32
@@ -245,9 +229,7 @@ int main(int argc, char **argv)
}
/* Reset endpoint before we start reading from it (mandatory) */
- r = rtlsdr_reset_buffer(dev);
- if (r < 0)
- fprintf(stderr, "WARNING: Failed to reset buffers.\n");
+ verbose_reset_buffer(dev);
if (sync_mode) {
fprintf(stderr, "Reading samples in sync mode...\n");