gr-baz Package

baz_rtl_source_c.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2004,2013 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23/*
24 * gr-baz by Balint Seeber (http://spench.net/contact)
25 * Information, documentation & samples: http://wiki.spench.net/wiki/gr-baz
26 */
27
28#ifndef INCLUDED_BAZ_RTL_SOURCE_C_H
29#define INCLUDED_BAZ_RTL_SOURCE_C_H
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <gnuradio/block.h>
36#include <gnuradio/msg_queue.h>
37
38#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
39#define NOMINMAX
40#endif
41
42#include <boost/thread.hpp>
43#include <boost/thread/condition.hpp>
44#include <boost/thread/recursive_mutex.hpp>
45#ifdef HAVE_XTIME
46#include <boost/thread/xtime.hpp>
47#endif // HAVE_XTIME
48
49#include <libusb-1.0/libusb.h> // FIXME: Automake
50#include <stdarg.h> // va_list
51
52#include "rtl2832.h"
53
54 class BAZ_API baz_rtl_source_c;
55 typedef boost::shared_ptr<baz_rtl_source_c> baz_rtl_source_c_sptr;
56
57/*!
58 * \brief Return a shared_ptr to a new instance of baz_rtl_source_c.
59 */
60 BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation = false, int output_size = 0);
61
62/*!
63 * \brief capture samples from an RTL2832-based device.
64 * \ingroup block
65 *
66 * \sa gr-baz: http://wiki.spench.net/wiki/gr-baz
67 */
68 class BAZ_API baz_rtl_source_c : public gr::block, public RTL2832_NAMESPACE::log_sink
69{
70 private:
71 friend BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation, int output_size);
72 private:
73 baz_rtl_source_c(bool defer_creation = false, int output_size = 0);
74 public:
75 ~baz_rtl_source_c();
76 private:
77 RTL2832_NAMESPACE::demod m_demod;
78 size_t m_recv_samples_per_packet;
79 uint64_t m_nSamplesReceived;
80 uint32_t m_nOverflows;
81 bool m_bRunning;
82 boost::recursive_mutex d_mutex;
83 boost::thread m_pCaptureThread;
84 uint32_t m_nBufferSize;
85 uint32_t m_nBufferStart;
86 uint32_t m_nBufferItems;
87 boost::condition m_hPacketEvent;
88 uint8_t* m_pUSBBuffer;
89 bool m_bBuffering;
90 uint32_t m_nReadLength;
91 uint32_t m_nBufferMultiplier;
92 bool m_bUseBuffer;
93 float m_fBufferLevel;
94 uint32_t m_nReadPacketCount;
95 uint32_t m_nBufferOverflowCount;
96 uint32_t m_nBufferUnderrunCount;
97#ifdef HAVE_XTIME
98 boost::xtime m_wait_delay, m_wait_next;
99#endif // HAVE_XTIME
100 private:
101 RTL2832_NAMESPACE::demod::PARAMS m_demod_params;
102 bool m_verbose;
103 bool m_relative_gain;
104 int m_output_size;
105 gr::msg_queue::sptr m_status_queue;
106 private:
107 enum log_level
108 {
109 LOG_LEVEL_ERROR = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_ERROR,
110 LOG_LEVEL_INFO = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_DEFAULT,
111 LOG_LEVEL_VERBOSE = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_VERBOSE
112 };
113 private: // log_sink
114 void on_log_message_va(int level, const char* msg, va_list args)
115 { log(level, msg, args); }
116 private:
117 void log(int level, const char* message, va_list args);
118#define IMPLEMENT_LOG_FUNCTION(suffix,level) \
119 inline void log_##suffix(const char* message, ...) \
120 { va_list args; va_start(args, message); log(level, message, args); }
121 IMPLEMENT_LOG_FUNCTION(error, LOG_LEVEL_ERROR)
122 IMPLEMENT_LOG_FUNCTION(verbose, LOG_LEVEL_VERBOSE)
123 private:
124 void reset();
125 static void _capture_thread(baz_rtl_source_c* p);
126 void capture_thread();
127 void report_status(int status);
128 public:
129 void set_defaults();
130 bool set_output_format(int size);
131 void set_status_msgq(gr::msg_queue::sptr queue);
132 bool create(bool reset_defaults = false);
133 void destroy();
134 public: // SWIG demod params set only
135 inline void set_vid(uint16_t vid)
136 { m_demod_params.vid = vid; }
137 inline void set_pid(uint16_t pid)
138 { m_demod_params.pid = pid; }
139 inline void set_default_timeout(int timeout) // 0: use default, -1: poll only
140 { m_demod_params.default_timeout = timeout; }
141 inline void set_fir_coefficients(const std::vector</*uint8_t*/int>& coeffs)
142 { 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]; }
143 inline void set_crystal_frequency(uint32_t freq)
144 { m_demod_params.crystal_frequency = freq; }
145 inline void set_tuner_name(const char* name)
146 { 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); }
147 public: // SWIG get only
148 inline size_t recv_samples_per_packet() const
149 { return m_recv_samples_per_packet; }
150 inline uint64_t samples_received() const
151 { return m_nSamplesReceived; }
152 inline uint32_t overflows() const
153 { return m_nOverflows; }
154 inline bool running() const
155 { return m_bRunning; }
156 inline uint32_t buffer_size() const
157 { return m_nBufferSize; }
158 inline uint32_t buffer_times() const
159 { return m_nBufferItems; }
160 inline bool buffering() const
161 { return m_bBuffering; }
162 inline uint32_t read_packet_count() const
163 { return m_nReadPacketCount; }
164 inline uint32_t buffer_overflow_count() const
165 { return m_nBufferOverflowCount; }
166 inline uint32_t buffer_underrun_count() const
167 { return m_nBufferUnderrunCount; }
168 public: // SWIG set (pre-create)
169 inline void set_relative_gain(bool on = true)
170 { m_relative_gain = on; }
171 inline void set_verbose(bool on = true)
172 { m_verbose = on; }
173 inline void set_read_length(uint32_t length)
174 { if (length > 0) m_nReadLength = length; }
175 inline void set_buffer_multiplier(uint32_t mul)
176 { m_nBufferMultiplier = mul; }
177 inline void set_use_buffer(bool use = true)
178 { m_bUseBuffer = use; }
179 inline void set_buffer_level(float level)
180 { m_fBufferLevel = level; }
181 public: // SWIG get
182 inline bool relative_gain() const
183 { return m_relative_gain; }
184 inline bool verbose() const
185 { return m_verbose; }
186 inline uint32_t read_length() const
187 { return m_nReadLength; }
188 inline uint32_t buffer_multiplier() const
189 { return m_nBufferMultiplier; }
190 inline bool use_buffer() const
191 { return m_bUseBuffer; }
192 inline float buffer_level() const
193 { return m_fBufferLevel; }
194 public: // SWIG set
195 bool set_sample_rate(double sample_rate);
196 bool set_frequency(double freq);
197 bool set_gain(double gain);
198 bool set_bandwidth(double bandwidth);
199 bool set_gain_mode(int mode);
200 bool set_gain_mode(const char* mode);
201 bool set_auto_gain_mode(bool on = true);
202 public: // SWIG get
203 inline const char* name() const
204 { return m_demod.name(); }
205 inline double sample_rate() const
206 { return m_demod.sample_rate(); }
207 inline RTL2832_NAMESPACE::range_t sample_rate_range() const
208 { return m_demod.sample_rate_range(); }
209 inline double frequency() const
210 { return m_demod.active_tuner()->frequency(); }
211 inline double gain() const
212 { return m_demod.active_tuner()->gain(); }
213 inline double bandwidth() const
214 { return m_demod.active_tuner()->bandwidth(); }
215 inline int gain_mode() const
216 { return m_demod.active_tuner()->gain_mode(); }
217 std::string gain_mode_string() const;
218 inline bool auto_gain_mode() const
219 { return m_demod.active_tuner()->auto_gain_mode(); }
220 public: // SWIG get: tuner ranges/values
221 inline RTL2832_NAMESPACE::range_t gain_range() const
222 { return m_demod.active_tuner()->gain_range(); }
223 inline RTL2832_NAMESPACE::values_t gain_values() const
224 { return m_demod.active_tuner()->gain_values(); }
225 inline RTL2832_NAMESPACE::range_t frequency_range() const
226 { return m_demod.active_tuner()->frequency_range(); }
227 inline RTL2832_NAMESPACE::range_t bandwidth_range() const
228 { return m_demod.active_tuner()->bandwidth_range(); }
229 inline RTL2832_NAMESPACE::values_t bandwidth_values() const
230 { return m_demod.active_tuner()->bandwidth_values(); }
231 inline RTL2832_NAMESPACE::num_name_map_t gain_modes() const
232 { return m_demod.active_tuner()->gain_modes(); }
233 inline std::pair<bool,int> calc_appropriate_gain_mode()/* const*/
234 { int i; bool b = m_demod.active_tuner()->calc_appropriate_gain_mode(i); return std::make_pair(b,i); }
235 public: // gr::block overrides
236 bool start();
237 bool stop();
238 public:
239 int general_work (int noutput_items,
240 gr_vector_int &ninput_items,
241 gr_vector_const_void_star &input_items,
242 gr_vector_void_star &output_items);
243};
244
245#endif /* INCLUDED_BAZ_RTL_SOURCE_C_H */
BAZ_API
#define BAZ_API
Definition: api.h:19
baz_rtl_source_c
class BAZ_API baz_rtl_source_c
Definition: baz_rtl_source_c.h:54
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::log_sink::on_log_message_va
virtual void on_log_message_va(int level, const char *msg, va_list args)=0
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
RTL2832_NAMESPACE::range_t
std::pair< double, double > range_t
Definition: rtl2832.h:84
RTL2832_NAMESPACE::num_name_map_t
std::map< int, std::string > num_name_map_t
Definition: rtl2832.h:86
RTL2832_NAMESPACE::values_t
std::vector< double > values_t
Definition: rtl2832.h:85
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
RTL2832_NAMESPACE::demod::params
Definition: rtl2832.h:318
RTL2832_NAMESPACE::demod::params::vid
uint16_t vid
Definition: rtl2832.h:319
RTL2832_NAMESPACE::demod::params::fir_coeff
uint8_t fir_coeff[RTL2832_FIR_COEFF_COUNT]
Definition: rtl2832.h:325
RTL2832_NAMESPACE::demod::params::crystal_frequency
uint32_t crystal_frequency
Definition: rtl2832.h:328
RTL2832_NAMESPACE::demod::params::tuner_name
char tuner_name[RTL2832_TUNER_NAME_LEN]
Definition: rtl2832.h:329
RTL2832_NAMESPACE::demod::params::use_custom_fir_coefficients
bool use_custom_fir_coefficients
Definition: rtl2832.h:324
RTL2832_NAMESPACE::demod::params::pid
uint16_t pid
Definition: rtl2832.h:320
RTL2832_NAMESPACE::demod::params::default_timeout
int default_timeout
Definition: rtl2832.h:322