Merged r6329:6428 of features/inband-usb + distcheck fixes into trunk.
[debian/gnuradio] / usrp / host / apps-inband / test_usrp_inband_overrun.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <mb_mblock.h>
27 #include <mb_runtime.h>
28 #include <mb_runtime_nop.h>             // QA only
29 #include <mb_protocol_class.h>
30 #include <mb_exception.h>
31 #include <mb_msg_queue.h>
32 #include <mb_message.h>
33 #include <mb_mblock_impl.h>
34 #include <mb_msg_accepter.h>
35 #include <mb_class_registry.h>
36 #include <pmt.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <iostream>
40 #include <fstream>
41
42 // Include the symbols needed for communication with USRP server
43 #include <symbols_usrp_server_cs.h>
44 #include <symbols_usrp_channel.h>
45 #include <symbols_usrp_low_level_cs.h>
46 #include <symbols_usrp_rx.h>
47
48 static bool verbose = true;
49
50 class test_usrp_rx : public mb_mblock
51 {
52   mb_port_sptr  d_rx;
53   mb_port_sptr  d_cs;
54   pmt_t         d_rx_chan;      // returned tx channel handle
55
56   enum state_t {
57     INIT,
58     OPENING_USRP,
59     ALLOCATING_CHANNEL,
60     RECEIVING,
61     CLOSING_CHANNEL,
62     CLOSING_USRP,
63   };
64
65   state_t       d_state;
66
67   std::ofstream d_ofile;
68
69   long d_n_overruns;
70
71   long d_samples_recvd;
72   long d_samples_to_recv;
73
74  public:
75   test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
76   ~test_usrp_rx();
77   void initial_transition();
78   void handle_message(mb_message_sptr msg);
79
80  protected:
81   void open_usrp();
82   void close_usrp();
83   void allocate_channel();
84   void send_packets();
85   void enter_receiving();
86   void build_and_send_next_frame();
87   void handle_response_recv_raw_samples(pmt_t invocation_handle);
88   void enter_closing_channel();
89 };
90
91 test_usrp_rx::test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
92   : mb_mblock(runtime, instance_name, user_arg),
93     d_n_overruns(0),
94     d_samples_recvd(0),
95     d_samples_to_recv(10e6)
96
97   
98   d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
99   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
100   
101   // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
102   pmt_t usrp_dict = pmt_make_dict();
103
104   // Specify the RBF to use
105   pmt_dict_set(usrp_dict,
106                pmt_intern("rbf"),
107                pmt_intern("nanocell9.rbf"));
108
109   pmt_dict_set(usrp_dict,
110                pmt_intern("decim-rx"),
111                pmt_from_long(128));
112
113   define_component("server", "usrp_server", usrp_dict);
114
115   connect("self", "rx0", "server", "rx0");
116   connect("self", "cs", "server", "cs");
117
118 }
119
120 test_usrp_rx::~test_usrp_rx()
121 {
122 }
123
124 void
125 test_usrp_rx::initial_transition()
126 {
127   open_usrp();
128 }
129
130 void
131 test_usrp_rx::handle_message(mb_message_sptr msg)
132 {
133   pmt_t event = msg->signal();
134   pmt_t data = msg->data();
135
136   pmt_t handle = PMT_F;
137   pmt_t status = PMT_F;
138   std::string error_msg;
139   
140   switch(d_state){
141     
142     //----------------------------- OPENING_USRP ----------------------------//
143     // We only expect a response from opening the USRP which should be succesful
144     // or failed.
145     case OPENING_USRP:
146       if (pmt_eq(event, s_response_open)){
147         status = pmt_nth(1, data);
148         if (pmt_eq(status, PMT_T)){
149           allocate_channel();
150           return;
151         }
152         else {
153           error_msg = "failed to open usrp:";
154           goto bail;
155         }
156       }
157       goto unhandled;
158       
159     //----------------------- ALLOCATING CHANNELS --------------------//
160     // Allocate an RX channel to perform the overrun test.
161     case ALLOCATING_CHANNEL:
162       if (pmt_eq(event, s_response_allocate_channel)){
163         status = pmt_nth(1, data);
164         d_rx_chan = pmt_nth(2, data);
165
166         if (pmt_eq(status, PMT_T)){
167           enter_receiving();
168           return;
169         }
170         else {
171           error_msg = "failed to allocate channel:";
172           goto bail;
173         }
174       }
175       goto unhandled;
176
177     //--------------------------- RECEIVING ------------------------------//
178     // In the receiving state, we receive samples until the specified amount
179     // while counting the number of overruns.
180     case RECEIVING:
181       if (pmt_eq(event, s_response_recv_raw_samples)){
182         status = pmt_nth(1, data);
183
184         if (pmt_eq(status, PMT_T)){
185           handle_response_recv_raw_samples(data);
186           return;
187         }
188         else {
189           error_msg = "bad response-xmit-raw-frame:";
190           goto bail;
191         }
192       }
193       goto unhandled;
194     
195     //------------------------- CLOSING CHANNEL ----------------------------//
196     // Check deallocation response for the RX channel 
197     case CLOSING_CHANNEL:
198       if (pmt_eq(event, s_response_deallocate_channel)){
199         status = pmt_nth(1, data);
200
201         if (pmt_eq(status, PMT_T)){
202           close_usrp();
203           return;
204         }
205         else {
206           error_msg = "failed to deallocate channel:";
207           goto bail;
208         }
209       }
210
211       // Alternately, we ignore all response recv samples while waiting for the
212       // channel to actually close
213       if (pmt_eq(event, s_response_recv_raw_samples))
214         return;
215
216       goto unhandled;
217
218     //--------------------------- CLOSING USRP ------------------------------//
219     // Once we have received a successful USRP close response, we shutdown all
220     // mblocks and exit.
221     case CLOSING_USRP:
222       if (pmt_eq(event, s_response_close)){
223         status = pmt_nth(1, data);
224
225         if (pmt_eq(status, PMT_T)){
226           std::cout << "\nOverruns: " << d_n_overruns << std::endl;
227           fflush(stdout);
228           shutdown_all(PMT_T);
229           return;
230         }
231         else {
232           error_msg = "failed to close USRP:";
233           goto bail;
234         }
235       }
236       goto unhandled;
237
238     default:
239       goto unhandled;
240   }
241   return;
242
243  // An error occured, print it, and shutdown all m-blocks
244  bail:
245   std::cerr << error_msg << data
246             << "status = " << status << std::endl;
247   shutdown_all(PMT_F);
248   return;
249
250  // Received an unhandled message for a specific state
251  unhandled:
252   if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
253     std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
254               << "in state "<< d_state << std::endl;
255 }
256
257
258 void
259 test_usrp_rx::open_usrp()
260 {
261   pmt_t which_usrp = pmt_from_long(0);
262
263   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
264   d_state = OPENING_USRP;
265   
266   if(verbose)
267     std::cout << "[TEST_USRP_INBAND_OVERRUN] Opening the USRP\n";
268 }
269
270 void
271 test_usrp_rx::close_usrp()
272 {
273   d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
274   d_state = CLOSING_USRP;
275   
276   if(verbose)
277     std::cout << "[TEST_USRP_INBAND_OVERRUN] Closing the USRP\n";
278 }
279
280 void
281 test_usrp_rx::allocate_channel()
282 {
283   long capacity = (long) 16e6;
284   d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
285   d_state = ALLOCATING_CHANNEL;
286   
287   if(verbose)
288     std::cout << "[TEST_USRP_INBAND_OVERRUN] Requesting RX channel allocation\n";
289 }
290
291 void
292 test_usrp_rx::enter_receiving()
293 {
294   d_state = RECEIVING;
295
296   d_rx->send(s_cmd_start_recv_raw_samples,
297              pmt_list2(PMT_F,
298                        d_rx_chan));
299
300   if(verbose)
301     std::cout << "[TEST_USRP_INBAND_OVERRUN] Receiving...\n";
302 }
303
304 void
305 test_usrp_rx::handle_response_recv_raw_samples(pmt_t data)
306 {
307   pmt_t invocation_handle = pmt_nth(0, data);
308   pmt_t status = pmt_nth(1, data);
309   pmt_t v_samples = pmt_nth(2, data);
310   pmt_t timestamp = pmt_nth(3, data);
311   pmt_t properties = pmt_nth(4, data);
312
313   d_samples_recvd += pmt_length(v_samples) / 4;
314
315   // Check for overrun
316   if(!pmt_is_dict(properties)) {
317     std::cout << "[TEST_USRP_INBAND_OVERRUN] Recv samples dictionary is improper\n";
318     return;
319   }
320
321   if(pmt_t overrun = pmt_dict_ref(properties, 
322                                   pmt_intern("overrun"), 
323                                   PMT_NIL)) {
324     if(pmt_eqv(overrun, PMT_T)) {
325       d_n_overruns++;
326
327       if(verbose && 0)
328         std::cout << "[TEST_USRP_INBAND_OVERRUN] Underrun\n";
329     }
330     else {
331     if(verbose && 0)
332       std::cout << "[TEST_USRP_INBAND_OVERRUN] No overrun\n" << overrun <<std::endl;
333     }
334   } else {
335
336     if(verbose && 0)
337       std::cout << "[TEST_USRP_INBAND_OVERRUN] No overrun\n";
338   }
339
340   // Check if the number samples we have received meets the test
341   if(d_samples_recvd >= d_samples_to_recv) {
342     d_rx->send(s_cmd_stop_recv_raw_samples, pmt_list2(PMT_NIL, d_rx_chan));
343     enter_closing_channel();
344     return;
345   }
346
347 }
348
349 void
350 test_usrp_rx::enter_closing_channel()
351 {
352   d_state = CLOSING_CHANNEL;
353   
354   sleep(2);
355   
356   d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan));
357   
358   if(verbose)
359     std::cout << "[TEST_USRP_INBAND_OVERRUN] Deallocating RX channel\n";
360 }
361
362 REGISTER_MBLOCK_CLASS(test_usrp_rx);
363
364
365 // ----------------------------------------------------------------
366
367 int
368 main (int argc, char **argv)
369 {
370   // handle any command line args here
371
372   mb_runtime_sptr rt = mb_make_runtime();
373   pmt_t result = PMT_NIL;
374
375   rt->run("top", "test_usrp_rx", PMT_F, &result);
376 }