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