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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
|
//
// Copyright 2010-2013 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include "Responder.hpp"
#include <uhd/property_tree.hpp>
#include <uhd/utils/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/thread/condition_variable.hpp>
#include <cmath>
#include <complex>
#include <csignal>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
const std::string _eth_file("eths_info.txt");
// Redirect output to stderr
struct cerr_redirect
{
cerr_redirect(std::streambuf* new_buffer) : old(std::cerr.rdbuf(new_buffer)) {}
~cerr_redirect()
{
std::cerr.rdbuf(old);
}
private:
std::streambuf* old;
};
// Catch keyboard interrupts for clean manual abort
static bool s_stop_signal_called = false;
static int s_signal = 0;
static void sig_int_handler(int signal)
{
s_stop_signal_called = true;
s_signal = signal;
}
// member of Responder to register sig int handler
void Responder::register_stop_signal_handler()
{
std::signal(SIGINT, &sig_int_handler);
}
// For ncurses. Print everything in stream to screen
void Responder::FLUSH_SCREEN()
{
printw("%s", _ss.str().c_str());
refresh();
_ss.str("");
}
// Like FLUSH_SCREEN but with new line
void Responder::FLUSH_SCREEN_NL()
{
do {
int y, x;
getyx(_window, y, x);
if (x > 0) {
printw("\n");
y++;
}
FLUSH_SCREEN();
} while (0);
}
// Constructor
Responder::Responder(Options& opt)
: _opt(opt)
, _stats_filename(opt.stats_filename)
, _delay(opt.delay)
, _samps_per_packet(opt.samps_per_packet)
, _delay_step(opt.delay_step)
, _simulate_frequency(opt.simulate_frequency)
, _allow_late_bursts(opt.allow_late_bursts)
, _no_delay(opt.no_delay)
,
// Initialize atributes not given by Options
_num_total_samps(0)
, // printed on exit
_overruns(0)
, // printed on exit
_max_success(0)
, // < 0 --> write results to file
_return_code(RETCODE_OK)
, _stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS)
, _timeout_burst_count(0)
, _timeout_eob_count(0)
, _y_delay_pos(-1)
, _x_delay_pos(-1)
, // Remember the cursor position of delay line.
_last_overrun_count(0)
{
time(&_dbginfo.start_time); // for debugging
// Disable logging to console
uhd::log::set_console_level(uhd::log::off);
if (uhd::set_thread_priority_safe(_opt.rt_priority, _opt.realtime)
== false) // try to set realtime scheduling
{
cerr << "Failed to set real-time" << endl;
}
_return_code = calculate_dependent_values();
// From this point on, everything is written to a ncurses window!
create_ncurses_window();
print_create_usrp_msg();
try {
_usrp = create_usrp_device();
} catch (const std::runtime_error& e) {
print_msg(e.what());
_return_code = RETCODE_RUNTIME_ERROR;
} catch (...) {
print_msg("unhandled ERROR");
_return_code = RETCODE_UNKNOWN_EXCEPTION;
print_msg_and_wait("create USRP device failed!\nPress key to abort test...");
return;
}
// Prepare array with response burst data.
_pResponse = alloc_response_buffer_with_data(_response_length);
// ensure that filename is set
string test_id = _usrp->get_mboard_name();
if (set_stats_filename(test_id)) {
_return_code = RETCODE_BAD_ARGS; // make sure run() does return!
FLUSH_SCREEN();
if (_opt.batch_mode == false) {
print_msg_and_wait("Press any key to end...");
}
return;
}
cerr_redirect(_ss_cerr.rdbuf());
register_stop_signal_handler();
}
int Responder::calculate_dependent_values()
{
_response_length = _opt.response_length();
_init_delay_count = (int64_t)(_opt.sample_rate * _opt.init_delay);
_dc_offset_countdown = (int64_t)(_opt.sample_rate * _opt.dc_offset_delay);
_level_calibration_countdown = (int64_t)_opt.level_calibration_count();
_original_simulate_duration = _simulate_duration =
_opt.simulate_duration(_simulate_frequency);
if (_simulate_duration > 0) {
// Skip settling period and calibration
_init_delay_count = 0;
_dc_offset_countdown = 0;
_level_calibration_countdown = 0;
double highest_delay = 0.0;
if (_opt.test_iterations > 0)
highest_delay = max(_opt.delay_max, _opt.delay_min);
else if (_no_delay == false)
highest_delay = _delay;
uint64_t highest_delay_samples = _opt.highest_delay_samples(highest_delay);
if ((highest_delay_samples + _response_length + _opt.flush_count)
> _simulate_duration) {
if (_opt.adjust_simulation_rate) // This is now done DURING the simulation
// based on active delay
{
//_simulate_frequency = max_possible_rate;
//_simulate_duration = (uint64_t)((double)sample_rate /
//_simulate_frequency);
} else {
cerr << boost::format(
"Highest delay and response duration will exceed the pulse "
"simulation rate (%ld + %ld > %ld samples)")
% highest_delay_samples % _response_length
% _simulate_duration
<< endl;
int max_possible_rate = (int)get_max_possible_frequency(
highest_delay_samples, _response_length);
double max_possible_delay =
(double)(_simulate_duration - (_response_length + _opt.flush_count))
/ (double)_opt.sample_rate;
cerr << boost::format("Simulation rate must be less than %i Hz, or "
"maximum delay must be less than %f s")
% max_possible_rate % max_possible_delay
<< endl;
if (_opt.ignore_simulation_check == 0)
return RETCODE_BAD_ARGS;
}
}
} else {
boost::format fmt(
"Simulation frequency too high (%f Hz with sample_rate %f Msps)");
fmt % _simulate_frequency % (_opt.sample_rate / 1e6);
cerr << fmt << endl;
return RETCODE_BAD_ARGS;
}
if (_opt.test_iterations > 0) // Force certain settings during test mode
{
_no_delay = false;
_allow_late_bursts = false;
_delay = _opt.delay_min;
}
return RETCODE_OK; // default return code
}
// print test title to ncurses window
void Responder::print_test_title()
{
if (_opt.test_title.empty() == false) {
std::string title(_opt.test_title);
boost::replace_all(title, "%", "%%");
print_msg(title + "\n");
}
}
void Responder::print_usrp_status()
{
std::string msg;
msg += (boost::format("Using device:\n%s\n") % _usrp->get_pp_string()).str();
msg += (boost::format("Setting RX rate: %f Msps\n") % (_opt.sample_rate / 1e6)).str();
msg += (boost::format("Actual RX rate: %f Msps\n") % (_usrp->get_rx_rate() / 1e6))
.str();
msg += (boost::format("Setting TX rate: %f Msps\n") % (_opt.sample_rate / 1e6)).str();
msg +=
(boost::format("Actual TX rate: %f Msps") % (_usrp->get_tx_rate() / 1e6)).str();
print_msg(msg);
print_tx_stream_status();
print_rx_stream_status();
}
void Responder::print_test_parameters()
{
// Some status output shoud be printed here!
size_t rx_max_num_samps = _rx_stream->get_max_num_samps();
size_t tx_max_num_samps = _tx_stream->get_max_num_samps();
std::string msg;
msg += (boost::format("Samples per buffer: %d\n") % _opt.samps_per_buff).str();
msg += (boost::format("Maximum number of samples: RX = %d, TX = %d\n")
% rx_max_num_samps % tx_max_num_samps)
.str();
msg += (boost::format("Response length: %ld samples (%f us)") % _response_length
% (_opt.response_duration * 1e6))
.str();
if (_simulate_duration > 0)
msg += (boost::format("\nSimulating pulses at %f Hz (every %ld samples)")
% _simulate_frequency % _simulate_duration)
.str();
if (_opt.test_iterations > 0) {
msg += (boost::format("\nTest coverage: %f -> %f (%f steps)") % _opt.delay_min
% _opt.delay_max % _opt.delay_step)
.str();
if (_opt.end_test_after_success_count > 0)
msg += (boost::format("\nTesting will end after %d successful delays")
% _opt.end_test_after_success_count)
.str();
}
if ((_dc_offset_countdown == 0) && (_simulate_frequency == 0.0)) {
msg += "\nDC offset disabled";
}
print_msg(msg);
}
// e.g. B200 doesn't support this command. Check if possible and only set rx_dc_offset if
// available
void Responder::set_usrp_rx_dc_offset(uhd::usrp::multi_usrp::sptr usrp, bool ena)
{
uhd::property_tree::sptr tree = usrp->get_tree();
// FIXME: Path needs to be build in a programmatic way.
bool dc_offset_exists =
tree->exists(uhd::fs_path("/mboards/0/rx_frontends/A/dc_offset"));
if (dc_offset_exists) {
usrp->set_rx_dc_offset(ena);
}
}
void Responder::print_create_usrp_msg()
{
std::string msg("Creating the USRP device");
if (_opt.device_args.empty() == false)
msg.append((boost::format(" with args \"%s\"") % _opt.device_args).str());
msg.append("...");
print_msg(msg);
}
uhd::usrp::multi_usrp::sptr Responder::create_usrp_device()
{
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(_opt.device_args);
usrp->set_rx_rate(_opt.sample_rate); // set the rx sample rate
usrp->set_tx_rate(_opt.sample_rate); // set the tx sample rate
_tx_stream = create_tx_streamer(usrp);
_rx_stream = create_rx_streamer(usrp);
if ((_dc_offset_countdown == 0) && (_simulate_frequency == 0.0))
set_usrp_rx_dc_offset(usrp, false);
return usrp;
}
uhd::rx_streamer::sptr Responder::create_rx_streamer(uhd::usrp::multi_usrp::sptr usrp)
{
uhd::stream_args_t stream_args("fc32"); // complex floats
if (_samps_per_packet > 0) {
stream_args.args["spp"] = str(boost::format("%d") % _samps_per_packet);
}
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
_samps_per_packet = rx_stream->get_max_num_samps();
return rx_stream;
}
void Responder::print_rx_stream_status()
{
std::string msg;
msg += (boost::format("Samples per packet set to: %d\n") % _samps_per_packet).str();
msg += (boost::format("Flushing burst with %d samples") % _opt.flush_count).str();
if (_opt.skip_eob)
msg += "\nSkipping End-Of-Burst";
print_msg(msg);
}
uhd::tx_streamer::sptr Responder::create_tx_streamer(uhd::usrp::multi_usrp::sptr usrp)
{
uhd::stream_args_t tx_stream_args("fc32"); // complex floats
if (_allow_late_bursts == false) {
tx_stream_args.args["underflow_policy"] = "next_burst";
}
uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(tx_stream_args);
return tx_stream;
}
void Responder::print_tx_stream_status()
{
std::string msg;
if (_allow_late_bursts == false) {
msg += "Underflow policy set to drop late bursts";
} else
msg += "Underflow policy set to allow late bursts";
if (_opt.skip_send)
msg += "\nNOT sending bursts";
else if (_opt.combine_eob)
msg += "\nCombining EOB into first send";
print_msg(msg);
}
// handle transmit timeouts properly
void Responder::handle_tx_timeout(int burst, int eob)
{
if (_timeout_burst_count == 0 && _timeout_eob_count == 0)
time(&_dbginfo.first_send_timeout);
_timeout_burst_count += burst;
_timeout_eob_count += eob;
print_timeout_msg();
}
void Responder::print_timeout_msg()
{
move(_y_delay_pos + 3, _x_delay_pos);
print_msg((boost::format("Send timeout, burst_count = %ld\teob_count = %ld\n")
% _timeout_burst_count % _timeout_eob_count)
.str());
}
uhd::tx_metadata_t Responder::get_tx_metadata(uhd::time_spec_t rx_time, size_t n)
{
uhd::tx_metadata_t tx_md;
tx_md.start_of_burst = true;
tx_md.end_of_burst = false;
if ((_opt.skip_eob == false) && (_opt.combine_eob)) {
tx_md.end_of_burst = true;
}
if (_no_delay == false) {
tx_md.has_time_spec = true;
tx_md.time_spec =
rx_time + uhd::time_spec_t(0, n, _opt.sample_rate) + uhd::time_spec_t(_delay);
} else {
tx_md.has_time_spec = false;
}
return tx_md;
}
bool Responder::send_tx_burst(uhd::time_spec_t rx_time, size_t n)
{
if (_opt.skip_send == true) {
return false;
}
// send a single packet
uhd::tx_metadata_t tx_md = get_tx_metadata(rx_time, n);
const size_t length_to_send =
_response_length + (_opt.flush_count - (tx_md.end_of_burst ? 0 : 1));
size_t num_tx_samps =
_tx_stream->send(_pResponse, length_to_send, tx_md, _opt.timeout); // send pulse!
if (num_tx_samps < length_to_send) {
handle_tx_timeout(1, 0);
}
if (_opt.skip_eob == false && _opt.combine_eob == false) {
tx_md.start_of_burst = false;
tx_md.end_of_burst = true;
tx_md.has_time_spec = false;
const size_t eob_length_to_send = 1;
size_t eob_num_tx_samps = _tx_stream->send(
&_pResponse[length_to_send], eob_length_to_send, tx_md); // send EOB
if (eob_num_tx_samps < eob_length_to_send) {
handle_tx_timeout(0, 1);
}
}
return true;
}
// ensure that stats_filename is not empty.
bool Responder::set_stats_filename(string test_id)
{
if (_stats_filename.empty()) {
string file_friendly_test_id(test_id);
boost::replace_all(file_friendly_test_id, " ", "_");
boost::format fmt = boost::format("%slatency-stats.id_%s-rate_%i-spb_%i-spp_%i%s")
% _opt.stats_filename_prefix % file_friendly_test_id
% (int)_opt.sample_rate % _opt.samps_per_buff
% _samps_per_packet % _opt.stats_filename_suffix;
_stats_filename = str(fmt) + ".txt";
_stats_log_filename = str(fmt) + ".log";
}
return check_for_existing_results();
}
// Check if results file can be overwritten
bool Responder::check_for_existing_results()
{
bool ex = false;
if ((_opt.skip_if_results_exist) && (boost::filesystem::exists(_stats_filename))) {
print_msg((boost::format("Skipping invocation as results file already exists: %s")
% _stats_filename)
.str());
ex = true;
}
return ex;
}
// Allocate an array with a burst response
float* Responder::alloc_response_buffer_with_data(
uint64_t response_length) // flush_count, output_value, output_scale are const
{
float* pResponse = new float[(response_length + _opt.flush_count) * 2];
for (unsigned int i = 0; i < (response_length * 2); ++i)
pResponse[i] = _opt.output_value * _opt.output_scale;
for (unsigned int i = (response_length * 2);
i < ((response_length + _opt.flush_count) * 2);
++i)
pResponse[i] = 0.0f;
return pResponse;
}
// print test parameters for current delay time
void Responder::print_formatted_delay_line(const uint64_t simulate_duration,
const uint64_t old_simulate_duration,
const STATS& statsPrev,
const double delay,
const double simulate_frequency)
{
if (_y_delay_pos < 0
|| _x_delay_pos < 0) { // make sure it gets printed to the same position everytime
getyx(_window, _y_delay_pos, _x_delay_pos);
}
double score = 0.0;
if (statsPrev.detected > 0)
score = 100.0 * (double)(statsPrev.detected - statsPrev.missed)
/ (double)statsPrev.detected;
std::string form;
boost::format fmt0("Delay now: %.6f (previous delay %.6f scored %.1f%% [%ld / %ld])");
fmt0 % delay % statsPrev.delay % score % (statsPrev.detected - statsPrev.missed)
% statsPrev.detected;
form += fmt0.str();
if (old_simulate_duration != simulate_duration) {
boost::format fmt1(" [Simulation rate now: %.1f Hz (%ld samples)]");
fmt1 % simulate_frequency % simulate_duration;
form = form + fmt1.str();
}
move(_y_delay_pos, _x_delay_pos);
print_msg(form);
}
// print message and wait for user interaction
void Responder::print_msg_and_wait(std::string msg)
{
msg = "\n" + msg;
print_msg(msg);
timeout(-1);
getch();
timeout(0);
}
// print message to ncurses window
void Responder::print_msg(std::string msg)
{
_ss << msg << endl;
FLUSH_SCREEN();
}
// Check if error occured during call to receive
bool Responder::handle_rx_errors(
uhd::rx_metadata_t::error_code_t err, size_t num_rx_samps)
{
// handle errors
if (err == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) {
std::string msg = (boost::format("Timeout while streaming (received %ld samples)")
% _num_total_samps)
.str();
print_error_msg(msg);
_return_code = RETCODE_RECEIVE_TIMEOUT;
return true;
} else if (err == uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET) {
std::string msg =
(boost::format("Bad packet (received %ld samples)") % _num_total_samps).str();
print_error_msg(msg);
_return_code = RETCODE_BAD_PACKET;
return true;
} else if ((num_rx_samps == 0) && (err == uhd::rx_metadata_t::ERROR_CODE_NONE)) {
print_error_msg("Received no samples");
_return_code = RETCODE_RECEIVE_FAILED;
return true;
} else if (err == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW) {
++_overruns;
print_overrun_msg(); // update overrun info on console.
} else if (err != uhd::rx_metadata_t::ERROR_CODE_NONE) {
throw std::runtime_error(str(boost::format("Unexpected error code 0x%x") % err));
}
return false;
}
// print overrun status message.
void Responder::print_overrun_msg()
{
if (_num_total_samps > (_last_overrun_count + (uint64_t)(_opt.sample_rate * 1.0))) {
int y, x, y_max, x_max;
getyx(_window, y, x);
getmaxyx(_window, y_max, x_max);
move(y_max - 1, 0);
print_msg((boost::format("Overruns: %d") % _overruns).str());
move(y, x);
_last_overrun_count = _num_total_samps;
}
}
// print error message on last line of ncurses window
void Responder::print_error_msg(std::string msg)
{
int y, x, y_max, x_max;
getyx(_window, y, x);
getmaxyx(_window, y_max, x_max);
move(y_max - 2, 0);
clrtoeol();
print_msg(msg);
move(y, x);
}
// calculate simulate frequency
double Responder::get_simulate_frequency(
double delay, uint64_t response_length, uint64_t original_simulate_duration)
{
double simulate_frequency = _simulate_frequency;
uint64_t highest_delay_samples = _opt.highest_delay_samples(delay);
if ((_opt.optimize_simulation_rate)
|| ((highest_delay_samples + response_length + _opt.flush_count)
> original_simulate_duration)) {
simulate_frequency =
get_max_possible_frequency(highest_delay_samples, response_length);
}
return simulate_frequency;
}
// calculate max possible simulate frequency
double Responder::get_max_possible_frequency(uint64_t highest_delay_samples,
uint64_t response_length) // only 2 args, others are all const!
{
return std::floor((double)_opt.sample_rate
/ (double)(highest_delay_samples + response_length
+ _opt.flush_count + _opt.optimize_padding));
}
// Check if conditions to finish test are met.
bool Responder::test_finished(size_t success_count)
{
if (success_count == _opt.end_test_after_success_count) {
print_msg(
(boost::format("\nTest complete after %d successes.") % success_count).str());
return true;
}
if (((_opt.delay_min <= _opt.delay_max) && (_delay >= _opt.delay_max))
|| ((_opt.delay_min > _opt.delay_max) && (_delay <= _opt.delay_max))) {
print_msg("\nTest complete.");
return true;
}
return false;
}
// handle keyboard input in interactive mode
bool Responder::handle_interactive_control()
{
std::string msg = "";
int c = wgetch(_window);
if (c > -1) {
// UP/DOWN Keys control delay step width
if ((c == KEY_DOWN) || (c == KEY_UP)) {
double dMag = log10(_delay_step);
int iMag = (int)floor(dMag);
iMag += ((c == KEY_UP) ? 1 : -1);
_delay_step = pow(10.0, iMag);
msg += (boost::format("Step: %f") % _delay_step).str();
}
// LEFT/RIGHT Keys control absolute delay length
if ((c == KEY_LEFT) || (c == KEY_RIGHT)) {
double step = _delay_step * ((c == KEY_RIGHT) ? 1 : -1);
if ((_delay + step) >= 0.0)
_delay += step;
msg += (boost::format("Delay: %f") % _delay).str();
}
// Enable/disable fixed delay <--> best effort mode
if (c == 'd') {
_no_delay = !_no_delay;
if (_no_delay)
msg += "Delay disabled (best effort)";
else
msg += (boost::format("Delay: %f") % _delay).str();
} else if (c == 'q') // exit test
{
return true; // signal test to stop
} else if (c == 'l') // change late burst policy
{
_allow_late_bursts = !_allow_late_bursts;
if (_allow_late_bursts)
msg += "Allowing late bursts";
else
msg += "Dropping late bursts";
}
print_interactive_msg(msg);
}
return false; // signal test to continue with updated values
}
// print updated interactive control value
void Responder::print_interactive_msg(std::string msg)
{
if (!msg.empty()) {
// move cursor back to beginning of line
int y, x;
getyx(_window, y, x);
if (x > 0) {
move(y, 0);
clrtoeol();
}
print_msg(msg);
move(y, 0);
}
}
// check if transmit burst is late
bool Responder::tx_burst_is_late()
{
uhd::async_metadata_t async_md;
if (_usrp->get_device()->recv_async_msg(async_md, 0)) {
if (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_TIME_ERROR) {
return true;
}
}
return false;
}
void Responder::create_ncurses_window()
{
_window = initscr();
cbreak(); // Unbuffered key input, except for signals (cf. 'raw')
noecho();
nonl();
intrflush(_window, FALSE);
keypad(_window, TRUE); // Enable function keys, arrow keys, ...
nodelay(_window, 0);
timeout(0);
}
// print all fixed test parameters
void Responder::print_init_test_status()
{
// Clear the window and write new data.
erase();
refresh();
print_test_title();
print_usrp_status();
print_test_parameters();
std::string msg("");
if (_opt.test_iterations > 0)
msg.append("Press Ctrl + C to abort test");
else
msg.append("Press Q stop streaming");
msg.append("\n");
print_msg(msg);
_y_delay_pos = -1; // reset delay display line pos.
_x_delay_pos = -1;
}
// in interactive mode with second usrp sending bursts. calibrate trigger level
float Responder::calibrate_usrp_for_test_run()
{
bool calibration_finished = false;
float threshold = 0.0f;
double ave_high = 0, ave_low = 0;
int ave_high_count = 0, ave_low_count = 0;
bool level_calibration_stage_2 =
false; // 1. stage = rough calibration ; 2. stage = fine calibration
std::vector<std::complex<float>> buff(_opt.samps_per_buff);
while (
not s_stop_signal_called && !calibration_finished && _return_code == RETCODE_OK) {
uhd::rx_metadata_t rx_md;
size_t num_rx_samps =
_rx_stream->recv(&buff.front(), buff.size(), rx_md, _opt.timeout);
// handle errors
if (handle_rx_errors(rx_md.error_code, num_rx_samps)) {
break;
}
// Wait for USRP for DC offset calibration
if (_dc_offset_countdown > 0) {
_dc_offset_countdown -= (int64_t)num_rx_samps;
if (_dc_offset_countdown > 0)
continue;
set_usrp_rx_dc_offset(_usrp, false);
print_msg("DC offset calibration complete");
}
// Wait for certain time to minimize POWER UP effects
if (_init_delay_count > 0) {
_init_delay_count -= (int64_t)num_rx_samps;
if (_init_delay_count > 0)
continue;
print_msg("Initial settling period elapsed");
}
////////////////////////////////////////////////////////////
// detect falling edges and calibrate detection values
if (_level_calibration_countdown > 0) {
if (level_calibration_stage_2 == false) {
float average = 0.0f;
for (size_t n = 0; n < num_rx_samps; n++)
average += buff[n].real() * _opt.invert;
average /= (float)num_rx_samps;
if (ave_low_count == 0) {
ave_low = average;
++ave_low_count;
} else if (average < ave_low) {
ave_low = average;
++ave_low_count;
}
if (ave_high_count == 0) {
ave_high = average;
++ave_high_count;
} else if (average > ave_high) {
ave_high = average;
++ave_high_count;
}
} else {
for (size_t n = 0; n < num_rx_samps; n++) {
float f = buff[n].real() * _opt.invert;
if (f >= threshold) {
ave_high += f;
ave_high_count++;
} else {
ave_low += f;
ave_low_count++;
}
}
}
_level_calibration_countdown -= (int64_t)num_rx_samps;
if (_level_calibration_countdown <= 0) {
if (level_calibration_stage_2 == false) {
level_calibration_stage_2 = true;
_level_calibration_countdown = _opt.level_calibration_count();
threshold = ave_low + ((ave_high - ave_low) / 2.0);
print_msg((boost::format("Phase #1: Ave low: %.3f (#%d), ave high: "
"%.3f (#%d), threshold: %.3f")
% ave_low % ave_low_count % ave_high % ave_high_count
% threshold)
.str());
ave_low_count = ave_high_count = 0;
ave_low = ave_high = 0.0f;
continue;
} else {
ave_low /= (double)ave_low_count;
ave_high /= (double)ave_high_count;
threshold = ave_low + ((ave_high - ave_low) * _opt.trigger_level);
print_msg((boost::format("Phase #2: Ave low: %.3f (#%d), ave high: "
"%.3f (#%d), threshold: %.3f\n")
% ave_low % ave_low_count % ave_high % ave_high_count
% threshold)
.str());
_stream_cmd.stream_mode =
uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
_stream_cmd.stream_now = true;
_usrp->issue_stream_cmd(_stream_cmd);
double diff = std::abs(ave_high - ave_low);
if (diff < _opt.pulse_detection_threshold) {
_return_code = RETCODE_BAD_ARGS;
print_error_msg(
(boost::format("Did not detect any pulses (difference %.6f < "
"detection threshold %.6f)")
% diff % _opt.pulse_detection_threshold)
.str());
break;
}
_stream_cmd.stream_mode =
uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;
_stream_cmd.stream_now = true;
_usrp->issue_stream_cmd(_stream_cmd);
}
} else
continue;
} // calibration finished
calibration_finished = true;
}
return threshold;
}
// try to stop USRP properly after tests
void Responder::stop_usrp_stream()
{
try {
if (_usrp) {
_stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;
_stream_cmd.stream_now = true;
_usrp->issue_stream_cmd(_stream_cmd);
}
} catch (...) {
//
}
}
// after each delay length update test parameters and print them
void Responder::update_and_print_parameters(const STATS& statsPrev, const double delay)
{
uint64_t old_simulate_duration = _simulate_duration;
_simulate_frequency =
get_simulate_frequency(delay, _response_length, _original_simulate_duration);
_simulate_duration = _opt.simulate_duration(_simulate_frequency);
print_formatted_delay_line(
_simulate_duration, old_simulate_duration, statsPrev, delay, _simulate_frequency);
_timeout_burst_count = 0;
_timeout_eob_count = 0;
}
// detect or simulate burst level.
bool Responder::get_new_state(
uint64_t total_samps, uint64_t simulate_duration, float val, float threshold)
{
bool new_state = false;
if (simulate_duration > 0) // only simulated input bursts!
new_state = (((total_samps) % simulate_duration) == 0);
else
new_state = (val >= threshold); // TODO: Just measure difference in fall
return new_state;
}
// detect a pulse, respond to it and count number of pulses.
// statsCurrent holds parameters.
uint64_t Responder::detect_respond_pulse_count(STATS& statsCurrent,
std::vector<std::complex<float>>& buff,
uint64_t trigger_count,
size_t num_rx_samps,
float threshold,
uhd::time_spec_t rx_time)
{
// buff, threshold
bool input_state = false;
for (size_t n = 0; n < num_rx_samps; n++) {
float f = buff[n].real() * _opt.invert;
bool new_state =
get_new_state(_num_total_samps + n, _simulate_duration, f, threshold);
if ((new_state == false) && (input_state)) // == falling_edge
{
trigger_count++;
statsCurrent.detected++;
if ((_opt.test_iterations > 0) && (_opt.skip_iterations > 0)
&& (statsCurrent.skipped == 0)
&& (_opt.skip_iterations == statsCurrent.detected)) {
memset(&statsCurrent, 0x00, sizeof(STATS));
statsCurrent.delay = _delay;
statsCurrent.detected = 1;
statsCurrent.skipped = _opt.skip_iterations;
trigger_count = 1;
}
if (!send_tx_burst(rx_time, n)) {
statsCurrent.missed++;
}
if (tx_burst_is_late()) {
statsCurrent.missed++;
}
}
input_state = new_state;
}
return trigger_count;
}
// this is the actual "work" function. All the fun happens here
void Responder::run_test(float threshold)
{
STATS statsCurrent; //, statsPrev;
memset(&statsCurrent, 0x00, sizeof(STATS));
if (_opt.test_iterations > 0) {
update_and_print_parameters(statsCurrent, _delay);
statsCurrent.delay = _opt.delay_min;
}
///////////////////////////////////////////////////////////////////////////
uint64_t trigger_count = 0;
size_t success_count = 0;
uint64_t num_total_samps_test = 0;
std::vector<std::complex<float>> buff(_opt.samps_per_buff);
while (not s_stop_signal_called && _return_code == RETCODE_OK) {
// get samples from rx stream.
uhd::rx_metadata_t rx_md;
size_t num_rx_samps =
_rx_stream->recv(&buff.front(), buff.size(), rx_md, _opt.timeout);
// handle errors
if (handle_rx_errors(rx_md.error_code, num_rx_samps)) {
break;
}
// detect falling edges, send respond pulse and check if response could be sent in
// time
trigger_count = detect_respond_pulse_count(
statsCurrent, buff, trigger_count, num_rx_samps, threshold, rx_md.time_spec);
// increase counters for single test and overall test samples count.
_num_total_samps += num_rx_samps;
num_total_samps_test += num_rx_samps;
// control section for interactive mode
if (_opt.test_iterations == 0) // == "interactive'
{
if (handle_interactive_control())
break;
}
// control section for test mode
if (_opt.test_iterations > 0) // == test mode / batch-mode
{
int step_return = test_step_finished(
trigger_count, num_total_samps_test, statsCurrent, success_count);
if (step_return == -2) // == test is finished with all desired delay steps
break;
else if (step_return == -1) // just continue test
continue;
else // test with one delay is finished
{
success_count = (size_t)step_return;
trigger_count = 0;
num_total_samps_test = 0;
memset(&statsCurrent,
0x00,
sizeof(STATS)); // reset current stats for next test iteration
statsCurrent.delay = _delay;
}
} // end test mode control section
} // exit outer loop after stop signal is called, test is finished or other break
// condition is met
if (s_stop_signal_called)
_return_code = RETCODE_MANUAL_ABORT;
}
// check if test with one specific delay is finished
int Responder::test_step_finished(uint64_t trigger_count,
uint64_t num_total_samps_test,
STATS statsCurrent,
size_t success_count)
{
if (((_opt.test_iterations_is_sample_count == false)
&& (trigger_count >= _opt.test_iterations))
|| ((_opt.test_iterations_is_sample_count)
&& (num_total_samps_test > _opt.test_iterations))) {
add_stats_to_results(statsCurrent, _delay);
if (statsCurrent.missed == 0) // == NO late bursts
++success_count;
else
success_count = 0;
if (test_finished(success_count))
return -2; // test is completely finished
_delay += _delay_step; // increase delay by one step
update_and_print_parameters(statsCurrent, _delay);
return success_count; // test is finished for one delay step
}
return -1; // == continue test
}
// save test results
void Responder::add_stats_to_results(STATS statsCurrent, double delay)
{
_max_success = max(_max_success,
(statsCurrent.detected - statsCurrent.missed)); // > 0 --> save results
uint64_t key = (uint64_t)(delay * 1e6);
_mapStats[key] = statsCurrent;
}
// run tests and handle errors
int Responder::run()
{
if (_return_code != RETCODE_OK)
return _return_code;
if (_opt.pause)
print_msg_and_wait("Press any key to begin...");
time(&_dbginfo.start_time_test);
// Put some info about the test on the console
print_init_test_status();
try {
// setup streaming
_stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS;
_stream_cmd.stream_now = true;
_usrp->issue_stream_cmd(_stream_cmd);
if (!_opt.batch_mode) {
float threshold = calibrate_usrp_for_test_run();
if (_return_code != RETCODE_OK) {
return _return_code;
}
run_test(threshold);
} else {
run_test();
}
} catch (const std::runtime_error& e) {
print_msg(e.what());
_return_code = RETCODE_RUNTIME_ERROR;
} catch (...) {
print_msg("Unhandled exception");
_return_code = RETCODE_UNKNOWN_EXCEPTION;
}
stop_usrp_stream();
time(&_dbginfo.end_time_test);
return (_return_code < 0 ? _return_code : _overruns);
}
/*
* Following functions are intended to be used by destructor only!
*/
// This method should print statistics after ncurses endwin.
void Responder::print_final_statistics()
{
cout << boost::format("Received %ld samples during test run") % _num_total_samps;
if (_overruns > 0)
cout << boost::format(" (%d overruns)") % _overruns;
cout << endl;
}
// safe test results to a log file if enabled
void Responder::write_log_file()
{
try {
if (_opt.log_file) {
std::map<std::string, std::string> hw_info = get_hw_info();
ofstream logs(_stats_log_filename.c_str());
logs << boost::format("title=%s") % _opt.test_title << endl;
logs << boost::format("device=%s") % _usrp->get_mboard_name() << endl;
logs << boost::format("device_args=%s") % _opt.device_args << endl;
logs << boost::format("type=%s") % hw_info["type"] << endl;
if (!hw_info.empty()) {
logs << boost::format("usrp_addr=%s") % hw_info["usrp_addr"] << endl;
logs << boost::format("usrp_name=%s") % hw_info["name"] << endl;
logs << boost::format("serial=%s") % hw_info["serial"] << endl;
logs << boost::format("host_interface=%s") % hw_info["interface"] << endl;
logs << boost::format("host_addr=%s") % hw_info["host_addr"] << endl;
logs << boost::format("host_mac=%s") % hw_info["mac"] << endl;
logs << boost::format("host_vendor=%s (id=%s)") % hw_info["vendor"]
% hw_info["vendor_id"]
<< endl;
logs << boost::format("host_device=%s (id=%s)") % hw_info["device"]
% hw_info["device_id"]
<< endl;
}
logs << boost::format("sample_rate=%f") % _opt.sample_rate << endl;
logs << boost::format("samps_per_buff=%i") % _opt.samps_per_buff << endl;
logs << boost::format("samps_per_packet=%i") % _samps_per_packet << endl;
logs << boost::format("delay_min=%f") % _opt.delay_min << endl;
logs << boost::format("delay_max=%f") % _opt.delay_max << endl;
logs << boost::format("delay_step=%f") % _delay_step << endl;
logs << boost::format("delay=%f") % _delay << endl;
logs << boost::format("init_delay=%f") % _opt.init_delay << endl;
logs << boost::format("response_duration=%f") % _opt.response_duration
<< endl;
logs << boost::format("response_length=%ld") % _response_length << endl;
logs << boost::format("timeout=%f") % _opt.timeout << endl;
logs << boost::format("timeout_burst_count=%ld") % _timeout_burst_count
<< endl;
logs << boost::format("timeout_eob_count=%f") % _timeout_eob_count << endl;
logs << boost::format("allow_late_bursts=%s")
% (_allow_late_bursts ? "yes" : "no")
<< endl;
logs << boost::format("skip_eob=%s") % (_opt.skip_eob ? "yes" : "no") << endl;
logs << boost::format("combine_eob=%s") % (_opt.combine_eob ? "yes" : "no")
<< endl;
logs << boost::format("skip_send=%s") % (_opt.skip_send ? "yes" : "no")
<< endl;
logs << boost::format("no_delay=%s") % (_no_delay ? "yes" : "no") << endl;
logs << boost::format("simulate_frequency=%f") % _simulate_frequency << endl;
logs << boost::format("simulate_duration=%ld") % _simulate_duration << endl;
logs << boost::format("original_simulate_duration=%ld")
% _original_simulate_duration
<< endl;
logs << boost::format("realtime=%s") % (_opt.realtime ? "yes" : "no") << endl;
logs << boost::format("rt_priority=%f") % _opt.rt_priority << endl;
logs << boost::format("test_iterations=%ld") % _opt.test_iterations << endl;
logs << boost::format("end_test_after_success_count=%i")
% _opt.end_test_after_success_count
<< endl;
logs << boost::format("skip_iterations=%i") % _opt.skip_iterations << endl;
logs << boost::format("overruns=%i") % _overruns << endl;
logs << boost::format("num_total_samps=%ld") % _num_total_samps << endl;
logs << boost::format("return_code=%i\t(%s)") % _return_code
% enum2str(_return_code)
<< endl;
logs << endl;
write_debug_info(logs);
}
} catch (...) {
cerr << "Failed to write log file to: " << _stats_log_filename << endl;
}
}
// write debug info to log file
void Responder::write_debug_info(ofstream& logs)
{
logs << endl << "%% DEBUG INFO %%" << endl;
logs << boost::format("dbg_time_start=%s") % get_gmtime_string(_dbginfo.start_time)
<< endl;
logs << boost::format("dbg_time_end=%s") % get_gmtime_string(_dbginfo.end_time)
<< endl;
logs << boost::format("dbg_time_duration=%d")
% difftime(_dbginfo.end_time, _dbginfo.start_time)
<< endl;
logs << boost::format("dbg_time_start_test=%s")
% get_gmtime_string(_dbginfo.start_time_test)
<< endl;
logs << boost::format("dbg_time_end_test=%s")
% get_gmtime_string(_dbginfo.end_time_test)
<< endl;
logs << boost::format("dbg_time_duration_test=%d")
% difftime(_dbginfo.end_time_test, _dbginfo.start_time_test)
<< endl;
logs << boost::format("dbg_time_first_send_timeout=%s")
% get_gmtime_string(_dbginfo.first_send_timeout)
<< endl;
}
// convert a time string to desired format
std::string Responder::get_gmtime_string(time_t time)
{
tm* ftm;
ftm = gmtime(&time);
std::string strtime;
strtime.append((boost::format("%i") % (ftm->tm_year + 1900)).str());
strtime.append((boost::format("-%02i") % ftm->tm_mon).str());
strtime.append((boost::format("-%02i") % ftm->tm_mday).str());
strtime.append((boost::format("-%02i") % ((ftm->tm_hour))).str());
strtime.append((boost::format(":%02i") % ftm->tm_min).str());
strtime.append((boost::format(":%02i") % ftm->tm_sec).str());
return strtime;
}
// read hardware info from file if available to include it in log file
std::map<std::string, std::string> Responder::get_hw_info()
{
std::map<std::string, std::string> result;
std::vector<std::map<std::string, std::string>> eths = read_eth_info();
if (eths.empty()) {
return result;
}
uhd::device_addr_t usrp_info = get_usrp_info();
std::string uaddr = get_ip_subnet_addr(usrp_info["addr"]);
for (unsigned int i = 0; i < eths.size(); i++) {
if (get_ip_subnet_addr(eths[i]["addr"]) == uaddr) {
result["type"] = usrp_info["type"];
result["usrp_addr"] = usrp_info["addr"];
result["name"] = usrp_info["name"];
result["serial"] = usrp_info["serial"];
result["interface"] = eths[i]["interface"];
result["host_addr"] = eths[i]["addr"];
result["mac"] = eths[i]["mac"];
result["vendor"] = eths[i]["vendor"];
result["vendor_id"] = eths[i]["vendor_id"];
result["device"] = eths[i]["device"];
result["device_id"] = eths[i]["device_id"];
break; // Use first item found. Imitate device discovery.
}
}
return result;
}
// subnet used to identify used network interface
std::string Responder::get_ip_subnet_addr(std::string ip)
{
return ip.substr(0, ip.rfind(".") + 1);
}
// get network interface info from file (should include all available interfaces)
std::vector<std::map<std::string, std::string>> Responder::read_eth_info()
{
const std::string eth_file(_eth_file);
std::vector<std::map<std::string, std::string>> eths;
try {
ifstream eth_info(eth_file.c_str());
if (!eth_info.is_open()) {
return eths;
}
const int len = 256;
char cline[len];
for (; !eth_info.eof();) {
eth_info.getline(cline, len);
std::string line(cline);
if (line.find("## ETH Interface") != std::string::npos) {
eth_info.getline(cline, len);
std::string eth(cline);
// cout << "interface=" << eth << endl;
std::map<std::string, std::string> iface;
iface["interface"] = eth;
eths.push_back(iface);
}
const std::string ipstr("\tip ");
if (line.find(ipstr) != std::string::npos) {
std::string ip(
line.replace(line.begin(), line.begin() + ipstr.length(), ""));
// cout << "ip=" << ip << endl;
eths.back()["addr"] = ip;
}
const std::string macstr("\tmac ");
if (line.find(macstr) != std::string::npos) {
std::string mac(
line.replace(line.begin(), line.begin() + macstr.length(), ""));
// cout << "mac=" << mac << endl;
eths.back()["mac"] = mac;
}
const std::string vstr("\t\tvendor ");
if (line.find(vstr) != std::string::npos) {
std::string vendor(
line.replace(line.begin(), line.begin() + vstr.length(), ""));
std::string vid(vendor.substr(0, 6));
vendor.replace(0, 7, "");
// cout << "id=" << vid << endl;
// cout << "vendor=" << vendor << endl;
eths.back()["vendor"] = vendor;
eths.back()["vendor_id"] = vid;
}
const std::string dstr("\t\tdevice ");
if (line.find(dstr) != std::string::npos) {
std::string device(
line.replace(line.begin(), line.begin() + dstr.length(), ""));
std::string did(device.substr(0, 6));
device.replace(0, 7, "");
// cout << "id=" << did << endl;
// cout << "device=" << device << endl;
eths.back()["device"] = device;
eths.back()["device_id"] = did;
}
}
} catch (...) {
// nothing in yet
}
return eths;
}
// get info on used USRP
uhd::device_addr_t Responder::get_usrp_info()
{
uhd::device_addrs_t device_addrs = uhd::device::find(_opt.device_args);
uhd::device_addr_t device_addr = device_addrs[0];
return device_addr;
}
// write statistics of test run to file
void Responder::write_statistics_to_file(StatsMap mapStats)
{
try {
ofstream results(_stats_filename.c_str());
for (StatsMap::iterator it = mapStats.begin(); it != mapStats.end(); ++it) {
STATS& stats = it->second;
double d = 0.0;
if (stats.detected > 0)
d = 1.0 - ((double)stats.missed / (double)stats.detected);
cout << "\t" << setprecision(6) << stats.delay << "\t\t" << setprecision(6)
<< d << endl;
results << (stats.delay * _opt.time_mul) << " " << setprecision(6) << d
<< endl;
}
cout << "Statistics written to: " << _stats_filename << endl;
} catch (...) {
cout << "Failed to write statistics to: " << _stats_filename << endl;
}
}
// make sure write files is intended
void Responder::safe_write_statistics_to_file(
StatsMap mapStats, uint64_t max_success, int return_code)
{
if ((_opt.test_iterations > 0) && (_stats_filename.empty() == false)
&& (_opt.no_stats_file == false)) {
if (mapStats.empty()) {
cout << "No results to output (not writing statistics file)" << endl;
} else if ((max_success == 0) && (return_code == RETCODE_MANUAL_ABORT)) {
cout << "Aborted before a single successful timed burst (not writing "
"statistics file)"
<< endl;
} else {
write_statistics_to_file(mapStats);
}
write_log_file();
}
}
// destructor, handle proper test shutdown
Responder::~Responder()
{
endwin();
if (_pResponse != NULL) {
delete[] _pResponse;
}
time(&_dbginfo.end_time);
// Print final info about test run
print_final_statistics();
// check conditions and write statistics to file
safe_write_statistics_to_file(_mapStats, _max_success, _return_code);
cout << "program exited with code = " << enum2str(_return_code) << endl;
}
// make test output more helpful
std::string Responder::enum2str(int return_code)
{
switch (return_code) {
case RETCODE_OK:
return "OK";
case RETCODE_BAD_ARGS:
return "BAD_ARGS";
case RETCODE_RUNTIME_ERROR:
return "RUNTIME_ERROR";
case RETCODE_UNKNOWN_EXCEPTION:
return "UNKNOWN_EXCEPTION";
case RETCODE_RECEIVE_TIMEOUT:
return "RECEIVE_TIMEOUT";
case RETCODE_RECEIVE_FAILED:
return "RECEIVE_FAILED";
case RETCODE_MANUAL_ABORT:
return "MANUAL_ABORT";
case RETCODE_BAD_PACKET:
return "BAD_PACKET";
case RETCODE_OVERFLOW:
return "OVERFLOW";
}
return "UNKNOWN";
}
|