GNU Radio's TEST Package
spyserver_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_SPYSERVER_SOURCE_C_H
23 #define INCLUDED_SPYSERVER_SOURCE_C_H
24 
25 #include <thread>
26 #include <atomic>
27 #include <boost/circular_buffer.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 
31 #include <gnuradio/sync_block.h>
32 
33 #include "source_iface.h"
34 #include "spyserver_protocol.h"
35 #include "tcp_client.h"
36 
37 class spyserver_source_c;
38 
39 /*
40  * We use boost::shared_ptr's instead of raw pointers for all access
41  * to gr::blocks (and many other data structures). The shared_ptr gets
42  * us transparent reference counting, which greatly simplifies storage
43  * management issues. This is especially helpful in our hybrid
44  * C++ / Python system.
45  *
46  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
47  *
48  * As a convention, the _sptr suffix indicates a boost::shared_ptr
49  */
50 typedef std::shared_ptr<spyserver_source_c> spyserver_source_c_sptr;
51 
52 /*!
53  * \brief Return a shared_ptr to a new instance of spyserver_source_c.
54  *
55  * To avoid accidental use of raw pointers, spyserver_source_c's
56  * constructor is private. make_spyserver_source_c is the public
57  * interface for creating new instances.
58  */
59 spyserver_source_c_sptr make_spyserver_source_c (const std::string & args = "");
60 
61 /*!
62  * \brief Provides a stream of complex samples.
63  * \ingroup block
64  */
66  public gr::sync_block,
67  public source_iface
68 {
69 private:
70  // The friend declaration allows make_spyserver_source_c to
71  // access the private constructor.
72 
73  friend spyserver_source_c_sptr make_spyserver_source_c (const std::string & args);
74 
75  /*!
76  * \brief Provides a stream of complex samples.
77  */
78  spyserver_source_c (const std::string & args); // private constructor
79 
80 
81 public:
82  ~spyserver_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  double set_lna_gain( double gain, size_t chan = 0 );
116  double set_mix_gain(double gain, size_t chan = 0 );
117  double set_if_gain( double gain, size_t chan = 0 );
118  double set_bb_gain( double gain, size_t chan = 0 ) { return set_mix_gain(gain, chan); };
119 
120  std::vector< std::string > get_antennas( size_t chan = 0 );
121  std::string set_antenna( const std::string & antenna, size_t chan = 0 );
122  std::string get_antenna( size_t chan = 0 );
123 
124  double set_bandwidth( double bandwidth, size_t chan = 0 );
125  double get_bandwidth( size_t chan = 0 );
126  osmosdr::freq_range_t get_bandwidth_range( size_t chan = 0 );
127 
128 private:
129  static constexpr unsigned int BufferSize = 64 * 1024;
130  const uint32_t ProtocolVersion = SPYSERVER_PROTOCOL_VERSION;
131  const std::string SoftwareID = std::string("gr-osmosdr");
132  const std::string NameNoDevice = std::string("SpyServer - No Device");
133  const std::string NameAirspyOne = std::string("SpyServer - Airspy One");
134  const std::string NameAirspyHF = std::string("SpyServer - Airspy HF+");
135  const std::string NameRTLSDR = std::string("SpyServer - RTLSDR");
136  const std::string NameUnknown = std::string("SpyServer - Unknown Device");
137 
138  uint32_t minimum_tunable_frequency;
139  uint32_t maximum_tunable_frequency;
140  uint32_t device_center_frequency;
141  uint32_t channel_center_frequency;
142  uint32_t channel_decimation_stage_count;
143  tcp_client client;
144 
145  void connect();
146  void disconnect();
147  void thread_loop();
148  bool say_hello();
149  void cleanup();
150  void on_connect();
151 
152  bool set_setting(uint32_t settingType, std::vector<uint32_t> params);
153  bool send_command(uint32_t cmd, std::vector<uint8_t> args);
154  void parse_message(char *buffer, uint32_t len);
155  int parse_header(char *buffer, uint32_t len);
156  int parse_body(char *buffer, uint32_t len);
157  void process_device_info();
158  void process_client_sync();
159  void process_uint8_samples();
160  void process_int16_samples();
161  void process_float_samples();
162  void process_uint8_fft();
163  void handle_new_message();
164  void set_stream_state();
165 
166  std::atomic_bool terminated;
167  std::atomic_bool streaming;
168  std::atomic_bool got_device_info;
169  std::atomic_bool got_sync_info;
170  std::atomic_bool can_control;
171  std::atomic_bool is_connected;
172  std::thread *receiver_thread;
173 
174  uint32_t dropped_buffers;
175  std::atomic<int64_t> down_stream_bytes;
176 
177  uint8_t *header_data;
178  uint8_t *body_buffer;
179  uint64_t body_buffer_length;
180  uint32_t parser_position;
181  uint32_t last_sequence_number;
182 
183  std::string ip;
184  int port;
185 
186  DeviceInfo device_info;
187  MessageHeader header;
188 
189  uint32_t streaming_mode;
190  uint32_t parser_phase;
191 
192  boost::circular_buffer<gr_complex> *_fifo;
193  boost::mutex _fifo_lock;
194  boost::condition_variable _samp_avail;
195 
196  std::vector< std::pair<double, uint32_t> > _sample_rates;
197  double _sample_rate;
198  double _center_freq;
199  double _gain;
200  double _digitalGain;
201 };
202 
203 #endif /* INCLUDED_SPYSERVER_SOURCE_C_H */
std::vector< std::string > get_gain_names(size_t chan=0)
double set_bb_gain(double gain, size_t chan=0)
Definition: spyserver_source_c.h:118
friend spyserver_source_c_sptr make_spyserver_source_c(const std::string &args)
Return a shared_ptr to a new instance of spyserver_source_c.
Definition: source_iface.h:32
Definition: spyserver_protocol.h:135
double set_lna_gain(double gain, size_t chan=0)
spyserver_source_c_sptr make_spyserver_source_c(const std::string &args="")
Return a shared_ptr to a new instance of spyserver_source_c.
double get_gain(size_t chan=0)
double get_freq_corr(size_t chan=0)
double set_gain(double gain, size_t chan=0)
double set_if_gain(double gain, size_t chan=0)
std::vector< std::string > get_antennas(size_t chan=0)
double set_bandwidth(double bandwidth, size_t chan=0)
double set_mix_gain(double gain, size_t chan=0)
Definition: ranges.h:75
Provides a stream of complex samples.
Definition: spyserver_source_c.h:65
double get_center_freq(size_t chan=0)
double set_sample_rate(double rate)
static std::vector< std::string > get_devices(bool fake=false)
double get_bandwidth(size_t chan=0)
size_t get_num_channels(void)
double set_freq_corr(double ppm, size_t chan=0)
osmosdr::gain_range_t get_gain_range(size_t chan=0)
std::string get_antenna(size_t chan=0)
Definition: spyserver_protocol.h:126
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
std::string set_antenna(const std::string &antenna, size_t chan=0)
bool get_gain_mode(size_t chan=0)
double get_sample_rate(void)
osmosdr::freq_range_t get_freq_range(size_t chan=0)
bool set_gain_mode(bool automatic, size_t chan=0)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
#define SPYSERVER_PROTOCOL_VERSION
Definition: spyserver_protocol.h:14
osmosdr::meta_range_t get_sample_rates(void)
double set_center_freq(double freq, size_t chan=0)
Definition: tcp_client.h:47