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 = true;
56 class test_usrp_inband_timestamps : public mb_mblock
61 pmt_t d_tx_chan; // returned tx channel handle
62 pmt_t d_rx_chan; // returned tx channel handle
64 struct timeval times[NBPING];
76 long d_nsamples_to_send;
77 long d_nsamples_xmitted;
78 long d_nframes_xmitted;
79 long d_samples_per_frame;
82 // for generating sine wave output
83 ui_nco<float,float> d_nco;
87 test_usrp_inband_timestamps(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
88 ~test_usrp_inband_timestamps();
89 void initial_transition();
90 void handle_message(mb_message_sptr msg);
95 void allocate_channel();
97 void enter_receiving();
98 void enter_transmitting();
99 void build_and_send_ping();
100 void build_and_send_next_frame();
101 void handle_xmit_response(pmt_t invocation_handle);
102 void enter_closing_channel();
105 test_usrp_inband_timestamps::test_usrp_inband_timestamps(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
106 : mb_mblock(runtime, instance_name, user_arg),
109 d_state(INIT), d_nsamples_to_send((long) 40e6),
110 d_nsamples_xmitted(0),
111 d_nframes_xmitted(0),
112 //d_samples_per_frame((long)(126)),
113 d_samples_per_frame((long)(126 * 2)), // non-full packet
114 //d_samples_per_frame((long)(126 * 3.5)), // non-full packet
115 //d_samples_per_frame((long)(126 * 4)), // full packet
116 d_done_sending(false),
120 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Initializing...\n";
122 d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
123 d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
124 d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
126 bool fake_usrp_p = false;
130 pmt_t usrp_dict = pmt_make_dict();
133 pmt_dict_set(usrp_dict,
134 pmt_intern("fake-usrp"),
138 // Set TX and RX interpolations
139 pmt_dict_set(usrp_dict,
140 pmt_intern("interp-tx"),
143 pmt_dict_set(usrp_dict,
144 pmt_intern("decim-rx"),
147 // Specify the RBF to use
148 pmt_dict_set(usrp_dict,
150 pmt_intern("tmac5.rbf"));
152 define_component("server", "usrp_server", usrp_dict);
154 connect("self", "tx0", "server", "tx0");
155 connect("self", "rx0", "server", "rx0");
156 connect("self", "cs", "server", "cs");
160 int interp = 32; // 32 -> 4MS/s
161 double sample_rate = 128e6 / interp;
162 d_nco.set_freq(2*M_PI * freq/sample_rate);
166 test_usrp_inband_timestamps::~test_usrp_inband_timestamps()
171 test_usrp_inband_timestamps::initial_transition()
177 test_usrp_inband_timestamps::handle_message(mb_message_sptr msg)
179 pmt_t event = msg->signal();
180 pmt_t data = msg->data();
181 pmt_t port_id = msg->port_id();
183 pmt_t handle = PMT_F;
184 pmt_t status = PMT_F;
185 std::string error_msg;
187 //std::cout << msg << std::endl;
191 if (pmt_eq(event, s_response_open)){
192 status = pmt_nth(1, data);
193 if (pmt_eq(status, PMT_T)){
198 error_msg = "failed to open usrp:";
204 case ALLOCATING_CHANNEL:
205 if (pmt_eq(event, s_response_allocate_channel)){
207 if(pmt_eq(d_tx->port_symbol(), port_id)) {
208 status = pmt_nth(1, data);
209 d_tx_chan = pmt_nth(2, data);
211 if (pmt_eq(status, PMT_T)){
214 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Received allocation for TX\n";
216 if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
218 enter_transmitting();
223 error_msg = "failed to allocate channel:";
228 if(pmt_eq(d_rx->port_symbol(), port_id)) {
229 status = pmt_nth(1, data);
230 d_rx_chan = pmt_nth(2, data);
232 if (pmt_eq(status, PMT_T)){
235 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Received allocation for TX\n";
237 if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
239 enter_transmitting();
244 error_msg = "failed to allocate channel:";
252 if (pmt_eq(event, s_response_xmit_raw_frame)){
253 handle = pmt_nth(0, data);
254 status = pmt_nth(1, data);
256 if (pmt_eq(status, PMT_T)){
257 handle_xmit_response(handle);
261 error_msg = "bad response-xmit-raw-frame:";
266 if (pmt_eq(event, s_response_from_control_channel)) {
267 std::cout << "ping response!\n";
271 case CLOSING_CHANNEL:
272 if (pmt_eq(event, s_response_deallocate_channel)){
273 status = pmt_nth(1, data);
275 if (pmt_eq(status, PMT_T)){
280 error_msg = "failed to deallocate channel:";
287 if (pmt_eq(event, s_response_close)){
288 status = pmt_nth(1, data);
290 if (pmt_eq(status, PMT_T)){
295 error_msg = "failed to close USRP:";
307 std::cerr << error_msg << data
308 << "status = " << status << std::endl;
314 std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
315 << "in state "<< d_state << std::endl;
320 test_usrp_inband_timestamps::open_usrp()
322 pmt_t which_usrp = pmt_from_long(0);
324 d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, which_usrp));
325 d_state = OPENING_USRP;
329 test_usrp_inband_timestamps::close_usrp()
331 d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
332 d_state = CLOSING_USRP;
335 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Closing USRP\n";
339 test_usrp_inband_timestamps::allocate_channel()
341 long capacity = (long) 16e6;
342 d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
343 d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
344 d_state = ALLOCATING_CHANNEL;
348 test_usrp_inband_timestamps::enter_receiving()
350 d_rx->send(s_cmd_start_recv_raw_samples,
356 test_usrp_inband_timestamps::enter_transmitting()
358 d_state = TRANSMITTING;
359 d_nsamples_xmitted = 0;
362 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Beginning transmission\n";
366 build_and_send_next_frame();
367 build_and_send_next_frame();
368 build_and_send_next_frame();
369 build_and_send_next_frame();
374 test_usrp_inband_timestamps::build_and_send_ping()
377 d_tx->send(s_cmd_to_control_channel,
378 pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
379 pmt_list2(pmt_from_long(0),
380 pmt_from_long(0))))));
382 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Ping sent" << std::endl;
386 test_usrp_inband_timestamps::build_and_send_next_frame()
388 // allocate the uniform vector for the samples
389 // FIXME perhaps hold on to this between calls
392 long nsamples_this_frame =
393 std::min(d_nsamples_to_send - d_nsamples_xmitted,
394 d_samples_per_frame);
396 long nsamples_this_frame = d_samples_per_frame;
399 if (nsamples_this_frame == 0){
400 d_done_sending = true;
405 size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
406 pmt_t uvec = pmt_make_s16vector(nshorts, 0);
408 int16_t *samples = pmt_s16vector_writeable_elements(uvec, ignore);
410 // fill in the complex sinusoid
412 for (int i = 0; i < nsamples_this_frame; i++){
416 d_nco.sincos(&s, 1, d_amplitude);
417 // write 16-bit i & q
418 samples[2*i] = (int16_t) s.real();
419 samples[2*i+1] = (int16_t) s.imag();
422 gr_complex s(d_amplitude, d_amplitude);
424 // write 16-bit i & q
425 samples[2*i] = (int16_t) s.real();
426 samples[2*i+1] = (int16_t) s.imag();
433 timestamp = pmt_from_long(0x0); // throw away
440 timestamp = pmt_from_long(0xffffffff); // NOW
441 timestamp = pmt_from_long(ptime);
450 std::cout << bskip << " -- " << bcurr << std::endl;
452 d_tx->send(s_cmd_xmit_raw_frame,
453 pmt_list4(pmt_from_long(d_nframes_xmitted), // invocation-handle
454 d_tx_chan, // channel
458 d_nsamples_xmitted += nsamples_this_frame;
462 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Transmitted frame\n";
464 //build_and_send_next_frame();
469 test_usrp_inband_timestamps::handle_xmit_response(pmt_t handle)
471 if (d_done_sending &&
472 pmt_to_long(handle) == (d_nframes_xmitted - 1)){
473 // We're done sending and have received all responses
474 enter_closing_channel();
477 build_and_send_next_frame();
478 //build_and_send_ping();
482 test_usrp_inband_timestamps::enter_closing_channel()
484 d_state = CLOSING_CHANNEL;
486 d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
489 std::cout << "[TEST_USRP_INBAND_TIMESTAMPS] Closing channel\n";
492 REGISTER_MBLOCK_CLASS(test_usrp_inband_timestamps);
495 // ----------------------------------------------------------------
498 main (int argc, char **argv)
500 // handle any command line args here
502 mb_runtime_sptr rt = mb_make_runtime();
503 pmt_t result = PMT_NIL;
505 rt->run("top", "test_usrp_inband_timestamps", PMT_F, &result);