os/kernelhwsrv/kerneltest/e32test/win32/usbrflct/usbrflct.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\win32\usbrflct\usbrflct.cpp
sl@0
    15
// Win32 USB test program USBRFLCT: performs I/O with a device running the
sl@0
    16
// Symbian OS test program T_USB, using the generic USB device driver USBIO.
sl@0
    17
// === Includes ===
sl@0
    18
// 
sl@0
    19
//
sl@0
    20
sl@0
    21
#include <windows.h>
sl@0
    22
#include <stdio.h>
sl@0
    23
#include <iostream.h>
sl@0
    24
#include <time.h>
sl@0
    25
sl@0
    26
#include "usbio.h"											// USBIO Dev Kit
sl@0
    27
#include "usbiopipe.h"										// ditto
sl@0
    28
sl@0
    29
sl@0
    30
// === Global Defines ===
sl@0
    31
sl@0
    32
// The version of this program
sl@0
    33
#define VERSION_MAJOR  1
sl@0
    34
#ifdef VERSION_MINOR
sl@0
    35
# undef VERSION_MINOR						  // VERSION_MINOR sometimes yields funny compiler warning (c4005)
sl@0
    36
#endif
sl@0
    37
#define VERSION_MINOR  6
sl@0
    38
#ifdef VERSION_MICRO
sl@0
    39
# undef VERSION_MICRO						  // ditto
sl@0
    40
#endif
sl@0
    41
#define VERSION_MICRO  0
sl@0
    42
sl@0
    43
// The version of the USBIO driver this program is compiled against
sl@0
    44
#define USBIO_VERSION_MAJOR  2
sl@0
    45
#define USBIO_VERSION_MINOR  41
sl@0
    46
sl@0
    47
#define MAX_DESCRIPTOR_BUFFER_SIZE 2047
sl@0
    48
sl@0
    49
// === Global Vars ===
sl@0
    50
sl@0
    51
// Our own private GUID (also used in the .inf file as 'DriverUserInterfaceGuid')
sl@0
    52
// {55606403-E62D-4707-9F56-40D48C6736D0}
sl@0
    53
static const GUID g_UsbioID =
sl@0
    54
	{0x55606403, 0xe62d, 0x4707, {0x9f, 0x56, 0x40, 0xd4, 0x8c, 0x67, 0x36, 0xd0}};
sl@0
    55
sl@0
    56
// Handle for USB device list
sl@0
    57
static HDEVINFO g_DevList = NULL;
sl@0
    58
sl@0
    59
// USBIO supported device
sl@0
    60
static CUsbIo g_UsbDev;
sl@0
    61
static CUsbIoPipe g_BulkOutPipe;
sl@0
    62
static CUsbIoPipe g_BulkInPipe;
sl@0
    63
sl@0
    64
// 2 Bulk endpoints
sl@0
    65
static UCHAR g_ucBulkOutEndpoint = 0;
sl@0
    66
static UCHAR g_ucBulkInEndpoint = 0;
sl@0
    67
sl@0
    68
// Read/write buffer
sl@0
    69
static const DWORD KBufferSize = 1024 * 1024;
sl@0
    70
static const DWORD KPreambleLength = 8;
sl@0
    71
static BYTE Data[KBufferSize];								// the read/write buffer
sl@0
    72
static DWORD Length;										// Length of a transfer
sl@0
    73
static time_t T_0;											// the starting time
sl@0
    74
sl@0
    75
static CUsbIoBuf g_Buf((VOID*) Data, KBufferSize);		// the data buffer
sl@0
    76
static CUsbIoBuf g_ZlpBuf (NULL, 0);					// the data buffer
sl@0
    77
sl@0
    78
static DWORD dwRC = USBIO_ERR_SUCCESS;						// global error indicator
sl@0
    79
sl@0
    80
enum
sl@0
    81
	{
sl@0
    82
	ELoop,
sl@0
    83
	ELoopDebug,
sl@0
    84
	EReceiveOnly,
sl@0
    85
	ETransmitOnly
sl@0
    86
	};
sl@0
    87
sl@0
    88
static int TransferMode = ELoop;
sl@0
    89
static bool VerboseMode = false;
sl@0
    90
static bool ZlpMode = false;
sl@0
    91
sl@0
    92
static unsigned int maxOutPacketSize = 0;
sl@0
    93
sl@0
    94
// The version of T_USB we require (at least)
sl@0
    95
static const DWORD KTusbVersion = 20070524;
sl@0
    96
sl@0
    97
// After how many iterations to update the CRT:
sl@0
    98
static const int KLoopModeDisplayUpdate = 1024;
sl@0
    99
// After how many iterations (= MBytes) to update the CRT:
sl@0
   100
static const int KUniModeDisplayUpdate = 10;
sl@0
   101
sl@0
   102
// Helper #defines
sl@0
   103
sl@0
   104
#define PRINT_IF_VERBOSE(string) \
sl@0
   105
		do { \
sl@0
   106
		if (VerboseMode) \
sl@0
   107
			{ \
sl@0
   108
			printf(string); \
sl@0
   109
			} \
sl@0
   110
		} while (0)
sl@0
   111
sl@0
   112
#define PRINT_IF_VERBOSE1(string, a) \
sl@0
   113
		do { \
sl@0
   114
		if (VerboseMode) \
sl@0
   115
			{ \
sl@0
   116
			printf(string, a); \
sl@0
   117
			} \
sl@0
   118
		} while (0)
sl@0
   119
sl@0
   120
sl@0
   121
// === Functions ===
sl@0
   122
sl@0
   123
//
sl@0
   124
// Process the command line arguments, printing a helpful message
sl@0
   125
// if none are supplied.
sl@0
   126
//
sl@0
   127
