utils: convenience functions

Signed-off-by: Steve Markgraf <steve@steve-m.de>
This commit is contained in:
Kyle Keen 2014-01-21 00:49:05 -05:00 committed by Steve Markgraf
parent 003bd51167
commit 27c0929939
10 changed files with 590 additions and 358 deletions

View File

@ -47,10 +47,17 @@ add_library(rtlsdr_static STATIC
tuner_r82xx.c tuner_r82xx.c
) )
add_library(convenience_static STATIC
convenience/convenience.c
)
if(WIN32) if(WIN32)
add_library(libgetopt_static STATIC add_library(libgetopt_static STATIC
getopt/getopt.c getopt/getopt.c
) )
target_link_libraries(convenience_static
rtlsdr_shared
)
endif() endif()
target_link_libraries(rtlsdr_static target_link_libraries(rtlsdr_static
@ -76,31 +83,31 @@ add_executable(rtl_adsb rtl_adsb.c)
add_executable(rtl_power rtl_power.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) 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} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_tcp rtlsdr_shared target_link_libraries(rtl_tcp rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_test rtlsdr_shared target_link_libraries(rtl_test rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_fm rtlsdr_shared target_link_libraries(rtl_fm rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_eeprom rtlsdr_shared target_link_libraries(rtl_eeprom rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_adsb rtlsdr_shared target_link_libraries(rtl_adsb rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )
target_link_libraries(rtl_power rtlsdr_shared target_link_libraries(rtl_power rtlsdr_shared convenience_static
${LIBUSB_LIBRARIES} ${LIBUSB_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
) )

View File

@ -2,6 +2,7 @@
# Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification # Please read Chapter 6 "Library interface versions" of the libtool documentation before making any modification
LIBVERSION=0:5:0 LIBVERSION=0:5:0
AUTOMAKE_OPTIONS = subdir-objects
INCLUDES = $(all_includes) -I$(top_srcdir)/include INCLUDES = $(all_includes) -I$(top_srcdir)/include
AM_CFLAGS = ${CFLAGS} -fPIC ${SYMBOL_VISIBILITY} 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 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_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_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_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_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_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_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) rtl_power_LDADD = librtlsdr.la $(LIBM)

View File

@ -0,0 +1,304 @@
/*
* Copyright (C) 2014 by Kyle Keen <keenerd@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/* a collection of user friendly tools
* todo: use strtol for more flexible int parsing
* */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#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<count; i++) {
err1 = abs(target_gain - nearest);
err2 = abs(target_gain - gains[i]);
if (err2 < err1) {
nearest = gains[i];
}
}
free(gains);
return nearest;
}
int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency)
{
int r;
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);
}
return r;
}
int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
{
int r;
r = rtlsdr_set_sample_rate(dev, samp_rate);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
} else {
fprintf(stderr, "Sampling at %u S/s.\n", samp_rate);
}
return r;
}
int verbose_direct_sampling(rtlsdr_dev_t *dev, int on)
{
int r;
r = rtlsdr_set_direct_sampling(dev, on);
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set direct sampling mode.\n");
return r;
}
if (on == 0) {
fprintf(stderr, "Direct sampling mode disabled.\n");}
if (on == 1) {
fprintf(stderr, "Enabled direct sampling mode, input 1/I.\n");}
if (on == 2) {
fprintf(stderr, "Enabled direct sampling mode, input 2/Q.\n");}
return r;
}
int verbose_offset_tuning(rtlsdr_dev_t *dev)
{
int r;
r = rtlsdr_set_offset_tuning(dev, 1);
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
} else {
fprintf(stderr, "Offset tuning mode enabled.\n");
}
return r;
}
int verbose_auto_gain(rtlsdr_dev_t *dev)
{
int r;
r = rtlsdr_set_tuner_gain_mode(dev, 0);
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
} else {
fprintf(stderr, "Tuner gain set to automatic.\n");
}
return r;
}
int verbose_gain_set(rtlsdr_dev_t *dev, int gain)
{
int r;
r = rtlsdr_set_tuner_gain_mode(dev, 1);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
return r;
}
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 %0.2f dB.\n", gain/10.0);
}
return r;
}
int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error)
{
int r;
if (ppm_error == 0) {
return 0;}
r = rtlsdr_set_freq_correction(dev, ppm_error);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set ppm error.\n");
} else {
fprintf(stderr, "Tuner error set to %i ppm.\n", ppm_error);
}
return r;
}
int verbose_reset_buffer(rtlsdr_dev_t *dev)
{
int r;
r = rtlsdr_reset_buffer(dev);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
return r;
}
int verbose_device_search(char *s)
{
int i, device_count, device, offset;
char *s2;
char vendor[256], product[256], serial[256];
device_count = rtlsdr_get_device_count();
if (!device_count) {
fprintf(stderr, "No supported devices found.\n");
return -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);
}
fprintf(stderr, "\n");
/* does string look like raw id number */
device = (int)strtol(s, &s2, 0);
if (s2[0] == '\0' && device >= 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

View File

@ -0,0 +1,142 @@
/*
* Copyright (C) 2014 by Kyle Keen <keenerd@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/* 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);

View File

@ -41,6 +41,7 @@
#include <libusb.h> #include <libusb.h>
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "convenience/convenience.h"
#ifdef _WIN32 #ifdef _WIN32
#define sleep Sleep #define sleep Sleep
@ -362,10 +363,9 @@ int main(int argc, char **argv)
char *filename = NULL; char *filename = NULL;
int n_read, r, opt; int n_read, r, opt;
int i, gain = AUTO_GAIN; /* tenths of a dB */ int i, gain = AUTO_GAIN; /* tenths of a dB */
uint32_t dev_index = 0; int dev_index = 0;
int device_count; int dev_given = 0;
int ppm_error = 0; int ppm_error = 0;
char vendor[256], product[256], serial[256];
pthread_mutex_init(&data_ready, NULL); pthread_mutex_init(&data_ready, NULL);
squares_precompute(); squares_precompute();
@ -373,7 +373,8 @@ int main(int argc, char **argv)
{ {
switch (opt) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = verbose_device_search(optarg);
dev_given = 1;
break; break;
case 'g': case 'g':
gain = (int)(atof(optarg) * 10); gain = (int)(atof(optarg) * 10);
@ -407,23 +408,15 @@ int main(int argc, char **argv)
buffer = malloc(DEFAULT_BUF_LENGTH * sizeof(uint8_t)); buffer = malloc(DEFAULT_BUF_LENGTH * sizeof(uint8_t));
device_count = rtlsdr_get_device_count(); if (!dev_given) {
if (!device_count) { dev_index = verbose_device_search("0");
fprintf(stderr, "No supported devices found.\n"); }
if (dev_index < 0) {
exit(1); exit(1);
} }
fprintf(stderr, "Found %d device(s):\n", device_count); r = rtlsdr_open(&dev, (uint32_t)dev_index);
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);
}
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n",
dev_index, rtlsdr_get_device_name(dev_index));
r = rtlsdr_open(&dev, dev_index);
if (r < 0) { if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1); exit(1);
@ -456,50 +449,29 @@ int main(int argc, char **argv)
/* Set the tuner gain */ /* Set the tuner gain */
if (gain == AUTO_GAIN) { if (gain == AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(dev, 0); verbose_auto_gain(dev);
} else { } else {
r = rtlsdr_set_tuner_gain_mode(dev, 1); gain = nearest_gain(dev, gain);
r = rtlsdr_set_tuner_gain(dev, gain); verbose_gain_set(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);
} }
r = rtlsdr_set_freq_correction(dev, ppm_error); verbose_ppm_set(dev, ppm_error);
r = rtlsdr_set_agc_mode(dev, 1); r = rtlsdr_set_agc_mode(dev, 1);
/* Set the tuner frequency */ /* Set the tuner frequency */
r = rtlsdr_set_center_freq(dev, ADSB_FREQ); verbose_set_frequency(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);}
/* Set the sample rate */ /* Set the sample rate */
fprintf(stderr, "Sampling at %u Hz.\n", ADSB_RATE); verbose_set_sample_rate(dev, ADSB_RATE);
r = rtlsdr_set_sample_rate(dev, ADSB_RATE);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to set sample rate.\n");}
/* Reset endpoint before we start reading from it (mandatory) */ /* Reset endpoint before we start reading from it (mandatory) */
r = rtlsdr_reset_buffer(dev); verbose_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);
pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(NULL)); pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(NULL));
rtlsdr_read_async(dev, rtlsdr_callback, (void *)(NULL), rtlsdr_read_async(dev, rtlsdr_callback, (void *)(NULL),
DEFAULT_ASYNC_BUF_NUMBER, DEFAULT_ASYNC_BUF_NUMBER,
DEFAULT_BUF_LENGTH); DEFAULT_BUF_LENGTH);
if (do_exit) { if (do_exit) {
fprintf(stderr, "\nUser cancel, exiting...\n");} fprintf(stderr, "\nUser cancel, exiting...\n");}
else { else {

View File

@ -68,6 +68,7 @@
#include <libusb.h> #include <libusb.h>
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "convenience/convenience.h"
#define DEFAULT_SAMPLE_RATE 24000 #define DEFAULT_SAMPLE_RATE 24000
#define DEFAULT_ASYNC_BUF_NUMBER 32 #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_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) #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) void rotate_90(unsigned char *buf, uint32_t len)
/* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j /* 90 rotation is 1+0j, 0+1j, -1+0j, 0-1j
or [0, 1, -3, 2, -4, -5, 7, -6] */ or [0, 1, -3, 2, -4, -5, 7, -6] */
@ -699,33 +707,6 @@ static void *demod_thread_fn(void *arg)
return 0; 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) void frequency_range(struct fm_state *fm, char *arg)
{ {
char *start, *stop, *step; char *start, *stop, *step;
@ -746,27 +727,6 @@ void frequency_range(struct fm_state *fm, char *arg)
step[-1] = ':'; 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<count; i++) {
err1 = abs(target_gain - close_gain);
err2 = abs(target_gain - gains[i]);
if (err2 < err1) {
close_gain = gains[i];
}
}
free(gains);
return close_gain;
}
void fm_init(struct fm_state *fm) void fm_init(struct fm_state *fm)
{ {
@ -803,8 +763,8 @@ int main(int argc, char **argv)
int n_read, r, opt, wb_mode = 0; int n_read, r, opt, wb_mode = 0;
int i, gain = AUTO_GAIN; // tenths of a dB int i, gain = AUTO_GAIN; // tenths of a dB
uint8_t *buffer; uint8_t *buffer;
uint32_t dev_index = 0; int32_t dev_index = 0;
int device_count; int dev_given = 0;
int ppm_error = 0; int ppm_error = 0;
char vendor[256], product[256], serial[256]; char vendor[256], product[256], serial[256];
fm_init(&fm); fm_init(&fm);
@ -815,7 +775,8 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "d:f:g:s:b:l:o:t:r:p:EFA:NWMULRDCh")) != -1) { while ((opt = getopt(argc, argv, "d:f:g:s:b:l:o:t:r:p:EFA:NWMULRDCh")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = verbose_device_search(optarg);
dev_given = 1;
break; break;
case 'f': case 'f':
if (fm.freq_len >= FREQUENCIES_LIMIT) { if (fm.freq_len >= FREQUENCIES_LIMIT) {
@ -938,19 +899,10 @@ int main(int argc, char **argv)
ACTUAL_BUF_LENGTH = lcm_post[fm.post_downsample] * DEFAULT_BUF_LENGTH; ACTUAL_BUF_LENGTH = lcm_post[fm.post_downsample] * DEFAULT_BUF_LENGTH;
buffer = malloc(ACTUAL_BUF_LENGTH * sizeof(uint8_t)); buffer = malloc(ACTUAL_BUF_LENGTH * sizeof(uint8_t));
device_count = rtlsdr_get_device_count(); if (!dev_given) {
if (!device_count) { dev_index = verbose_device_search("0");
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);
}
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n", fprintf(stderr, "Using device %d: %s\n",
dev_index, rtlsdr_get_device_name(dev_index)); dev_index, rtlsdr_get_device_name(dev_index));
@ -987,20 +939,13 @@ int main(int argc, char **argv)
/* Set the tuner gain */ /* Set the tuner gain */
if (gain == AUTO_GAIN) { if (gain == AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(dev, 0); verbose_auto_gain(dev);
} else { } else {
r = rtlsdr_set_tuner_gain_mode(dev, 1); gain = nearest_gain(dev, gain);
gain = nearest_gain(gain); verbose_gain_set(dev, gain);
r = rtlsdr_set_tuner_gain(dev, gain);
} }
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set tuner gain.\n"); verbose_ppm_set(dev, ppm_error);
} 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);
}
r = rtlsdr_set_freq_correction(dev, ppm_error);
if (strcmp(filename, "-") == 0) { /* Write samples to stdout */ if (strcmp(filename, "-") == 0) { /* Write samples to stdout */
fm.file = stdout; fm.file = stdout;
@ -1016,9 +961,7 @@ int main(int argc, char **argv)
} }
/* Reset endpoint before we start reading from it (mandatory) */ /* Reset endpoint before we start reading from it (mandatory) */
r = rtlsdr_reset_buffer(dev); verbose_reset_buffer(dev);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(&fm)); pthread_create(&demod_thread, NULL, demod_thread_fn, (void *)(&fm));
/*rtlsdr_read_async(dev, rtlsdr_callback, (void *)(&fm), /*rtlsdr_read_async(dev, rtlsdr_callback, (void *)(&fm),

View File

@ -64,6 +64,7 @@
#include <libusb.h> #include <libusb.h>
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "convenience/convenience.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y))
@ -424,101 +425,6 @@ void rms_power(struct tuning_state *ts)
ts->samples += 1; 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<count; i++) {
err1 = abs(target_gain - close_gain);
err2 = abs(target_gain - gains[i]);
if (err2 < err1) {
close_gain = gains[i];
}
}
free(gains);
return close_gain;
}
void frequency_range(char *arg, double crop) void frequency_range(char *arg, double crop)
/* flesh out the tunes[] for scanning */ /* flesh out the tunes[] for scanning */
// do we want the fewest ranges (easy) or the fewest bins (harder)? // do we want the fewest ranges (easy) or the fewest bins (harder)?
@ -852,8 +758,8 @@ int main(int argc, char **argv)
int f_set = 0; int f_set = 0;
int gain = AUTO_GAIN; // tenths of a dB int gain = AUTO_GAIN; // tenths of a dB
uint8_t *buffer; uint8_t *buffer;
uint32_t dev_index = 0; int dev_index = 0;
int device_count; int dev_given = 0;
int ppm_error = 0; int ppm_error = 0;
int interval = 10; int interval = 10;
int fft_threads = 1; int fft_threads = 1;
@ -862,7 +768,6 @@ int main(int argc, char **argv)
int direct_sampling = 0; int direct_sampling = 0;
int offset_tuning = 0; int offset_tuning = 0;
double crop = 0.0; double crop = 0.0;
char vendor[256], product[256], serial[256];
char *freq_optarg; char *freq_optarg;
time_t next_tick; time_t next_tick;
time_t time_now; time_t time_now;
@ -879,7 +784,8 @@ int main(int argc, char **argv)
f_set = 1; f_set = 1;
break; break;
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = verbose_device_search(optarg);
dev_given = 1;
break; break;
case 'g': case 'g':
gain = (int)(atof(optarg) * 10); gain = (int)(atof(optarg) * 10);
@ -972,23 +878,15 @@ int main(int argc, char **argv)
fprintf(stderr, "Reporting every %i seconds\n", interval); fprintf(stderr, "Reporting every %i seconds\n", interval);
device_count = rtlsdr_get_device_count(); if (!dev_given) {
if (!device_count) { dev_index = verbose_device_search("0");
fprintf(stderr, "No supported devices found.\n"); }
if (dev_index < 0) {
exit(1); exit(1);
} }
fprintf(stderr, "Found %d device(s):\n", device_count); r = rtlsdr_open(&dev, (uint32_t)dev_index);
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);
}
fprintf(stderr, "\n");
fprintf(stderr, "Using device %d: %s\n",
dev_index, rtlsdr_get_device_name(dev_index));
r = rtlsdr_open(&dev, dev_index);
if (r < 0) { if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1); exit(1);
@ -1006,39 +904,22 @@ int main(int argc, char **argv)
#endif #endif
if (direct_sampling) { if (direct_sampling) {
r = rtlsdr_set_direct_sampling(dev, 1); verbose_direct_sampling(dev, 1);
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set direct sampling mode.\n");
} else {
fprintf(stderr, "Direct sampling mode enabled.\n");
}
} }
if (offset_tuning) { if (offset_tuning) {
r = rtlsdr_set_offset_tuning(dev, 1); verbose_offset_tuning(dev);
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
} else {
fprintf(stderr, "Offset tuning mode enabled.\n");
}
} }
/* Set the tuner gain */ /* Set the tuner gain */
if (gain == AUTO_GAIN) { if (gain == AUTO_GAIN) {
r = rtlsdr_set_tuner_gain_mode(dev, 0); verbose_auto_gain(dev);
} else { } else {
r = rtlsdr_set_tuner_gain_mode(dev, 1); gain = nearest_gain(dev, gain);
gain = nearest_gain(gain); verbose_gain_set(dev, gain);
r = rtlsdr_set_tuner_gain(dev, gain);
} }
if (r != 0) {
fprintf(stderr, "WARNING: Failed to set tuner gain.\n"); verbose_ppm_set(dev, ppm_error);
} 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);
}
r = rtlsdr_set_freq_correction(dev, ppm_error);
if (strcmp(filename, "-") == 0) { /* Write log to stdout */ if (strcmp(filename, "-") == 0) { /* Write log to stdout */
file = stdout; file = stdout;
@ -1055,9 +936,7 @@ int main(int argc, char **argv)
} }
/* Reset endpoint before we start reading from it (mandatory) */ /* Reset endpoint before we start reading from it (mandatory) */
r = rtlsdr_reset_buffer(dev); verbose_reset_buffer(dev);
if (r < 0) {
fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
/* actually do stuff */ /* actually do stuff */
rtlsdr_set_sample_rate(dev, (uint32_t)tunes[0].rate); rtlsdr_set_sample_rate(dev, (uint32_t)tunes[0].rate);

View File

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

View File

@ -40,6 +40,7 @@
#include <pthread.h> #include <pthread.h>
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "convenience/convenience.h"
#ifdef _WIN32 #ifdef _WIN32
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -93,7 +94,8 @@ void usage(void)
"\t[-s samplerate in Hz (default: 2048000 Hz)]\n" "\t[-s samplerate in Hz (default: 2048000 Hz)]\n"
"\t[-b number of buffers (default: 32, set by library)]\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[-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); exit(1);
} }
@ -367,9 +369,11 @@ int main(int argc, char **argv)
int port = 1234; int port = 1234;
uint32_t frequency = 100000000, samp_rate = 2048000; uint32_t frequency = 100000000, samp_rate = 2048000;
struct sockaddr_in local, remote; struct sockaddr_in local, remote;
int device_count; uint32_t buf_num = 0;
uint32_t dev_index = 0, buf_num = 0; int dev_index = 0;
int dev_given = 0;
int gain = 0; int gain = 0;
int ppm_error = 0;
struct llist *curelem,*prev; struct llist *curelem,*prev;
pthread_attr_t attr; pthread_attr_t attr;
void *status; void *status;
@ -387,19 +391,20 @@ int main(int argc, char **argv)
struct sigaction sigact, sigign; struct sigaction sigact, sigign;
#endif #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) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = verbose_device_search(optarg);
dev_given = 1;
break; break;
case 'f': case 'f':
frequency = (uint32_t)atof(optarg); frequency = (uint32_t)atofs(optarg);
break; break;
case 'g': case 'g':
gain = (int)(atof(optarg) * 10); /* tenths of a dB */ gain = (int)(atof(optarg) * 10); /* tenths of a dB */
break; break;
case 's': case 's':
samp_rate = (uint32_t)atof(optarg); samp_rate = (uint32_t)atofs(optarg);
break; break;
case 'a': case 'a':
addr = optarg; addr = optarg;
@ -413,6 +418,9 @@ int main(int argc, char **argv)
case 'n': case 'n':
llbuf_num = atoi(optarg); llbuf_num = atoi(optarg);
break; break;
case 'P':
ppm_error = atoi(optarg);
break;
default: default:
usage(); usage();
break; break;
@ -422,21 +430,20 @@ int main(int argc, char **argv)
if (argc < optind) if (argc < optind)
usage(); usage();
device_count = rtlsdr_get_device_count(); if (!dev_given) {
if (!device_count) { dev_index = verbose_device_search("0");
fprintf(stderr, "No supported devices found.\n");
exit(1);
} }
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) { if (NULL == dev) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1); exit(1);
} }
printf("Using %s\n", rtlsdr_get_device_name(dev_index));
#ifndef _WIN32 #ifndef _WIN32
sigact.sa_handler = sighandler; sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask); sigemptyset(&sigact.sa_mask);
@ -449,6 +456,10 @@ int main(int argc, char **argv)
#else #else
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE ); SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif #endif
/* Set the tuner error */
verbose_ppm_set(dev, ppm_error);
/* Set the sample rate */ /* Set the sample rate */
r = rtlsdr_set_sample_rate(dev, samp_rate); r = rtlsdr_set_sample_rate(dev, samp_rate);
if (r < 0) if (r < 0)

