-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_randomtest.cxx
776 lines (661 loc) · 24.4 KB
/
simple_randomtest.cxx
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
// papoon_usb: "Not Insane" USB library for STM32F103xx MCUs
// Copyright (C) 2019,2020 Mark R. Rubin
//
// This file is part of papoon_usb.
//
// The papoon_usb program 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 of
// the License, or (at your option) any later version.
//
// The papoon_usb program 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
// (LICENSE.txt) along with the papoon_usb program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#include <signal.h>
#include <string.h>
#include <iomanip>
#include <iostream>
#include <libusb.h>
#include <random_test.hxx>
#if !defined(UP_MAX_SIZE) || !defined(DOWN_MAX_SIZE)
#error must define UP_MAX_SIZE and DOWN_MAX_SIZE
#endif
#ifndef SEND_B4_RECV_MAX
#error must define SEND_B4_RECV_MAX
#endif
#if UP_MAX_SIZE != 2 \
&& UP_MAX_SIZE != 4 \
&& UP_MAX_SIZE != 8 \
&& UP_MAX_SIZE != 16 \
&& UP_MAX_SIZE != 32 \
&& UP_MAX_SIZE != 64 \
&& UP_MAX_SIZE != 128 \
&& UP_MAX_SIZE != 256 \
&& UP_MAX_SIZE != 512 \
&& UP_MAX_SIZE != 1024
#error UP_MAX_SIZE must be even power of 2
#endif
#if DOWN_MAX_SIZE % 4 != 0
#error DOWN_MAX_SIZE must be evenly divisible by 4
#endif
#if !defined(UP_SEED) || !defined(DOWN_SEED)
#error must define UP_SEED and DOWN_SEED
#endif
#if !defined(MASTER_LENGTH_SEED) || !defined(SLAVE_LENGTH_SEED)
#error must define MASTER_LENGTH_SEED and SLAVE_LENGTH_SEED
#endif
#ifndef SYNC_LENGTH
#error must define SYNC_LENGTH
#endif
#ifndef HISTOGRAM_LENGTH
#error must define HISTOGRAM_LENGTH
#endif
#ifndef REPORT_EVERY
#error must define REPORT_EVERY
#endif
namespace {
enum class TransferType {
INTERRUPT = 0,
BULK
};
static const uint16_t VENDOR = 0x0483,
PRODUCT = 0x62e3,
MAX_IN_PACKET = 64 ,
MAX_OUT_PACKET = 64 ;
static const int OUT_TIMEOUT = 10000, // milliseconds (10 seconds)
IN_TIMEOUT = 0; // no timeout
static const uint8_t IN_ENDPOINT = 0x81,
OUT_ENDPOINT = 0x02;
libusb_device_handle *device_handle = 0;
libusb_transfer *transfer_in = 0,
*transfer_out = 0;
unittest::RandomTest<HISTOGRAM_LENGTH> random_test(UP_SEED ,
DOWN_SEED ,
MASTER_LENGTH_SEED,
DOWN_MAX_SIZE ,
SEND_B4_RECV_MAX );
struct timeval prev ;
uint8_t down_buf[DOWN_MAX_SIZE] ,
up_buf[ UP_MAX_SIZE] ;
struct timeval begin = {0} ;
unsigned handle_events = 0 ,
sends_pending = 0 ,
up_errors = 0 ,
down_errors = 0 ;
TransferType transfer_type = TransferType::INTERRUPT;
bool down_pending = false ;
uint8_t in_endpoint = IN_ENDPOINT ,
out_endpoint = OUT_ENDPOINT ;
unsigned ups = 0, // DEBUG
sends = 0, // DEBUG
downs = 0; // DEBUG
unsigned out_timeouts = 0,
last_out_timeout = 0; // DEBUG
void report(
int signum)
{
std::cout << "\nreport (signal "
<< signum
<< "):"
<< std::endl;
struct timeval crnt, diff;
if (gettimeofday(&crnt, 0) == -1) {
std::cerr << "gettimeofday() crnt failure:"
<< strerror(errno)
<< " ("
<< errno
<< ')'
<< std::endl;
report(-1);
}
timersub(&crnt, &begin, &diff);
double elapsed
= static_cast<double>(diff.tv_sec )
+ static_cast<double>(diff.tv_usec) / 1000000.0,
bytes_per_packet
= random_test.send_count()
? static_cast<double>(random_test.send_bytes())
/ static_cast<double>(random_test.send_count())
: 0.0 ;
std::cout << "evnt: "
<< handle_events
<< " sends_pending: "
<< sends_pending
<< "\nerrs: "
<< up_errors
<< " up "
<< down_errors
<< " down"
<< std::endl;
std::cout << "outs: "
<< out_timeouts
<< " timouts, last @ "
<< last_out_timeout
<< std::endl;
std::cout << "dbug: "// DEBUG
<< sends
<< " sends "
<< downs
<< " downs "
<< ups
<< " ups"
<< std::endl;
std::cout << "send: "
<< random_test.send_bytes()
<< " bytes "
<< random_test.send_count()
<< " xfers "
<< elapsed
<< " seconds\navgs: "
<< std::fixed
<< std::setprecision(1)
<< static_cast<double>(random_test.send_count()) / elapsed
<< " xfers/sec "
<< static_cast<double>(random_test.send_bytes()) / elapsed
<< " bytes/sec "
<< std::fixed
<< std::setprecision(2)
<< bytes_per_packet
<< " bytes/xfer\nrecvs_btwn histogram:";
for (unsigned ndx = 0 ; ndx < random_test.histogram_length() ; ++ndx)
std::cout << ' '
<< random_test.recvs_btwn_sends(ndx) ;
std::cout << std::endl;
bytes_per_packet
= random_test.recv_count()
? static_cast<double>(random_test.recv_bytes())
/ static_cast<double>(random_test.recv_count())
: 0.0 ;
std::cout << "recv: "
<< random_test.recv_bytes()
<< " bytes "
<< random_test.recv_count()
<< " xfers "
<< elapsed
<< " seconds\navgs: "
<< std::fixed
<< std::setprecision(1)
<< static_cast<double>(random_test.recv_count()) / elapsed
<< " xfers/sec "
<< static_cast<double>(random_test.recv_bytes()) / elapsed
<< " bytes/sec "
<< std::fixed
<< std::setprecision(2)
<< bytes_per_packet
<< " bytes/xfer\nsends_btwn histogram:";
for (unsigned ndx = 0 ; ndx < random_test.histogram_length() ; ++ndx)
std::cout << ' '
<< random_test.sends_btwn_recvs(ndx) ;
std::cout << std::endl;
if (signum != 0) {
libusb_release_interface(device_handle, 0);
#if 0 // this hangs if exiting because random check failure
libusb_close(device_handle);
#endif
exit(signum);
}
} // report(
void report(
int signum ,
unsigned linenum)
{
std::cerr << "Abort, line "
<< linenum
<< " code: "
<< signum;
report(signum);
}
#ifdef DEBUG
uint16_t checksum(
const uint8_t* const buffer,
const unsigned length)
{
uint16_t sum = 0;
for (unsigned ndx = 0 ; ndx < length ; ++ndx)
sum += buffer[ndx];
return sum;
}
std::string buffer_ascii(
const uint8_t* const buffer,
const unsigned length)
{
std::stringstream stream;
for (unsigned ndx = 0 ; ndx < length ; ++ndx)
stream << ( isprint(buffer[ndx])
? static_cast<char>(buffer[ndx])
: '.');
return stream.str();
}
#endif
void up_callback(
struct libusb_transfer *transfer)
{
++ups; // DEBUG
if ( transfer->status != LIBUSB_TRANSFER_COMPLETED
|| transfer->actual_length <= 0) {
std::cerr << "up_callback() transfer error: status: 0x"
<< std::hex
<< transfer->status
<< std::dec
<< " length: "
<< transfer->length
<< " actual_length="
<< transfer->actual_length
<< std::endl;
++up_errors;
report(-1, __LINE__);
}
#ifdef DEBUG
std::cerr << "read "
<< std::setw(2)
<< transfer->actual_length
<< " bytes cksum: 0x"
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< checksum(up_buf, transfer->length)
<< std::setfill(' ')
<< std::dec
<< " "
<< buffer_ascii(up_buf, transfer->length)
<< std::endl;
#endif
if (random_test.synced()) {
if (!random_test.recv(up_buf, transfer->actual_length)) {
std::cerr << "Data error in up packet actual_length: "
<< transfer->actual_length
<< std::endl;
report(-1, __LINE__);
}
}
else
random_test.recv_sync(up_buf, transfer->actual_length);
#ifdef RANDOMTEST_LIBUSB_ASYNC
RESUBMIT:
#if 0 // should stay as previously set, don't need to redo
if (transfer_type == TransferType::INTERRUPT)
libusb_fill_interrupt_transfer(transfer_in ,
device_handle,
in_endpoint ,
up_buf ,
MAX_IN_PACKET,
up_callback ,
0 ,
IN_TIMOUT );
else
libusb_fill_bulk_transfer (transfer_in ,
device_handle,
in_endpoint ,
up_buf ,
MAX_IN_PACKET,
up_callback ,
0 ,
IN_TIMOUT );
#endif
int error;
if ( (error = libusb_submit_transfer(transfer))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_submit_transfer(<in_transfer, callback()>) "
"failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(-1, __LINE__) ;
}
#endif
}
#ifdef RANDOMTEST_LIBUSB_ASYNC
void down_callback(
struct libusb_transfer *transfer)
{
++downs; // DEBUG
if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
++out_timeouts;
last_out_timeout = random_test.send_count();
// need to resubmit?
return;
}
if ( transfer->status != LIBUSB_TRANSFER_COMPLETED
|| transfer->actual_length != transfer->length ) {
std::cerr << "down_callback() transfer error: status: 0x"
<< std::hex
<< transfer->status
<< std::dec
<< " length: "
<< transfer->length
<< " actual_length="
<< transfer->actual_length
<< std::endl;
++down_errors;
int error;
if ( (error = libusb_submit_transfer(transfer))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_submit_transfer(<in_transfer, callback()>) "
"failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(-1, __LINE__) ;
}
}
down_pending = false;
#ifdef DEBUG
std::cerr << "wrtn "
<< std::setw(2)
<< transfer->actual_length
<< " bytes cksum: 0x"
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< checksum(down_buf, transfer->actual_length)
<< std::setfill(' ')
<< std::dec
<< " "
<< buffer_ascii(down_buf, transfer->actual_length)
<< std::endl;
#endif
}
#endif // ifdef RANDOMTEST_LIBUSB_ASYNC
void send(
const uint16_t length)
{
if (down_pending) // DEBUG
std::cerr << "send() impossible down_pending==true"
<< std::endl;
++sends; // DEBUG
int error;
#ifdef RANDOMTEST_LIBUSB_ASYNC
if (transfer_type == TransferType::INTERRUPT)
libusb_fill_interrupt_transfer(transfer_out ,
device_handle,
out_endpoint ,
down_buf ,
length ,
down_callback,
0 ,
OUT_TIMEOUT );
else
libusb_fill_bulk_transfer (transfer_out ,
device_handle,
out_endpoint ,
down_buf ,
length ,
down_callback,
0 ,
OUT_TIMEOUT );
down_pending = true;
if ( (error = libusb_submit_transfer(transfer_out))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_submit_transfer(<transfer_out, send()>) failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(error, __LINE__);
}
#else
int bytes_transferred;
if (transfer_type == TransferType::INTERRUPT)
error = libusb_interrupt_transfer(device_handle ,
out_endpoint ,
down_buf ,
length ,
&bytes_transferred,
OUT_TIMEOUT );
else
error = libusb_bulk_transfer (device_handle ,
out_endpoint ,
down_buf ,
length ,
&bytes_transferred,
OUT_TIMEOUT );
if ( error != static_cast<int>(LIBUSB_SUCCESS)
|| bytes_transferred != length ) {
std::cerr << "out libusb_interrupt_transfer() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(-1, __LINE__);
}
#ifdef DEBUG
std::cerr << "writ "
<< std::setw(2)
<< bytes_transferred
<< " bytes cksum: 0x"
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< checksum(down_buf, bytes_transferred)
<< std::setfill(' ')
<< std::dec
<< " "
<< buffer_ascii(down_buf, bytes_transferred)
<< std::endl;
#endif
#endif
}
} // namespace
int main(
int argc ,
char *argv[])
{
uint16_t vendor = VENDOR ,
product = PRODUCT;
if (argc > 1 && argv[1][0] == '-') {
std::cout << "Usage: "
<< argv[0]
<< " [<vendor id> [product id [in endpoint [out endpoint "
" [i<nterupt>|b<ulk>]]]]]"
<< std::endl;
return 0;
}
if (argc > 1) vendor = strtol(argv[1], 0, 16);
if (argc > 2) product = strtol(argv[2], 0, 16);
if (argc > 3) in_endpoint = strtol(argv[3], 0, 16);
if (argc > 4) out_endpoint = strtol(argv[4], 0, 16);
if (argc > 4) out_endpoint = strtol(argv[4], 0, 16);
if (argc > 5) transfer_type = argv[5][0] == 'i'
? TransferType::INTERRUPT
: TransferType::BULK ;
int error;
if ((error = libusb_init(0)) != static_cast<int>(LIBUSB_SUCCESS)) {
std::cerr << "libusb_init() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
if (!(device_handle = libusb_open_device_with_vid_pid(0, vendor, product))){
std::cerr << "libusb_open_device_with_vid_pid(0, "
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< vendor
<< ", "
<< product
<< ") failure"
<< std::endl;
return 1;
}
#if 0 // not necessary, Linux kernel has no driver for custom simple USB device
if ( (error = libusb_set_auto_detach_kernel_driver(device_handle, 1))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_set_auto_detach_kernel_driver(device_handle, 1)) "
"failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
#endif
if ( (error = libusb_claim_interface(device_handle, 0))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_claim_interface(device_handle, 0) failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
if (!(transfer_in = libusb_alloc_transfer(0))) {
std::cerr << "libusb_alloc_transfer() for transfer_in failure"
<< std::endl;
return 1;
}
if (!(transfer_out = libusb_alloc_transfer(0))) {
std::cerr << "libusb_alloc_transfer() for transfer_out failure"
<< std::endl;
return 1;
}
libusb_set_auto_detach_kernel_driver(device_handle, 1);
if (gettimeofday(&prev, 0) == -1) {
std::cerr << "gettimeofday() first call failure:"
<< strerror(errno)
<< " ("
<< errno
<< ')'
<< std::endl;
return 1;
}
signal(SIGINT, report);
#ifdef RANDOMTEST_LIBUSB_ASYNC
if (transfer_type == TransferType::INTERRUPT)
libusb_fill_interrupt_transfer(transfer_in ,
device_handle,
in_endpoint ,
up_buf ,
MAX_IN_PACKET,
up_callback ,
0 ,
IN_TIMEOUT );
else
libusb_fill_bulk_transfer (transfer_in ,
device_handle,
in_endpoint ,
up_buf ,
MAX_IN_PACKET,
up_callback ,
0 ,
IN_TIMEOUT );
if ( (error = libusb_submit_transfer(transfer_in))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_submit_transfer(<transfer_in, main()>) failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
return error;
}
#endif
random_test.send_sync(down_buf, SYNC_LENGTH);
send(SYNC_LENGTH);
struct timeval tv = {20, 0}; // 20 seconds
unsigned prev_report = 0 ;
while (true) {
#ifdef RANDOMTEST_LIBUSB_ASYNC
if ( (error = libusb_handle_events_timeout_completed(0, &tv, 0))
!= static_cast<int>(LIBUSB_SUCCESS) ) {
std::cerr << "libusb_handle_events_completed() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(-1, __LINE__);
}
++handle_events;
#endif // ifdef RANDOMTEST_LIBUSB_ASYNC
if (begin.tv_sec == 0 && random_test.synced()) {
if (gettimeofday(&begin, 0) == -1) {
std::cerr << "gettimeofday() begin failure:"
<< strerror(errno)
<< " ("
<< errno
<< ')'
<< std::endl;
report(-1, __LINE__);
}
}
else if ( random_test.send_count()
+ random_test.recv_count()
- prev_report
>= REPORT_EVERY ) {
report(0);
prev_report = random_test.send_count() + random_test.recv_count();
}
struct timeval crnt, diff;
if (gettimeofday(&crnt, 0) == -1) {
std::cerr << "gettimeofday() crnt failure:"
<< strerror(errno)
<< " ("
<< errno
<< ')'
<< std::endl;
report(-1, __LINE__);
}
timersub(&crnt, &prev, &diff);
#ifdef DEBUG
if (diff.tv_sec < 1) continue;
#else
if (!random_test.synced() && diff.tv_sec < 1) continue;
#endif
prev = crnt;
#ifndef RANDOMTEST_LIBUSB_ASYNC
int bytes_transferred;
if (transfer_type == TransferType::INTERRUPT)
error = libusb_interrupt_transfer(device_handle ,
in_endpoint ,
up_buf ,
MAX_IN_PACKET ,
&bytes_transferred,
IN_TIMEOUT );
else
error = libusb_bulk_transfer (device_handle ,
in_endpoint ,
up_buf ,
MAX_IN_PACKET ,
&bytes_transferred,
IN_TIMEOUT );
if (error != static_cast<int>(LIBUSB_SUCCESS)) {
std::cerr << "libusb_{interrupt,bulk}_transfer() failure: "
<< libusb_strerror(static_cast<libusb_error>(error))
<< '('
<< error
<< ')'
<< std::endl;
report(error, __LINE__);
}
transfer_in->actual_length = bytes_transferred;
up_callback(transfer_in);
#endif
#ifdef RANDOMTEST_LIBUSB_ASYNC
if (down_pending)
++sends_pending;
else {
#endif
uint16_t length = random_test.send(down_buf);
if (length) // will be zero if not yet synced
send(length);
#ifdef RANDOMTEST_LIBUSB_ASYNC
}
#endif
}
} // main()