3 * Copyright 2007,2008 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 <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>
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>
47 static bool verbose = false;
49 class test_usrp_inband_ping : public mb_mblock
52 mb_port_sptr d_tx; // Ports connected to the USRP server
56 pmt_t d_tx_chan; // Returned channel from TX allocation
57 pmt_t d_rx_chan; // Returned channel from RX allocation
59 pmt_t d_which_usrp; // The USRP to use for the test
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
64 // Keep track of current state
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);
84 void allocating_channels();
85 void enter_warming_usrp();
87 void build_and_send_ping();
88 void closing_channels();
94 main (int argc, char **argv)
96 // handle any command line args here
98 mb_runtime_sptr rt = mb_make_runtime();
99 pmt_t result = PMT_NIL;
101 rt->run("top", "test_usrp_inband_ping", PMT_F, &result);
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),
109 d_which_usrp(pmt_from_long(0)),
113 // A dictionary is used to pass parameters to the USRP
114 pmt_t usrp_dict = pmt_make_dict();
116 // Specify the RBF to use
117 pmt_dict_set(usrp_dict,
119 pmt_intern("fixed1.rbf"));
121 // Set TX and RX interpolations
122 pmt_dict_set(usrp_dict,
123 pmt_intern("interp-tx"),
126 pmt_dict_set(usrp_dict,
127 pmt_intern("decim-rx"),
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);
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");
142 test_usrp_inband_ping::~test_usrp_inband_ping()
147 test_usrp_inband_ping::initial_transition()
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)
156 test_usrp_inband_ping::handle_message(mb_message_sptr msg)
158 pmt_t event = msg->signal();
159 pmt_t data = msg->data();
160 pmt_t port_id = msg->port_id();
162 pmt_t handle = PMT_F;
163 pmt_t status = PMT_F;
164 std::string error_msg;
166 // Dispatch based on state
169 //----------------------------- OPENING_USRP ----------------------------//
170 // We only expect a response from opening the USRP which should be succesful
174 if(pmt_eq(event, s_response_open)) {
176 status = pmt_nth(1, data); // failed/succes
178 if(pmt_eq(status, PMT_T)) {
179 allocating_channels();
183 error_msg = "failed to open usrp:";
189 goto unhandled; // all other messages not handled in this state
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:
198 // A TX allocation response
199 if(pmt_eq(event, s_response_allocate_channel)
200 && pmt_eq(d_tx->port_symbol(), port_id))
202 status = pmt_nth(1, data);
204 // If successful response, extract the channel
205 if(pmt_eq(status, PMT_T)) {
207 d_tx_chan = pmt_nth(2, data);
210 std::cout << "[TEST_USRP_INBAND_PING] Received TX allocation"
211 << " on channel " << d_tx_chan << std::endl;
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();
219 else { // TX allocation failed
220 error_msg = "failed to allocate TX channel:";
225 // A RX allocation response
226 if(pmt_eq(event, s_response_allocate_channel)
227 && pmt_eq(d_rx->port_symbol(), port_id))
229 status = pmt_nth(1, data);
231 // If successful response, extract the channel
232 if(pmt_eq(status, PMT_T)) {
234 d_rx_chan = pmt_nth(2, data);
237 std::cout << "[TEST_USRP_INBAND_PING] Received RX allocation"
238 << " on channel " << d_rx_chan << std::endl;
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();
246 else { // RX allocation failed
247 error_msg = "failed to allocate RX channel:";
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.
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))
268 if(d_warm_recvd > d_warm_msgs)
279 case CLOSING_CHANNELS:
290 // An error occured, print it, and shutdown all m-blocks
292 std::cerr << error_msg << data
293 << "status = " << status << std::endl;
297 // Received an unhandled message for a specific state
300 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
301 << "in state "<< d_state << std::endl;
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
309 test_usrp_inband_ping::opening_usrp()
313 std::cout << "[TEST_USRP_INBAND_PING] Opening USRP "
314 << d_which_usrp << std::endl;
316 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
317 d_state = OPENING_USRP;
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.
325 test_usrp_inband_ping::allocating_channels()
327 d_state = ALLOCATING_CHANNELS;
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)));
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.
338 test_usrp_inband_ping::enter_warming_usrp()
340 d_state = WARMING_USRP;
342 for(int i=0; i < d_warm_msgs; i++)
343 build_and_send_ping();
347 test_usrp_inband_ping::enter_pinging()
352 std::cout << "[TEST_USRP_INBAND_PING] Running ping tests\n";
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.
361 test_usrp_inband_ping::build_and_send_ping()
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,
368 pmt_from_long(0)))));
371 std::cout << "[TEST_USRP_INBAND_PING] Ping!!" << std::endl;
374 REGISTER_MBLOCK_CLASS(test_usrp_inband_ping);