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