USRP_Server  2.0
A flexible, GPU-accelerated radio-frequency readout software.
USRP_server_settings.hpp
Go to the documentation of this file.
1 /* @brief Containd definitions of variables, macros and functions needed for the server basic settings and definitions.
3  *
4  *
5  *
6 */
8 #ifndef USRP_SERVER_SETTING_IMPORTED
9 #define USRP_SERVER_SETTING_IMPORTED
10 
11 #include <sys/prctl.h>
12 #include <fstream>
13 #include <csignal>
14 #include <memory>
15 #include <sys/socket.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <sched.h>
19 #include <iterator>
20 #include <algorithm>
21 #include <chrono>
22 #include <ctime>
23 #include <mutex>
24 #include <pthread.h>
25 #include <thread>
26 #include <assert.h>
27 #include <future>
28 
29 #include <uhd/utils/thread.hpp>
30 #include <uhd/utils/safe_main.hpp>
31 #include <uhd/utils/static.hpp>
32 #include <uhd/exception.hpp>
33 #include <uhd/usrp/multi_usrp.hpp>
34 
35 #include <boost/math/special_functions/round.hpp>
36 #include <boost/format.hpp>
37 #include <boost/lexical_cast.hpp>
38 #include <boost/algorithm/string.hpp>
39 #include <boost/atomic.hpp>
40 #include <boost/filesystem.hpp>
41 #include <boost/asio.hpp>
42 #include <boost/asio/use_future.hpp>
43 #include <boost/array.hpp>
44 #include <boost/lockfree/queue.hpp>
45 #include <boost/lockfree/spsc_queue.hpp>
46 #include <boost/timer/timer.hpp>
47 #include <boost/property_tree/json_parser.hpp>
48 #include <boost/exception/diagnostic_information.hpp>
49 #include <boost/date_time/posix_time/posix_time.hpp>
50 #include <boost/asio/basic_deadline_timer.hpp>
51 #include <boost/thread/thread.hpp>
52 
53 #define BOOST_LOG_DYN_LINK 1
54 #include <boost/log/core.hpp>
55 #include <boost/log/trivial.hpp>
56 #include <boost/log/expressions.hpp>
57 #include <boost/log/sinks/text_file_backend.hpp>
58 #include <boost/log/utility/setup/file.hpp>
59 #include <boost/log/utility/setup/common_attributes.hpp>
60 #include <boost/log/sources/severity_logger.hpp>
61 #include <boost/log/sources/record_ostream.hpp>
62 
63 namespace logging = boost::log;
64 namespace src = boost::log::sources;
65 namespace sinks = boost::log::sinks;
66 namespace keywords = boost::log::keywords;
67 using namespace logging::trivial;
68 
70 
71 #include <cuda_runtime.h>
72 //#include <cufft.h>
73 
74 //offset for LO tuning.
75 #define OFFSET_F 0//100000000
76 
77 //length of the TX and RX queue. for non real time application longer queue are needed to stack up data
78 
79 //this two values increase the ammount of cpu RAM initially allocated. Increasing those values will result in more memory usage.
80 
82 #define RX_QUEUE_LENGTH 100
83 
85 #define TX_QUEUE_LENGTH 100
86 
87 //increasing those values will only increase the limit of RAM that COULD be used.
88 #define ERROR_QUEUE_LENGTH 1000
89 #define STREAM_QUEUE_LENGTH 1000
90 
92 #define SW_LOOP_QUEUE_LENGTH 100
93 #define SECONDARY_STREAM_QUEUE_LENGTH 100 //between the stream and the filewriter (keep it long if writing files)
94 
95 //cut-off frequency of the post-demodulation decimator filter (relative to Nyquist)(deprecated)
96 #define ADDITIONAL_FILTER_FCUT 0.2
97 
98 //buffer safety lengths
99 #define MAX_USEFULL_BUFFER 6000000
100 #define MIN_USEFULL_BUFFER 50000
101 
102 #define DEFAULT_BUFFER_LEN 1000000
103 
105 extern int TCP_SYNC_PORT;
106 
108 extern int TCP_ASYNC_PORT;
109 
110 extern std::string device_arguments;
111 
112 //valid for TX and RX operations, describe the signal generation/demodulation.
113 enum w_type { TONES, CHIRP, NOISE , RAMP, NODSP, SWONLY, DIRECT};
114 
115 std::string w_type_to_str(w_type enumerator);
116 
117 w_type string_to_w_type(std::string input_string);
118 
119 std::vector<w_type> string_to_w_type_vector(std::vector<std::string> string_vector);
120 
121 //state of the USRP antenna
122 enum ant_mode { TX, RX, OFF };
123 
124 std::string ant_mode_to_str(ant_mode enumerator);
125 
126 ant_mode ant_mode_from_string(std::string str);
127 
128 //describe the hardware and software paramenter for a single antenna of the USRP.
129 struct param{
130 
131  //how to use the selected antenna
132  ant_mode mode = OFF;
133 
134  //hardware parameters
135  int rate,gain,bw;
136  size_t tone;
137 
138  //runtime hardware parameters
139  size_t samples;
140  double delay;
141  float burst_on; //time length of the bursts in seconds
142  float burst_off; //time between bursts in seconds
143  size_t buffer_len; //length of the transport buffer (both GPU and USRP). SET to 0 for default.
144  bool tuning_mode; //0 for integer and 1 for fractional
145  //software signal parameters
146  std::vector<int> freq;
147  std::vector<w_type> wave_type;
148  std::vector<float> ampl;
149  size_t decim; //all channels have the same decimation factor
150  std::vector<float> chirp_t;
151  std::vector<int> chirp_f;
152  std::vector<int> swipe_s;
153 
154  size_t data_mem_mult; //memory multiplier added to cope with analysis data expansion.
155 
156  //polyphase filter bank specific
157  int fft_tones; // it is an int because of size_t* incompatible with cufft calls
158  size_t pf_average;
159 
160  //returns the maximum output buffer size (not all samples of that size will be always good)
161  //TODO something's wrong with this function
162  int get_output_buffer_size();
163 
164  //the execution of this measurement, if TX, requres a dynamical memory allocation?
165  bool dynamic_buffer();
166 };
167 
168 //should desctibe the settings for a single USRP
169 //ther is a parameter struct for each antenna
170 struct usrp_param{
171 
172  int usrp_number;
173 
174  param A_TXRX;
175  param B_TXRX;
176  param A_RX2;
177  param B_RX2;
178 
179  //how mny rx or tx to set up
180  int get_number(ant_mode T);
181 
182  bool is_A_active();
183 
184  bool is_B_active();
185 
186 };
187 
188 //contains the general setting to use for the USRP
189 //and the general server settings
190 struct server_settings{
191 
192  //internal or external clock reference
193  std::string clock_reference;
194 
195  //which gpu use for signal processing on this device
196  int GPU_device_index;
197 
198  //defaults buffer lengths
199  int default_rx_buffer_len;
200  int default_tx_buffer_len;
201 
202  //enable TCP streaming
203  bool TCP_streaming;
204 
205  //enable file writing
206  bool FILE_writing;
207 
208  void validate();
209 
210  void autoset();
211 
212 };
213 
214 //wrapping the buffer with some metadata
215 struct RX_wrapper{
216  float2* buffer; //pointer to data content
217  int usrp_number; //identifies the usrp
218  char front_end_code; //specify RF frontend
219  int packet_number; //packet number
220  int length; //length of the buffer
221  int errors; //how many errors occured
222  int channels;
223 };
224 
225 std::string get_front_end_name(char code);
226 
227 //queues used for data communication between data generation/analysis classes and hardware interface class
228 
229 typedef boost::lockfree::queue< RX_wrapper, boost::lockfree::fixed_sized<(bool)true>> rx_queue;
230 typedef boost::lockfree::queue< float2*, boost::lockfree::fixed_sized<(bool)true>> tx_queue;
231 typedef boost::lockfree::queue< int, boost::lockfree::fixed_sized<(bool)true>> error_queue;
232 
234 // One of the main causes of error in the system is the RX/TX processes core switching. The switch instroduces a delay that is not well tollerated by the real-time tasks.
235 void Thread_Prioriry(boost::thread& Thread, int priority, int affinity);
236 
240 void SetThreadName(boost::thread* thread, const char* threadName);
241 #endif
std::string device_arguments
std::string ant_mode_to_str(ant_mode enumerator)
std::string get_front_end_name(char code)
int TCP_SYNC_PORT
void SetThreadName(boost::thread *thread, const char *threadName)
std::string w_type_to_str(w_type enumerator)
void Thread_Prioriry(boost::thread &Thread, int priority, int affinity)
ant_mode ant_mode_from_string(std::string str)
w_type string_to_w_type(std::string input_string)
std::vector< w_type > string_to_w_type_vector(std::vector< std::string > string_vector)
int TCP_ASYNC_PORT