VirtualBox

source: vbox/trunk/src/VBox/Main/USBControllerImpl.cpp@ 22379

Last change on this file since 22379 was 22303, checked in by vboxsync, 15 years ago

Main/XML: fix USB filter data not being saved

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