gr-osmosdr/lib/source_impl.cc

970 lines
25 KiB
C++
Raw Normal View History

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"
2012-04-06 13:29:14 +00:00
#endif
2013-05-29 20:34:40 +00:00
#include <gnuradio/io_signature.h>
#include <gnuradio/blocks/null_source.h>
#include <gnuradio/blocks/throttle.h>
#include <gnuradio/constants.h>
2012-04-06 13:29:14 +00:00
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_OSMOSDR
#include <osmosdr_src_c.h>
#endif
#ifdef ENABLE_FCD
#include <fcd_source_c.h>
2012-04-28 18:18:14 +00:00
#endif
#ifdef ENABLE_FILE
#include <file_source_c.h>
#endif
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_RTL
#include <rtl_source_c.h>
#endif
#ifdef ENABLE_RTL_TCP
#include <rtl_tcp_source_c.h>
#endif
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_UHD
#include <uhd_source_c.h>
#endif
#ifdef ENABLE_MIRI
#include <miri_source_c.h>
#endif
#ifdef ENABLE_SDRPLAY
#include <sdrplay_source_c.h>
#endif
#ifdef ENABLE_HACKRF
#include <hackrf_source_c.h>
#endif
#ifdef ENABLE_BLADERF
#include <bladerf_source_c.h>
#endif
#ifdef ENABLE_RFSPACE
#include <rfspace_source_c.h>
#endif
#ifdef ENABLE_AIRSPY
#include <airspy_source_c.h>
#endif
#ifdef ENABLE_AIRSPYHF
#include <airspyhf_source_c.h>
#endif
2015-02-16 01:57:12 +00:00
#ifdef ENABLE_SOAPY
#include <soapy_source_c.h>
#endif
#ifdef ENABLE_REDPITAYA
#include <redpitaya_source_c.h>
#endif
#ifdef ENABLE_FREESRP
#include <freesrp_source_c.h>
#endif
#include "arg_helpers.h"
#include "source_impl.h"
2012-04-06 13:29:14 +00:00
/*
* Create a new instance of source_impl and return
2012-04-06 13:29:14 +00:00
* a boost shared_ptr. This is effectively the public constructor.
*/
osmosdr::source::sptr
osmosdr::source::make( const std::string &args )
2012-04-06 13:29:14 +00:00
{
return gnuradio::get_initial_sptr( new source_impl(args) );
2012-04-06 13:29:14 +00:00
}
/*
* The private constructor
*/
source_impl::source_impl( const std::string &args )
: gr::hier_block2 ("source_impl",
2013-05-29 20:34:40 +00:00
gr::io_signature::make(0, 0, 0),
args_to_io_signature(args)),
_sample_rate(NAN)
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
bool device_specified = false;
std::vector< std::string > arg_list = args_to_vector(args);
std::vector< std::string > dev_types;
#ifdef ENABLE_FILE
dev_types.push_back("file");
#endif
#ifdef ENABLE_OSMOSDR
dev_types.push_back("osmosdr");
#endif
#ifdef ENABLE_FCD
dev_types.push_back("fcd");
#endif
#ifdef ENABLE_RTL
dev_types.push_back("rtl");
#endif
#ifdef ENABLE_RTL_TCP
dev_types.push_back("rtl_tcp");
#endif
#ifdef ENABLE_UHD
dev_types.push_back("uhd");
#endif
#ifdef ENABLE_MIRI
dev_types.push_back("miri");
#endif
#ifdef ENABLE_SDRPLAY
dev_types.push_back("sdrplay");
#endif
#ifdef ENABLE_HACKRF
dev_types.push_back("hackrf");
#endif
#ifdef ENABLE_BLADERF
dev_types.push_back("bladerf");
#endif
#ifdef ENABLE_RFSPACE
dev_types.push_back("rfspace");
#endif
#ifdef ENABLE_AIRSPY
dev_types.push_back("airspy");
2015-02-16 01:57:12 +00:00
#endif
#ifdef ENABLE_AIRSPYHF
dev_types.push_back("airspyhf");
#endif
2015-02-16 01:57:12 +00:00
#ifdef ENABLE_SOAPY
dev_types.push_back("soapy");
#endif
#ifdef ENABLE_REDPITAYA
dev_types.push_back("redpitaya");
#endif
#ifdef ENABLE_FREESRP
dev_types.push_back("freesrp");
#endif
std::cerr << "gr-osmosdr "
<< GR_OSMOSDR_VERSION << " (" << GR_OSMOSDR_LIBVER << ") "
2013-05-29 20:34:40 +00:00
<< "gnuradio " << gr::version() << std::endl;
2013-05-07 20:44:08 +00:00
std::cerr << "built-in source types: ";
for (std::string dev_type : dev_types)
std::cerr << dev_type << " ";
std::cerr << std::endl;
#ifdef ENABLE_RFSPACE
dev_types.push_back("sdr-iq"); /* additional aliases for rfspace backend */
dev_types.push_back("sdr-ip");
dev_types.push_back("netsdr");
dev_types.push_back("cloudiq");
#endif
for (std::string arg : arg_list) {
dict_t dict = params_to_dict(arg);
for (std::string dev_type : dev_types) {
if ( dict.count( dev_type ) ) {
device_specified = true;
break;
}
}
}
if ( ! device_specified ) {
std::vector< std::string > dev_list;
#ifdef ENABLE_OSMOSDR
for (std::string dev : osmosdr_src_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_FCD
for (std::string dev : fcd_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_RTL
for (std::string dev : rtl_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_UHD
for (std::string dev : uhd_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_MIRI
for (std::string dev : miri_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_SDRPLAY
for (std::string dev : sdrplay_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_BLADERF
for (std::string dev : bladerf_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_RFSPACE
for (std::string dev : rfspace_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_HACKRF
for (std::string dev : hackrf_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_AIRSPY
for (std::string dev : airspy_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_AIRSPYHF
for (std::string dev : airspyhf_source_c::get_devices())
dev_list.push_back( dev );
#endif
2015-02-16 01:57:12 +00:00
#ifdef ENABLE_SOAPY
for (std::string dev : soapy_source_c::get_devices())
2015-02-16 01:57:12 +00:00
dev_list.push_back( dev );
#endif
#ifdef ENABLE_REDPITAYA
for (std::string dev : redpitaya_source_c::get_devices())
dev_list.push_back( dev );
#endif
#ifdef ENABLE_FREESRP
for (std::string dev : freesrp_source_c::get_devices())
dev_list.push_back( dev );
#endif
// std::cerr << std::endl;
// for (std::string dev : dev_list)
// std::cerr << "'" << dev << "'" << std::endl;
if ( dev_list.size() )
arg_list.push_back( dev_list.front() );
else
throw std::runtime_error("No supported devices found (check the connection and/or udev rules).");
}
for (std::string arg : arg_list) {
dict_t dict = params_to_dict(arg);
2012-04-06 13:29:14 +00:00
// std::cerr << std::endl;
// for (dict_t::value_type &entry : dict)
// std::cerr << "'" << entry.first << "' = '" << entry.second << "'" << std::endl;
source_iface *iface = NULL;
2013-05-29 20:34:40 +00:00
gr::basic_block_sptr block;
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_OSMOSDR
if ( dict.count("osmosdr") ) {
osmosdr_src_c_sptr src = osmosdr_make_src_c( arg );
block = src; iface = src.get();
2012-04-28 18:18:14 +00:00
}
#endif
#ifdef ENABLE_FCD
if ( dict.count("fcd") ) {
fcd_source_c_sptr src = make_fcd_source_c( arg );
block = src; iface = src.get();
2012-04-28 18:18:14 +00:00
}
#endif
#ifdef ENABLE_FILE
if ( dict.count("file") ) {
file_source_c_sptr src = make_file_source_c( arg );
block = src; iface = src.get();
}
#endif
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_RTL
if ( dict.count("rtl") ) {
rtl_source_c_sptr src = make_rtl_source_c( arg );
block = src; iface = src.get();
2012-04-28 18:18:14 +00:00
}
#endif
#ifdef ENABLE_RTL_TCP
if ( dict.count("rtl_tcp") ) {
rtl_tcp_source_c_sptr src = make_rtl_tcp_source_c( arg );
block = src; iface = src.get();
}
2012-04-28 18:18:14 +00:00
#endif
2012-04-28 18:18:14 +00:00
#ifdef ENABLE_UHD
if ( dict.count("uhd") ) {
uhd_source_c_sptr src = make_uhd_source_c( arg );
block = src; iface = src.get();
2012-04-28 18:18:14 +00:00
}
#endif
#ifdef ENABLE_MIRI
if ( dict.count("miri") ) {
miri_source_c_sptr src = make_miri_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_SDRPLAY
if ( dict.count("sdrplay") ) {
sdrplay_source_c_sptr src = make_sdrplay_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_HACKRF
if ( dict.count("hackrf") ) {
hackrf_source_c_sptr src = make_hackrf_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_BLADERF
if ( dict.count("bladerf") ) {
bladerf_source_c_sptr src = make_bladerf_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_RFSPACE
if ( dict.count("rfspace") ||
dict.count("sdr-iq") ||
dict.count("sdr-ip") ||
dict.count("netsdr") ||
dict.count("cloudiq") ) {
rfspace_source_c_sptr src = make_rfspace_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_AIRSPY
if ( dict.count("airspy") ) {
airspy_source_c_sptr src = make_airspy_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_AIRSPYHF
if ( dict.count("airspyhf") ) {
airspyhf_source_c_sptr src = make_airspyhf_source_c( arg );
block = src; iface = src.get();
}
#endif
2015-02-16 01:57:12 +00:00
#ifdef ENABLE_SOAPY
if ( dict.count("soapy") ) {
soapy_source_c_sptr src = make_soapy_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_REDPITAYA
if ( dict.count("redpitaya") ) {
redpitaya_source_c_sptr src = make_redpitaya_source_c( arg );
block = src; iface = src.get();
}
#endif
#ifdef ENABLE_FREESRP
if ( dict.count("freesrp") ) {
freesrp_source_c_sptr src = make_freesrp_source_c( arg );
block = src; iface = src.get();
}
#endif
if ( iface != NULL && long(block.get()) != 0 ) {
_devs.push_back( iface );
for (size_t i = 0; i < iface->get_num_channels(); i++) {
#ifdef HAVE_IQBALANCE
gr::iqbalance::optimize_c::sptr iq_opt = gr::iqbalance::optimize_c::make( 0 );
gr::iqbalance::fix_cc::sptr iq_fix = gr::iqbalance::fix_cc::make();
connect(block, i, iq_fix, 0);
connect(iq_fix, 0, self(), channel++);
connect(block, i, iq_opt, 0);
msg_connect(iq_opt, "iqbal_corr", iq_fix, "iqbal_corr");
_iq_opt.push_back( iq_opt.get() );
_iq_fix.push_back( iq_fix.get() );
#else
connect(block, i, self(), channel++);
#endif
}
} else if ( (iface != NULL) || (long(block.get()) != 0) )
throw std::runtime_error("Either iface or block are NULL.");
}
2012-04-28 18:18:14 +00:00
if (!_devs.size())
2012-04-28 18:18:14 +00:00
throw std::runtime_error("No devices specified via device arguments.");
2012-04-06 13:29:14 +00:00
}
size_t source_impl::get_num_channels()
2012-04-06 13:29:14 +00:00
{
size_t channels = 0;
for (source_iface *dev : _devs)
2012-04-28 18:18:14 +00:00
channels += dev->get_num_channels();
return channels;
2012-04-11 21:37:02 +00:00
}
2012-04-06 13:29:14 +00:00
bool source_impl::seek( long seek_point, int whence, size_t chan )
{
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
if ( chan == channel++ )
return dev->seek( seek_point, whence, dev_chan );
return false;
}
#define NO_DEVICES_MSG "FATAL: No device(s) available to work with."
osmosdr::meta_range_t source_impl::get_sample_rates()
2012-04-11 21:37:02 +00:00
{
if ( ! _devs.empty() )
return _devs[0]->get_sample_rates(); // assume same devices used in the group
#if 0
else
throw std::runtime_error(NO_DEVICES_MSG);
#endif
return osmosdr::meta_range_t();;
2012-04-06 13:29:14 +00:00
}
double source_impl::set_sample_rate(double rate)
2012-04-06 13:29:14 +00:00
{
double sample_rate = 0;
2012-04-28 18:18:14 +00:00
if (_sample_rate != rate) {
#if 0
if (_devs.empty())
throw std::runtime_error(NO_DEVICES_MSG);
#endif
for (source_iface *dev : _devs)
sample_rate = dev->set_sample_rate(rate);
#ifdef HAVE_IQBALANCE
size_t channel = 0;
for (source_iface *dev : _devs) {
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++) {
if ( channel < _iq_opt.size() ) {
gr::iqbalance::optimize_c *opt = _iq_opt[channel];
if ( opt->period() > 0 ) { /* optimize is enabled */
opt->set_period( dev->get_sample_rate() / 5 );
opt->reset();
}
}
channel++;
}
}
#endif
_sample_rate = sample_rate;
}
2012-04-28 18:18:14 +00:00
return _sample_rate;
2012-04-06 13:29:14 +00:00
}
double source_impl::get_sample_rate()
2012-04-06 13:29:14 +00:00
{
double sample_rate = 0;
if (!_devs.empty())
sample_rate = _devs[0]->get_sample_rate(); // assume same devices used in the group
#if 0
else
throw std::runtime_error(NO_DEVICES_MSG);
#endif
return sample_rate;
2012-04-06 13:29:14 +00:00
}
osmosdr::freq_range_t source_impl::get_freq_range( size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_freq_range( dev_chan );
2012-04-28 18:18:14 +00:00
return osmosdr::freq_range_t();
2012-04-06 13:29:14 +00:00
}
double source_impl::set_center_freq( double freq, size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
if ( chan == channel++ ) {
if ( _center_freq[ chan ] != freq ) {
_center_freq[ chan ] = freq;
return dev->set_center_freq( freq, dev_chan );
} else { return _center_freq[ chan ]; }
}
2012-04-28 18:18:14 +00:00
return 0;
2012-04-06 13:29:14 +00:00
}
double source_impl::get_center_freq( size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_center_freq( dev_chan );
2012-04-28 18:18:14 +00:00
return 0;
2012-04-06 13:29:14 +00:00
}
double source_impl::set_freq_corr( double ppm, size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
if ( chan == channel++ ) {
if ( _freq_corr[ chan ] != ppm ) {
_freq_corr[ chan ] = ppm;
return dev->set_freq_corr( ppm, dev_chan );
} else { return _freq_corr[ chan ]; }
}
2012-04-28 18:18:14 +00:00
return 0;
2012-04-06 13:29:14 +00:00
}
double source_impl::get_freq_corr( size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_freq_corr( dev_chan );
2012-04-28 18:18:14 +00:00
return 0;
2012-04-06 13:29:14 +00:00
}
std::vector<std::string> source_impl::get_gain_names( size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_gain_names( dev_chan );
2012-04-28 18:18:14 +00:00
return std::vector< std::string >();
2012-04-11 21:37:02 +00:00
}
2012-04-06 13:29:14 +00:00
osmosdr::gain_range_t source_impl::get_gain_range( size_t chan )
2012-04-11 21:37:02 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_gain_range( dev_chan );
2012-04-28 18:18:14 +00:00
return osmosdr::gain_range_t();
2012-04-06 13:29:14 +00:00
}
osmosdr::gain_range_t source_impl::get_gain_range( const std::string & name, size_t chan )
2012-04-06 13:29:14 +00:00
{
2012-04-28 18:18:14 +00:00
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
2012-04-28 18:18:14 +00:00
if ( chan == channel++ )
return dev->get_gain_range( name, dev_chan );
2012-04-28 18:18:14 +00:00
return osmosdr::gain_range_t();
2012-04-06 13:29:14 +00:00
}
bool source_impl::set_gain_mode( bool automatic, size_t chan )
2012-05-04 22:43:32 +00:00
{
size_t channel = 0;
for (source_iface *dev : _devs)
for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
if ( chan == channel++ ) {