VirtualBox

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

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

OSE build fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.8 KB
Line 
1/* $Id: USBControllerImpl.cpp 30856 2010-07-14 18:50:49Z 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()
62 : pParent(NULL)
63 {};
64
65 ~Data()
66 {};
67
68 /** Parent object. */
69 Machine * const pParent;
70 /** Peer object. */
71 const ComObjPtr<USBController> pPeer;
72
73 Backupable<BackupableUSBData> bd;
74#ifdef VBOX_WITH_USB
75 // the following fields need special backup/rollback/commit handling,
76 // so they cannot be a part of BackupableData
77 Backupable<DeviceFilterList> llDeviceFilters;
78#endif
79};
80
81
82
83// constructor / destructor
84/////////////////////////////////////////////////////////////////////////////
85
86DEFINE_EMPTY_CTOR_DTOR (USBController)
87
88HRESULT USBController::FinalConstruct()
89{
90 return S_OK;
91}
92
93void USBController::FinalRelease()
94{
95 uninit();
96}
97
98// public initializer/uninitializer for internal purposes only
99/////////////////////////////////////////////////////////////////////////////
100
101/**
102 * Initializes the USB controller object.
103 *
104 * @returns COM result indicator.
105 * @param aParent Pointer to our parent object.
106 */
107HRESULT USBController::init(Machine *aParent)
108{
109 LogFlowThisFunc(("aParent=%p\n", aParent));
110
111 ComAssertRet(aParent, E_INVALIDARG);
112
113 /* Enclose the state transition NotReady->InInit->Ready */
114 AutoInitSpan autoInitSpan(this);
115 AssertReturn(autoInitSpan.isOk(), E_FAIL);
116
117 m = new Data();
118
119 unconst(m->pParent) = aParent;
120 /* mPeer is left null */
121
122 m->bd.allocate();
123#ifdef VBOX_WITH_USB
124 m->llDeviceFilters.allocate();
125#endif
126
127 /* Confirm a successful initialization */
128 autoInitSpan.setSucceeded();
129
130 return S_OK;
131}
132
133/**
134 * Initializes the USB controller object given another USB controller object
135 * (a kind of copy constructor). This object shares data with
136 * the object passed as an argument.
137 *
138 * @returns COM result indicator.
139 * @param aParent Pointer to our parent object.
140 * @param aPeer The object to share.
141 *
142 * @note This object must be destroyed before the original object
143 * it shares data with is destroyed.
144 */
145HRESULT USBController::init(Machine *aParent, USBController *aPeer)
146{
147 LogFlowThisFunc(("aParent=%p, aPeer=%p\n", aParent, aPeer));
148
149 ComAssertRet(aParent && aPeer, E_INVALIDARG);
150
151 /* Enclose the state transition NotReady->InInit->Ready */
152 AutoInitSpan autoInitSpan(this);
153 AssertReturn(autoInitSpan.isOk(), E_FAIL);
154
155 m = new Data();
156
157 unconst(m->pParent) = aParent;
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();
200
201 unconst(m->pParent) = aParent;
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// ComObjPtr<USBDeviceFilter> filter = getDependentChild(aFilter);
511// if (!filter)
512// return setError (E_INVALIDARG,
513// tr ("The given USB device filter is not created within "
514// "this VirtualBox instance"));
515
516 if (filter->mInList)
517 return setError(VBOX_E_INVALID_OBJECT_STATE,
518 tr("The given USB device filter is already in the list"));
519
520 /* backup the list before modification */
521 m->llDeviceFilters.backup();
522
523 /* iterate to the position... */
524 DeviceFilterList::iterator it;
525 if (aPosition < m->llDeviceFilters->size())
526 {
527 it = m->llDeviceFilters->begin();
528 std::advance (it, aPosition);
529 }
530 else
531 it = m->llDeviceFilters->end();
532 /* ...and insert */
533 m->llDeviceFilters->insert (it, filter);
534 filter->mInList = true;
535
536 /* notify the proxy (only when it makes sense) */
537 if (filter->getData().mActive && Global::IsOnline(adep.machineState()))
538 {
539 USBProxyService *service = m->pParent->getVirtualBox()->host()->usbProxyService();
540 ComAssertRet(service, E_FAIL);
541
542 ComAssertRet(filter->getId() == NULL, E_FAIL);
543 filter->getId() = service->insertFilter (&filter->getData().mUSBFilter);
544 }
545
546 alock.release();
547 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
548 m->pParent->setModified(Machine::IsModified_USB);
549 mlock.release();
550
551 return S_OK;
552
553#else /* VBOX_WITH_USB */
554
555 NOREF(aPosition);
556 NOREF(aFilter);
557 ReturnComNotImplemented();
558
559#endif /* VBOX_WITH_USB */
560}
561
562STDMETHODIMP USBController::RemoveDeviceFilter(ULONG aPosition,
563 IUSBDeviceFilter **aFilter)
564{
565#ifdef VBOX_WITH_USB
566
567 CheckComArgOutPointerValid(aFilter);
568
569 AutoCaller autoCaller(this);
570 if (FAILED(autoCaller.rc())) return autoCaller.rc();
571
572 /* the machine needs to be mutable */
573 AutoMutableStateDependency adep(m->pParent);
574 if (FAILED(adep.rc())) return adep.rc();
575
576 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
577
578 if (!m->llDeviceFilters->size())
579 return setError(E_INVALIDARG,
580 tr("The USB device filter list is empty"));
581
582 if (aPosition >= m->llDeviceFilters->size())
583 return setError(E_INVALIDARG,
584 tr("Invalid position: %lu (must be in range [0, %lu])"),
585 aPosition, m->llDeviceFilters->size() - 1);
586
587 /* backup the list before modification */
588 m->llDeviceFilters.backup();
589
590 ComObjPtr<USBDeviceFilter> filter;
591 {
592 /* iterate to the position... */
593 DeviceFilterList::iterator it = m->llDeviceFilters->begin();
594 std::advance (it, aPosition);
595 /* ...get an element from there... */
596 filter = *it;
597 /* ...and remove */
598 filter->mInList = false;
599 m->llDeviceFilters->erase (it);
600 }
601
602 /* cancel sharing (make an independent copy of data) */
603 filter->unshare();
604
605 filter.queryInterfaceTo(aFilter);
606
607 /* notify the proxy (only when it makes sense) */
608 if (filter->getData().mActive && Global::IsOnline(adep.machineState()))
609 {
610 USBProxyService *service = m->pParent->getVirtualBox()->host()->usbProxyService();
611 ComAssertRet(service, E_FAIL);
612
613 ComAssertRet(filter->getId() != NULL, E_FAIL);
614 service->removeFilter(filter->getId());
615 filter->getId() = NULL;
616 }
617
618 alock.release();
619 AutoWriteLock mlock(m->pParent COMMA_LOCKVAL_SRC_POS);
620 m->pParent->setModified(Machine::IsModified_USB);
621 mlock.release();
622
623 return S_OK;
624
625#else /* VBOX_WITH_USB */
626
627 NOREF(aPosition);
628 NOREF(aFilter);
629 ReturnComNotImplemented();
630
631#endif /* VBOX_WITH_USB */
632}
633
634// public methods only for internal purposes
635/////////////////////////////////////////////////////////////////////////////
636
637/**
638 * Loads settings from the given machine node.
639 * May be called once right after this object creation.
640 *
641 * @param aMachineNode <Machine> node.
642 *
643 * @note Does not lock "this" as Machine::loadHardware, which calls this, does not lock either.
644 */
645HRESULT USBController::loadSettings(const settings::USBController &data)
646{
647 AutoCaller autoCaller(this);
648 AssertComRCReturnRC(autoCaller.rc());
649
650 /* Note: we assume that the default values for attributes of optional
651 * nodes are assigned in the Data::Data() constructor and don't do it
652 * here. It implies that this method may only be called after constructing
653 * a new BIOSSettings object while all its data fields are in the default
654 * values. Exceptions are fields whose creation time defaults don't match
655 * values that should be applied when these fields are not explicitly set
656 * in the settings file (for backwards compatibility reasons). This takes
657 * place when a setting of a newly created object must default to A while
658 * the same setting of an object loaded from the old settings file must
659 * default to B. */
660
661 m->bd->fEnabled = data.fEnabled;
662 m->bd->fEnabledEHCI = data.fEnabledEHCI;
663
664#ifdef VBOX_WITH_USB
665 for (settings::USBDeviceFiltersList::const_iterator it = data.llDeviceFilters.begin();
666 it != data.llDeviceFilters.end();
667 ++it)
668 {
669 const settings::USBDeviceFilter &f = *it;
670 ComObjPtr<USBDeviceFilter> pFilter;
671 pFilter.createObject();
672 HRESULT rc = pFilter->init(this, // parent
673 f);
674 if (FAILED(rc)) return rc;
675
676 m->llDeviceFilters->push_back(pFilter);
677 pFilter->mInList = true;
678 }
679#endif /* VBOX_WITH_USB */
680
681 return S_OK;
682}
683
684/**
685 * Saves settings to the given machine node.
686 *
687 * @param aMachineNode <Machine> node.
688 *
689 * @note Locks this object for reading.
690 */
691HRESULT USBController::saveSettings(settings::USBController &data)
692{
693 AutoCaller autoCaller(this);
694 if (FAILED(autoCaller.rc())) return autoCaller.rc();
695
696 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
697
698 data.fEnabled = !!m->bd->fEnabled;
699 data.fEnabledEHCI = !!m->bd->fEnabledEHCI;
700
701#ifdef VBOX_WITH_USB
702 data.llDeviceFilters.clear();
703
704 for (DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
705 it != m->llDeviceFilters->end();
706 ++it)
707 {
708 AutoWriteLock filterLock(*it COMMA_LOCKVAL_SRC_POS);
709 const USBDeviceFilter::Data &filterData = (*it)->getData();
710
711 Bstr str;
712
713 settings::USBDeviceFilter f;
714 f.strName = filterData.mName;
715 f.fActive = !!filterData.mActive;
716 (*it)->COMGETTER(VendorId)(str.asOutParam());
717 f.strVendorId = str;
718 (*it)->COMGETTER(ProductId)(str.asOutParam());
719 f.strProductId = str;
720 (*it)->COMGETTER (Revision) (str.asOutParam());
721 f.strRevision = str;
722 (*it)->COMGETTER (Manufacturer) (str.asOutParam());
723 f.strManufacturer = str;
724 (*it)->COMGETTER (Product) (str.asOutParam());
725 f.strProduct = str;
726 (*it)->COMGETTER (SerialNumber) (str.asOutParam());
727 f.strSerialNumber = str;
728 (*it)->COMGETTER (Port) (str.asOutParam());
729 f.strPort = str;
730 f.strRemote = filterData.mRemote.string();
731 f.ulMaskedInterfaces = filterData.mMaskedIfs;
732
733 data.llDeviceFilters.push_back(f);
734 }
735#endif /* VBOX_WITH_USB */
736
737 return S_OK;
738}
739
740/** @note Locks objects for writing! */
741void USBController::rollback()
742{
743 AutoCaller autoCaller(this);
744 AssertComRCReturnVoid(autoCaller.rc());
745
746 /* we need the machine state */
747 AutoAnyStateDependency adep(m->pParent);
748 AssertComRCReturnVoid(adep.rc());
749
750 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
751
752 m->bd.rollback();
753
754#ifdef VBOX_WITH_USB
755
756 if (m->llDeviceFilters.isBackedUp())
757 {
758 USBProxyService *service = m->pParent->getVirtualBox()->host()->usbProxyService();
759 Assert(service);
760
761 /* uninitialize all new filters (absent in the backed up list) */
762 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
763 DeviceFilterList *backedList = m->llDeviceFilters.backedUpData();
764 while (it != m->llDeviceFilters->end())
765 {
766 if (std::find (backedList->begin(), backedList->end(), *it) ==
767 backedList->end())
768 {
769 /* notify the proxy (only when it makes sense) */
770 if ((*it)->getData().mActive &&
771 Global::IsOnline (adep.machineState()))
772 {
773 USBDeviceFilter *filter = *it;
774 Assert(filter->getId() != NULL);
775 service->removeFilter(filter->getId());
776 filter->getId() = NULL;
777 }
778
779 (*it)->uninit();
780 }
781 ++ it;
782 }
783
784 if (Global::IsOnline (adep.machineState()))
785 {
786 /* find all removed old filters (absent in the new list)
787 * and insert them back to the USB proxy */
788 it = backedList->begin();
789 while (it != backedList->end())
790 {
791 if (std::find (m->llDeviceFilters->begin(), m->llDeviceFilters->end(), *it) ==
792 m->llDeviceFilters->end())
793 {
794 /* notify the proxy (only when necessary) */
795 if ((*it)->getData().mActive)
796 {
797 USBDeviceFilter *flt = *it; /* resolve ambiguity */
798 Assert(flt->getId() == NULL);
799 flt->getId() = service->insertFilter(&flt->getData().mUSBFilter);
800 }
801 }
802 ++ it;
803 }
804 }
805
806 /* restore the list */
807 m->llDeviceFilters.rollback();
808 }
809
810 /* here we don't depend on the machine state any more */
811 adep.release();
812
813 /* rollback any changes to filters after restoring the list */
814 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
815 while (it != m->llDeviceFilters->end())
816 {
817 if ((*it)->isModified())
818 {
819 (*it)->rollback();
820 /* call this to notify the USB proxy about changes */
821 onDeviceFilterChange(*it);
822 }
823 ++it;
824 }
825
826#endif /* VBOX_WITH_USB */
827}
828
829/**
830 * @note Locks this object for writing, together with the peer object (also
831 * for writing) if there is one.
832 */
833void USBController::commit()
834{
835 /* sanity */
836 AutoCaller autoCaller(this);
837 AssertComRCReturnVoid (autoCaller.rc());
838
839 /* sanity too */
840 AutoCaller peerCaller(m->pPeer);
841 AssertComRCReturnVoid (peerCaller.rc());
842
843 /* lock both for writing since we modify both (mPeer is "master" so locked
844 * first) */
845 AutoMultiWriteLock2 alock(m->pPeer, this COMMA_LOCKVAL_SRC_POS);
846
847 if (m->bd.isBackedUp())
848 {
849 m->bd.commit();
850 if (m->pPeer)
851 {
852 /* attach new data to the peer and reshare it */
853 AutoWriteLock peerlock(m->pPeer COMMA_LOCKVAL_SRC_POS);
854 m->pPeer->m->bd.attach(m->bd);
855 }
856 }
857
858#ifdef VBOX_WITH_USB
859 bool commitFilters = false;
860
861 if (m->llDeviceFilters.isBackedUp())
862 {
863 m->llDeviceFilters.commit();
864
865 /* apply changes to peer */
866 if (m->pPeer)
867 {
868 AutoWriteLock peerlock(m->pPeer COMMA_LOCKVAL_SRC_POS);
869
870 /* commit all changes to new filters (this will reshare data with
871 * peers for those who have peers) */
872 DeviceFilterList *newList = new DeviceFilterList();
873 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
874 while (it != m->llDeviceFilters->end())
875 {
876 (*it)->commit();
877
878 /* look if this filter has a peer filter */
879 ComObjPtr<USBDeviceFilter> peer = (*it)->peer();
880 if (!peer)
881 {
882 /* no peer means the filter is a newly created one;
883 * create a peer owning data this filter share it with */
884 peer.createObject();
885 peer->init(m->pPeer, *it, true /* aReshare */);
886 }
887 else
888 {
889 /* remove peer from the old list */
890 m->pPeer->m->llDeviceFilters->remove(peer);
891 }
892 /* and add it to the new list */
893 newList->push_back (peer);
894
895 ++ it;
896 }
897
898 /* uninit old peer's filters that are left */
899 it = m->pPeer->m->llDeviceFilters->begin();
900 while (it != m->pPeer->m->llDeviceFilters->end())
901 {
902 (*it)->uninit();
903 ++ it;
904 }
905
906 /* attach new list of filters to our peer */
907 m->pPeer->m->llDeviceFilters.attach(newList);
908 }
909 else
910 {
911 /* we have no peer (our parent is the newly created machine);
912 * just commit changes to filters */
913 commitFilters = true;
914 }
915 }
916 else
917 {
918 /* the list of filters itself is not changed,
919 * just commit changes to filters themselves */
920 commitFilters = true;
921 }
922
923 if (commitFilters)
924 {
925 DeviceFilterList::const_iterator it = m->llDeviceFilters->begin();
926 while (it != m->llDeviceFilters->end())
927 {
928 (*it)->commit();
929 ++ it;
930 }
931 }
932#endif /* VBOX_WITH_USB */
933}
934
935/**
936 * @note Locks this object for writing, together with the peer object
937 * represented by @a aThat (locked for reading).
938 */
939void USBController::copyFrom (USBController *aThat)
940{
941 AssertReturnVoid (aThat != NULL);
942
943 /* sanity */
944 AutoCaller autoCaller(this);
945 AssertComRCReturnVoid (autoCaller.rc());
946
947 /* sanity too */
948 AutoCaller thatCaller (aThat);
949 AssertComRCReturnVoid (thatCaller.rc());
950
951 /* even more sanity */
952 AutoAnyStateDependency adep(m->pParent);
953 AssertComRCReturnVoid (adep.rc());
954 /* Machine::copyFrom() may not be called when the VM is running */
955 AssertReturnVoid (!Global::IsOnline (adep.machineState()));
956
957 /* peer is not modified, lock it for reading (aThat is "master" so locked
958 * first) */
959 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
960 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
961
962 /* this will back up current data */
963 m->bd.assignCopy(aThat->m->bd);
964
965#ifdef VBOX_WITH_USB
966
967 /* Note that we won't inform the USB proxy about new filters since the VM is
968 * not running when we are here and therefore no need to do so */
969
970 /* create private copies of all filters */
971 m->llDeviceFilters.backup();
972 m->llDeviceFilters->clear();
973 for (DeviceFilterList::const_iterator it = aThat->m->llDeviceFilters->begin();
974 it != aThat->m->llDeviceFilters->end();
975 ++ it)
976 {
977 ComObjPtr<USBDeviceFilter> filter;
978 filter.createObject();
979 filter->initCopy (this, *it);
980 m->llDeviceFilters->push_back (filter);
981 }
982
983#endif /* VBOX_WITH_USB */
984}
985
986#ifdef VBOX_WITH_USB
987
988/**
989 * Called by setter methods of all USB device filters.
990 *
991 * @note Locks nothing.
992 */
993HRESULT USBController::onDeviceFilterChange (USBDeviceFilter *aFilter,
994 BOOL aActiveChanged /* = FALSE */)
995{
996 AutoCaller autoCaller(this);
997 AssertComRCReturnRC(autoCaller.rc());
998
999 /* we need the machine state */
1000 AutoAnyStateDependency adep(m->pParent);
1001 AssertComRCReturnRC(adep.rc());
1002
1003 /* nothing to do if the machine isn't running */
1004 if (!Global::IsOnline (adep.machineState()))
1005 return S_OK;
1006
1007 /* we don't modify our data fields -- no need to lock */
1008
1009 if (aFilter->mInList && m->pParent->isRegistered())
1010 {
1011 USBProxyService *service = m->pParent->getVirtualBox()->host()->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->pParent->getVirtualBox()->host()->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