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_msg_accepter.h>
33 #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_underrun : 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 long d_nsamples_to_send;
79 long d_nsamples_xmitted;
80 long d_nframes_xmitted;
81 long d_samples_per_frame;
84 // for generating sine wave output
85 ui_nco<float,float> d_nco;
91 test_usrp_inband_underrun(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
92 ~test_usrp_inband_underrun();
93 void initial_transition();
94 void handle_message(mb_message_sptr msg);
98 void allocating_channels();
99 void write_register();
100 void read_register();
101 void closing_channels();
103 void enter_receiving();
104 void enter_transmitting();
105 void build_and_send_ping();
106 void build_and_send_next_frame();
107 void handle_xmit_response(pmt_t handle);
108 void handle_recv_response(pmt_t dict);
113 main (int argc, char **argv)
115 // handle any command line args here
117 mb_runtime_sptr rt = mb_make_runtime();
118 pmt_t result = PMT_NIL;
120 rt->run("top", "test_usrp_inband_underrun", PMT_F, &result);
124 test_usrp_inband_underrun::test_usrp_inband_underrun(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
125 : mb_mblock(runtime, instance_name, user_arg),
128 d_which_usrp(pmt_from_long(0)),
130 d_nsamples_to_send((long) 27e6),
131 d_nsamples_xmitted(0),
132 d_nframes_xmitted(0),
133 d_samples_per_frame(d_nsamples_to_send), // full packet
135 d_done_sending(false),
140 // A dictionary is used to pass parameters to the USRP
141 pmt_t usrp_dict = pmt_make_dict();
143 // Specify the RBF to use
144 pmt_dict_set(usrp_dict,
146 pmt_intern("inband_1rxhb_1tx.rbf"));
148 // Set TX and RX interpolations
149 pmt_dict_set(usrp_dict,
150 pmt_intern("interp-tx"),
153 pmt_dict_set(usrp_dict,
154 pmt_intern("decim-rx"),
157 d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
158 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
159 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
161 // Create an instance of USRP server and connect ports
162 define_component("server", "usrp_server", usrp_dict);
163 connect("self", "tx0", "server", "tx0");
164 connect("self", "rx0", "server", "rx0");
165 connect("self", "cs", "server", "cs");
169 int interp = 32; // 32 -> 4MS/s
170 double sample_rate = 128e6 / interp;
171 d_nco.set_freq(2*M_PI * freq/sample_rate);
174 test_usrp_inband_underrun::~test_usrp_inband_underrun()
179 test_usrp_inband_underrun::initial_transition()
184 // Handle message reads all incoming messages from USRP server which will be
185 // initialization and ping responses. We perform actions based on the current
186 // state and the event (ie, ping response)
188 test_usrp_inband_underrun::handle_message(mb_message_sptr msg)
190 pmt_t event = msg->signal();
191 pmt_t data = msg->data();
192 pmt_t port_id = msg->port_id();
194 pmt_t handle = PMT_F;
195 pmt_t status = PMT_F;
196 pmt_t dict = PMT_NIL;
197 std::string error_msg;
199 // Check the recv sample responses for underruns and count
200 if(pmt_eq(event, s_response_recv_raw_samples)) {
201 handle = pmt_nth(0, data);
202 status = pmt_nth(1, data);
203 dict = pmt_nth(4, data);
205 if(pmt_eq(status, PMT_T)) {
206 handle_recv_response(dict);
210 error_msg = "error while receiving samples:";
216 // Dispatch based on state
219 //----------------------------- OPENING_USRP ----------------------------//
220 // We only expect a response from opening the USRP which should be succesful
224 if(pmt_eq(event, s_response_open)) {
226 status = pmt_nth(1, data); // failed/succes
228 if(pmt_eq(status, PMT_T)) {
229 allocating_channels();
233 error_msg = "failed to open usrp:";
239 goto unhandled; // all other messages not handled in this state
242 //----------------------- ALLOCATING CHANNELS --------------------//
243 // When allocating channels, we need to wait for 2 responses from
244 // USRP server: one for TX and one for RX. Both are initialized to
245 // NIL so we know to continue to the next state once both are set.
246 case ALLOCATING_CHANNELS:
248 // A TX allocation response
249 if(pmt_eq(event, s_response_allocate_channel)
250 && pmt_eq(d_tx->port_symbol(), port_id))
252 status = pmt_nth(1, data);
254 // If successful response, extract the channel
255 if(pmt_eq(status, PMT_T)) {
257 d_tx_chan = pmt_nth(2, data);
260 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received TX allocation"
261 << " on channel " << d_tx_chan << std::endl;
263 // If the RX has also been allocated already, we can continue
264 if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
266 enter_transmitting();
271 else { // TX allocation failed
272 error_msg = "failed to allocate TX channel:";
277 // A RX allocation response
278 if(pmt_eq(event, s_response_allocate_channel)
279 && pmt_eq(d_rx->port_symbol(), port_id))
281 status = pmt_nth(1, data);
283 // If successful response, extract the channel
284 if(pmt_eq(status, PMT_T)) {
286 d_rx_chan = pmt_nth(2, data);
289 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received RX allocation"
290 << " on channel " << d_rx_chan << std::endl;
292 // If the TX has also been allocated already, we can continue
293 if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
295 enter_transmitting();
300 else { // RX allocation failed
301 error_msg = "failed to allocate RX channel:";
314 //-------------------------- TRANSMITTING ----------------------------//
315 // In the transmit state we count the number of underruns received and
316 // ballpark the number with an expected count (something >1 for starters)
319 // Check that the transmits are OK
320 if (pmt_eq(event, s_response_xmit_raw_frame)){
321 handle = pmt_nth(0, data);
322 status = pmt_nth(1, data);
324 if (pmt_eq(status, PMT_T)){
325 handle_xmit_response(handle);
329 error_msg = "bad response-xmit-raw-frame:";
336 //------------------------- CLOSING CHANNELS ----------------------------//
337 // Check deallocation responses, once the TX and RX channels are both
338 // deallocated then we close the USRP.
339 case CLOSING_CHANNELS:
341 if (pmt_eq(event, s_response_deallocate_channel)
342 && pmt_eq(d_tx->port_symbol(), port_id))
344 status = pmt_nth(1, data);
346 // If successful, set the port to NIL
347 if(pmt_eq(status, PMT_T)) {
351 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received TX deallocation\n";
353 // If the RX is also deallocated, we can close the USRP
354 if(pmt_eq(d_rx_chan, PMT_NIL))
361 error_msg = "failed to deallocate TX channel:";
367 if (pmt_eq(event, s_response_deallocate_channel)
368 && pmt_eq(d_rx->port_symbol(), port_id))
370 status = pmt_nth(1, data);
372 // If successful, set the port to NIL
373 if(pmt_eq(status, PMT_T)) {
377 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received RX deallocation\n";
379 // If the TX is also deallocated, we can close the USRP
380 if(pmt_eq(d_tx_chan, PMT_NIL))
387 error_msg = "failed to deallocate RX channel:";
395 //--------------------------- CLOSING USRP ------------------------------//
396 // Once we have received a successful USRP close response, we shutdown all
400 if (pmt_eq(event, s_response_close)) {
402 status = pmt_nth(1, data);
404 if(pmt_eq(status, PMT_T)) {
407 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Successfully closed USRP\n";
409 std::cout << "\nUnderruns: " << d_n_underruns << std::endl;
417 error_msg = "failed to close USRP:";
429 // An error occured, print it, and shutdown all m-blocks
431 std::cerr << error_msg << data
432 << "status = " << status << std::endl;
436 // Received an unhandled message for a specific state
438 if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
439 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
440 << "in state "<< d_state << std::endl;
445 // Sends a command to USRP server to open up a connection to the
446 // specified USRP, which is defaulted to USRP 0 on the system
448 test_usrp_inband_underrun::opening_usrp()
452 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Opening USRP "
453 << d_which_usrp << std::endl;
455 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
456 d_state = OPENING_USRP;
459 // RX and TX channels must be allocated so that the USRP server can
460 // properly share bandwidth across multiple USRPs. No commands will be
461 // successful to the USRP through the USRP server on the TX or RX channels until
462 // a bandwidth allocation has been received.
464 test_usrp_inband_underrun::allocating_channels()
466 d_state = ALLOCATING_CHANNELS;
468 long capacity = (long) 16e6;
469 d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
470 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
473 // After allocating the channels, a write register command will be sent to the
476 test_usrp_inband_underrun::write_register()
478 d_state = WRITE_REGISTER;
482 d_tx->send(s_cmd_to_control_channel, // C/S packet
483 pmt_list2(PMT_NIL, // invoc handle
485 pmt_list2(s_op_write_reg,
488 pmt_from_long(0xbeef))))));
491 std::cout << "[TEST_USRP_INBAND_REGISTERS] Writing 0xbeef to "
494 read_register(); // immediately transition to read the register
497 // Temporary: for testing pings
499 test_usrp_inband_underrun::build_and_send_ping()
502 d_tx->send(s_cmd_to_control_channel,
503 pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
504 pmt_list2(pmt_from_long(0),
505 pmt_from_long(0))))));
507 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Ping sent" << std::endl;
510 // After writing to the register, we want to read the value back and ensure that
511 // it is the same value that we wrote.
513 test_usrp_inband_underrun::read_register()
515 d_state = READ_REGISTER;
519 d_tx->send(s_cmd_to_control_channel, // C/S packet
520 pmt_list2(PMT_NIL, // invoc handle
522 pmt_list2(s_op_read_reg,
524 pmt_from_long(0), // rid
525 pmt_from_long(reg))))));
527 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Reading from register "
531 // Used to enter the receiving state
533 test_usrp_inband_underrun::enter_receiving()
535 d_rx->send(s_cmd_start_recv_raw_samples,
540 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Started RX sample stream\n";
544 test_usrp_inband_underrun::enter_transmitting()
546 d_state = TRANSMITTING;
547 d_nsamples_xmitted = 0;
550 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Entering transmit state...\n";
552 build_and_send_next_frame(); // fire off 4 to start pipeline
553 build_and_send_next_frame();
554 build_and_send_next_frame();
555 build_and_send_next_frame();
559 test_usrp_inband_underrun::build_and_send_next_frame()
562 long nsamples_this_frame =
563 std::min(d_nsamples_to_send - d_nsamples_xmitted,
564 d_samples_per_frame);
566 if (nsamples_this_frame == 0){
567 d_done_sending = true;
571 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
572 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
574 int16_t *samples = pmt_s16vector_writable_elements(uvec, ignore);
576 // fill in the complex sinusoid
578 for (int i = 0; i < nsamples_this_frame; i++){
582 d_nco.sincos(&s, 1, d_amplitude);
583 // write 16-bit i & q
584 samples[2*i] = (int16_t) s.real();
585 samples[2*i+1] = (int16_t) s.imag();
588 gr_complex s(d_amplitude, d_amplitude);
590 // write 16-bit i & q
591 samples[2*i] = (int16_t) s.real();
592 samples[2*i+1] = (int16_t) s.imag();
597 std::cout << "[TEST_USRP_INBAND_TX] Transmitting frame...\n";
599 pmt_t timestamp = pmt_from_long(0xffffffff); // NOW
600 d_tx->send(s_cmd_xmit_raw_frame,
601 pmt_list4(pmt_from_long(d_nframes_xmitted), // invocation-handle
602 d_tx_chan, // channel
606 d_nsamples_xmitted += nsamples_this_frame;
610 std::cout << "[TEST_USRP_INBAND_TX] Transmitted frame\n";
615 test_usrp_inband_underrun::handle_xmit_response(pmt_t handle)
617 if (d_done_sending &&
618 pmt_to_long(handle) == (d_nframes_xmitted - 1)){
619 // We're done sending and have received all responses
624 build_and_send_next_frame();
628 test_usrp_inband_underrun::handle_recv_response(pmt_t dict)
630 if(!pmt_is_dict(dict)) {
631 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Recv samples dictionary is improper\n";
635 // Read the TX interpolations
636 if(pmt_t underrun = pmt_dict_ref(dict,
637 pmt_intern("underrun"),
639 if(pmt_eqv(underrun, PMT_T)) {
643 std::cout << "[TEST_USRP_INBAND_UNDERRUN] Underrun\n";
647 std::cout << "[TEST_USRP_INBAND_UNDERRUN] No underrun\n" << underrun <<std::endl;
652 std::cout << "[TEST_USRP_INBAND_UNDERRUN] No underrun\n";
658 test_usrp_inband_underrun::closing_channels()
660 d_state = CLOSING_CHANNELS;
662 d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
663 d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan));
667 test_usrp_inband_underrun::closing_usrp()
669 d_state = CLOSING_USRP;
671 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
674 REGISTER_MBLOCK_CLASS(test_usrp_inband_underrun);