2012-04-06 13:29:14 +00:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
|
|
|
|
*
|
|
|
|
* GNU Radio 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 3, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* config.h is generated by configure. It contains the results
|
|
|
|
* of probing for features, options etc. It should be the first
|
|
|
|
* file included in your .cc file.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-27 12:27:05 +00:00
|
|
|
#include "rtl_source_c.h"
|
2012-04-06 13:29:14 +00:00
|
|
|
#include <gr_io_signature.h>
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
#include <boost/assign.hpp>
|
2012-04-29 17:53:17 +00:00
|
|
|
#include <boost/format.hpp>
|
2012-06-29 19:27:22 +00:00
|
|
|
#include <boost/detail/endian.hpp>
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-04-06 13:29:14 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <rtl-sdr.h>
|
|
|
|
|
2012-04-28 18:18:14 +00:00
|
|
|
#include <osmosdr_arg_helpers.h>
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
using namespace boost::assign;
|
|
|
|
|
2012-11-28 19:44:43 +00:00
|
|
|
#define BUF_LEN (16 * 32 * 512) /* must be multiple of 512 */
|
2012-05-05 15:40:23 +00:00
|
|
|
#define BUF_NUM 32
|
2012-05-27 12:27:05 +00:00
|
|
|
#define BUF_SKIP 1 // buffers to skip due to initial garbage
|
|
|
|
|
|
|
|
#define BYTES_PER_SAMPLE 2 // rtl device delivers 8 bit unsigned IQ data
|
2012-04-26 17:14:42 +00:00
|
|
|
|
2012-04-06 13:29:14 +00:00
|
|
|
/*
|
|
|
|
* Create a new instance of rtl_source_c and return
|
|
|
|
* a boost shared_ptr. This is effectively the public constructor.
|
|
|
|
*/
|
|
|
|
rtl_source_c_sptr
|
|
|
|
make_rtl_source_c (const std::string &args)
|
|
|
|
{
|
|
|
|
return gnuradio::get_initial_sptr(new rtl_source_c (args));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Specify constraints on number of input and output streams.
|
|
|
|
* This info is used to construct the input and output signatures
|
|
|
|
* (2nd & 3rd args to gr_block's constructor). The input and
|
|
|
|
* output signatures are used by the runtime system to
|
|
|
|
* check that a valid number and type of inputs and outputs
|
|
|
|
* are connected to this block. In this case, we accept
|
2012-04-28 18:18:14 +00:00
|
|
|
* only 0 input and 1 output.
|
2012-04-06 13:29:14 +00:00
|
|
|
*/
|
|
|
|
static const int MIN_IN = 0; // mininum number of input streams
|
|
|
|
static const int MAX_IN = 0; // maximum number of input streams
|
|
|
|
static const int MIN_OUT = 1; // minimum number of output streams
|
|
|
|
static const int MAX_OUT = 1; // maximum number of output streams
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The private constructor
|
|
|
|
*/
|
|
|
|
rtl_source_c::rtl_source_c (const std::string &args)
|
|
|
|
: gr_sync_block ("rtl_source_c",
|
|
|
|
gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
|
2012-06-02 20:50:48 +00:00
|
|
|
gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex))),
|
2012-08-08 18:27:27 +00:00
|
|
|
_dev(NULL),
|
|
|
|
_buf(NULL),
|
2012-06-02 20:50:48 +00:00
|
|
|
_running(true),
|
|
|
|
_auto_gain(false),
|
|
|
|
_skipped(0)
|
2012-04-06 13:29:14 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2012-11-04 13:25:00 +00:00
|
|
|
int index;
|
2012-09-16 15:01:32 +00:00
|
|
|
unsigned int dev_index = 0, rtl_freq = 0, tuner_freq = 0, direct_samp = 0;
|
2012-10-10 19:03:48 +00:00
|
|
|
unsigned int offset_tune = 0;
|
2012-11-04 13:25:00 +00:00
|
|
|
char manufact[256];
|
|
|
|
char product[256];
|
|
|
|
char serial[256];
|
2012-04-28 18:18:14 +00:00
|
|
|
|
|
|
|
dict_t dict = params_to_dict(args);
|
2012-04-26 17:14:42 +00:00
|
|
|
|
2012-11-04 13:25:00 +00:00
|
|
|
if (dict.count("rtl")) {
|
2012-11-04 14:08:16 +00:00
|
|
|
if ( (index = rtlsdr_get_index_by_serial( dict["rtl"].c_str() )) >= 0 ) {
|
2012-11-04 13:25:00 +00:00
|
|
|
dev_index = index; /* use the resolved index value */
|
2012-11-04 14:08:16 +00:00
|
|
|
} else { /* use the numeric value of the argument */
|
|
|
|
try {
|
|
|
|
dev_index = boost::lexical_cast< unsigned int >( dict["rtl"] );
|
|
|
|
} catch ( std::exception &ex ) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Failed to use '" + dict["rtl"] + "' as index: " + ex.what());
|
|
|
|
}
|
|
|
|
}
|
2012-11-04 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( dev_index >= rtlsdr_get_device_count() )
|
|
|
|
throw std::runtime_error("Wrong rtlsdr device index given.");
|
|
|
|
|
|
|
|
std::cerr << "Using device #" << dev_index;
|
|
|
|
|
|
|
|
memset(manufact, 0, sizeof(manufact));
|
|
|
|
memset(product, 0, sizeof(product));
|
|
|
|
memset(serial, 0, sizeof(serial));
|
|
|
|
if ( !rtlsdr_get_device_usb_strings( dev_index, manufact, product, serial ) ) {
|
|
|
|
if (strlen(manufact))
|
|
|
|
std::cerr << " " << manufact;
|
|
|
|
if (strlen(product))
|
|
|
|
std::cerr << " " << product;
|
|
|
|
if (strlen(serial))
|
|
|
|
std::cerr << " SN: " << serial;
|
|
|
|
} else {
|
|
|
|
std::cerr << " " << rtlsdr_get_device_name(dev_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
2012-04-26 17:14:42 +00:00
|
|
|
|
2012-04-29 17:53:17 +00:00
|
|
|
if (dict.count("rtl_xtal"))
|
|
|
|
rtl_freq = (unsigned int)boost::lexical_cast< double >( dict["rtl_xtal"] );
|
|
|
|
|
|
|
|
if (dict.count("tuner_xtal"))
|
|
|
|
tuner_freq = (unsigned int)boost::lexical_cast< double >( dict["tuner_xtal"] );
|
|
|
|
|
2012-09-16 15:01:32 +00:00
|
|
|
if (dict.count("direct_samp"))
|
|
|
|
direct_samp = boost::lexical_cast< unsigned int >( dict["direct_samp"] );
|
|
|
|
|
2012-10-10 19:03:48 +00:00
|
|
|
if (dict.count("offset_tune"))
|
|
|
|
offset_tune = boost::lexical_cast< unsigned int >( dict["offset_tune"] );
|
|
|
|
|
2012-11-28 19:44:43 +00:00
|
|
|
_buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;
|
2012-05-05 15:40:23 +00:00
|
|
|
|
2012-11-28 19:44:43 +00:00
|
|
|
if (dict.count("buffers"))
|
|
|
|
_buf_num = boost::lexical_cast< unsigned int >( dict["buffers"] );
|
|
|
|
|
|
|
|
if (dict.count("buflen"))
|
|
|
|
_buf_len = boost::lexical_cast< unsigned int >( dict["buflen"] );
|
|
|
|
|
|
|
|
if (0 == _buf_num)
|
|
|
|
_buf_num = BUF_NUM;
|
|
|
|
|
|
|
|
if (0 == _buf_len || _buf_len % 512 != 0) /* len must be multiple of 512 */
|
|
|
|
_buf_len = BUF_LEN;
|
|
|
|
|
|
|
|
if ( BUF_NUM != _buf_num || BUF_LEN != _buf_len ) {
|
|
|
|
std::cerr << "Using " << _buf_num << " buffers of size " << _buf_len << "."
|
2012-05-05 15:40:23 +00:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:44:43 +00:00
|
|
|
_samp_avail = _buf_len / BYTES_PER_SAMPLE;
|
|
|
|
|
2012-04-06 13:29:14 +00:00
|
|
|
// create a lookup table for gr_complex values
|
2012-04-26 17:14:42 +00:00
|
|
|
for (unsigned int i = 0; i <= 0xffff; i++) {
|
2012-06-29 19:27:22 +00:00
|
|
|
#ifdef BOOST_LITTLE_ENDIAN
|
2012-05-27 12:27:05 +00:00
|
|
|
_lut.push_back( gr_complex( (float(i & 0xff) - 127.5f) * (1.0f/128.0f),
|
2012-04-09 20:47:46 +00:00
|
|
|
(float(i >> 8) - 127.5f) * (1.0f/128.0f) ) );
|
2012-06-29 19:27:22 +00:00
|
|
|
#else // BOOST_BIG_ENDIAN
|
2012-04-09 20:47:46 +00:00
|
|
|
_lut.push_back( gr_complex( (float(i >> 8) - 127.5f) * (1.0f/128.0f),
|
|
|
|
(float(i & 0xff) - 127.5f) * (1.0f/128.0f) ) );
|
2012-04-06 16:52:26 +00:00
|
|
|
#endif
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_dev = NULL;
|
2012-04-11 21:37:02 +00:00
|
|
|
ret = rtlsdr_open( &_dev, dev_index );
|
|
|
|
if (ret < 0)
|
2012-05-27 12:27:05 +00:00
|
|
|
throw std::runtime_error("Failed to open rtlsdr device.");
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-04-29 17:53:17 +00:00
|
|
|
if (rtl_freq > 0 || tuner_freq > 0) {
|
|
|
|
if (rtl_freq)
|
|
|
|
std::cerr << "Setting rtl clock to " << rtl_freq << " Hz." << std::endl;
|
|
|
|
if (tuner_freq)
|
|
|
|
std::cerr << "Setting tuner clock to " << tuner_freq << " Hz." << std::endl;
|
|
|
|
|
|
|
|
ret = rtlsdr_set_xtal_freq( _dev, rtl_freq, tuner_freq );
|
|
|
|
if (ret < 0)
|
2012-05-27 12:27:05 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
str(boost::format("Failed to set xtal frequencies. Error %d.") % ret ));
|
2012-04-29 17:53:17 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
ret = rtlsdr_set_sample_rate( _dev, 1024000 );
|
2012-04-06 13:29:14 +00:00
|
|
|
if (ret < 0)
|
2012-05-27 12:27:05 +00:00
|
|
|
throw std::runtime_error("Failed to set default samplerate.");
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-06-02 20:50:48 +00:00
|
|
|
ret = rtlsdr_set_tuner_gain_mode(_dev, int(!_auto_gain));
|
2012-05-05 19:06:00 +00:00
|
|
|
if (ret < 0)
|
2012-08-01 20:21:00 +00:00
|
|
|
throw std::runtime_error("Failed to set tuner gain mode.");
|
|
|
|
|
|
|
|
ret = rtlsdr_set_agc_mode(_dev, int(_auto_gain));
|
|
|
|
if (ret < 0)
|
|
|
|
throw std::runtime_error("Failed to set agc mode.");
|
2012-05-05 19:06:00 +00:00
|
|
|
|
2012-09-16 15:01:32 +00:00
|
|
|
if (direct_samp) {
|
|
|
|
ret = rtlsdr_set_direct_sampling(_dev, direct_samp);
|
|
|
|
if (ret < 0)
|
2012-11-28 19:44:43 +00:00
|
|
|
throw std::runtime_error("Failed to enable direct sampling.");
|
2012-09-16 15:01:32 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 19:03:48 +00:00
|
|
|
if (offset_tune) {
|
|
|
|
ret = rtlsdr_set_offset_tuning(_dev, offset_tune);
|
|
|
|
if (ret < 0)
|
|
|
|
throw std::runtime_error("Failed to enable offset tuning.");
|
|
|
|
}
|
|
|
|
|
2012-06-02 20:50:48 +00:00
|
|
|
ret = rtlsdr_reset_buffer( _dev );
|
|
|
|
if (ret < 0)
|
|
|
|
throw std::runtime_error("Failed to reset usb buffers.");
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-07-18 19:24:57 +00:00
|
|
|
set_if_gain( 24 ); /* preset to a reasonable default (non-GRC use case) */
|
|
|
|
|
2012-06-02 20:50:48 +00:00
|
|
|
_buf = (unsigned short **) malloc(_buf_num * sizeof(unsigned short *));
|
2012-05-04 22:43:32 +00:00
|
|
|
|
2012-08-08 18:27:27 +00:00
|
|
|
if (_buf) {
|
|
|
|
for(unsigned int i = 0; i < _buf_num; ++i)
|
2012-11-28 19:44:43 +00:00
|
|
|
_buf[i] = (unsigned short *) malloc(_buf_len);
|
2012-08-08 18:27:27 +00:00
|
|
|
}
|
2012-05-13 18:01:50 +00:00
|
|
|
|
2012-04-06 13:29:14 +00:00
|
|
|
_thread = gruel::thread(_rtlsdr_wait, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our virtual destructor.
|
|
|
|
*/
|
|
|
|
rtl_source_c::~rtl_source_c ()
|
|
|
|
{
|
|
|
|
if (_dev) {
|
2012-05-20 10:30:28 +00:00
|
|
|
_running = false;
|
2012-04-06 13:29:14 +00:00
|
|
|
rtlsdr_cancel_async( _dev );
|
2012-08-08 18:31:51 +00:00
|
|
|
_thread.join();
|
2012-04-06 13:29:14 +00:00
|
|
|
rtlsdr_close( _dev );
|
|
|
|
_dev = NULL;
|
|
|
|
}
|
2012-04-26 17:14:42 +00:00
|
|
|
|
2012-08-08 18:27:27 +00:00
|
|
|
if (_buf) {
|
|
|
|
for(unsigned int i = 0; i < _buf_num; ++i) {
|
|
|
|
if (_buf[i])
|
|
|
|
free(_buf[i]);
|
|
|
|
}
|
2012-04-26 17:14:42 +00:00
|
|
|
|
2012-08-08 18:27:27 +00:00
|
|
|
free(_buf);
|
|
|
|
_buf = NULL;
|
|
|
|
}
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtl_source_c::_rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx)
|
|
|
|
{
|
|
|
|
rtl_source_c *obj = (rtl_source_c *)ctx;
|
|
|
|
obj->rtlsdr_callback(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtl_source_c::rtlsdr_callback(unsigned char *buf, uint32_t len)
|
|
|
|
{
|
2012-05-13 18:01:50 +00:00
|
|
|
if (_skipped < BUF_SKIP) {
|
|
|
|
_skipped++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock( _buf_mutex );
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
int buf_tail = (_buf_head + _buf_used) % _buf_num;
|
|
|
|
memcpy(_buf[buf_tail], buf, len);
|
2012-04-06 16:52:26 +00:00
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
if (_buf_used == _buf_num) {
|
2012-08-08 18:45:03 +00:00
|
|
|
std::cerr << "O" << std::flush;
|
2012-04-26 17:14:42 +00:00
|
|
|
_buf_head = (_buf_head + 1) % _buf_num;
|
|
|
|
} else {
|
|
|
|
_buf_used++;
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
2012-04-06 16:52:26 +00:00
|
|
|
}
|
2012-04-11 21:37:02 +00:00
|
|
|
|
|
|
|
_buf_cond.notify_one();
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtl_source_c::_rtlsdr_wait(rtl_source_c *obj)
|
|
|
|
{
|
|
|
|
obj->rtlsdr_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtl_source_c::rtlsdr_wait()
|
|
|
|
{
|
2012-11-28 19:44:43 +00:00
|
|
|
int ret = rtlsdr_read_async( _dev, _rtlsdr_callback, (void *)this, 0, _buf_len );
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
_running = false;
|
|
|
|
|
|
|
|
if ( ret != 0 )
|
|
|
|
std::cerr << "rtlsdr_read_async returned with " << ret << std::endl;
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rtl_source_c::work( int noutput_items,
|
|
|
|
gr_vector_const_void_star &input_items,
|
|
|
|
gr_vector_void_star &output_items )
|
|
|
|
{
|
2012-04-06 16:52:26 +00:00
|
|
|
gr_complex *out = (gr_complex *)output_items[0];
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock( _buf_mutex );
|
|
|
|
|
2012-05-27 12:27:05 +00:00
|
|
|
while (_buf_used < 3 && _running) // collect at least 3 buffers
|
|
|
|
_buf_cond.wait( lock );
|
2012-04-26 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
if (!_running)
|
|
|
|
return WORK_DONE;
|
|
|
|
|
2012-05-27 12:27:05 +00:00
|
|
|
unsigned short *buf = _buf[_buf_head] + _buf_offset;
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-05-27 12:32:50 +00:00
|
|
|
if (noutput_items <= _samp_avail) {
|
2012-05-13 02:17:02 +00:00
|
|
|
for (int i = 0; i < noutput_items; ++i)
|
2012-05-27 12:27:05 +00:00
|
|
|
*out++ = _lut[ *(buf + i) ];
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
_buf_offset += noutput_items;
|
|
|
|
_samp_avail -= noutput_items;
|
|
|
|
} else {
|
2012-05-27 12:32:50 +00:00
|
|
|
for (int i = 0; i < _samp_avail; ++i)
|
2012-05-27 12:27:05 +00:00
|
|
|
*out++ = _lut[ *(buf + i) ];
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-04-06 16:52:26 +00:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock( _buf_mutex );
|
2012-04-06 13:29:14 +00:00
|
|
|
|
2012-04-26 17:14:42 +00:00
|
|
|
_buf_head = (_buf_head + 1) % _buf_num;
|
|
|
|
_buf_used--;
|
2012-04-06 16:52:26 +00:00
|
|
|
}
|
2012-04-26 17:14:42 +00:00
|
|
|
|
|
|
|
buf = _buf[_buf_head];
|
|
|
|
|
|
|
|
int remaining = noutput_items - _samp_avail;
|
|
|
|
|
2012-05-13 02:17:02 +00:00
|
|
|
for (int i = 0; i < remaining; ++i)
|
2012-05-27 12:27:05 +00:00
|
|
|
*out++ = _lut[ *(buf + i) ];
|
2012-04-26 17:14:42 +00:00
|
|
|
|
|
|
|
_buf_offset = remaining;
|
2012-11-28 19:44:43 +00:00
|
|
|
_samp_avail = (_buf_len / BYTES_PER_SAMPLE) - remaining;
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return noutput_items;
|
|
|
|
}
|
|
|
|
|
2012-05-05 18:43:34 +00:00
|
|
|
std::vector<std::string> rtl_source_c::get_devices()
|
|
|
|
{
|
2012-05-20 10:30:28 +00:00
|
|
|
std::vector<std::string> devices;
|
2012-05-05 18:43:34 +00:00
|
|
|
|
2012-05-20 10:30:28 +00:00
|
|
|
for (unsigned int i = 0; i < rtlsdr_get_device_count(); i++) {
|
|
|
|
std::string args = "rtl=" + boost::lexical_cast< std::string >( i );
|
|
|
|
args += ",label='" + std::string(rtlsdr_get_device_name( i )) + "'";
|
|
|
|
devices.push_back( args );
|
|
|
|
}
|
2012-05-05 18:43:34 +00:00
|
|
|
|
2012-05-20 10:30:28 +00:00
|
|
|
return devices;
|
2012-05-05 18:43:34 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
size_t rtl_source_c::get_num_channels()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
osmosdr::meta_range_t rtl_source_c::get_sample_rates()
|
|
|
|
{
|
|
|
|
osmosdr::meta_range_t range;
|
|
|
|
|
2012-11-28 21:40:28 +00:00
|
|
|
range += osmosdr::range_t( 250000 ); // known to work
|
2012-06-01 23:18:07 +00:00
|
|
|
range += osmosdr::range_t( 1000000 ); // known to work
|
2012-04-26 17:14:42 +00:00
|
|
|
range += osmosdr::range_t( 1024000 ); // known to work
|
2012-05-04 22:43:32 +00:00
|
|
|
range += osmosdr::range_t( 1800000 ); // known to work
|
2012-06-01 23:18:07 +00:00
|
|
|
range += osmosdr::range_t( 1920000 ); // known to work
|
2012-09-30 18:58:21 +00:00
|
|
|
range += osmosdr::range_t( 2000000 ); // known to work
|
2012-04-26 17:14:42 +00:00
|
|
|
range += osmosdr::range_t( 2048000 ); // known to work
|
2012-06-01 23:18:07 +00:00
|
|
|
range += osmosdr::range_t( 2400000 ); // known to work
|
2012-09-30 18:58:21 +00:00
|
|
|
// range += osmosdr::range_t( 2600000 ); // may work
|
|
|
|
// range += osmosdr::range_t( 2800000 ); // may work
|
|
|
|
// range += osmosdr::range_t( 3000000 ); // may work
|
|
|
|
// range += osmosdr::range_t( 3200000 ); // max rate
|
2012-04-11 21:37:02 +00:00
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::set_sample_rate(double rate)
|
2012-04-06 13:29:14 +00:00
|
|
|
{
|
|
|
|
if (_dev) {
|
2012-04-11 21:37:02 +00:00
|
|
|
rtlsdr_set_sample_rate( _dev, (uint32_t)rate );
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
return get_sample_rate();
|
2012-04-06 13:29:14 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
double rtl_source_c::get_sample_rate()
|
2012-04-06 13:29:14 +00:00
|
|
|
{
|
2012-04-11 21:37:02 +00:00
|
|
|
if (_dev)
|
2012-04-06 13:29:14 +00:00
|
|
|
return (double)rtlsdr_get_sample_rate( _dev );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
osmosdr::freq_range_t rtl_source_c::get_freq_range( size_t chan )
|
|
|
|
{
|
|
|
|
osmosdr::freq_range_t range;
|
|
|
|
|
2012-06-01 23:18:07 +00:00
|
|
|
if (_dev) {
|
|
|
|
enum rtlsdr_tuner tuner = rtlsdr_get_tuner_type(_dev);
|
|
|
|
|
|
|
|
if ( tuner == RTLSDR_TUNER_E4000 ) {
|
|
|
|
/* there is a (temperature dependent) gap between 1100 to 1250 MHz */
|
2012-07-01 09:18:29 +00:00
|
|
|
range += osmosdr::range_t( 50e6, 2.2e9 );
|
2012-06-01 23:18:07 +00:00
|
|
|
} else if ( tuner == RTLSDR_TUNER_FC0012 ) {
|
2012-07-01 09:18:29 +00:00
|
|
|
/* range += osmosdr::range_t( 50e6, 2.2e9 ); */
|
2012-06-01 23:18:07 +00:00
|
|
|
} else if ( tuner == RTLSDR_TUNER_FC0013 ) {
|
2012-07-01 09:18:29 +00:00
|
|
|
/* range += osmosdr::range_t( 50e6, 2.2e9 ); */
|
2012-06-01 23:18:07 +00:00
|
|
|
} else if ( tuner == RTLSDR_TUNER_FC2580 ) {
|
2012-07-01 09:18:29 +00:00
|
|
|
/* range += osmosdr::range_t( 50e6, 2.2e9 ); */
|
2012-06-01 23:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 21:37:02 +00:00
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::set_center_freq( double freq, size_t chan )
|
2012-04-06 13:29:14 +00:00
|
|
|
{
|
|
|
|
if (_dev)
|
2012-04-11 21:37:02 +00:00
|
|
|
rtlsdr_set_center_freq( _dev, (uint32_t)freq );
|
|
|
|
|
|
|
|
return get_center_freq( chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::get_center_freq( size_t chan )
|
|
|
|
{
|
|
|
|
if (_dev)
|
|
|
|
return (double)rtlsdr_get_center_freq( _dev );
|
2012-04-06 13:29:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
double rtl_source_c::set_freq_corr( double ppm, size_t chan )
|
2012-04-06 13:29:14 +00:00
|
|
|
{
|
2012-04-11 21:37:02 +00:00
|
|
|
if ( _dev )
|
|
|
|
rtlsdr_set_freq_correction( _dev, (int)ppm );
|
|
|
|
|
|
|
|
return get_freq_corr( chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::get_freq_corr( size_t chan )
|
|
|
|
{
|
|
|
|
if ( _dev )
|
|
|
|
return (double)rtlsdr_get_freq_correction( _dev );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> rtl_source_c::get_gain_names( size_t chan )
|
|
|
|
{
|
2012-06-02 15:02:31 +00:00
|
|
|
std::vector< std::string > gains;
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-06-02 15:02:31 +00:00
|
|
|
gains += "LNA";
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-06-02 15:02:31 +00:00
|
|
|
return gains;
|
2012-04-11 21:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
osmosdr::gain_range_t rtl_source_c::get_gain_range( size_t chan )
|
|
|
|
{
|
|
|
|
osmosdr::gain_range_t range;
|
|
|
|
|
2012-06-01 23:18:07 +00:00
|
|
|
if (_dev) {
|
|
|
|
int count = rtlsdr_get_tuner_gains(_dev, NULL);
|
|
|
|
if (count > 0) {
|
2012-06-02 19:56:17 +00:00
|
|
|
int* gains = new int[ count ];
|
2012-06-01 23:18:07 +00:00
|
|
|
count = rtlsdr_get_tuner_gains(_dev, gains);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
range += osmosdr::range_t( gains[i] / 10.0 );
|
2012-06-02 19:56:17 +00:00
|
|
|
delete[] gains;
|
2012-06-01 23:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 21:37:02 +00:00
|
|
|
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
osmosdr::gain_range_t rtl_source_c::get_gain_range( const std::string & name, size_t chan )
|
|
|
|
{
|
|
|
|
return get_gain_range( chan );
|
|
|
|
}
|
|
|
|
|
2012-05-17 08:35:58 +00:00
|
|
|
bool rtl_source_c::set_gain_mode( bool automatic, size_t chan )
|
2012-05-04 22:43:32 +00:00
|
|
|
{
|
|
|
|
if (_dev) {
|
2012-05-17 08:35:58 +00:00
|
|
|
if (!rtlsdr_set_tuner_gain_mode(_dev, int(!automatic))) {
|
|
|
|
_auto_gain = automatic;
|
2012-05-04 22:43:32 +00:00
|
|
|
}
|
2012-08-01 20:21:00 +00:00
|
|
|
|
|
|
|
rtlsdr_set_agc_mode(_dev, int(automatic));
|
2012-05-04 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return get_gain_mode(chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rtl_source_c::get_gain_mode( size_t chan )
|
|
|
|
{
|
2012-05-05 19:06:00 +00:00
|
|
|
return _auto_gain;
|
2012-05-04 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 21:37:02 +00:00
|
|
|
double rtl_source_c::set_gain( double gain, size_t chan )
|
|
|
|
{
|
2012-07-08 21:56:05 +00:00
|
|
|
osmosdr::gain_range_t rf_gains = rtl_source_c::get_gain_range( chan );
|
2012-07-18 19:24:57 +00:00
|
|
|
|
|
|
|
if (_dev) {
|
|
|
|
rtlsdr_set_tuner_gain( _dev, int(rf_gains.clip(gain) * 10.0) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_gain( chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::set_gain( double gain, const std::string & name, size_t chan)
|
|
|
|
{
|
|
|
|
return set_gain( gain, chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::get_gain( size_t chan )
|
|
|
|
{
|
|
|
|
if ( _dev )
|
|
|
|
return ((double)rtlsdr_get_tuner_gain( _dev )) / 10.0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::get_gain( const std::string & name, size_t chan )
|
|
|
|
{
|
|
|
|
return get_gain( chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
double rtl_source_c::set_if_gain(double gain, size_t chan)
|
|
|
|
{
|
2012-07-08 21:56:05 +00:00
|
|
|
std::vector< osmosdr::gain_range_t > if_gains;
|
2012-05-05 19:37:13 +00:00
|
|
|
|
2012-07-08 21:56:05 +00:00
|
|
|
if_gains += osmosdr::gain_range_t(-3, 6, 9);
|
|
|
|
if_gains += osmosdr::gain_range_t(0, 9, 3);
|
|
|
|
if_gains += osmosdr::gain_range_t(0, 9, 3);
|
|
|
|
if_gains += osmosdr::gain_range_t(0, 2, 1);
|
|
|
|
if_gains += osmosdr::gain_range_t(3, 15, 3);
|
|
|
|
if_gains += osmosdr::gain_range_t(3, 15, 3);
|
|
|
|
|
|
|
|
std::map< int, double > gains;
|
|
|
|
|
|
|
|
/* initialize with min gains */
|
|
|
|
for (unsigned int i = 0; i < if_gains.size(); i++) {
|
|
|
|
gains[ i + 1 ] = if_gains[ i ].start();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = if_gains.size() - 1; i >= 0; i--) {
|
|
|
|
osmosdr::gain_range_t range = if_gains[ i ];
|
|
|
|
|
2012-07-18 19:24:57 +00:00
|
|
|
double error = gain;
|
2012-07-08 21:56:05 +00:00
|
|
|
|
|
|
|
for( double g = range.start(); g <= range.stop(); g += range.step() ) {
|
|
|
|
|
|
|
|
double sum = 0;
|
|
|
|
for (int j = 0; j < int(gains.size()); j++) {
|
|
|
|
if ( i == j )
|
|
|
|
sum += g;
|
|
|
|
else
|
|
|
|
sum += gains[ j + 1 ];
|
|
|
|
}
|
|
|
|
|
2012-07-18 19:24:57 +00:00
|
|
|
double err = abs(gain - sum);
|
2012-07-08 21:56:05 +00:00
|
|
|
if (err < error) {
|
|
|
|
error = err;
|
|
|
|
gains[ i + 1 ] = g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if 0
|
2012-07-18 19:24:57 +00:00
|
|
|
std::cerr << gain << " => "; double sum = 0;
|
2012-07-08 21:56:05 +00:00
|
|
|
for (unsigned int i = 0; i < gains.size(); i++) {
|
|
|
|
sum += gains[ i + 1 ];
|
|
|
|
std::cerr << gains[ i + 1 ] << " ";
|
|
|
|
}
|
|
|
|
std::cerr << " = " << sum << std::endl;
|
|
|
|
#endif
|
|
|
|
if (_dev) {
|
|
|
|
for (unsigned int stage = 1; stage <= gains.size(); stage++) {
|
|
|
|
rtlsdr_set_tuner_if_gain( _dev, stage, int(gains[ stage ] * 10.0));
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 21:37:02 +00:00
|
|
|
|
2012-07-18 19:24:57 +00:00
|
|
|
return gain;
|
2012-04-11 21:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< std::string > rtl_source_c::get_antennas( size_t chan )
|
|
|
|
{
|
|
|
|
std::vector< std::string > antennas;
|
|
|
|
|
|
|
|
antennas += get_antenna( chan );
|
|
|
|
|
|
|
|
return antennas;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rtl_source_c::set_antenna( const std::string & antenna, size_t chan )
|
|
|
|
{
|
|
|
|
return get_antenna( chan );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rtl_source_c::get_antenna( size_t chan )
|
|
|
|
{
|
|
|
|
return "ANT";
|
|
|
|
}
|