GNU Radio's TEST Package
rfspace_source_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_RFSPACE_SOURCE_C_H
21 #define INCLUDED_RFSPACE_SOURCE_C_H
22 
23 //#define USE_ASIO
24 
25 #ifdef USE_ASIO
26 #include <boost/asio.hpp>
27 #endif
28 #include <gnuradio/thread/thread.h>
29 #include <gnuradio/block.h>
30 #include <gnuradio/sync_block.h>
31 
32 #include <boost/circular_buffer.hpp>
33 
34 #include <mutex>
35 #include <condition_variable>
36 
37 #include "osmosdr/ranges.h"
38 #include "source_iface.h"
39 #ifdef USE_ASIO
40 using boost::asio::ip::tcp;
41 using boost::asio::ip::udp;
42 #endif
43 class rfspace_source_c;
44 
45 #ifndef SOCKET
46 #define SOCKET int
47 #endif
48 
49 /*
50  * We use boost::shared_ptr's instead of raw pointers for all access
51  * to gr_blocks (and many other data structures). The shared_ptr gets
52  * us transparent reference counting, which greatly simplifies storage
53  * management issues. This is especially helpful in our hybrid
54  * C++ / Python system.
55  *
56  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
57  *
58  * As a convention, the _sptr suffix indicates a boost::shared_ptr
59  */
60 typedef boost::shared_ptr<rfspace_source_c> rfspace_source_c_sptr;
61 
62 /*!
63  * \brief Return a shared_ptr to a new instance of rfspace_source_c.
64  *
65  * To avoid accidental use of raw pointers, rfspace_source_c's
66  * constructor is private. rfspace_make_source_c is the public
67  * interface for creating new instances.
68  */
69 rfspace_source_c_sptr make_rfspace_source_c (const std::string & args = "");
70 
72  public gr::sync_block,
73  public source_iface
74 {
75 private:
76  // The friend declaration allows rfspace_make_source_c to
77  // access the private constructor.
78  friend rfspace_source_c_sptr make_rfspace_source_c (const std::string & args);
79 
80  rfspace_source_c (const std::string & args); // private constructor
81 
82 public:
83  ~rfspace_source_c (); // public destructor
84 
85  bool start();
86  bool stop();
87 
88  int work(int noutput_items,
89  gr_vector_const_void_star &input_items,
90  gr_vector_void_star &output_items );
91 
92  static std::vector< std::string > get_devices( bool fake = false );
93 
94  size_t get_num_channels( void );
95 
97  double set_sample_rate( double rate );
98  double get_sample_rate( void );
99 
100  osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
101  double set_center_freq( double freq, size_t chan = 0 );
102  double get_center_freq( size_t chan = 0 );
103  double set_freq_corr( double ppm, size_t chan = 0 );
104  double get_freq_corr( size_t chan = 0 );
105 
106  std::vector<std::string> get_gain_names( size_t chan = 0 );
107  osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
108  osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
109  bool set_gain_mode( bool automatic, size_t chan = 0 );
110  bool get_gain_mode( size_t chan = 0 );
111  double set_gain( double gain, size_t chan = 0 );
112  double set_gain( double gain, const std::string & name, size_t chan = 0 );
113  double get_gain( size_t chan = 0 );
114  double get_gain( const std::string & name, 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: /* functions */
125  void apply_channel( unsigned char *cmd, size_t chan = 0 );
126 
127  bool transaction( const unsigned char *cmd, size_t size );
128 
129  bool transaction( const unsigned char *cmd, size_t size,
130  std::vector< unsigned char > &response );
131 
132  void usb_read_task();
133  void tcp_keepalive_task();
134 
135 private: /* members */
136  enum radio_type
137  {
138  RADIO_UNKNOWN = 0,
139  RFSPACE_SDR_IQ,
140  RFSPACE_SDR_IP,
141  RFSPACE_NETSDR,
142  RFSPACE_CLOUDIQ
143  };
144 
145  radio_type _radio;
146 
147 #ifdef USE_ASIO
148  boost::asio::io_service _io_service;
149  tcp::resolver _resolver;
150  tcp::socket _t;
151  udp::socket _u;
152 #else
153  SOCKET _tcp;
154  SOCKET _udp;
155 #endif
156  int _usb;
157  bool _running;
158  bool _keep_running;
159  uint16_t _sequence;
160 
161  size_t _nchan;
162  double _sample_rate;
163  double _bandwidth;
164 
165  gr::thread::thread _thread;
166  bool _run_usb_read_task;
167  bool _run_tcp_keepalive_task;
168  std::mutex _tcp_lock;
169 
170  boost::circular_buffer<gr_complex> *_fifo;
171  std::mutex _fifo_lock;
172  std::condition_variable _samp_avail;
173 
174  std::vector< unsigned char > _resp;
175  std::mutex _resp_lock;
176  std::condition_variable _resp_avail;
177 };
178 
179 #endif /* INCLUDED_RFSPACE_SOURCE_C_H */
double set_bandwidth(double bandwidth, size_t chan=0)
static std::vector< std::string > get_devices(bool fake=false)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
Definition: source_iface.h:32
std::string get_antenna(size_t chan=0)
std::string set_antenna(const std::string &antenna, size_t chan=0)
osmosdr::freq_range_t get_freq_range(size_t chan=0)
#define SOCKET
Definition: rfspace_source_c.h:46
osmosdr::gain_range_t get_gain_range(size_t chan=0)
Definition: ranges.h:75
size_t get_num_channels(void)
bool get_gain_mode(size_t chan=0)
double set_sample_rate(double rate)
double get_center_freq(size_t chan=0)
rfspace_source_c_sptr make_rfspace_source_c(const std::string &args="")
Return a shared_ptr to a new instance of rfspace_source_c.
friend rfspace_source_c_sptr make_rfspace_source_c(const std::string &args)
Return a shared_ptr to a new instance of rfspace_source_c.
double set_gain(double gain, size_t chan=0)
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
bool set_gain_mode(bool automatic, size_t chan=0)
Definition: rfspace_source_c.h:71
std::vector< std::string > get_antennas(size_t chan=0)
std::vector< std::string > get_gain_names(size_t chan=0)
double set_freq_corr(double ppm, size_t chan=0)
osmosdr::meta_range_t get_sample_rates(void)
double get_gain(size_t chan=0)
double get_sample_rate(void)
double set_center_freq(double freq, size_t chan=0)
double get_freq_corr(size_t chan=0)
double get_bandwidth(size_t chan=0)