d779c9a6442d734c95894ed171063bde3475146c
[debian/gnuradio] / usrp / limbo / apps-inband / test_usrp_inband_ping.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 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 <sys/time.h>
38 #include <iostream>
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_tx.h>
45 #include <symbols_usrp_rx.h>
46
47 static bool verbose = false;
48
49 class test_usrp_inband_ping : public mb_mblock
50 {
51
52   mb_port_sptr  d_tx;   // Ports connected to the USRP server
53   mb_port_sptr  d_rx;
54   mb_port_sptr  d_cs;
55
56   pmt_t   d_tx_chan;    // Returned channel from TX allocation
57   pmt_t   d_rx_chan;    // Returned channel from RX allocation
58
59   pmt_t   d_which_usrp; // The USRP to use for the test
60
61   long    d_warm_msgs;  // The number of messages to 'warm' the USRP
62   long    d_warm_recvd; // The number of msgs received in the 'warm' state
63
64   // Keep track of current state
65   enum state_t {
66     INIT,
67     OPENING_USRP,
68     ALLOCATING_CHANNELS,
69     WARMING_USRP,
70     PINGING,
71     CLOSING_CHANNELS,
72     CLOSING_USRP,
73   };
74   state_t d_state;
75
76  public:
77   test_usrp_inband_ping(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
78   ~test_usrp_inband_ping();
79   void initial_transition();
80   void handle_message(mb_message_sptr msg);
81
82  protected:
83   void opening_usrp();
84   void allocating_channels();
85   void enter_warming_usrp();
86   void enter_pinging();
87   void build_and_send_ping();
88   void closing_channels();
89   void closing_usrp();
90 };
91
92
93 int
94 main (int argc, char **argv)
95 {
96   // handle any command line args here
97
98   mb_runtime_sptr rt = mb_make_runtime();
99   pmt_t result = PMT_NIL;
100
101   rt->run("top", "test_usrp_inband_ping", PMT_F, &result);
102 }
103
104
105 test_usrp_inband_ping::test_usrp_inband_ping(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
106   : mb_mblock(runtime, instance_name, user_arg),
107   d_tx_chan(PMT_NIL),
108   d_rx_chan(PMT_NIL),
109   d_which_usrp(pmt_from_long(0)),
110   d_state(INIT)
111 {
112   
113   // A dictionary is used to pass parameters to the USRP
114   pmt_t usrp_dict = pmt_make_dict();
115
116   // Specify the RBF to use
117   pmt_dict_set(usrp_dict,
118                pmt_intern("rbf"),
119                pmt_intern("fixed1.rbf"));
120
121   // Set TX and RX interpolations
122   pmt_dict_set(usrp_dict,
123                pmt_intern("interp-tx"),
124                pmt_from_long(128));
125
126   pmt_dict_set(usrp_dict,
127                pmt_intern("decim-rx"),
128                pmt_from_long(16));
129   
130   d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
131   d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
132   d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
133
134   // Create an instance of USRP server and connect ports
135   define_component("server", "usrp_server", usrp_dict);
136   connect("self", "tx0", "server", "tx0");
137   connect("self", "rx0", "server", "rx0");
138   connect("self", "cs", "server", "cs");
139
140 }
141
142 test_usrp_inband_ping::~test_usrp_inband_ping()
143 {
144 }
145
146 void
147 test_usrp_inband_ping::initial_transition()
148 {
149   opening_usrp();
150 }
151
152 // Handle message reads all incoming messages from USRP server which will be
153 // initialization and ping responses.  We perform actions based on the current
154 // state and the event (ie, ping response)
155 void
156 test_usrp_inband_ping::handle_message(mb_message_sptr msg)
157 {
158   pmt_t event = msg->signal();
159   pmt_t data = msg->data();
160   pmt_t port_id = msg->port_id();
161
162   pmt_t handle = PMT_F;
163   pmt_t status = PMT_F;
164   std::string error_msg;
165
166   // Dispatch based on state
167   switch(d_state) {
168
169     //----------------------------- OPENING_USRP ----------------------------//
170     // We only expect a response from opening the USRP which should be succesful
171     // or failed.
172     case OPENING_USRP:
173       
174       if(pmt_eq(event, s_response_open)) {
175
176         status = pmt_nth(1, data);          // failed/succes
177         
178         if(pmt_eq(status, PMT_T)) {
179           allocating_channels();
180           return;
181         }
182         else {
183           error_msg = "failed to open usrp:";
184           goto bail;
185         }
186
187       }
188
189       goto unhandled;   // all other messages not handled in this state
190       
191     
192     //----------------------- ALLOCATING CHANNELS --------------------//
193     // When allocating channels, we need to wait for 2 responses from
194     // USRP server: one for TX and one for RX.  Both are initialized to
195     // NIL so we know to continue to the next state once both are set.
196     case ALLOCATING_CHANNELS:
197
198       // A TX allocation response
199       if(pmt_eq(event, s_response_allocate_channel)
200           && pmt_eq(d_tx->port_symbol(), port_id)) 
201       {
202         status = pmt_nth(1, data);
203         
204         // If successful response, extract the channel
205         if(pmt_eq(status, PMT_T)) {
206           
207           d_tx_chan = pmt_nth(2, data);
208
209           if(verbose)
210             std::cout << "[TEST_USRP_INBAND_PING] Received TX allocation"
211                       << " on channel " << d_tx_chan << std::endl;
212
213           // If the RX has also been allocated already, we can continue
214           if(!pmt_eqv(d_rx_chan, PMT_NIL)) 
215             enter_warming_usrp();
216
217           return;
218         }
219         else {  // TX allocation failed
220           error_msg = "failed to allocate TX channel:";
221           goto bail;
222         }
223       }
224       
225       // A RX allocation response
226       if(pmt_eq(event, s_response_allocate_channel)
227           && pmt_eq(d_rx->port_symbol(), port_id)) 
228       {
229         status = pmt_nth(1, data);
230         
231         // If successful response, extract the channel
232         if(pmt_eq(status, PMT_T)) {
233           
234           d_rx_chan = pmt_nth(2, data);
235
236           if(verbose)
237             std::cout << "[TEST_USRP_INBAND_PING] Received RX allocation"
238                       << " on channel " << d_rx_chan << std::endl;
239
240           // If the TX has also been allocated already, we can continue
241           if(!pmt_eqv(d_tx_chan, PMT_NIL)) 
242             enter_warming_usrp();
243
244           return;
245         }
246         else {  // RX allocation failed
247           error_msg = "failed to allocate RX channel:";
248           goto bail;
249         }
250       }
251
252       goto unhandled;
253
254     //----------------------- WARMING USRP --------------------//
255     // The FX2 seems to need some amount of data to be buffered
256     // before it begins reading.  We use this state to simply
257     // warm up the USRP before benchmarking pings.
258     case WARMING_USRP:
259
260       // We really don't care about the responses from the
261       // control channel in the warming stage, but once we receive
262       // the proper number of responses we switch states.
263       if(pmt_eq(event, s_response_from_control_channel)
264           && pmt_eq(d_rx->port_symbol(), port_id))
265       {
266         d_warm_recvd++;
267
268         if(d_warm_recvd > d_warm_msgs)
269           enter_pinging();
270
271         return;
272       }
273
274       goto unhandled;
275
276     case PINGING:
277       goto unhandled;
278
279     case CLOSING_CHANNELS:
280       goto unhandled;
281
282     case CLOSING_USRP:
283       goto unhandled;
284
285     case INIT:
286       goto unhandled;
287
288   }
289  
290  // An error occured, print it, and shutdown all m-blocks
291  bail:
292   std::cerr << error_msg << data
293             << "status = " << status << std::endl;
294   shutdown_all(PMT_F);
295   return;
296
297  // Received an unhandled message for a specific state
298  unhandled:
299   if(verbose)
300     std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
301               << "in state "<< d_state << std::endl;
302
303 }
304
305
306 // Sends a command to USRP server to open up a connection to the
307 // specified USRP, which is defaulted to USRP 0 on the system
308 void
309 test_usrp_inband_ping::opening_usrp()
310 {
311
312   if(verbose)
313     std::cout << "[TEST_USRP_INBAND_PING] Opening USRP " 
314               << d_which_usrp << std::endl;
315
316   d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
317   d_state = OPENING_USRP;
318 }
319
320 // RX and TX channels must be allocated so that the USRP server can
321 // properly share bandwidth across multiple USRPs.  No commands will be
322 // successful to the USRP through the USRP server on the TX or RX channels until
323 // a bandwidth allocation has been received.
324 void
325 test_usrp_inband_ping::allocating_channels()
326 {
327   d_state = ALLOCATING_CHANNELS;
328
329   long capacity = (long) 16e6;
330   d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
331   d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
332 }
333
334 // The USRP needs some amount of initial data to pass a buffering point such
335 // that it begins to pull and read data from the FX2.  We send an arbitrary
336 // amount of data to start the pipeline, which are just pings.
337 void
338 test_usrp_inband_ping::enter_warming_usrp()
339 {
340   d_state = WARMING_USRP;
341
342   for(int i=0; i < d_warm_msgs; i++)
343     build_and_send_ping();
344 }
345
346 void
347 test_usrp_inband_ping::enter_pinging()
348 {
349   d_state = PINGING;
350   
351   if(verbose)
352     std::cout << "[TEST_USRP_INBAND_PING] Running ping tests\n";
353
354 }
355
356 // Pings are sent over the TX channel using the signal 'cmd-to-control-channel'
357 // to the USRP server.  Within this message there can be infinite subpackets
358 // stored as a list (the second parameter) and sent.  The only subpacket we send
359 // is a ping, interpreted by the 'op-ping-fixed' signal.
360 void
361 test_usrp_inband_ping::build_and_send_ping()
362 {
363   
364   d_tx->send(s_cmd_to_control_channel,    // USRP server signal
365              pmt_list2(PMT_NIL,           // invocation handle 
366                        pmt_list1(pmt_list3(s_op_ping_fixed, 
367                                            pmt_from_long(0), 
368                                            pmt_from_long(0)))));
369
370   if(verbose)
371     std::cout << "[TEST_USRP_INBAND_PING] Ping!!" << std::endl;
372 }
373
374 REGISTER_MBLOCK_CLASS(test_usrp_inband_ping);