1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
/* -*- c++ -*- */
/*
* Copyright 2007,2008 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mblock/mblock.h>
#include <mblock/runtime.h>
#include <mblock/protocol_class.h>
#include <mblock/exception.h>
#include <mblock/msg_queue.h>
#include <mblock/message.h>
#include <mblock/msg_accepter.h>
#include <mblock/class_registry.h>
#include <pmt.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <iostream>
#include <ui_nco.h>
// Include the symbols needed for communication with USRP server
#include <symbols_usrp_server_cs.h>
#include <symbols_usrp_channel.h>
#include <symbols_usrp_low_level_cs.h>
#include <symbols_usrp_tx.h>
#include <symbols_usrp_rx.h>
static bool verbose = true;
class test_usrp_inband_underrun : public mb_mblock
{
mb_port_sptr d_tx; // Ports connected to the USRP server
mb_port_sptr d_rx;
mb_port_sptr d_cs;
pmt_t d_tx_chan; // Returned channel from TX allocation
pmt_t d_rx_chan; // Returned channel from RX allocation
pmt_t d_which_usrp; // The USRP to use for the test
long d_warm_msgs; // The number of messages to 'warm' the USRP
long d_warm_recvd; // The number of msgs received in the 'warm' state
// Keep track of current state
enum state_t {
INIT,
OPENING_USRP,
ALLOCATING_CHANNELS,
WRITE_REGISTER,
READ_REGISTER,
TRANSMITTING,
CLOSING_CHANNELS,
CLOSING_USRP,
};
state_t d_state;
long d_nsamples_to_send;
long d_nsamples_xmitted;
long d_nframes_xmitted;
long d_samples_per_frame;
bool d_done_sending;
// for generating sine wave output
ui_nco<float,float> d_nco;
double d_amplitude;
long d_n_underruns;
public:
test_usrp_inband_underrun(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg);
~test_usrp_inband_underrun();
void initial_transition();
void handle_message(mb_message_sptr msg);
protected:
void opening_usrp();
void allocating_channels();
void write_register();
void read_register();
void closing_channels();
void closing_usrp();
void enter_receiving();
void enter_transmitting();
void build_and_send_ping();
void build_and_send_next_frame();
void handle_xmit_response(pmt_t handle);
void handle_recv_response(pmt_t dict);
};
int
main (int argc, char **argv)
{
// handle any command line args here
mb_runtime_sptr rt = mb_make_runtime();
pmt_t result = PMT_NIL;
rt->run("top", "test_usrp_inband_underrun", PMT_F, &result);
}
test_usrp_inband_underrun::test_usrp_inband_underrun(mb_runtime *runtime, const std::string &instance_name, pmt_t user_arg)
: mb_mblock(runtime, instance_name, user_arg),
d_tx_chan(PMT_NIL),
d_rx_chan(PMT_NIL),
d_which_usrp(pmt_from_long(0)),
d_state(INIT),
d_nsamples_to_send((long) 27e6),
d_nsamples_xmitted(0),
d_nframes_xmitted(0),
d_samples_per_frame(d_nsamples_to_send), // full packet
d_done_sending(false),
d_amplitude(16384),
d_n_underruns(0)
{
// A dictionary is used to pass parameters to the USRP
pmt_t usrp_dict = pmt_make_dict();
// Specify the RBF to use
pmt_dict_set(usrp_dict,
pmt_intern("rbf"),
pmt_intern("inband_1rxhb_1tx.rbf"));
// Set TX and RX interpolations
pmt_dict_set(usrp_dict,
pmt_intern("interp-tx"),
pmt_from_long(64));
pmt_dict_set(usrp_dict,
pmt_intern("decim-rx"),
pmt_from_long(128));
d_tx = define_port("tx0", "usrp-tx", false, mb_port::INTERNAL);
d_rx = define_port("rx0", "usrp-rx", false, mb_port::INTERNAL);
d_cs = define_port("cs", "usrp-server-cs", false, mb_port::INTERNAL);
// Create an instance of USRP server and connect ports
define_component("server", "usrp_server", usrp_dict);
connect("self", "tx0", "server", "tx0");
connect("self", "rx0", "server", "rx0");
connect("self", "cs", "server", "cs");
// initialize NCO
double freq = 100e3;
int interp = 32; // 32 -> 4MS/s
double sample_rate = 128e6 / interp;
d_nco.set_freq(2*M_PI * freq/sample_rate);
}
test_usrp_inband_underrun::~test_usrp_inband_underrun()
{
}
void
test_usrp_inband_underrun::initial_transition()
{
opening_usrp();
}
// Handle message reads all incoming messages from USRP server which will be
// initialization and ping responses. We perform actions based on the current
// state and the event (ie, ping response)
void
test_usrp_inband_underrun::handle_message(mb_message_sptr msg)
{
pmt_t event = msg->signal();
pmt_t data = msg->data();
pmt_t port_id = msg->port_id();
pmt_t handle = PMT_F;
pmt_t status = PMT_F;
pmt_t dict = PMT_NIL;
std::string error_msg;
// Check the recv sample responses for underruns and count
if(pmt_eq(event, s_response_recv_raw_samples)) {
handle = pmt_nth(0, data);
status = pmt_nth(1, data);
dict = pmt_nth(4, data);
if(pmt_eq(status, PMT_T)) {
handle_recv_response(dict);
return;
}
else {
error_msg = "error while receiving samples:";
goto bail;
}
}
// Dispatch based on state
switch(d_state) {
//----------------------------- OPENING_USRP ----------------------------//
// We only expect a response from opening the USRP which should be succesful
// or failed.
case OPENING_USRP:
if(pmt_eq(event, s_response_open)) {
status = pmt_nth(1, data); // failed/succes
if(pmt_eq(status, PMT_T)) {
allocating_channels();
return;
}
else {
error_msg = "failed to open usrp:";
goto bail;
}
}
goto unhandled; // all other messages not handled in this state
//----------------------- ALLOCATING CHANNELS --------------------//
// When allocating channels, we need to wait for 2 responses from
// USRP server: one for TX and one for RX. Both are initialized to
// NIL so we know to continue to the next state once both are set.
case ALLOCATING_CHANNELS:
// A TX allocation response
if(pmt_eq(event, s_response_allocate_channel)
&& pmt_eq(d_tx->port_symbol(), port_id))
{
status = pmt_nth(1, data);
// If successful response, extract the channel
if(pmt_eq(status, PMT_T)) {
d_tx_chan = pmt_nth(2, data);
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received TX allocation"
<< " on channel " << d_tx_chan << std::endl;
// If the RX has also been allocated already, we can continue
if(!pmt_eqv(d_rx_chan, PMT_NIL)) {
enter_receiving();
enter_transmitting();
}
return;
}
else { // TX allocation failed
error_msg = "failed to allocate TX channel:";
goto bail;
}
}
// A RX allocation response
if(pmt_eq(event, s_response_allocate_channel)
&& pmt_eq(d_rx->port_symbol(), port_id))
{
status = pmt_nth(1, data);
// If successful response, extract the channel
if(pmt_eq(status, PMT_T)) {
d_rx_chan = pmt_nth(2, data);
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received RX allocation"
<< " on channel " << d_rx_chan << std::endl;
// If the TX has also been allocated already, we can continue
if(!pmt_eqv(d_tx_chan, PMT_NIL)) {
enter_receiving();
enter_transmitting();
}
return;
}
else { // RX allocation failed
error_msg = "failed to allocate RX channel:";
goto bail;
}
}
goto unhandled;
case WRITE_REGISTER:
goto unhandled;
case READ_REGISTER:
goto unhandled;
//-------------------------- TRANSMITTING ----------------------------//
// In the transmit state we count the number of underruns received and
// ballpark the number with an expected count (something >1 for starters)
case TRANSMITTING:
// Check that the transmits are OK
if (pmt_eq(event, s_response_xmit_raw_frame)){
handle = pmt_nth(0, data);
status = pmt_nth(1, data);
if (pmt_eq(status, PMT_T)){
handle_xmit_response(handle);
return;
}
else {
error_msg = "bad response-xmit-raw-frame:";
goto bail;
}
}
goto unhandled;
//------------------------- CLOSING CHANNELS ----------------------------//
// Check deallocation responses, once the TX and RX channels are both
// deallocated then we close the USRP.
case CLOSING_CHANNELS:
if (pmt_eq(event, s_response_deallocate_channel)
&& pmt_eq(d_tx->port_symbol(), port_id))
{
status = pmt_nth(1, data);
// If successful, set the port to NIL
if(pmt_eq(status, PMT_T)) {
d_tx_chan = PMT_NIL;
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received TX deallocation\n";
// If the RX is also deallocated, we can close the USRP
if(pmt_eq(d_rx_chan, PMT_NIL))
closing_usrp();
return;
} else {
error_msg = "failed to deallocate TX channel:";
goto bail;
}
}
if (pmt_eq(event, s_response_deallocate_channel)
&& pmt_eq(d_rx->port_symbol(), port_id))
{
status = pmt_nth(1, data);
// If successful, set the port to NIL
if(pmt_eq(status, PMT_T)) {
d_rx_chan = PMT_NIL;
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Received RX deallocation\n";
// If the TX is also deallocated, we can close the USRP
if(pmt_eq(d_tx_chan, PMT_NIL))
closing_usrp();
return;
} else {
error_msg = "failed to deallocate RX channel:";
goto bail;
}
}
goto unhandled;
//--------------------------- CLOSING USRP ------------------------------//
// Once we have received a successful USRP close response, we shutdown all
// mblocks and exit.
case CLOSING_USRP:
if (pmt_eq(event, s_response_close)) {
status = pmt_nth(1, data);
if(pmt_eq(status, PMT_T)) {
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Successfully closed USRP\n";
std::cout << "\nUnderruns: " << d_n_underruns << std::endl;
fflush(stdout);
shutdown_all(PMT_T);
return;
} else {
error_msg = "failed to close USRP:";
goto bail;
}
}
goto unhandled;
case INIT:
goto unhandled;
}
// An error occured, print it, and shutdown all m-blocks
bail:
std::cerr << error_msg << data
<< "status = " << status << std::endl;
shutdown_all(PMT_F);
return;
// Received an unhandled message for a specific state
unhandled:
if(verbose && !pmt_eq(event, pmt_intern("%shutdown")))
std::cout << "test_usrp_inband_tx: unhandled msg: " << msg
<< "in state "<< d_state << std::endl;
}
// Sends a command to USRP server to open up a connection to the
// specified USRP, which is defaulted to USRP 0 on the system
void
test_usrp_inband_underrun::opening_usrp()
{
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Opening USRP "
<< d_which_usrp << std::endl;
d_cs->send(s_cmd_open, pmt_list2(PMT_NIL, d_which_usrp));
d_state = OPENING_USRP;
}
// RX and TX channels must be allocated so that the USRP server can
// properly share bandwidth across multiple USRPs. No commands will be
// successful to the USRP through the USRP server on the TX or RX channels until
// a bandwidth allocation has been received.
void
test_usrp_inband_underrun::allocating_channels()
{
d_state = ALLOCATING_CHANNELS;
long capacity = (long) 16e6;
d_tx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
d_rx->send(s_cmd_allocate_channel, pmt_list2(PMT_T, pmt_from_long(capacity)));
}
// After allocating the channels, a write register command will be sent to the
// USRP.
void
test_usrp_inband_underrun::write_register()
{
d_state = WRITE_REGISTER;
long reg = 0;
d_tx->send(s_cmd_to_control_channel, // C/S packet
pmt_list2(PMT_NIL, // invoc handle
pmt_list1(
pmt_list2(s_op_write_reg,
pmt_list2(
pmt_from_long(reg),
pmt_from_long(0xbeef))))));
if(verbose)
std::cout << "[TEST_USRP_INBAND_REGISTERS] Writing 0xbeef to "
<< reg << std::endl;
read_register(); // immediately transition to read the register
}
// Temporary: for testing pings
void
test_usrp_inband_underrun::build_and_send_ping()
{
d_tx->send(s_cmd_to_control_channel,
pmt_list2(PMT_NIL, pmt_list1(pmt_list2(s_op_ping_fixed,
pmt_list2(pmt_from_long(0),
pmt_from_long(0))))));
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Ping sent" << std::endl;
}
// After writing to the register, we want to read the value back and ensure that
// it is the same value that we wrote.
void
test_usrp_inband_underrun::read_register()
{
d_state = READ_REGISTER;
long reg = 9;
d_tx->send(s_cmd_to_control_channel, // C/S packet
pmt_list2(PMT_NIL, // invoc handle
pmt_list1(
pmt_list2(s_op_read_reg,
pmt_list2(
pmt_from_long(0), // rid
pmt_from_long(reg))))));
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Reading from register "
<< reg << std::endl;
}
// Used to enter the receiving state
void
test_usrp_inband_underrun::enter_receiving()
{
d_rx->send(s_cmd_start_recv_raw_samples,
pmt_list2(PMT_F,
d_rx_chan));
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Started RX sample stream\n";
}
void
test_usrp_inband_underrun::enter_transmitting()
{
d_state = TRANSMITTING;
d_nsamples_xmitted = 0;
if(verbose)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Entering transmit state...\n";
build_and_send_next_frame(); // fire off 4 to start pipeline
build_and_send_next_frame();
build_and_send_next_frame();
build_and_send_next_frame();
}
void
test_usrp_inband_underrun::build_and_send_next_frame()
{
long nsamples_this_frame =
std::min(d_nsamples_to_send - d_nsamples_xmitted,
d_samples_per_frame);
if (nsamples_this_frame == 0){
d_done_sending = true;
return;
}
size_t nshorts = 2 * nsamples_this_frame; // 16-bit I & Q
pmt_t uvec = pmt_make_s16vector(nshorts, 0);
size_t ignore;
int16_t *samples = pmt_s16vector_writable_elements(uvec, ignore);
// fill in the complex sinusoid
for (int i = 0; i < nsamples_this_frame; i++){
if (1){
gr_complex s;
d_nco.sincos(&s, 1, d_amplitude);
// write 16-bit i & q
samples[2*i] = (int16_t) s.real();
samples[2*i+1] = (int16_t) s.imag();
}
else {
gr_complex s(d_amplitude, d_amplitude);
// write 16-bit i & q
samples[2*i] = (int16_t) s.real();
samples[2*i+1] = (int16_t) s.imag();
}
}
if(verbose)
std::cout << "[TEST_USRP_INBAND_TX] Transmitting frame...\n";
pmt_t timestamp = pmt_from_long(0xffffffff); // NOW
d_tx->send(s_cmd_xmit_raw_frame,
pmt_list4(pmt_from_long(d_nframes_xmitted), // invocation-handle
d_tx_chan, // channel
uvec, // the samples
timestamp));
d_nsamples_xmitted += nsamples_this_frame;
d_nframes_xmitted++;
if(verbose)
std::cout << "[TEST_USRP_INBAND_TX] Transmitted frame\n";
}
void
test_usrp_inband_underrun::handle_xmit_response(pmt_t handle)
{
if (d_done_sending &&
pmt_to_long(handle) == (d_nframes_xmitted - 1)){
// We're done sending and have received all responses
closing_channels();
return;
}
build_and_send_next_frame();
}
void
test_usrp_inband_underrun::handle_recv_response(pmt_t dict)
{
if(!pmt_is_dict(dict)) {
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Recv samples dictionary is improper\n";
return;
}
// Read the TX interpolations
if(pmt_t underrun = pmt_dict_ref(dict,
pmt_intern("underrun"),
PMT_NIL)) {
if(pmt_eqv(underrun, PMT_T)) {
d_n_underruns++;
if(verbose && 0)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] Underrun\n";
}
else {
if(verbose && 0)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] No underrun\n" << underrun <<std::endl;
}
} else {
if(verbose && 0)
std::cout << "[TEST_USRP_INBAND_UNDERRUN] No underrun\n";
}
}
void
test_usrp_inband_underrun::closing_channels()
{
d_state = CLOSING_CHANNELS;
d_tx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_tx_chan));
d_rx->send(s_cmd_deallocate_channel, pmt_list2(PMT_NIL, d_rx_chan));
}
void
test_usrp_inband_underrun::closing_usrp()
{
d_state = CLOSING_USRP;
d_cs->send(s_cmd_close, pmt_list1(PMT_NIL));
}
REGISTER_MBLOCK_CLASS(test_usrp_inband_underrun);
|