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>
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_rx.h>
46 static bool verbose = true;
48 class test_usrp_rx : public mb_mblock
52 pmt_t d_rx_chan; // returned tx channel handle
65 std::ofstream d_ofile;
70 long d_samples_to_recv;
73 test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
75 void initial_transition();
76 void handle_message(mb_message_sptr msg);
81 void allocate_channel();
83 void enter_receiving();
84 void build_and_send_next_frame();
85 void handle_response_recv_raw_samples(pmt_t invocation_handle);
86 void enter_closing_channel();
89 test_usrp_rx::test_usrp_rx(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
90 : mb_mblock(runtime, instance_name, user_arg),
93 d_samples_to_recv(10e6)
96 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
97 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
99 // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
100 pmt_t usrp_dict = pmt_make_dict();
102 // Specify the RBF to use
103 pmt_dict_set(usrp_dict,
105 pmt_intern("nanocell9.rbf"));
107 pmt_dict_set(usrp_dict,
108 pmt_intern("decim-rx"),
111 define_component("server", "usrp_server", usrp_dict);
113 connect("self", "rx0", "server", "rx0");
114 connect("self", "cs", "server", "cs");
118 test_usrp_rx::~test_usrp_rx()
123 test_usrp_rx::initial_transition()
129 test_usrp_rx::handle_message(mb_message_sptr msg)
131 pmt_t event = msg->signal();
132 pmt_t data = msg->data();
134 pmt_t handle = PMT_F;
135 pmt_t status = PMT_F;
136 std::string error_msg;
140 //----------------------------- OPENING_USRP ----------------------------//
141 // We only expect a response from opening the USRP which should be succesful
144 if (pmt_eq(event, s_response_open)){
145 status = pmt_nth(1, data);
146 if (pmt_eq(status, PMT_T)){
151 error_msg = "failed to open usrp:";
157 //----------------------- ALLOCATING CHANNELS --------------------//
158 // Allocate an RX channel to perform the overrun test.
159 case ALLOCATING_CHANNEL:
160 if (pmt_eq(event, s_response_allocate_channel)){
161 status = pmt_nth(1, data);
162 d_rx_chan = pmt_nth(2, data);
164 if (pmt_eq(status, PMT_T)){
169 error_msg = "failed to allocate channel:";
175 //--------------------------- RECEIVING ------------------------------//
176 // In the receiving state, we receive samples until the specified amount
177 // while counting the number of overruns.
179 if (pmt_eq(event, s_response_recv_raw_samples)){
180 status = pmt_nth(1, data);
182 if (pmt_eq(status, PMT_T)){
183 handle_response_recv_raw_samples(data);
187 error_msg = "bad response-xmit-raw-frame:";
193 //------------------------- CLOSING CHANNEL ----------------------------//
194 // Check deallocation response for the RX channel
195 case CLOSING_CHANNEL:
196 if (pmt_eq(event, s_response_deallocate_channel)){
197 status = pmt_nth(1, data);
199 if (pmt_eq(status, PMT_T)){
204 error_msg = "failed to deallocate channel:";
209 // Alternately, we ignore all response recv samples while waiting for the
210 // channel to actually close
211 if (pmt_eq(event, s_response_recv_raw_samples))
216 //--------------------------- CLOSING USRP ------------------------------//
217 // Once we have received a successful USRP close response, we shutdown all
220 if (pmt_eq(event, s_response_close)){
221 status = pmt_nth(1, data);
223 if (pmt_eq(status, PMT_T)){
224 std::cout << "\nOverruns: " << d_n_overruns << std::endl;
230 error_msg = "failed to close USRP:";
241 // An error occured, print it, and shutdown all m-blocks
243 std::cerr << error_msg << data
244 << "status = " << status << std::endl;
248 // Received an unhandled message for a specific state
250 if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
251 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
252 << "in state "<< d_state << std::endl;
257 test_usrp_rx::open_usrp()
259 pmt_t which_usrp = pmt_from_long(0);
261 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
262 d_state = OPENING_USRP;
265 std::cout << "[TEST_USRP_INBAND_OVERRUN] Opening the USRP\n";
269 test_usrp_rx::close_usrp()
271 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
272 d_state = CLOSING_USRP;
275 std::cout << "[TEST_USRP_INBAND_OVERRUN] Closing the USRP\n";
279 test_usrp_rx::allocate_channel()
281 long capacity = (long) 16e6;
282 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
283 d_state = ALLOCATING_CHANNEL;
286 std::cout << "[TEST_USRP_INBAND_OVERRUN] Requesting RX channel allocation\n";
290 test_usrp_rx::enter_receiving()
294 d_rx->send(s_cmd_start_recv_raw_samples,
299 std::cout << "[TEST_USRP_INBAND_OVERRUN] Receiving...\n";
303 test_usrp_rx::handle_response_recv_raw_samples(pmt_t data)
305 pmt_t invocation_handle = pmt_nth(0, data);
306 pmt_t status = pmt_nth(1, data);
307 pmt_t v_samples = pmt_nth(2, data);
308 pmt_t timestamp = pmt_nth(3, data);
309 pmt_t properties = pmt_nth(4, data);
311 d_samples_recvd += pmt_length(v_samples) / 4;
314 if(!pmt_is_dict(properties)) {
315 std::cout << "[TEST_USRP_INBAND_OVERRUN] Recv samples dictionary is improper\n";
319 if(pmt_t overrun = pmt_dict_ref(properties,
320 pmt_intern("overrun"),
322 if(pmt_eqv(overrun, PMT_T)) {
326 std::cout << "[TEST_USRP_INBAND_OVERRUN] Underrun\n";
330 std::cout << "[TEST_USRP_INBAND_OVERRUN] No overrun\n" << overrun <<std::endl;
335 std::cout << "[TEST_USRP_INBAND_OVERRUN] No overrun\n";
338 // Check if the number samples we have received meets the test
339 if(d_samples_recvd >= d_samples_to_recv) {
340 d_rx->send(s_cmd_stop_recv_raw_samples, pmt_list2(PMT_NIL, d_rx_chan));
341 enter_closing_channel();
348 test_usrp_rx::enter_closing_channel()
350 d_state = CLOSING_CHANNEL;
354 d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan));
357 std::cout << "[TEST_USRP_INBAND_OVERRUN] Deallocating RX channel\n";
360 REGISTER_MBLOCK_CLASS(test_usrp_rx);
363 // ----------------------------------------------------------------
366 main (int argc, char **argv)
368 // handle any command line args here
370 mb_runtime_sptr rt = mb_make_runtime();
371 pmt_t result = PMT_NIL;
373 rt->run("top", "test_usrp_rx", PMT_F, &result);