USRP_Server  2.0
A flexible, GPU-accelerated radio-frequency readout software.
USRP_hardware_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef USRP_HW_MANAGER
3 #define USRP_HW_MANAGER
4 
8 #include <uhd/types/time_spec.hpp>
9 
10 
13  public:
14 
15  //internally stored usrp number
17 
18  //determine if the hardware has to be replaced by a software loop
19  bool sw_loop;
20 
21  //address of the device controlled by this instance
22  uhd::usrp::multi_usrp::sptr main_usrp;
23 
26  hardware_manager(server_settings* settings, bool sw_loop_init, size_t usrp_number = 0);
27 
30  bool preset_usrp(usrp_param* requested_config);
31 
33  rx_queue* A_RX_queue;
34 
36  tx_queue* A_TX_queue;
37 
39  rx_queue* B_RX_queue;
40 
42  tx_queue* B_TX_queue;
43 
46  bool check_rx_status(bool verbose = false);
47 
49  bool check_A_rx_status(bool verbose = false);
50 
52  bool check_B_rx_status(bool verbose = false);
53 
56  bool check_tx_status(bool verbose = false);
57 
59  bool check_A_tx_status(bool verbose = false);
60 
62  bool check_B_tx_status(bool verbose = false);
63 
68  void start_tx(
69  threading_condition* wait_condition, //before joining wait for that condition
70  int thread_op, //core affinity of the process
71  param *current_settings, //representative of the paramenters (must match A or B frontend description)
72  char front_end, //must be "A" or "B"
73  preallocator<float2>* memory = NULL //if the thread is transmitting a buffer that requires dynamical allocation than a pointer to custo memory manager class has to be passed.
74 
75  );
76 
78  void start_rx(
79  int buffer_len, //length of the buffer. MUST be the same of the preallocator initialization
80  threading_condition* wait_condition, //before joining wait for that condition
81  preallocator<float2>* memory, //custom memory preallocator
82  int thread_op, //core affinity number
83  param *current_settings, //representative of the paramenters (must match A or B frontend description)
84  char front_end //must be "A" or "B"
85 
86  );
87 
89  void close_tx();
90 
92  void close_rx();
93 
95  int clean_tx_queue(tx_queue* TX_queue,preallocator<float2>* memory);
96 
98  int clean_rx_queue(rx_queue* RX_queue, preallocator<float2>* memory);
99 
100  std::atomic<bool> B_rx_thread_operation;
101  std::atomic<bool> A_rx_thread_operation;
102  private:
103 
105  bool tx_loaded_cmd_A;
106 
108  bool tx_loaded_cmd_B;
109 
111  bool rx_loaded_cmd_A;
112 
114  bool rx_loaded_cmd_B;
115 
116  void apply(usrp_param* requested_config);
117 
118  bool check_tuning();
119 
120  //the next variables will be called by the tx and rx functions to stream and receive packets.
121  //they are managed inside this class as I suspect the frequent reinitialization of the streaming causes
122  //the DAC sync error.
123  uhd::rx_streamer::sptr A_rx_stream;
124  uhd::tx_streamer::sptr A_tx_stream;
125  uhd::rx_streamer::sptr B_rx_stream;
126  uhd::tx_streamer::sptr B_tx_stream;
127 
128  void set_streams();
129 
130  void flush_rx_streamer(uhd::rx_streamer::sptr &rx_streamer);
131 
132 
133  boost::thread* A_rx_thread;
134 
135  boost::thread* B_rx_thread;
136 
137  //pointer to tx thread and boolean chk variable
138  std::atomic<bool> A_tx_thread_operation;
139  boost::thread* A_tx_thread;
140  std::atomic<bool> B_tx_thread_operation;
141  boost::thread* B_tx_thread;
142 
143  //queue for sharing the error event code with RX thread
144  error_queue* A_tx_error_queue;
145  error_queue* B_tx_error_queue;
146 
147  //kind of device to look for
148  uhd::device_addr_t hint;
149 
150  //array of usrp addresses
151  uhd::device_addrs_t dev_addrs;
152 
153  //last configuration of the usrp device
154  usrp_param config;
155 
156  //pointer to the software loop queue
157  tx_queue* A_sw_loop_queue;
158  tx_queue* B_sw_loop_queue;
159 
160  //channel vector. is 1 unit long because each channel has its own streamer
161  //used in set_streams() method.
162  std::vector<size_t> channel_num;
163 
164 
165  //port-already-connected map: in initialization no port is connected and no thrad is started
166  ant_mode A_TXRX_chk;
167  ant_mode B_RX2_chk;
168  ant_mode B_TXRX_chk;
169  ant_mode A_RX2_chk;
170 
171  //clear the stream before setting a new configuration
172  //TODO this can potentially cause a memory leak every time the channel configuration is changed.
173  void clear_streams();
174 
175 
176  char front_end_code0;
177  char front_end_code1;
178 
179  //apply the single antenna configuration
180  // NOTE: only changes parameters when needed
181  //returns eventual diagnostic messages
182  std::string apply_antenna_config(param *parameters, param *old_parameters, size_t chan);
183 
184  //check if there are more than 1 tx/rx channel
185  bool check_double_txrx(ant_mode TXRX);
186 
187 
188  //check if the selected mode has to be tuned
189  bool check_global_mode_presence(ant_mode mode, size_t chan);
190 
191  void software_tx_thread(
192  param *current_settings, //some parameters are useful also in sw
193  preallocator<float2>* memory, //custom memory preallocator
194  tx_queue* TX_queue,
195  tx_queue* sw_loop_queue,
196  char front_end
197  );
198 
199  void single_tx_thread(
200  param *current_settings, //(managed internally to the class) user parameter to use for rx setting
201  threading_condition* wait_condition, //before joining wait for that condition
202  tx_queue* TX_queue,
203  uhd::tx_streamer::sptr &tx_stream, //asscociated usrp stream
204  preallocator<float2>* memory, //custom memory preallocator
205  char front_end
206  );
207 
208  //ment to be in a thread. receive messages asyncronously on metadata
209  void async_stream(uhd::tx_streamer::sptr &tx_stream, char fron_tend);
210 
211  void software_rx_thread(
212  param *current_settings,
213  preallocator<float2>* memory,
214  rx_queue* Rx_queue,
215  tx_queue* sw_loop_queue,
216  char front_end
217  );
218 
219 
220  void single_rx_thread(
221  param *current_settings, //(managed internally) user parameter to use for rx setting
222 
223  rx_queue* Rx_queue, //(managed internally)queue to use for pushing
224  threading_condition* wait_condition, //before joining wait for that condition
225  preallocator<float2>* memory, //custom memory preallocator
226  uhd::rx_streamer::sptr &rx_stream, //associated usrp streamer
227  char front_end
228 
229  );
230 };
231 
232 #endif
std::atomic< bool > B_rx_thread_operation
void start_rx(int buffer_len, threading_condition *wait_condition, preallocator< float2 > *memory, int thread_op, param *current_settings, char front_end)
@ brief start a rx thread.
bool check_tx_status(bool verbose=false)
Check the status of every tx operations. Returns the status of A or B.
bool check_rx_status(bool verbose=false)
Check the status of every rx operations. Returns the status of A or B.
std::atomic< bool > A_rx_thread_operation
bool check_A_rx_status(bool verbose=false)
Check the status of A rx operations.
int clean_rx_queue(rx_queue *RX_queue, preallocator< float2 > *memory)
Release the memory associated with pointers holded by a rx queue using the respective memory allocato...
bool check_B_tx_status(bool verbose=false)
Check the status of B tx operations.
tx_queue * B_TX_queue
Queue accessed to stream data from B frontend.
tx_queue * A_TX_queue
Queue accessed to stream data from A frontend.
hardware_manager(server_settings *settings, bool sw_loop_init, size_t usrp_number=0)
The initializer of the class can be used to select which usrp is controlled by the class Default call...
int clean_tx_queue(tx_queue *TX_queue, preallocator< float2 > *memory)
Release the memory associated with pointers holded by a tx queue using the respective memory allocato...
bool preset_usrp(usrp_param *requested_config)
Set the USRP device with user parameters.
bool check_B_rx_status(bool verbose=false)
Check the status of B rx operations.
void close_tx()
Close all the tx streamer threads.
void start_tx(threading_condition *wait_condition, int thread_op, param *current_settings, char front_end, preallocator< float2 > *memory=NULL)
Start a transmission thread. The threads started by this function do two things: pop a packet from th...
void close_rx()
Close all the rx streamer threads.
bool check_A_tx_status(bool verbose=false)
Check the status of A tx operations.
rx_queue * A_RX_queue
Queue accessed to retrive data from A frontend.
Manages the hardware I/O of one usrp unit.
uhd::usrp::multi_usrp::sptr main_usrp
rx_queue * B_RX_queue
Queue accessed to retrive data from B frontend.