View File

@ -37,6 +37,7 @@
#endif #endif
#include "rtl-sdr.h" #include "rtl-sdr.h"
#include "convenience/convenience.h"
#define DEFAULT_SAMPLE_RATE 2048000 #define DEFAULT_SAMPLE_RATE 2048000
#define DEFAULT_ASYNC_BUF_NUMBER 32 #define DEFAULT_ASYNC_BUF_NUMBER 32
@ -211,10 +212,10 @@ int main(int argc, char **argv)
int i, tuner_benchmark = 0; int i, tuner_benchmark = 0;
int sync_mode = 0; int sync_mode = 0;
uint8_t *buffer; 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 samp_rate = DEFAULT_SAMPLE_RATE;
uint32_t out_block_size = DEFAULT_BUF_LENGTH; uint32_t out_block_size = DEFAULT_BUF_LENGTH;
int device_count;
int count; int count;
int gains[100]; int gains[100];
int real_rate; int real_rate;
@ -223,7 +224,8 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "d:s:b:tpS::")) != -1) { while ((opt = getopt(argc, argv, "d:s:b:tpS::")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
dev_index = atoi(optarg); dev_index = verbose_device_search(optarg);
dev_given = 1;
break; break;
case 's': case 's':
samp_rate = (uint32_t)atof(optarg); samp_rate = (uint32_t)atof(optarg);
@ -259,22 +261,15 @@ int main(int argc, char **argv)
buffer = malloc(out_block_size * sizeof(uint8_t)); buffer = malloc(out_block_size * sizeof(uint8_t));
device_count = rtlsdr_get_device_count(); if (!dev_given) {
if (!device_count) { dev_index = verbose_device_search("0");
fprintf(stderr, "No supported devices found.\n"); }
if (dev_index < 0) {
exit(1); exit(1);
} }
fprintf(stderr, "Found %d device(s):\n", device_count); r = rtlsdr_open(&dev, (uint32_t)dev_index);
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));
r = rtlsdr_open(&dev, dev_index);
if (r < 0) { if (r < 0) {
fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index);
exit(1); exit(1);
@ -299,9 +294,7 @@ int main(int argc, char **argv)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
/* Set the sample rate */ /* Set the sample rate */
r = rtlsdr_set_sample_rate(dev, samp_rate); verbose_set_sample_rate(dev, samp_rate);
if (r < 0)
fprintf(stderr, "WARNING: Failed to set sample rate.\n");
if (tuner_benchmark) { if (tuner_benchmark) {
if (rtlsdr_get_tuner_type(dev) == RTLSDR_TUNER_E4000) 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); r = rtlsdr_set_testmode(dev, 1);
/* Reset endpoint before we start reading from it (mandatory) */ /* Reset endpoint before we start reading from it (mandatory) */
r = rtlsdr_reset_buffer(dev); verbose_reset_buffer(dev);
if (r < 0)
fprintf(stderr, "WARNING: Failed to reset buffers.\n");
if (ppm_benchmark && !sync_mode) { if (ppm_benchmark && !sync_mode) {
fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_benchmark); fprintf(stderr, "Reporting PPM error measurement every %i seconds...\n", ppm_benchmark);