static int ProcessCmdLine(int argc, char* argv[])
sl@0
   128
	{
sl@0
   129
	char help_text[] =
sl@0
   130
		"* Syntax: usbrflct [options]\n"
sl@0
   131
		"* Options:\n"
sl@0
   132
		"* /[r|t]    receive|transmit only; default is to loop\n"
sl@0
   133
		"* /z        zero length packet at end of a transmission\n"
sl@0
   134
		"*	    (only if last packet is full length)\n"
sl@0
   135
		"* /l        loop mode with stats printed for every iteration\n"
sl@0
   136
		"* /v        verbose driver & program output\n"
sl@0
   137
		"* /[h|?]    displays this help text\n"
sl@0
   138
		"\n";
sl@0
   139
sl@0
   140
	for (int i = 1; i < argc; i++)
sl@0
   141
		{
sl@0
   142
		strupr(argv[i]);
sl@0
   143
		if ((argv[i][0] == '-') || (argv[i][0] == '/'))
sl@0
   144
			{
sl@0
   145
			switch (argv[i][1])
sl@0
   146
				{
sl@0
   147
			case 'R':
sl@0
   148
			case 'r':
sl@0
   149
				TransferMode = EReceiveOnly;
sl@0
   150
				break;
sl@0
   151
			case 'T':
sl@0
   152
			case 't':
sl@0
   153
				TransferMode = ETransmitOnly;
sl@0
   154
				break;
sl@0
   155
			case 'L':
sl@0
   156
			case 'l':
sl@0
   157
				TransferMode = ELoopDebug;
sl@0
   158
				break;
sl@0
   159
			case 'V':
sl@0
   160
			case 'v':
sl@0
   161
				VerboseMode = true;
sl@0
   162
				break;
sl@0
   163
			case 'Z':
sl@0
   164
			case 'z':
sl@0
   165
				ZlpMode = true;
sl@0
   166
				break;
sl@0
   167
			case '?':
sl@0
   168
			case 'H':
sl@0
   169
			case 'h':
sl@0
   170
				cout << help_text;
sl@0
   171
				return -1;
sl@0
   172
			default:
sl@0
   173
				cout << "* Invalid argument: " << argv[i] << "\n";
sl@0
   174
				cout << help_text;
sl@0
   175
				return -1;
sl@0
   176
				}
sl@0
   177
			}
sl@0
   178
		}
sl@0
   179
	return 0;
sl@0
   180
	}
sl@0
   181
sl@0
   182
sl@0
   183
static void OpenUsbDevice()
sl@0
   184
	{
sl@0
   185
	CUsbIo::DestroyDeviceList(g_DevList);
sl@0
   186
sl@0
   187
	// Enumerate attached USB devices supported by USBIO
sl@0
   188
	g_DevList = CUsbIo::CreateDeviceList(&g_UsbioID);
sl@0
   189
sl@0
   190
	// Open first device in list
sl@0
   191
	dwRC = g_UsbDev.Open(0, g_DevList, &g_UsbioID);
sl@0
   192
sl@0
   193
	PRINT_IF_VERBOSE1("\nCUsbIo::Open returned <0x%X>\n", dwRC);
sl@0
   194
sl@0
   195
	if (dwRC == USBIO_ERR_VERSION_MISMATCH)
sl@0
   196
		{
sl@0
   197
		printf("\n* Error: \"The API version reported by the USBRFLCT driver\n" \
sl@0
   198
			   "*         does not match the expected version.\"\n");
sl@0
   199
		printf("* The driver will need to be updated as follows:\n");
sl@0
   200
		printf("* 1. Connect the device to the PC & start T_USB,\n" \
sl@0
   201
			   "* then find the USB device in the Windows Device Manager\n" \
sl@0
   202
			   "* ('Control Panel'->'System'->'Hardware'->'Device Manager').\n" \
sl@0
   203
			   "* Right click on the device name and choose 'Uninstall...'.\n");
sl@0
   204
		printf("* 2. In c:\\winnt\\inf\\, find (by searching for \"Symbian\") and\n" \
sl@0
   205
			   "* delete the *.INF file that was used to install the existing\n" \
sl@0
   206
			   "* version of USBRFLCT.SYS. Make sure to also delete the\n" \
sl@0
   207
			   "* precompiled version of that file (<samename>.PNF).\n");
sl@0
   208
		printf("* 3. In c:\\winnt\\system32\\drivers\\, delete the file USBRFLCT.SYS.\n");
sl@0
   209
		printf("* Then unplug & reconnect the USB device and, when prompted, install\n" \
sl@0
   210
			   "* the new USBRFLCT.SYS driver using the .INF file from this distribution.\n" \
sl@0
   211
			   "* (All files can be found under e32test\\win32\\usbrflct_distribution\\.)\n");
sl@0
   212
		}
sl@0
   213
	}
sl@0
   214
sl@0
   215
sl@0
   216
static void CloseUsbDevice()
sl@0
   217
	{
sl@0
   218
	// Close the device
sl@0
   219
	g_UsbDev.Close();
sl@0
   220
	PRINT_IF_VERBOSE("CUsbIo::Close called\n");
sl@0
   221
	}
sl@0
   222
sl@0
   223
sl@0
   224
static void GetDeviceDescriptor()
sl@0
   225
	{
sl@0
   226
	USB_DEVICE_DESCRIPTOR DeviceDescriptor;
sl@0
   227
sl@0
   228
	memset(&DeviceDescriptor, 0, sizeof(USB_DEVICE_DESCRIPTOR));
sl@0
   229
sl@0
   230
	// Get device descriptor
sl@0
   231
	dwRC = g_UsbDev.GetDeviceDescriptor(&DeviceDescriptor);
sl@0
   232
	PRINT_IF_VERBOSE1("CUsbIo::GetDeviceDescriptor returned <0x%X>\n", dwRC);
sl@0
   233
sl@0
   234
	if (VerboseMode && (dwRC == USBIO_ERR_SUCCESS))
sl@0
   235
		{
sl@0
   236
		printf("\nDEVICE DESCRIPTOR:\n"
sl@0
   237
			   "bLength = <%u>\n"
sl@0
   238
			   "bDescriptorType = <%u>\n"
sl@0
   239
			   "bcdUSB = <%u>\n"
sl@0
   240
			   "bDeviceClass = <%u>\n"
sl@0
   241
			   "bDeviceSubClass = <%u>\n"
sl@0
   242
			   "bDeviceProtocol = <%u>\n"
sl@0
   243
			   "bMaxPacketSize0 = <%u>\n"
sl@0
   244
			   "idVendor = <%u>\n"
sl@0
   245
			   "idProduct = <%u>\n"
sl@0
   246
			   "bcdDevice = <%u>\n"
sl@0
   247
			   "iManufacturer = <%u>\n"
sl@0
   248
			   "iProduct = <%u>\n"
sl@0
   249
			   "iSerialNumber = <%u>\n"
sl@0
   250
			   "bNumConfigurations = <%u>\n\n",
sl@0
   251
			   DeviceDescriptor.bLength,
sl@0
   252
			   DeviceDescriptor.bDescriptorType,
sl@0
   253
			   DeviceDescriptor.bcdUSB,
sl@0
   254
			   DeviceDescriptor.bDeviceClass,
sl@0
   255
			   DeviceDescriptor.bDeviceSubClass,
sl@0
   256
			   DeviceDescriptor.bDeviceProtocol,
sl@0
   257
			   DeviceDescriptor.bMaxPacketSize0,
sl@0
   258
			   DeviceDescriptor.idVendor,
sl@0
   259
			   DeviceDescriptor.idProduct,
sl@0
   260
			   DeviceDescriptor.bcdDevice,
sl@0
   261
			   DeviceDescriptor.iManufacturer,
sl@0
   262
			   DeviceDescriptor.iProduct,
sl@0
   263
			   DeviceDescriptor.iSerialNumber,
sl@0
   264
			   DeviceDescriptor.bNumConfigurations);
sl@0
   265
		}
sl@0
   266
	}
