USBControllerImpl.cpp revision 7fa58e26669bf006ccf2629084fed8a24ce4b936
/** @file
*
* Implementation of IUSBController.
*/
/*
* 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 "USBControllerImpl.h"
#include "MachineImpl.h"
#include "VirtualBoxImpl.h"
#include "HostImpl.h"
#include "USBDeviceImpl.h"
#include "HostUSBDeviceImpl.h"
#include "Logging.h"
#include "USBProxyService.h"
#include <iprt/cpputils.h>
#include <algorithm>
// defines
/////////////////////////////////////////////////////////////////////////////
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
{
return S_OK;
}
void USBController::FinalRelease()
{
uninit();
}
// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the USB controller object.
*
* @returns COM result indicator.
* @param aParent Pointer to our parent object.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* mPeer is left null */
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the USB controller object given another USB controller object
* (a kind of copy constructor). This object shares data with
* the object passed as an argument.
*
* @returns COM result indicator.
* @param aParent Pointer to our parent object.
* @param aPeer The object to share.
*
* @note This object must be destroyed before the original object
* it shares data with is destroyed.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* create copies of all filters */
{
++ it;
}
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the USB controller object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* mPeer is left null */
/* create private copies of all filters */
{
++ it;
}
/* Confirm a successful initialization */
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void USBController::uninit()
{
LogFlowThisFunc (("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan (this);
if (autoUninitSpan.uninitDone())
return;
/* uninit all filters (including those still referenced by clients) */
}
// IUSBController properties
/////////////////////////////////////////////////////////////////////////////
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock for safety */
}
return S_OK;
}
{
if (!aUSBStandard)
return E_POINTER;
AutoCaller autoCaller (this);
/* not accessing data -- no need to lock */
*aUSBStandard = 0x0101;
return S_OK;
}
{
if (!aDevicesFilters)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
// IUSBController methods
/////////////////////////////////////////////////////////////////////////////
{
if (!aFilter)
return E_POINTER;
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
return S_OK;
}
{
if (!aFilter)
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
if (!filter)
return setError (E_INVALIDARG,
tr ("The given USB device filter is not created within "
"this VirtualBox instance"));
return setError (E_INVALIDARG,
tr ("The given USB device filter is already in the list"));
/* backup the list before modification */
/* iterate to the position... */
{
}
else
/* ...and insert */
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
}
return S_OK;
}
{
if (!aFilter)
return E_POINTER;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
if (!mDeviceFilters->size())
return setError (E_INVALIDARG,
tr ("The USB device filter list is empty"));
return setError (E_INVALIDARG,
tr ("Invalid position: %lu (must be in range [0, %lu])"),
/* backup the list before modification */
{
/* iterate to the position... */
/* ...get an element from there... */
/* ...and remove */
}
/* cancel sharing (make an independent copy of data) */
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
}
return S_OK;
}
// public methods only for internal purposes
/////////////////////////////////////////////////////////////////////////////
/**
* Loads settings from the configuration node.
*
* @note Locks objects for writing!
*/
{
AutoCaller autoCaller (this);
Assert (controller);
/* enabled */
bool enabled;
unsigned filterCount = 0;
{
bool active;
/* error info is set by init() when appropriate */
{
}
}
return rc;
}
/**
* Saves settings to the configuration node.
*
* @note Locks objects for reading!
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* first, delete the entry */
if (VBOX_SUCCESS (vrc))
{
}
/* then, recreate it */
/* enabled */
{
/* all are optional */
#ifndef VBOX_WITH_USBFILTER
#else /* VBOX_WITH_USBFILTER */
#endif /* VBOX_WITH_USBFILTER */
++ it;
}
return S_OK;
}
/** @note Locks objects for reading! */
bool USBController::isModified()
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return true;
/* see whether any of filters has changed its data */
++ it)
{
if ((*it)->isModified())
return true;
}
return false;
}
/** @note Locks objects for reading! */
bool USBController::isReallyModified()
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
if (mData.hasActualChanges())
return true;
if (!mDeviceFilters.isBackedUp())
{
/* see whether any of filters has changed its data */
++ it)
{
if ((*it)->isReallyModified())
return true;
}
return false;
}
return true;
if (mDeviceFilters->size() == 0)
return false;
/* Make copies to speed up comparison */
{
bool found = false;
{
{
found = true;
break;
}
else
++ thatIt;
}
if (found)
else
return false;
}
return false;
}
/** @note Locks objects for writing! */
bool USBController::rollback()
{
AutoCaller autoCaller (this);
/* we need the machine state */
bool dataChanged = false;
if (mData.isBackedUp())
{
/* we need to check all data to see whether anything will be changed
* after rollback */
}
if (mDeviceFilters.isBackedUp())
{
ComAssertRet (service, false);
/* uninitialize all new filters (absent in the backed up list) */
{
backedList->end())
{
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
}
}
++ it;
}
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
#else
#endif
{
/* find all removed old filters (absent in the new list)
* and insert them back to the USB proxy */
{
mDeviceFilters->end())
{
/* notify the proxy (only when necessary) */
{
}
}
++ it;
}
}
/* restore the list */
}
/* here we don't depend on the machine state any more */
/* rollback any changes to filters after restoring the list */
{
if ((*it)->isModified())
{
/* call this to notify the USB proxy about changes */
}
++ it;
}
return dataChanged;
}
/** @note Locks objects for writing! */
void USBController::commit()
{
AutoCaller autoCaller (this);
if (mData.isBackedUp())
{
if (mPeer)
{
// attach new data to the peer and reshare it
}
}
bool commitFilters = false;
if (mDeviceFilters.isBackedUp())
{
// apply changes to peer
if (mPeer)
{
// commit all changes to new filters (this will reshare data with
// peers for those who have peers)
{
// look if this filter has a peer filter
if (!peer)
{
// no peer means the filter is a newly created one;
// create a peer owning data this filter share it with
peer.createObject();
}
else
{
// remove peer from the old list
}
// and add it to the new list
++ it;
}
// uninit old peer's filters that are left
{
++ it;
}
// attach new list of filters to our peer
}
else
{
// we have no peer (our parent is the newly created machine);
// just commit changes to filters
commitFilters = true;
}
}
else
{
// the list of filters itself is not changed,
// just commit changes to filters themselves
commitFilters = true;
}
if (commitFilters)
{
{
++ it;
}
}
}
/** @note Locks object for writing and that object for reading! */
{
AutoCaller autoCaller (this);
if (mParent->isRegistered())
{
/* reuse onMachineRegistered to tell USB proxy to remove all current
filters */
AssertComRCReturn (rc, (void) 0);
}
/* this will back up current data */
/* create private copies of all filters */
mDeviceFilters->clear();
++ it)
{
}
if (mParent->isRegistered())
{
/* reuse onMachineRegistered to tell USB proxy to insert all current
filters */
AssertComRCReturn (rc, (void) 0);
}
}
/**
* Called by VirtualBox when it changes the registered state
* of the machine this USB controller belongs to.
*
* @param aRegistered new registered state of the machine
*
* @note Locks nothing.
*/
{
AutoCaller autoCaller (this);
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
notifyProxy (!!aRegistered);
#endif
return S_OK;
}
/**
* Called by setter methods of all USB device filters.
*
* @note Locks nothing.
*/
{
AutoCaller autoCaller (this);
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
#else
/* we need the machine state */
/* nothing to do if the machine isn't running */
return S_OK;
#endif
/* we don't modify our data fields -- no need to lock */
{
if (aActiveChanged)
{
{
}
else
{
}
}
else
{
{
/* update the filter in the proxy */
}
}
}
return S_OK;
}
/**
* Returns true if the given USB device matches to at least one of
* this controller's USB device filters.
*
* A HostUSBDevice specific version.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* Disabled USB controllers cannot actually work with USB devices */
return false;
bool match = false;
/* apply self filters */
++ it)
{
}
return match;
}
/**
* Returns true if the given USB device matches to at least one of
* this controller's USB device filters.
*
* A generic version that accepts any IUSBDevice on input.
*
* @note
* This method MUST correlate with HostUSBDevice::isMatch()
* in the sense of the device matching logic.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* Disabled USB controllers cannot actually work with USB devices */
return false;
/* query fields */
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
ComAssertRet (vendorId, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
ComAssertRet (productId, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
if (!manufacturer.isNull())
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
if (!serialNumber.isNull())
#endif
ComAssertComRCRet (rc, false);
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
bool match = false;
/* apply self filters */
++ it)
{
continue;
continue;
#ifndef VBOX_WITH_USBFILTER
continue;
continue;
continue;
#if !defined (RT_OS_WINDOWS)
/* these filters are temporarily ignored on Win32 */
continue;
continue;
continue;
continue;
#endif
#else /* VBOX_WITH_USBFILTER */
continue;
#endif /* VBOX_WITH_USBFILTER */
match = true;
break;
}
return match;
}
/**
* Notifies the proxy service about all filters as requested by the
* @a aInsertFilters argument.
*
* @param aInsertFilters @c true to insert filters, @c false to remove.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
{
/* notify the proxy (only if the filter is active) */
{
if (aInsertFilters)
{
}
else
{
/* It's possible that the given filter was not inserted the proxy
* when this method gets called (as a result of an early VM
* process crash for example. So, don't assert that ID != NULL. */
{
}
}
}
++ it;
}
return S_OK;
}
// private methods
/////////////////////////////////////////////////////////////////////////////