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