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 <mb_runtime_nop.h> // QA only
29 #include <mblock/protocol_class.h>
30 #include <mblock/exception.h>
31 #include <mblock/msg_queue.h>
32 #include <mblock/message.h>
33 #include <mb_mblock_impl.h>
34 #include <mblock/msg_accepter.h>
35 #include <mblock/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_rx.h>
48 static bool verbose = true;
50 class test_usrp_rx : public mb_mblock
54 pmt_t d_rx_chan0, d_rx_chan1;
67 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),
91 d_rx_chan0(PMT_NIL), d_rx_chan1(PMT_NIL),
93 d_samples_to_recv(20e6)
95 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
96 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
98 // Pass a dictionary to usrp_server which specifies which interface to use, the stub or USRP
99 pmt_t usrp_dict = pmt_make_dict();
101 // To test the application without a USRP
102 bool fake_usrp_p = false;
104 pmt_dict_set(usrp_dict,
105 pmt_intern("fake-usrp"),
109 // Specify the RBF to use
110 pmt_dict_set(usrp_dict,
112 pmt_intern("inband_2rxhb_2tx.rbf"));
114 pmt_dict_set(usrp_dict,
115 pmt_intern("decim-rx"),
118 define_component("server", "usrp_server", usrp_dict);
120 connect("self", "rx0", "server", "rx0");
121 connect("self", "cs", "server", "cs");
125 test_usrp_rx::~test_usrp_rx()
130 test_usrp_rx::initial_transition()
136 test_usrp_rx::handle_message(mb_message_sptr msg)
138 pmt_t event = msg->signal();
139 pmt_t data = msg->data();
141 pmt_t handle = PMT_F;
142 pmt_t status = PMT_F;
143 std::string error_msg;
147 //----------------------------- OPENING_USRP ----------------------------//
148 // We only expect a response from opening the USRP which should be succesful
151 if (pmt_eq(event, s_response_open)){
152 status = pmt_nth(1, data);
153 if (pmt_eq(status, PMT_T)){
158 error_msg = "failed to open usrp:";
164 //----------------------- ALLOCATING CHANNELS --------------------//
165 // Allocate an RX channel to perform the overrun test.
166 case ALLOCATING_CHANNEL:
167 if (pmt_eq(event, s_response_allocate_channel)){
168 status = pmt_nth(1, data);
169 if(pmt_eqv(d_rx_chan0, PMT_NIL))
170 d_rx_chan0 = pmt_nth(2, data);
172 d_rx_chan1 = pmt_nth(2, data);
174 if (pmt_eq(status, PMT_T) && !pmt_eqv(d_rx_chan1, PMT_NIL)){
178 else if(pmt_eq(status, PMT_F)){
179 error_msg = "failed to allocate channel:";
186 //--------------------------- RECEIVING ------------------------------//
187 // In the receiving state, we receive samples until the specified amount
188 // while counting the number of overruns.
190 if (pmt_eq(event, s_response_recv_raw_samples)){
191 status = pmt_nth(1, data);
193 if (pmt_eq(status, PMT_T)){
194 handle_response_recv_raw_samples(data);
198 error_msg = "bad response-xmit-raw-frame:";
204 //------------------------- CLOSING CHANNEL ----------------------------//
205 // Check deallocation response for the RX channel
206 case CLOSING_CHANNEL:
207 if (pmt_eq(event, s_response_deallocate_channel)){
208 status = pmt_nth(1, data);
210 if (pmt_eq(status, PMT_T)){
215 error_msg = "failed to deallocate channel:";
220 // Alternately, we ignore all response recv samples while waiting for the
221 // channel to actually close
222 if (pmt_eq(event, s_response_recv_raw_samples))
227 //--------------------------- CLOSING USRP ------------------------------//
228 // Once we have received a successful USRP close response, we shutdown all
231 if (pmt_eq(event, s_response_close)){
232 status = pmt_nth(1, data);
234 if (pmt_eq(status, PMT_T)){
240 error_msg = "failed to close USRP:";
251 // An error occured, print it, and shutdown all m-blocks
253 std::cerr << error_msg << data
254 << "status = " << status << std::endl;
258 // Received an unhandled message for a specific state
260 if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
261 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
262 << "in state "<< d_state << std::endl;
267 test_usrp_rx::open_usrp()
269 pmt_t which_usrp = pmt_from_long(0);
271 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
272 d_state = OPENING_USRP;
275 std::cout << "[TEST_USRP_INBAND_RX] Opening the USRP\n";
279 test_usrp_rx::close_usrp()
282 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
283 d_state = CLOSING_USRP;
286 std::cout << "[TEST_USRP_INBAND_RX] Closing the USRP\n";
290 test_usrp_rx::allocate_channel()
292 long capacity = (long) 16e6;
293 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
294 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
295 d_state = ALLOCATING_CHANNEL;
298 std::cout << "[TEST_USRP_INBAND_RX] Requesting RX channel allocation\n";
302 test_usrp_rx::enter_receiving()
306 d_rx->send(s_cmd_start_recv_raw_samples,
310 d_rx->send(s_cmd_start_recv_raw_samples,
315 std::cout << "[TEST_USRP_INBAND_RX] Receiving...\n";
319 test_usrp_rx::handle_response_recv_raw_samples(pmt_t data)
321 pmt_t invocation_handle = pmt_nth(0, data);
322 pmt_t status = pmt_nth(1, data);
323 pmt_t v_samples = pmt_nth(2, data);
324 pmt_t timestamp = pmt_nth(3, data);
325 pmt_t channel = pmt_nth(4, data);
326 pmt_t properties = pmt_nth(5, data);
328 d_samples_recvd += pmt_length(v_samples) / 4;
331 if(!pmt_is_dict(properties)) {
332 std::cout << "[TEST_USRP_INBAND_RX] Recv samples dictionary is improper\n";
336 // Check if the number samples we have received meets the test
337 if(d_samples_recvd >= d_samples_to_recv) {
338 d_rx->send(s_cmd_stop_recv_raw_samples, pmt_list2(PMT_NIL, d_rx_chan0));
339 d_rx->send(s_cmd_stop_recv_raw_samples, pmt_list2(PMT_NIL, d_rx_chan1));
340 enter_closing_channel();
347 test_usrp_rx::enter_closing_channel()
349 d_state = CLOSING_CHANNEL;
351 d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan0));
352 d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan1));
355 std::cout << "[TEST_USRP_INBAND_RX] Deallocating RX channel\n";
358 REGISTER_MBLOCK_CLASS(test_usrp_rx);
361 // ----------------------------------------------------------------
364 main (int argc, char **argv)
366 mb_runtime_sptr rt = mb_make_runtime();
367 pmt_t result = PMT_NIL;
369 rt->run("top", "test_usrp_rx", PMT_F, &result);