diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ffaf435..77e1dc4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,10 +47,17 @@ add_library(rtlsdr_static STATIC tuner_r82xx.c ) +add_library(convenience_static STATIC + convenience/convenience.c +) + if(WIN32) add_library(libgetopt_static STATIC getopt/getopt.c ) +target_link_libraries(convenience_static + rtlsdr_shared +) endif() target_link_libraries(rtlsdr_static @@ -76,31 +83,31 @@ add_executable(rtl_adsb rtl_adsb.c) add_executable(rtl_power rtl_power.c) set(INSTALL_TARGETS rtlsdr_shared rtlsdr_static rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_power) -target_link_libraries(rtl_sdr rtlsdr_shared +target_link_libraries(rtl_sdr rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_tcp rtlsdr_shared +target_link_libraries(rtl_tcp rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_test rtlsdr_shared +target_link_libraries(rtl_test rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_fm rtlsdr_shared +target_link_libraries(rtl_fm rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_eeprom rtlsdr_shared +target_link_libraries(rtl_eeprom rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_adsb rtlsdr_shared +target_link_libraries(rtl_adsb rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) -target_link_libraries(rtl_power rtlsdr_shared +target_link_libraries(rtl_power rtlsdr_shared convenience_static ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ) diff --git a/src/Makefile.am b/src/Makefile.am index 8b27660..94aec95 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,7 @@ # Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification LIBVERSION=0:5:0 +AUTOMAKE_OPTIONS = subdir-objects INCLUDES = $(all_includes) -I$(top_srcdir)/include AM_CFLAGS = ${CFLAGS} -fPIC ${SYMBOL_VISIBILITY} @@ -12,23 +13,23 @@ librtlsdr_la_LDFLAGS = -version-info $(LIBVERSION) bin_PROGRAMS = rtl_sdr rtl_tcp rtl_test rtl_fm rtl_eeprom rtl_adsb rtl_power -rtl_sdr_SOURCES = rtl_sdr.c +rtl_sdr_SOURCES = rtl_sdr.c convenience/convenience.c rtl_sdr_LDADD = librtlsdr.la -rtl_tcp_SOURCES = rtl_tcp.c +rtl_tcp_SOURCES = rtl_tcp.c convenience/convenience.c rtl_tcp_LDADD = librtlsdr.la -rtl_test_SOURCES = rtl_test.c +rtl_test_SOURCES = rtl_test.c convenience/convenience.c rtl_test_LDADD = librtlsdr.la $(LIBM) -rtl_fm_SOURCES = rtl_fm.c +rtl_fm_SOURCES = rtl_fm.c convenience/convenience.c rtl_fm_LDADD = librtlsdr.la $(LIBM) -rtl_eeprom_SOURCES = rtl_eeprom.c +rtl_eeprom_SOURCES = rtl_eeprom.c convenience/convenience.c rtl_eeprom_LDADD = librtlsdr.la $(LIBM) -rtl_adsb_SOURCES = rtl_adsb.c +rtl_adsb_SOURCES = rtl_adsb.c convenience/convenience.c rtl_adsb_LDADD = librtlsdr.la $(LIBM) -rtl_power_SOURCES = rtl_power.c +rtl_power_SOURCES = rtl_power.c convenience/convenience.c rtl_power_LDADD = librtlsdr.la $(LIBM) diff --git a/src/convenience/convenience.c b/src/convenience/convenience.c new file mode 100644 index 0000000..517dc4e --- /dev/null +++ b/src/convenience/convenience.c @@ -0,0 +1,304 @@ +/* + * Copyright (C) 2014 by Kyle Keen + * + * 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 + * (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 . + */ + +/* a collection of user friendly tools + * todo: use strtol for more flexible int parsing + * */ + +#include +#include +#include + +#ifndef _WIN32 +#include +#else +#include +#include +#include +#define _USE_MATH_DEFINES +#endif + +#include + +#include "rtl-sdr.h" + +double atofs(char *s) +/* standard suffixes */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case 'g': + case 'G': + suff *= 1e3; + case 'm': + case 'M': + suff *= 1e3; + case 'k': + case 'K': + suff *= 1e3; + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +double atoft(char *s) +/* time suffixes, returns seconds */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case 'h': + case 'H': + suff *= 60; + case 'm': + case 'M': + suff *= 60; + case 's': + case 'S': + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +double atofp(char *s) +/* percent suffixes */ +{ + char last; + int len; + double suff = 1.0; + len = strlen(s); + last = s[len-1]; + s[len-1] = '\0'; + switch (last) { + case '%': + suff *= 0.01; + suff *= atof(s); + s[len-1] = last; + return suff; + } + s[len-1] = last; + return atof(s); +} + +int nearest_gain(rtlsdr_dev_t *dev, int target_gain) +{ + int i, r, err1, err2, count, nearest; + int* gains; + r = rtlsdr_set_tuner_gain_mode(dev, 1); + if (r < 0) { + fprintf(stderr, "WARNING: Failed to enable manual gain.\n"); + return r; + } + count = rtlsdr_get_tuner_gains(dev, NULL); + if (count <= 0) { + return 0; + } + gains = malloc(sizeof(int) * count); + count = rtlsdr_get_tuner_gains(dev, gains); + nearest = gains[0]; + for (i=0; i= 0 && device < device_count) { + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string exact match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_get_device_usb_strings(i, vendor, product, serial); + if (strcmp(s, serial) != 0) { + continue;} + device = i; + fprintf(stderr, "Using device %d: %s\n", + device, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string prefix match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_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, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + /* does string suffix match a serial */ + for (i = 0; i < device_count; i++) { + rtlsdr_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, rtlsdr_get_device_name((uint32_t)device)); + return device; + } + fprintf(stderr, "No matching devices found.\n"); + return -1; +} + +// vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab diff --git a/src/convenience/convenience.h b/src/convenience/convenience.h new file mode 100644 index 0000000..1faa2af --- /dev/null +++ b/src/convenience/convenience.h @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2014 by Kyle Keen + * + * 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 + * (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 . + */ + +/* a collection of user friendly tools */ + +/*! + * Convert standard suffixes (k, M, G) to double + * + * \param s a string to be parsed + * \return double + */ + +double atofs(char *s); + +/*! + * Convert time suffixes (s, m, h) to double + * + * \param s a string to be parsed + * \return seconds as double + */ + +double atoft(char *s); + +/*! + * Convert percent suffixe (%) to double + * + * \param s a string to be parsed + * \return double + */ + +double atofp(char *s); + +/*! + * Find nearest supported gain + * + * \param dev the device handle given by rtlsdr_open() + * \param target_gain in tenths of a dB + * \return 0 on success + */ + +int nearest_gain(rtlsdr_dev_t *dev, int target_gain); + +/*! + * Set device frequency and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param frequency in Hz + * \return 0 on success + */ + +int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency); + +/*! + * Set device sample rate and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param samp_rate in samples/second + * \return 0 on success + */ + +int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate); + +/*! + * Enable or disable the direct sampling mode and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param on 0 means disabled, 1 I-ADC input enabled, 2 Q-ADC input enabled + * \return 0 on success + */ + +int verbose_direct_sampling(rtlsdr_dev_t *dev, int on); + +/*! + * Enable offset tuning and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_offset_tuning(rtlsdr_dev_t *dev); + +/*! + * Enable auto gain and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_auto_gain(rtlsdr_dev_t *dev); + +/*! + * Set tuner gain and report status on stderr + * + * \param dev the device handle given by rtlsdr_open() + * \param gain in tenths of a dB + * \return 0 on success + */ + +int verbose_gain_set(rtlsdr_dev_t *dev, int gain); + +/*! + * Set the frequency correction value for the device and report status on stderr. + * + * \param dev the device handle given by rtlsdr_open() + * \param ppm_error correction value in parts per million (ppm) + * \return 0 on success + */ + +int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error); + +/*! + * Reset buffer + * + * \param dev the device handle given by rtlsdr_open() + * \return 0 on success + */ + +int verbose_reset_buffer(rtlsdr_dev_t *dev); + +/*! + * Find the closest matching device. + * + * \param s a string to be parsed + * \return dev_index int, -1 on error + */ + +int verbose_device_search(char *s); + diff --git a/src/rtl_adsb.c b/src/rtl_adsb.c index 097a6f4..10447cd 100644 --- a/src/rtl_adsb.c +++ b/src/rtl_adsb.c @@ -41,6 +41,7 @@ #include #include "rtl-sdr.h" +#include "convenience/convenience.h" #ifdef _WIN32 #define sleep Sleep @@ -362,10 +363,9 @@ int main(int argc, char **argv) char *filename = NULL; int n_read, r, opt; int i, gain = AUTO_GAIN; /* tenths of a dB */ - uint32_t dev_index = 0; - int device_count; + int dev_index = 0; + int dev_given = 0; int ppm_error = 0; - char vendor[256], product[256], serial[256]; pthread_mutex_init(&data_ready, NULL); squares_precompute(); @@ -373,7 +373,8 @@ int main(int argc, char **argv) { switch (opt) { case 'd': - dev_index = atoi(optarg); + dev_index = verbose_device_search(optarg); + dev_given = 1; break; case 'g': gain = (int)(atof(optarg) * 10); @@ -407,23 +408,15 @@ int main(int argc, char **argv) buffer = malloc(DEFAULT_BUF_LENGTH * 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); @@ -456,50 +449,29 @@ int main(int argc, char **argv) /* Set the tuner gain */ if (gain == AUTO_GAIN) { - r = rtlsdr_set_tuner_gain_mode(dev, 0); + verbose_auto_gain(dev); } else { - r = rtlsdr_set_tuner_gain_mode(dev, 1); - r = rtlsdr_set_tuner_gain(dev, gain); - } - if (r != 0) { - fprintf(stderr, "WARNING: Failed to set tuner gain.\n"); - } else if (gain == AUTO_GAIN) { - fprintf(stderr, "Tuner gain set to automatic.\n"); - } else { - fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0); + gain = nearest_gain(dev, gain); + verbose_gain_set(dev, gain); } - r = rtlsdr_set_freq_correction(dev, ppm_error); + verbose_ppm_set(dev, ppm_error); r = rtlsdr_set_agc_mode(dev, 1); /* Set the tuner frequency */ - r = rtlsdr_set_center_freq(dev, ADSB_FREQ); - if (r < 0) { - fprintf(stderr, "WARNING: Failed to set center freq.\n");} - else { - fprintf(stderr, "Tuned to %u Hz.\n", ADSB_FREQ);} + verbose_set_frequency(dev, ADSB_FREQ); /* Set the sample rate */ - fprintf(stderr, "Sampling at %u Hz.\n", ADSB_RATE); - r = rtlsdr_set_sample_rate(dev, ADSB_RATE); - if (r < 0) { - fprintf(stderr, "WARNING: Failed to set sample rate.\n");} + verbose_set_sample_rate(dev, ADSB_RATE); /* 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");} - - /* flush old junk */ - sleep(1); - rtlsdr_read_sync(dev, NULL, 4096, NULL); + verbose_reset_buffer(dev); pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(NULL)); rtlsdr_read_async(dev, rtlsdr_callback, (void *)(NULL), DEFAULT_ASYNC_BUF_NUMBER, DEFAULT_BUF_LENGTH); - if (do_exit) { fprintf(stderr, "\nUser cancel, exiting...\n");} else { diff --git a/src/rtl_fm.c b/src/rtl_fm.c index 5b251e5..f732bca 100644 --- a/src/rtl_fm.c +++ b/src/rtl_fm.c @@ -68,6 +68,7 @@ #include #include "rtl-sdr.h" +#include "convenience/convenience.h" #define DEFAULT_SAMPLE_RATE 24000 #define DEFAULT_ASYNC_BUF_NUMBER 32 @@ -197,6 +198,13 @@ static void sighandler(int signum) #define safe_cond_signal(n, m) pthread_mutex_lock(m); pthread_cond_signal(n); pthread_mutex_unlock(m) #define safe_cond_wait(n, m) pthread_mutex_lock(m); pthread_cond_wait(n, m); pthread_mutex_unlock(m) +#ifdef _MSC_VER +double log2(double n) +{ + return log(n) / log(2.0); +} +#endif + void rotate_90(unsigned char *buf, uint32_t len) /* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j or [0, 1, -3, 2, -4, -5, 7, -6] */ @@ -699,33 +707,6 @@ static void *demod_thread_fn(void *arg) return 0; } -double atofs(char *f) -/* standard suffixes */ -{ - char last; - int len; - double suff = 1.0; - len = strlen(f); - last = f[len-1]; - f[len-1] = '\0'; - switch (last) { - case 'g': - case 'G': - suff *= 1e3; - case 'm': - case 'M': - suff *= 1e3; - case 'k': - case 'K': - suff *= 1e3; - suff *= atof(f); - f[len-1] = last; - return suff; - } - f[len-1] = last; - return atof(f); -} - void frequency_range(struct fm_state *fm, char *arg) { char *start, *stop, *step; @@ -746,27 +727,6 @@ void frequency_range(struct fm_state *fm, char *arg) step[-1] = ':'; } -int nearest_gain(int target_gain) -{ - int i, err1, err2, count, close_gain; - int* gains; - count = rtlsdr_get_tuner_gains(dev, NULL); - if (count <= 0) { - return 0; - } - gains = malloc(sizeof(int) * count); - count = rtlsdr_get_tuner_gains(dev, gains); - close_gain = gains[0]; - for (i=0; i= FREQUENCIES_LIMIT) { @@ -938,18 +899,9 @@ int main(int argc, char **argv) ACTUAL_BUF_LENGTH = lcm_post[fm.post_downsample] * DEFAULT_BUF_LENGTH; buffer = malloc(ACTUAL_BUF_LENGTH * sizeof(uint8_t)); - device_count = rtlsdr_get_device_count(); - if (!device_count) { - fprintf(stderr, "No supported devices found.\n"); - exit(1); - } - - 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_given) { + dev_index = verbose_device_search("0"); } - fprintf(stderr, "\n"); fprintf(stderr, "Using device %d: %s\n", dev_index, rtlsdr_get_device_name(dev_index)); @@ -987,20 +939,13 @@ int main(int argc, char **argv) /* Set the tuner gain */ if (gain == AUTO_GAIN) { - r = rtlsdr_set_tuner_gain_mode(dev, 0); - } else { - r = rtlsdr_set_tuner_gain_mode(dev, 1); - gain = nearest_gain(gain); - r = rtlsdr_set_tuner_gain(dev, gain); - } - if (r != 0) { - fprintf(stderr, "WARNING: Failed to set tuner gain.\n"); - } else if (gain == AUTO_GAIN) { - fprintf(stderr, "Tuner gain set to automatic.\n"); + verbose_auto_gain(dev); } else { - fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0); + gain = nearest_gain(dev, gain); + verbose_gain_set(dev, gain); } - r = rtlsdr_set_freq_correction(dev, ppm_error); + + verbose_ppm_set(dev, ppm_error); if (strcmp(filename, "-") == 0) { /* Write samples to stdout */ fm.file = stdout; @@ -1016,9 +961,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); pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(&fm)); /*rtlsdr_read_async(dev, rtlsdr_callback, (void *)(&fm), diff --git a/src/rtl_power.c b/src/rtl_power.c index 68b6fed..e96dd2a 100644 --- a/src/rtl_power.c +++ b/src/rtl_power.c @@ -64,6 +64,7 @@ #include #include "rtl-sdr.h" +#include "convenience/convenience.h" #define MAX(x, y) (((x) > (y)) ? (x) : (y)) @@ -424,101 +425,6 @@ void rms_power(struct tuning_state *ts) ts->samples += 1; } -double atofs(char *f) -/* standard suffixes */ -{ - char last; - int len; - double suff = 1.0; - len = strlen(f); - last = f[len-1]; - f[len-1] = '\0'; - switch (last) { - case 'g': - case 'G': - suff *= 1e3; - case 'm': - case 'M': - suff *= 1e3; - case 'k': - case 'K': - suff *= 1e3; - suff *= atof(f); - f[len-1] = last; - return suff; - } - f[len-1] = last; - return atof(f); -} - -double atoft(char *f) -/* time suffixes */ -{ - char last; - int len; - double suff = 1.0; - len = strlen(f); - last = f[len-1]; - f[len-1] = '\0'; - switch (last) { - case 'h': - case 'H': - suff *= 60; - case 'm': - case 'M': - suff *= 60; - case 's': - case 'S': - suff *= atof(f); - f[len-1] = last; - return suff; - } - f[len-1] = last; - return atof(f); -} - -double atofp(char *f) -/* percent suffixes */ -{ - char last; - int len; - double suff = 1.0; - len = strlen(f); - last = f[len-1]; - f[len-1] = '\0'; - switch (last) { - case '%': - suff *= 0.01; - suff *= atof(f); - f[len-1] = last; - return suff; - } - f[len-1] = last; - return atof(f); -} - -int nearest_gain(int target_gain) -{ - int i, err1, err2, count, close_gain; - int* gains; - count = rtlsdr_get_tuner_gains(dev, NULL); - if (count <= 0) { - return 0; - } - gains = malloc(sizeof(int) * count); - count = rtlsdr_get_tuner_gains(dev, gains); - close_gain = gains[0]; - for (i=0; i #include "rtl-sdr.h" +#include "convenience/convenience.h" #ifdef _WIN32 #pragma comment(lib, "ws2_32.lib") @@ -93,7 +94,8 @@ void usage(void) "\t[-s samplerate in Hz (default: 2048000 Hz)]\n" "\t[-b number of buffers (default: 32, set by library)]\n" "\t[-n max number of linked list buffers to keep (default: 500)]\n" - "\t[-d device index (default: 0)]\n"); + "\t[-d device index (default: 0)]\n" + "\t[-P ppm_error (default: 0)]\n"); exit(1); } @@ -367,9 +369,11 @@ int main(int argc, char **argv) int port = 1234; uint32_t frequency = 100000000, samp_rate = 2048000; struct sockaddr_in local, remote; - int device_count; - uint32_t dev_index = 0, buf_num = 0; + uint32_t buf_num = 0; + int dev_index = 0; + int dev_given = 0; int gain = 0; + int ppm_error = 0; struct llist *curelem,*prev; pthread_attr_t attr; void *status; @@ -387,19 +391,20 @@ int main(int argc, char **argv) struct sigaction sigact, sigign; #endif - while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:")) != -1) { + while ((opt = getopt(argc, argv, "a:p:f:g:s:b:n:d:P:")) != -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 'a': addr = optarg; @@ -413,6 +418,9 @@ int main(int argc, char **argv) case 'n': llbuf_num = atoi(optarg); break; + case 'P': + ppm_error = atoi(optarg); + break; default: usage(); break; @@ -422,21 +430,20 @@ int main(int argc, char **argv) if (argc < optind) usage(); - 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"); } - printf("Found %d device(s).\n", device_count); + if (dev_index < 0) { + exit(1); + } - rtlsdr_open(&dev, dev_index); + rtlsdr_open(&dev, (uint32_t)dev_index); if (NULL == dev) { fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); exit(1); } - printf("Using %s\n", rtlsdr_get_device_name(dev_index)); #ifndef _WIN32 sigact.sa_handler = sighandler; sigemptyset(&sigact.sa_mask); @@ -449,6 +456,10 @@ int main(int argc, char **argv) #else SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); #endif + + /* Set the tuner error */ + verbose_ppm_set(dev, ppm_error); + /* Set the sample rate */ r = rtlsdr_set_sample_rate(dev, samp_rate); if (r < 0) diff --git a/src/rtl_test.c b/src/rtl_test.c index 8b6fc77..d7dbf81 100644 --- a/src/rtl_test.c +++ b/src/rtl_test.c @@ -37,6 +37,7 @@ #endif #include "rtl-sdr.h" +#include "convenience/convenience.h" #define DEFAULT_SAMPLE_RATE 2048000 #define DEFAULT_ASYNC_BUF_NUMBER 32 @@ -211,10 +212,10 @@ int main(int argc, char **argv) int i, tuner_benchmark = 0; int sync_mode = 0; uint8_t *buffer; - uint32_t dev_index = 0; + int dev_index = 0; + int dev_given = 0; uint32_t samp_rate = DEFAULT_SAMPLE_RATE; uint32_t out_block_size = DEFAULT_BUF_LENGTH; - int device_count; int count; int gains[100]; int real_rate; @@ -223,7 +224,8 @@ int main(int argc, char **argv) while ((opt = getopt(argc, argv, "d:s:b:tpS::")) != -1) { switch (opt) { case 'd': - dev_index = atoi(optarg); + dev_index = verbose_device_search(optarg); + dev_given = 1; break; case 's': samp_rate = (uint32_t)atof(optarg); @@ -259,22 +261,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++) - fprintf(stderr, " %d: %s\n", i, rtlsdr_get_device_name(i)); - fprintf(stderr, "\n"); - - fprintf(stderr, "Using device %d: %s\n", - dev_index, - rtlsdr_get_device_name(dev_index)); + if (dev_index < 0) { + exit(1); + } - 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); @@ -299,9 +294,7 @@ int main(int argc, char **argv) fprintf(stderr, "\n"); /* 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); if (tuner_benchmark) { if (rtlsdr_get_tuner_type(dev) == RTLSDR_TUNER_E4000) @@ -316,9 +309,7 @@ int main(int argc, char **argv) r = rtlsdr_set_testmode(dev, 1); /* 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 (ppm_benchmark && !sync_mode) { fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_benchmark);