sl@0
   267
sl@0
   268
sl@0
   269
static void GetConfigurationDescriptor()
sl@0
   270
	{
sl@0
   271
	CHAR szBuffer[MAX_DESCRIPTOR_BUFFER_SIZE] = "";
sl@0
   272
	USB_CONFIGURATION_DESCRIPTOR* pConfigDescriptor = NULL;
sl@0
   273
	USB_INTERFACE_DESCRIPTOR* pInterfaceDescriptor = NULL;
sl@0
   274
	USB_ENDPOINT_DESCRIPTOR* pEndpointDescriptor = NULL;
sl@0
   275
sl@0
   276
	DWORD dwByteCount = MAX_DESCRIPTOR_BUFFER_SIZE;
sl@0
   277
sl@0
   278
	memset(szBuffer, 0, sizeof(szBuffer));
sl@0
   279
sl@0
   280
	// Get first configuration descriptor
sl@0
   281
	dwRC =
sl@0
   282
		g_UsbDev.GetConfigurationDescriptor((USB_CONFIGURATION_DESCRIPTOR*) szBuffer,
sl@0
   283
											dwByteCount, 0);
sl@0
   284
	PRINT_IF_VERBOSE1("CUsbIo::GetConfigurationDescriptor returned <0x%X>\n", dwRC);
sl@0
   285
sl@0
   286
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   287
		{
sl@0
   288
		USB_COMMON_DESCRIPTOR* Desc;
sl@0
   289
		ULONG rl = dwByteCount;
sl@0
   290
		ULONG ulDescLength = 0;
sl@0
   291
		CHAR* data = szBuffer;
sl@0
   292
sl@0
   293
		while (rl > 0)
sl@0
   294
			{
sl@0
   295
			Desc = (USB_COMMON_DESCRIPTOR*) data;
sl@0
   296
			ulDescLength = Desc->bLength;
sl@0
   297
			if ((ulDescLength > rl) || (ulDescLength == 0))
sl@0
   298
				{
sl@0
   299
				printf("Length remaining too short!\n");
sl@0
   300
				rl = 0;
sl@0
   301
				}
sl@0
   302
			else
sl@0
   303
				{
sl@0
   304
				switch (Desc->bDescriptorType)
sl@0
   305
					{
sl@0
   306
				case USB_CONFIGURATION_DESCRIPTOR_TYPE:
sl@0
   307
					pConfigDescriptor =
sl@0
   308
						(USB_CONFIGURATION_DESCRIPTOR*) data;
sl@0
   309
					if (VerboseMode)
sl@0
   310
						{
sl@0
   311
						printf("\nCONFIGURATION DESCRIPTOR:\n"
sl@0
   312
							   "bLength = <%u>\n"
sl@0
   313
							   "bDescriptorType = <%u>\n"
sl@0
   314
							   "wTotalLength = <%u>\n"
sl@0
   315
							   "bNumInterfaces = <%u>\n"
sl@0
   316
							   "bConfigurationValue = <%u>\n"
sl@0
   317
							   "iConfiguration = <%u>\n"
sl@0
   318
							   "bmAttributes = <%u>\n"
sl@0
   319
							   "MaxPower = <%u>\n",
sl@0
   320
							   pConfigDescriptor->bLength,
sl@0
   321
							   pConfigDescriptor->bDescriptorType,
sl@0
   322
							   pConfigDescriptor->wTotalLength,
sl@0
   323
							   pConfigDescriptor->bNumInterfaces,
sl@0
   324
							   pConfigDescriptor->bConfigurationValue,
sl@0
   325
							   pConfigDescriptor->iConfiguration,
sl@0
   326
							   pConfigDescriptor->bmAttributes,
sl@0
   327
							   pConfigDescriptor->MaxPower);
sl@0
   328
						}
sl@0
   329
					break;
sl@0
   330
				case USB_INTERFACE_DESCRIPTOR_TYPE:
sl@0
   331
					pInterfaceDescriptor =
sl@0
   332
						(USB_INTERFACE_DESCRIPTOR*) data;
sl@0
   333
					if (VerboseMode)
sl@0
   334
						{
sl@0
   335
						printf("\nINTERFACE DESCRIPTOR: \n"
sl@0
   336
							   "bLength = <%u>\n"
sl@0
   337
							   "bDescriptorType = <%u>\n"
sl@0
   338
							   "bInterfaceNumber = <%u>\n"
sl@0
   339
							   "bAlternateSetting = <%u>\n"
sl@0
   340
							   "bNumEndpoints = <%u>\n"
sl@0
   341
							   "bInterfaceClass = <%u>\n"
sl@0
   342
							   "bInterfaceSubClass = <%u>\n"
sl@0
   343
							   "bInterfaceProtocol = <%u>\n"
sl@0
   344
							   "iInterface = <%u>\n",
sl@0
   345
							   pInterfaceDescriptor->bLength,
sl@0
   346
							   pInterfaceDescriptor->bDescriptorType,
sl@0
   347
							   pInterfaceDescriptor->bInterfaceNumber,
sl@0
   348
							   pInterfaceDescriptor->bAlternateSetting,
sl@0
   349
							   pInterfaceDescriptor->bNumEndpoints,
sl@0
   350
							   pInterfaceDescriptor->bInterfaceClass,
sl@0
   351
							   pInterfaceDescriptor->bInterfaceSubClass,
sl@0
   352
							   pInterfaceDescriptor->bInterfaceProtocol,
sl@0
   353
							   pInterfaceDescriptor->iInterface);
sl@0
   354
						}
sl@0
   355
					break;
sl@0
   356
				case USB_ENDPOINT_DESCRIPTOR_TYPE:
sl@0
   357
					pEndpointDescriptor =
sl@0
   358
						(USB_ENDPOINT_DESCRIPTOR*) data;
sl@0
   359
					if (VerboseMode)
sl@0
   360
						{
sl@0
   361
						printf("\nENDPOINT DESCRIPTOR: \n"
sl@0
   362
							   "bLength = <%u>\n"
sl@0
   363
							   "bDescriptorType = <%u>\n"
sl@0
   364
							   "bEndpointAddress = <%u>\n"
sl@0
   365
							   "bmAttributes = <%u>\n"
sl@0
   366
							   "wMaxPacketSize = <%u>\n"
sl@0
   367
							   "bInterval = <%u>\n",
sl@0
   368
							   pEndpointDescriptor->bLength,
sl@0
   369
							   pEndpointDescriptor->bDescriptorType,
sl@0
   370
							   pEndpointDescriptor->bEndpointAddress,
sl@0
   371
							   pEndpointDescriptor->bmAttributes,
sl@0
   372
							   pEndpointDescriptor->wMaxPacketSize,
sl@0
   373
							   pEndpointDescriptor->bInterval);
sl@0
   374
						}
sl@0
   375
					break;
sl@0
   376
				default:
sl@0
   377
					break;
sl@0
   378
					}
sl@0
   379
				}
sl@0
   380
			data += ulDescLength;
sl@0
   381
			rl -= ulDescLength;
sl@0
   382
			}
sl@0
   383
		}
sl@0
   384
	PRINT_IF_VERBOSE("\n");
sl@0
   385
	}
