3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
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>
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>
49 static bool verbose = false;
51 class test_usrp_inband_ping : public mb_mblock
54 mb_port_sptr d_tx; // Ports connected to the USRP server
58 pmt_t d_tx_chan; // Returned channel from TX allocation
59 pmt_t d_rx_chan; // Returned channel from RX allocation
61 pmt_t d_which_usrp; // The USRP to use for the test
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
66 // Keep track of current state
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);
86 void allocating_channels();
87 void enter_warming_usrp();
89 void build_and_send_ping();
90 void closing_channels();
96 main (int argc, char **argv)
98 // handle any command line args here
100 mb_runtime_sptr rt = mb_make_runtime();
101 pmt_t result = PMT_NIL;
103 rt->run("top", "test_usrp_inband_ping", PMT_F, &result);
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),
111 d_which_usrp(pmt_from_long(0)),
115 // A dictionary is used to pass parameters to the USRP
116 pmt_t usrp_dict = pmt_make_dict();
118 // Specify the RBF to use
119 pmt_dict_set(usrp_dict,
121 pmt_intern("fixed1.rbf"));
123 // Set TX and RX interpolations
124 pmt_dict_set(usrp_dict,
125 pmt_intern("interp-tx"),
128 pmt_dict_set(usrp_dict,
129 pmt_intern("interp-rx"),
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);
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");
144 test_usrp_inband_ping::~test_usrp_inband_ping()
149 test_usrp_inband_ping::initial_transition()
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)
158 test_usrp_inband_ping::handle_message(mb_message_sptr msg)
160 pmt_t event = msg->signal();
161 pmt_t data = msg->data();
162 pmt_t port_id = msg->port_id();
164 pmt_t handle = PMT_F;
165 pmt_t status = PMT_F;
166 std::string error_msg;
168 // Dispatch based on state
171 //----------------------------- OPENING_USRP ----------------------------//
172 // We only expect a response from opening the USRP which should be succesful
176 if(pmt_eq(event, s_response_open)) {
178 status = pmt_nth(1, data); // failed/succes
180 if(pmt_eq(status, PMT_T)) {
181 allocating_channels();
185 error_msg = "failed to open usrp:";
191 goto unhandled; // all other messages not handled in this state
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:
200 // A TX allocation response
201 if(pmt_eq(event, s_response_allocate_channel)
202 && pmt_eq(d_tx->port_symbol(), port_id))
204 status = pmt_nth(1, data);
206 // If successful response, extract the channel
207 if(pmt_eq(status, PMT_T)) {
209 d_tx_chan = pmt_nth(2, data);
212 std::cout << "[TEST_USRP_INBAND_PING] Received TX allocation"
213 << " on channel " << d_tx_chan << std::endl;
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();
221 else { // TX allocation failed
222 error_msg = "failed to allocate TX channel:";
227 // A RX allocation response
228 if(pmt_eq(event, s_response_allocate_channel)
229 && pmt_eq(d_rx->port_symbol(), port_id))
231 status = pmt_nth(1, data);
233 // If successful response, extract the channel
234 if(pmt_eq(status, PMT_T)) {
236 d_rx_chan = pmt_nth(2, data);
239 std::cout << "[TEST_USRP_INBAND_PING] Received RX allocation"
240 << " on channel " << d_rx_chan << std::endl;
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();
248 else { // RX allocation failed
249 error_msg = "failed to allocate RX channel:";
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.
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))
270 if(d_warm_recvd > d_warm_msgs)
281 case CLOSING_CHANNELS:
292 // An error occured, print it, and shutdown all m-blocks
294 std::cerr << error_msg << data
295 << "status = " << status << std::endl;
299 // Received an unhandled message for a specific state
302 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
303 << "in state "<< d_state << std::endl;
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
311 test_usrp_inband_ping::opening_usrp()
315 std::cout << "[TEST_USRP_INBAND_PING] Opening USRP "
316 << d_which_usrp << std::endl;
318 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
319 d_state = OPENING_USRP;
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.
327 test_usrp_inband_ping::allocating_channels()
329 d_state = ALLOCATING_CHANNELS;
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)));
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.
340 test_usrp_inband_ping::enter_warming_usrp()
342 d_state = WARMING_USRP;
344 for(int i=0; i < d_warm_msgs; i++)
345 build_and_send_ping();
349 test_usrp_inband_ping::enter_pinging()
354 std::cout << "[TEST_USRP_INBAND_PING] Running ping tests\n";
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.
363 test_usrp_inband_ping::build_and_send_ping()
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,
370 pmt_from_long(0)))));
373 std::cout << "[TEST_USRP_INBAND_PING] Ping!!" << std::endl;
376 REGISTER_MBLOCK_CLASS(test_usrp_inband_ping);