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_protocol_class.h>
29 #include <mb_exception.h>
30 #include <mb_msg_queue.h>
31 #include <mb_message.h>
32 //#include <mb_mblock_impl.h>
33 #include <mb_msg_accepter.h>
34 #include <mb_class_registry.h>
41 // Include the symbols needed for communication with USRP server
42 #include <symbols_usrp_server_cs.h>
43 #include <symbols_usrp_channel.h>
44 #include <symbols_usrp_low_level_cs.h>
45 #include <symbols_usrp_tx.h>
46 #include <symbols_usrp_rx.h>
48 static bool verbose = true;
50 class test_usrp_inband_registers : public mb_mblock
53 mb_port_sptr d_tx; // Ports connected to the USRP server
57 pmt_t d_tx_chan; // Returned channel from TX allocation
58 pmt_t d_rx_chan; // Returned channel from RX allocation
60 pmt_t d_which_usrp; // The USRP to use for the test
62 long d_warm_msgs; // The number of messages to 'warm' the USRP
63 long d_warm_recvd; // The number of msgs received in the 'warm' state
65 // Keep track of current state
78 test_usrp_inband_registers(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
79 ~test_usrp_inband_registers();
80 void initial_transition();
81 void handle_message(mb_message_sptr msg);
85 void allocating_channels();
86 void write_register();
88 void closing_channels();
90 void enter_receiving();
91 void build_and_send_ping();
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_registers", PMT_F, &result);
107 test_usrp_inband_registers::test_usrp_inband_registers(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("inband_1rxhb_1tx.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("decim-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_registers::~test_usrp_inband_registers()
149 test_usrp_inband_registers::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_registers::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)) {
223 else { // TX allocation failed
224 error_msg = "failed to allocate TX channel:";
229 // A RX allocation response
230 if(pmt_eq(event, s_response_allocate_channel)
231 && pmt_eq(d_rx->port_symbol(), port_id))
233 status = pmt_nth(1, data);
235 // If successful response, extract the channel
236 if(pmt_eq(status, PMT_T)) {
238 d_rx_chan = pmt_nth(2, data);
241 std::cout << "[TEST_USRP_INBAND_PING] Received RX allocation"
242 << " on channel " << d_rx_chan << std::endl;
244 // If the TX has also been allocated already, we can continue
245 if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
252 else { // RX allocation failed
253 error_msg = "failed to allocate RX channel:";
260 //-------------------------- WRITE REGISTER ----------------------------//
261 // In the write register state, we do not expect to receive any messages
262 // since the write does not directly generate a response until the USRP
267 //-------------------------- READ REGISTER ----------------------------//
268 // In the read register state, we only expect a read register response back
269 // that has the value we expect to have in it. We read the response, ensure
270 // that the read was successful and display the register value.
273 if(pmt_eq(event, s_response_from_control_channel)
274 && pmt_eq(d_tx->port_symbol(), port_id))
276 status = pmt_nth(1, data);
278 // If the read was successful, we extract the subpacket information
279 if(pmt_eq(status, PMT_T)) {
281 pmt_t subp = pmt_nth(2, data); // subpacket should be the read reg reply
283 pmt_t subp_sig = pmt_nth(0, subp);
284 pmt_t subp_data = pmt_nth(1, subp);
286 if(!pmt_eqv(subp_sig, s_op_read_reg_reply)) {
287 error_msg = "received improper subpacket when expecting reg reply.";
291 pmt_t rid = pmt_nth(0, subp_data);
292 pmt_t reg_num = pmt_nth(1, subp_data);
293 pmt_t reg_val = pmt_nth(2, subp_data);
296 std::cout << "[TEST_USRP_INBAND_REGISTERS] Received read reg reply "
298 << "RID: " << rid << ", "
299 << "Reg: " << reg_num << ", "
300 << "Val: " << reg_val
303 // read_register(); FIX ME STATE TRANSITION
306 } else { // bail on unsuccessful write
307 error_msg = "failed to write to register.";
313 case CLOSING_CHANNELS:
324 // An error occured, print it, and shutdown all m-blocks
326 std::cerr << error_msg << data
327 << "status = " << status << std::endl;
331 // Received an unhandled message for a specific state
333 if(verbose && !pmt_eq(event, s_response_recv_raw_samples))
334 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
335 << "in state "<< d_state << std::endl;
340 // Sends a command to USRP server to open up a connection to the
341 // specified USRP, which is defaulted to USRP 0 on the system
343 test_usrp_inband_registers::opening_usrp()
347 std::cout << "[TEST_USRP_INBAND_PING] Opening USRP "
348 << d_which_usrp << std::endl;
350 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
351 d_state = OPENING_USRP;
354 // RX and TX channels must be allocated so that the USRP server can
355 // properly share bandwidth across multiple USRPs. No commands will be
356 // successful to the USRP through the USRP server on the TX or RX channels until
357 // a bandwidth allocation has been received.
359 test_usrp_inband_registers::allocating_channels()
361 d_state = ALLOCATING_CHANNELS;
363 long capacity = (long) 16e6;
364 d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
365 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
368 // After allocating the channels, a write register command will be sent to the
371 test_usrp_inband_registers::write_register()
373 d_state = WRITE_REGISTER;
377 d_tx->send(s_cmd_to_control_channel, // C/S packet
378 pmt_list2(PMT_NIL, // invoc handle
380 pmt_list2(s_op_write_reg,
383 pmt_from_long(0xbeef))))));
386 std::cout << "[TEST_USRP_INBAND_REGISTERS] Writing 0xbeef to "
389 read_register(); // immediately transition to read the register
392 // Temporary: for testing pings
394 test_usrp_inband_registers::build_and_send_ping()
397 d_tx->send(s_cmd_to_control_channel,
398 pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
399 pmt_list2(pmt_from_long(0),
400 pmt_from_long(0))))));
402 std::cout << "[TEST_USRP_INBAND_CS] Ping sent" << std::endl;
405 // After writing to the register, we want to read the value back and ensure that
406 // it is the same value that we wrote.
408 test_usrp_inband_registers::read_register()
410 d_state = READ_REGISTER;
414 d_tx->send(s_cmd_to_control_channel, // C/S packet
415 pmt_list2(PMT_NIL, // invoc handle
417 pmt_list2(s_op_read_reg,
419 pmt_from_long(0), // rid
420 pmt_from_long(reg))))));
422 std::cout << "[TEST_USRP_INBAND_REGISTERS] Reading from register "
426 // Used to enter the receiving state
428 test_usrp_inband_registers::enter_receiving()
430 d_rx->send(s_cmd_start_recv_raw_samples,
435 REGISTER_MBLOCK_CLASS(test_usrp_inband_registers);