sl@0
   386
sl@0
   387
sl@0
   388
static void GetStringDescriptor()
sl@0
   389
	{
sl@0
   390
	CHAR szBuffer[MAX_DESCRIPTOR_BUFFER_SIZE] = "";
sl@0
   391
	USB_STRING_DESCRIPTOR* pStringDescriptor = NULL;
sl@0
   392
	DWORD dwByteCount = MAX_DESCRIPTOR_BUFFER_SIZE;
sl@0
   393
sl@0
   394
	memset(szBuffer, 0, sizeof(szBuffer));
sl@0
   395
sl@0
   396
	// Get string descriptor
sl@0
   397
	dwRC = g_UsbDev.GetStringDescriptor((USB_STRING_DESCRIPTOR*) szBuffer,
sl@0
   398
										dwByteCount, 1, 0);
sl@0
   399
	PRINT_IF_VERBOSE1("CUsbIo::GetStringDescriptor returned <0x%X>\n", dwRC);
sl@0
   400
sl@0
   401
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   402
		{
sl@0
   403
		pStringDescriptor = (USB_STRING_DESCRIPTOR*) szBuffer;
sl@0
   404
		if (VerboseMode)
sl@0
   405
			{
sl@0
   406
			printf("\nSTRING DESCRIPTOR:\n"
sl@0
   407
				   "bLength = <%u>\n"
sl@0
   408
				   "bDescriptorType = <%u>\n"
sl@0
   409
				   "bString = <",							// output continues below!
sl@0
   410
				   pStringDescriptor->bLength,
sl@0
   411
				   pStringDescriptor->bDescriptorType);
sl@0
   412
			}
sl@0
   413
		INT i = 0;
sl@0
   414
		CHAR* Ptr = szBuffer;
sl@0
   415
		for (i = 2, Ptr += 2;
sl@0
   416
			 i < pStringDescriptor->bLength;
sl@0
   417
			 i += 2, Ptr += 2)
sl@0
   418
			{
sl@0
   419
			PRINT_IF_VERBOSE1("%c", *Ptr);
sl@0
   420
			}
sl@0
   421
		PRINT_IF_VERBOSE(">\n\n");
sl@0
   422
		}
sl@0
   423
	}
sl@0
   424
sl@0
   425
sl@0
   426
static void SetConfiguration()
sl@0
   427
	{
sl@0
   428
	USBIO_SET_CONFIGURATION SetConfig;
sl@0
   429
sl@0
   430
	memset(&SetConfig, 0, sizeof(USBIO_SET_CONFIGURATION));
sl@0
   431
sl@0
   432
	// Set the first configuration as active
sl@0
   433
	SetConfig.ConfigurationIndex = 0;
sl@0
   434
	SetConfig.NbOfInterfaces = 1;
sl@0
   435
	SetConfig.InterfaceList[0].InterfaceIndex = 0;
sl@0
   436
	SetConfig.InterfaceList[0].AlternateSettingIndex = 0;
sl@0
   437
	SetConfig.InterfaceList[0].MaximumTransferSize = KBufferSize;
sl@0
   438
	dwRC = g_UsbDev.SetConfiguration(&SetConfig);
sl@0
   439
	PRINT_IF_VERBOSE1("CUsbIo::SetConfiguration returned <0x%X>\n", dwRC);
sl@0
   440
	}
sl@0
   441
sl@0
   442
sl@0
   443
