os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/UsbcvControl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/UsbcvControl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,433 @@
     1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// UsbcvControl.cpp: implementation of the UsbcvControl class.
    1.18 +//
    1.19 +
    1.20 +#include "stdafx.h"
    1.21 +#include "t_usb_win.h"
    1.22 +#include "UsbcvControl.h"
    1.23 +
    1.24 +#include "usbio.h"											// USBIO Dev Kit
    1.25 +
    1.26 +#include "global.h"
    1.27 +
    1.28 +#ifdef _DEBUG
    1.29 +#undef THIS_FILE
    1.30 +static char THIS_FILE[]=__FILE__;
    1.31 +#define new DEBUG_NEW
    1.32 +#endif
    1.33 +
    1.34 +extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
    1.35 +
    1.36 +extern BOOL gAbort;
    1.37 +
    1.38 +// define the CreateProcess strings for the Usbcv test app
    1.39 +#define APPNAME "C:\\Program Files\\USB-IF Test Suite\\USBCommandVerifier\\UsbCV13.exe"
    1.40 +#define APPTITLE "Automated USBCV"
    1.41 +#define CMDLINE " /title \"Automated USBCV\" /drv hcdriver"
    1.42 +#define CURDIR "C:\\Program Files\\USB-IF Test Suite\\USBCommandVerifier\\lib"
    1.43 +
    1.44 +#define DIALOG_CLASS "#32770"
    1.45 +#define BUTTON_CLASS "Button"
    1.46 +#define STATIC_CLASS "Static"
    1.47 +
    1.48 +#define ID_COMPLIANCE 0x3EC
    1.49 +#define ID_RUN 0x3F5
    1.50 +#define ID_EXIT 0x1
    1.51 +
    1.52 +#define DEVLIST_TITLE ""
    1.53 +#define ID_OKBUTTON 0x3E8
    1.54 +#define ID_TESTLIST 0x3E9
    1.55 +#define ID_DEVLIST 0x3EA
    1.56 +#define ID_TEXTBOX 0x3EB
    1.57 +#define ID_CONFIRM 0x3EC
    1.58 +#define ID_ABORT 0x3ED
    1.59 +
    1.60 +#define TESTRESULT_TITLE "Test Results"
    1.61 +#define ID_RESULT_TEXT 0xFFFF
    1.62 +#define ID_RESULT_OK 0x2
    1.63 +
    1.64 +#define BUFFER_SIZE 256
    1.65 +#define VIDPID_SIZE	16
    1.66 +#define TEST_PASSED "Passed"
    1.67 +#define TEST_FAILED "Failed"
    1.68 +
    1.69 +// Wait sleep in milliseconds, other waits as repetitions of the sleep time
    1.70 +#define WAIT_SLEEP 250					
    1.71 +#define CREATE_WAIT 40
    1.72 +#define SELECT_WAIT 20
    1.73 +#define CONTINUE_WAIT 120
    1.74 +#define RESULT_WAIT	1200
    1.75 +#define EXIT_WAIT 40
    1.76 +
    1.77 +#define USBMS_DEVICE_REQUEST_CONTINUES	6
    1.78 +
    1.79 +//////////////////////////////////////////////////////////////////////
    1.80 +// Construction/Destruction
    1.81 +//////////////////////////////////////////////////////////////////////
    1.82 +
    1.83 +UsbcvControl::UsbcvControl()
    1.84 +{
    1.85 +
    1.86 +    ZeroMemory( &si, sizeof(si) );
    1.87 +    si.cb = sizeof(si);
    1.88 +    ZeroMemory( &pi, sizeof(pi) );
    1.89 +
    1.90 +}
    1.91 +
    1.92 +UsbcvControl::~UsbcvControl()
    1.93 +{
    1.94 +	DWORD exitCode;
    1.95 +	
    1.96 +	if (GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode))
    1.97 +		{
    1.98 +		if (exitCode == STILL_ACTIVE)
    1.99 +			{
   1.100 +			SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_EXIT,BN_CLICKED),(LPARAM)exitButton);
   1.101 +			}
   1.102 +		int i = 0;
   1.103 +		while (exitCode == STILL_ACTIVE && i++ < EXIT_WAIT)
   1.104 +			{
   1.105 +			Sleep (WAIT_SLEEP);
   1.106 +			GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode);
   1.107 +			}
   1.108 +		// Force an unclean process termination only if necessary
   1.109 +		if (exitCode == STILL_ACTIVE)
   1.110 +			{
   1.111 +			TerminateProcess(pi.hProcess,0);
   1.112 +			}
   1.113 +		}	
   1.114 +}
   1.115 +
   1.116 +DWORD UsbcvControl::Create()
   1.117 +{
   1.118 +	mainWindow = NULL;
   1.119 +	exitButton = NULL;
   1.120 +
   1.121 +	if (!CreateProcess (APPNAME,CMDLINE,NULL,NULL,FALSE,0,NULL,CURDIR,&si,&pi))
   1.122 +		return USBIO_ERR_FAILED;
   1.123 +
   1.124 +	HWND complianceButton;
   1.125 +
   1.126 +	for (int i=0; i < CREATE_WAIT && !mainWindow; i++)
   1.127 +		{
   1.128 +		mainWindow = FindWindow(DIALOG_CLASS,APPTITLE);
   1.129 +		if (mainWindow)
   1.130 +			{
   1.131 +			complianceButton = GetDlgItem(mainWindow,ID_COMPLIANCE);
   1.132 +			runButton = GetDlgItem(mainWindow,ID_RUN);
   1.133 +			exitButton = GetDlgItem(mainWindow,ID_EXIT);	
   1.134 +			if (!complianceButton || !runButton || !exitButton)
   1.135 +				{
   1.136 +				mainWindow = NULL;
   1.137 +				}
   1.138 +			}
   1.139 +		if (!mainWindow)
   1.140 +			Sleep(WAIT_SLEEP);
   1.141 +		}
   1.142 +
   1.143 +	if (mainWindow)
   1.144 +		{
   1.145 +		SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_COMPLIANCE,BN_CLICKED),(LPARAM)complianceButton);
   1.146 +
   1.147 +		return USBIO_ERR_SUCCESS;
   1.148 +		}
   1.149 +	else
   1.150 +		{
   1.151 +		return USBIO_ERR_FAILED;
   1.152 +		}
   1.153 +}
   1.154 +
   1.155 +DWORD UsbcvControl::TestAllDevices(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2)
   1.156 +{
   1.157 +	DWORD dwRC = USBIO_ERR_SUCCESS;
   1.158 +	HWND testList;
   1.159 +	int chapter9_TestIndex = -1;
   1.160 +	int MSC_TestIndex = -1;
   1.161 +	LRESULT lResult;
   1.162 +
   1.163 +	testList = GetDlgItem(mainWindow,ID_TESTLIST);
   1.164 +
   1.165 +	LRESULT itemCount = SendMessage (testList,LB_GETCOUNT,0,0);
   1.166 +	if (!itemCount)
   1.167 +		return USBIO_ERR_FAILED;
   1.168 +
   1.169 +	char strBuffer[BUFFER_SIZE];
   1.170 +	for (int i = 0; i < itemCount; i++)
   1.171 +		{
   1.172 +		LRESULT lResult = SendMessage (testList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
   1.173 +		if (lResult > 0)
   1.174 +			{
   1.175 +			if (strstr (strBuffer,"Chapter 9 Tests"))
   1.176 +				chapter9_TestIndex = i;
   1.177 +			if (strstr (strBuffer,"MSC Tests"))
   1.178 +				MSC_TestIndex = i;
   1.179 +			}
   1.180 +		}
   1.181 +
   1.182 +		
   1.183 +	if (chapter9_TestIndex < 0 || MSC_TestIndex < 0)
   1.184 +		return USBIO_ERR_FAILED;
   1.185 +
   1.186 +
   1.187 +	int ch9DeviceIndex = 0;
   1.188 +	int numCh9Devices = 1;
   1.189 +	int mscDeviceIndex = 0;
   1.190 +	int numMscDevices = 1;
   1.191 +	int testResult;
   1.192 +	bool msDevice = false;
   1.193 +	while (numCh9Devices > 0 && dwRC == USBIO_ERR_SUCCESS)
   1.194 +		{
   1.195 +		// select the chapter 9 tests
   1.196 +		lResult = SendMessage (testList,LB_SETCURSEL,(WPARAM)chapter9_TestIndex,0);
   1.197 +		lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_TESTLIST,LBN_SELCHANGE),(LPARAM)testList);
   1.198 +
   1.199 +		// then run it
   1.200 +		lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
   1.201 +
   1.202 +		dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&ch9DeviceIndex,&numCh9Devices,&msDevice);
   1.203 +		if (ch9DeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
   1.204 +			{
   1.205 +
   1.206 +			dwRC = WaitForTestResult(&testResult);
   1.207 +			if (dwRC == USBIO_ERR_SUCCESS)
   1.208 +				{
   1.209 +				if (!testResult)
   1.210 +					{
   1.211 +					gAbort = TRUE;
   1.212 +					}
   1.213 +				PRINT_TIME "USBCV Chapter 9 Test Complete - %s  ",testResult ? "Passed" : "Failed");
   1.214 +				ch9DeviceIndex++;
   1.215 +				}
   1.216 +			else
   1.217 +				{
   1.218 +				PRINT_ALWAYS "Error in Running USBCV\n");
   1.219 +				return dwRC;
   1.220 +				}
   1.221 +			}
   1.222 +
   1.223 +		if (msDevice && numMscDevices > 0 && dwRC == USBIO_ERR_SUCCESS)
   1.224 +			{
   1.225 +			// select the mass storage tests
   1.226 +			lResult = SendMessage (testList,LB_SETCURSEL,(WPARAM)MSC_TestIndex,0);
   1.227 +			lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_TESTLIST,LBN_SELCHANGE),(LPARAM)testList);
   1.228 +
   1.229 +			// then run it
   1.230 +			lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
   1.231 +
   1.232 +			if (dwRC == USBIO_ERR_SUCCESS)
   1.233 +				dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&mscDeviceIndex,&numMscDevices,&msDevice);
   1.234 +			if (mscDeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
   1.235 +				{
   1.236 +				dwRC = ContinueOk ("The following test might destroy",ID_CONFIRM);
   1.237 +				for (int i = 0; i < USBMS_DEVICE_REQUEST_CONTINUES; i++)
   1.238 +					{
   1.239 +					if (dwRC == USBIO_ERR_SUCCESS)
   1.240 +						dwRC = ContinueOk ("Processing device request",ID_OKBUTTON);
   1.241 +					}
   1.242 +				if (dwRC == USBIO_ERR_SUCCESS)
   1.243 +					dwRC = ContinueOk ("Disconnect and power off",ID_CONFIRM);
   1.244 +
   1.245 +				if (dwRC == USBIO_ERR_SUCCESS)
   1.246 +					{
   1.247 +					dwRC = WaitForTestResult(&testResult);
   1.248 +					if (dwRC == USBIO_ERR_SUCCESS)
   1.249 +						{
   1.250 +						if (!testResult)
   1.251 +							{
   1.252 +							gAbort = TRUE;
   1.253 +							}
   1.254 +						PRINT_TIME "USBCV Mass Storage Class Test Complete - %s  ",testResult ? "Passed" : "Failed");
   1.255 +						mscDeviceIndex++;
   1.256 +						}
   1.257 +					else
   1.258 +						{
   1.259 +						PRINT_ALWAYS "Error in Running USBCV\n");
   1.260 +						return dwRC;
   1.261 +						}
   1.262 +					}
   1.263 +				}
   1.264 +			}
   1.265 +		}
   1.266 +
   1.267 +	return dwRC;
   1.268 +}
   1.269 +
   1.270 +DWORD UsbcvControl::ContinueOk (char * choiceText, int buttonId)
   1.271 +{
   1.272 +	HWND choiceDialog = NULL;
   1.273 +	HWND okButton;
   1.274 +	char strBuffer[BUFFER_SIZE];
   1.275 +
   1.276 +	for (int i=0; i < CONTINUE_WAIT && !choiceDialog; i++)
   1.277 +		{
   1.278 +		choiceDialog = FindWindow(DIALOG_CLASS,"");
   1.279 +		if (choiceDialog)
   1.280 +			{
   1.281 +			UINT textSize = GetDlgItemText (choiceDialog,ID_TEXTBOX,strBuffer,BUFFER_SIZE);
   1.282 +			okButton = GetDlgItem(choiceDialog,buttonId);
   1.283 +			if (!textSize || !okButton || !strstr (strBuffer,choiceText))
   1.284 +				{
   1.285 +				choiceDialog = NULL;
   1.286 +				}
   1.287 +			}
   1.288 +		if (!choiceDialog)
   1.289 +			Sleep(WAIT_SLEEP);
   1.290 +		}
   1.291 +
   1.292 +	if (!choiceDialog)
   1.293 +		return USBIO_ERR_FAILED;
   1.294 +
   1.295 +	SendMessage (choiceDialog,WM_COMMAND,MAKEWPARAM(buttonId,BN_CLICKED),(LPARAM)okButton);
   1.296 +
   1.297 +	return USBIO_ERR_SUCCESS;
   1.298 +
   1.299 +}
   1.300 +
   1.301 +DWORD UsbcvControl::SelectDevice(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2, int * deviceIndex, int * numDevices, bool * msDevice)
   1.302 +{
   1.303 +	HWND devListDialog = NULL;
   1.304 +	HWND devList;
   1.305 +	HWND okButton;
   1.306 +	LRESULT lResult;
   1.307 +	int i;
   1.308 +
   1.309 +	for (i=0; i < SELECT_WAIT && !devListDialog; i++)
   1.310 +		{
   1.311 +		devListDialog = FindWindow(DIALOG_CLASS,DEVLIST_TITLE);
   1.312 +		if (devListDialog)
   1.313 +			{
   1.314 +			devList = GetDlgItem(devListDialog,ID_DEVLIST);
   1.315 +			okButton = GetDlgItem(devListDialog,ID_OKBUTTON);
   1.316 +			if (!devList || !okButton)
   1.317 +				{
   1.318 +				devListDialog = NULL;
   1.319 +				}
   1.320 +			}
   1.321 +		if (!devListDialog)
   1.322 +			Sleep(WAIT_SLEEP);
   1.323 +		}
   1.324 +
   1.325 +	if (!devListDialog)
   1.326 +		return USBIO_ERR_FAILED;
   1.327 +
   1.328 +	LRESULT itemCount = SendMessage (devList,LB_GETCOUNT,0,0);
   1.329 +	if (!itemCount)
   1.330 +		return USBIO_ERR_FAILED;
   1.331 +
   1.332 +	* numDevices = 0;
   1.333 +	if (* deviceIndex >= itemCount)
   1.334 +		return USBIO_ERR_SUCCESS;
   1.335 +
   1.336 +	char strBuffer[BUFFER_SIZE];
   1.337 +	char strVID [VIDPID_SIZE];
   1.338 +	char strPID1 [VIDPID_SIZE];
   1.339 +	char strPID2 [VIDPID_SIZE];
   1.340 +	bool deviceFound = false;
   1.341 +
   1.342 +	for (i = * deviceIndex; i < itemCount; i++)
   1.343 +		{
   1.344 +		lResult = SendMessage (devList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
   1.345 +		if (lResult > 0)
   1.346 +			{
   1.347 +			sprintf (strVID,"VID=%04x",aVendorId);
   1.348 +			sprintf (strPID1,"PID=%04x",aProductId1);
   1.349 +			sprintf (strPID2,"PID=%04x",aProductId2);
   1.350 +			if (strstr (strBuffer,strVID) && (strstr (strBuffer,strPID1) || strstr (strBuffer,strPID2)))
   1.351 +				{
   1.352 +				if (deviceFound)
   1.353 +					{
   1.354 +					(* numDevices)++;
   1.355 +					}
   1.356 +				else
   1.357 +					{
   1.358 +					deviceFound = true;
   1.359 +					* msDevice = strstr (strBuffer,strPID2) != NULL;
   1.360 +					* deviceIndex = i;
   1.361 +					lResult = SendMessage (devList,LB_SETCURSEL,(WPARAM)i,0);
   1.362 +					}
   1.363 +				}
   1.364 +			}
   1.365 +		}
   1.366 +
   1.367 +
   1.368 +	if (deviceFound)
   1.369 +		{
   1.370 +		if (lResult == LB_ERR)
   1.371 +			{
   1.372 +			return USBIO_ERR_FAILED;
   1.373 +			}
   1.374 +		else
   1.375 +			{
   1.376 +			lResult = SendMessage (devListDialog,WM_COMMAND,MAKEWPARAM(ID_OKBUTTON,BN_CLICKED),(LPARAM)okButton);
   1.377 +
   1.378 +			return USBIO_ERR_SUCCESS;
   1.379 +			}
   1.380 +		}
   1.381 +	else
   1.382 +		{
   1.383 +		* deviceIndex = -1;
   1.384 +
   1.385 +		return USBIO_ERR_FAILED;
   1.386 +		}
   1.387 +
   1.388 +}
   1.389 +
   1.390 +
   1.391 +DWORD UsbcvControl::WaitForTestResult(BOOL * passFail)
   1.392 +{
   1.393 +	HWND testResultDialog = NULL;
   1.394 +	HWND okButton;
   1.395 +	char strBuffer[BUFFER_SIZE];
   1.396 +
   1.397 +	for (int i=0; i < RESULT_WAIT && !testResultDialog; i++)
   1.398 +		{
   1.399 +		testResultDialog = FindWindow(DIALOG_CLASS,TESTRESULT_TITLE);
   1.400 +		if (testResultDialog)
   1.401 +			{
   1.402 +			UINT textSize = GetDlgItemText(testResultDialog,ID_RESULT_TEXT,(LPTSTR)&strBuffer,BUFFER_SIZE);
   1.403 +			okButton = GetDlgItem(testResultDialog,ID_RESULT_OK);
   1.404 +			if (!textSize || !okButton)
   1.405 +				{
   1.406 +				testResultDialog = NULL;
   1.407 +				}
   1.408 +			}
   1.409 +		if (!testResultDialog)
   1.410 +			Sleep(WAIT_SLEEP);
   1.411 +		}
   1.412 +
   1.413 +	if (!testResultDialog)
   1.414 +		return USBIO_ERR_FAILED;
   1.415 +
   1.416 +	if (strstr (strBuffer,TEST_PASSED))
   1.417 +		{
   1.418 +		* passFail = TRUE;
   1.419 +		}
   1.420 +	else
   1.421 +		{
   1.422 +		if (strstr (strBuffer,TEST_FAILED))
   1.423 +			{
   1.424 +			* passFail = FALSE;
   1.425 +			}
   1.426 +		else
   1.427 +			{
   1.428 +			return USBIO_ERR_FAILED;
   1.429 +			}
   1.430 +		}
   1.431 +
   1.432 +	SendMessage (testResultDialog,WM_COMMAND,MAKEWPARAM(ID_RESULT_OK,BN_CLICKED),(LPARAM)okButton);
   1.433 +
   1.434 +	return USBIO_ERR_SUCCESS;
   1.435 +
   1.436 +}