libusbk-sys 0.2.0

Rust Windows library for accessing USB devices via libusbK
Documentation
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
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
/*!********************************************************************
libusbK - kBench USB benchmark/diagnostic tool.
Copyright (C) 2012 Travis Lee Robinson. All Rights Reserved.
libusb-win32.sourceforge.net

Development : Travis Lee Robinson  (libusbdotnet@gmail.com)
Testing     : Xiaofan Chen         (xiaofanc@gmail.com)

At the discretion of the user of this library, this software may be
licensed under the terms of the GNU Public License v3 or a BSD-Style
license as outlined in the following files:
* LICENSE-gpl3.txt
* LICENSE-bsd.txt

License files are located in a license folder at the root of source and
binary distributions.
********************************************************************!*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <wtypes.h>

#include "libusbk.h"
#include "lusbk_version.h"
#include "lusbk_linked_list.h"

// warning C4127: conditional expression is constant.
#pragma warning(disable: 4127)

#define COMPOSITE_MERGE_MODE 1


#define MAX_OUTSTANDING_TRANSFERS 10


#define USB_ENDPOINT_ADDRESS_MASK 0x0F


// This is used only in VerifyData() for display information
// about data validation mismatches.
#define CONVDAT(format,...) printf("[data-mismatch] " format,__VA_ARGS__)


// All output is directed through these macros.
//
#define LOG(LogTypeString,format,...)  printf("%s[" __FUNCTION__ "] "format, LogTypeString, __VA_ARGS__)

#define LOG_NO_FN(LogTypeString,format,...)  printf("%s" format "%s",LogTypeString,__VA_ARGS__,"")

#define CONERR(format,...) LOG("Error:",format,__VA_ARGS__)

#define CONMSG(format,...) LOG_NO_FN("",format,__VA_ARGS__)

#define CONWRN(format,...) LOG("Warn:",format,__VA_ARGS__)

#define CONDBG(format,...) LOG_NO_FN("",format,__VA_ARGS__)


#define CONERR0(message) CONERR("%s", message)

#define CONMSG0(message) CONMSG("%s", message)

#define CONWRN0(message) CONWRN("%s", message)

#define CONDBG0(message) CONDBG("%s", message)


static LPCSTR DrvIdNames[8] = { "libusbK", "libusb0", "WinUSB", "libusb0 filter", "Unknown", "Unknown", "Unknown" };
#define GetDrvIdString(DriverID)	(DrvIdNames[((((LONG)(DriverID))<0) || ((LONG)(DriverID)) >= KUSB_DRVID_COUNT)?KUSB_DRVID_COUNT:(DriverID)])


static LPCSTR DevSpeedStrings[4] = { "Unknown", "Low/Full", "Unknown", "High" };
#define GetDevSpeedString(DevSpeed)	(DevSpeedStrings[(DevSpeed) & 0x3])


#define VerifyListLock(mTest) while(InterlockedExchange(&((mTest)->VerifyLock),1) != 0) Sleep(0)

#define VerifyListUnlock(mTest) InterlockedExchange(&((mTest)->VerifyLock),0)


KUSB_DRIVER_API K;

typedef struct _BENCHMARK_BUFFER
{
	PUCHAR	Data;
	LONG	DataLength;
	LONG	SyncFailed;

	struct _BENCHMARK_BUFFER* prev;
	struct _BENCHMARK_BUFFER* next;
} BENCHMARK_BUFFER, * PBENCHMARK_BUFFER;
// Custom vendor requests that must be implemented in the benchmark firmware.
// Test selection can be bypassed with the "notestselect" argument.
//
typedef enum _BENCHMARK_DEVICE_COMMAND
{
	SET_TEST = 0x0E,
	GET_TEST = 0x0F,
} BENCHMARK_DEVICE_COMMAND, * PBENCHMARK_DEVICE_COMMAND;

// Tests supported by the official benchmark firmware.
//
typedef enum _BENCHMARK_DEVICE_TEST_TYPE
{
	TestTypeNone = 0x00,
	TestTypeRead = 0x01,
	TestTypeWrite = 0x02,
	TestTypeLoop = TestTypeRead | TestTypeWrite,
} BENCHMARK_DEVICE_TEST_TYPE, * PBENCHMARK_DEVICE_TEST_TYPE;

// This software was mainly created for testing the libusb-win32 kernel & user driver.
typedef enum _BENCHMARK_TRANSFER_MODE
{
	// Tests for the libusb-win32 sync transfer function.
	TRANSFER_MODE_SYNC,

	// Test for async function, iso transfers, and queued transfers
	TRANSFER_MODE_ASYNC,
} BENCHMARK_TRANSFER_MODE;

typedef struct _BENCHMARK_ISOCH_RESULTS
{
	UINT GoodPackets;
	UINT BadPackets;
	UINT Length;
	UINT TotalPackets;
}BENCHMARK_ISOCH_RESULTS;

// Holds all of the information about a test.
typedef struct _BENCHMARK_TEST_PARAM
{
	// User configurable value set from the command line.
	//
	INT Vid;			// Vendor ID
	INT Pid;			// Porduct ID
	INT Intf;			// Interface number
	INT	Altf;			// Alt Interface number
	INT Ep;				// Endpoint number (1-15)
	INT Refresh;		// Refresh interval (ms)
	INT Timeout;		// Transfer timeout (ms)
	INT Retry;			// Number for times to retry a timed out transfer before aborting
	INT AllocBufferSize;		// Number of bytes to transfer
	INT ReadLength;
	INT WriteLength;
	INT BufferCount;	// Number of outstanding asynchronous transfers
	BOOL NoTestSelect;	// If true, don't send control message to select the test type.
	BOOL UseList;		// Show the user a device list and let them choose a benchmark device.
	INT FixedIsoPackets; // Number of fixed packets to use for ISO writes. Ignored for read eps.
	INT Priority;		// Priority to run this thread at.
	BOOL Verify;		// Only for loop and read test. If true, verifies data integrity.
	BOOL VerifyDetails;	// If true, prints detailed information for each invalid byte.
	enum BENCHMARK_DEVICE_TEST_TYPE TestType;	// The benchmark test type.
	enum BENCHMARK_TRANSFER_MODE TransferMode;	// Sync or Async

	// Internal value use during the test.
	//
	KLST_HANDLE DeviceList;
	KLST_DEVINFO_HANDLE SelectedDeviceProfile;
	HANDLE DeviceHandle;
	KUSB_HANDLE InterfaceHandle;
	USB_DEVICE_DESCRIPTOR DeviceDescriptor;
	USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
	WINUSB_PIPE_INFORMATION_EX PipeInformation[32];
	BOOL IsCancelled;
	BOOL IsUserAborted;

	BYTE* VerifyBuffer;		// Stores the verify test pattern for 1 packet.
	WORD VerifyBufferSize;	// Size of VerifyBuffer
	BOOL Use_UsbK_Init;
	BOOL ListDevicesOnly;
	ULONG DeviceSpeed;

	BOOL ReadLogEnabled;
	FILE* ReadLogFile;

	BOOL WriteLogEnabled;
	FILE* WriteLogFile;

	volatile long VerifyLock;
	BENCHMARK_BUFFER* VerifyList;
	BOOL IsLoopSynced;

	UCHAR UseRawIO;
	UCHAR DefaultAltSetting;
	BOOL UseIsoAsap;


} BENCHMARK_TEST_PARAM, * PBENCHMARK_TEST_PARAM;

// The benchmark transfer context used for asynchronous transfers.  see TransferAsync().
typedef struct _BENCHMARK_TRANSFER_HANDLE
{
	KISOCH_HANDLE IsochHandle;
	OVERLAPPED Overlapped;
	BOOL InUse;
	PUCHAR Data;
	INT DataMaxLength;
	INT ReturnCode;
	BENCHMARK_ISOCH_RESULTS IsochResults;
} BENCHMARK_TRANSFER_HANDLE, * PBENCHMARK_TRANSFER_HANDLE;
#pragma warning(disable:4200)
// Holds all of the information about a transfer.
typedef struct _BENCHMARK_TRANSFER_PARAM
{
	PBENCHMARK_TEST_PARAM Test;
	UINT FrameNumber;
	UINT NumberOfIsoPackets;
	HANDLE ThreadHandle;
	DWORD ThreadID;
	WINUSB_PIPE_INFORMATION_EX Ep;
	USB_SUPERSPEED_ENDPOINT_COMPANION_DESCRIPTOR EpCompanionDescriptor;
	BOOL HasEpCompanionDescriptor;
	BOOL IsRunning;

	LONGLONG TotalTransferred;
	LONG LastTransferred;

	LONG Packets;
	DWORD StartTick;
	DWORD LastTick;
	DWORD LastStartTick;

	INT TotalTimeoutCount;
	INT RunningTimeoutCount;

	INT TotalErrorCount;
	INT RunningErrorCount;

	INT ShortTransferCount;

	INT TransferHandleNextIndex;
	INT TransferHandleWaitIndex;
	INT OutstandingTransferCount;

	BENCHMARK_TRANSFER_HANDLE TransferHandles[MAX_OUTSTANDING_TRANSFERS];
	BENCHMARK_ISOCH_RESULTS IsochResults;

	// Placeholder for end of structure; this is where the raw data for the
	// transfer buffer is allocated.
	//
	UCHAR Buffer[0];
} BENCHMARK_TRANSFER_PARAM, * PBENCHMARK_TRANSFER_PARAM;

#include <pshpack1.h>
typedef struct _KBENCH_CONTEXT_LSTK
{
	BYTE Selected;
} KBENCH_CONTEXT_LSTK, * PKBENCH_CONTEXT_LSTK;
#include <poppack.h>

#pragma warning(default:4200)

// Benchmark device api.
BOOL Bench_Open(__in PBENCHMARK_TEST_PARAM test);

BOOL Bench_Configure(__in KUSB_HANDLE handle,
	__in BENCHMARK_DEVICE_COMMAND command,
	__in UCHAR intf,
	__deref_inout PBENCHMARK_DEVICE_TEST_TYPE testType);

// Critical section for running status.
CRITICAL_SECTION DisplayCriticalSection;

// Finds the interface for [interface_number] in a libusb-win32 config descriptor.
// If first_interface is not NULL, it is set to the first interface in the config.
//

// Internal function used by the benchmark application.
void ShowHelp(void);
void ShowCopyright(void);

void SetTestDefaults(PBENCHMARK_TEST_PARAM test);
int GetTestDeviceFromArgs(PBENCHMARK_TEST_PARAM test);
int GetTestDeviceFromList(PBENCHMARK_TEST_PARAM test);

char* GetParamStrValue(const char* src, const char* paramName);
BOOL GetParamIntValue(const char* src, const char* paramName, INT* returnValue);
int ValidateBenchmarkArgs(PBENCHMARK_TEST_PARAM test);
int ParseBenchmarkArgs(PBENCHMARK_TEST_PARAM testParams, int argc, char** argv);
void FreeTransferParam(PBENCHMARK_TRANSFER_PARAM* testTransferRef);
PBENCHMARK_TRANSFER_PARAM CreateTransferParam(PBENCHMARK_TEST_PARAM test, int endpointID);
void GetAverageBytesSec(PBENCHMARK_TRANSFER_PARAM transferParam, DOUBLE* bps);
void GetCurrentBytesSec(PBENCHMARK_TRANSFER_PARAM transferParam, DOUBLE* bps);
void ShowRunningStatus(PBENCHMARK_TRANSFER_PARAM readParam, PBENCHMARK_TRANSFER_PARAM writeParam);
void ShowTestInfo(PBENCHMARK_TEST_PARAM test);
void ShowTransferInfo(PBENCHMARK_TRANSFER_PARAM transferParam);

BOOL WaitForTestTransfer(PBENCHMARK_TRANSFER_PARAM transferParam, UINT msToWait);
void ResetRunningStatus(PBENCHMARK_TRANSFER_PARAM transferParam);

// The thread transfer routine.
DWORD TransferThreadProc(PBENCHMARK_TRANSFER_PARAM transferParams);

#define TRANSFER_DISPLAY(TransferParam, ReadingString, WritingString) \

	((TransferParam->Ep.PipeId & USB_ENDPOINT_DIRECTION_MASK) ? ReadingString : WritingString)

#define INC_ROLL(IncField, RollOverValue) if ((++IncField) >= RollOverValue) IncField = 0


#define ENDPOINT_TYPE(TransferParam) (TransferParam->Ep.PipeType & 3)

const char* TestDisplayString[] = { "None", "Read", "Write", "Loop", NULL };
const char* EndpointTypeDisplayString[] = { "Control", "Isochronous", "Bulk", "Interrupt", NULL };

LONG WinError(__in_opt DWORD errorCode)
{
	LPSTR buffer = NULL;

	errorCode = errorCode ? labs(errorCode) : GetLastError();
	if (!errorCode) return errorCode;

	if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
		NULL, errorCode, 0, (LPSTR)&buffer, 0, NULL) > 0)
	{
		CONERR("%s\n", buffer);
		SetLastError(0);
	}
	else
	{
		CONERR("FormatMessage error!\n");
	}

	if (buffer)
		LocalFree(buffer);

	return -labs(errorCode);
}

void SetTestDefaults(PBENCHMARK_TEST_PARAM test)
{
	memset(test, 0, sizeof(*test));

	test->Ep = 0x00;
	test->Vid = 0x04D8;
	test->Pid = 0xFA2E;
	test->Refresh = 1000;
	test->Timeout = 5000;
	test->TestType = TestTypeLoop;
	test->AllocBufferSize = 4096;
	test->ReadLength = test->AllocBufferSize;
	test->WriteLength = test->AllocBufferSize;
	test->BufferCount = 1;
	test->Priority = THREAD_PRIORITY_NORMAL;
	test->Intf = -1;
	test->Altf = -1;
	test->UseRawIO = 0xFF;
}

VOID AppendLoopBuffer(PBENCHMARK_TEST_PARAM Test, PUCHAR data, LONG dataLength)
{
	if (Test->Verify && Test->TestType == TestTypeLoop)
	{
		BENCHMARK_BUFFER* newVerifyBuf = malloc(sizeof(BENCHMARK_BUFFER) + dataLength);

		memset(newVerifyBuf, 0, sizeof(BENCHMARK_BUFFER));

		newVerifyBuf->Data = &(((PUCHAR)newVerifyBuf)[sizeof(BENCHMARK_BUFFER)]);
		newVerifyBuf->DataLength = dataLength;
		memcpy(newVerifyBuf->Data, data, dataLength);

		VerifyListLock(Test);
		DL_APPEND(Test->VerifyList, newVerifyBuf);
		VerifyListUnlock(Test);
	}
}

BOOL Bench_Open(__in PBENCHMARK_TEST_PARAM test)
{
	UCHAR altSetting;
	KUSB_HANDLE associatedHandle;
	UINT transferred;
	KLST_DEVINFO_HANDLE deviceInfo;

	test->SelectedDeviceProfile = NULL;

	LstK_MoveReset(test->DeviceList);

	while (LstK_MoveNext(test->DeviceList, &deviceInfo))
	{
		// enabled
		UINT userContext = (UINT)LibK_GetContext(deviceInfo, KLIB_HANDLE_TYPE_LSTINFOK);
		if (userContext != TRUE) continue;

		if (!LibK_LoadDriverAPI(&K, deviceInfo->DriverID))
		{
			WinError(0);
			CONWRN("could not load driver api %s.\n", GetDrvIdString(deviceInfo->DriverID));
			continue;
		}
		if (!test->Use_UsbK_Init)
		{
			test->DeviceHandle = CreateFileA(deviceInfo->DevicePath,
				GENERIC_READ | GENERIC_WRITE,
				FILE_SHARE_READ | FILE_SHARE_WRITE,
				NULL,
				OPEN_EXISTING,
				FILE_FLAG_OVERLAPPED,
				NULL);

			if (!test->DeviceHandle || test->DeviceHandle == INVALID_HANDLE_VALUE)
			{
				WinError(0);
				test->DeviceHandle = NULL;
				CONWRN("could not create device handle.\n%s\n", deviceInfo->DevicePath);
				continue;
			}

			if (!K.Initialize(test->DeviceHandle, &test->InterfaceHandle))
			{
				WinError(0);
				CloseHandle(test->DeviceHandle);
				test->DeviceHandle = NULL;
				test->InterfaceHandle = NULL;
				CONWRN("could not initialize device.\n%s\n", deviceInfo->DevicePath);
				continue;
			}
		}
		else
		{
			if (!K.Init(&test->InterfaceHandle, deviceInfo))
			{
				WinError(0);
				test->DeviceHandle = NULL;
				test->InterfaceHandle = NULL;
				CONWRN("could not open device.\n%s\n", deviceInfo->DevicePath);
				continue;
			}

		}

		if (!K.GetDescriptor(test->InterfaceHandle,
			USB_DESCRIPTOR_TYPE_DEVICE,
			0, 0,
			(PUCHAR)&test->DeviceDescriptor,
			sizeof(test->DeviceDescriptor),
			&transferred))
		{
			WinError(0);

			K.Free(test->InterfaceHandle);
			test->InterfaceHandle = NULL;

			if (!test->Use_UsbK_Init)
			{
				CloseHandle(test->DeviceHandle);
				test->DeviceHandle = NULL;
			}

			CONWRN("could not get device descriptor.\n%s\n", deviceInfo->DevicePath);
			continue;
		}
		test->Vid = (INT)test->DeviceDescriptor.idVendor;
		test->Pid = (INT)test->DeviceDescriptor.idProduct;

	NextInterface:

		// While searching for hardware specifics we are also gathering information and storing it
		// in our test.
		memset(&test->InterfaceDescriptor, 0, sizeof(test->InterfaceDescriptor));
		altSetting = 0;
		while (K.QueryInterfaceSettings(test->InterfaceHandle, altSetting, &test->InterfaceDescriptor))
		{
			// found an interface
			UCHAR pipeIndex = 0;
			int hasIsoEndpoints = 0;
			int hasZeroMaxPacketEndpoints = 0;

			memset(&test->PipeInformation, 0, sizeof(test->PipeInformation));
			while (K.QueryPipeEx(test->InterfaceHandle, altSetting, pipeIndex, &test->PipeInformation[pipeIndex]))
			{
				// found a pipe
				if (test->PipeInformation[pipeIndex].PipeType == UsbdPipeTypeIsochronous)
					hasIsoEndpoints++;

				if (!test->PipeInformation[pipeIndex].MaximumPacketSize)
					hasZeroMaxPacketEndpoints++;

				pipeIndex++;
			}

			if (pipeIndex > 0)
			{
				// -1 means the user din't specifiy so we find the most suitable device.
				//
				if (((test->Intf == -1) || (test->Intf == test->InterfaceDescriptor.bInterfaceNumber)) &&
					((test->Altf == -1) || (test->Altf == test->InterfaceDescriptor.bAlternateSetting)))
				{
					// if the user actually specifies an alt iso setting with zero MaxPacketEndpoints
					// we let let them.
					if (test->Altf == -1 && hasIsoEndpoints && hasZeroMaxPacketEndpoints)
					{
						// user didn't specfiy and we know we can't tranfer with this alt setting so skip it.
						CONMSG("skipping interface %02X:%02X. zero-length iso endpoints exist.\n",
							test->InterfaceDescriptor.bInterfaceNumber,
							test->InterfaceDescriptor.bAlternateSetting);
					}
					else
					{
						// this is the one we are looking for.
						test->Intf = test->InterfaceDescriptor.bInterfaceNumber;
						test->Altf = test->InterfaceDescriptor.bAlternateSetting;
						test->SelectedDeviceProfile = deviceInfo;

						// some buffering is required for iso.
						if (hasIsoEndpoints && test->BufferCount == 1)
							test->BufferCount++;

						test->DefaultAltSetting = 0;
						K.GetCurrentAlternateSetting(test->InterfaceHandle, &test->DefaultAltSetting);
						if (!K.SetCurrentAlternateSetting(test->InterfaceHandle, test->InterfaceDescriptor.bAlternateSetting))
						{
							CONERR("failed selecting alternate setting %02Xh\n", test->Altf);
							return FALSE;
						}
						return TRUE;
					}
				}
			}
			altSetting++;
			memset(&test->InterfaceDescriptor, 0, sizeof(test->InterfaceDescriptor));
		}
		if (K.GetAssociatedInterface(test->InterfaceHandle, 0, &associatedHandle))
		{
			// this device has more interfaces to look at.
			//
			K.Free(test->InterfaceHandle);
			test->InterfaceHandle = associatedHandle;
			goto NextInterface;
		}

		// This one didn't match the test specifics; continue on to the next potential match.
		K.Free(test->InterfaceHandle);
		test->InterfaceHandle = NULL;
	}

	CONERR("device interface/alt interface not found (%02Xh/%02Xh).\n", test->Intf, test->Altf);
	return FALSE;
}

BOOL Bench_Configure(__in KUSB_HANDLE handle,
	__in BENCHMARK_DEVICE_COMMAND command,
	__in UCHAR intf,
	__deref_inout PBENCHMARK_DEVICE_TEST_TYPE testType)
{
	UCHAR buffer[1];
	UINT transferred = 0;
	WINUSB_SETUP_PACKET Pkt;
	KUSB_SETUP_PACKET* defPkt = (KUSB_SETUP_PACKET*)&Pkt;

	memset(&Pkt, 0, sizeof(Pkt));
	defPkt->BmRequest.Dir = BMREQUEST_DIR_DEVICE_TO_HOST;
	defPkt->BmRequest.Type = BMREQUEST_TYPE_VENDOR;
	defPkt->Request = (UCHAR)command;
	defPkt->Value = (UCHAR)*testType;
	defPkt->Index = intf;
	defPkt->Length = 1;

	if (!handle || handle == INVALID_HANDLE_VALUE)
		return WinError(ERROR_INVALID_HANDLE);

	if (K.ControlTransfer(handle, Pkt, buffer, 1, &transferred, NULL))
	{
		if (transferred)
			return TRUE;
	}

	return WinError(0);
}

INT VerifyData(PBENCHMARK_TRANSFER_PARAM transferParam, BYTE* data, INT dataLength)
{

	WORD verifyDataSize = transferParam->Test->VerifyBufferSize;
	BYTE* verifyData = transferParam->Test->VerifyBuffer;
	BYTE keyC = 0;
	BOOL seedKey = TRUE;
	INT dataLeft = dataLength;
	INT dataIndex = 0;
	INT packetIndex = 0;
	INT verifyIndex = 0;

	while (dataLeft > 1)
	{
		verifyDataSize = dataLeft > transferParam->Test->VerifyBufferSize ? transferParam->Test->VerifyBufferSize : (WORD)dataLeft;

		if (seedKey)
			keyC = data[dataIndex + 1];
		else
		{
			if (data[dataIndex + 1] == 0)
			{
				keyC = 0;
			}
			else
			{
				keyC++;
			}
		}
		seedKey = FALSE;
		// Index 0 is always 0.
		// The key is always at index 1
		verifyData[1] = keyC;

		if (memcmp(&data[dataIndex], verifyData, verifyDataSize) != 0)
		{
			// Packet verification failed.

			// Reset the key byte on the next packet.
			seedKey = TRUE;

			CONVDAT("Packet=#%d Data=#%d\n", packetIndex, dataIndex);

			if (transferParam->Test->VerifyDetails)
			{
				for (verifyIndex = 0; verifyIndex < verifyDataSize; verifyIndex++)
				{
					if (verifyData[verifyIndex] == data[dataIndex + verifyIndex])
						continue;

					CONVDAT("packet-offset=%d expected %02Xh got %02Xh\n",
						verifyIndex,
						verifyData[verifyIndex],
						data[dataIndex + verifyIndex]);

				}
			}
		}

		// Move to the next packet.
		packetIndex++;
		dataLeft -= verifyDataSize;
		dataIndex += verifyDataSize;
	}

	return 0;
}

int TransferSync(PBENCHMARK_TRANSFER_PARAM transferParam)
{
	UINT transferred;
	BOOL success;
	if (transferParam->Ep.PipeId & USB_ENDPOINT_DIRECTION_MASK)
	{
		success = K.ReadPipe(transferParam->Test->InterfaceHandle,
			transferParam->Ep.PipeId,
			transferParam->Buffer,
			transferParam->Test->ReadLength,
			&transferred,
			NULL);
	}
	else
	{
		AppendLoopBuffer(transferParam->Test, transferParam->Buffer, transferParam->Test->WriteLength);
		success = K.WritePipe(transferParam->Test->InterfaceHandle,
			transferParam->Ep.PipeId,
			transferParam->Buffer,
			transferParam->Test->WriteLength,
			&transferred,
			NULL);
	}

	return success ? (int)transferred : -labs(GetLastError());
}

BOOL WINAPI CalcIsochTransfeResultsCb(_in UINT PacketIndex, _ref PUINT Offset, _ref PUINT Length, _ref PUINT Status, _in PVOID UserState)
{
	BENCHMARK_ISOCH_RESULTS* pResults = UserState;
	
	UNREFERENCED_PARAMETER(PacketIndex);
	UNREFERENCED_PARAMETER(Offset);

	if (*Status)
	{
		pResults->BadPackets++;
	}
	else
	{
		if (*Length)
		{
			pResults->GoodPackets++;
			pResults->Length += *Length;
		}
	}
	pResults->TotalPackets++;

	return TRUE;
}

int TransferAsync(PBENCHMARK_TRANSFER_PARAM transferParam, PBENCHMARK_TRANSFER_HANDLE* handleRef)
{
	int ret = 0;
	BOOL success;
	PBENCHMARK_TRANSFER_HANDLE handle;
	DWORD transferErrorCode;

	*handleRef = NULL;

	// Submit transfers until the maximum number of outstanding transfer(s) is reached.
	while (transferParam->OutstandingTransferCount < transferParam->Test->BufferCount)
	{
		// Get the next available benchmark transfer handle.
		*handleRef = handle = &transferParam->TransferHandles[transferParam->TransferHandleNextIndex];

		// If a libusb-win32 transfer context hasn't been setup for this benchmark transfer
		// handle, do it now.
		//
		if (!handle->Overlapped.hEvent)
		{
			handle->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
			// Data buffer(s) are located at the end of the transfer param.
			handle->Data = transferParam->Buffer + (transferParam->TransferHandleNextIndex * transferParam->Test->AllocBufferSize);
		}
		else
		{
			// re-initialize and re-use the overlapped
			ResetEvent(handle->Overlapped.hEvent);
		}

		if (transferParam->Ep.PipeId & USB_ENDPOINT_DIRECTION_MASK)
		{
			handle->DataMaxLength = transferParam->Test->ReadLength;
			if (transferParam->Ep.PipeType == UsbdPipeTypeIsochronous)
			{
				success = K.IsochReadPipe(handle->IsochHandle,
					handle->DataMaxLength,
					&transferParam->FrameNumber,
					0,
					&handle->Overlapped);
			}
			else
			{
				success = K.ReadPipe(transferParam->Test->InterfaceHandle,
					transferParam->Ep.PipeId,
					handle->Data,
					handle->DataMaxLength,
					NULL,
					&handle->Overlapped);
			}
		}
		else
		{
			AppendLoopBuffer(transferParam->Test, handle->Data, transferParam->Test->WriteLength);
			handle->DataMaxLength = transferParam->Test->WriteLength;
			if (transferParam->Ep.PipeType == UsbdPipeTypeIsochronous)
			{
				success = K.IsochWritePipe(handle->IsochHandle,
					handle->DataMaxLength,
					&transferParam->FrameNumber,
					0,
					&handle->Overlapped);
			}
			else
			{
				success = K.WritePipe(transferParam->Test->InterfaceHandle,
					transferParam->Ep.PipeId,
					handle->Data,
					handle->DataMaxLength,
					NULL,
					&handle->Overlapped);

			}
		}
		transferErrorCode = GetLastError();

		if (!success && transferErrorCode == ERROR_IO_PENDING)
		{
			transferErrorCode = ERROR_SUCCESS;
			success = TRUE;
		}

		// Submit this transfer now.
		handle->ReturnCode = ret = -labs(transferErrorCode);
		if (ret < 0)
		{
			handle->InUse = FALSE;
			goto Done;
		}

		// Mark this handle has InUse.
		handle->InUse = TRUE;

		// When transfers ir successfully submitted, OutstandingTransferCount goes up; when
		// they are completed it goes down.
		//
		transferParam->OutstandingTransferCount++;

		// Move TransferHandleNextIndex to the next available transfer.
		INC_ROLL(transferParam->TransferHandleNextIndex, transferParam->Test->BufferCount);

	}

	// If the number of outstanding transfers has reached the limit, wait for the
	// oldest outstanding transfer to complete.
	//
	if (transferParam->OutstandingTransferCount == transferParam->Test->BufferCount)
	{
		UINT transferred;
		// TransferHandleWaitIndex is the index of the oldest outstanding transfer.
		*handleRef = handle = &transferParam->TransferHandles[transferParam->TransferHandleWaitIndex];

		// Only wait, cancelling & freeing is handled by the caller.
		if (WaitForSingleObject(handle->Overlapped.hEvent, transferParam->Test->Timeout) != WAIT_OBJECT_0)
		{
			if (!transferParam->Test->IsUserAborted)
				ret = WinError(0);
			else
				ret = -labs(GetLastError());

			handle->ReturnCode = ret;
			goto Done;
		}
		if (!K.GetOverlappedResult(transferParam->Test->InterfaceHandle, &handle->Overlapped, &transferred, FALSE))
		{
			if (!transferParam->Test->IsUserAborted)
				ret = WinError(0);
			else
				ret = -labs(GetLastError());

			handle->ReturnCode = ret;
			goto Done;
		}
		if (transferParam->Ep.PipeType == UsbdPipeTypeIsochronous && transferParam->Ep.PipeId & 0x80)
		{
			// iso read pipe
			memset(&handle->IsochResults, 0, sizeof(handle->IsochResults));
			IsochK_EnumPackets(handle->IsochHandle, &CalcIsochTransfeResultsCb, 0, &handle->IsochResults);
			transferParam->IsochResults.TotalPackets += handle->IsochResults.TotalPackets;
			transferParam->IsochResults.GoodPackets += handle->IsochResults.GoodPackets;
			transferParam->IsochResults.BadPackets += handle->IsochResults.BadPackets;
			transferParam->IsochResults.Length += handle->IsochResults.Length;
			transferred = handle->IsochResults.Length;
		}
		else if (transferParam->Ep.PipeType == UsbdPipeTypeIsochronous)
		{
			// iso write pipe
			transferred = handle->DataMaxLength;

			transferParam->IsochResults.TotalPackets += transferParam->NumberOfIsoPackets;
			transferParam->IsochResults.GoodPackets += transferParam->NumberOfIsoPackets;
		}

		handle->ReturnCode = ret = (DWORD)transferred;

		if (ret < 0) goto Done;

		// Mark this handle has no longer InUse.
		handle->InUse = FALSE;

		// When transfers ir successfully submitted, OutstandingTransferCount goes up; when
		// they are completed it goes down.
		//
		transferParam->OutstandingTransferCount--;

		// Move TransferHandleWaitIndex to the oldest outstanding transfer.
		INC_ROLL(transferParam->TransferHandleWaitIndex, transferParam->Test->BufferCount);
	}

Done:
	return ret;
}

VOID VerifyLoopData(PBENCHMARK_TEST_PARAM Test, PUCHAR chData, int dataRemaining)
{
	LONG syncOffset = 0;
	PUCHAR dataBuffer = chData;
	int dataLength = dataRemaining;

	VerifyListLock(Test);

	if (dataRemaining)
	{
		int verifyStageSize;
		PBENCHMARK_BUFFER writeVerifyBuffer, tmpWriteVerifyBuffer;

		DL_FOREACH_SAFE(Test->VerifyList, writeVerifyBuffer, tmpWriteVerifyBuffer)
		{
			if (!Test->IsLoopSynced)
			{
				verifyStageSize = min(writeVerifyBuffer->DataLength, dataRemaining);
				while (verifyStageSize > 0)
				{
					Test->IsLoopSynced = memcmp(writeVerifyBuffer->Data, chData, verifyStageSize) == 0 ? TRUE : FALSE;
					if (Test->IsLoopSynced) break;

					syncOffset++;
					dataRemaining--;
					chData++;
					verifyStageSize = min(writeVerifyBuffer->DataLength, dataRemaining);
				}

				if (!Test->IsLoopSynced)
				{
					chData = dataBuffer;
					dataRemaining = dataLength;

					if (writeVerifyBuffer->SyncFailed++ == 0)
					{
						writeVerifyBuffer = NULL;
					}
				}
				else
				{
					float reliability = ((float)verifyStageSize / (float)dataLength) * 100.0F;
					CONMSG("Loop data synchronized. Offset=%u Byte=%02Xh Reliability=%.1f%%.\n",
						syncOffset, chData[0], reliability);
				}
			}

			if (Test->IsLoopSynced)
			{
				verifyStageSize = min(writeVerifyBuffer->DataLength, dataRemaining);

				if (memcmp(writeVerifyBuffer->Data, chData, verifyStageSize) == 0)
				{
					writeVerifyBuffer->DataLength -= verifyStageSize;
					dataRemaining -= verifyStageSize;
					writeVerifyBuffer->Data += verifyStageSize;
					chData = &chData[verifyStageSize];
				}
				else
				{
					CONVDAT("DataLength=%u\n", writeVerifyBuffer->DataLength);
					Test->IsLoopSynced = FALSE;

					if (writeVerifyBuffer->SyncFailed++ == 0)
					{
						// Do not delete the verify buffer until it fails to verify once before.
						writeVerifyBuffer = NULL;
						dataRemaining = 0;
					}
				}
			}

			if (dataRemaining == 0)
				break;
		}

		if (!Test->IsLoopSynced)
		{
			CONVDAT("Loop data not in sync.\n");
		}

		// If writeVerifyBuffer is not null at this point, all buffers before it are deleted.
		// if writeVerifyBuffer.DataLength == 0, it is also deleted.
		if (writeVerifyBuffer && Test->VerifyList)
		{
			PBENCHMARK_BUFFER writeVerifyBufferDel, tmpWriteVerifyBuffer;
			DL_FOREACH_SAFE(Test->VerifyList, writeVerifyBufferDel, tmpWriteVerifyBuffer)
			{
				if (writeVerifyBuffer != writeVerifyBufferDel)
				{
					DL_DELETE(Test->VerifyList, writeVerifyBufferDel);
					free(writeVerifyBufferDel);
				}
				else
				{
					if (writeVerifyBufferDel->DataLength <= 0)
					{
						DL_DELETE(Test->VerifyList, writeVerifyBufferDel);
						free(writeVerifyBufferDel);
					}
					break;
				}
			}
		}
	}

	VerifyListUnlock(Test);
}

DWORD TransferThreadProc(PBENCHMARK_TRANSFER_PARAM transferParam)
{
	int ret, i;
	PBENCHMARK_TRANSFER_HANDLE handle;
	PUCHAR data;

	transferParam->IsRunning = TRUE;

	while (!transferParam->Test->IsCancelled)
	{
		data = NULL;
		handle = NULL;

		if (transferParam->Test->TransferMode == TRANSFER_MODE_SYNC)
		{
			ret = TransferSync(transferParam);
			if (ret >= 0) data = transferParam->Buffer;
		}
		else if (transferParam->Test->TransferMode == TRANSFER_MODE_ASYNC)
		{
			ret = TransferAsync(transferParam, &handle);
			if ((handle) && ret >= 0) data = handle->Data;
		}
		else
		{
			CONERR("invalid transfer mode %d\n", transferParam->Test->TransferMode);
			goto Done;
		}

		if (transferParam->Test->Verify &&
			transferParam->Test->VerifyList &&
			transferParam->Test->TestType == TestTypeLoop &&
			USB_ENDPOINT_DIRECTION_IN(transferParam->Ep.PipeId) && ret > 0)
		{
			VerifyLoopData(transferParam->Test, data, ret);
		}

		if (ret < 0)
		{
			// The user pressed 'Q'.
			if (transferParam->Test->IsUserAborted) break;

			// Transfer timed out
			if (ret == ERROR_SEM_TIMEOUT || ret == ERROR_OPERATION_ABORTED || ret == ERROR_CANCELLED)
			{
				transferParam->TotalTimeoutCount++;
				transferParam->RunningTimeoutCount++;
				CONWRN("Timeout #%d %s on Ep%02Xh..\n",
					transferParam->RunningTimeoutCount,
					TRANSFER_DISPLAY(transferParam, "reading", "writing"),
					transferParam->Ep.PipeId);

				if (transferParam->RunningTimeoutCount > transferParam->Test->Retry)
					break;
			}
			else
			{
				// An error (other than a timeout) occured.
				transferParam->TotalErrorCount++;
				transferParam->RunningErrorCount++;
				CONERR("failed %s! %d of %d ret=%d\n",
					TRANSFER_DISPLAY(transferParam, "reading", "writing"),
					transferParam->RunningErrorCount,
					transferParam->Test->Retry + 1,
					ret);

				K.ResetPipe(transferParam->Test->InterfaceHandle, transferParam->Ep.PipeId);

				if (transferParam->RunningErrorCount > transferParam->Test->Retry)
					break;

			}
			ret = 0;
		}
		else
		{
			transferParam->RunningErrorCount = 0;
			transferParam->RunningTimeoutCount = 0;
			if (USB_ENDPOINT_DIRECTION_IN(transferParam->Ep.PipeId))
			{
				if (transferParam->Test->ReadLogFile)
				{
					fwrite(data, ret, 1, transferParam->Test->ReadLogFile);
				}

				if (transferParam->Test->Verify && transferParam->Test->TestType != TestTypeLoop)
				{
					VerifyData(transferParam, data, ret);
				}
			}
			else
			{
				if (transferParam->Test->WriteLogFile)
				{
					fwrite(data, ret, 1, transferParam->Test->WriteLogFile);
				}
			}
		}

		EnterCriticalSection(&DisplayCriticalSection);

		if (!transferParam->StartTick && transferParam->Packets >= 0)
		{
			transferParam->StartTick = GetTickCount();
			transferParam->LastStartTick = transferParam->StartTick;
			transferParam->LastTick = transferParam->StartTick;

			transferParam->LastTransferred = 0;
			transferParam->TotalTransferred = 0;
			transferParam->Packets = 0;
		}
		else
		{
			if (!transferParam->LastStartTick)
			{
				transferParam->LastStartTick = transferParam->LastTick;
				transferParam->LastTransferred = 0;
			}
			transferParam->LastTick = GetTickCount();

			transferParam->LastTransferred += ret;
			transferParam->TotalTransferred += ret;
			transferParam->Packets++;
		}

		LeaveCriticalSection(&DisplayCriticalSection);
	}

Done:

	for (i = 0; i < transferParam->Test->BufferCount; i++)
	{
		if (transferParam->TransferHandles[i].Overlapped.hEvent)
		{
			if (transferParam->TransferHandles[i].InUse)
			{
				if (!K.AbortPipe(
					transferParam->Test->InterfaceHandle, transferParam->Ep.PipeId) &&
					!transferParam->Test->IsUserAborted)
				{
					ret = WinError(0);
					CONERR("failed cancelling transfer! ret=%d\n", ret);
				}
				else
				{
					CloseHandle(transferParam->TransferHandles[i].Overlapped.hEvent);
					transferParam->TransferHandles[i].Overlapped.hEvent = NULL;
					transferParam->TransferHandles[i].InUse = FALSE;
				}
			}
			Sleep(0);
		}
	}

	for (i = 0; i < transferParam->Test->BufferCount; i++)
	{
		if (transferParam->TransferHandles[i].Overlapped.hEvent)
		{
			if (transferParam->TransferHandles[i].InUse)
			{
				WaitForSingleObject(transferParam->TransferHandles[i].Overlapped.hEvent, transferParam->Test->Timeout);
			}
			else
			{
				WaitForSingleObject(transferParam->TransferHandles[i].Overlapped.hEvent, 0);
			}
			CloseHandle(transferParam->TransferHandles[i].Overlapped.hEvent);
			transferParam->TransferHandles[i].Overlapped.hEvent = NULL;
		}
		transferParam->TransferHandles[i].InUse = FALSE;
	}

	transferParam->IsRunning = FALSE;
	return 0;
}

char* GetParamStrValue(const char* src, const char* paramName)
{
	return (strstr(src, paramName) == src) ? (char*)(src + strlen(paramName)) : NULL;
}

BOOL GetParamIntValue(const char* src, const char* paramName, INT* returnValue)
{
	char* value = GetParamStrValue(src, paramName);
	if (value)
	{
		*returnValue = strtol(value, NULL, 0);
		return TRUE;
	}
	return FALSE;
}

int ValidateBenchmarkArgs(PBENCHMARK_TEST_PARAM test)
{
	if (test->BufferCount < 1 || test->BufferCount > MAX_OUTSTANDING_TRANSFERS)
	{
		CONERR("Invalid BufferCount argument %d. BufferCount must be greater than 0 and less than or equal to %d.\n",
			test->BufferCount, MAX_OUTSTANDING_TRANSFERS);
		return -1;
	}

	return 0;
}

int ParseBenchmarkArgs(PBENCHMARK_TEST_PARAM testParams, int argc, char** argv)
{
#define GET_INT_VAL

	char arg[MAX_PATH];
	char* value;
	int iarg;

	for (iarg = 1; iarg < argc; iarg++)
	{
		if (strcpy_s(arg, _countof(arg), argv[iarg]) != ERROR_SUCCESS)
			return -1;

		_strlwr_s(arg, MAX_PATH);

		if (GetParamIntValue(arg, "vid=", &testParams->Vid)) {}
		else if (GetParamIntValue(arg, "pid=", &testParams->Pid)) {}
		else if (GetParamIntValue(arg, "retry=", &testParams->Retry)) {}
		else if (GetParamIntValue(arg, "buffercount=", &testParams->BufferCount))
		{
			if (testParams->BufferCount > 1)
				testParams->TransferMode = TRANSFER_MODE_ASYNC;
		}
		else if (GetParamIntValue(arg, "buffersize=", &testParams->AllocBufferSize) ||
			GetParamIntValue(arg, "size=", &testParams->AllocBufferSize))
		{
			if (testParams->ReadLength == 4096)
				testParams->ReadLength = testParams->AllocBufferSize;
			if (testParams->WriteLength == 4096)
				testParams->WriteLength = testParams->AllocBufferSize;

		}
		else if (GetParamIntValue(arg, "readsize=", &testParams->ReadLength)) {}
		else if (GetParamIntValue(arg, "writesize=", &testParams->WriteLength)) {}
		else if (GetParamIntValue(arg, "timeout=", &testParams->Timeout)) {}
		else if (GetParamIntValue(arg, "intf=", &testParams->Intf)) {}
		else if (GetParamIntValue(arg, "altf=", &testParams->Altf)) {}
		else if (GetParamIntValue(arg, "ep=", &testParams->Ep))
		{
			testParams->Ep &= 0xf;
		}
		else if (GetParamIntValue(arg, "refresh=", &testParams->Refresh)) {}
		else if (GetParamIntValue(arg, "fixedisopackets=", &testParams->FixedIsoPackets)) {}
		else if ((value = GetParamStrValue(arg, "mode=")) != NULL)
		{
			if (GetParamStrValue(value, "sync"))
			{
				testParams->TransferMode = TRANSFER_MODE_SYNC;
			}
			else if (GetParamStrValue(value, "async"))
			{
				testParams->TransferMode = TRANSFER_MODE_ASYNC;
			}
			else
			{
				// Invalid EndpointType argument.
				CONERR("invalid transfer mode argument! %s\n", argv[iarg]);
				return -1;

			}
		}
		else if ((value = GetParamStrValue(arg, "priority=")) != NULL)
		{
			if (GetParamStrValue(value, "lowest"))
			{
				testParams->Priority = THREAD_PRIORITY_LOWEST;
			}
			else if (GetParamStrValue(value, "belownormal"))
			{
				testParams->Priority = THREAD_PRIORITY_BELOW_NORMAL;
			}
			else if (GetParamStrValue(value, "normal"))
			{
				testParams->Priority = THREAD_PRIORITY_NORMAL;
			}
			else if (GetParamStrValue(value, "abovenormal"))
			{
				testParams->Priority = THREAD_PRIORITY_ABOVE_NORMAL;
			}
			else if (GetParamStrValue(value, "highest"))
			{
				testParams->Priority = THREAD_PRIORITY_HIGHEST;
			}
			else
			{
				CONERR("invalid priority argument! %s\n", argv[iarg]);
				return -1;
			}
		}
		else if ((value = GetParamStrValue(arg, "rawio=")) != NULL)
		{
			if (GetParamStrValue(value, "t") ||
				GetParamStrValue(value, "y") ||
				GetParamStrValue(value, "1"))
			{
				testParams->UseRawIO = 1;
			}
			else
			{
				testParams->UseRawIO = 0;
			}
		}
		else if (!_stricmp(arg, "notestselect"))
		{
			testParams->NoTestSelect = TRUE;
		}
		else if (!_stricmp(arg, "logread"))
		{
			testParams->ReadLogEnabled = TRUE;
		}
		else if (!_stricmp(arg, "logwrite"))
		{
			testParams->WriteLogEnabled = TRUE;
		}
		else if (!_stricmp(arg, "log"))
		{
			testParams->ReadLogEnabled = TRUE;
			testParams->WriteLogEnabled = TRUE;
		}
		else if (!_stricmp(arg, "read"))
		{
			testParams->TestType = TestTypeRead;
		}
		else if (!_stricmp(arg, "write"))
		{
			testParams->TestType = TestTypeWrite;
		}
		else if (!_stricmp(arg, "loop"))
		{
			testParams->TestType = TestTypeLoop;
		}
		else if (!_stricmp(arg, "listonly"))
		{
			testParams->UseList = TRUE;
			testParams->ListDevicesOnly = TRUE;
		}
		else if (!_stricmp(arg, "list"))
		{
			testParams->UseList = TRUE;
		}
		else if (!_stricmp(arg, "verifydetails"))
		{
			testParams->VerifyDetails = TRUE;
			testParams->Verify = TRUE;
		}
		else if (!_stricmp(arg, "verify"))
		{
			testParams->Verify = TRUE;
		}
		else if (!_stricmp(arg, "composite"))
		{
			testParams->Use_UsbK_Init = TRUE;
		}
		else if (!_stricmp(arg, "isoasap"))
		{
			testParams->UseIsoAsap = TRUE;
		}
		else
		{
			CONERR("invalid argument! %s\n", argv[iarg]);
			return -1;
		}
	}
	return ValidateBenchmarkArgs(testParams);
}

INT CreateVerifyBuffer(PBENCHMARK_TEST_PARAM test, WORD endpointMaxPacketSize)
{
	int i;
	BYTE indexC = 0;
	test->VerifyBuffer = malloc(endpointMaxPacketSize);
	if (!test->VerifyBuffer)
	{
		CONERR("memory allocation failure at line %d!\n", __LINE__);
		return -1;
	}

	test->VerifyBufferSize = endpointMaxPacketSize;

	for (i = 0; i < endpointMaxPacketSize; i++)
	{
		test->VerifyBuffer[i] = indexC++;
		if (indexC == 0) indexC = 1;
	}

	return 0;
}

void FreeTransferParam(PBENCHMARK_TRANSFER_PARAM* testTransferRef)
{
	PBENCHMARK_TRANSFER_PARAM pTransferParam;
	INT i;
	if ((!testTransferRef) || !*testTransferRef) return;
	pTransferParam = *testTransferRef;

	if (pTransferParam->Test)
	{
		for (i = 0; i < pTransferParam->Test->BufferCount; i++)
		{
			if (pTransferParam->TransferHandles[i].IsochHandle)
			{
				IsochK_Free(pTransferParam->TransferHandles[i].IsochHandle);
			}
		}
	}
	if (pTransferParam->ThreadHandle)
	{
		CloseHandle(pTransferParam->ThreadHandle);
		pTransferParam->ThreadHandle = NULL;
	}

	free(pTransferParam);

	*testTransferRef = NULL;
}

PBENCHMARK_TRANSFER_PARAM CreateTransferParam(PBENCHMARK_TEST_PARAM test, int endpointID)
{
	PBENCHMARK_TRANSFER_PARAM transferParam = NULL;
	int pipeIndex, bufferIndex;
	int allocSize;

	PWINUSB_PIPE_INFORMATION_EX pipeInfo = NULL;

	/// Get Pipe Information
	for (pipeIndex = 0; pipeIndex < test->InterfaceDescriptor.bNumEndpoints; pipeIndex++)
	{
		if (!(endpointID & USB_ENDPOINT_ADDRESS_MASK))
		{
			// Use first endpoint that matches the direction
			if ((test->PipeInformation[pipeIndex].PipeId & USB_ENDPOINT_DIRECTION_MASK) == endpointID)
			{
				pipeInfo = &test->PipeInformation[pipeIndex];
				break;
			}
		}
		else
		{
			if ((int)test->PipeInformation[pipeIndex].PipeId == endpointID)
			{
				pipeInfo = &test->PipeInformation[pipeIndex];
				break;
			}
		}
	}

	if (!pipeInfo)
	{
		CONERR("failed locating EP%02Xh!\n", endpointID);
		goto Done;
	}

	if (!pipeInfo->MaximumPacketSize)
	{
		CONWRN("MaximumPacketSize=0 for EP%02Xh. check alternate settings.\n", pipeInfo->PipeId);
	}

	test->AllocBufferSize = max(test->AllocBufferSize, test->ReadLength);
	test->AllocBufferSize = max(test->AllocBufferSize, test->WriteLength);

	allocSize = sizeof(BENCHMARK_TRANSFER_PARAM) + (test->AllocBufferSize * test->BufferCount);
	transferParam = (PBENCHMARK_TRANSFER_PARAM)malloc(allocSize);

	if (transferParam)
	{
		UINT numIsoPackets;
		memset(transferParam, 0, allocSize);
		transferParam->Test = test;

		memcpy(&transferParam->Ep, pipeInfo, sizeof(transferParam->Ep));
		transferParam->HasEpCompanionDescriptor = K.GetSuperSpeedPipeCompanionDescriptor(test->InterfaceHandle, test->InterfaceDescriptor.bAlternateSetting, (UCHAR)pipeIndex, &transferParam->EpCompanionDescriptor);

		if (ENDPOINT_TYPE(transferParam) == USB_ENDPOINT_TYPE_ISOCHRONOUS)
		{
			transferParam->Test->TransferMode = TRANSFER_MODE_ASYNC;

			if (!transferParam->Ep.MaximumBytesPerInterval)
			{
				CONERR("Unable to determine 'MaximumBytesPerInterval' for isochornous pipe %02X\n", transferParam->Ep.PipeId);
				CONERR0("- Device firmware may be incorrectly configured.");
				FreeTransferParam(&transferParam);
				goto Done;
			}
			numIsoPackets = transferParam->Test->AllocBufferSize / transferParam->Ep.MaximumBytesPerInterval;
			transferParam->NumberOfIsoPackets = numIsoPackets;
			if (numIsoPackets == 0 || ((numIsoPackets % 8) && test->DeviceSpeed >= 3) || transferParam->Test->AllocBufferSize % transferParam->Ep.MaximumBytesPerInterval)
			{
				const UINT minBufferSize = transferParam->Ep.MaximumBytesPerInterval * 8;
				CONERR("Buffer size is not correct for isochornous pipe %02X\n", transferParam->Ep.PipeId);
				CONERR("- Buffer size must be an interval of %u\n", minBufferSize);
				FreeTransferParam(&transferParam);
				goto Done;
			}

			for (bufferIndex = 0; bufferIndex < transferParam->Test->BufferCount; bufferIndex++)
			{
				transferParam->TransferHandles[bufferIndex].Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

				// Data buffer(s) are located at the end of the transfer param.
				transferParam->TransferHandles[bufferIndex].Data = transferParam->Buffer + (bufferIndex * transferParam->Test->AllocBufferSize);

				if (!IsochK_Init(&transferParam->TransferHandles[bufferIndex].IsochHandle, test->InterfaceHandle, transferParam->Ep.PipeId, numIsoPackets, transferParam->TransferHandles[bufferIndex].Data, transferParam->Test->AllocBufferSize))
				{
					CONERR("IsochK_Init failed for isochornous pipe %02X\n", transferParam->Ep.PipeId);
					CONERR("- ErrorCode = %u\n", GetLastError());
					FreeTransferParam(&transferParam);
					goto Done;
				}

				if (!IsochK_SetPacketOffsets(transferParam->TransferHandles[bufferIndex].IsochHandle, transferParam->Ep.MaximumBytesPerInterval))
				{
					CONERR("IsochK_SetPacketOffsets failed for isochornous pipe %02X\n", transferParam->Ep.PipeId);
					CONERR("- ErrorCode = %u\n", GetLastError());
					FreeTransferParam(&transferParam);
					goto Done;
				}
			}
		}

		ResetRunningStatus(transferParam);

		transferParam->ThreadHandle = CreateThread(
			NULL,
			0,
			(LPTHREAD_START_ROUTINE)TransferThreadProc,
			transferParam,
			CREATE_SUSPENDED,
			&transferParam->ThreadID);

		if (!transferParam->ThreadHandle)
		{
			CONERR0("failed creating thread!\n");
			FreeTransferParam(&transferParam);
			goto Done;
		}

		// If verify mode is on, this is a loop test, and this is a write endpoint, fill
		// the buffers with the same test data sent by a benchmark device when running
		// a read only test.
		if (transferParam->Test->TestType == TestTypeLoop && USB_ENDPOINT_DIRECTION_OUT(pipeInfo->PipeId))
		{
			// Data Format:
			// [0][KeyByte] 2 3 4 5 ..to.. wMaxPacketSize (if data byte rolls it is incremented to 1)
			// Increment KeyByte and repeat
			//
			BYTE indexC = 0;
			INT bufferIndex = 0;
			WORD dataIndex;
			INT packetIndex;
			INT packetCount = ((transferParam->Test->BufferCount * test->ReadLength) / pipeInfo->MaximumPacketSize);
			for (packetIndex = 0; packetIndex < packetCount; packetIndex++)
			{
				indexC = 2;
				for (dataIndex = 0; dataIndex < pipeInfo->MaximumPacketSize; dataIndex++)
				{
					if (dataIndex == 0)			// Start
						transferParam->Buffer[bufferIndex] = 0;
					else if (dataIndex == 1)	// Key
						transferParam->Buffer[bufferIndex] = packetIndex & 0xFF;
					else						// Data
						transferParam->Buffer[bufferIndex] = indexC++;

					// if wMaxPacketSize is > 255, indexC resets to 1.
					if (indexC == 0) indexC = 1;

					bufferIndex++;
				}
			}
		}
	}

Done:
	if (!transferParam)
		CONERR0("failed creating transfer param!\n");

	return transferParam;
}

void GetAverageBytesSec(PBENCHMARK_TRANSFER_PARAM transferParam, DOUBLE* bps)
{
	DOUBLE ticksSec;
	if ((!transferParam->StartTick) ||
		(transferParam->StartTick >= transferParam->LastTick) ||
		transferParam->TotalTransferred == 0)
	{
		*bps = 0;
	}
	else
	{
		ticksSec = (transferParam->LastTick - transferParam->StartTick) / 1000.0;
		*bps = (transferParam->TotalTransferred / ticksSec);
	}
}

void GetCurrentBytesSec(PBENCHMARK_TRANSFER_PARAM transferParam, DOUBLE* bps)
{
	DOUBLE ticksSec;
	if ((!transferParam->StartTick) ||
		(!transferParam->LastStartTick) ||
		(transferParam->LastTick <= transferParam->LastStartTick) ||
		transferParam->LastTransferred == 0)
	{
		*bps = 0;
	}
	else
	{
		ticksSec = (transferParam->LastTick - transferParam->LastStartTick) / 1000.0;
		*bps = transferParam->LastTransferred / ticksSec;
	}
}

void ShowRunningStatus(PBENCHMARK_TRANSFER_PARAM readParam, PBENCHMARK_TRANSFER_PARAM writeParam)
{
	static BENCHMARK_TRANSFER_PARAM gReadParamTransferParam, gWriteParamTransferParam;
	DOUBLE bpsReadOverall = 0;
	DOUBLE bpsReadLastTransfer = 0;
	DOUBLE bpsWriteOverall = 0;
	DOUBLE bpsWriteLastTransfer = 0;
	UINT zlp = 0;
	UINT totalPackets = 0;
	UINT totalIsoPackets = 0;
	UINT goodIsoPackets = 0;
	UINT badIsoPackets = 0;

	// LOCK the display critical section
	EnterCriticalSection(&DisplayCriticalSection);

	if (readParam)
		memcpy(&gReadParamTransferParam, readParam, sizeof(BENCHMARK_TRANSFER_PARAM));

	if (writeParam)
		memcpy(&gWriteParamTransferParam, writeParam, sizeof(BENCHMARK_TRANSFER_PARAM));

	// UNLOCK the display critical section
	LeaveCriticalSection(&DisplayCriticalSection);

	if (readParam != NULL && (!gReadParamTransferParam.StartTick || gReadParamTransferParam.StartTick >= gReadParamTransferParam.LastTick))
	{
		CONMSG("Synchronizing Read %d..\n", abs(gReadParamTransferParam.Packets));
	}
	if (writeParam != NULL && (!gWriteParamTransferParam.StartTick || gWriteParamTransferParam.StartTick >= gWriteParamTransferParam.LastTick))
	{
		CONMSG("Synchronizing Write %d..\n", abs(gWriteParamTransferParam.Packets));
	}
	else
	{
		if (readParam)
		{
			GetAverageBytesSec(&gReadParamTransferParam, &bpsReadOverall);
			GetCurrentBytesSec(&gReadParamTransferParam, &bpsReadLastTransfer);
			if (gReadParamTransferParam.LastTransferred == 0) zlp++;
			readParam->LastStartTick = 0;
			totalPackets += gReadParamTransferParam.Packets;
			totalIsoPackets += gReadParamTransferParam.IsochResults.TotalPackets;
			goodIsoPackets += gReadParamTransferParam.IsochResults.GoodPackets;
			badIsoPackets += gReadParamTransferParam.IsochResults.BadPackets;

		}
		if (writeParam)
		{
			GetAverageBytesSec(&gWriteParamTransferParam, &bpsWriteOverall);
			GetCurrentBytesSec(&gWriteParamTransferParam, &bpsWriteLastTransfer);
			if (gWriteParamTransferParam.LastTransferred == 0) zlp++;
			writeParam->LastStartTick = 0;
			totalPackets += gWriteParamTransferParam.Packets;
			totalIsoPackets += gWriteParamTransferParam.IsochResults.TotalPackets;
			goodIsoPackets += gWriteParamTransferParam.IsochResults.GoodPackets;
			badIsoPackets += gWriteParamTransferParam.IsochResults.BadPackets;
		}

		if (totalIsoPackets)
		{
			CONMSG("Avg. Bytes/s: %.2f Transfers: %d Bytes/s: %.2f ISO-Packets (Total/Good/Bad):%u/%u/%u\n",
				bpsReadOverall + bpsWriteOverall, totalPackets, bpsReadLastTransfer + bpsWriteLastTransfer, totalIsoPackets, goodIsoPackets, badIsoPackets);
		}
		else
		{
			if (zlp)
			{
				CONMSG("Avg. Bytes/s: %.2f Transfers: %d %u Zero-length-transfer(s)\n",
					bpsReadOverall + bpsWriteOverall, totalPackets, zlp);
			}
			else
			{
				CONMSG("Avg. Bytes/s: %.2f Transfers: %d Bytes/s: %.2f\n",
					bpsReadOverall + bpsWriteOverall, totalPackets, bpsReadLastTransfer + bpsWriteLastTransfer);
			}
		}
	}

}
void ShowTransferInfo(PBENCHMARK_TRANSFER_PARAM transferParam)
{
	DOUBLE bpsAverage;
	DOUBLE bpsCurrent;
	DOUBLE elapsedSeconds;

	if (!transferParam) return;

	if (transferParam->HasEpCompanionDescriptor)
	{
		if (transferParam->EpCompanionDescriptor.wBytesPerInterval)
		{
			if (transferParam->Ep.PipeType == UsbdPipeTypeIsochronous)
			{
				CONMSG("%s %s (Ep%02Xh) Maximum Bytes Per Interval:%lu Max Bursts:%u Multi:%u\n",
					EndpointTypeDisplayString[ENDPOINT_TYPE(transferParam)],
					TRANSFER_DISPLAY(transferParam, "Read", "Write"),
					transferParam->Ep.PipeId,
					transferParam->Ep.MaximumBytesPerInterval,
					transferParam->EpCompanionDescriptor.bMaxBurst+1,
					transferParam->EpCompanionDescriptor.bmAttributes.Isochronous.Mult+1);

			}
			else if (transferParam->Ep.PipeType == UsbdPipeTypeBulk)
			{
				CONMSG("%s %s (Ep%02Xh) Maximum Bytes Per Interval:%lu Max Bursts:%u Max Streams:%u\n",
					EndpointTypeDisplayString[ENDPOINT_TYPE(transferParam)],
					TRANSFER_DISPLAY(transferParam, "Read", "Write"),
					transferParam->Ep.PipeId,
					transferParam->Ep.MaximumBytesPerInterval,
					transferParam->EpCompanionDescriptor.bMaxBurst + 1,
					transferParam->EpCompanionDescriptor.bmAttributes.Bulk.MaxStreams + 1);

			}
			else
			{
				CONMSG("%s %s (Ep%02Xh) Maximum Bytes Per Interval:%lu\n",
					EndpointTypeDisplayString[ENDPOINT_TYPE(transferParam)],
					TRANSFER_DISPLAY(transferParam, "Read", "Write"),
					transferParam->Ep.PipeId,
					transferParam->Ep.MaximumBytesPerInterval);

			}
		}
		else
		{
			CONMSG("%s %s (Ep%02Xh) Maximum Packet Size:%d\n",
				EndpointTypeDisplayString[ENDPOINT_TYPE(transferParam)],
				TRANSFER_DISPLAY(transferParam, "Read", "Write"),
				transferParam->Ep.PipeId,
				transferParam->Ep.MaximumPacketSize);
		}
	}
	else
	{
		CONMSG("%s %s (Ep%02Xh) Maximum Packet Size: %d\n",
			EndpointTypeDisplayString[ENDPOINT_TYPE(transferParam)],
			TRANSFER_DISPLAY(transferParam, "Read", "Write"),
			transferParam->Ep.PipeId,
			transferParam->Ep.MaximumPacketSize);
	}
	if (transferParam->StartTick)
	{
		GetAverageBytesSec(transferParam, &bpsAverage);
		GetCurrentBytesSec(transferParam, &bpsCurrent);
		CONMSG("\tTotal Bytes     : %I64d\n", transferParam->TotalTransferred);
		CONMSG("\tTotal Transfers : %d\n", transferParam->Packets);

		if (transferParam->ShortTransferCount)
		{
			CONMSG("\tShort Transfers : %d\n", transferParam->ShortTransferCount);
		}
		if (transferParam->TotalTimeoutCount)
		{
			CONMSG("\tTimeout Errors  : %d\n", transferParam->TotalTimeoutCount);
		}
		if (transferParam->TotalErrorCount)
		{
			CONMSG("\tOther Errors    : %d\n", transferParam->TotalErrorCount);
		}

		CONMSG("\tAvg. Bytes/sec  : %.2f\n", bpsAverage);

		if (transferParam->StartTick && transferParam->StartTick < transferParam->LastTick)
		{
			elapsedSeconds = (transferParam->LastTick - transferParam->StartTick) / 1000.0;

			CONMSG("\tElapsed Time    : %.2f seconds\n", elapsedSeconds);
		}

		CONMSG0("\n");
	}

}

void ShowTestInfo(PBENCHMARK_TEST_PARAM test)
{
	if (!test) return;

	CONMSG("%s Test Information\n", TestDisplayString[test->TestType & 3]);
	CONMSG("\tDriver          : %s\n", GetDrvIdString(test->SelectedDeviceProfile->DriverID));
	CONMSG("\tVid / Pid       : %04Xh / %04Xh\n", test->DeviceDescriptor.idVendor, test->DeviceDescriptor.idProduct);
	CONMSG("\tDevicePath      : %s\n", test->SelectedDeviceProfile->DevicePath);
	CONMSG("\tDevice Speed    : %s\n", GetDevSpeedString(test->DeviceSpeed));
	CONMSG("\tInterface #     : %02Xh\n", test->InterfaceDescriptor.bInterfaceNumber);
	CONMSG("\tAlt Interface # : %02Xh\n", test->InterfaceDescriptor.bAlternateSetting);
	CONMSG("\tNum Endpoints   : %u\n", test->InterfaceDescriptor.bNumEndpoints);
	CONMSG("\tPriority        : %d\n", test->Priority);
	CONMSG("\tRead Size       : %d\n", test->ReadLength);
	CONMSG("\tWrite Size      : %d\n", test->WriteLength);
	CONMSG("\tBuffer Count    : %d\n", test->BufferCount);
	CONMSG("\tDisplay Refresh : %d (ms)\n", test->Refresh);
	CONMSG("\tTransfer Timeout: %d (ms)\n", test->Timeout);
	CONMSG("\tRetry Count     : %d\n", test->Retry);
	CONMSG("\tVerify Data     : %s%s\n",
		test->Verify ? "On" : "Off",
		(test->Verify && test->VerifyDetails) ? " (Detailed)" : "");

	CONMSG0("\n");
}

BOOL WaitForTestTransfer(PBENCHMARK_TRANSFER_PARAM transferParam, UINT msToWait)
{
	DWORD exitCode;

	while (transferParam)
	{
		if (!transferParam->IsRunning)
		{
			if (GetExitCodeThread(transferParam->ThreadHandle, &exitCode))
			{
				CONMSG("stopped Ep%02Xh thread.\tExitCode=%d\n",
					transferParam->Ep.PipeId, exitCode);
				break;


			}

			CONERR("failed getting Ep%02Xh thread exit code!\n", transferParam->Ep.PipeId);
			break;
		}

		CONMSG("waiting for Ep%02Xh thread..\n", transferParam->Ep.PipeId);
		WaitForSingleObject(transferParam->ThreadHandle, 100);
		if (msToWait != INFINITE)
		{
			if ((msToWait - 100) == 0 || (msToWait - 100) > msToWait)
				return FALSE;
		}
	}

	return TRUE;
}
void ResetRunningStatus(PBENCHMARK_TRANSFER_PARAM transferParam)
{
	if (!transferParam) return;

	transferParam->StartTick = 0;
	transferParam->TotalTransferred = 0;
	transferParam->Packets = -2;
	transferParam->LastTick = 0;
	transferParam->RunningTimeoutCount = 0;
}

int GetTestDeviceFromArgs(PBENCHMARK_TEST_PARAM test)
{
	CHAR id[MAX_PATH];
	KLST_DEVINFO_HANDLE deviceInfo = NULL;

	LstK_MoveReset(test->DeviceList);

	while (LstK_MoveNext(test->DeviceList, &deviceInfo))
	{
		int vid = -1;
		int pid = -1;
		int mi = -1;
		PCHAR chID;

		// disabled
		LibK_SetContext(deviceInfo, KLIB_HANDLE_TYPE_LSTINFOK, (KLIB_USER_CONTEXT)FALSE);

		memset(id, 0, sizeof(id));
		strcpy_s(id, MAX_PATH - 1, deviceInfo->DeviceID);
		_strlwr_s(id, MAX_PATH);

		if ((chID = strstr(id, "vid_")) != NULL)
			sscanf_s(chID, "vid_%04x", &vid);
		if ((chID = strstr(id, "pid_")) != NULL)
			sscanf_s(chID, "pid_%04x", &pid);
		if ((chID = strstr(id, "mi_")) != NULL)
			sscanf_s(chID, "mi_%02x", &mi);

		if (test->Vid == vid && test->Pid == pid)
		{
			// enabled
			LibK_SetContext(deviceInfo, KLIB_HANDLE_TYPE_LSTINFOK, (KLIB_USER_CONTEXT)TRUE);
		}
	}

	return ERROR_SUCCESS;
}

int GetTestDeviceFromList(PBENCHMARK_TEST_PARAM test)
{
	UCHAR selection;
	UCHAR count = 0;
	KLST_DEVINFO_HANDLE deviceInfo = NULL;

	LstK_MoveReset(test->DeviceList);

	if (test->ListDevicesOnly)
	{
		while (LstK_MoveNext(test->DeviceList, &deviceInfo))
		{
			count++;
			CONMSG("%02u. %s (%s) [%s]\n", count, deviceInfo->DeviceDesc, deviceInfo->DeviceID, GetDrvIdString(deviceInfo->DriverID));
		}

		return ERROR_SUCCESS;
	}
	else
	{
		while (LstK_MoveNext(test->DeviceList, &deviceInfo) && count < 9)
		{
			CONMSG("%u. %s (%s) [%s]\n", count + 1, deviceInfo->DeviceDesc, deviceInfo->DeviceID, GetDrvIdString(deviceInfo->DriverID));
			count++;

			// enabled
			LibK_SetContext(deviceInfo, KLIB_HANDLE_TYPE_LSTINFOK, (KLIB_USER_CONTEXT)TRUE);

		}
		if (!count)
		{
			CONERR("%04Xh:%04Xh device not found\n", test->Vid, test->Pid);
			return -1;
		}

		CONMSG("Select device (1-%u) :", count);
		while (_kbhit()) _getch();

		selection = (CHAR)_getche();
		selection -= (UCHAR)'0';
		CONMSG0("\n\n");

		if (selection > 0 && selection <= count)
		{
			count = 0;
			while (LstK_MoveNext(test->DeviceList, &deviceInfo) && ++count != selection)
			{
				// disabled
				LibK_SetContext(deviceInfo, KLIB_HANDLE_TYPE_LSTINFOK, (KLIB_USER_CONTEXT)FALSE);

			}

			if (!deviceInfo)
			{
				CONERR("unknown selection\n");
				return -1;
			}

			return ERROR_SUCCESS;
		}
	}

	return -1;
}

int __cdecl main(int argc, char** argv)
{
	BENCHMARK_TEST_PARAM Test;
	PBENCHMARK_TRANSFER_PARAM ReadTest = NULL;
	PBENCHMARK_TRANSFER_PARAM WriteTest = NULL;
	int key;
	LONG ec;
	UINT count, length;
	UCHAR bIsoAsap;


	if (argc == 1)
	{
		ShowHelp();
		return -1;
	}

	SetTestDefaults(&Test);

	// Load the command line arguments.
	if (ParseBenchmarkArgs(&Test, argc, argv) < 0)
		return -1;

	// Initialize the critical section used for locking
	// the volatile members of the transfer params in order
	// to update/modify the running statistics.
	//
	InitializeCriticalSection(&DisplayCriticalSection);

	if (!LstK_Init(&Test.DeviceList, 0))
	{
		ec = GetLastError();
		CONERR("failed getting device list ec=%08Xh\n", ec);
		goto Done;
	}

	count = 0;
	LstK_Count(Test.DeviceList, &count);
	if (!count)
	{
		CONERR("device list empty.\n");
		goto Done;
	}

	if (Test.ListDevicesOnly)
	{
		CONMSG("CurrentProcessId=%u Count=%d\n", GetCurrentProcessId(), count);
	}
	else
	{
		CONMSG("device-count=%u\n", count);
	}

	if (Test.UseList)
	{
		if (GetTestDeviceFromList(&Test) < 0)
			goto Done;
	}
	else
	{
		if (GetTestDeviceFromArgs(&Test) < 0)
			goto Done;
	}

	if (Test.ListDevicesOnly)
		goto Done;

	if (!Bench_Open(&Test))
		goto Done;

	CONMSG("opened %s (%s)..\n", Test.SelectedDeviceProfile->DeviceDesc, Test.SelectedDeviceProfile->DeviceID);

	// If "NoTestSelect" appears in the command line then don't send the control
	// messages for selecting the test type.
	//
	if (!Test.NoTestSelect)
	{
		if (Bench_Configure(Test.InterfaceHandle, SET_TEST, (UCHAR)Test.Intf, &Test.TestType) != TRUE)
		{
			CONERR("setting bechmark test type #%d!\n\n", Test.TestType);
			goto Done;
		}
	}

	// Get the device speed.
	length = sizeof(Test.DeviceSpeed);
	K.QueryDeviceInformation(Test.InterfaceHandle, DEVICE_SPEED, &length, (PUCHAR)&Test.DeviceSpeed);

	// If reading from the device create the read transfer param. This will also create
	// a thread in a suspended state.
	//
	if (Test.TestType & TestTypeRead)
	{
		ReadTest = CreateTransferParam(&Test, Test.Ep | USB_ENDPOINT_DIRECTION_MASK);
		if (!ReadTest) goto Done;
		if (Test.UseRawIO != 0xFF)
		{
			if (!K.SetPipePolicy(Test.InterfaceHandle, ReadTest->Ep.PipeId, RAW_IO, 1, &Test.UseRawIO))
			{
				CONERR("SetPipePolicy:RAW_IO failed. ErrorCode=%08Xh\n", GetLastError());
				goto Done;
			}
		}
		//K.ResetPipe(ReadTest->Test->InterfaceHandle, ReadTest->Ep.PipeId);
	}

	// If writing to the device create the write transfer param. This will also create
	// a thread in a suspended state.
	//
	if (Test.TestType & TestTypeWrite)
	{
		WriteTest = CreateTransferParam(&Test, Test.Ep);
		if (!WriteTest) goto Done;
		if (Test.FixedIsoPackets)
		{
			if (!K.SetPipePolicy(Test.InterfaceHandle, WriteTest->Ep.PipeId, ISO_NUM_FIXED_PACKETS, 2, &Test.FixedIsoPackets))
			{
				CONERR("SetPipePolicy:ISO_NUM_FIXED_PACKETS failed. ErrorCode=%08Xh\n", GetLastError());
				goto Done;
			}
		}
		if (Test.UseRawIO != 0xFF)
		{
			if (!K.SetPipePolicy(Test.InterfaceHandle, WriteTest->Ep.PipeId, RAW_IO, 1, &Test.UseRawIO))
			{
				CONERR("SetPipePolicy:RAW_IO failed. ErrorCode=%08Xh\n", GetLastError());
				goto Done;
			}
		}

		//K.ResetPipe(WriteTest->Test->InterfaceHandle, WriteTest->Ep.PipeId);

	}

	if (Test.Verify)
	{
		if (ReadTest && WriteTest)
		{
			if (CreateVerifyBuffer(&Test, WriteTest->Ep.MaximumPacketSize) < 0)
				goto Done;
		}
		else if (ReadTest)
		{
			if (CreateVerifyBuffer(&Test, ReadTest->Ep.MaximumPacketSize) < 0)
				goto Done;
		}
	}

	if (Test.ReadLogEnabled)
		Test.ReadLogFile = fopen("read.log", "wb");

	if (Test.WriteLogEnabled)
		Test.WriteLogFile = fopen("write.log", "wb");


	ShowTestInfo(&Test);
	ShowTransferInfo(ReadTest);
	ShowTransferInfo(WriteTest);

	CONMSG0("\nWhile the test is running:\n");
	CONMSG0("Press 'Q' to quit\n");
	CONMSG0("Press 'T' for test details\n");
	CONMSG0("Press 'I' for status information\n");
	CONMSG0("Press 'R' to reset averages\n");
	CONMSG0("\nPress 'Q' to exit, any other key to begin..");
	key = _getch();
	CONMSG0("\n");

	if (key == 'Q' || key == 'q') goto Done;

	if ((WriteTest && WriteTest->Ep.PipeType == UsbdPipeTypeIsochronous) || (ReadTest && ReadTest->Ep.PipeType == UsbdPipeTypeIsochronous))
	{
		UINT frameNumber;
		if (!K.GetCurrentFrameNumber(Test.InterfaceHandle, &frameNumber))
		{
			CONERR("GetCurrentFrameNumber Failed. ErrorCode=%u", GetLastError());
			goto Done;
		}
		frameNumber += Test.BufferCount * 2;
		if (WriteTest)
		{
			WriteTest->FrameNumber = frameNumber;
			frameNumber++;
		}
		if (ReadTest)
		{
			ReadTest->FrameNumber = frameNumber;
			frameNumber++;
		}

	}

	bIsoAsap = (UCHAR)Test.UseIsoAsap;
	if (ReadTest) K.SetPipePolicy(Test.InterfaceHandle, ReadTest->Ep.PipeId, ISO_ALWAYS_START_ASAP, 1, &bIsoAsap);
	if (WriteTest) K.SetPipePolicy(Test.InterfaceHandle, WriteTest->Ep.PipeId, ISO_ALWAYS_START_ASAP, 1, &bIsoAsap);

	// Set the thread priority and start it.
	if (ReadTest)
	{
		SetThreadPriority(ReadTest->ThreadHandle, Test.Priority);
		ResumeThread(ReadTest->ThreadHandle);
	}

	// Set the thread priority and start it.
	if (WriteTest)
	{
		SetThreadPriority(WriteTest->ThreadHandle, Test.Priority);
		ResumeThread(WriteTest->ThreadHandle);
	}

	while (!Test.IsCancelled)
	{
		Sleep(Test.Refresh);

		if (_kbhit())
		{
			// A key was pressed.
			key = _getch();
			switch (key)
			{
			case 'Q':
			case 'q':
				Test.IsUserAborted = TRUE;
				Test.IsCancelled = TRUE;
				break;
			case 'T':
			case 't':
				ShowTestInfo(&Test);
				break;
			case 'I':
			case 'i':
				// LOCK the display critical section
				EnterCriticalSection(&DisplayCriticalSection);

				// Print benchmark test details.
				ShowTransferInfo(ReadTest);
				ShowTransferInfo(WriteTest);


				// UNLOCK the display critical section
				LeaveCriticalSection(&DisplayCriticalSection);
				break;

			case 'R':
			case 'r':
				// LOCK the display critical section
				EnterCriticalSection(&DisplayCriticalSection);

				// Reset the running status.
				ResetRunningStatus(ReadTest);
				ResetRunningStatus(WriteTest);

				// UNLOCK the display critical section
				LeaveCriticalSection(&DisplayCriticalSection);
				break;
			}

			// Only one key at a time.
			while (_kbhit()) _getch();
		}

		// If the read test should be running and it isn't, cancel the test.
		if ((ReadTest) && !ReadTest->IsRunning)
		{
			Test.IsCancelled = TRUE;
			break;
		}

		// If the write test should be running and it isn't, cancel the test.
		if ((WriteTest) && !WriteTest->IsRunning)
		{
			Test.IsCancelled = TRUE;
			break;
		}

		// Print benchmark stats
		ShowRunningStatus(ReadTest, WriteTest);

	}

	// Wait for the transfer threads to complete gracefully if it
	// can be done in 1s. All of the code from this point to
	// WaitForTestTransfer() is not required.  It is here only to
	// improve response time when the test is cancelled.
	//
	WaitForTestTransfer(ReadTest, 1000);
	if ((ReadTest) && ReadTest->IsRunning)
	{
		// If the thread is still running, abort the endpoint.
		CONWRN("Aborting Read Pipe 0x%02X..", ReadTest->Ep.PipeId);
		K.AbortPipe(Test.InterfaceHandle, ReadTest->Ep.PipeId);
	}
	
	WaitForTestTransfer(WriteTest, 1000);
	if ((WriteTest) && WriteTest->IsRunning)
	{
		// If the thread is still running, abort the endpoint.
		CONWRN("Aborting Write Pipe 0x%02X..", WriteTest->Ep.PipeId);
		K.AbortPipe(Test.InterfaceHandle, WriteTest->Ep.PipeId);
	}

	// WaitForTestTransfer will not return until the thread
	// has exited.
	if ((ReadTest) && ReadTest->IsRunning) 
		WaitForTestTransfer(ReadTest, INFINITE);
	if ((WriteTest) && WriteTest->IsRunning) 
		WaitForTestTransfer(WriteTest, INFINITE);

	
	// Print benchmark detailed stats
	ShowTestInfo(&Test);
	if (ReadTest) ShowTransferInfo(ReadTest);
	if (WriteTest) ShowTransferInfo(WriteTest);


Done:
	if (Test.InterfaceHandle)
	{
		// before the device is closed, set the altsetting back to default
		// NOTE: Is very good practice to declare an empty (no endpoints) alt interface setting in your
		//       firmware as the first alt setting. This gives you a way to free up resources in the
		//       firmware and driver when your done with it. (put device in low power mode, disable
		//       endpoints, etc..)
		K.SetAltInterface(Test.InterfaceHandle, Test.InterfaceDescriptor.bInterfaceNumber, FALSE, Test.DefaultAltSetting);

		// close and free resources
		K.Free(Test.InterfaceHandle);
		
		Test.InterfaceHandle = NULL;
	}
	if (!Test.Use_UsbK_Init)
	{
		if (Test.DeviceHandle)
		{
			CloseHandle(Test.DeviceHandle);
			Test.DeviceHandle = NULL;
		}
	}
	if (Test.VerifyBuffer)
	{
		PBENCHMARK_BUFFER verifyBuffer, verifyListTmp;

		free(Test.VerifyBuffer);
		Test.VerifyBuffer = NULL;

		DL_FOREACH_SAFE(Test.VerifyList, verifyBuffer, verifyListTmp)
		{
			DL_DELETE(Test.VerifyList, verifyBuffer);
			free(verifyBuffer);
		}
	}

	if (Test.ReadLogFile)
	{
		fflush(Test.ReadLogFile);
		fclose(Test.ReadLogFile);
		Test.ReadLogFile = NULL;
	}

	if (Test.WriteLogFile)
	{
		fflush(Test.WriteLogFile);
		fclose(Test.WriteLogFile);
		Test.WriteLogFile = NULL;
	}

	LstK_Free(Test.DeviceList);
	FreeTransferParam(&ReadTest);
	FreeTransferParam(&WriteTest);

	DeleteCriticalSection(&DisplayCriticalSection);

	if (!Test.ListDevicesOnly)
	{
		CONMSG0("Press any key to exit..");
		_getch();
		CONMSG0("\n");
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////////
/* END OF PROGRAM                                                           */
//////////////////////////////////////////////////////////////////////////////
void ShowHelp(void)
{
#define ID_HELP_TEXT  10020

#define ID_DOS_TEXT   300


	CONST CHAR* src;
	DWORD src_count, charsWritten;
	HGLOBAL res_data;
	HANDLE handle;
	HRSRC hSrc;

	ShowCopyright();

	hSrc = FindResourceA(NULL, MAKEINTRESOURCEA(ID_HELP_TEXT), MAKEINTRESOURCEA(ID_DOS_TEXT));
	if (!hSrc)	return;

	src_count = SizeofResource(NULL, hSrc);

	res_data = LoadResource(NULL, hSrc);
	if (!res_data)	return;

	src = (char*)LockResource(res_data);
	if (!src) return;

	if ((handle = GetStdHandle(STD_ERROR_HANDLE)) != INVALID_HANDLE_VALUE)
		WriteConsoleA(handle, src, src_count, &charsWritten, NULL);
}

void ShowCopyright(void)
{
	CONMSG("%s v%s (%s)\n",
		RC_FILENAME_STR,
		RC_VERSION_STR,
		DEFINE_TO_STR(VERSION_DATE));
	CONMSG0("Copyright (c) 2012 Travis Lee Robinson. <libusbdotnet@gmail.com>\n");
}