From 9167a01248cdc28f5145cd1b2c5abfdc57a099bf Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sun, 6 May 2012 00:34:13 +0200 Subject: consolidate program arguments --- src/CMakeLists.txt | 2 +- src/Makefile.am | 3 +- src/main.c | 267 ----------------------------------------------------- src/rtl_sdr.c | 267 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rtl_tcp.c | 29 +++--- 5 files changed, 284 insertions(+), 284 deletions(-) delete mode 100644 src/main.c create mode 100644 src/rtl_sdr.c (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6d23607..e84c303 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,7 +57,7 @@ endif() ######################################################################## # Build utility ######################################################################## -add_executable(rtl_sdr main.c) +add_executable(rtl_sdr rtl_sdr.c) add_executable(rtl_tcp rtl_tcp.c) target_link_libraries(rtl_sdr rtlsdr_static ${LIBUSB_LIBRARIES} diff --git a/src/Makefile.am b/src/Makefile.am index f8cc5d6..8dce817 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,8 @@ librtlsdr_la_SOURCES = rtl-sdr.c tuner_e4k.c tuner_fc0012.c tuner_fc0013.c tuner librtlsdr_la_LDFALGS = -version-info $(LIBVERSION) bin_PROGRAMS = rtl_sdr rtl_tcp -rtl_sdr_SOURCES = main.c + +rtl_sdr_SOURCES = rtl_sdr.c rtl_sdr_LDADD = librtlsdr.la rtl_tcp_SOURCES = rtl_tcp.c diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 9cdf2da..0000000 --- a/src/main.c +++ /dev/null @@ -1,267 +0,0 @@ -/* - * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver - * Copyright (C) 2012 by Steve Markgraf - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include - -#ifndef _WIN32 -#include -#else -#include -#endif - -#include "rtl-sdr.h" - -#define DEFAULT_SAMPLE_RATE 2048000 -#define DEFAULT_ASYNC_BUF_NUMBER 32 -#define DEFAULT_BUF_LENGTH (16 * 16384) -#define MINIMAL_BUF_LENGTH 512 -#define MAXIMAL_BUF_LENGTH (256 * 16384) - -static int do_exit = 0; -static rtlsdr_dev_t *dev = NULL; - -void usage(void) -{ - #ifdef _WIN32 - fprintf(stderr,"rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n" - "Usage:\t rtl-sdr-win.exe [device_index] [samplerate in kHz] " - "[gain] [frequency in hz] [filename]\n"); - #else - fprintf(stderr, - "rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n" - "Usage:\t -f frequency_to_tune_to [Hz]\n" - "\t[-s samplerate (default: 2048000 Hz)]\n" - "\t[-d device_index (default: 0)]\n" - "\t[-g tuner_gain (default: 0 dB)]\n" - "\t[-b output_block_size (default: 16 * 16384)]\n" - "\t[-S force sync output (default: async)]\n" - "\toutput_filename (a '-' dumps samples to stdout)\n\n"); -#endif - exit(1); -} - -#ifdef _WIN32 -BOOL WINAPI -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; -} -#else -static void sighandler(int signum) -{ - fprintf(stderr, "Signal caught, exiting!\n"); - do_exit = 1; - rtlsdr_cancel_async(dev); -} -#endif - -static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) -{ - if (ctx) { - if (fwrite(buf, 1, len, (FILE*)ctx) != len) { - fprintf(stderr, "Short write, samples lost, exiting!\n"); - rtlsdr_cancel_async(dev); - } - } -} - -int main(int argc, char **argv) -{ -#ifndef _WIN32 - struct sigaction sigact; -#endif - char *filename = NULL; - int n_read; - int r, opt; - int i, gain = 0; - int sync_mode = 0; - FILE *file; - uint8_t *buffer; - uint32_t dev_index = 0; - uint32_t frequency = 0; - uint32_t samp_rate = DEFAULT_SAMPLE_RATE; - uint32_t out_block_size = DEFAULT_BUF_LENGTH; - int device_count; -#ifndef _WIN32 - while ((opt = getopt(argc, argv, "d:f:g:s:b:S::")) != -1) { - switch (opt) { - case 'd': - dev_index = atoi(optarg); - break; - case 'f': - frequency = (uint32_t)atof(optarg); - break; - case 'g': - gain = atoi(optarg); - break; - case 's': - samp_rate = (int)atof(optarg); - break; - case 'b': - out_block_size = (uint32_t)atof(optarg); - break; - case 'S': - sync_mode = 1; - break; - default: - usage(); - break; - } - } - - if (argc <= optind) { - usage(); - } else { - filename = argv[optind]; - } -#else - if(argc <6) - usage(); - dev_index = atoi(argv[1]); - samp_rate = atoi(argv[2])*1000; - gain=atoi(argv[3]); - frequency = atoi(argv[4]); - filename = argv[5]; -#endif - 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; - } - - 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); - } - - 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)); - - r = rtlsdr_open(&dev, dev_index); - if (r < 0) { - fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); - exit(1); - } -#ifndef _WIN32 - sigact.sa_handler = sighandler; - sigemptyset(&sigact.sa_mask); - sigact.sa_flags = 0; - sigaction(SIGINT, &sigact, NULL); - sigaction(SIGTERM, &sigact, NULL); - sigaction(SIGQUIT, &sigact, NULL); - sigaction(SIGPIPE, &sigact, NULL); -#else - 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"); - - /* 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); - - /* 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 %i dB.\n", gain); - - if(strcmp(filename, "-") == 0) { /* Write samples to stdout */ - file = stdout; - } else { - file = fopen(filename, "wb"); - if (!file) { - fprintf(stderr, "Failed to open %s\n", filename); - goto out; - } - } - - /* 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"); - - if (sync_mode) { - fprintf(stderr, "Reading samples in sync mode...\n"); - while (!do_exit) { - r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read); - if (r < 0) { - fprintf(stderr, "WARNING: sync read failed.\n"); - break; - } - - if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) { - fprintf(stderr, "Short write, samples lost, exiting!\n"); - break; - } - - if ((uint32_t)n_read < out_block_size) { - fprintf(stderr, "Short read, samples lost, exiting!\n"); - break; - } - } - } else { - fprintf(stderr, "Reading samples in async mode...\n"); - r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file, - DEFAULT_ASYNC_BUF_NUMBER, out_block_size); - } - - if (do_exit) - fprintf(stderr, "\nUser cancel, exiting...\n"); - else - fprintf(stderr, "\nLibrary error %d, exiting...\n", r); - - if (file != stdout) - fclose(file); - - rtlsdr_close(dev); - free (buffer); -out: - return r >= 0 ? r : -r; -} diff --git a/src/rtl_sdr.c b/src/rtl_sdr.c new file mode 100644 index 0000000..232ab77 --- /dev/null +++ b/src/rtl_sdr.c @@ -0,0 +1,267 @@ +/* + * rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver + * Copyright (C) 2012 by Steve Markgraf + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 +#include +#else +#include +#endif + +#include "rtl-sdr.h" + +#define DEFAULT_SAMPLE_RATE 2048000 +#define DEFAULT_ASYNC_BUF_NUMBER 32 +#define DEFAULT_BUF_LENGTH (16 * 16384) +#define MINIMAL_BUF_LENGTH 512 +#define MAXIMAL_BUF_LENGTH (256 * 16384) + +static int do_exit = 0; +static rtlsdr_dev_t *dev = NULL; + +void usage(void) +{ + #ifdef _WIN32 + fprintf(stderr,"rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n" + "Usage:\t rtl-sdr-win.exe [device_index] [samplerate in kHz] " + "[gain] [frequency in Hz] [filename]\n"); + #else + fprintf(stderr, + "rtl-sdr, an I/Q recorder for RTL2832 based DVB-T receivers\n\n" + "Usage:\t -f frequency_to_tune_to [Hz]\n" + "\t[-s samplerate (default: 2048000 Hz)]\n" + "\t[-d device_index (default: 0)]\n" + "\t[-g tuner_gain (default: 0 dB)]\n" + "\t[-b output_block_size (default: 16 * 16384)]\n" + "\t[-S force sync output (default: async)]\n" + "\tfilename (a '-' dumps samples to stdout)\n\n"); +#endif + exit(1); +} + +#ifdef _WIN32 +BOOL WINAPI +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; +} +#else +static void sighandler(int signum) +{ + fprintf(stderr, "Signal caught, exiting!\n"); + do_exit = 1; + rtlsdr_cancel_async(dev); +} +#endif + +static void rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx) +{ + if (ctx) { + if (fwrite(buf, 1, len, (FILE*)ctx) != len) { + fprintf(stderr, "Short write, samples lost, exiting!\n"); + rtlsdr_cancel_async(dev); + } + } +} + +int main(int argc, char **argv) +{ +#ifndef _WIN32 + struct sigaction sigact; +#endif + char *filename = NULL; + int n_read; + int r, opt; + int i, gain = 0; + int sync_mode = 0; + FILE *file; + uint8_t *buffer; + uint32_t dev_index = 0; + uint32_t frequency = 0; + uint32_t samp_rate = DEFAULT_SAMPLE_RATE; + uint32_t out_block_size = DEFAULT_BUF_LENGTH; + int device_count; +#ifndef _WIN32 + while ((opt = getopt(argc, argv, "d:f:g:s:b:S::")) != -1) { + switch (opt) { + case 'd': + dev_index = atoi(optarg); + break; + case 'f': + frequency = (uint32_t)atof(optarg); + break; + case 'g': + gain = (int)atof(optarg) * 10; + break; + case 's': + samp_rate = (uint32_t)atof(optarg); + break; + case 'b': + out_block_size = (uint32_t)atof(optarg); + break; + case 'S': + sync_mode = 1; + break; + default: + usage(); + break; + } + } + + if (argc <= optind) { + usage(); + } else { + filename = argv[optind]; + } +#else + if(argc <6) + usage(); + dev_index = atoi(argv[1]); + samp_rate = atoi(argv[2])*1000; + gain=(int)atof(argv[3]) * 10; + frequency = atoi(argv[4]); + filename = argv[5]; +#endif + 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; + } + + 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); + } + + 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)); + + r = rtlsdr_open(&dev, dev_index); + if (r < 0) { + fprintf(stderr, "Failed to open rtlsdr device #%d.\n", dev_index); + exit(1); + } +#ifndef _WIN32 + sigact.sa_handler = sighandler; + sigemptyset(&sigact.sa_mask); + sigact.sa_flags = 0; + sigaction(SIGINT, &sigact, NULL); + sigaction(SIGTERM, &sigact, NULL); + sigaction(SIGQUIT, &sigact, NULL); + sigaction(SIGPIPE, &sigact, NULL); +#else + 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"); + + /* 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); + + /* 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 %i dB.\n", gain); + + if(strcmp(filename, "-") == 0) { /* Write samples to stdout */ + file = stdout; + } else { + file = fopen(filename, "wb"); + if (!file) { + fprintf(stderr, "Failed to open %s\n", filename); + goto out; + } + } + + /* 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"); + + if (sync_mode) { + fprintf(stderr, "Reading samples in sync mode...\n"); + while (!do_exit) { + r = rtlsdr_read_sync(dev, buffer, out_block_size, &n_read); + if (r < 0) { + fprintf(stderr, "WARNING: sync read failed.\n"); + break; + } + + if (fwrite(buffer, 1, n_read, file) != (size_t)n_read) { + fprintf(stderr, "Short write, samples lost, exiting!\n"); + break; + } + + if ((uint32_t)n_read < out_block_size) { + fprintf(stderr, "Short read, samples lost, exiting!\n"); + break; + } + } + } else { + fprintf(stderr, "Reading samples in async mode...\n"); + r = rtlsdr_read_async(dev, rtlsdr_callback, (void *)file, + DEFAULT_ASYNC_BUF_NUMBER, out_block_size); + } + + if (do_exit) + fprintf(stderr, "\nUser cancel, exiting...\n"); + else + fprintf(stderr, "\nLibrary error %d, exiting...\n", r); + + if (file != stdout) + fclose(file); + + rtlsdr_close(dev); + free (buffer); +out: + return r >= 0 ? r : -r; +} diff --git a/src/rtl_tcp.c b/src/rtl_tcp.c index 89019c0..2fb1b34 100644 --- a/src/rtl_tcp.c +++ b/src/rtl_tcp.c @@ -79,15 +79,14 @@ void usage(void) #ifdef _WIN32 printf("rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n" "Usage:\t rtl-sdr-win.exe [listen addr] [listen port] " - "[samplerate in kHz] [frequency in hz] [device index]\n"); + "[samplerate in kHz] [frequency in Hz] [device index]\n"); #else printf("rtl-sdr, an I/Q recorder for RTL2832 based USB-sticks\n\n" "Usage:\t -a listen address\n" "\t[-p listen port (default: 1234)\n" "\t -f frequency to tune to [Hz]\n" - "\t[-s samplerate in kHz (default: 2048 kHz)]\n" - "\t[-d device index (default: 0)]\n" - "\toutput filename\n"); + "\t[-s samplerate in Hz (default: 2048000 Hz)]\n" + "\t[-d device index (default: 0)]\n"); #endif exit(1); } @@ -277,12 +276,13 @@ static void *command_worker(void *arg) } switch(cmd.cmd) { case 0x01: - printf("set freq %d\n", cmd.param); - rtlsdr_set_center_freq(dev, cmd.param); - break; + printf("set freq %d\n", cmd.param); + rtlsdr_set_center_freq(dev, cmd.param); + break; case 0x04: - rtlsdr_set_tuner_gain(dev, cmd.param); - break; + printf("set gain %d\n", cmd.param); + rtlsdr_set_tuner_gain(dev, cmd.param); + break; default: break; } @@ -315,11 +315,14 @@ int main(int argc, char **argv) struct sigaction sigact; while ((opt = getopt(argc, argv, "a:p:f:s:d:")) != -1) { switch (opt) { + case 'd': + dev_index = atoi(optarg); + break; case 'f': - frequency = atoi(optarg); + frequency = (uint32_t)atof(optarg); break; case 's': - samp_rate = atoi(optarg)*1000; + samp_rate = (uint32_t)atof(optarg); break; case 'a': addr = optarg; @@ -327,9 +330,6 @@ int main(int argc, char **argv) case 'p': port = atoi(optarg); break; - case 'd': - dev_index = atoi(optarg); - break; default: usage(); break; @@ -398,7 +398,6 @@ int main(int argc, char **argv) if (r < 0) fprintf(stderr, "WARNING: Failed to reset buffers.\n"); - pthread_mutex_init(&exit_cond_lock, NULL); pthread_mutex_init(&ll_mutex, NULL); pthread_mutex_init(&exit_cond_lock, NULL); -- cgit v1.3.1