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_runtime_nop.h> // QA only
29 #include <mb_protocol_class.h>
30 #include <mb_exception.h>
31 #include <mb_msg_queue.h>
32 #include <mb_message.h>
33 #include <mb_mblock_impl.h>
34 #include <mb_msg_accepter.h>
35 #include <mb_class_registry.h>
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_tx.h>
47 #include <symbols_usrp_rx.h>
51 static bool verbose = false;
53 class test_usrp_cs : public mb_mblock
58 pmt_t d_tx_chan; // returned tx channel handle
59 pmt_t d_rx_chan; // returned tx channel handle
61 struct timeval times[NBPING];
73 long d_nsamples_to_send;
74 long d_nsamples_xmitted;
75 long d_nframes_xmitted;
76 long d_samples_per_frame;
79 // for generating sine wave output
80 ui_nco<float,float> d_nco;
84 test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
86 void initial_transition();
87 void handle_message(mb_message_sptr msg);
92 void allocate_channel();
94 void enter_receiving();
95 void enter_transmitting();
96 void build_and_send_ping();
97 void build_and_send_next_frame();
98 void handle_xmit_response(pmt_t invocation_handle);
99 void enter_closing_channel();
102 test_usrp_cs::test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
103 : mb_mblock(runtime, instance_name, user_arg),
106 d_state(INIT), d_nsamples_to_send((long) 40e6),
107 d_nsamples_xmitted(0),
108 d_nframes_xmitted(0),
109 //d_samples_per_frame((long)(126)),
110 //d_samples_per_frame((long)(126 * 3.5)), // non-full packet
111 d_samples_per_frame((long)(126 * 4)), // full packet
112 d_done_sending(false),
116 std::cout << "[TEST_USRP_INBAND_CS] Initializing...\n";
118 d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
119 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
120 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
122 //bool fake_usrp_p = true;
123 bool fake_usrp_p = false;
127 pmt_t usrp_dict = pmt_make_dict();
130 pmt_dict_set(usrp_dict,
131 pmt_intern("fake-usrp"),
135 // Set TX and RX interpolations
136 pmt_dict_set(usrp_dict,
137 pmt_intern("interp-tx"),
140 pmt_dict_set(usrp_dict,
141 pmt_intern("interp-rx"),
144 // Specify the RBF to use
145 pmt_dict_set(usrp_dict,
147 pmt_intern("boe.rbf"));
149 define_component("server", "usrp_server", usrp_dict);
151 connect("self", "tx0", "server", "tx0");
152 connect("self", "rx0", "server", "rx0");
153 connect("self", "cs", "server", "cs");
157 int interp = 32; // 32 -> 4MS/s
158 double sample_rate = 128e6 / interp;
159 d_nco.set_freq(2*M_PI * freq/sample_rate);
161 // FIXME need to somehow set the interp rate in the USRP.
162 // for now, we'll have the low-level code hardwire it.
165 test_usrp_cs::~test_usrp_cs()
170 test_usrp_cs::initial_transition()
176 test_usrp_cs::handle_message(mb_message_sptr msg)
178 pmt_t event = msg->signal();
179 pmt_t data = msg->data();
180 pmt_t port_id = msg->port_id();
182 pmt_t handle = PMT_F;
183 pmt_t status = PMT_F;
184 std::string error_msg;
186 //std::cout << msg << std::endl;
190 if (pmt_eq(event, s_response_open)){
191 status = pmt_nth(1, data);
192 if (pmt_eq(status, PMT_T)){
197 error_msg = "failed to open usrp:";
203 case ALLOCATING_CHANNEL:
204 if (pmt_eq(event, s_response_allocate_channel)){
206 if(pmt_eq(d_tx->port_symbol(), port_id)) {
207 status = pmt_nth(1, data);
208 d_tx_chan = pmt_nth(2, data);
210 if (pmt_eq(status, PMT_T)){
213 std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
215 if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
217 enter_transmitting();
222 error_msg = "failed to allocate channel:";
227 if(pmt_eq(d_rx->port_symbol(), port_id)) {
228 status = pmt_nth(1, data);
229 d_rx_chan = pmt_nth(2, data);
231 if (pmt_eq(status, PMT_T)){
234 std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
236 if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
238 enter_transmitting();
243 error_msg = "failed to allocate channel:";
251 if (pmt_eq(event, s_response_xmit_raw_frame)){
252 handle = pmt_nth(0, data);
253 status = pmt_nth(1, data);
255 if (pmt_eq(status, PMT_T)){
256 handle_xmit_response(handle);
260 error_msg = "bad response-xmit-raw-frame:";
266 case CLOSING_CHANNEL:
267 if (pmt_eq(event, s_response_deallocate_channel)){
268 status = pmt_nth(1, data);
270 if (pmt_eq(status, PMT_T)){
275 error_msg = "failed to deallocate channel:";
282 if (pmt_eq(event, s_response_close)){
283 status = pmt_nth(1, data);
285 if (pmt_eq(status, PMT_T)){
290 error_msg = "failed to close USRP:";
302 std::cerr << error_msg << data
303 << "status = " << status << std::endl;
309 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
310 << "in state "<< d_state << std::endl;
315 test_usrp_cs::open_usrp()
317 pmt_t which_usrp = pmt_from_long(0);
319 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
320 d_state = OPENING_USRP;
324 test_usrp_cs::close_usrp()
326 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
327 d_state = CLOSING_USRP;
330 std::cout << "[TEST_USRP_INBAND_CS] Closing USRP\n";
334 test_usrp_cs::allocate_channel()
336 long capacity = (long) 16e6;
337 d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
338 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
339 d_state = ALLOCATING_CHANNEL;
343 test_usrp_cs::enter_receiving()
345 d_rx->send(s_cmd_start_recv_raw_samples,
351 test_usrp_cs::enter_transmitting()
353 d_state = TRANSMITTING;
354 d_nsamples_xmitted = 0;
357 std::cout << "[TEST_USRP_INBAND_CS] Beginning transmission\n";
361 // build_and_send_next_frame(); // fire off 4 to start pipeline
363 build_and_send_ping();
364 build_and_send_ping();
365 build_and_send_ping();
370 test_usrp_cs::build_and_send_ping()
373 d_tx->send(s_cmd_to_control_channel,
374 pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
375 pmt_list2(pmt_from_long(0),
376 pmt_from_long(0))))));
378 std::cout << "[TEST_USRP_INBAND_CS] Ping sent" << std::endl;
382 test_usrp_cs::build_and_send_next_frame()
384 // allocate the uniform vector for the samples
385 // FIXME perhaps hold on to this between calls
388 long nsamples_this_frame =
389 std::min(d_nsamples_to_send - d_nsamples_xmitted,
390 d_samples_per_frame);
392 long nsamples_this_frame = d_samples_per_frame;
395 if (nsamples_this_frame == 0){
396 d_done_sending = true;
401 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
402 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
404 int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
406 // fill in the complex sinusoid
408 for (int i = 0; i < nsamples_this_frame; i++){
412 d_nco.sincos(&s, 1, d_amplitude);
413 // write 16-bit i & q
414 samples[2*i] = (int16_t) s.real();
415 samples[2*i+1] = (int16_t) s.imag();
418 gr_complex s(d_amplitude, d_amplitude);
420 // write 16-bit i & q
421 samples[2*i] = (int16_t) s.real();
422 samples[2*i+1] = (int16_t) s.imag();
426 pmt_t timestamp = pmt_from_long(0xffffffff); // NOW
427 d_tx->send(s_cmd_xmit_raw_frame,
428 pmt_list4(pmt_from_long(d_nframes_xmitted), // invocation-handle
429 d_tx_chan, // channel
433 d_nsamples_xmitted += nsamples_this_frame;
437 std::cout << "[TEST_USRP_INBAND_CS] Transmitted frame\n";
442 test_usrp_cs::handle_xmit_response(pmt_t handle)
444 if (d_done_sending &&
445 pmt_to_long(handle) == (d_nframes_xmitted - 1)){
446 // We're done sending and have received all responses
447 enter_closing_channel();
450 //build_and_send_next_frame();
454 test_usrp_cs::enter_closing_channel()
456 d_state = CLOSING_CHANNEL;
458 d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
461 std::cout << "[TEST_USRP_INBAND_CS] Closing channel\n";
464 REGISTER_MBLOCK_CLASS(test_usrp_cs);
467 // ----------------------------------------------------------------
470 main (int argc, char **argv)
472 // handle any command line args here
474 mb_runtime_sptr rt = mb_make_runtime();
475 pmt_t result = PMT_NIL;
477 rt->run("top", "test_usrp_cs", PMT_F, &result);