GNU Radio's OsmoSDR Package
hackrf_sink_c.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
4  *
5  * GNU Radio is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3, or (at your option)
8  * any later version.
9  *
10  * GNU Radio is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNU Radio; see the file COPYING. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifndef INCLUDED_HACKRF_SINK_C_H
21 #define INCLUDED_HACKRF_SINK_C_H
22 
23 #include <gnuradio/thread/thread.h>
24 #include <gnuradio/sync_block.h>
25 
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition_variable.hpp>
28 
29 #include <libhackrf/hackrf.h>
30 
31 #include "sink_iface.h"
32 
33 class hackrf_sink_c;
34 
35 typedef struct circular_buffer
36 {
37  void *buffer; // data buffer
38  void *buffer_end; // end of data buffer
39  size_t capacity; // maximum number of items in the buffer
40  size_t count; // number of items in the buffer
41  size_t sz; // size of each item in the buffer
42  void *head; // pointer to head
43  void *tail; // pointer to tail
45 
46 /*
47  * We use boost::shared_ptr's instead of raw pointers for all access
48  * to gr::blocks (and many other data structures). The shared_ptr gets
49  * us transparent reference counting, which greatly simplifies storage
50  * management issues. This is especially helpful in our hybrid
51  * C++ / Python system.
52  *
53  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
54  *
55  * As a convention, the _sptr suffix indicates a boost::shared_ptr
56  */
57 typedef boost::shared_ptr<hackrf_sink_c> hackrf_sink_c_sptr;
58 
59 /*!
60  * \brief Return a shared_ptr to a new instance of hackrf_sink_c.
61  *
62  * To avoid accidental use of raw pointers, hackrf_sink_c's
63  * constructor is private. make_hackrf_sink_c is the public
64  * interface for creating new instances.
65  */
66 hackrf_sink_c_sptr make_hackrf_sink_c (const std::string & args = "");
67 
69  public gr::sync_block,
70  public sink_iface
71 {
72 private:
73  // The friend declaration allows hackrf_make_sink_c to
74  // access the private constructor.
75  friend hackrf_sink_c_sptr make_hackrf_sink_c (const std::string & args);
76 
77  hackrf_sink_c (const std::string & args); // private constructor
78 
79 public:
80  ~hackrf_sink_c (); // public destructor
81 
82  bool start();
83  bool stop();
84 
85  int work( int noutput_items,
86  gr_vector_const_void_star &input_items,
87  gr_vector_void_star &output_items );
88 
89  static std::vector< std::string > get_devices();
90 
91  size_t get_num_channels( void );
92 
94  double set_sample_rate( double rate );
95  double get_sample_rate( void );
96 
97  osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
98  double set_center_freq( double freq, size_t chan = 0 );
99  double get_center_freq( size_t chan = 0 );
100  double set_freq_corr( double ppm, size_t chan = 0 );
101  double get_freq_corr( size_t chan = 0 );
102 
103  std::vector<std::string> get_gain_names( size_t chan = 0 );
104  osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
105  osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
106  bool set_gain_mode( bool automatic, size_t chan = 0 );
107  bool get_gain_mode( size_t chan = 0 );
108  double set_gain( double gain, size_t chan = 0 );
109  double set_gain( double gain, const std::string & name, size_t chan = 0 );
110  double get_gain( size_t chan = 0 );
111  double get_gain( const std::string & name, size_t chan = 0 );
112 
113  double set_if_gain( double gain, size_t chan = 0 );
114  double set_bb_gain( double gain, size_t chan = 0 );
115 
116  std::vector< std::string > get_antennas( size_t chan = 0 );
117  std::string set_antenna( const std::string & antenna, size_t chan = 0 );
118  std::string get_antenna( size_t chan = 0 );
119 
120  double set_bandwidth( double bandwidth, size_t chan = 0 );
121  double get_bandwidth( size_t chan = 0 );
122  osmosdr::freq_range_t get_bandwidth_range( size_t chan = 0 );
123 
124 private:
125  static int _hackrf_tx_callback(hackrf_transfer* transfer);
126  int hackrf_tx_callback(unsigned char *buffer, uint32_t length);
127  static void _hackrf_wait(hackrf_sink_c *obj);
128  void hackrf_wait();
129 
130  static int _usage;
131  static boost::mutex _usage_mutex;
132 
133  hackrf_device *_dev;
134 // gr::thread::thread _thread;
135 
136  circular_buffer_t _cbuf;
137  char *_buf;
138  unsigned int _buf_num;
139  unsigned int _buf_used;
140  boost::mutex _buf_mutex;
141  boost::condition_variable _buf_cond;
142 
143  double _sample_rate;
144  double _center_freq;
145  double _freq_corr;
146  bool _auto_gain;
147  double _amp_gain;
148  double _vga_gain;
149  double _bandwidth;
150 };
151 
152 #endif /* INCLUDED_HACKRF_SINK_C_H */
size_t capacity
Definition: hackrf_sink_c.h:39
std::string set_antenna(const std::string &antenna, size_t chan=0)
double get_center_freq(size_t chan=0)
Definition: hackrf_sink_c.h:35
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
void * buffer_end
Definition: hackrf_sink_c.h:38
size_t get_num_channels(void)
std::vector< std::string > get_gain_names(size_t chan=0)
double get_gain(size_t chan=0)
bool get_gain_mode(size_t chan=0)
double get_freq_corr(size_t chan=0)
void * buffer
Definition: hackrf_sink_c.h:37
Definition: ranges.h:69
Definition: sink_iface.h:31
size_t count
Definition: hackrf_sink_c.h:40
bool set_gain_mode(bool automatic, size_t chan=0)
double get_sample_rate(void)
friend hackrf_sink_c_sptr make_hackrf_sink_c(const std::string &args)
Return a shared_ptr to a new instance of hackrf_sink_c.
struct circular_buffer circular_buffer_t
double set_center_freq(double freq, size_t chan=0)
osmosdr::meta_range_t get_sample_rates(void)
double set_sample_rate(double rate)
void * tail
Definition: hackrf_sink_c.h:43
double set_gain(double gain, size_t chan=0)
size_t sz
Definition: hackrf_sink_c.h:41
hackrf_sink_c_sptr make_hackrf_sink_c(const std::string &args="")
Return a shared_ptr to a new instance of hackrf_sink_c.
double set_bandwidth(double bandwidth, size_t chan=0)
std::string get_antenna(size_t chan=0)
double set_bb_gain(double gain, size_t chan=0)
double set_if_gain(double gain, size_t chan=0)
double get_bandwidth(size_t chan=0)
osmosdr::gain_range_t get_gain_range(size_t chan=0)
osmosdr::freq_range_t get_freq_range(size_t chan=0)
static std::vector< std::string > get_devices()
std::vector< std::string > get_antennas(size_t chan=0)
void * head
Definition: hackrf_sink_c.h:42
Definition: hackrf_sink_c.h:68
double set_freq_corr(double ppm, size_t chan=0)