c37c19c005d86ed62c698c5fba57f4484f0526ae
[debian/gnuradio] / usrp / host / apps-inband / test_usrp_inband_rx.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_samples_recvd;
70   long d_samples_to_recv;
71
72  public:
73   test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
74   ~test_usrp_rx();
75   void initial_transition();
76   void handle_message(mb_message_sptr msg);
77
78  protected:
79   void open_usrp();
80   void close_usrp();
81   void allocate_channel();
82   void send_packets();
83   void enter_receiving();
84   void build_and_send_next_frame();
85   void handle_response_recv_raw_samples(pmt_t invocation_handle);
86   void enter_closing_channel();
87 };
88
89 test_usrp_rx::test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
90   : mb_mblock(runtime, instance_name, user_arg),
91     d_samples_recvd(0),
92     d_samples_to_recv(5e6)
93
94   
95   d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
96   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
97   
98   // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
99   pmt_t usrp_dict = pmt_make_dict();
100
101   // Specify the RBF to use
102   pmt_dict_set(usrp_dict,
103                pmt_intern("rbf"),
104                pmt_intern("nanocell9.rbf"));
105
106   pmt_dict_set(usrp_dict,
107                pmt_intern("decim-rx"),
108                pmt_from_long(128));
109
110   define_component("server", "usrp_server", usrp_dict);
111
112   connect("self", "rx0", "server", "rx0");
113   connect("self", "cs", "server", "cs");
114
115 }
116
117 test_usrp_rx::~test_usrp_rx()
118 {
119 }
120
121 void
122 test_usrp_rx::initial_transition()
123 {
124   open_usrp();
125 }
126
127 void
128 test_usrp_rx::handle_message(mb_message_sptr msg)
129 {
130   pmt_t event = msg->signal();
131   pmt_t data = msg->data();
132
133   pmt_t handle = PMT_F;
134   pmt_t status = PMT_F;
135   std::string error_msg;
136   
137   switch(d_state){
138     
139     //----------------------------- OPENING_USRP ----------------------------//
140     // We only expect a response from opening the USRP which should be succesful
141     // or failed.
142     case OPENING_USRP:
143       if (pmt_eq(event, s_response_open)){
144         status = pmt_nth(1, data);
145         if (pmt_eq(status, PMT_T)){
146           allocate_channel();
147           return;
148         }
149         else {
150           error_msg = "failed to open usrp:";
151           goto bail;
152         }
153       }
154       goto unhandled;
155       
156     //----------------------- ALLOCATING CHANNELS --------------------//
157     // Allocate an RX channel to perform the overrun test.
158     case ALLOCATING_CHANNEL:
159       if (pmt_eq(event, s_response_allocate_channel)){
160         status = pmt_nth(1, data);
161         d_rx_chan = pmt_nth(2, data);
162
163         if (pmt_eq(status, PMT_T)){
164           enter_receiving();
165           return;
166         }
167         else {
168           error_msg = "failed to allocate channel:";
169           goto bail;
170         }
171       }
172       goto unhandled;
173
174     //--------------------------- RECEIVING ------------------------------//
175     // In the receiving state, we receive samples until the specified amount
176     // while counting the number of overruns.
177     case RECEIVING:
178       if (pmt_eq(event, s_response_recv_raw_samples)){
179         status = pmt_nth(1, data);
180
181         if (pmt_eq(status, PMT_T)){
182           handle_response_recv_raw_samples(data);
183           return;
184         }
185         else {
186           error_msg = "bad response-xmit-raw-frame:";
187           goto bail;
188         }
189       }
190       goto unhandled;
191     
192     //------------------------- CLOSING CHANNEL ----------------------------//
193     // Check deallocation response for the RX channel 
194     case CLOSING_CHANNEL:
195       if (pmt_eq(event, s_response_deallocate_channel)){
196         status = pmt_nth(1, data);
197
198         if (pmt_eq(status, PMT_T)){
199           close_usrp();
200           return;
201         }
202         else {
203           error_msg = "failed to deallocate channel:";
204           goto bail;
205         }
206       }
207
208       // Alternately, we ignore all response recv samples while waiting for the
209       // channel to actually close
210       if (pmt_eq(event, s_response_recv_raw_samples))
211         return;
212
213       goto unhandled;
214
215     //--------------------------- CLOSING USRP ------------------------------//
216     // Once we have received a successful USRP close response, we shutdown all
217     // mblocks and exit.
218     case CLOSING_USRP:
219       if (pmt_eq(event, s_response_close)){
220         status = pmt_nth(1, data);
221
222         if (pmt_eq(status, PMT_T)){
223           fflush(stdout);
224           shutdown_all(PMT_T);
225           return;
226         }
227         else {
228           error_msg = "failed to close USRP:";
229           goto bail;
230         }
231       }
232       goto unhandled;
233
234     default:
235       goto unhandled;
236   }
237   return;
238
239  // An error occured, print it, and shutdown all m-blocks
240  bail:
241   std::cerr << error_msg << data
242             << "status = " << status << std::endl;
243   shutdown_all(PMT_F);
244   return;
245
246  // Received an unhandled message for a specific state
247  unhandled:
248   if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
249     std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
250               << "in state "<< d_state << std::endl;
251 }
252
253
254 void
255 test_usrp_rx::open_usrp()
256 {
257   pmt_t which_usrp = pmt_from_long(0);
258
259   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
260   d_state = OPENING_USRP;
261   
262   if(verbose)
263     std::cout << "[TEST_USRP_INBAND_RX] Opening the USRP\n";
264 }
265
266 void
267 test_usrp_rx::close_usrp()
268 {
269   d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
270   d_state = CLOSING_USRP;
271   
272   if(verbose)
273     std::cout << "[TEST_USRP_INBAND_RX] Closing the USRP\n";
274 }
275
276 void
277 test_usrp_rx::allocate_channel()
278 {
279   long capacity = (long) 16e6;
280   d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
281   d_state = ALLOCATING_CHANNEL;
282   
283   if(verbose)
284     std::cout << "[TEST_USRP_INBAND_RX] Requesting RX channel allocation\n";
285 }
286
287 void
288 test_usrp_rx::enter_receiving()
289 {
290   d_state = RECEIVING;
291
292   d_rx->send(s_cmd_start_recv_raw_samples,
293              pmt_list2(PMT_F,
294                        d_rx_chan));
295
296   if(verbose)
297     std::cout << "[TEST_USRP_INBAND_RX] Receiving...\n";
298 }
299
300 void
301 test_usrp_rx::handle_response_recv_raw_samples(pmt_t data)
302 {
303   pmt_t invocation_handle = pmt_nth(0, data);
304   pmt_t status = pmt_nth(1, data);
305   pmt_t v_samples = pmt_nth(2, data);
306   pmt_t timestamp = pmt_nth(3, data);
307   pmt_t properties = pmt_nth(4, data);
308
309   d_samples_recvd += pmt_length(v_samples) / 4;
310
311   // Check for overrun
312   if(!pmt_is_dict(properties)) {
313     std::cout << "[TEST_USRP_INBAND_RX] Recv samples dictionary is improper\n";
314     return;
315   }
316
317   // Check if the number samples we have received meets the test
318   if(d_samples_recvd >= d_samples_to_recv) {
319     d_rx->send(s_cmd_stop_recv_raw_samples, pmt_list2(PMT_NIL, d_rx_chan));
320     enter_closing_channel();
321     return;
322   }
323
324 }
325
326 void
327 test_usrp_rx::enter_closing_channel()
328 {
329   d_state = CLOSING_CHANNEL;
330   
331   sleep(2);
332   
333   d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan));
334   
335   if(verbose)
336     std::cout << "[TEST_USRP_INBAND_RX] Deallocating RX channel\n";
337 }
338
339 REGISTER_MBLOCK_CLASS(test_usrp_rx);
340
341
342 // ----------------------------------------------------------------
343
344 int
345 main (int argc, char **argv)
346 {
347   // handle any command line args here
348
349   mb_runtime_sptr rt = mb_make_runtime();
350   pmt_t result = PMT_NIL;
351
352   rt->run("top", "test_usrp_rx", PMT_F, &result);
353 }