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