USBProxyService.cpp revision 968570a47a6b42b4f3e3f4ec822217f74442547d
/** @file
* VirtualBox USB Proxy Service (base) class.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
#include "USBProxyService.h"
#include "Logging.h"
#include <iprt/semaphore.h>
/** @todo add the required locking. */
/**
* Initialize data members.
*/
{
}
/**
* Empty destructor.
*/
{
LogFlowThisFunc (("\n"));
mTerminate = true;
}
bool USBProxyService::isActive (void)
{
return mThread != NIL_RTTHREAD;
}
int USBProxyService::getLastError (void)
{
return mLastError;
}
int USBProxyService::start (void)
{
int rc = VINF_SUCCESS;
if (mThread == NIL_RTTHREAD)
{
/*
* Force update before starting the poller thread.
*/
wait (0);
processChanges ();
/*
* Create the poller thread which will look for changes.
*/
mTerminate = false;
if (VBOX_SUCCESS (rc))
else
{
mLastError = rc;
}
}
else
return rc;
}
int USBProxyService::stop (void)
{
int rc = VINF_SUCCESS;
if (mThread != NIL_RTTHREAD)
{
/*
* Mark the thread for termination and kick it.
*/
ASMAtomicXchgSize (&mTerminate, true);
rc = interruptWait();
/*
* Wait for the thread to finish and then update the state.
*/
if (rc == VERR_INVALID_HANDLE)
rc = VINF_SUCCESS;
if (VBOX_SUCCESS (rc))
{
mTerminate = false;
}
else
{
mLastError = rc;
}
}
else
LogFlowThisFunc (("not active\n"));
return rc;
}
/**
* Sort a list of USB devices.
*
* @returns Pointer to the head of the sorted doubly linked list.
* @param aDevices Head pointer (can be both singly and doubly linked list).
*/
{
while (pDevices)
{
/* unlink head */
if (pDevices)
/* find location. */
while ( pCur
/* insert (after pCur) */
if (pCur)
{
else
}
else
{
if (pHead)
else
}
}
return pHead;
}
void USBProxyService::processChanges (void)
{
LogFlowThisFunc (("\n"));
/*
* Get the sorted list of USB devices.
*/
if (pDevices)
{
/*
* We need to lock the host object for writing because
* a) the subsequent code may call Host methods that require a write
* lock
* b) we will lock HostUSBDevice objects below and want to make sure
* the lock order is always the same (Host, HostUSBDevice, as
* expected by Host) to avoid cross-deadlocks
*/
/*
* Compare previous list with the previous list of devices
* and merge in any changes while notifying Host.
*/
|| pDevices)
{
/*
* Assert that the object is still alive (we still reference it in
* the collection and we're the only one who calls uninit() on it
*/
/*
* properties. All Host callbacks also imply the object is locked.
*/
/*
* Compare.
*/
int iDiff;
iDiff = 1;
else
{
if (!pDevices)
iDiff = -1;
else
}
if (!iDiff)
{
/*
* The device still there, update the state and move on. The PUSBDEVICE
* structure is eaten by updateDeviceState / HostUSBDevice::updateState().
*/
{
Log (("USBProxyService::processChanges: state change %p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"} state=%d%s\n",
(HostUSBDevice *)DevPtr, pCur->idVendor, pCur->idProduct, pCur->pszProduct, pCur->pszManufacturer, DevPtr->state(), DevPtr->isStatePending() ? " (pending async op)" : ""));
}
It++;
}
else
{
if (iDiff > 0)
{
/*
* Head of pDevices was attached.
*/
Log (("USBProxyService::processChanges: attached %p/%p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
(HostUSBDevice *)NewObj, pNew, pNew->idVendor, pNew->idProduct, pNew->pszProduct, pNew->pszManufacturer));
/* Not really necessary to lock here, but make Assert checks happy. */
}
else
{
/*
* DevPtr was detached, unless there is a pending async request.
* Check if the async request timed out before making a decision.
*/
if (DevPtr->isStatePending())
if (DevPtr->isStatePending())
{
It++;
Log (("USBProxyService::processChanges: detached but pending %d/%d %p\n",
}
else
{
Log (("USBProxyService::processChanges: detached %p\n",
/* from now on, the object is no more valid,
* uninitialize to avoid abuse */
}
}
}
} /* while */
}
else
{
/* we need to lock the host object for writing because
* a) the subsequent code may call Host methods that require a write
* lock
* b) we will lock HostUSBDevice objects below and want to make sure
* the lock order is always the same (Host, HostUSBDevice, as
* expected by Host) to avoid cross-deadlocks */
/* All devices were detached */
{
/* assert that the object is still alive (we still reference it in
* the collection and we're the only one who calls uninit() on it */
/*
* DevPtr was detached.
*/
Log (("USBProxyService::processChanges: detached %p\n",
/* from now on, the object is no more valid,
* uninitialize to avoid abuse */
}
}
LogFlowThisFunc (("returns void\n"));
}
{
/*
* Processing loop.
*/
for (;;)
{
if (pThis->mTerminate)
break;
pThis->processChanges();
}
LogFlowFunc (("returns VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
{
while (cCfgs-- > 0)
{
while (cIfs-- > 0)
{
/* next */
pIf++;
}
/* next */
pCfg++;
}
}
{
}
{
if (!aSerial)
aSerial = "";
for (;;)
{
if (!u8)
break;
pu8++;
}
return u64;
}
{
AssertReturn (aDevice, false);
if (aDevice->isStatePending())
{
switch (aDevice->pendingStateEx())
{
case HostUSBDevice::kNothingPending:
switch (aDevice->pendingState())
{
/* @todo USBDEVICESTATE_USED_BY_GUEST seems not to be used anywhere in the proxy code; it's
* quite logical because the proxy doesn't know anything about guest VMs. We use HELD_BY_PROXY
* instead -- it is sufficient and is what Main expects. */
case USBDeviceState_USBDeviceUnavailable: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST; break;
case USBDeviceState_USBDeviceBusy: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; break;
default:
break;
}
break;
/* don't call updateDeviceState until it's reattached. */
return false;
/* Let updateDeviceState / HostUSBDevice::updateState deal with this. */
break;
default:
break;
}
}
}
/* Stubs which the host specific classes overrides: */
{
return RTThreadSleep (250);
}
int USBProxyService::interruptWait (void)
{
return VERR_NOT_IMPLEMENTED;
}
{
return NULL;
}
void USBProxyService::serviceThreadInit (void)
{
}
void USBProxyService::serviceThreadTerm (void)
{
}
/**
* The default implementation returns non-NULL to emulate successful insertions
* for those subclasses that don't reimplement this method.
*/
#ifndef VBOX_WITH_USBFILTER
#else
#endif
{
// return non-NULL to prevent failed assertions in Main
return (void *) 1;
}
{
}
{
return VERR_NOT_IMPLEMENTED;
}
{
return VERR_NOT_IMPLEMENTED;
}
{
}
{
return VERR_NOT_IMPLEMENTED;
}
{
AssertReturn (aDevice, false);
if ( !fRc
&& aDevice->isStatePending()
fRc = true;
return fRc;
}
{
}
{
}