static void GetConfigurationInfo()
sl@0
   444
	{
sl@0
   445
	USBIO_CONFIGURATION_INFO ConfigInfo;
sl@0
   446
	USHORT i = 0;
sl@0
   447
sl@0
   448
	memset(&ConfigInfo, 0, sizeof(USBIO_CONFIGURATION_INFO));
sl@0
   449
sl@0
   450
	dwRC = g_UsbDev.GetConfigurationInfo(&ConfigInfo);
sl@0
   451
	PRINT_IF_VERBOSE1("CUsbIo::GetConfigurationInfo returned <0x%X>\n", dwRC);
sl@0
   452
sl@0
   453
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   454
		{
sl@0
   455
		if (VerboseMode)
sl@0
   456
			{
sl@0
   457
			printf("\nCONFIGURATION INFO:\n"
sl@0
   458
				   "NbOfInterfaces = <%lu>\n"
sl@0
   459
				   "NbOfPipes = <%lu>\n",
sl@0
   460
				   ConfigInfo.NbOfInterfaces,
sl@0
   461
				   ConfigInfo.NbOfPipes);
sl@0
   462
			}
sl@0
   463
		for (i = 0; i < ConfigInfo.NbOfInterfaces; i++)
sl@0
   464
			{
sl@0
   465
			if (VerboseMode)
sl@0
   466
				{
sl@0
   467
				printf("\nINTERFACE <%u>:\n", i + 1);
sl@0
   468
				printf("InterfaceNumber = <%u>\n"
sl@0
   469
					   "AlternateSetting = <%u>\n"
sl@0
   470
					   "Class = <%u>\n"
sl@0
   471
					   "SubClass = <%u>\n"
sl@0
   472
					   "Protocol = <%u>\n"
sl@0
   473
					   "NumberOfPipes = <%u>\n"
sl@0
   474
					   "reserved1 = <%u>\n"
sl@0
   475
					   "reserved2 = <%u>\n",
sl@0
   476
					   ConfigInfo.InterfaceInfo[i].InterfaceNumber,
sl@0
   477
					   ConfigInfo.InterfaceInfo[i].AlternateSetting,
sl@0
   478
					   ConfigInfo.InterfaceInfo[i].Class,
sl@0
   479
					   ConfigInfo.InterfaceInfo[i].SubClass,
sl@0
   480
					   ConfigInfo.InterfaceInfo[i].Protocol,
sl@0
   481
					   ConfigInfo.InterfaceInfo[i].NumberOfPipes,
sl@0
   482
					   ConfigInfo.InterfaceInfo[i].reserved1,
sl@0
   483
					   ConfigInfo.InterfaceInfo[i].reserved2);
sl@0
   484
				}
sl@0
   485
			}
sl@0
   486
		for (i = 0; i < ConfigInfo.NbOfPipes; i++)
sl@0
   487
			{
sl@0
   488
			PRINT_IF_VERBOSE("\n");
sl@0
   489
			if ((ConfigInfo.PipeInfo[i].PipeType == PipeTypeBulk) &&
sl@0
   490
				!(ConfigInfo.PipeInfo[i].EndpointAddress & 0x80))
sl@0
   491
				{
sl@0
   492
				PRINT_IF_VERBOSE("Bulk OUT pipe found:\n");
sl@0
   493
				g_ucBulkOutEndpoint = ConfigInfo.PipeInfo[i].EndpointAddress;
sl@0
   494
				maxOutPacketSize = ConfigInfo.PipeInfo[i].MaximumPacketSize;
sl@0
   495
				}
sl@0
   496
			else if ((ConfigInfo.PipeInfo[i].PipeType == PipeTypeBulk) &&
sl@0
   497
					 (ConfigInfo.PipeInfo[i].EndpointAddress & 0x80))
sl@0
   498
				{
sl@0
   499
				PRINT_IF_VERBOSE("Bulk IN pipe found:\n");
sl@0
   500
				g_ucBulkInEndpoint = ConfigInfo.PipeInfo[i].EndpointAddress;
sl@0
   501
				}
sl@0
   502
			if (VerboseMode)
sl@0
   503
				{
sl@0
   504
				printf("PIPE <%u>\n", i + 1);
sl@0
   505
				printf("PipeType = <%d>\n"
sl@0
   506
					   "MaximumTransferSize = <%lu>\n"
sl@0
   507
					   "MaximumPacketSize = <%u>\n"
sl@0
   508
					   "EndpointAddress = <%u>\n"
sl@0
   509
					   "Interval = <%u>\n"
sl@0
   510
					   "InterfaceNumber = <%u>\n"
sl@0
   511
					   "reserved1 = <%u>\n"
sl@0
   512
					   "reserved2 = <%u>\n"
sl@0
   513
					   "reserved3 = <%u>\n",
sl@0
   514
					   ConfigInfo.PipeInfo[i].PipeType,
sl@0
   515
					   ConfigInfo.PipeInfo[i].MaximumTransferSize,
sl@0
   516
					   ConfigInfo.PipeInfo[i].MaximumPacketSize,
sl@0
   517
					   ConfigInfo.PipeInfo[i].EndpointAddress,
sl@0
   518
					   ConfigInfo.PipeInfo[i].Interval,
sl@0
   519
					   ConfigInfo.PipeInfo[i].InterfaceNumber,
sl@0
   520
					   ConfigInfo.PipeInfo[i].reserved1,
sl@0
   521
					   ConfigInfo.PipeInfo[i].reserved2,
sl@0
   522
					   ConfigInfo.PipeInfo[i].reserved3);
sl@0
   523
				}
sl@0
   524
			}
sl@0
   525
		}
sl@0
   526
	PRINT_IF_VERBOSE("\n");
sl@0
   527
	}
sl@0
   528
sl@0
   529
sl@0
   530
static void OpenPipes()
sl@0
   531
	{
sl@0
   532
	CUsbIo::DestroyDeviceList(g_DevList);
sl@0
   533
sl@0
   534
	// Enumerate attached USB devices supported by USBIO
sl@0
   535
	g_DevList = CUsbIo::CreateDeviceList(&g_UsbioID);
sl@0
   536
sl@0
   537
	// Create the bulk OUT pipe
sl@0
   538
	dwRC = g_BulkOutPipe.Bind(0, g_ucBulkOutEndpoint, g_DevList, &g_UsbioID);
sl@0
   539
	PRINT_IF_VERBOSE1("CUsbIoPipe::Bind (Bulk OUT) returned <0x%X>\n", dwRC);
sl@0
   540
sl@0
   541
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   542
		{
sl@0
   543
		// Create the bulk IN pipe
sl@0
   544
		dwRC = g_BulkInPipe.Bind(0, g_ucBulkInEndpoint, g_DevList, &g_UsbioID);
sl@0
   545
		PRINT_IF_VERBOSE1("CUsbIoPipe::Bind (Bulk IN) returned <0x%X>\n", dwRC);
sl@0
   546
		}
sl@0
   547
	PRINT_IF_VERBOSE("\n");
sl@0
   548
	}
sl@0
   549
sl@0
   550
sl@0
   551
static void ClosePipes()
sl@0
   552
	{
sl@0
   553
	// Close down the bulk OUT pipe
sl@0
   554
	dwRC = g_BulkOutPipe.Unbind();
sl@0
   555
	PRINT_IF_VERBOSE1("CUsbIoPipe::Unbind (Bulk OUT) returned <0x%X>\n", dwRC);
sl@0
   556
sl@0
   557
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   558
		{
sl@0
   559
		// Close down the bulk IN pipe
sl@0
   560
		dwRC = g_BulkInPipe.Unbind();
sl@0
   561
		PRINT_IF_VERBOSE1("CUsbIoPipe::Unbind (Bulk IN) returned <0x%X>\n", dwRC);
sl@0
   562
		}
sl@0
   563
	}
sl@0
   564
sl@0
   565
sl@0
   566
