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
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 */
50typedef boost::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 */
59spyserver_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{
69private:
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
81public:
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
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 );
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 );
127
128 void set_biast( bool enabled );
129 bool get_biast();
130
131private:
132 static constexpr unsigned int BufferSize = 64 * 1024;
133 const uint32_t ProtocolVersion = SPYSERVER_PROTOCOL_VERSION;
134 const std::string SoftwareID = std::string("gr-osmosdr");
135 const std::string NameNoDevice = std::string("SpyServer - No Device");
136 const std::string NameAirspyOne = std::string("SpyServer - Airspy One");
137 const std::string NameAirspyHF = std::string("SpyServer - Airspy HF+");
138 const std::string NameRTLSDR = std::string("SpyServer - RTLSDR");
139 const std::string NameUnknown = std::string("SpyServer - Unknown Device");
140
141 uint32_t minimum_tunable_frequency;
142 uint32_t maximum_tunable_frequency;
143 uint32_t device_center_frequency;
144 uint32_t channel_center_frequency;
145 uint32_t channel_decimation_stage_count;
146 tcp_client client;
147
148 void connect();
149 void disconnect();
150 void thread_loop();
151 bool say_hello();
152 void cleanup();
153 void on_connect();
154
155 bool set_setting(uint32_t settingType, std::vector<uint32_t> params);
156 bool send_command(uint32_t cmd, std::vector<uint8_t> args);
157 void parse_message(char *buffer, uint32_t len);
158 int parse_header(char *buffer, uint32_t len);
159 int parse_body(char *buffer, uint32_t len);
160 void process_device_info();
161 void process_client_sync();
162 void process_uint8_samples();
163 void process_int16_samples();
164 void process_float_samples();
165 void process_uint8_fft();
166 void handle_new_message();
167 void set_stream_state();
168
169 std::atomic_bool terminated;
170 std::atomic_bool streaming;
171 std::atomic_bool got_device_info;
172 std::atomic_bool got_sync_info;
173 std::atomic_bool can_control;
174 std::atomic_bool is_connected;
175 std::thread *receiver_thread;
176
177 uint32_t dropped_buffers;
178 std::atomic<int64_t> down_stream_bytes;
179
180 uint8_t *header_data;
181 uint8_t *body_buffer;
182 uint64_t body_buffer_length;
183 uint32_t parser_position;
184 uint32_t last_sequence_number;
185
186 std::string ip;
187 int port;
188
189 DeviceInfo device_info;
190 MessageHeader header;
191
192 uint32_t streaming_mode;
193 uint32_t parser_phase;
194
195 boost::circular_buffer<gr_complex> *_fifo;
196 boost::mutex _fifo_lock;
197 boost::condition_variable _samp_avail;
198
199 std::vector< std::pair<double, uint32_t> > _sample_rates;
200 double _sample_rate;
201 double _center_freq;
202 double _gain;
203 double _digitalGain;
204};
205
206#endif /* INCLUDED_SPYSERVER_SOURCE_C_H */
Definition: source_iface.h:33
Provides a stream of complex samples.
Definition: spyserver_source_c.h:68
double get_center_freq(size_t chan=0)
bool set_gain_mode(bool automatic, size_t chan=0)
static std::vector< std::string > get_devices(bool fake=false)
osmosdr::meta_range_t get_sample_rates(void)
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.
osmosdr::gain_range_t get_gain_range(const std::string &name, size_t chan=0)
double set_center_freq(double freq, size_t chan=0)
void set_biast(bool enabled)
double get_bandwidth(size_t chan=0)
int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
std::vector< std::string > get_antennas(size_t chan=0)
double set_lna_gain(double gain, size_t chan=0)
std::vector< std::string > get_gain_names(size_t chan=0)
double set_gain(double gain, size_t chan=0)
double set_mix_gain(double gain, size_t chan=0)
double get_freq_corr(size_t chan=0)
double set_gain(double gain, const std::string &name, size_t chan=0)
double set_bandwidth(double bandwidth, size_t chan=0)
osmosdr::freq_range_t get_freq_range(size_t chan=0)
double get_sample_rate(void)
double set_bb_gain(double gain, size_t chan=0)
Definition: spyserver_source_c.h:118
bool get_gain_mode(size_t chan=0)
double set_if_gain(double gain, size_t chan=0)
osmosdr::freq_range_t get_bandwidth_range(size_t chan=0)
double set_sample_rate(double rate)
double get_gain(const std::string &name, size_t chan=0)
std::string set_antenna(const std::string &antenna, size_t chan=0)
size_t get_num_channels(void)
double get_gain(size_t chan=0)
osmosdr::gain_range_t get_gain_range(size_t chan=0)
std::string get_antenna(size_t chan=0)
double set_freq_corr(double ppm, size_t chan=0)
Definition: tcp_client.h:47
#define SPYSERVER_PROTOCOL_VERSION
Definition: spyserver_protocol.h:14
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: spyserver_protocol.h:136
Definition: spyserver_protocol.h:127
Definition: ranges.h:75