|
gr-baz Package
|
00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2004,2013 Free Software Foundation, Inc. 00004 * 00005 * This file is part of GNU Radio 00006 * 00007 * GNU Radio is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3, or (at your option) 00010 * any later version. 00011 * 00012 * GNU Radio is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with GNU Radio; see the file COPYING. If not, write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 /* 00024 * gr-baz by Balint Seeber (http://spench.net/contact) 00025 * Information, documentation & samples: http://wiki.spench.net/wiki/gr-baz 00026 */ 00027 00028 #ifndef INCLUDED_BAZ_RTL_SOURCE_C_H 00029 #define INCLUDED_BAZ_RTL_SOURCE_C_H 00030 00031 #ifdef HAVE_CONFIG_H 00032 #include "config.h" 00033 #endif 00034 00035 #include <gr_block.h> 00036 #include <gr_msg_queue.h> 00037 00038 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) 00039 #define NOMINMAX 00040 #endif 00041 00042 #include <boost/thread.hpp> 00043 #include <boost/thread/condition.hpp> 00044 #include <boost/thread/recursive_mutex.hpp> 00045 #ifdef HAVE_XTIME 00046 #include <boost/thread/xtime.hpp> 00047 #endif // HAVE_XTIME 00048 00049 #include <libusb-1.0/libusb.h> // FIXME: Automake 00050 #include <stdarg.h> // va_list 00051 00052 #include "rtl2832.h" 00053 00054 class BAZ_API baz_rtl_source_c; 00055 typedef boost::shared_ptr<baz_rtl_source_c> baz_rtl_source_c_sptr; 00056 00057 /*! 00058 * \brief Return a shared_ptr to a new instance of baz_rtl_source_c. 00059 */ 00060 BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation = false, int output_size = 0); 00061 00062 /*! 00063 * \brief capture samples from an RTL2832-based device. 00064 * \ingroup block 00065 * 00066 * \sa gr-baz: http://wiki.spench.net/wiki/gr-baz 00067 */ 00068 class BAZ_API baz_rtl_source_c : public gr_block, public RTL2832_NAMESPACE::log_sink 00069 { 00070 private: 00071 friend BAZ_API baz_rtl_source_c_sptr baz_make_rtl_source_c(bool defer_creation, int output_size); 00072 private: 00073 baz_rtl_source_c(bool defer_creation = false, int output_size = 0); 00074 public: 00075 ~baz_rtl_source_c(); 00076 private: 00077 RTL2832_NAMESPACE::demod m_demod; 00078 size_t m_recv_samples_per_packet; 00079 uint64_t m_nSamplesReceived; 00080 uint32_t m_nOverflows; 00081 bool m_bRunning; 00082 boost::recursive_mutex d_mutex; 00083 boost::thread m_pCaptureThread; 00084 uint32_t m_nBufferSize; 00085 uint32_t m_nBufferStart; 00086 uint32_t m_nBufferItems; 00087 boost::condition m_hPacketEvent; 00088 uint8_t* m_pUSBBuffer; 00089 bool m_bBuffering; 00090 uint32_t m_nReadLength; 00091 uint32_t m_nBufferMultiplier; 00092 bool m_bUseBuffer; 00093 float m_fBufferLevel; 00094 uint32_t m_nReadPacketCount; 00095 uint32_t m_nBufferOverflowCount; 00096 uint32_t m_nBufferUnderrunCount; 00097 #ifdef HAVE_XTIME 00098 boost::xtime m_wait_delay, m_wait_next; 00099 #endif // HAVE_XTIME 00100 private: 00101 RTL2832_NAMESPACE::demod::PARAMS m_demod_params; 00102 bool m_verbose; 00103 bool m_relative_gain; 00104 int m_output_size; 00105 gr_msg_queue_sptr m_status_queue; 00106 private: 00107 enum log_level 00108 { 00109 LOG_LEVEL_ERROR = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_ERROR, 00110 LOG_LEVEL_INFO = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_DEFAULT, 00111 LOG_LEVEL_VERBOSE = RTL2832_NAMESPACE::log_sink::LOG_LEVEL_VERBOSE 00112 }; 00113 private: // log_sink 00114 void on_log_message_va(int level, const char* msg, va_list args) 00115 { log(level, msg, args); } 00116 private: 00117 void log(int level, const char* message, va_list args); 00118 #define IMPLEMENT_LOG_FUNCTION(suffix,level) \ 00119 inline void log_##suffix(const char* message, ...) \ 00120 { va_list args; va_start(args, message); log(level, message, args); } 00121 IMPLEMENT_LOG_FUNCTION(error, LOG_LEVEL_ERROR) 00122 IMPLEMENT_LOG_FUNCTION(verbose, LOG_LEVEL_VERBOSE) 00123 private: 00124 void reset(); 00125 static void _capture_thread(baz_rtl_source_c* p); 00126 void capture_thread(); 00127 void report_status(int status); 00128 public: 00129 void set_defaults(); 00130 bool set_output_format(int size); 00131 void set_status_msgq(gr_msg_queue_sptr queue); 00132 bool create(bool reset_defaults = false); 00133 void destroy(); 00134 public: // SWIG demod params set only 00135 inline void set_vid(uint16_t vid) 00136 { m_demod_params.vid = vid; } 00137 inline void set_pid(uint16_t pid) 00138 { m_demod_params.pid = pid; } 00139 inline void set_default_timeout(int timeout) // 0: use default, -1: poll only 00140 { m_demod_params.default_timeout = timeout; } 00141 inline void set_fir_coefficients(const std::vector</*uint8_t*/int>& coeffs) 00142 { 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]; } 00143 inline void set_crystal_frequency(uint32_t freq) 00144 { m_demod_params.crystal_frequency = freq; } 00145 inline void set_tuner_name(const char* name) 00146 { 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); } 00147 public: // SWIG get only 00148 inline size_t recv_samples_per_packet() const 00149 { return m_recv_samples_per_packet; } 00150 inline uint64_t samples_received() const 00151 { return m_nSamplesReceived; } 00152 inline uint32_t overflows() const 00153 { return m_nOverflows; } 00154 inline bool running() const 00155 { return m_bRunning; } 00156 inline uint32_t buffer_size() const 00157 { return m_nBufferSize; } 00158 inline uint32_t buffer_times() const 00159 { return m_nBufferItems; } 00160 inline bool buffering() const 00161 { return m_bBuffering; } 00162 inline uint32_t read_packet_count() const 00163 { return m_nReadPacketCount; } 00164 inline uint32_t buffer_overflow_count() const 00165 { return m_nBufferOverflowCount; } 00166 inline uint32_t buffer_underrun_count() const 00167 { return m_nBufferUnderrunCount; } 00168 public: // SWIG set (pre-create) 00169 inline void set_relative_gain(bool on = true) 00170 { m_relative_gain = on; } 00171 inline void set_verbose(bool on = true) 00172 { m_verbose = on; } 00173 inline void set_read_length(uint32_t length) 00174 { if (length > 0) m_nReadLength = length; } 00175 inline void set_buffer_multiplier(uint32_t mul) 00176 { m_nBufferMultiplier = mul; } 00177 inline void set_use_buffer(bool use = true) 00178 { m_bUseBuffer = use; } 00179 inline void set_buffer_level(float level) 00180 { m_fBufferLevel = level; } 00181 public: // SWIG get 00182 inline bool relative_gain() const 00183 { return m_relative_gain; } 00184 inline bool verbose() const 00185 { return m_verbose; } 00186 inline uint32_t read_length() const 00187 { return m_nReadLength; } 00188 inline uint32_t buffer_multiplier() const 00189 { return m_nBufferMultiplier; } 00190 inline bool use_buffer() const 00191 { return m_bUseBuffer; } 00192 inline float buffer_level() const 00193 { return m_fBufferLevel; } 00194 public: // SWIG set 00195 bool set_sample_rate(double sample_rate); 00196 bool set_frequency(double freq); 00197 bool set_gain(double gain); 00198 bool set_bandwidth(double bandwidth); 00199 bool set_gain_mode(int mode); 00200 bool set_gain_mode(const char* mode); 00201 bool set_auto_gain_mode(bool on = true); 00202 public: // SWIG get 00203 inline const char* name() const 00204 { return m_demod.name(); } 00205 inline double sample_rate() const 00206 { return m_demod.sample_rate(); } 00207 inline RTL2832_NAMESPACE::range_t sample_rate_range() const 00208 { return m_demod.sample_rate_range(); } 00209 inline double frequency() const 00210 { return m_demod.active_tuner()->frequency(); } 00211 inline double gain() const 00212 { return m_demod.active_tuner()->gain(); } 00213 inline double bandwidth() const 00214 { return m_demod.active_tuner()->bandwidth(); } 00215 inline int gain_mode() const 00216 { return m_demod.active_tuner()->gain_mode(); } 00217 std::string gain_mode_string() const; 00218 inline bool auto_gain_mode() const 00219 { return m_demod.active_tuner()->auto_gain_mode(); } 00220 public: // SWIG get: tuner ranges/values 00221 inline RTL2832_NAMESPACE::range_t gain_range() const 00222 { return m_demod.active_tuner()->gain_range(); } 00223 inline RTL2832_NAMESPACE::values_t gain_values() const 00224 { return m_demod.active_tuner()->gain_values(); } 00225 inline RTL2832_NAMESPACE::range_t frequency_range() const 00226 { return m_demod.active_tuner()->frequency_range(); } 00227 inline RTL2832_NAMESPACE::range_t bandwidth_range() const 00228 { return m_demod.active_tuner()->bandwidth_range(); } 00229 inline RTL2832_NAMESPACE::values_t bandwidth_values() const 00230 { return m_demod.active_tuner()->bandwidth_values(); } 00231 inline RTL2832_NAMESPACE::num_name_map_t gain_modes() const 00232 { return m_demod.active_tuner()->gain_modes(); } 00233 inline std::pair<bool,int> calc_appropriate_gain_mode()/* const*/ 00234 { int i; bool b = m_demod.active_tuner()->calc_appropriate_gain_mode(i); return std::make_pair(b,i); } 00235 public: // gr_block overrides 00236 bool start(); 00237 bool stop(); 00238 public: 00239 int general_work (int noutput_items, 00240 gr_vector_int &ninput_items, 00241 gr_vector_const_void_star &input_items, 00242 gr_vector_void_star &output_items); 00243 }; 00244 00245 #endif /* INCLUDED_BAZ_RTL_SOURCE_C_H */