1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
4  * Copyright 2020 Clayton Smith <argilo@gmail.com>
5  *
6  * gr-osmosdr is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * gr-osmosdr is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with gr-osmosdr; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 #ifndef INCLUDED_HACKRF_SOURCE_C_H
22 #define INCLUDED_HACKRF_SOURCE_C_H
23 
24 #include <gnuradio/sync_block.h>
25 
26 #include <condition_variable>
27 #include <mutex>
28 
29 #include <libhackrf/hackrf.h>
30 
31 #include "source_iface.h"
32 #include "hackrf_common.h"
33 
34 class hackrf_source_c;
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<hackrf_source_c> hackrf_source_c_sptr;
48 
49 /*!
50  * \brief Return a shared_ptr to a new instance of hackrf_source_c.
51  *
52  * To avoid accidental use of raw pointers, hackrf_source_c's
53  * constructor is private.  make_hackrf_source_c is the public
54  * interface for creating new instances.
55  */
56 hackrf_source_c_sptr make_hackrf_source_c (const std::string & args = "");
57 
58 /*!
59  * \brief Provides a stream of complex samples.
60  * \ingroup block
61  */
62 class hackrf_source_c :
63     public gr::sync_block,
64     public source_iface,
65     protected hackrf_common
66 {
67 private:
68   // The friend declaration allows make_hackrf_source_c to
69   // access the private constructor.
70   friend hackrf_source_c_sptr make_hackrf_source_c (const std::string & args);
71 
72   /*!
73    * \brief Provides a stream of complex samples.
74    */
75   hackrf_source_c (const std::string & args);  	// private constructor
76 
77 public:
78   ~hackrf_source_c (); 	// public destructor
79 
80   bool start();
81   bool stop();
82 
83   int work( int noutput_items,
84             gr_vector_const_void_star &input_items,
85             gr_vector_void_star &output_items );
86 
87   static std::vector< std::string > get_devices();
88 
89   size_t get_num_channels( void );
90 
91   osmosdr::meta_range_t get_sample_rates( void );
92   double set_sample_rate( double rate );
93   double get_sample_rate( void );
94 
95   osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
96   double set_center_freq( double freq, size_t chan = 0 );
97   double get_center_freq( size_t chan = 0 );
98   double set_freq_corr( double ppm, size_t chan = 0 );
99   double get_freq_corr( size_t chan = 0 );
100 
101   std::vector<std::string> get_gain_names( size_t chan = 0 );
102   osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
103   osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
104   bool set_gain_mode( bool automatic, size_t chan = 0 );
105   bool get_gain_mode( size_t chan = 0 );
106   double set_gain( double gain, size_t chan = 0 );
107   double set_gain( double gain, const std::string & name, size_t chan = 0 );
108   double get_gain( size_t chan = 0 );
109   double get_gain( const std::string & name, size_t chan = 0 );
110 
111   double set_if_gain( double gain, size_t chan = 0 );
112   double set_bb_gain( double gain, size_t chan = 0 );
113 
114   std::vector< std::string > get_antennas( size_t chan = 0 );
115   std::string set_antenna( const std::string & antenna, size_t chan = 0 );
116   std::string get_antenna( size_t chan = 0 );
117 
118   double set_bandwidth( double bandwidth, size_t chan = 0 );
119   double get_bandwidth( size_t chan = 0 );
120   osmosdr::freq_range_t get_bandwidth_range( size_t chan = 0 );
121 
122 private:
123   static int _hackrf_rx_callback(hackrf_transfer* transfer);
124   int hackrf_rx_callback(unsigned char *buf, uint32_t len);
125 
126   std::vector<float> _lut;
127 
128   unsigned char **_buf;
129   unsigned int _buf_num;
130   unsigned int _buf_len;
131   unsigned int _buf_head;
132   unsigned int _buf_used;
133   std::mutex _buf_mutex;
134   std::condition_variable _buf_cond;
135 
136   unsigned int _buf_offset;
137   int _samp_avail;
138 
139   double _lna_gain;
140   double _vga_gain;
141 };
142 
143 #endif /* INCLUDED_HACKRF_SOURCE_C_H */
144