kerneltest/e32test/usb/t_usb_win/src/t_usb_winDlg.cpp
changeset 0 a41df078684a
child 15 4122176ea935
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     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 // t_usb_winDlg.cpp : implementation file
       
    15 //
       
    16 
       
    17 #include "stdafx.h"
       
    18 #include <dbt.h>
       
    19 #include <afxpriv.h>
       
    20 #include "t_usb_win.h"
       
    21 #include "t_usb_winDlg.h"
       
    22 #include "global.h"
       
    23 
       
    24 #include "usbio.h"											// USBIO Dev Kit
       
    25 #include "usbiopipe.h"										// ditto
       
    26 
       
    27 #ifdef _DEBUG
       
    28 #define new DEBUG_NEW
       
    29 #undef THIS_FILE
       
    30 static char THIS_FILE[] = __FILE__;
       
    31 #endif
       
    32 
       
    33 /////////////////////////////////////////////////////////////////////////////
       
    34 // external globals
       
    35 extern CUsbIo gUsbDev;
       
    36 extern GUID gUsbioID;
       
    37 extern BOOL gVerboseMode;
       
    38 extern HANDLE gDeviceConnectEvent; 
       
    39 extern HANDLE gDeviceDisconnectEvent; 
       
    40 extern FILE *logStream;
       
    41 
       
    42 extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
       
    43 extern UINT DoTests(LPVOID pParam);
       
    44 
       
    45 /////////////////////////////////////////////////////////////////////////////
       
    46 // CAboutDlg dialog used for App About
       
    47 
       
    48 class CAboutDlg : public CDialog
       
    49 {
       
    50 public:
       
    51 	CAboutDlg();
       
    52 
       
    53 // Dialog Data
       
    54 	//{{AFX_DATA(CAboutDlg)
       
    55 	enum { IDD = IDD_ABOUTBOX };
       
    56 	//}}AFX_DATA
       
    57 
       
    58 	// ClassWizard generated virtual function overrides
       
    59 	//{{AFX_VIRTUAL(CAboutDlg)
       
    60 	protected:
       
    61 	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
       
    62 	//}}AFX_VIRTUAL
       
    63 
       
    64 // Implementation
       
    65 protected:
       
    66 	//{{AFX_MSG(CAboutDlg)
       
    67 	//}}AFX_MSG
       
    68 	DECLARE_MESSAGE_MAP()
       
    69 };
       
    70 
       
    71 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
       
    72 {
       
    73 	//{{AFX_DATA_INIT(CAboutDlg)
       
    74 	//}}AFX_DATA_INIT
       
    75 }
       
    76 
       
    77 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
       
    78 {
       
    79 	CDialog::DoDataExchange(pDX);
       
    80 	//{{AFX_DATA_MAP(CAboutDlg)
       
    81 	//}}AFX_DATA_MAP
       
    82 }
       
    83 
       
    84 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
       
    85 	//{{AFX_MSG_MAP(CAboutDlg)
       
    86 		// No message handlers
       
    87 	//}}AFX_MSG_MAP
       
    88 END_MESSAGE_MAP()
       
    89 
       
    90 /////////////////////////////////////////////////////////////////////////////
       
    91 // CT_usb_winDlg dialog
       
    92 
       
    93 CT_usb_winDlg::CT_usb_winDlg(CWnd* pParent /*=NULL*/)
       
    94 	: CDialog(CT_usb_winDlg::IDD, pParent)
       
    95 {
       
    96 	//{{AFX_DATA_INIT(CTempwinDlg)
       
    97 		// NOTE: the ClassWizard will add member initialization here
       
    98 	//}}AFX_DATA_INIT
       
    99 	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
       
   100 	m_hIcon = AfxGetApp()->LoadIcon(IDD_T_USB_WIN_DIALOG);
       
   101 }
       
   102 
       
   103 
       
   104 void CT_usb_winDlg::DoDataExchange(CDataExchange* pDX)
       
   105 {
       
   106 	CDialog::DoDataExchange(pDX);
       
   107 	//{{AFX_DATA_MAP(CT_usb_winDlg)
       
   108 	DDX_Control(pDX, IDC_TEXT1, m_text1);
       
   109 	//}}AFX_DATA_MAP
       
   110 }
       
   111 
       
   112 BEGIN_MESSAGE_MAP(CT_usb_winDlg, CDialog)
       
   113 	//{{AFX_MSG_MAP(CT_usb_winDlg)
       
   114 	ON_WM_SYSCOMMAND()
       
   115 	ON_WM_CLOSE()
       
   116     ON_MESSAGE(WM_USER_PRINTOUT, OnUserMessage)
       
   117 	ON_WM_DESTROY()
       
   118 	//}}AFX_MSG_MAP
       
   119     ON_WM_DEVICECHANGE()
       
   120 END_MESSAGE_MAP()
       
   121 
       
   122 
       
   123 void CT_usb_winDlg::OutputString(const char *s)
       
   124 {
       
   125   int len;
       
   126   int limit;
       
   127   
       
   128   // get current length
       
   129   len=m_text1.GetWindowTextLength(); 
       
   130   // get limit
       
   131   limit = m_text1.GetLimitText() - 2048;
       
   132 
       
   133   if ( len > limit ) {
       
   134     // delete 8K text
       
   135     m_text1.SetSel(0,8192);
       
   136     m_text1.Clear();
       
   137     len=m_text1.GetWindowTextLength(); 
       
   138   }
       
   139   // append string
       
   140   m_text1.SetSel(len,len);
       
   141   m_text1.ReplaceSel(s, FALSE);
       
   142 }
       
   143 
       
   144 void CT_usb_winDlg::DisplayHello()
       
   145 	{
       
   146 	PRINT_ALWAYS "*------------------------------------------------------------------------------"NL); 
       
   147  
       
   148 
       
   149 	PRINT_ALWAYS "* T_USB_WIN v%d.%d.%d (for use with TUSBWIN.SYS v%d.%d)"NL,
       
   150 		   VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO,
       
   151 		   USBIO_VERSION_MAJOR, USBIO_VERSION_MINOR);
       
   152 
       
   153 	PRINT_ALWAYS "* USB Test Application, Host-side Part"NL);
       
   154 	PRINT_ALWAYS "*   Copyright (c) 2007 Symbian Software Ltd."NL);
       
   155 	PRINT_ALWAYS "*------------------------------------------------------------------------------"NL); 
       
   156 	}
       
   157 
       
   158 /////////////////////////////////////////////////////////////////////////////
       
   159 // CT_usb_winDlg message handlers
       
   160 
       
   161 BOOL CT_usb_winDlg::OnInitDialog()
       
   162 {
       
   163 	CDialog::OnInitDialog();
       
   164 
       
   165 	// TODO: Add extra initialization here
       
   166 	// Add "About..." menu item to system menu.
       
   167 
       
   168 	// IDM_ABOUTBOX must be in the system command range.
       
   169 	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
       
   170 	ASSERT(IDM_ABOUTBOX < 0xF000);
       
   171 
       
   172 	CMenu* pSysMenu = GetSystemMenu(FALSE);
       
   173 	if (pSysMenu != NULL)
       
   174 	{
       
   175 		CString strAboutMenu;
       
   176 		strAboutMenu.LoadString(IDS_ABOUTBOX);
       
   177 		if (!strAboutMenu.IsEmpty())
       
   178 		{
       
   179 			pSysMenu->AppendMenu(MF_SEPARATOR);
       
   180 			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
       
   181 		}
       
   182 	}
       
   183 
       
   184 	// init font
       
   185 	BOOL succ = m_font.CreatePointFont(
       
   186                 80,  //int nPointSize, 
       
   187                 "System",  //LPCTSTR lpszFaceName,
       
   188                 NULL        // CDC* pDC = NULL
       
   189                 );
       
   190 	if ( succ )
       
   191 	{
       
   192 		m_text1.SetFont(&m_font);
       
   193 	} else {
       
   194 		AfxMessageBox("Unable to initialize font.");
       
   195 	}
       
   196 
       
   197 	// set text limit to 512K
       
   198 	m_text1.SetLimitText(512*1024);
       
   199 
       
   200 	DisplayHello();
       
   201 
       
   202 	if (!RegisterDevNotify(&gUsbioID,&m_DevNotify)) {
       
   203 		PRINT_ALWAYS "ERROR: Unable to register device notification."NL);
       
   204 		} 
       
   205 
       
   206 	AfxBeginThread (DoTests, NULL);
       
   207 
       
   208 	return TRUE;  // return TRUE  unless you set the focus to a control
       
   209 }
       
   210 
       
   211 /////////////////////////////////////////////////////////////////////////////
       
   212 // CT_usb_winDlg::OnClose
       
   213 //      OnClose makes sure the log file stream is closed.
       
   214 
       
   215 void CT_usb_winDlg::OnClose()
       
   216 {
       
   217 	if (logStream)
       
   218 		fclose (logStream);
       
   219 
       
   220 	CDialog::OnClose();
       
   221 }
       
   222 
       
   223 void CT_usb_winDlg::OnSysCommand(UINT nID, LPARAM lParam)
       
   224 {
       
   225 	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
       
   226 	{
       
   227 		CAboutDlg dlgAbout;
       
   228 		dlgAbout.DoModal();
       
   229 	}
       
   230 	else
       
   231 	{
       
   232 		CDialog::OnSysCommand(nID, lParam);
       
   233 	}
       
   234 }
       
   235 
       
   236 
       
   237 //
       
   238 // Message Handler for WM_USER_PRINTOUT
       
   239 //
       
   240 // get string buffer from message, print out, free buffer
       
   241 //
       
   242 LRESULT CT_usb_winDlg::OnUserMessage(WPARAM wParam, LPARAM lParam)
       
   243 {
       
   244   const char* buffer = (const char*)lParam;
       
   245 
       
   246   if ( buffer!=NULL ) {
       
   247     // print to output window
       
   248     OutputString(buffer);
       
   249   }
       
   250 
       
   251   return 0;
       
   252 }
       
   253 
       
   254 
       
   255 BOOLEAN CT_usb_winDlg::RegisterDevNotify(
       
   256   const GUID *InterfaceClassGuid, 
       
   257   HDEVNOTIFY *hDevNotify
       
   258   )
       
   259 /*
       
   260 Routine Description:
       
   261     Registers for notification of changes in the device interfaces for
       
   262     the specified interface class GUID. 
       
   263 
       
   264 Parameters:
       
   265     InterfaceClassGuid - The interface class GUID for the device 
       
   266         interfaces. 
       
   267 
       
   268     hDevNotify - Receives the device notification handle. On failure, 
       
   269         this value is NULL.
       
   270 
       
   271 Return Value:
       
   272     If the function succeeds, the return value is TRUE.
       
   273 
       
   274     If the function fails, the return value is FALSE.
       
   275 */
       
   276 {
       
   277     DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
       
   278 
       
   279     ZeroMemory(&NotificationFilter, sizeof(NotificationFilter) );
       
   280     NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
       
   281     NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
       
   282     NotificationFilter.dbcc_classguid = *InterfaceClassGuid;
       
   283 
       
   284     // device notifications should be send to the main dialog
       
   285     *hDevNotify = RegisterDeviceNotification(
       
   286                       m_hWnd, 
       
   287                       &NotificationFilter,
       
   288                       DEVICE_NOTIFY_WINDOW_HANDLE
       
   289                       );
       
   290     if ( !(*hDevNotify) ) {
       
   291       DWORD Err = GetLastError();
       
   292       PRINT_ALWAYS "RegisterDeviceNotification failed, errcode:%08X"NL,Err);
       
   293       return FALSE;
       
   294     }
       
   295 
       
   296     return TRUE;
       
   297 }
       
   298 
       
   299 
       
   300 #define _ENUMSTR(e) (x==(e)) ? #e
       
   301 
       
   302 static const char* DeviceChangeMsgStr(UINT x)
       
   303 {
       
   304   return (
       
   305     _ENUMSTR(DBT_DEVICEARRIVAL) :
       
   306     _ENUMSTR(DBT_DEVICEQUERYREMOVE) :
       
   307     _ENUMSTR(DBT_DEVICEQUERYREMOVEFAILED) :
       
   308     _ENUMSTR(DBT_DEVICEREMOVEPENDING) :
       
   309     _ENUMSTR(DBT_DEVICEREMOVECOMPLETE) :
       
   310     _ENUMSTR(DBT_DEVICETYPESPECIFIC) :
       
   311     _ENUMSTR(DBT_CUSTOMEVENT) :
       
   312     _ENUMSTR(DBT_USERDEFINED) :
       
   313     _ENUMSTR(DBT_DEVNODES_CHANGED) :
       
   314     "unknown"
       
   315   );
       
   316 }
       
   317 
       
   318 BOOL CT_usb_winDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
       
   319 {
       
   320 	DEV_BROADCAST_DEVICEINTERFACE *data=(DEV_BROADCAST_DEVICEINTERFACE*)dwData;
       
   321 
       
   322 	PRINT_IF_VERBOSE "OnDeviceChange message: %08X (%s)"NL,nEventType,DeviceChangeMsgStr(nEventType));
       
   323 
       
   324 	// check if data is valid
       
   325 	if (data == NULL || data->dbcc_name == NULL || strlen(data->dbcc_name)==0) {
       
   326 		return TRUE;
       
   327 	}
       
   328 
       
   329 	// convert interface name to CString
       
   330 	CString Name(data->dbcc_name);
       
   331 
       
   332 	// there is some strange behavior in Win98
       
   333 	// there are notifications with dbcc_name = "."
       
   334 	// we ignore this
       
   335 	if (Name.GetLength() < 5) {
       
   336 		return TRUE;
       
   337 	}
       
   338 
       
   339 	switch (nEventType) 
       
   340 		{
       
   341 		case DBT_DEVICEREMOVECOMPLETE:
       
   342 			// a device with our interface has been removed or is stopped
       
   343 			PRINT_NOLOG "Device disconnected."NL);
       
   344 			PRINT_IF_VERBOSE "Device path: %s."NL,data->dbcc_name); 
       
   345 			// close the global  handle
       
   346 			if ( gUsbDev.GetDevicePathName() && (0==Name.CompareNoCase(gUsbDev.GetDevicePathName())) )
       
   347 				{
       
   348 				PRINT_IF_VERBOSE "Closing driver interface"NL NL);
       
   349 				gUsbDev.Close();
       
   350 				}
       
   351 			SetEvent (gDeviceDisconnectEvent);
       
   352 			break;
       
   353 
       
   354 		case DBT_DEVICEARRIVAL:
       
   355 			// a device with our interface has been activated (started)
       
   356 			PRINT_NOLOG "Device connected."NL); 
       
   357 			PRINT_IF_VERBOSE "Device path: %s."NL,data->dbcc_name); 
       
   358  			SetEvent (gDeviceConnectEvent);
       
   359 			break;
       
   360 		
       
   361 		case DBT_DEVICEQUERYREMOVE:
       
   362 			// windows asked, if our device can be removed, we answer with TRUE (yes)
       
   363 			PRINT_IF_VERBOSE "MSG: DBT_DEVICEQUERYREMOVE -- Application returned success."NL);
       
   364 			break;
       
   365 		
       
   366 		case DBT_DEVICEREMOVEPENDING:
       
   367 			// device remove is pending
       
   368 			PRINT_IF_VERBOSE "MSG: DBT_DEVICEREMOVEPENDING."NL);
       
   369 			break;
       
   370 		
       
   371 		default:
       
   372 			break;
       
   373 		} 
       
   374 
       
   375   return TRUE;
       
   376 }
       
   377 
       
   378 void CT_usb_winDlg::OnDestroy() 
       
   379 {
       
   380   // close device
       
   381   gUsbDev.Close();
       
   382 
       
   383   // The following is a work-around for a bug in Win98,
       
   384   // the system becomes unstable if UnregisterDeviceNotification is called
       
   385   // Therefore we don't call it on Win98.
       
   386   if ( _winver >= 0x500 ) {
       
   387     if ( m_DevNotify!=NULL ) {
       
   388       UnregisterDeviceNotification(m_DevNotify);
       
   389       m_DevNotify = NULL;
       
   390     } 
       
   391   }
       
   392 
       
   393   CDialog::OnDestroy();
       
   394 
       
   395 }