os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/UsbcvControl.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // UsbcvControl.cpp: implementation of the UsbcvControl class.
    15 //
    16 
    17 #include "stdafx.h"
    18 #include "t_usb_win.h"
    19 #include "UsbcvControl.h"
    20 
    21 #include "usbio.h"											// USBIO Dev Kit
    22 
    23 #include "global.h"
    24 
    25 #ifdef _DEBUG
    26 #undef THIS_FILE
    27 static char THIS_FILE[]=__FILE__;
    28 #define new DEBUG_NEW
    29 #endif
    30 
    31 extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
    32 
    33 extern BOOL gAbort;
    34 
    35 // define the CreateProcess strings for the Usbcv test app
    36 #define APPNAME "C:\\Program Files\\USB-IF Test Suite\\USBCommandVerifier\\UsbCV13.exe"
    37 #define APPTITLE "Automated USBCV"
    38 #define CMDLINE " /title \"Automated USBCV\" /drv hcdriver"
    39 #define CURDIR "C:\\Program Files\\USB-IF Test Suite\\USBCommandVerifier\\lib"
    40 
    41 #define DIALOG_CLASS "#32770"
    42 #define BUTTON_CLASS "Button"
    43 #define STATIC_CLASS "Static"
    44 
    45 #define ID_COMPLIANCE 0x3EC
    46 #define ID_RUN 0x3F5
    47 #define ID_EXIT 0x1
    48 
    49 #define DEVLIST_TITLE ""
    50 #define ID_OKBUTTON 0x3E8
    51 #define ID_TESTLIST 0x3E9
    52 #define ID_DEVLIST 0x3EA
    53 #define ID_TEXTBOX 0x3EB
    54 #define ID_CONFIRM 0x3EC
    55 #define ID_ABORT 0x3ED
    56 
    57 #define TESTRESULT_TITLE "Test Results"
    58 #define ID_RESULT_TEXT 0xFFFF
    59 #define ID_RESULT_OK 0x2
    60 
    61 #define BUFFER_SIZE 256
    62 #define VIDPID_SIZE	16
    63 #define TEST_PASSED "Passed"
    64 #define TEST_FAILED "Failed"
    65 
    66 // Wait sleep in milliseconds, other waits as repetitions of the sleep time
    67 #define WAIT_SLEEP 250					
    68 #define CREATE_WAIT 40
    69 #define SELECT_WAIT 20
    70 #define CONTINUE_WAIT 120
    71 #define RESULT_WAIT	1200
    72 #define EXIT_WAIT 40
    73 
    74 #define USBMS_DEVICE_REQUEST_CONTINUES	6
    75 
    76 //////////////////////////////////////////////////////////////////////
    77 // Construction/Destruction
    78 //////////////////////////////////////////////////////////////////////
    79 
    80 UsbcvControl::UsbcvControl()
    81 {
    82 
    83     ZeroMemory( &si, sizeof(si) );
    84     si.cb = sizeof(si);
    85     ZeroMemory( &pi, sizeof(pi) );
    86 
    87 }
    88 
    89 UsbcvControl::~UsbcvControl()
    90 {
    91 	DWORD exitCode;
    92 	
    93 	if (GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode))
    94 		{
    95 		if (exitCode == STILL_ACTIVE)
    96 			{
    97 			SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_EXIT,BN_CLICKED),(LPARAM)exitButton);
    98 			}
    99 		int i = 0;
   100 		while (exitCode == STILL_ACTIVE && i++ < EXIT_WAIT)
   101 			{
   102 			Sleep (WAIT_SLEEP);
   103 			GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode);
   104 			}
   105 		// Force an unclean process termination only if necessary
   106 		if (exitCode == STILL_ACTIVE)
   107 			{
   108 			TerminateProcess(pi.hProcess,0);
   109 			}
   110 		}	
   111 }
   112 
   113 DWORD UsbcvControl::Create()
   114 {
   115 	mainWindow = NULL;
   116 	exitButton = NULL;
   117 
   118 	if (!CreateProcess (APPNAME,CMDLINE,NULL,NULL,FALSE,0,NULL,CURDIR,&si,&pi))
   119 		return USBIO_ERR_FAILED;
   120 
   121 	HWND complianceButton;
   122 
   123 	for (int i=0; i < CREATE_WAIT && !mainWindow; i++)
   124 		{
   125 		mainWindow = FindWindow(DIALOG_CLASS,APPTITLE);
   126 		if (mainWindow)
   127 			{
   128 			complianceButton = GetDlgItem(mainWindow,ID_COMPLIANCE);
   129 			runButton = GetDlgItem(mainWindow,ID_RUN);
   130 			exitButton = GetDlgItem(mainWindow,ID_EXIT);	
   131 			if (!complianceButton || !runButton || !exitButton)
   132 				{
   133 				mainWindow = NULL;
   134 				}
   135 			}
   136 		if (!mainWindow)
   137 			Sleep(WAIT_SLEEP);
   138 		}
   139 
   140 	if (mainWindow)
   141 		{
   142 		SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_COMPLIANCE,BN_CLICKED),(LPARAM)complianceButton);
   143 
   144 		return USBIO_ERR_SUCCESS;
   145 		}
   146 	else
   147 		{
   148 		return USBIO_ERR_FAILED;
   149 		}
   150 }
   151 
   152 DWORD UsbcvControl::TestAllDevices(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2)
   153 {
   154 	DWORD dwRC = USBIO_ERR_SUCCESS;
   155 	HWND testList;
   156 	int chapter9_TestIndex = -1;
   157 	int MSC_TestIndex = -1;
   158 	LRESULT lResult;
   159 
   160 	testList = GetDlgItem(mainWindow,ID_TESTLIST);
   161 
   162 	LRESULT itemCount = SendMessage (testList,LB_GETCOUNT,0,0);
   163 	if (!itemCount)
   164 		return USBIO_ERR_FAILED;
   165 
   166 	char strBuffer[BUFFER_SIZE];
   167 	for (int i = 0; i < itemCount; i++)
   168 		{
   169 		LRESULT lResult = SendMessage (testList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
   170 		if (lResult > 0)
   171 			{
   172 			if (strstr (strBuffer,"Chapter 9 Tests"))
   173 				chapter9_TestIndex = i;
   174 			if (strstr (strBuffer,"MSC Tests"))
   175 				MSC_TestIndex = i;
   176 			}
   177 		}
   178 
   179 		
   180 	if (chapter9_TestIndex < 0 || MSC_TestIndex < 0)
   181 		return USBIO_ERR_FAILED;
   182 
   183 
   184 	int ch9DeviceIndex = 0;
   185 	int numCh9Devices = 1;
   186 	int mscDeviceIndex = 0;
   187 	int numMscDevices = 1;
   188 	int testResult;
   189 	bool msDevice = false;
   190 	while (numCh9Devices > 0 && dwRC == USBIO_ERR_SUCCESS)
   191 		{
   192 		// select the chapter 9 tests
   193 		lResult = SendMessage (testList,LB_SETCURSEL,(WPARAM)chapter9_TestIndex,0);
   194 		lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_TESTLIST,LBN_SELCHANGE),(LPARAM)testList);
   195 
   196 		// then run it
   197 		lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
   198 
   199 		dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&ch9DeviceIndex,&numCh9Devices,&msDevice);
   200 		if (ch9DeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
   201 			{
   202 
   203 			dwRC = WaitForTestResult(&testResult);
   204 			if (dwRC == USBIO_ERR_SUCCESS)
   205 				{
   206 				if (!testResult)
   207 					{
   208 					gAbort = TRUE;
   209 					}
   210 				PRINT_TIME "USBCV Chapter 9 Test Complete - %s  ",testResult ? "Passed" : "Failed");
   211 				ch9DeviceIndex++;
   212 				}
   213 			else
   214 				{
   215 				PRINT_ALWAYS "Error in Running USBCV\n");
   216 				return dwRC;
   217 				}
   218 			}
   219 
   220 		if (msDevice && numMscDevices > 0 && dwRC == USBIO_ERR_SUCCESS)
   221 			{
   222 			// select the mass storage tests
   223 			lResult = SendMessage (testList,LB_SETCURSEL,(WPARAM)MSC_TestIndex,0);
   224 			lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_TESTLIST,LBN_SELCHANGE),(LPARAM)testList);
   225 
   226 			// then run it
   227 			lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
   228 
   229 			if (dwRC == USBIO_ERR_SUCCESS)
   230 				dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&mscDeviceIndex,&numMscDevices,&msDevice);
   231 			if (mscDeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
   232 				{
   233 				dwRC = ContinueOk ("The following test might destroy",ID_CONFIRM);
   234 				for (int i = 0; i < USBMS_DEVICE_REQUEST_CONTINUES; i++)
   235 					{
   236 					if (dwRC == USBIO_ERR_SUCCESS)
   237 						dwRC = ContinueOk ("Processing device request",ID_OKBUTTON);
   238 					}
   239 				if (dwRC == USBIO_ERR_SUCCESS)
   240 					dwRC = ContinueOk ("Disconnect and power off",ID_CONFIRM);
   241 
   242 				if (dwRC == USBIO_ERR_SUCCESS)
   243 					{
   244 					dwRC = WaitForTestResult(&testResult);
   245 					if (dwRC == USBIO_ERR_SUCCESS)
   246 						{
   247 						if (!testResult)
   248 							{
   249 							gAbort = TRUE;
   250 							}
   251 						PRINT_TIME "USBCV Mass Storage Class Test Complete - %s  ",testResult ? "Passed" : "Failed");
   252 						mscDeviceIndex++;
   253 						}
   254 					else
   255 						{
   256 						PRINT_ALWAYS "Error in Running USBCV\n");
   257 						return dwRC;
   258 						}
   259 					}
   260 				}
   261 			}
   262 		}
   263 
   264 	return dwRC;
   265 }
   266 
   267 DWORD UsbcvControl::ContinueOk (char * choiceText, int buttonId)
   268 {
   269 	HWND choiceDialog = NULL;
   270 	HWND okButton;
   271 	char strBuffer[BUFFER_SIZE];
   272 
   273 	for (int i=0; i < CONTINUE_WAIT && !choiceDialog; i++)
   274 		{
   275 		choiceDialog = FindWindow(DIALOG_CLASS,"");
   276 		if (choiceDialog)
   277 			{
   278 			UINT textSize = GetDlgItemText (choiceDialog,ID_TEXTBOX,strBuffer,BUFFER_SIZE);
   279 			okButton = GetDlgItem(choiceDialog,buttonId);
   280 			if (!textSize || !okButton || !strstr (strBuffer,choiceText))
   281 				{
   282 				choiceDialog = NULL;
   283 				}
   284 			}
   285 		if (!choiceDialog)
   286 			Sleep(WAIT_SLEEP);
   287 		}
   288 
   289 	if (!choiceDialog)
   290 		return USBIO_ERR_FAILED;
   291 
   292 	SendMessage (choiceDialog,WM_COMMAND,MAKEWPARAM(buttonId,BN_CLICKED),(LPARAM)okButton);
   293 
   294 	return USBIO_ERR_SUCCESS;
   295 
   296 }
   297 
   298 DWORD UsbcvControl::SelectDevice(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2, int * deviceIndex, int * numDevices, bool * msDevice)
   299 {
   300 	HWND devListDialog = NULL;
   301 	HWND devList;
   302 	HWND okButton;
   303 	LRESULT lResult;
   304 	int i;
   305 
   306 	for (i=0; i < SELECT_WAIT && !devListDialog; i++)
   307 		{
   308 		devListDialog = FindWindow(DIALOG_CLASS,DEVLIST_TITLE);
   309 		if (devListDialog)
   310 			{
   311 			devList = GetDlgItem(devListDialog,ID_DEVLIST);
   312 			okButton = GetDlgItem(devListDialog,ID_OKBUTTON);
   313 			if (!devList || !okButton)
   314 				{
   315 				devListDialog = NULL;
   316 				}
   317 			}
   318 		if (!devListDialog)
   319 			Sleep(WAIT_SLEEP);
   320 		}
   321 
   322 	if (!devListDialog)
   323 		return USBIO_ERR_FAILED;
   324 
   325 	LRESULT itemCount = SendMessage (devList,LB_GETCOUNT,0,0);
   326 	if (!itemCount)
   327 		return USBIO_ERR_FAILED;
   328 
   329 	* numDevices = 0;
   330 	if (* deviceIndex >= itemCount)
   331 		return USBIO_ERR_SUCCESS;
   332 
   333 	char strBuffer[BUFFER_SIZE];
   334 	char strVID [VIDPID_SIZE];
   335 	char strPID1 [VIDPID_SIZE];
   336 	char strPID2 [VIDPID_SIZE];
   337 	bool deviceFound = false;
   338 
   339 	for (i = * deviceIndex; i < itemCount; i++)
   340 		{
   341 		lResult = SendMessage (devList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
   342 		if (lResult > 0)
   343 			{
   344 			sprintf (strVID,"VID=%04x",aVendorId);
   345 			sprintf (strPID1,"PID=%04x",aProductId1);
   346 			sprintf (strPID2,"PID=%04x",aProductId2);
   347 			if (strstr (strBuffer,strVID) && (strstr (strBuffer,strPID1) || strstr (strBuffer,strPID2)))
   348 				{
   349 				if (deviceFound)
   350 					{
   351 					(* numDevices)++;
   352 					}
   353 				else
   354 					{
   355 					deviceFound = true;
   356 					* msDevice = strstr (strBuffer,strPID2) != NULL;
   357 					* deviceIndex = i;
   358 					lResult = SendMessage (devList,LB_SETCURSEL,(WPARAM)i,0);
   359 					}
   360 				}
   361 			}
   362 		}
   363 
   364 
   365 	if (deviceFound)
   366 		{
   367 		if (lResult == LB_ERR)
   368 			{
   369 			return USBIO_ERR_FAILED;
   370 			}
   371 		else
   372 			{
   373 			lResult = SendMessage (devListDialog,WM_COMMAND,MAKEWPARAM(ID_OKBUTTON,BN_CLICKED),(LPARAM)okButton);
   374 
   375 			return USBIO_ERR_SUCCESS;
   376 			}
   377 		}
   378 	else
   379 		{
   380 		* deviceIndex = -1;
   381 
   382 		return USBIO_ERR_FAILED;
   383 		}
   384 
   385 }
   386 
   387 
   388 DWORD UsbcvControl::WaitForTestResult(BOOL * passFail)
   389 {
   390 	HWND testResultDialog = NULL;
   391 	HWND okButton;
   392 	char strBuffer[BUFFER_SIZE];
   393 
   394 	for (int i=0; i < RESULT_WAIT && !testResultDialog; i++)
   395 		{
   396 		testResultDialog = FindWindow(DIALOG_CLASS,TESTRESULT_TITLE);
   397 		if (testResultDialog)
   398 			{
   399 			UINT textSize = GetDlgItemText(testResultDialog,ID_RESULT_TEXT,(LPTSTR)&strBuffer,BUFFER_SIZE);
   400 			okButton = GetDlgItem(testResultDialog,ID_RESULT_OK);
   401 			if (!textSize || !okButton)
   402 				{
   403 				testResultDialog = NULL;
   404 				}
   405 			}
   406 		if (!testResultDialog)
   407 			Sleep(WAIT_SLEEP);
   408 		}
   409 
   410 	if (!testResultDialog)
   411 		return USBIO_ERR_FAILED;
   412 
   413 	if (strstr (strBuffer,TEST_PASSED))
   414 		{
   415 		* passFail = TRUE;
   416 		}
   417 	else
   418 		{
   419 		if (strstr (strBuffer,TEST_FAILED))
   420 			{
   421 			* passFail = FALSE;
   422 			}
   423 		else
   424 			{
   425 			return USBIO_ERR_FAILED;
   426 			}
   427 		}
   428 
   429 	SendMessage (testResultDialog,WM_COMMAND,MAKEWPARAM(ID_RESULT_OK,BN_CLICKED),(LPARAM)okButton);
   430 
   431 	return USBIO_ERR_SUCCESS;
   432 
   433 }