VirtualBox

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

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

Main: use settings struct for machine user data; remove iprt::MiniString::raw() and change all occurences to c_str()

  • 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 31539 2010-08-10 15:40:18Z vboxsync $ */
2/** @file
3 * Implementation of IUSBController.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 S_OK;
93}
94
95void USBController::FinalRelease()
96{
97 uninit();
98}
99
100// public initializer/uninitializer for internal purposes only
101/////////////////////////////////////////////////////////////////////////////
102
103/**
104 * Initializes the USB controller object.
105 *
106 * @returns COM result indicator.
107 * @param aParent Pointer to our parent object.
108 */
109HRESULT USBController::init(Machine *aParent)
110{
111 LogFlowThisFunc(("aParent=%p\n", aParent));
112
113 ComAssertRet(aParent, E_INVALIDARG);
114
115 /* Enclose the state transition NotReady->InInit->Ready */
116 AutoInitSpan autoInitSpan(this);
117 AssertReturn(autoInitSpan.isOk(), E_FAIL);
118
119 m = new Data(aParent);
120
121 /* mPeer is left null */
122
123 m->bd.allocate();
124#ifdef VBOX_WITH_USB
125 m->llDeviceFilters.allocate();
126#endif
127
128 /* Confirm a successful initialization */
129 autoInitSpan.setSucceeded();
130
131 return S_OK;
132}
133
134/**
135 * Initializes the USB controller object given another USB controller object
136 * (a kind of copy constructor). This object shares data with
137 * the object passed as an argument.
138 *
139 * @returns COM result indicator.
140 * @param aParent Pointer to our parent object.
141 * @param aPeer The object to share.
142 *
143 * @note This object must be destroyed before the original object
144 * it shares data with is destroyed.
145 */
146HRESULT USBController::init(Machine *aParent, USBController *aPeer)
147{
148 LogFlowThisFunc(("aParent=%p, aPeer=%p\n", aParent, aPeer));
149
150 ComAssertRet(aParent && aPeer, E_INVALIDARG);
151
152 /* Enclose the state transition NotReady->InInit->Ready */
153 AutoInitSpan autoInitSpan(this);
154 AssertReturn(autoInitSpan.isOk(), E_FAIL);
155
156 m = new Data(aParent);
157
158 unconst(m->pPeer) = aPeer;
159
160 AutoWriteLock thatlock(aPeer COMMA_LOCKVAL_SRC_POS);
161 m->bd.share(aPeer->m->bd);
162
163#ifdef VBOX_WITH_USB
164 /* create copies of all filters */
165 m->llDeviceFilters.allocate();
166 DeviceFilterList::const_iterator it = aPeer->m->llDeviceFilters->begin();
167 while (it != aPeer->m->llDeviceFilters->end())
168 {
169 ComObjPtr<USBDeviceFilter> filter;
170 filter.createObject();
171 filter->init(this, *it);
172 m->llDeviceFilters->push_back(filter);
173 ++ it;
174 }
175#endif /* VBOX_WITH_USB */
176
177 /* Confirm a successful initialization */
178 autoInitSpan.setSucceeded();
179
180 return S_OK;
181}
182
183
184/**
185 * Initializes the USB controller object given another guest object
186 * (a kind of copy constructor). This object makes a private copy of data
187 * of the original object passed as an argument.
188 */
189HRESULT USBController::initCopy(Machine *aParent, USBController *aPeer)
190{
191 LogFlowThisFunc(("aParent=%p, aPeer=%p\n", aParent, aPeer));
192
193 ComAssertRet(aParent && aPeer, E_INVALIDARG);
194
195 /* Enclose the state transition NotReady->InInit->Ready */
196 AutoInitSpan autoInitSpan(this);
197 AssertReturn(autoInitSpan.isOk(), E_FAIL);
198
199 m = new Data(aParent);
200
201 /* mPeer is left null */
202
203 AutoWriteLock thatlock(aPeer COMMA_LOCKVAL_SRC_POS);
204 m->bd.attachCopy(aPeer->m->bd);
205
206#ifdef VBOX_WITH_USB
207 /* create private copies of all filters */
208 m->llDeviceFilters.allocate();
209 DeviceFilterList::const_iterator it = aPeer->m->llDeviceFilters->begin();
210 while (it != aPeer->m->llDeviceFilters->end())
211 {
212 ComObjPtr<USBDeviceFilter> filter;
213 filter.createObject();
214 filter->initCopy(this, *it);
215 m->llDeviceFilters->push_back(filter);
216 ++ it;
217 }
218#endif /* VBOX_WITH_USB */
219
220 /* Confirm a successful initialization */
221 autoInitSpan.setSucceeded();
222
223 return S_OK;
224}
225
226
227/**
228 * Uninitializes the instance and sets the ready flag to FALSE.
229 * Called either from FinalRelease() or by the parent when it gets destroyed.
230 */
231void USBController::uninit()
232{
233 LogFlowThisFunc(("\n"));
234
235 /* Enclose the state transition Ready->InUninit->NotReady */
236 AutoUninitSpan autoUninitSpan(this);
237 if (autoUninitSpan.uninitDone())
238 return;
239
240#ifdef VBOX_WITH_USB
241 // uninit all device filters on the list (it's a standard std::list not an ObjectsList
242 // so we must uninit() manually)
243 for (DeviceFilterList::iterator it = m->llDeviceFilters->begin();
244 it != m->llDeviceFilters->end();
245 ++it)
246 (*it)->uninit();
247
248 m->llDeviceFilters.free();
249#endif
250 m->bd.free();
251
252 unconst(m->pPeer) = NULL;
253 unconst(m->pParent) = NULL;
254
255 delete m;
256 m = NULL;
257}
258
259
260// IUSBController properties
261/////////////////////////////////////////////////////////////////////////////
262
263STDMETHODIMP USBController::COMGETTER(Enabled) (BOOL *aEnabled)
264{
265 CheckComArgOutPointerValid(aEnabled);
266
267 AutoCaller autoCaller(this);
268 if (FAILED(autoCaller.rc())) return autoCaller.rc();
269
270 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
271
272 *aEnabled = m->bd->fEnabled;
273
274 return S_OK;
275}
276
277
278STDMETHODIMP USBController::COMSETTER(Enabled) (BOOL aEnabled)
279{
280 LogFlowThisFunc(("aEnabled=%RTbool\n", aEnabled));
281
282 AutoCaller autoCaller(this);
283 if (FAILED(autoCaller.rc())) return autoCaller.rc();
284
285 /* the machine needs to be mutable */
286 AutoMutableStateDependency adep(m->pParent);
287 if (FAILED(adep.rc())) return adep.rc();
288
289 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
290
291 if (m->bd->fEnabled != aEnabled)
292 {
293 m->bd.backup();
294 m->bd->fEnabled = aEnabled;
295
296 // leave the lock for safety
297 alock.release();
298
299 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
300 m->pParent->setModified(Machine::IsModified_USB);
301 mlock.release();
302
303 m->pParent->onUSBControllerChange();
304 }
305
306 return S_OK;
307}
308
309STDMETHODIMP USBController::COMGETTER(EnabledEhci) (BOOL *aEnabled)
310{
311 CheckComArgOutPointerValid(aEnabled);
312
313 AutoCaller autoCaller(this);
314 if (FAILED(autoCaller.rc())) return autoCaller.rc();
315
316 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
317
318 *aEnabled = m->bd->fEnabledEHCI;
319
320 return S_OK;
321}
322
323STDMETHODIMP USBController::COMSETTER(EnabledEhci) (BOOL aEnabled)
324{
325 LogFlowThisFunc(("aEnabled=%RTbool\n", aEnabled));
326
327 AutoCaller autoCaller(this);
328 if (FAILED(autoCaller.rc())) return autoCaller.rc();
329
330 /* the machine needs to be mutable */
331 AutoMutableStateDependency adep(m->pParent);
332 if (FAILED(adep.rc())) return adep.rc();
333
334 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
335
336 if (m->bd->fEnabledEHCI != aEnabled)
337 {
338 m->bd.backup();
339 m->bd->fEnabledEHCI = aEnabled;
340
341 // leave the lock for safety
342 alock.release();
343
344 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
345 m->pParent->setModified(Machine::IsModified_USB);
346 mlock.release();
347
348 m->pParent->onUSBControllerChange();
349 }
350
351 return S_OK;
352}
353
354STDMETHODIMP USBController::COMGETTER(ProxyAvailable) (BOOL *aEnabled)
355{
356 CheckComArgOutPointerValid(aEnabled);
357
358 AutoCaller autoCaller(this);
359 if (FAILED(autoCaller.rc())) return autoCaller.rc();
360
361 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
362
363#ifdef VBOX_WITH_USB
364 *aEnabled = true;
365#else
366 *aEnabled = false;
367#endif
368
369 return S_OK;
370}
371
372STDMETHODIMP USBController::COMGETTER(USBStandard) (USHORT *aUSBStandard)
373{
374 CheckComArgOutPointerValid(aUSBStandard);
375
376 AutoCaller autoCaller(this);
377 if (FAILED(autoCaller.rc())) return autoCaller.rc();
378
379 /* not accessing data -- no need to lock */
380
381 /** @todo This is no longer correct */
382 *aUSBStandard = 0x0101;
383
384 return S_OK;
385}
386
387#ifndef VBOX_WITH_USB
388/**
389 * Fake class for build without USB.
390 * We need an empty collection & enum for deviceFilters, that's all.
391 */
392class ATL_NO_VTABLE USBDeviceFilter :
393 public VirtualBoxBase,
394 VBOX_SCRIPTABLE_IMPL(IUSBDeviceFilter)
395{
396public:
397 DECLARE_NOT_AGGREGATABLE(USBDeviceFilter)
398 DECLARE_PROTECT_FINAL_CONSTRUCT()
399 BEGIN_COM_MAP(USBDeviceFilter)
400 COM_INTERFACE_ENTRY(ISupportErrorInfo)
401 COM_INTERFACE_ENTRY(IUSBDeviceFilter)
402 END_COM_MAP()
403
404 DECLARE_EMPTY_CTOR_DTOR (USBDeviceFilter)
405
406 // IUSBDeviceFilter properties
407 STDMETHOD(COMGETTER(Name)) (BSTR *aName);
408 STDMETHOD(COMSETTER(Name)) (IN_BSTR aName);
409 STDMETHOD(COMGETTER(Active)) (BOOL *aActive);
410 STDMETHOD(COMSETTER(Active)) (BOOL aActive);
411 STDMETHOD(COMGETTER(VendorId)) (BSTR *aVendorId);
412 STDMETHOD(COMSETTER(VendorId)) (IN_BSTR aVendorId);
413 STDMETHOD(COMGETTER(ProductId)) (BSTR *aProductId);
414 STDMETHOD(COMSETTER(ProductId)) (IN_BSTR aProductId);
415 STDMETHOD(COMGETTER(Revision)) (BSTR *aRevision);
416 STDMETHOD(COMSETTER(Revision)) (IN_BSTR aRevision);
417 STDMETHOD(COMGETTER(Manufacturer)) (BSTR *aManufacturer);
418 STDMETHOD(COMSETTER(Manufacturer)) (IN_BSTR aManufacturer);
419 STDMETHOD(COMGETTER(Product)) (BSTR *aProduct);
420 STDMETHOD(COMSETTER(Product)) (IN_BSTR aProduct);
421 STDMETHOD(COMGETTER(SerialNumber)) (BSTR *aSerialNumber);
422 STDMETHOD(COMSETTER(SerialNumber)) (IN_BSTR aSerialNumber);
423 STDMETHOD(COMGETTER(Port)) (BSTR *aPort);
424 STDMETHOD(COMSETTER(Port)) (IN_BSTR aPort);
425 STDMETHOD(COMGETTER(Remote)) (BSTR *aRemote);
426 STDMETHOD(COMSETTER(Remote)) (IN_BSTR aRemote);
427 STDMETHOD(COMGETTER(MaskedInterfaces)) (ULONG *aMaskedIfs);
428 STDMETHOD(COMSETTER(MaskedInterfaces)) (ULONG aMaskedIfs);
429};
430#endif /* !VBOX_WITH_USB */
431
432
433STDMETHODIMP USBController::COMGETTER(DeviceFilters) (ComSafeArrayOut(IUSBDeviceFilter *, aDevicesFilters))
434{
435#ifdef VBOX_WITH_USB
436 CheckComArgOutSafeArrayPointerValid(aDevicesFilters);
437
438 AutoCaller autoCaller(this);
439 if (FAILED(autoCaller.rc())) return autoCaller.rc();
440
441 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
442
443 SafeIfaceArray<IUSBDeviceFilter> collection (*m->llDeviceFilters.data());
444 collection.detachTo(ComSafeArrayOutArg(aDevicesFilters));
445
446 return S_OK;
447#else
448 NOREF(aDevicesFilters);
449# ifndef RT_OS_WINDOWS
450 NOREF(aDevicesFiltersSize);
451# endif
452 ReturnComNotImplemented();
453#endif
454}
455
456// IUSBController methods
457/////////////////////////////////////////////////////////////////////////////
458
459STDMETHODIMP USBController::CreateDeviceFilter (IN_BSTR aName,
460 IUSBDeviceFilter **aFilter)
461{
462#ifdef VBOX_WITH_USB
463 CheckComArgOutPointerValid(aFilter);
464
465 CheckComArgStrNotEmptyOrNull(aName);
466
467 AutoCaller autoCaller(this);
468 if (FAILED(autoCaller.rc())) return autoCaller.rc();
469
470 /* the machine needs to be mutable */
471 AutoMutableStateDependency adep(m->pParent);
472 if (FAILED(adep.rc())) return adep.rc();
473
474 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
475
476 ComObjPtr<USBDeviceFilter> filter;
477 filter.createObject();
478 HRESULT rc = filter->init (this, aName);
479 ComAssertComRCRetRC (rc);
480 rc = filter.queryInterfaceTo(aFilter);
481 AssertComRCReturnRC(rc);
482
483 return S_OK;
484#else
485 NOREF(aName);
486 NOREF(aFilter);
487 ReturnComNotImplemented();
488#endif
489}
490
491STDMETHODIMP USBController::InsertDeviceFilter(ULONG aPosition,
492 IUSBDeviceFilter *aFilter)
493{
494#ifdef VBOX_WITH_USB
495
496 CheckComArgNotNull(aFilter);
497
498 AutoCaller autoCaller(this);
499 if (FAILED(autoCaller.rc())) return autoCaller.rc();
500
501 /* the machine needs to be mutable */
502 AutoMutableStateDependency adep(m->pParent);
503 if (FAILED(adep.rc())) return adep.rc();
504
505 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
506
507 ComObjPtr<USBDeviceFilter> filter = static_cast<USBDeviceFilter*>(aFilter);
508 // @todo r=dj make sure the input object is actually from us
509// ComObjPtr<USBDeviceFilter> filter = getDependentChild(aFilter);
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