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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // UsbcvControl.cpp: implementation of the UsbcvControl class.
18 #include "t_usb_win.h"
19 #include "UsbcvControl.h"
21 #include "usbio.h" // USBIO Dev Kit
27 static char THIS_FILE[]=__FILE__;
31 extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
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"
41 #define DIALOG_CLASS "#32770"
42 #define BUTTON_CLASS "Button"
43 #define STATIC_CLASS "Static"
45 #define ID_COMPLIANCE 0x3EC
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
57 #define TESTRESULT_TITLE "Test Results"
58 #define ID_RESULT_TEXT 0xFFFF
59 #define ID_RESULT_OK 0x2
61 #define BUFFER_SIZE 256
62 #define VIDPID_SIZE 16
63 #define TEST_PASSED "Passed"
64 #define TEST_FAILED "Failed"
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
74 #define USBMS_DEVICE_REQUEST_CONTINUES 6
76 //////////////////////////////////////////////////////////////////////
77 // Construction/Destruction
78 //////////////////////////////////////////////////////////////////////
80 UsbcvControl::UsbcvControl()
83 ZeroMemory( &si, sizeof(si) );
85 ZeroMemory( &pi, sizeof(pi) );
89 UsbcvControl::~UsbcvControl()
93 if (GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode))
95 if (exitCode == STILL_ACTIVE)
97 SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_EXIT,BN_CLICKED),(LPARAM)exitButton);
100 while (exitCode == STILL_ACTIVE && i++ < EXIT_WAIT)
103 GetExitCodeProcess(pi.hProcess,(LPDWORD)&exitCode);
105 // Force an unclean process termination only if necessary
106 if (exitCode == STILL_ACTIVE)
108 TerminateProcess(pi.hProcess,0);
113 DWORD UsbcvControl::Create()
118 if (!CreateProcess (APPNAME,CMDLINE,NULL,NULL,FALSE,0,NULL,CURDIR,&si,&pi))
119 return USBIO_ERR_FAILED;
121 HWND complianceButton;
123 for (int i=0; i < CREATE_WAIT && !mainWindow; i++)
125 mainWindow = FindWindow(DIALOG_CLASS,APPTITLE);
128 complianceButton = GetDlgItem(mainWindow,ID_COMPLIANCE);
129 runButton = GetDlgItem(mainWindow,ID_RUN);
130 exitButton = GetDlgItem(mainWindow,ID_EXIT);
131 if (!complianceButton || !runButton || !exitButton)
142 SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_COMPLIANCE,BN_CLICKED),(LPARAM)complianceButton);
144 return USBIO_ERR_SUCCESS;
148 return USBIO_ERR_FAILED;
152 DWORD UsbcvControl::TestAllDevices(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2)
154 DWORD dwRC = USBIO_ERR_SUCCESS;
156 int chapter9_TestIndex = -1;
157 int MSC_TestIndex = -1;
160 testList = GetDlgItem(mainWindow,ID_TESTLIST);
162 LRESULT itemCount = SendMessage (testList,LB_GETCOUNT,0,0);
164 return USBIO_ERR_FAILED;
166 char strBuffer[BUFFER_SIZE];
167 for (int i = 0; i < itemCount; i++)
169 LRESULT lResult = SendMessage (testList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
172 if (strstr (strBuffer,"Chapter 9 Tests"))
173 chapter9_TestIndex = i;
174 if (strstr (strBuffer,"MSC Tests"))
180 if (chapter9_TestIndex < 0 || MSC_TestIndex < 0)
181 return USBIO_ERR_FAILED;
184 int ch9DeviceIndex = 0;
185 int numCh9Devices = 1;
186 int mscDeviceIndex = 0;
187 int numMscDevices = 1;
189 bool msDevice = false;
190 while (numCh9Devices > 0 && dwRC == USBIO_ERR_SUCCESS)
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);
197 lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
199 dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&ch9DeviceIndex,&numCh9Devices,&msDevice);
200 if (ch9DeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
203 dwRC = WaitForTestResult(&testResult);
204 if (dwRC == USBIO_ERR_SUCCESS)
210 PRINT_TIME "USBCV Chapter 9 Test Complete - %s ",testResult ? "Passed" : "Failed");
215 PRINT_ALWAYS "Error in Running USBCV\n");
220 if (msDevice && numMscDevices > 0 && dwRC == USBIO_ERR_SUCCESS)
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);
227 lResult = SendMessage (mainWindow,WM_COMMAND,MAKEWPARAM(ID_RUN,BN_CLICKED),(LPARAM)runButton);
229 if (dwRC == USBIO_ERR_SUCCESS)
230 dwRC = SelectDevice (aVendorId,aProductId1,aProductId2,&mscDeviceIndex,&numMscDevices,&msDevice);
231 if (mscDeviceIndex >= 0 && dwRC == USBIO_ERR_SUCCESS)
233 dwRC = ContinueOk ("The following test might destroy",ID_CONFIRM);
234 for (int i = 0; i < USBMS_DEVICE_REQUEST_CONTINUES; i++)
236 if (dwRC == USBIO_ERR_SUCCESS)
237 dwRC = ContinueOk ("Processing device request",ID_OKBUTTON);
239 if (dwRC == USBIO_ERR_SUCCESS)
240 dwRC = ContinueOk ("Disconnect and power off",ID_CONFIRM);
242 if (dwRC == USBIO_ERR_SUCCESS)
244 dwRC = WaitForTestResult(&testResult);
245 if (dwRC == USBIO_ERR_SUCCESS)
251 PRINT_TIME "USBCV Mass Storage Class Test Complete - %s ",testResult ? "Passed" : "Failed");
256 PRINT_ALWAYS "Error in Running USBCV\n");
267 DWORD UsbcvControl::ContinueOk (char * choiceText, int buttonId)
269 HWND choiceDialog = NULL;
271 char strBuffer[BUFFER_SIZE];
273 for (int i=0; i < CONTINUE_WAIT && !choiceDialog; i++)
275 choiceDialog = FindWindow(DIALOG_CLASS,"");
278 UINT textSize = GetDlgItemText (choiceDialog,ID_TEXTBOX,strBuffer,BUFFER_SIZE);
279 okButton = GetDlgItem(choiceDialog,buttonId);
280 if (!textSize || !okButton || !strstr (strBuffer,choiceText))
290 return USBIO_ERR_FAILED;
292 SendMessage (choiceDialog,WM_COMMAND,MAKEWPARAM(buttonId,BN_CLICKED),(LPARAM)okButton);
294 return USBIO_ERR_SUCCESS;
298 DWORD UsbcvControl::SelectDevice(USHORT aVendorId, USHORT aProductId1, USHORT aProductId2, int * deviceIndex, int * numDevices, bool * msDevice)
300 HWND devListDialog = NULL;
306 for (i=0; i < SELECT_WAIT && !devListDialog; i++)
308 devListDialog = FindWindow(DIALOG_CLASS,DEVLIST_TITLE);
311 devList = GetDlgItem(devListDialog,ID_DEVLIST);
312 okButton = GetDlgItem(devListDialog,ID_OKBUTTON);
313 if (!devList || !okButton)
315 devListDialog = NULL;
323 return USBIO_ERR_FAILED;
325 LRESULT itemCount = SendMessage (devList,LB_GETCOUNT,0,0);
327 return USBIO_ERR_FAILED;
330 if (* deviceIndex >= itemCount)
331 return USBIO_ERR_SUCCESS;
333 char strBuffer[BUFFER_SIZE];
334 char strVID [VIDPID_SIZE];
335 char strPID1 [VIDPID_SIZE];
336 char strPID2 [VIDPID_SIZE];
337 bool deviceFound = false;
339 for (i = * deviceIndex; i < itemCount; i++)
341 lResult = SendMessage (devList,LB_GETTEXT,(WPARAM)i,(LPARAM)&strBuffer);
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)))
356 * msDevice = strstr (strBuffer,strPID2) != NULL;
358 lResult = SendMessage (devList,LB_SETCURSEL,(WPARAM)i,0);
367 if (lResult == LB_ERR)
369 return USBIO_ERR_FAILED;
373 lResult = SendMessage (devListDialog,WM_COMMAND,MAKEWPARAM(ID_OKBUTTON,BN_CLICKED),(LPARAM)okButton);
375 return USBIO_ERR_SUCCESS;
382 return USBIO_ERR_FAILED;
388 DWORD UsbcvControl::WaitForTestResult(BOOL * passFail)
390 HWND testResultDialog = NULL;
392 char strBuffer[BUFFER_SIZE];
394 for (int i=0; i < RESULT_WAIT && !testResultDialog; i++)
396 testResultDialog = FindWindow(DIALOG_CLASS,TESTRESULT_TITLE);
397 if (testResultDialog)
399 UINT textSize = GetDlgItemText(testResultDialog,ID_RESULT_TEXT,(LPTSTR)&strBuffer,BUFFER_SIZE);
400 okButton = GetDlgItem(testResultDialog,ID_RESULT_OK);
401 if (!textSize || !okButton)
403 testResultDialog = NULL;
406 if (!testResultDialog)
410 if (!testResultDialog)
411 return USBIO_ERR_FAILED;
413 if (strstr (strBuffer,TEST_PASSED))
419 if (strstr (strBuffer,TEST_FAILED))
425 return USBIO_ERR_FAILED;
429 SendMessage (testResultDialog,WM_COMMAND,MAKEWPARAM(ID_RESULT_OK,BN_CLICKED),(LPARAM)okButton);
431 return USBIO_ERR_SUCCESS;