static void ReceiveVersion()
sl@0
   567
	{
sl@0
   568
	// Here we (hope to) read an 8 byte packet containing the T_USB version.
sl@0
   569
	printf("* Waiting for T_USB version packet to arrive...");
sl@0
   570
sl@0
   571
	// The first 4 bytes are interpreted as an int32 value.
sl@0
   572
	DWORD bytes_read = 0;
sl@0
   573
	g_Buf.NumberOfBytesToTransfer = KPreambleLength;
sl@0
   574
	g_BulkInPipe.Read(&g_Buf);
sl@0
   575
	dwRC = g_BulkInPipe.WaitForCompletion(&g_Buf, INFINITE);
sl@0
   576
	printf(" done.\n");
sl@0
   577
	bytes_read = g_Buf.BytesTransferred;
sl@0
   578
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   579
		{
sl@0
   580
		if (bytes_read < KPreambleLength)
sl@0
   581
			{
sl@0
   582
			printf("* Read less bytes (%d) than expected (%d).\n",
sl@0
   583
				   bytes_read, KPreambleLength);
sl@0
   584
			dwRC = USBIO_ERR_FAILED;
sl@0
   585
			return;
sl@0
   586
			}
sl@0
   587
		}
sl@0
   588
	else
sl@0
   589
		{
sl@0
   590
		printf("\n* Error: CUsbIoPipe::Read (version) returned <0x%X>\n", dwRC);
sl@0
   591
		return;
sl@0
   592
		}
sl@0
   593
	// First make sure it's actually the version packet, and not
sl@0
   594
	// a data preamble packet of an old T_USB.
sl@0
   595
	if (!(Data[4] == 'V' &&
sl@0
   596
		  Data[5] == 'e' &&
sl@0
   597
		  Data[6] == 'r' &&
sl@0
   598
		  Data[7] == 's'))
sl@0
   599
		{
sl@0
   600
		printf("* Inadequate version of T_USB: no version packet was sent (we need at least %d)\n",
sl@0
   601
			   KTusbVersion);
sl@0
   602
		dwRC = USBIO_ERR_FAILED;
sl@0
   603
		return;
sl@0
   604
		}
sl@0
   605
	DWORD tusb_version = *((ULONG*) Data);						// first 4 bytes
sl@0
   606
	if (tusb_version < KTusbVersion)
sl@0
   607
		{
sl@0
   608
		printf("* Inadequate version of T_USB: %d (we need at least %d)\n",
sl@0
   609
			   tusb_version, KTusbVersion);
sl@0
   610
		dwRC = USBIO_ERR_FAILED;
sl@0
   611
		return;
sl@0
   612
		}
sl@0
   613
	printf("* Suitable version of T_USB found: %d.\n", tusb_version);
sl@0
   614
	}
sl@0
   615
sl@0
   616
sl@0
   617
static void SendVersion()
sl@0
   618
	{
sl@0
   619
	// Here we send an 8 byte packet containing USBRFLCT's + USBIO's versions.
sl@0
   620
	printf("* Sending our version packet to T_USB...");
sl@0
   621
sl@0
   622
	DWORD bytes_written = 0;
sl@0
   623
	g_Buf.NumberOfBytesToTransfer = KPreambleLength;
sl@0
   624
	Data[0] = VERSION_MAJOR;
sl@0
   625
	Data[1] = VERSION_MINOR;
sl@0
   626
	Data[2] = VERSION_MICRO;
sl@0
   627
	Data[3] = USBIO_VERSION_MAJOR;
sl@0
   628
	Data[4] = USBIO_VERSION_MINOR;
sl@0
   629
	g_BulkOutPipe.Write(&g_Buf);
sl@0
   630
	dwRC = g_BulkOutPipe.WaitForCompletion(&g_Buf, INFINITE);
sl@0
   631
	printf(" done.\n");
sl@0
   632
	bytes_written = g_Buf.BytesTransferred;
sl@0
   633
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   634
		{
sl@0
   635
		if (bytes_written < KPreambleLength)
sl@0
   636
			{
sl@0
   637
			printf("* Wrote less bytes (%d) than requested (%d).\n",
sl@0
   638
				   bytes_written, KPreambleLength);
sl@0
   639
			dwRC = USBIO_ERR_FAILED;
sl@0
   640
			}
sl@0
   641
		}
sl@0
   642
	else
sl@0
   643
		{
sl@0
   644
		printf("\n* Error: CUsbIoPipe::Write (version) returned <0x%X>\n", dwRC);
sl@0
   645
		}
sl@0
   646
	}
sl@0
   647
sl@0
   648
sl@0
   649
static void ExchangeVersions()
sl@0
   650
	{
sl@0
   651
	SendVersion();
sl@0
   652
	if (dwRC != USBIO_ERR_SUCCESS)
sl@0
   653
		return;
sl@0
   654
	ReceiveVersion();
sl@0
   655
	}
sl@0
   656
sl@0
   657
sl@0
   658
static void GetLength()
sl@0
   659
	{
sl@0
   660
	// The first two bytes are interpreted as a length value.
sl@0
   661
	DWORD bytes_read = KPreambleLength;
sl@0
   662
	g_Buf.NumberOfBytesToTransfer = KPreambleLength;
sl@0
   663
sl@0
   664
	g_BulkInPipe.Read(&g_Buf);
sl@0
   665
	dwRC = g_BulkInPipe.WaitForCompletion(&g_Buf, INFINITE);
sl@0
   666
	bytes_read = g_Buf.BytesTransferred;
sl@0
   667
sl@0
   668
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   669
		{
sl@0
   670
		if (bytes_read < KPreambleLength)
sl@0
   671
			{
sl@0
   672
			printf("* Read less bytes (%d) than expected (%d).\n",
sl@0
   673
				   bytes_read, KPreambleLength);
sl@0
   674
			dwRC = USBIO_ERR_FAILED;
sl@0
   675
			return;
sl@0
   676
			}
sl@0
   677
		}
sl@0
   678
	else
sl@0
   679
		{
sl@0
   680
		printf("\n* Error: CUsbIoPipe::Read (length) returned <0x%X>\n", dwRC);
sl@0
   681
		return;
sl@0
   682
		}
sl@0
   683
	Length = *((ULONG*) Data);								// first 4 bytes
sl@0
   684
	if (Length > KBufferSize)
sl@0
   685
		{
sl@0
   686
		printf("* This is too much: %d (our buffer is too small: %d)\n",
sl@0
   687
			   Length, KBufferSize);
sl@0
   688
		dwRC = USBIO_ERR_FAILED;
sl@0
   689
		}
sl@0
   690
	if (VerboseMode)
sl@0
   691
		{
sl@0
   692
		printf("* Just read %d bytes, now assuming transfer length is %d bytes.\n",
sl@0
   693
			   bytes_read, Length);
sl@0
   694
		}
sl@0
   695
	else if (TransferMode == EReceiveOnly || TransferMode == ETransmitOnly)
sl@0
   696
		{
sl@0
   697
		printf("* Single transfer size: %d bytes.\n", Length);
sl@0
   698
		}
sl@0
   699
	}
sl@0
   700
sl@0
   701
sl@0
   702
