USBTest.cpp revision c55c68b6a3324172e9dc207926215845880b0f90
/** @file
*
* VBox host drivers - USB drivers - Filter & driver installation
*
* Installation code
*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <windows.h>
#include <setupapi.h>
#include <newdev.h>
#include <stdio.h>
/** Handle to the open device. */
/** Flags whether or not we started the service. */
static bool g_fStartedService = false;
/**
* Attempts to start the service, creating it if necessary.
*
* @returns 0 on success.
* @returns -1 on failure.
* @param fRetry Indicates retry call.
*/
int usbMonStartService(void)
{
/*
* Check if the driver service is there.
*/
{
AssertMsgFailed(("couldn't open service manager in SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS mode!\n"));
return -1;
}
/*
* Try open our service to check it's status.
*/
if (!hService)
return -1;
/*
* Check if open and on demand create succeeded.
*/
int rc = -1;
if (hService)
{
/*
* Query service status to see if we need to start it or not.
*/
{
/*
* Start it.
*/
printf("usbMonStartService -> start it\n");
if (fRc)
g_fStartedService = true;
}
/*
* Wait for the service to finish starting.
* We'll wait for 10 seconds then we'll give up.
*/
{
int iWait;
{
Sleep(100);
}
("Failed to start. LastError=%Rwa iWait=%d status=%d\n",
}
rc = 0;
/*
* Close open handles.
*/
}
else
{
}
if (!CloseServiceHandle(hSMgr))
AssertFailed();
return rc;
}
/**
* Stops a possibly running service.
*
* @returns 0 on success.
* @returns -1 on failure.
*/
int usbMonStopService(void)
{
printf("usbMonStopService\n");
/*
* Assume it didn't exist, so we'll create the service.
*/
int rc = -1;
if (hSMgr)
{
if (hService)
{
/*
* Stop the service.
*/
rc = 0;
{
int iWait = 100;
{
Sleep(100);
}
rc = 0;
else
}
else
{
AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
}
}
else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
rc = 0;
else
{
}
}
return rc;
}
/**
* Release specified USB device to the host.
*
* @returns VBox status code
* @param usVendorId Vendor id
* @param usProductId Product id
* @param usRevision Revision
*/
{
DWORD cbReturned = 0;
if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RELEASE_DEVICE, &release, sizeof(release), NULL, 0, &cbReturned, NULL))
{
return RTErrConvertFromWin32(GetLastError());
}
return VINF_SUCCESS;
}
/**
* Add USB device filter
*
* @returns VBox status code.
* @param pszVendor Vendor filter string
* @param pszProduct Product filter string
* @param pszRevision Revision filter string
* @param ppID Pointer to filter id
*/
int usbMonInsertFilter(const char *pszVendor, const char *pszProduct, const char *pszRevision, void **ppID)
{
DWORD cbReturned = 0;
if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_ADD_FILTER, &filter, sizeof(filter), &flt_add, sizeof(flt_add), &cbReturned, NULL))
{
return RTErrConvertFromWin32(GetLastError());
}
return VINF_SUCCESS;
}
/**
* Remove USB device filter
*
* @returns VBox status code.
* @param aID Filter id
*/
int usbMonRemoveFilter (void *aID)
{
DWORD cbReturned = 0;
if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_REMOVE_FILTER, &uId, sizeof(uId), NULL, 0,&cbReturned, NULL))
{
return RTErrConvertFromWin32(GetLastError());
}
return VINF_SUCCESS;
}
/**
* Initialize the USB monitor
*
* @returns VBox status code.
*/
int usbMonitorInit()
{
int rc;
USBSUP_VERSION version = {0};
printf("usbproxy: usbLibInit\n");
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
NULL); // No template file
if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
{
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
NULL); // No template file
if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
{
/* AssertFailed(); */
goto failure;
}
}
/*
* Check the version
*/
cbReturned = 0;
if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_VERSION, NULL, 0,&version, sizeof(version), &cbReturned, NULL))
{
goto failure;
}
{
printf("usbproxy: Filter driver version mismatch!!\n");
goto failure;
}
return VINF_SUCCESS;
if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
{
}
return rc;
}
/**
* Terminate the USB monitor
*
* @returns VBox status code.
*/
int usbMonitorTerm()
{
if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
{
}
/*
* If we started the service we might consider stopping it too.
*
* Since this won't work unless the process starting it is the
* last user we might wanna skip this...
*/
if (g_fStartedService)
{
g_fStartedService = false;
}
return VINF_SUCCESS;
}
{
int rc;
printf("USB test\n");
rc = usbMonitorInit();
printf("Waiting to capture device\n");
getchar();
printf("Releasing device\n");
rc = usbMonitorTerm();
return 0;
}