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 <symbols_usrp_server_cs.h>
42 #include <symbols_usrp_channel.h>
43 #include <symbols_usrp_low_level_cs.h>
44 #include <symbols_usrp_tx.h>
45 #include <symbols_usrp_rx.h>
49 static bool verbose = false;
51 class test_usrp_cs : public mb_mblock
56 pmt_t d_tx_chan; // returned tx channel handle
57 pmt_t d_rx_chan; // returned tx channel handle
59 struct timeval times[NBPING];
71 long d_nsamples_to_send;
72 long d_nsamples_xmitted;
73 long d_nframes_xmitted;
74 long d_samples_per_frame;
77 // for generating sine wave output
78 ui_nco<float,float> d_nco;
82 test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
84 void initial_transition();
85 void handle_message(mb_message_sptr msg);
90 void allocate_channel();
92 void enter_receiving();
93 void enter_transmitting();
94 void build_and_send_ping();
95 void build_and_send_next_frame();
96 void handle_xmit_response(pmt_t invocation_handle);
97 void enter_closing_channel();
100 test_usrp_cs::test_usrp_cs(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
101 : mb_mblock(runtime, instance_name, user_arg),
104 d_state(INIT), d_nsamples_to_send((long) 40e6),
105 d_nsamples_xmitted(0),
106 d_nframes_xmitted(0),
107 //d_samples_per_frame((long)(126)),
108 //d_samples_per_frame((long)(126 * 3.5)), // non-full packet
109 d_samples_per_frame((long)(126 * 4)), // full packet
110 d_done_sending(false),
114 std::cout << "[TEST_USRP_INBAND_CS] Initializing...\n";
116 d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
117 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
118 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
120 //bool fake_usrp_p = true;
121 bool fake_usrp_p = false;
125 pmt_t usrp_dict = pmt_make_dict();
128 pmt_dict_set(usrp_dict,
129 pmt_intern("fake-usrp"),
133 // Set TX and RX interpolations
134 pmt_dict_set(usrp_dict,
135 pmt_intern("interp-tx"),
138 pmt_dict_set(usrp_dict,
139 pmt_intern("decim-rx"),
142 // Specify the RBF to use
143 pmt_dict_set(usrp_dict,
145 pmt_intern("boe.rbf"));
147 define_component("server", "usrp_server", usrp_dict);
149 connect("self", "tx0", "server", "tx0");
150 connect("self", "rx0", "server", "rx0");
151 connect("self", "cs", "server", "cs");
155 int interp = 32; // 32 -> 4MS/s
156 double sample_rate = 128e6 / interp;
157 d_nco.set_freq(2*M_PI * freq/sample_rate);
159 // FIXME need to somehow set the interp rate in the USRP.
160 // for now, we'll have the low-level code hardwire it.
163 test_usrp_cs::~test_usrp_cs()
168 test_usrp_cs::initial_transition()
174 test_usrp_cs::handle_message(mb_message_sptr msg)
176 pmt_t event = msg->signal();
177 pmt_t data = msg->data();
178 pmt_t port_id = msg->port_id();
180 pmt_t handle = PMT_F;
181 pmt_t status = PMT_F;
182 std::string error_msg;
184 //std::cout << msg << std::endl;
188 if (pmt_eq(event, s_response_open)){
189 status = pmt_nth(1, data);
190 if (pmt_eq(status, PMT_T)){
195 error_msg = "failed to open usrp:";
201 case ALLOCATING_CHANNEL:
202 if (pmt_eq(event, s_response_allocate_channel)){
204 if(pmt_eq(d_tx->port_symbol(), port_id)) {
205 status = pmt_nth(1, data);
206 d_tx_chan = pmt_nth(2, data);
208 if (pmt_eq(status, PMT_T)){
211 std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
213 if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
215 enter_transmitting();
220 error_msg = "failed to allocate channel:";
225 if(pmt_eq(d_rx->port_symbol(), port_id)) {
226 status = pmt_nth(1, data);
227 d_rx_chan = pmt_nth(2, data);
229 if (pmt_eq(status, PMT_T)){
232 std::cout << "[TEST_USRP_INBAND_CS] Received allocation for TX\n";
234 if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
236 enter_transmitting();
241 error_msg = "failed to allocate channel:";
249 if (pmt_eq(event, s_response_xmit_raw_frame)){
250 handle = pmt_nth(0, data);
251 status = pmt_nth(1, data);
253 if (pmt_eq(status, PMT_T)){
254 handle_xmit_response(handle);
258 error_msg = "bad response-xmit-raw-frame:";
264 case CLOSING_CHANNEL:
265 if (pmt_eq(event, s_response_deallocate_channel)){
266 status = pmt_nth(1, data);
268 if (pmt_eq(status, PMT_T)){
273 error_msg = "failed to deallocate channel:";
280 if (pmt_eq(event, s_response_close)){
281 status = pmt_nth(1, data);
283 if (pmt_eq(status, PMT_T)){
288 error_msg = "failed to close USRP:";
300 std::cerr << error_msg << data
301 << "status = " << status << std::endl;
307 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
308 << "in state "<< d_state << std::endl;
313 test_usrp_cs::open_usrp()
315 pmt_t which_usrp = pmt_from_long(0);
317 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
318 d_state = OPENING_USRP;
322 test_usrp_cs::close_usrp()
324 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
325 d_state = CLOSING_USRP;
328 std::cout << "[TEST_USRP_INBAND_CS] Closing USRP\n";
332 test_usrp_cs::allocate_channel()
334 long capacity = (long) 16e6;
335 d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
336 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
337 d_state = ALLOCATING_CHANNEL;
341 test_usrp_cs::enter_receiving()
343 d_rx->send(s_cmd_start_recv_raw_samples,
349 test_usrp_cs::enter_transmitting()
351 d_state = TRANSMITTING;
352 d_nsamples_xmitted = 0;
355 std::cout << "[TEST_USRP_INBAND_CS] Beginning transmission\n";
359 // build_and_send_next_frame(); // fire off 4 to start pipeline
361 build_and_send_ping();
362 build_and_send_ping();
363 build_and_send_ping();
368 test_usrp_cs::build_and_send_ping()
371 d_tx->send(s_cmd_to_control_channel,
372 pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
373 pmt_list2(pmt_from_long(0),
374 pmt_from_long(0))))));
376 std::cout << "[TEST_USRP_INBAND_CS] Ping sent" << std::endl;
380 test_usrp_cs::build_and_send_next_frame()
382 // allocate the uniform vector for the samples
383 // FIXME perhaps hold on to this between calls
386 long nsamples_this_frame =
387 std::min(d_nsamples_to_send - d_nsamples_xmitted,
388 d_samples_per_frame);
390 long nsamples_this_frame = d_samples_per_frame;
393 if (nsamples_this_frame == 0){
394 d_done_sending = true;
399 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
400 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
402 int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
404 // fill in the complex sinusoid
406 for (int i = 0; i < nsamples_this_frame; i++){
410 d_nco.sincos(&s, 1, d_amplitude);
411 // write 16-bit i & q
412 samples[2*i] = (int16_t) s.real();
413 samples[2*i+1] = (int16_t) s.imag();
416 gr_complex s(d_amplitude, d_amplitude);
418 // write 16-bit i & q
419 samples[2*i] = (int16_t) s.real();
420 samples[2*i+1] = (int16_t) s.imag();
424 pmt_t timestamp = pmt_from_long(0xffffffff); // NOW
425 d_tx->send(s_cmd_xmit_raw_frame,
426 pmt_list4(pmt_from_long(d_nframes_xmitted), // invocation-handle
427 d_tx_chan, // channel
431 d_nsamples_xmitted += nsamples_this_frame;
435 std::cout << "[TEST_USRP_INBAND_CS] Transmitted frame\n";
440 test_usrp_cs::handle_xmit_response(pmt_t handle)
442 if (d_done_sending &&
443 pmt_to_long(handle) == (d_nframes_xmitted - 1)){
444 // We're done sending and have received all responses
445 enter_closing_channel();
448 //build_and_send_next_frame();
452 test_usrp_cs::enter_closing_channel()
454 d_state = CLOSING_CHANNEL;
456 d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
459 std::cout << "[TEST_USRP_INBAND_CS] Closing channel\n";
462 REGISTER_MBLOCK_CLASS(test_usrp_cs);
465 // ----------------------------------------------------------------
468 main (int argc, char **argv)
470 // handle any command line args here
472 mb_runtime_sptr rt = mb_make_runtime();
473 pmt_t result = PMT_NIL;
475 rt->run("top", "test_usrp_cs", PMT_F, &result);