baz_rtl_source_c.h
Go to the documentation of this file.
/* -*- c++ -*- */
/*
* Copyright 2004,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* 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.
*/
/*
* gr-baz by Balint Seeber (http://spench.net/contact)
* Information, documentation & samples: http://wiki.spench.net/wiki/gr-baz
*/
#ifndef INCLUDED_BAZ_RTL_SOURCE_C_H
#define INCLUDED_BAZ_RTL_SOURCE_C_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/block.h>
#include <gnuradio/msg_queue.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#define NOMINMAX
#endif
#include <boost/thread.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/recursive_mutex.hpp>
#ifdef HAVE_XTIME
#include <boost/thread/xtime.hpp>
#endif // HAVE_XTIME
#include <libusb-1.0/libusb.h> // FIXME: Automake
#include <stdarg.h> // va_list
#include "rtl2832.h"
class BAZ_API baz_rtl_source_c;
typedef boost::shared_ptr<baz_rtl_source_c> baz_rtl_source_c_sptr;
/*!
* \brief Return a shared_ptr to a new instance of baz_rtl_source_c.
*/
BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation = false, int output_size = 0);
/*!
* \brief capture samples from an RTL2832-based device.
* \ingroup block
*
* \sa gr-baz: http://wiki.spench.net/wiki/gr-baz
*/
class BAZ_API baz_rtl_source_c : public gr::block, public RTL2832_NAMESPACE::log_sink
{
private:
friend BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation, int output_size);
private:
baz_rtl_source_c(bool defer_creation = false, int output_size = 0);
public:
~baz_rtl_source_c();
private:
RTL2832_NAMESPACE::demod m_demod;
size_t m_recv_samples_per_packet;
uint64_t m_nSamplesReceived;
uint32_t m_nOverflows;
bool m_bRunning;
boost::recursive_mutex d_mutex;
boost::thread m_pCaptureThread;
uint32_t m_nBufferSize;
uint32_t m_nBufferStart;
uint32_t m_nBufferItems;
boost::condition m_hPacketEvent;
uint8_t* m_pUSBBuffer;
bool m_bBuffering;
uint32_t m_nReadLength;
uint32_t m_nBufferMultiplier;
bool m_bUseBuffer;
float m_fBufferLevel;
uint32_t m_nReadPacketCount;
uint32_t m_nBufferOverflowCount;
uint32_t m_nBufferUnderrunCount;
#ifdef HAVE_XTIME
boost::xtime m_wait_delay, m_wait_next;
#endif // HAVE_XTIME
private:
RTL2832_NAMESPACE::demod::PARAMS m_demod_params;
bool m_verbose;
bool m_relative_gain;
int m_output_size;
gr::msg_queue::sptr m_status_queue;
private:
enum log_level
{
LOG_LEVEL_ERROR = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_ERROR,
LOG_LEVEL_INFO = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_DEFAULT,
LOG_LEVEL_VERBOSE = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_VERBOSE
};
private: // log_sink
void on_log_message_va(int level, const char* msg, va_list args)
{ log(level, msg, args); }
private:
void log(int level, const char* message, va_list args);
#define IMPLEMENT_LOG_FUNCTION(suffix,level) \
inline void log_##suffix(const char* message, ...) \
{ va_list args; va_start(args, message); log(level, message, args); }
IMPLEMENT_LOG_FUNCTION(error, LOG_LEVEL_ERROR)
IMPLEMENT_LOG_FUNCTION(verbose, LOG_LEVEL_VERBOSE)
private:
void reset();
static void _capture_thread(baz_rtl_source_c* p);
void capture_thread();
void report_status(int status);
public:
void set_defaults();
bool set_output_format(int size);
void set_status_msgq(gr::msg_queue::sptr queue);
bool create(bool reset_defaults = false);
void destroy();
public: // SWIG demod params set only
inline void set_vid(uint16_t vid)
{ m_demod_params.vid = vid; }
inline void set_pid(uint16_t pid)
{ m_demod_params.pid = pid; }
inline void set_default_timeout(int timeout) // 0: use default, -1: poll only
{ m_demod_params.default_timeout = timeout; }
inline void set_fir_coefficients(const std::vector</*uint8_t*/int>& coeffs)
{ m_demod_params.use_custom_fir_coefficients = !coeffs.empty(); for (size_t i = 0; i < std::min(/*RTL2832_NAMESPACE::demod::*/RTL2832_FIR_COEFF_COUNT, (int)coeffs.size()); ++i) m_demod_params.fir_coeff[i] = (uint8_t)coeffs[i]; }
inline void set_crystal_frequency(uint32_t freq)
{ m_demod_params.crystal_frequency = freq; }
inline void set_tuner_name(const char* name)
{ if (name == NULL) m_demod_params.tuner_name[0] = '\0'; else strncpy(m_demod_params.tuner_name, name, /*RTL2832_NAMESPACE::demod::*/RTL2832_TUNER_NAME_LEN-1); }
public: // SWIG get only
inline size_t recv_samples_per_packet() const
{ return m_recv_samples_per_packet; }
inline uint64_t samples_received() const
{ return m_nSamplesReceived; }
inline uint32_t overflows() const
{ return m_nOverflows; }
inline bool running() const
{ return m_bRunning; }
inline uint32_t buffer_size() const
{ return m_nBufferSize; }
inline uint32_t buffer_times() const
{ return m_nBufferItems; }
inline bool buffering() const
{ return m_bBuffering; }
inline uint32_t read_packet_count() const
{ return m_nReadPacketCount; }
inline uint32_t buffer_overflow_count() const
{ return m_nBufferOverflowCount; }
inline uint32_t buffer_underrun_count() const
{ return m_nBufferUnderrunCount; }
public: // SWIG set (pre-create)
inline void set_relative_gain(bool on = true)
{ m_relative_gain = on; }
inline void set_verbose(bool on = true)
{ m_verbose = on; }
inline void set_read_length(uint32_t length)
{ if (length > 0) m_nReadLength = length; }
inline void set_buffer_multiplier(uint32_t mul)
{ m_nBufferMultiplier = mul; }
inline void set_use_buffer(bool use = true)
{ m_bUseBuffer = use; }
inline void set_buffer_level(float level)
{ m_fBufferLevel = level; }
public: // SWIG get
inline bool relative_gain() const
{ return m_relative_gain; }
inline bool verbose() const
{ return m_verbose; }
inline uint32_t read_length() const
{ return m_nReadLength; }
inline uint32_t buffer_multiplier() const
{ return m_nBufferMultiplier; }
inline bool use_buffer() const
{ return m_bUseBuffer; }
inline float buffer_level() const
{ return m_fBufferLevel; }
public: // SWIG set
bool set_sample_rate(double sample_rate);
bool set_frequency(double freq);
bool set_gain(double gain);
bool set_bandwidth(double bandwidth);
bool set_gain_mode(int mode);
bool set_gain_mode(const char* mode);
bool set_auto_gain_mode(bool on = true);
public: // SWIG get
inline const char* name() const
{ return m_demod.name(); }
inline double sample_rate() const
{ return m_demod.sample_rate(); }
inline RTL2832_NAMESPACE::range_t sample_rate_range() const
{ return m_demod.sample_rate_range(); }
inline double frequency() const
{ return m_demod.active_tuner()->frequency(); }
inline double gain() const
{ return m_demod.active_tuner()->gain(); }
inline double bandwidth() const
{ return m_demod.active_tuner()->bandwidth(); }
inline int gain_mode() const
{ return m_demod.active_tuner()->gain_mode(); }
std::string gain_mode_string() const;
inline bool auto_gain_mode() const
{ return m_demod.active_tuner()->auto_gain_mode(); }
public: // SWIG get: tuner ranges/values
inline RTL2832_NAMESPACE::range_t gain_range() const
{ return m_demod.active_tuner()->gain_range(); }
inline RTL2832_NAMESPACE::values_t gain_values() const
{ return m_demod.active_tuner()->gain_values(); }
inline RTL2832_NAMESPACE::range_t frequency_range() const
{ return m_demod.active_tuner()->frequency_range(); }
inline RTL2832_NAMESPACE::range_t bandwidth_range() const
{ return m_demod.active_tuner()->bandwidth_range(); }
inline RTL2832_NAMESPACE::values_t bandwidth_values() const
{ return m_demod.active_tuner()->bandwidth_values(); }
inline RTL2832_NAMESPACE::num_name_map_t gain_modes() const
{ return m_demod.active_tuner()->gain_modes(); }
inline std::pair<bool,int> calc_appropriate_gain_mode()/* const*/
{ int i; bool b = m_demod.active_tuner()->calc_appropriate_gain_mode(i); return std::make_pair(b,i); }
public: // gr::block overrides
bool start();
bool stop();
public:
int general_work (int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};
#endif /* INCLUDED_BAZ_RTL_SOURCE_C_H */
BAZ_API
#define BAZ_API
Definition api.h:19
baz_make_rtl_source_c
BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation=false, int output_size=0)
Return a shared_ptr to a new instance of baz_rtl_source_c.
IMPLEMENT_LOG_FUNCTION
#define IMPLEMENT_LOG_FUNCTION(suffix, level)
Definition baz_rtl_source_c.h:118
RTL2832_NAMESPACE::demod
Definition rtl2832.h:310
RTL2832_NAMESPACE::demod::sample_rate
double sample_rate() const
Definition rtl2832.h:373
RTL2832_NAMESPACE::demod::active_tuner
tuner * active_tuner() const
Definition rtl2832.h:371
RTL2832_NAMESPACE::demod::sample_rate_range
range_t sample_rate_range() const
Definition rtl2832.h:377
RTL2832_NAMESPACE::demod::name
const char * name() const
RTL2832_NAMESPACE::log_sink
Definition rtl2832.h:104
RTL2832_NAMESPACE::log_sink::level
level
Definition rtl2832.h:107
RTL2832_NAMESPACE::log_sink::LOG_LEVEL_ERROR
@ LOG_LEVEL_ERROR
Definition rtl2832.h:108
RTL2832_NAMESPACE::log_sink::LOG_LEVEL_DEFAULT
@ LOG_LEVEL_DEFAULT
Definition rtl2832.h:109
RTL2832_NAMESPACE::log_sink::LOG_LEVEL_VERBOSE
@ LOG_LEVEL_VERBOSE
Definition rtl2832.h:110
RTL2832_NAMESPACE::tuner::gain_range
virtual range_t gain_range() const =0
RTL2832_NAMESPACE::tuner::calc_appropriate_gain_mode
virtual bool calc_appropriate_gain_mode(int &mode)=0
RTL2832_NAMESPACE::tuner::bandwidth_values
virtual values_t bandwidth_values() const =0
RTL2832_NAMESPACE::tuner::gain
virtual double gain() const =0
RTL2832_NAMESPACE::tuner::auto_gain_mode
virtual bool auto_gain_mode() const =0
RTL2832_NAMESPACE::tuner::frequency_range
virtual range_t frequency_range() const =0
RTL2832_NAMESPACE::tuner::bandwidth_range
virtual range_t bandwidth_range() const =0
RTL2832_NAMESPACE::tuner::gain_modes
virtual num_name_map_t gain_modes() const =0
RTL2832_NAMESPACE::tuner::gain_mode
gain_mode
Definition rtl2832.h:149
RTL2832_NAMESPACE::tuner::gain_values
virtual values_t gain_values() const =0
RTL2832_NAMESPACE::tuner::frequency
virtual double frequency() const =0
RTL2832_NAMESPACE::tuner::bandwidth
virtual double bandwidth() const =0
baz_rtl_source_c
capture samples from an RTL2832-based device.
Definition baz_rtl_source_c.h:69
baz_rtl_source_c::use_buffer
bool use_buffer() const
Definition baz_rtl_source_c.h:190
baz_rtl_source_c::relative_gain
bool relative_gain() const
Definition baz_rtl_source_c.h:182
baz_rtl_source_c::buffer_size
uint32_t buffer_size() const
Definition baz_rtl_source_c.h:156
baz_rtl_source_c::set_buffer_multiplier
void set_buffer_multiplier(uint32_t mul)
Definition baz_rtl_source_c.h:175
baz_rtl_source_c::set_status_msgq
void set_status_msgq(gr::msg_queue::sptr queue)
baz_rtl_source_c::set_gain
bool set_gain(double gain)
baz_rtl_source_c::set_vid
void set_vid(uint16_t vid)
Definition baz_rtl_source_c.h:135
baz_rtl_source_c::set_verbose
void set_verbose(bool on=true)
Definition baz_rtl_source_c.h:171
baz_rtl_source_c::set_fir_coefficients
void set_fir_coefficients(const std::vector< int > &coeffs)
Definition baz_rtl_source_c.h:141
baz_rtl_source_c::gain_modes
RTL2832_NAMESPACE::num_name_map_t gain_modes() const
Definition baz_rtl_source_c.h:231
baz_rtl_source_c::running
bool running() const
Definition baz_rtl_source_c.h:154
baz_rtl_source_c::set_auto_gain_mode
bool set_auto_gain_mode(bool on=true)
baz_rtl_source_c::samples_received
uint64_t samples_received() const
Definition baz_rtl_source_c.h:150
baz_rtl_source_c::read_packet_count
uint32_t read_packet_count() const
Definition baz_rtl_source_c.h:162
baz_rtl_source_c::bandwidth_values
RTL2832_NAMESPACE::values_t bandwidth_values() const
Definition baz_rtl_source_c.h:229
baz_rtl_source_c::bandwidth_range
RTL2832_NAMESPACE::range_t bandwidth_range() const
Definition baz_rtl_source_c.h:227
baz_rtl_source_c::start
bool start()
baz_rtl_source_c::general_work
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
baz_rtl_source_c::overflows
uint32_t overflows() const
Definition baz_rtl_source_c.h:152
baz_rtl_source_c::verbose
bool verbose() const
Definition baz_rtl_source_c.h:184
baz_rtl_source_c::buffering
bool buffering() const
Definition baz_rtl_source_c.h:160
baz_rtl_source_c::set_relative_gain
void set_relative_gain(bool on=true)
Definition baz_rtl_source_c.h:169
baz_rtl_source_c::set_gain_mode
bool set_gain_mode(int mode)
baz_rtl_source_c::bandwidth
double bandwidth() const
Definition baz_rtl_source_c.h:213
baz_rtl_source_c::buffer_level
float buffer_level() const
Definition baz_rtl_source_c.h:192
baz_rtl_source_c::gain_mode_string
std::string gain_mode_string() const
baz_rtl_source_c::buffer_multiplier
uint32_t buffer_multiplier() const
Definition baz_rtl_source_c.h:188
baz_rtl_source_c::set_default_timeout
void set_default_timeout(int timeout)
Definition baz_rtl_source_c.h:139
baz_rtl_source_c::set_read_length
void set_read_length(uint32_t length)
Definition baz_rtl_source_c.h:173
baz_rtl_source_c::frequency
double frequency() const
Definition baz_rtl_source_c.h:209
baz_rtl_source_c::create
bool create(bool reset_defaults=false)
baz_rtl_source_c::name
const char * name() const
Definition baz_rtl_source_c.h:203
baz_rtl_source_c::recv_samples_per_packet
size_t recv_samples_per_packet() const
Definition baz_rtl_source_c.h:148
baz_rtl_source_c::gain_values
RTL2832_NAMESPACE::values_t gain_values() const
Definition baz_rtl_source_c.h:223
baz_rtl_source_c::set_bandwidth
bool set_bandwidth(double bandwidth)
baz_rtl_source_c::buffer_times
uint32_t buffer_times() const
Definition baz_rtl_source_c.h:158
baz_rtl_source_c::buffer_overflow_count
uint32_t buffer_overflow_count() const
Definition baz_rtl_source_c.h:164
baz_rtl_source_c::auto_gain_mode
bool auto_gain_mode() const
Definition baz_rtl_source_c.h:218
baz_rtl_source_c::set_tuner_name
void set_tuner_name(const char *name)
Definition baz_rtl_source_c.h:145
baz_rtl_source_c::destroy
void destroy()
baz_rtl_source_c::sample_rate_range
RTL2832_NAMESPACE::range_t sample_rate_range() const
Definition baz_rtl_source_c.h:207
baz_rtl_source_c::set_gain_mode
bool set_gain_mode(const char *mode)
baz_rtl_source_c::set_frequency
bool set_frequency(double freq)
baz_rtl_source_c::set_pid
void set_pid(uint16_t pid)
Definition baz_rtl_source_c.h:137
baz_rtl_source_c::set_use_buffer
void set_use_buffer(bool use=true)
Definition baz_rtl_source_c.h:177
baz_rtl_source_c::stop
bool stop()
baz_rtl_source_c::sample_rate
double sample_rate() const
Definition baz_rtl_source_c.h:205
baz_rtl_source_c::gain_mode
int gain_mode() const
Definition baz_rtl_source_c.h:215
baz_rtl_source_c::calc_appropriate_gain_mode
std::pair< bool, int > calc_appropriate_gain_mode()
Definition baz_rtl_source_c.h:233
baz_rtl_source_c::set_crystal_frequency
void set_crystal_frequency(uint32_t freq)
Definition baz_rtl_source_c.h:143
baz_rtl_source_c::set_sample_rate
bool set_sample_rate(double sample_rate)
baz_rtl_source_c::buffer_underrun_count
uint32_t buffer_underrun_count() const
Definition baz_rtl_source_c.h:166
baz_rtl_source_c::~baz_rtl_source_c
~baz_rtl_source_c()
baz_rtl_source_c::read_length
uint32_t read_length() const
Definition baz_rtl_source_c.h:186
baz_rtl_source_c::gain_range
RTL2832_NAMESPACE::range_t gain_range() const
Definition baz_rtl_source_c.h:221
baz_rtl_source_c::set_defaults
void set_defaults()
baz_rtl_source_c::set_buffer_level
void set_buffer_level(float level)
Definition baz_rtl_source_c.h:179
baz_rtl_source_c::set_output_format
bool set_output_format(int size)
baz_rtl_source_c::baz_make_rtl_source_c
friend BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation, int output_size)
Return a shared_ptr to a new instance of baz_rtl_source_c.
baz_rtl_source_c::gain
double gain() const
Definition baz_rtl_source_c.h:211
baz_rtl_source_c::frequency_range
RTL2832_NAMESPACE::range_t frequency_range() const
Definition baz_rtl_source_c.h:225
config.h
rtl2832.h
RTL2832_TUNER_NAME_LEN
#define RTL2832_TUNER_NAME_LEN
Definition rtl2832.h:307
RTL2832_FIR_COEFF_COUNT
#define RTL2832_FIR_COEFF_COUNT
Definition rtl2832.h:306