VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/USBControllerImpl.cpp@ 35638

Last change on this file since 35638 was 35638, checked in by vboxsync, 14 years ago

Main. QT/FE: fix long standing COM issue

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.7 KB
Line 
1/* $Id: USBControllerImpl.cpp 35638 2011-01-19 19:10:49Z vboxsync $ */
2/** @file
3 * Implementation of IUSBController.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "USBControllerImpl.h"
19
20#include "Global.h"
21#include "MachineImpl.h"
22#include "VirtualBoxImpl.h"
23#include "HostImpl.h"
24#ifdef VBOX_WITH_USB
25# include "USBDeviceImpl.h"
26# include "HostUSBDeviceImpl.h"
27# include "USBProxyService.h"
28# include "USBDeviceFilterImpl.h"
29#endif
30
31#include <iprt/string.h>
32#include <iprt/cpp/utils.h>
33
34#include <VBox/err.h>
35#include <VBox/settings.h>
36
37#include <algorithm>
38
39#include "AutoStateDep.h"
40#include "AutoCaller.h"
41#include "Logging.h"
42
43// defines
44/////////////////////////////////////////////////////////////////////////////
45
46typedef std::list< ComObjPtr<USBDeviceFilter> > DeviceFilterList;
47
48struct BackupableUSBData
49{
50 BackupableUSBData()
51 : fEnabled(false),
52 fEnabledEHCI(false)
53 { }
54
55 BOOL fEnabled;
56 BOOL fEnabledEHCI;
57};
58
59struct USBController::Data
60{
61 Data(Machine *pMachine)
62 : pParent(pMachine),
63 pHost(pMachine->getVirtualBox()->host())
64 { }
65
66 ~Data()
67 {};
68
69 Machine * const pParent;
70 Host * const pHost;
71
72 // peer machine's USB controller
73 const ComObjPtr<USBController> pPeer;
74
75 Backupable<BackupableUSBData> bd;
76#ifdef VBOX_WITH_USB
77 // the following fields need special backup/rollback/commit handling,
78 // so they cannot be a part of BackupableData
79 Backupable<DeviceFilterList> llDeviceFilters;
80#endif
81};
82
83
84
85// constructor / destructor
86/////////////////////////////////////////////////////////////////////////////
87
88DEFINE_EMPTY_CTOR_DTOR (USBController)
89
90HRESULT USBController::FinalConstruct()
91{
92 return BaseFinalConstruct();
93}
94
95void USBController::FinalRelease()
96{
97 uninit();
98 BaseFinalRelease();
99}
100
101// public initializer/uninitializer for internal purposes only
102/////////////////////////////////////////////////////////////////////////////
103
104/**
105 * Initializes the USB controller object.
106 *
107 * @returns COM result indicator.
108 * @param aParent Pointer to our parent object.
109 */
110HRESULT USBController::init(Machine *aParent)
111{
112 LogFlowThisFunc(("aParent=%p\n", aParent));
113
114 ComAssertRet(aParent, E_INVALIDARG);
115
116 /* Enclose the state transition NotReady->InInit->Ready */
117 AutoInitSpan autoInitSpan(this);
118 AssertReturn(autoInitSpan.isOk(), E_FAIL);
119
120 m = new Data(aParent);
121
122 /* mPeer is left null */
123
124 m->bd.allocate();
125#ifdef VBOX_WITH_USB
126 m->llDeviceFilters.allocate();
127#endif
128
129 /* Confirm a successful initialization */
130 autoInitSpan.setSucceeded();
131
132 return S_OK;
133}
134
135/**
136 * Initializes the USB controller object given another USB controller object
137 * (a kind of copy constructor). This object shares data with
138 * the object passed as an argument.
139 *
140 * @returns COM result indicator.
141 * @param aParent Pointer to our parent object.
142 * @param aPeer The object to share.
143 *
144 * @note This object must be destroyed before the original object
145 * it shares data with is destroyed.
146 */
147HRESULT USBController::init(Machine *aParent, USBController *aPeer)
148{
149 LogFlowThisFunc(("aParent=%p, aPeer=%p\n", aParent, aPeer));
150
151 ComAssertRet(aParent && aPeer, E_INVALIDARG);
152
153 /* Enclose the state transition NotReady->InInit->Ready */
154 AutoInitSpan autoInitSpan(this);
155 AssertReturn(autoInitSpan.isOk(), E_FAIL);
156
157 m = new Data(aParent);
158
159 unconst(m->pPeer) = aPeer;
160
161 AutoWriteLock thatlock(aPeer COMMA_LOCKVAL_SRC_POS);
162 m->bd.share(aPeer->m->bd);
163
164#ifdef VBOX_WITH_USB
165 /* create copies of all filters */
166 m->llDeviceFilters.allocate();
167 DeviceFilterList::const_iterator it = aPeer->m->llDeviceFilters->begin();
168 while (it != aPeer->m->llDeviceFilters->end())
169 {
170 ComObjPtr<USBDeviceFilter> filter;
171 filter.createObject();
172 filter->init(this, *it);
173 m->llDeviceFilters->push_back(filter);
174 ++ it;
175 }
176#endif /* VBOX_WITH_USB */
177
178 /* Confirm a successful initialization */
179 autoInitSpan.setSucceeded();
180
181 return S_OK;
182}
183
184
185/**
186 * Initializes the USB controller object given another guest object
187 * (a kind of copy constructor). This object makes a private copy of data
188 * of the original object passed as an argument.
189 */
190HRESULT USBController::initCopy(Machine *aParent, USBController *aPeer)
191{
192 LogFlowThisFunc(("aParent=%p, aPeer=%p\n", aParent, aPeer));
193
194 ComAssertRet(aParent && aPeer, E_INVALIDARG);
195
196 /* Enclose the state transition NotReady->InInit->Ready */
197 AutoInitSpan autoInitSpan(this);
198 AssertReturn(autoInitSpan.isOk(), E_FAIL);
199
200 m = new Data(aParent);
201
202 /* mPeer is left null */
203
204 AutoWriteLock thatlock(aPeer COMMA_LOCKVAL_SRC_POS);
205 m->bd.attachCopy(aPeer->m->bd);
206
207#ifdef VBOX_WITH_USB
208 /* create private copies of all filters */
209 m->llDeviceFilters.allocate();
210 DeviceFilterList::const_iterator it = aPeer->m->llDeviceFilters->begin();
211 while (it != aPeer->m->llDeviceFilters->end())
212 {
213 ComObjPtr<USBDeviceFilter> filter;
214 filter.createObject();
215 filter->initCopy(this, *it);
216 m->llDeviceFilters->push_back(filter);
217 ++ it;
218 }
219#endif /* VBOX_WITH_USB */
220
221 /* Confirm a successful initialization */
222 autoInitSpan.setSucceeded();
223
224 return S_OK;
225}
226
227
228/**
229 * Uninitializes the instance and sets the ready flag to FALSE.
230 * Called either from FinalRelease() or by the parent when it gets destroyed.
231 */
232void USBController::uninit()
233{
234 LogFlowThisFunc(("\n"));
235
236 /* Enclose the state transition Ready->InUninit->NotReady */
237 AutoUninitSpan autoUninitSpan(this);
238 if (autoUninitSpan.uninitDone())
239 return;
240
241#ifdef VBOX_WITH_USB
242 // uninit all device filters on the list (it's a standard std::list not an ObjectsList
243 // so we must uninit() manually)
244 for (DeviceFilterList::iterator it = m->llDeviceFilters->begin();
245 it != m->llDeviceFilters->end();
246 ++it)
247 (*it)->uninit();
248
249 m->llDeviceFilters.free();
250#endif
251 m->bd.free();
252
253 unconst(m->pPeer) = NULL;
254 unconst(m->pParent) = NULL;
255
256 delete m;
257 m = NULL;
258}
259
260
261// IUSBController properties
262/////////////////////////////////////////////////////////////////////////////
263
264STDMETHODIMP USBController::COMGETTER(Enabled) (BOOL *aEnabled)
265{
266 CheckComArgOutPointerValid(aEnabled);
267
268 AutoCaller autoCaller(this);
269 if (FAILED(autoCaller.rc())) return autoCaller.rc();
270
271 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
272
273 *aEnabled = m->bd->fEnabled;
274
275 return S_OK;
276}
277
278
279STDMETHODIMP USBController::COMSETTER(Enabled) (BOOL aEnabled)
280{
281 LogFlowThisFunc(("aEnabled=%RTbool\n", aEnabled));
282
283 AutoCaller autoCaller(this);
284 if (FAILED(autoCaller.rc())) return autoCaller.rc();
285
286 /* the machine needs to be mutable */
287 AutoMutableStateDependency adep(m->pParent);
288 if (FAILED(adep.rc())) return adep.rc();
289
290 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
291
292 if (m->bd->fEnabled != aEnabled)
293 {
294 m->bd.backup();
295 m->bd->fEnabled = aEnabled;
296
297 // leave the lock for safety
298 alock.release();
299
300 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
301 m->pParent->setModified(Machine::IsModified_USB);
302 mlock.release();
303
304 m->pParent->onUSBControllerChange();
305 }
306
307 return S_OK;
308}
309
310STDMETHODIMP USBController::COMGETTER(EnabledEhci) (BOOL *aEnabled)
311{
312 CheckComArgOutPointerValid(aEnabled);
313
314 AutoCaller autoCaller(this);
315 if (FAILED(autoCaller.rc())) return autoCaller.rc();
316
317 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
318
319 *aEnabled = m->bd->fEnabledEHCI;
320
321 return S_OK;
322}
323
324STDMETHODIMP USBController::COMSETTER(EnabledEhci) (BOOL aEnabled)
325{
326 LogFlowThisFunc(("aEnabled=%RTbool\n", aEnabled));
327
328 AutoCaller autoCaller(this);
329 if (FAILED(autoCaller.rc())) return autoCaller.rc();
330
331 /* the machine needs to be mutable */
332 AutoMutableStateDependency adep(m->pParent);
333 if (FAILED(adep.rc())) return adep.rc();
334
335 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
336
337 if (m->bd->fEnabledEHCI != aEnabled)
338 {
339 m->bd.backup();
340 m->bd->fEnabledEHCI = aEnabled;
341
342 // leave the lock for safety
343 alock.release();
344
345 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
346 m->pParent->setModified(Machine::IsModified_USB);
347 mlock.release();
348
349 m->pParent->onUSBControllerChange();
350 }
351
352 return S_OK;
353}
354
355STDMETHODIMP USBController::COMGETTER(ProxyAvailable) (BOOL *aEnabled)
356{
357 CheckComArgOutPointerValid(aEnabled);
358
359 AutoCaller autoCaller(this);
360 if (FAILED(autoCaller.rc())) return autoCaller.rc();
361
362 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
363
364#ifdef VBOX_WITH_USB
365 *aEnabled = true;
366#else
367 *aEnabled = false;
368#endif
369
370 return S_OK;
371}
372
373STDMETHODIMP USBController::COMGETTER(USBStandard) (USHORT *aUSBStandard)
374{
375 CheckComArgOutPointerValid(aUSBStandard);
376
377 AutoCaller autoCaller(this);
378 if (FAILED(autoCaller.rc())) return autoCaller.rc();
379
380 /* not accessing data -- no need to lock */
381
382 /** @todo This is no longer correct */
383 *aUSBStandard = 0x0101;
384
385 return S_OK;
386}
387
388#ifndef VBOX_WITH_USB
389/**
390 * Fake class for build without USB.
391 * We need an empty collection & enum for deviceFilters, that's all.
392 */
393class ATL_NO_VTABLE USBDeviceFilter :
394 public VirtualBoxBase,
395 VBOX_SCRIPTABLE_IMPL(IUSBDeviceFilter)
396{
397public:
398 DECLARE_NOT_AGGREGATABLE(USBDeviceFilter)
399 DECLARE_PROTECT_FINAL_CONSTRUCT()
400 BEGIN_COM_MAP(USBDeviceFilter)
401 COM_INTERFACE_ENTRY(ISupportErrorInfo)
402 COM_INTERFACE_ENTRY(IUSBDeviceFilter)
403 END_COM_MAP()
404
405 DECLARE_EMPTY_CTOR_DTOR (USBDeviceFilter)
406
407 // IUSBDeviceFilter properties
408 STDMETHOD(COMGETTER(Name)) (BSTR *aName);
409 STDMETHOD(COMSETTER(Name)) (IN_BSTR aName);
410 STDMETHOD(COMGETTER(Active)) (BOOL *aActive);
411 STDMETHOD(COMSETTER(Active)) (BOOL aActive);
412 STDMETHOD(COMGETTER(VendorId)) (BSTR *aVendorId);
413 STDMETHOD(COMSETTER(VendorId)) (IN_BSTR aVendorId);
414 STDMETHOD(COMGETTER(ProductId)) (BSTR *aProductId);
415 STDMETHOD(COMSETTER(ProductId)) (IN_BSTR aProductId);
416 STDMETHOD(COMGETTER(Revision)) (BSTR *aRevision);
417 STDMETHOD(COMSETTER(Revision)) (IN_BSTR aRevision);
418 STDMETHOD(COMGETTER(Manufacturer)) (BSTR *aManufacturer);
419 STDMETHOD(COMSETTER(Manufacturer)) (IN_BSTR aManufacturer);
420 STDMETHOD(COMGETTER(Product)) (BSTR *aProduct);
421 STDMETHOD(COMSETTER(Product)) (IN_BSTR aProduct);
422 STDMETHOD(COMGETTER(SerialNumber)) (BSTR *aSerialNumber);
423 STDMETHOD(COMSETTER(SerialNumber)) (IN_BSTR aSerialNumber);
424 STDMETHOD(COMGETTER(Port)) (BSTR *aPort);
425 STDMETHOD(COMSETTER(Port)) (IN_BSTR aPort);
426 STDMETHOD(COMGETTER(Remote)) (BSTR *aRemote);
427 STDMETHOD(COMSETTER(Remote)) (IN_BSTR aRemote);
428 STDMETHOD(COMGETTER(MaskedInterfaces)) (ULONG *aMaskedIfs);
429 STDMETHOD(COMSETTER(MaskedInterfaces)) (ULONG aMaskedIfs);
430};
431#endif /* !VBOX_WITH_USB */
432
433
434STDMETHODIMP USBController::COMGETTER(DeviceFilters) (ComSafeArrayOut(IUSBDeviceFilter *, aDevicesFilters))
435{
436#ifdef VBOX_WITH_USB
437 CheckComArgOutSafeArrayPointerValid(aDevicesFilters);
438
439 AutoCaller autoCaller(this);
440 if (FAILED(autoCaller.rc())) return autoCaller.rc();
441
442 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
443
444 SafeIfaceArray<IUSBDeviceFilter> collection (*m->llDeviceFilters.data());
445 collection.detachTo(ComSafeArrayOutArg(aDevicesFilters));
446
447 return S_OK;
448#else
449 NOREF(aDevicesFilters);
450# ifndef RT_OS_WINDOWS
451 NOREF(aDevicesFiltersSize);
452# endif
453 ReturnComNotImplemented();
454#endif
455}
456
457// IUSBController methods
458/////////////////////////////////////////////////////////////////////////////
459
460STDMETHODIMP USBController::CreateDeviceFilter (IN_BSTR aName,
461 IUSBDeviceFilter **aFilter)
462{
463#ifdef VBOX_WITH_USB
464 CheckComArgOutPointerValid(aFilter);
465
466 CheckComArgStrNotEmptyOrNull(aName);
467
468 AutoCaller autoCaller(this);
469 if (FAILED(autoCaller.rc())) return autoCaller.rc();
470
471 /* the machine needs to be mutable */
472 AutoMutableStateDependency adep(m->pParent);
473 if (FAILED(adep.rc())) return adep.rc();
474
475 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
476
477 ComObjPtr<USBDeviceFilter> filter;
478 filter.createObject();
479 HRESULT rc = filter->init (this, aName);
480 ComAssertComRCRetRC (rc);
481 rc = filter.queryInterfaceTo(aFilter);
482 AssertComRCReturnRC(rc);
483
484 return S_OK;
485#else
486 NOREF(aName);
487 NOREF(aFilter);
488 ReturnComNotImplemented();
489#endif
490}
491
492STDMETHODIMP USBController::InsertDeviceFilter(ULONG aPosition,
493 IUSBDeviceFilter *aFilter)
494{
495#ifdef VBOX_WITH_USB
496
497 CheckComArgNotNull(aFilter);
498
499 AutoCaller autoCaller(this);
500 if (FAILED(autoCaller.rc())) return autoCaller.rc();
501
502 /* the machine needs to be mutable */
503 AutoMutableStateDependency adep(m->pParent);
504 if (FAILED(adep.rc())) return adep.rc();
505
506 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
507
508 ComObjPtr<USBDeviceFilter> filter = static_cast<USBDeviceFilter*>(aFilter);
509 // @todo r=dj make sure the input object is actually from us
510// if (!filter)
511// return setError (E_INVALIDARG,
512// tr ("The given USB device filter is not created within "
513// "this VirtualBox instance"));
514
515 if (filter->mInList)
516 return setError(VBOX_E_INVALID_OBJECT_STATE,
517 tr("The given USB device filter is already in the list"));
518
519 /* backup the list before modification */
520 m->llDeviceFilters.backup();
521
522 /* iterate to the position... */
523 DeviceFilterList::iterator it;
524 if (aPosition < m->llDeviceFilters->size())
525 {
526 it = m->llDeviceFilters->begin();
527 std::advance (it, aPosition);
528 }
529 else
530 it = m->llDeviceFilters->end();
531 /* ...and insert */
532 m->llDeviceFilters->insert (it, filter);
533 filter->mInList = true;
534
535 /* notify the proxy (only when it makes sense) */
536 if (filter->getData().mActive && Global::IsOnline(adep.machineState()))
537 {
538 USBProxyService *service = m->pHost->usbProxyService();
539 ComAssertRet(service, E_FAIL);
540
541 ComAssertRet(filter->getId() == NULL, E_FAIL);
542 filter->getId() = service->insertFilter (&filter->getData().mUSBFilter);
543 }
544
545 alock.release();
546 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
547 m->pParent->setModified(Machine::IsModified_USB);
548 mlock.release();
549
550 return S_OK;
551
552#else /* VBOX_WITH_USB */
553
554 NOREF(aPosition);
555 NOREF(aFilter);
556 ReturnComNotImplemented();
557
558#endif /* VBOX_WITH_USB */
559}
560
561STDMETHODIMP USBController::RemoveDeviceFilter(ULONG aPosition,
562 IUSBDeviceFilter **aFilter)
563{
564#ifdef VBOX_WITH_USB
565
566 CheckComArgOutPointerValid(aFilter);
567
568 AutoCaller autoCaller(this);
569 if (FAILED(autoCaller.rc())) return autoCaller.rc();
570
571 /* the machine needs to be mutable */
572 AutoMutableStateDependency adep(m->pParent);
573 if (FAILED(adep.rc())) return adep.rc();
574
575 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
576
577 if (!m->llDeviceFilters->size())
578 return setError(E_INVALIDARG,
579 tr("The USB device filter list is empty"));
580
581 if (aPosition >= m->llDeviceFilters->size())
582 return setError(E_INVALIDARG,
583 tr("Invalid position: %lu (must be in range [0, %lu])"),
584 aPosition, m->llDeviceFilters->size() - 1);
585
586 /* backup the list before modification */
587 m->llDeviceFilters.backup();
588
589 ComObjPtr<USBDeviceFilter> filter;
590 {
591 /* iterate to the position... */
592 DeviceFilterList::iterator it = m->llDeviceFilters->begin();
593 std::advance (it, aPosition);
594 /* ...get an element from there... */
595 filter = *it;
596 /* ...and remove */
597 filter->mInList = false;
598 m->llDeviceFilters->erase (it);
599 }
600
601 /* cancel sharing (make an independent copy of data) */
602 filter->unshare();
603
604 filter.queryInterfaceTo(aFilter);
605
606 /* notify the proxy (only when it makes sense) */
607 if (filter->getData().mActive && Global::IsOnline(adep.machineState()))
608 {
609 USBProxyService *service = m->pHost->usbProxyService();
610 ComAssertRet(service, E_FAIL);
611
612 ComAssertRet(filter->getId() != NULL, E_FAIL);
613 service->removeFilter(filter->getId());
614 filter->getId() = NULL;
615 }
616
617 alock.release();
618 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
619 m->pParent->setModified(Machine::IsModified_USB);
620 mlock.release();
621
622 return S_OK;
623
624#else /* VBOX_WITH_USB */
625
626 NOREF(aPosition);
627 NOREF(aFilter);
628 ReturnComNotImplemented();
629
630#endif /* VBOX_WITH_USB */
631}
632
633// public methods only for internal purposes
634/////////////////////////////////////////////////////////////////////////////
635
636/**
637 * Loads settings from the given machine node.
638 * May be called once right after this object creation.
639 *
640 * @param aMachineNode <Machine> node.
641 *
642 * @note Does not lock "this" as Machine::loadHardware, which calls this, does not lock either.
643 */
644HRESULT USBController::loadSettings(const settings::USBController &data)
645{
646 AutoCaller autoCaller(this);
647 AssertComRCReturnRC(autoCaller.rc());
648
649 /* Note: we assume that the default values for attributes of optional
650 * nodes are assigned in the Data::Data() constructor and don't do it
651 * here. It implies that this method may only be called after constructing
652 * a new BIOSSettings object while all its data fields are in the default
653 * values. Exceptions are fields whose creation time defaults don't match
654 * values that should be applied when these fields are not explicitly set
655 * in the settings file (for backwards compatibility reasons). This takes
656 * place when a setting of a newly created object must default to A while
657 * the same setting of an object loaded from the old settings file must
658 * default to B. */
659
660 m->bd->fEnabled = data.fEnabled;
661 m->bd->fEnabledEHCI = data.fEnabledEHCI;
662
663#ifdef VBOX_WITH_USB
664 for (settings::USBDeviceFiltersList::const_iterator it = data.llDeviceFilters.begin();
665 it != data.llDeviceFilters.end();
666 ++it)
667 {
668 const settings::USBDeviceFilter &f = *it;
669 ComObjPtr<USBDeviceFilter> pFilter;
670 pFilter.createObject();
671 HRESULT rc = pFilter->init(this, // parent
672 f);
673 if (FAILED(rc)) return rc;
674
675 m->llDeviceFilters->push_back(pFilter);
676 pFilter->mInList = true;
677 }
678#endif /* VBOX_WITH_USB */
679
680 return S_OK;
681}
682
683/**
684 * Saves settings to the given machine node.
685 *
686 * @param aMachineNode <Machine> node.
687 *
688 * @note Locks this object for reading.
689 */
690HRESULT USBController::saveSettings(settings::USBController &data)
691{
692 AutoCaller autoCaller(this);
693 if (FAILED(autoCaller.rc())) return autoCaller.rc();
694
695 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
696
697 data.fEnabled = !!m->bd->fEnabled;
698 data.fEnabledEHCI = !!m->bd->fEnabledEHCI;
699
700#ifdef VBOX_WITH_USB
701 data.llDeviceFilters.clear();
702
703 for (DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
704 it != m->llDeviceFilters->end();
705 ++it)
706 {
707 AutoWriteLock filterLock(*it COMMA_LOCKVAL_SRC_POS);
708 const USBDeviceFilter::Data &filterData = (*it)->getData();
709
710 Bstr str;
711
712 settings::USBDeviceFilter f;
713 f.strName = filterData.mName;
714 f.fActive = !!filterData.mActive;
715 (*it)->COMGETTER(VendorId)(str.asOutParam());
716 f.strVendorId = str;
717 (*it)->COMGETTER(ProductId)(str.asOutParam());
718 f.strProductId = str;
719 (*it)->COMGETTER (Revision) (str.asOutParam());
720 f.strRevision = str;
721 (*it)->COMGETTER (Manufacturer) (str.asOutParam());
722 f.strManufacturer = str;
723 (*it)->COMGETTER (Product) (str.asOutParam());
724 f.strProduct = str;
725 (*it)->COMGETTER (SerialNumber) (str.asOutParam());
726 f.strSerialNumber = str;
727 (*it)->COMGETTER (Port) (str.asOutParam());
728 f.strPort = str;
729 f.strRemote = filterData.mRemote.string();
730 f.ulMaskedInterfaces = filterData.mMaskedIfs;
731
732 data.llDeviceFilters.push_back(f);
733 }
734#endif /* VBOX_WITH_USB */
735
736 return S_OK;
737}
738
739/** @note Locks objects for writing! */
740void USBController::rollback()
741{
742 AutoCaller autoCaller(this);
743 AssertComRCReturnVoid(autoCaller.rc());
744
745 /* we need the machine state */
746 AutoAnyStateDependency adep(m->pParent);
747 AssertComRCReturnVoid(adep.rc());
748
749 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
750
751 m->bd.rollback();
752
753#ifdef VBOX_WITH_USB
754
755 if (m->llDeviceFilters.isBackedUp())
756 {
757 USBProxyService *service = m->pHost->usbProxyService();
758 Assert(service);
759
760 /* uninitialize all new filters (absent in the backed up list) */
761 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
762 DeviceFilterList *backedList = m->llDeviceFilters.backedUpData();
763 while (it != m->llDeviceFilters->end())
764 {
765 if (std::find (backedList->begin(), backedList->end(), *it) ==
766 backedList->end())
767 {
768 /* notify the proxy (only when it makes sense) */
769 if ((*it)->getData().mActive &&
770 Global::IsOnline (adep.machineState()))
771 {
772 USBDeviceFilter *filter = *it;
773 Assert(filter->getId() != NULL);
774 service->removeFilter(filter->getId());
775 filter->getId() = NULL;
776 }
777
778 (*it)->uninit();
779 }
780 ++ it;
781 }
782
783 if (Global::IsOnline (adep.machineState()))
784 {
785 /* find all removed old filters (absent in the new list)
786 * and insert them back to the USB proxy */
787 it = backedList->begin();
788 while (it != backedList->end())
789 {
790 if (std::find (m->llDeviceFilters->begin(), m->llDeviceFilters->end(), *it) ==
791 m->llDeviceFilters->end())
792 {
793 /* notify the proxy (only when necessary) */
794 if ((*it)->getData().mActive)
795 {
796 USBDeviceFilter *flt = *it; /* resolve ambiguity */
797 Assert(flt->getId() == NULL);
798 flt->getId() = service->insertFilter(&flt->getData().mUSBFilter);
799 }
800 }
801 ++ it;
802 }
803 }
804
805 /* restore the list */
806 m->llDeviceFilters.rollback();
807 }
808
809 /* here we don't depend on the machine state any more */
810 adep.release();
811
812 /* rollback any changes to filters after restoring the list */
813 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
814 while (it != m->llDeviceFilters->end())
815 {
816 if ((*it)->isModified())
817 {
818 (*it)->rollback();
819 /* call this to notify the USB proxy about changes */
820 onDeviceFilterChange(*it);
821 }
822 ++it;
823 }
824
825#endif /* VBOX_WITH_USB */
826}
827
828/**
829 * @note Locks this object for writing, together with the peer object (also
830 * for writing) if there is one.
831 */
832void USBController::commit()
833{
834 /* sanity */
835 AutoCaller autoCaller(this);
836 AssertComRCReturnVoid (autoCaller.rc());
837
838 /* sanity too */
839 AutoCaller peerCaller(m->pPeer);
840 AssertComRCReturnVoid (peerCaller.rc());
841
842 /* lock both for writing since we modify both (mPeer is "master" so locked
843 * first) */
844 AutoMultiWriteLock2 alock(m->pPeer, this COMMA_LOCKVAL_SRC_POS);
845
846 if (m->bd.isBackedUp())
847 {
848 m->bd.commit();
849 if (m->pPeer)
850 {
851 /* attach new data to the peer and reshare it */
852 AutoWriteLock peerlock(m->pPeer COMMA_LOCKVAL_SRC_POS);
853 m->pPeer->m->bd.attach(m->bd);
854 }
855 }
856
857#ifdef VBOX_WITH_USB
858 bool commitFilters = false;
859
860 if (m->llDeviceFilters.isBackedUp())
861 {
862 m->llDeviceFilters.commit();
863
864 /* apply changes to peer */
865 if (m->pPeer)
866 {
867 AutoWriteLock peerlock(m->pPeer COMMA_LOCKVAL_SRC_POS);
868
869 /* commit all changes to new filters (this will reshare data with
870 * peers for those who have peers) */
871 DeviceFilterList *newList = new DeviceFilterList();
872 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
873 while (it != m->llDeviceFilters->end())
874 {
875 (*it)->commit();
876
877 /* look if this filter has a peer filter */
878 ComObjPtr<USBDeviceFilter> peer = (*it)->peer();
879 if (!peer)
880 {
881 /* no peer means the filter is a newly created one;
882 * create a peer owning data this filter share it with */
883 peer.createObject();
884 peer->init(m->pPeer, *it, true /* aReshare */);
885 }
886 else
887 {
888 /* remove peer from the old list */
889 m->pPeer->m->llDeviceFilters->remove(peer);
890 }
891 /* and add it to the new list */
892 newList->push_back (peer);
893
894 ++ it;
895 }
896
897 /* uninit old peer's filters that are left */
898 it = m->pPeer->m->llDeviceFilters->begin();
899 while (it != m->pPeer->m->llDeviceFilters->end())
900 {
901 (*it)->uninit();
902 ++ it;
903 }
904
905 /* attach new list of filters to our peer */
906 m->pPeer->m->llDeviceFilters.attach(newList);
907 }
908 else
909 {
910 /* we have no peer (our parent is the newly created machine);
911 * just commit changes to filters */
912 commitFilters = true;
913 }
914 }
915 else
916 {
917 /* the list of filters itself is not changed,
918 * just commit changes to filters themselves */
919 commitFilters = true;
920 }
921
922 if (commitFilters)
923 {
924 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
925 while (it != m->llDeviceFilters->end())
926 {
927 (*it)->commit();
928 ++ it;
929 }
930 }
931#endif /* VBOX_WITH_USB */
932}
933
934/**
935 * @note Locks this object for writing, together with the peer object
936 * represented by @a aThat (locked for reading).
937 */
938void USBController::copyFrom (USBController *aThat)
939{
940 AssertReturnVoid (aThat != NULL);
941
942 /* sanity */
943 AutoCaller autoCaller(this);
944 AssertComRCReturnVoid (autoCaller.rc());
945
946 /* sanity too */
947 AutoCaller thatCaller (aThat);
948 AssertComRCReturnVoid (thatCaller.rc());
949
950 /* even more sanity */
951 AutoAnyStateDependency adep(m->pParent);
952 AssertComRCReturnVoid (adep.rc());
953 /* Machine::copyFrom() may not be called when the VM is running */
954 AssertReturnVoid (!Global::IsOnline (adep.machineState()));
955
956 /* peer is not modified, lock it for reading (aThat is "master" so locked
957 * first) */
958 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
959 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
960
961 /* this will back up current data */
962 m->bd.assignCopy(aThat->m->bd);
963
964#ifdef VBOX_WITH_USB
965
966 /* Note that we won't inform the USB proxy about new filters since the VM is
967 * not running when we are here and therefore no need to do so */
968
969 /* create private copies of all filters */
970 m->llDeviceFilters.backup();
971 m->llDeviceFilters->clear();
972 for (DeviceFilterList::const_iterator it = aThat->m->llDeviceFilters->begin();
973 it != aThat->m->llDeviceFilters->end();
974 ++ it)
975 {
976 ComObjPtr<USBDeviceFilter> filter;
977 filter.createObject();
978 filter->initCopy (this, *it);
979 m->llDeviceFilters->push_back (filter);
980 }
981
982#endif /* VBOX_WITH_USB */
983}
984
985#ifdef VBOX_WITH_USB
986
987/**
988 * Called by setter methods of all USB device filters.
989 *
990 * @note Locks nothing.
991 */
992HRESULT USBController::onDeviceFilterChange (USBDeviceFilter *aFilter,
993 BOOL aActiveChanged /* = FALSE */)
994{
995 AutoCaller autoCaller(this);
996 AssertComRCReturnRC(autoCaller.rc());
997
998 /* we need the machine state */
999 AutoAnyStateDependency adep(m->pParent);
1000 AssertComRCReturnRC(adep.rc());
1001
1002 /* nothing to do if the machine isn't running */
1003 if (!Global::IsOnline (adep.machineState()))
1004 return S_OK;
1005
1006 /* we don't modify our data fields -- no need to lock */
1007
1008 if ( aFilter->mInList
1009 && m->pParent->isRegistered())
1010 {
1011 USBProxyService *service = m->pHost->usbProxyService();
1012 ComAssertRet(service, E_FAIL);
1013
1014 if (aActiveChanged)
1015 {
1016 /* insert/remove the filter from the proxy */
1017 if (aFilter->getData().mActive)
1018 {
1019 ComAssertRet(aFilter->getId() == NULL, E_FAIL);
1020 aFilter->getId() = service->insertFilter(&aFilter->getData().mUSBFilter);
1021 }
1022 else
1023 {
1024 ComAssertRet(aFilter->getId() != NULL, E_FAIL);
1025 service->removeFilter(aFilter->getId());
1026 aFilter->getId() = NULL;
1027 }
1028 }
1029 else
1030 {
1031 if (aFilter->getData().mActive)
1032 {
1033 /* update the filter in the proxy */
1034 ComAssertRet(aFilter->getId() != NULL, E_FAIL);
1035 service->removeFilter(aFilter->getId());
1036 aFilter->getId() = service->insertFilter(&aFilter->getData().mUSBFilter);
1037 }
1038 }
1039 }
1040
1041 return S_OK;
1042}
1043
1044/**
1045 * Returns true if the given USB device matches to at least one of
1046 * this controller's USB device filters.
1047 *
1048 * A HostUSBDevice specific version.
1049 *
1050 * @note Locks this object for reading.
1051 */
1052bool USBController::hasMatchingFilter (const ComObjPtr<HostUSBDevice> &aDevice, ULONG *aMaskedIfs)
1053{
1054 AutoCaller autoCaller(this);
1055 AssertComRCReturn (autoCaller.rc(), false);
1056
1057 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1058
1059 /* Disabled USB controllers cannot actually work with USB devices */
1060 if (!m->bd->fEnabled)
1061 return false;
1062
1063 /* apply self filters */
1064 for (DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
1065 it != m->llDeviceFilters->end();
1066 ++ it)
1067 {
1068 AutoWriteLock filterLock(*it COMMA_LOCKVAL_SRC_POS);
1069 if (aDevice->isMatch((*it)->getData()))
1070 {
1071 *aMaskedIfs = (*it)->getData().mMaskedIfs;
1072 return true;
1073 }
1074 }
1075
1076 return false;
1077}
1078
1079/**
1080 * Returns true if the given USB device matches to at least one of
1081 * this controller's USB device filters.
1082 *
1083 * A generic version that accepts any IUSBDevice on input.
1084 *
1085 * @note
1086 * This method MUST correlate with HostUSBDevice::isMatch()
1087 * in the sense of the device matching logic.
1088 *
1089 * @note Locks this object for reading.
1090 */
1091bool USBController::hasMatchingFilter (IUSBDevice *aUSBDevice, ULONG *aMaskedIfs)
1092{
1093 LogFlowThisFuncEnter();
1094
1095 AutoCaller autoCaller(this);
1096 AssertComRCReturn (autoCaller.rc(), false);
1097
1098 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1099
1100 /* Disabled USB controllers cannot actually work with USB devices */
1101 if (!m->bd->fEnabled)
1102 return false;
1103
1104 HRESULT rc = S_OK;
1105
1106 /* query fields */
1107 USBFILTER dev;
1108 USBFilterInit (&dev, USBFILTERTYPE_CAPTURE);
1109
1110 USHORT vendorId = 0;
1111 rc = aUSBDevice->COMGETTER(VendorId) (&vendorId);
1112 ComAssertComRCRet(rc, false);
1113 ComAssertRet(vendorId, false);
1114 int vrc = USBFilterSetNumExact (&dev, USBFILTERIDX_VENDOR_ID, vendorId, true); AssertRC(vrc);
1115
1116 USHORT productId = 0;
1117 rc = aUSBDevice->COMGETTER(ProductId) (&productId);
1118 ComAssertComRCRet(rc, false);
1119 vrc = USBFilterSetNumExact (&dev, USBFILTERIDX_PRODUCT_ID, productId, true); AssertRC(vrc);
1120
1121 USHORT revision;
1122 rc = aUSBDevice->COMGETTER(Revision) (&revision);
1123 ComAssertComRCRet(rc, false);
1124 vrc = USBFilterSetNumExact (&dev, USBFILTERIDX_DEVICE, revision, true); AssertRC(vrc);
1125
1126 Bstr manufacturer;
1127 rc = aUSBDevice->COMGETTER(Manufacturer) (manufacturer.asOutParam());
1128 ComAssertComRCRet(rc, false);
1129 if (!manufacturer.isEmpty())
1130 USBFilterSetStringExact (&dev, USBFILTERIDX_MANUFACTURER_STR, Utf8Str(manufacturer).c_str(), true);
1131
1132 Bstr product;
1133 rc = aUSBDevice->COMGETTER(Product) (product.asOutParam());
1134 ComAssertComRCRet(rc, false);
1135 if (!product.isEmpty())
1136 USBFilterSetStringExact (&dev, USBFILTERIDX_PRODUCT_STR, Utf8Str(product).c_str(), true);
1137
1138 Bstr serialNumber;
1139 rc = aUSBDevice->COMGETTER(SerialNumber) (serialNumber.asOutParam());
1140 ComAssertComRCRet(rc, false);
1141 if (!serialNumber.isEmpty())
1142 USBFilterSetStringExact (&dev, USBFILTERIDX_SERIAL_NUMBER_STR, Utf8Str(serialNumber).c_str(), true);
1143
1144 Bstr address;
1145 rc = aUSBDevice->COMGETTER(Address) (address.asOutParam());
1146 ComAssertComRCRet(rc, false);
1147
1148 USHORT port = 0;
1149 rc = aUSBDevice->COMGETTER(Port)(&port);
1150 ComAssertComRCRet(rc, false);
1151 USBFilterSetNumExact (&dev, USBFILTERIDX_PORT, port, true);
1152
1153 BOOL remote = FALSE;
1154 rc = aUSBDevice->COMGETTER(Remote)(&remote);
1155 ComAssertComRCRet(rc, false);
1156 ComAssertRet(remote == TRUE, false);
1157
1158 bool match = false;
1159
1160 /* apply self filters */
1161 for (DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
1162 it != m->llDeviceFilters->end();
1163 ++ it)
1164 {
1165 AutoWriteLock filterLock(*it COMMA_LOCKVAL_SRC_POS);
1166 const USBDeviceFilter::Data &aData = (*it)->getData();
1167
1168 if (!aData.mActive)
1169 continue;
1170 if (!aData.mRemote.isMatch (remote))
1171 continue;
1172 if (!USBFilterMatch (&aData.mUSBFilter, &dev))
1173 continue;
1174
1175 match = true;
1176 *aMaskedIfs = aData.mMaskedIfs;
1177 break;
1178 }
1179
1180 LogFlowThisFunc(("returns: %d\n", match));
1181 LogFlowThisFuncLeave();
1182
1183 return match;
1184}
1185
1186/**
1187 * Notifies the proxy service about all filters as requested by the
1188 * @a aInsertFilters argument.
1189 *
1190 * @param aInsertFilters @c true to insert filters, @c false to remove.
1191 *
1192 * @note Locks this object for reading.
1193 */
1194HRESULT USBController::notifyProxy (bool aInsertFilters)
1195{
1196 LogFlowThisFunc(("aInsertFilters=%RTbool\n", aInsertFilters));
1197
1198 AutoCaller autoCaller(this);
1199 AssertComRCReturn (autoCaller.rc(), false);
1200
1201 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1202
1203 USBProxyService *service = m->pHost->usbProxyService();
1204 AssertReturn(service, E_FAIL);
1205
1206 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
1207 while (it != m->llDeviceFilters->end())
1208 {
1209 USBDeviceFilter *flt = *it; /* resolve ambiguity (for ComPtr below) */
1210
1211 /* notify the proxy (only if the filter is active) */
1212 if (flt->getData().mActive)
1213 {
1214 if (aInsertFilters)
1215 {
1216 AssertReturn(flt->getId() == NULL, E_FAIL);
1217 flt->getId() = service->insertFilter(&flt->getData().mUSBFilter);
1218 }
1219 else
1220 {
1221 /* It's possible that the given filter was not inserted the proxy
1222 * when this method gets called (as a result of an early VM
1223 * process crash for example. So, don't assert that ID != NULL. */
1224 if (flt->getId() != NULL)
1225 {
1226 service->removeFilter(flt->getId());
1227 flt->getId() = NULL;
1228 }
1229 }
1230 }
1231 ++ it;
1232 }
1233
1234 return S_OK;
1235}
1236
1237Machine* USBController::getMachine()
1238{
1239 return m->pParent;
1240}
1241
1242#endif /* VBOX_WITH_USB */
1243
1244// private methods
1245/////////////////////////////////////////////////////////////////////////////
1246/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette