GNU Radio's OsmoSDR Package
rtl_source_c.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
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 #ifndef INCLUDED_RTLSDR_SOURCE_C_H
23 #define INCLUDED_RTLSDR_SOURCE_C_H
24 
25 #include <gnuradio/sync_block.h>
26 
27 #include <gnuradio/thread/thread.h>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 
31 #include "source_iface.h"
32 
33 class rtl_source_c;
34 typedef struct rtlsdr_dev rtlsdr_dev_t;
35 
36 /*
37  * We use boost::shared_ptr's instead of raw pointers for all access
38  * to gr::blocks (and many other data structures). The shared_ptr gets
39  * us transparent reference counting, which greatly simplifies storage
40  * management issues. This is especially helpful in our hybrid
41  * C++ / Python system.
42  *
43  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
44  *
45  * As a convention, the _sptr suffix indicates a boost::shared_ptr
46  */
47 typedef boost::shared_ptr<rtl_source_c> rtl_source_c_sptr;
48 
49 /*!
50  * \brief Return a shared_ptr to a new instance of rtl_source_c.
51  *
52  * To avoid accidental use of raw pointers, rtl_source_c's
53  * constructor is private. make_rtl_source_c is the public
54  * interface for creating new instances.
55  */
56 rtl_source_c_sptr make_rtl_source_c (const std::string & args = "");
57 
58 /*!
59  * \brief Provides a stream of complex samples.
60  * \ingroup block
61  *
62  */
63 class rtl_source_c :
64  public gr::sync_block,
65  public source_iface
66 {
67 private:
68  // The friend declaration allows make_rtl_source_c to
69  // access the private constructor.
70 
71  friend rtl_source_c_sptr make_rtl_source_c (const std::string & args);
72 
73  /*!
74  * \brief Provides a stream of complex samples.
75  */
76  rtl_source_c (const std::string & args); // private constructor
77 
78 public:
79  ~rtl_source_c (); // public destructor
80 
81  int work( int noutput_items,
82  gr_vector_const_void_star &input_items,
83  gr_vector_void_star &output_items );
84 
85  static std::vector< std::string > get_devices();
86 
87  size_t get_num_channels( void );
88 
90  double set_sample_rate( double rate );
91  double get_sample_rate( void );
92 
93  osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
94  double set_center_freq( double freq, size_t chan = 0 );
95  double get_center_freq( size_t chan = 0 );
96  double set_freq_corr( double ppm, size_t chan = 0 );
97  double get_freq_corr( size_t chan = 0 );
98 
99  std::vector<std::string> get_gain_names( size_t chan = 0 );
100  osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
101  osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
102  bool set_gain_mode( bool automatic, size_t chan = 0 );
103  bool get_gain_mode( size_t chan = 0 );
104  double set_gain( double gain, size_t chan = 0 );
105  double set_gain( double gain, const std::string & name, size_t chan = 0 );
106  double get_gain( size_t chan = 0 );
107  double get_gain( const std::string & name, size_t chan = 0 );
108 
109  double set_if_gain( double gain, size_t chan = 0 );
110 
111  std::vector< std::string > get_antennas( size_t chan = 0 );
112  std::string set_antenna( const std::string & antenna, size_t chan = 0 );
113  std::string get_antenna( size_t chan = 0 );
114 
115 protected:
116  bool start();
117  bool stop();
118 
119 private:
120  static void _rtlsdr_callback(unsigned char *buf, uint32_t len, void *ctx);
121  void rtlsdr_callback(unsigned char *buf, uint32_t len);
122  static void _rtlsdr_wait(rtl_source_c *obj);
123  void rtlsdr_wait();
124 
125  std::vector<gr_complex> _lut;
126 
127  rtlsdr_dev_t *_dev;
128  gr::thread::thread _thread;
129  unsigned short **_buf;
130  unsigned int _buf_num;
131  unsigned int _buf_len;
132  unsigned int _buf_head;
133  unsigned int _buf_used;
134  boost::mutex _buf_mutex;
135  boost::condition_variable _buf_cond;
136  bool _running;
137 
138  unsigned int _buf_offset;
139  int _samp_avail;
140 
141  bool _no_tuner;
142  bool _auto_gain;
143  double _if_gain;
144  unsigned int _skipped;
145 };
146 
147 #endif /* INCLUDED_RTLSDR_SOURCE_C_H */
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
Definition: source_iface.h:31
Provides a stream of complex samples.
Definition: rtl_source_c.h:63
double set_gain(double gain, size_t chan=0)
double set_sample_rate(double rate)
static std::vector< std::string > get_devices()
double set_center_freq(double freq, size_t chan=0)
std::vector< std::string > get_gain_names(size_t chan=0)
osmosdr::gain_range_t get_gain_range(size_t chan=0)
Definition: ranges.h:69
bool get_gain_mode(size_t chan=0)
bool set_gain_mode(bool automatic, size_t chan=0)
size_t get_num_channels(void)
double set_if_gain(double gain, size_t chan=0)
std::string set_antenna(const std::string &antenna, size_t chan=0)
std::vector< std::string > get_antennas(size_t chan=0)
double get_sample_rate(void)
double get_freq_corr(size_t chan=0)
rtl_source_c_sptr make_rtl_source_c(const std::string &args="")
Return a shared_ptr to a new instance of rtl_source_c.
osmosdr::meta_range_t get_sample_rates(void)
double set_freq_corr(double ppm, size_t chan=0)
std::string get_antenna(size_t chan=0)
double get_center_freq(size_t chan=0)
osmosdr::freq_range_t get_freq_range(size_t chan=0)
double get_gain(size_t chan=0)
friend rtl_source_c_sptr make_rtl_source_c(const std::string &args)
Return a shared_ptr to a new instance of rtl_source_c.
struct rtlsdr_dev rtlsdr_dev_t
Definition: rtl_source_c.h:34