GNU Radio's OsmoSDR Package
hackrf_source_c.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 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_HACKRF_SOURCE_C_H
23 #define INCLUDED_HACKRF_SOURCE_C_H
24 
25 #include <gnuradio/thread/thread.h>
26 #include <gnuradio/sync_block.h>
27 
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 
31 #include <libhackrf/hackrf.h>
32 
33 #include "source_iface.h"
34 
35 class hackrf_source_c;
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<hackrf_source_c> hackrf_source_c_sptr;
49 
50 /*!
51  * \brief Return a shared_ptr to a new instance of hackrf_source_c.
52  *
53  * To avoid accidental use of raw pointers, hackrf_source_c's
54  * constructor is private. make_hackrf_source_c is the public
55  * interface for creating new instances.
56  */
57 hackrf_source_c_sptr make_hackrf_source_c (const std::string & args = "");
58 
59 /*!
60  * \brief Provides a stream of complex samples.
61  * \ingroup block
62  */
64  public gr::sync_block,
65  public source_iface
66 {
67 private:
68  // The friend declaration allows make_hackrf_source_c to
69  // access the private constructor.
70 
71  friend hackrf_source_c_sptr make_hackrf_source_c (const std::string & args);
72 
73  /*!
74  * \brief Provides a stream of complex samples.
75  */
76  hackrf_source_c (const std::string & args); // private constructor
77 
78 public:
79  ~hackrf_source_c (); // public destructor
80 
81  bool start();
82  bool stop();
83 
84  int work( int noutput_items,
85  gr_vector_const_void_star &input_items,
86  gr_vector_void_star &output_items );
87 
88  static std::vector< std::string > get_devices();
89 
90  size_t get_num_channels( void );
91 
93  double set_sample_rate( double rate );
94  double get_sample_rate( void );
95 
96  osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
97  double set_center_freq( double freq, size_t chan = 0 );
98  double get_center_freq( size_t chan = 0 );
99  double set_freq_corr( double ppm, size_t chan = 0 );
100  double get_freq_corr( size_t chan = 0 );
101 
102  std::vector<std::string> get_gain_names( size_t chan = 0 );
103  osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
104  osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
105  bool set_gain_mode( bool automatic, size_t chan = 0 );
106  bool get_gain_mode( size_t chan = 0 );
107  double set_gain( double gain, size_t chan = 0 );
108  double set_gain( double gain, const std::string & name, size_t chan = 0 );
109  double get_gain( size_t chan = 0 );
110  double get_gain( const std::string & name, size_t chan = 0 );
111 
112  double set_if_gain( double gain, size_t chan = 0 );
113  double set_bb_gain( double gain, size_t chan = 0 );
114 
115  std::vector< std::string > get_antennas( size_t chan = 0 );
116  std::string set_antenna( const std::string & antenna, size_t chan = 0 );
117  std::string get_antenna( size_t chan = 0 );
118 
119  double set_bandwidth( double bandwidth, size_t chan = 0 );
120  double get_bandwidth( size_t chan = 0 );
121  osmosdr::freq_range_t get_bandwidth_range( size_t chan = 0 );
122 
123 private:
124  static int _hackrf_rx_callback(hackrf_transfer* transfer);
125  int hackrf_rx_callback(unsigned char *buf, uint32_t len);
126  static void _hackrf_wait(hackrf_source_c *obj);
127  void hackrf_wait();
128 
129  static int _usage;
130  static boost::mutex _usage_mutex;
131 
132  std::vector<gr_complex> _lut;
133 
134  hackrf_device *_dev;
135  gr::thread::thread _thread;
136  unsigned short **_buf;
137  unsigned int _buf_num;
138  unsigned int _buf_len;
139  unsigned int _buf_head;
140  unsigned int _buf_used;
141  boost::mutex _buf_mutex;
142  boost::condition_variable _buf_cond;
143 
144  unsigned int _buf_offset;
145  int _samp_avail;
146 
147  double _sample_rate;
148  double _center_freq;
149  double _freq_corr;
150  bool _auto_gain;
151  double _amp_gain;
152  double _lna_gain;
153  double _vga_gain;
154  double _bandwidth;
155 };
156 
157 #endif /* INCLUDED_HACKRF_SOURCE_C_H */
std::vector< std::string > get_gain_names(size_t chan=0)
static std::vector< std::string > get_devices()
Definition: source_iface.h:31
hackrf_source_c_sptr make_hackrf_source_c(const std::string &args="")
Return a shared_ptr to a new instance of hackrf_source_c.
std::string set_antenna(const std::string &antenna, size_t chan=0)
double get_freq_corr(size_t chan=0)
double get_bandwidth(size_t chan=0)
osmosdr::gain_range_t get_gain_range(size_t chan=0)
Definition: ranges.h:69
osmosdr::freq_range_t get_freq_range(size_t chan=0)
friend hackrf_source_c_sptr make_hackrf_source_c(const std::string &args)
Return a shared_ptr to a new instance of hackrf_source_c.
double set_bandwidth(double bandwidth, size_t chan=0)
double set_sample_rate(double rate)
bool set_gain_mode(bool automatic, size_t chan=0)
std::vector< std::string > get_antennas(size_t chan=0)
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
double get_gain(size_t chan=0)
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_sample_rate(void)
double set_gain(double gain, size_t chan=0)
bool get_gain_mode(size_t chan=0)
size_t get_num_channels(void)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
double get_center_freq(size_t chan=0)
Provides a stream of complex samples.
Definition: hackrf_source_c.h:63
double set_center_freq(double freq, size_t chan=0)
double set_bb_gain(double gain, size_t chan=0)
double set_if_gain(double gain, size_t chan=0)