static void ReadData()
sl@0
   703
	{
sl@0
   704
	// We have to setup a read for at least one byte in order to get
sl@0
   705
	// the host to issue IN tokens for our zero-byte read:
sl@0
   706
	if (Length == 0)
sl@0
   707
		g_Buf.NumberOfBytesToTransfer = 1;
sl@0
   708
	else
sl@0
   709
		g_Buf.NumberOfBytesToTransfer = Length;
sl@0
   710
sl@0
   711
	g_BulkInPipe.Read(&g_Buf);
sl@0
   712
	dwRC = g_BulkInPipe.WaitForCompletion(&g_Buf, INFINITE);
sl@0
   713
	DWORD bytes_read = g_Buf.BytesTransferred;
sl@0
   714
sl@0
   715
	if (dwRC != USBIO_ERR_SUCCESS)
sl@0
   716
		{
sl@0
   717
		printf("\n* Error: CUsbIoPipe::Read (data) returned <0x%X>\n", dwRC);
sl@0
   718
		}
sl@0
   719
	else
sl@0
   720
		{
sl@0
   721
		if (bytes_read != Length)
sl@0
   722
			{
sl@0
   723
			printf("* Read more/less bytes (%d) than expected (%d).\n",
sl@0
   724
				   bytes_read, Length);
sl@0
   725
			dwRC = USBIO_ERR_FAILED;
sl@0
   726
			}
sl@0
   727
		else
sl@0
   728
			{
sl@0
   729
			PRINT_IF_VERBOSE1("* Read %d bytes.\n", Length);
sl@0
   730
			}
sl@0
   731
		}
sl@0
   732
	}
sl@0
   733
sl@0
   734
sl@0
   735
static void WriteData()
sl@0
   736
	{
sl@0
   737
	DWORD bytes_written = Length;
sl@0
   738
sl@0
   739
	g_Buf.NumberOfBytesToTransfer = bytes_written;
sl@0
   740
sl@0
   741
	g_BulkOutPipe.Write(&g_Buf);
sl@0
   742
	dwRC = g_BulkOutPipe.WaitForCompletion(&g_Buf, INFINITE);
sl@0
   743
	if (ZlpMode && (Length >= maxOutPacketSize) && ((Length % maxOutPacketSize) == 0))
sl@0
   744
	{
sl@0
   745
		// writes a zero length packet
sl@0
   746
		g_BulkOutPipe.Write(&g_ZlpBuf);
sl@0
   747
		dwRC = g_BulkOutPipe.WaitForCompletion(&g_ZlpBuf, INFINITE);
sl@0
   748
	}
sl@0
   749
sl@0
   750
sl@0
   751
	bytes_written = g_Buf.BytesTransferred;
sl@0
   752
sl@0
   753
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   754
		{
sl@0
   755
		PRINT_IF_VERBOSE1("* Wrote %d bytes.\n", bytes_written);
sl@0
   756
		}
sl@0
   757
	else
sl@0
   758
		{
sl@0
   759
		printf("\n* Error: CUsbIoPipe::Write returned <0x%X>\n", dwRC);
sl@0
   760
		}
sl@0
   761
	}
sl@0
   762
sl@0
   763
sl@0
   764
void PrintStats()
sl@0
   765
	{
sl@0
   766
	static DWORD loop = 0;									// the loop counter
sl@0
   767
	static double xfer_size = 0;							// the total transfer amount so far
sl@0
   768
	time_t t_1 = time(NULL);								// current time
sl@0
   769
	double t_diff = difftime(t_1, T_0);						// this yields current seconds since start
sl@0
   770
	xfer_size += (KPreambleLength + (2 * Length)) * KLoopModeDisplayUpdate;	//
sl@0
   771
	double xfer_rate = xfer_size / t_diff;					// mean transfer rate since start
sl@0
   772
	loop += KLoopModeDisplayUpdate;
sl@0
   773
	printf("* Iter: %d (%d bytes) Total: %.0f bytes Rate: %.0f bytes/s  \r",
sl@0
   774
		   loop, Length, xfer_size, xfer_rate);
sl@0
   775
	}
sl@0
   776
sl@0
   777
sl@0
   778
void PrintStatsEveryLoop()
sl@0
   779
	{
sl@0
   780
	static DWORD loop = 0;									// the loop counter
sl@0
   781
	static double xfer_size = 0;							// the total transfer amount so far
sl@0
   782
	time_t t_1 = time(NULL);								// current time
sl@0
   783
	double t_diff = difftime(t_1, T_0);						// this yields current seconds since start
sl@0
   784
	xfer_size += (KPreambleLength + (2 * Length));			//
sl@0
   785
	double xfer_rate = xfer_size / t_diff;					// mean transfer rate since start
sl@0
   786
	printf("* Iter: %d (%d bytes) Total: %.0f bytes Rate: %.0f bytes/s  \r",
sl@0
   787
		   ++loop, Length, xfer_size, xfer_rate);
sl@0
   788
	}
sl@0
   789
sl@0
   790
sl@0
   791
void PrintUnidirStats()
sl@0
   792
	{
sl@0
   793
	static DWORD loop = 0;									// the loop counter
sl@0
   794
	static double xfer_size = 0;							// the total transfer amount so far
sl@0
   795
	time_t t_1 = time(NULL);								// current time
sl@0
   796
	double t_diff = difftime(t_1, T_0);						// this yields current seconds since start
sl@0
   797
	xfer_size += Length * KUniModeDisplayUpdate;
sl@0
   798
	double xfer_rate = xfer_size / t_diff;					// mean transfer rate since start
sl@0
   799
	loop += KUniModeDisplayUpdate;
sl@0
   800
	printf("* Iter: %d (%d bytes) Total: %.0f bytes Rate: %.0f bytes/s  \r",
sl@0
   801
		   loop, Length, xfer_size, xfer_rate);
sl@0
   802
	}
sl@0
   803
sl@0
   804
sl@0
   805
static void LoopTransfer()
sl@0
   806
	{
sl@0
   807
	printf("* Loop Transfers -- reading & writing alternately.\n");
sl@0
   808
sl@0
   809
	T_0 = time(NULL);										// starting time
sl@0
   810
sl@0
   811
	while (dwRC == USBIO_ERR_SUCCESS)
sl@0
   812
		{
sl@0
   813
		static DWORD n = 0;
sl@0
   814
sl@0
   815
		// First we get the length (+ the packet size)
sl@0
   816
		GetLength();
sl@0
   817
sl@0
   818
		if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   819
			{
sl@0
   820
			// Then we read 'Length' bytes
sl@0
   821
			ReadData();
sl@0
   822
			}
sl@0
   823
sl@0
   824
		if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   825
			{
sl@0
   826
			// Now we send the received data back to the client.
sl@0
   827
			WriteData();
sl@0
   828
			}
sl@0
   829
sl@0
   830
		if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   831
			{
sl@0
   832
			// Finally, sometimes we print some statistics
sl@0
   833
			if (TransferMode == ELoopDebug)
sl@0
   834
				PrintStatsEveryLoop();
sl@0
   835
			else if ((++n % KLoopModeDisplayUpdate) == 0)
sl@0
   836
				PrintStats();
sl@0
   837
			}
sl@0
   838
		}
sl@0
   839
	}
sl@0
   840
sl@0
   841
sl@0
   842
static void ReceiveOnlyTransfer()
sl@0
   843
	{
sl@0
   844
	printf("* Receive-only transfers (IN).\n");
sl@0
   845
sl@0
   846
	// First (and only once) we get the transfer length (+ the packet size)
sl@0
   847
	GetLength();
sl@0
   848
sl@0
   849
	T_0 = time(NULL);										// starting time
sl@0
   850
sl@0
   851
	while (dwRC == USBIO_ERR_SUCCESS)
sl@0
   852
		{
sl@0
   853
		static DWORD n = -1;
sl@0
   854
		static DWORD pktNum;
sl@0
   855
sl@0
   856
		// Then we read 'Length' bytes
sl@0
   857
		ReadData();
sl@0
   858
		pktNum = *(DWORD *)&Data;
sl@0
   859
		if (pktNum != ++n)
sl@0
   860
			{
sl@0
   861
			printf ("\n* Error: rcv'd wrong pkt number: 0x%x (expected: 0x%x)\n", pktNum, n);
sl@0
   862
			// reset from the received packet number, so that ...
sl@0
   863
			// if a packet is lost or duplicated a single error is reported
sl@0
   864
			n = pktNum;
sl@0
   865
			}
sl@0
   866
		// Finally, sometimes we print some statistics
sl@0
   867
		if ((n % KUniModeDisplayUpdate) == 0)
sl@0
   868
			PrintUnidirStats();
sl@0
   869
		}
sl@0
   870
	}
sl@0
   871
sl@0
   872
sl@0
   873
static void TransmitOnlyTransfer()
sl@0
   874
	{
sl@0
   875
	printf("* Transmit-only transfers (OUT).\n");
sl@0
   876
sl@0
   877
	// First (and only once) we get the transfer length (+ the packet size)
sl@0
   878
	GetLength();
sl@0
   879
sl@0
   880
	T_0 = time(NULL);										// starting time
sl@0
   881
sl@0
   882
	while (dwRC == USBIO_ERR_SUCCESS)
sl@0
   883
		{
sl@0
   884
		static DWORD n = 0;
sl@0
   885
		// First the packet number is put into the first four bytes
sl@0
   886
		*(DWORD *)&Data = n++;
sl@0
   887
		// Then we write 'Length' bytes
sl@0
   888
		WriteData();
sl@0
   889
sl@0
   890
		// Finally, sometimes we print some statistics
sl@0
   891
		if ((n % KUniModeDisplayUpdate) == 0)
sl@0
   892
			PrintUnidirStats();
sl@0
   893
		}
sl@0
   894
	}
sl@0
   895
sl@0
   896
sl@0
   897
static void DoTransfers()
sl@0
   898
	{
sl@0
   899
	switch (TransferMode)
sl@0
   900
		{
sl@0
   901
	case ELoop:
sl@0
   902
	case ELoopDebug:
sl@0
   903
		LoopTransfer();
sl@0
   904
		break;
sl@0
   905
	case EReceiveOnly:
sl@0
   906
		ReceiveOnlyTransfer();
sl@0
   907
		break;
sl@0
   908
	case ETransmitOnly:
sl@0
   909
		TransmitOnlyTransfer();
sl@0
   910
		break;
sl@0
   911
	default:
sl@0
   912
		dwRC = -1;
sl@0
   913
		break;
sl@0
   914
		}
sl@0
   915
	}
sl@0
   916
sl@0
   917
sl@0
   918
static void Delay(int milliseconds)
sl@0
   919
	{
sl@0
   920
	printf("* Short wait... ");
sl@0
   921
	Sleep(milliseconds);
sl@0
   922
	printf("done.\n");
sl@0
   923
	}
sl@0
   924
sl@0
   925
sl@0
   926
static void PrintHello()
sl@0
   927
	{
sl@0
   928
	printf("*--------------------------------------------------\n");
sl@0
   929
	printf("* USBRFLCT v%d.%d.%d (for use with USBRFLCT.SYS v%d.%d)\n",
sl@0
   930
		   VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO,
sl@0
   931
		   USBIO_VERSION_MAJOR, USBIO_VERSION_MINOR);
sl@0
   932
	printf("* USB Reflector Test Program / T_USB Host-side Part\n");
sl@0
   933
	printf("*   Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).\n");
sl@0
   934
	printf("*--------------------------------------------------\n");
sl@0
   935
	}
sl@0
   936
sl@0
   937
sl@0
   938
int main(int argc, char* argv[])
sl@0
   939
	{
sl@0
   940
	PrintHello();
sl@0
   941
sl@0
   942
	if (ProcessCmdLine(argc, argv) != 0)
sl@0
   943
		return -1;
sl@0
   944
sl@0
   945
	OpenUsbDevice();
sl@0
   946
sl@0
   947
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   948
		{
sl@0
   949
		GetDeviceDescriptor();
sl@0
   950
		}
sl@0
   951
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   952
		{
sl@0
   953
		GetConfigurationDescriptor();
sl@0
   954
		}
sl@0
   955
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   956
		{
sl@0
   957
		GetStringDescriptor();
sl@0
   958
		}
sl@0
   959
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   960
		{
sl@0
   961
		SetConfiguration();
sl@0
   962
		}
sl@0
   963
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   964
		{
sl@0
   965
		// In order to give the USB device-side program (t_usb)
sl@0
   966
		// enough time after getting configured to carry out
sl@0
   967
		// some device tests, we wait here for a short while
sl@0
   968
		// before proceeding:
sl@0
   969
		Delay(2000);
sl@0
   970
		GetConfigurationInfo();
sl@0
   971
		}
sl@0
   972
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   973
		{
sl@0
   974
		OpenPipes();
sl@0
   975
		}
sl@0
   976
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   977
		{
sl@0
   978
		ExchangeVersions();
sl@0
   979
		}
sl@0
   980
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   981
		{
sl@0
   982
		DoTransfers();
sl@0
   983
		}
sl@0
   984
	if (dwRC == USBIO_ERR_SUCCESS)
sl@0
   985
		{
sl@0
   986
		ClosePipes();
sl@0
   987
		}
sl@0
   988
sl@0
   989
	CloseUsbDevice();
sl@0
   990
sl@0
   991
	return 0;
sl@0
   992
	}
sl@0
   993
sl@0
   994
sl@0
   995
// --eof