VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/USBProxyService.cpp@ 60551

Last change on this file since 60551 was 60551, checked in by vboxsync, 9 years ago

Main/USBProxyService,USBProxyBackendUsbIp: Fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.5 KB
Line 
1/* $Id: USBProxyService.cpp 60551 2016-04-18 17:37:22Z vboxsync $ */
2/** @file
3 * VirtualBox USB Proxy Service (base) class.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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 "USBProxyService.h"
19#include "HostUSBDeviceImpl.h"
20#include "HostImpl.h"
21#include "MachineImpl.h"
22#include "VirtualBoxImpl.h"
23
24#include "AutoCaller.h"
25#include "Logging.h"
26
27#include <VBox/com/array.h>
28#include <VBox/err.h>
29#include <iprt/asm.h>
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/mem.h>
33#include <iprt/string.h>
34
35/** Pair of a USB proxy backend and the opaque filter data assigned by the backend. */
36typedef std::pair<ComObjPtr<USBProxyBackend> , void *> USBFilterPair;
37/** List of USB filter pairs. */
38typedef std::list<USBFilterPair> USBFilterList;
39
40/**
41 * Data for a USB device filter.
42 */
43struct USBFilterData
44{
45 USBFilterData()
46 : llUsbFilters()
47 { }
48
49 USBFilterList llUsbFilters;
50};
51
52/**
53 * Initialize data members.
54 */
55USBProxyService::USBProxyService(Host *aHost)
56 : mHost(aHost), mDevices(), mBackends()
57{
58 LogFlowThisFunc(("aHost=%p\n", aHost));
59}
60
61
62/**
63 * Stub needed as long as the class isn't virtual
64 */
65HRESULT USBProxyService::init(void)
66{
67# if defined(RT_OS_DARWIN)
68 ComObjPtr<USBProxyBackendDarwin> UsbProxyBackendHost;
69# elif defined(RT_OS_LINUX)
70 ComObjPtr<USBProxyBackendLinux> UsbProxyBackendHost;
71# elif defined(RT_OS_OS2)
72 ComObjPtr<USBProxyBackendOs2> UsbProxyBackendHost;
73# elif defined(RT_OS_SOLARIS)
74 ComObjPtr<USBProxyBackendSolaris> UsbProxyBackendHost;
75# elif defined(RT_OS_WINDOWS)
76 ComObjPtr<USBProxyBackendWindows> UsbProxyBackendHost;
77# elif defined(RT_OS_FREEBSD)
78 ComObjPtr<USBProxyBackendFreeBSD> UsbProxyBackendHost;
79# else
80 ComObjPtr<USBProxyBackend> UsbProxyBackendHost;
81# endif
82 UsbProxyBackendHost.createObject();
83 int vrc = UsbProxyBackendHost->init(this, Utf8Str("host"), Utf8Str(""));
84 if (RT_FAILURE(vrc))
85 {
86 mLastError = vrc;
87 }
88 else
89 mBackends.push_back(static_cast<ComObjPtr<USBProxyBackend> >(UsbProxyBackendHost));
90
91 return S_OK;
92}
93
94
95/**
96 * Empty destructor.
97 */
98USBProxyService::~USBProxyService()
99{
100 LogFlowThisFunc(("\n"));
101 while (!mBackends.empty())
102 mBackends.pop_front();
103
104 mDevices.clear();
105 mBackends.clear();
106 mHost = NULL;
107}
108
109
110/**
111 * Query if the service is active and working.
112 *
113 * @returns true if the service is up running.
114 * @returns false if the service isn't running.
115 */
116bool USBProxyService::isActive(void)
117{
118 return mBackends.size() > 0;
119}
120
121
122/**
123 * Get last error.
124 * Can be used to check why the proxy !isActive() upon construction.
125 *
126 * @returns VBox status code.
127 */
128int USBProxyService::getLastError(void)
129{
130 return mLastError;
131}
132
133
134/**
135 * We're using the Host object lock.
136 *
137 * This is just a temporary measure until all the USB refactoring is
138 * done, probably... For now it help avoiding deadlocks we don't have
139 * time to fix.
140 *
141 * @returns Lock handle.
142 */
143RWLockHandle *USBProxyService::lockHandle() const
144{
145 return mHost->lockHandle();
146}
147
148
149void *USBProxyService::insertFilter(PCUSBFILTER aFilter)
150{
151 USBFilterData *pFilterData = new USBFilterData();
152
153 for (USBProxyBackendList::iterator it = mBackends.begin();
154 it != mBackends.end();
155 ++it)
156 {
157 ComObjPtr<USBProxyBackend> pUsbProxyBackend = *it;
158 void *pvId = pUsbProxyBackend->insertFilter(aFilter);
159
160 pFilterData->llUsbFilters.push_back(USBFilterPair(pUsbProxyBackend, pvId));
161 }
162
163 return pFilterData;
164}
165
166void USBProxyService::removeFilter(void *aId)
167{
168 USBFilterData *pFilterData = (USBFilterData *)aId;
169
170 for (USBFilterList::iterator it = pFilterData->llUsbFilters.begin();
171 it != pFilterData->llUsbFilters.end();
172 ++it)
173 {
174 ComObjPtr<USBProxyBackend> pUsbProxyBackend = it->first;
175 pUsbProxyBackend->removeFilter(it->second);
176 }
177
178 pFilterData->llUsbFilters.clear();
179 delete pFilterData;
180}
181
182/**
183 * Gets the collection of USB devices, slave of Host::USBDevices.
184 *
185 * This is an interface for the HostImpl::USBDevices property getter.
186 *
187 *
188 * @param aUSBDevices Where to store the pointer to the collection.
189 *
190 * @returns COM status code.
191 *
192 * @remarks The caller must own the write lock of the host object.
193 */
194HRESULT USBProxyService::getDeviceCollection(std::vector<ComPtr<IHostUSBDevice> > &aUSBDevices)
195{
196 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
197
198 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
199
200 aUSBDevices.resize(mDevices.size());
201 size_t i = 0;
202 for (HostUSBDeviceList::const_iterator it = mDevices.begin(); it != mDevices.end(); ++it, ++i)
203 aUSBDevices[i] = *it;
204
205 return S_OK;
206}
207
208
209HRESULT USBProxyService::addUSBDeviceSource(const com::Utf8Str &aBackend, const com::Utf8Str &aId, const com::Utf8Str &aAddress,
210 const std::vector<com::Utf8Str> &aPropertyNames, const std::vector<com::Utf8Str> &aPropertyValues)
211{
212 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
213
214 HRESULT hrc = createUSBDeviceSource(aBackend, aId, aAddress, aPropertyNames, aPropertyValues);
215 if (SUCCEEDED(hrc))
216 {
217 alock.release();
218 AutoWriteLock vboxLock(mHost->i_parent() COMMA_LOCKVAL_SRC_POS);
219 return mHost->i_parent()->i_saveSettings();
220 }
221
222 return hrc;
223}
224
225HRESULT USBProxyService::removeUSBDeviceSource(const com::Utf8Str &aId)
226{
227 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
228
229 for (USBProxyBackendList::iterator it = mBackends.begin();
230 it != mBackends.end();
231 ++it)
232 {
233 ComObjPtr<USBProxyBackend> UsbProxyBackend = *it;
234
235 if (aId.equals(UsbProxyBackend->i_getId()))
236 {
237 mBackends.erase(it);
238
239 /*
240 * The proxy backend uninit method will be called when the pointer goes
241 * out of scope.
242 */
243
244 alock.release();
245 AutoWriteLock vboxLock(mHost->i_parent() COMMA_LOCKVAL_SRC_POS);
246 return mHost->i_parent()->i_saveSettings();
247 }
248 }
249
250 return setError(VBOX_E_OBJECT_NOT_FOUND,
251 tr("The USB device source \"%s\" could not be found"), aId.c_str());
252}
253
254/**
255 * Request capture of a specific device.
256 *
257 * This is in an interface for SessionMachine::CaptureUSBDevice(), which is
258 * an internal worker used by Console::AttachUSBDevice() from the VM process.
259 *
260 * When the request is completed, SessionMachine::onUSBDeviceAttach() will
261 * be called for the given machine object.
262 *
263 *
264 * @param aMachine The machine to attach the device to.
265 * @param aId The UUID of the USB device to capture and attach.
266 *
267 * @returns COM status code and error info.
268 *
269 * @remarks This method may operate synchronously as well as asynchronously. In the
270 * former case it will temporarily abandon locks because of IPC.
271 */
272HRESULT USBProxyService::captureDeviceForVM(SessionMachine *aMachine, IN_GUID aId, const com::Utf8Str &aCaptureFilename)
273{
274 ComAssertRet(aMachine, E_INVALIDARG);
275 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
276
277 /*
278 * Translate the device id into a device object.
279 */
280 ComObjPtr<HostUSBDevice> pHostDevice = findDeviceById(aId);
281 if (pHostDevice.isNull())
282 return setError(E_INVALIDARG,
283 tr("The USB device with UUID {%RTuuid} is not currently attached to the host"), Guid(aId).raw());
284
285 /*
286 * Try to capture the device
287 */
288 alock.release();
289 return pHostDevice->i_requestCaptureForVM(aMachine, true /* aSetError */, aCaptureFilename);
290}
291
292
293/**
294 * Notification from VM process about USB device detaching progress.
295 *
296 * This is in an interface for SessionMachine::DetachUSBDevice(), which is
297 * an internal worker used by Console::DetachUSBDevice() from the VM process.
298 *
299 * @param aMachine The machine which is sending the notification.
300 * @param aId The UUID of the USB device is concerns.
301 * @param aDone \a false for the pre-action notification (necessary
302 * for advancing the device state to avoid confusing
303 * the guest).
304 * \a true for the post-action notification. The device
305 * will be subjected to all filters except those of
306 * of \a Machine.
307 *
308 * @returns COM status code.
309 *
310 * @remarks When \a aDone is \a true this method may end up doing IPC to other
311 * VMs when running filters. In these cases it will temporarily
312 * abandon its locks.
313 */
314HRESULT USBProxyService::detachDeviceFromVM(SessionMachine *aMachine, IN_GUID aId, bool aDone)
315{
316 LogFlowThisFunc(("aMachine=%p{%s} aId={%RTuuid} aDone=%RTbool\n",
317 aMachine,
318 aMachine->i_getName().c_str(),
319 Guid(aId).raw(),
320 aDone));
321
322 // get a list of all running machines while we're outside the lock
323 // (getOpenedMachines requests locks which are incompatible with the lock of the machines list)
324 SessionMachinesList llOpenedMachines;
325 mHost->i_parent()->i_getOpenedMachines(llOpenedMachines);
326
327 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
328
329 ComObjPtr<HostUSBDevice> pHostDevice = findDeviceById(aId);
330 ComAssertRet(!pHostDevice.isNull(), E_FAIL);
331 AutoWriteLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
332
333 /*
334 * Work the state machine.
335 */
336 LogFlowThisFunc(("id={%RTuuid} state=%s aDone=%RTbool name={%s}\n",
337 pHostDevice->i_getId().raw(), pHostDevice->i_getStateName(), aDone, pHostDevice->i_getName().c_str()));
338 bool fRunFilters = false;
339 HRESULT hrc = pHostDevice->i_onDetachFromVM(aMachine, aDone, &fRunFilters);
340
341 /*
342 * Run filters if necessary.
343 */
344 if ( SUCCEEDED(hrc)
345 && fRunFilters)
346 {
347 USBProxyBackend *pUsbProxyBackend = pHostDevice->i_getUsbProxyBackend();
348 Assert(aDone && pHostDevice->i_getUnistate() == kHostUSBDeviceState_HeldByProxy && pHostDevice->i_getMachine().isNull());
349 devLock.release();
350 alock.release();
351 HRESULT hrc2 = pUsbProxyBackend->runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine);
352 ComAssertComRC(hrc2);
353 }
354 return hrc;
355}
356
357
358/**
359 * Apply filters for the machine to all eligible USB devices.
360 *
361 * This is in an interface for SessionMachine::CaptureUSBDevice(), which
362 * is an internal worker used by Console::AutoCaptureUSBDevices() from the
363 * VM process at VM startup.
364 *
365 * Matching devices will be attached to the VM and may result IPC back
366 * to the VM process via SessionMachine::onUSBDeviceAttach() depending
367 * on whether the device needs to be captured or not. If capture is
368 * required, SessionMachine::onUSBDeviceAttach() will be called
369 * asynchronously by the USB proxy service thread.
370 *
371 * @param aMachine The machine to capture devices for.
372 *
373 * @returns COM status code, perhaps with error info.
374 *
375 * @remarks Temporarily locks this object, the machine object and some USB
376 * device, and the called methods will lock similar objects.
377 */
378HRESULT USBProxyService::autoCaptureDevicesForVM(SessionMachine *aMachine)
379{
380 LogFlowThisFunc(("aMachine=%p{%s}\n",
381 aMachine,
382 aMachine->i_getName().c_str()));
383
384 /*
385 * Make a copy of the list because we cannot hold the lock protecting it.
386 * (This will not make copies of any HostUSBDevice objects, only reference them.)
387 */
388 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
389 HostUSBDeviceList ListCopy = mDevices;
390 alock.release();
391
392 for (HostUSBDeviceList::iterator it = ListCopy.begin();
393 it != ListCopy.end();
394 ++it)
395 {
396 ComObjPtr<HostUSBDevice> pHostDevice = *it;
397 AutoReadLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
398 if ( pHostDevice->i_getUnistate() == kHostUSBDeviceState_HeldByProxy
399 || pHostDevice->i_getUnistate() == kHostUSBDeviceState_Unused
400 || pHostDevice->i_getUnistate() == kHostUSBDeviceState_Capturable)
401 {
402 USBProxyBackend *pUsbProxyBackend = pHostDevice->i_getUsbProxyBackend();
403 devLock.release();
404 pUsbProxyBackend->runMachineFilters(aMachine, pHostDevice);
405 }
406 }
407
408 return S_OK;
409}
410
411
412/**
413 * Detach all USB devices currently attached to a VM.
414 *
415 * This is in an interface for SessionMachine::DetachAllUSBDevices(), which
416 * is an internal worker used by Console::powerDown() from the VM process
417 * at VM startup, and SessionMachine::uninit() at VM abend.
418 *
419 * This is, like #detachDeviceFromVM(), normally a two stage journey
420 * where \a aDone indicates where we are. In addition we may be called
421 * to clean up VMs that have abended, in which case there will be no
422 * preparatory call. Filters will be applied to the devices in the final
423 * call with the risk that we have to do some IPC when attaching them
424 * to other VMs.
425 *
426 * @param aMachine The machine to detach devices from.
427 *
428 * @returns COM status code, perhaps with error info.
429 *
430 * @remarks Write locks the host object and may temporarily abandon
431 * its locks to perform IPC.
432 */
433HRESULT USBProxyService::detachAllDevicesFromVM(SessionMachine *aMachine, bool aDone, bool aAbnormal)
434{
435 // get a list of all running machines while we're outside the lock
436 // (getOpenedMachines requests locks which are incompatible with the host object lock)
437 SessionMachinesList llOpenedMachines;
438 mHost->i_parent()->i_getOpenedMachines(llOpenedMachines);
439
440 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
441
442 /*
443 * Make a copy of the device list (not the HostUSBDevice objects, just
444 * the list) since we may end up performing IPC and temporarily have
445 * to abandon locks when applying filters.
446 */
447 HostUSBDeviceList ListCopy = mDevices;
448
449 for (HostUSBDeviceList::iterator it = ListCopy.begin();
450 it != ListCopy.end();
451 ++it)
452 {
453 ComObjPtr<HostUSBDevice> pHostDevice = *it;
454 AutoWriteLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
455 if (pHostDevice->i_getMachine() == aMachine)
456 {
457 /*
458 * Same procedure as in detachUSBDevice().
459 */
460 bool fRunFilters = false;
461 HRESULT hrc = pHostDevice->i_onDetachFromVM(aMachine, aDone, &fRunFilters, aAbnormal);
462 if ( SUCCEEDED(hrc)
463 && fRunFilters)
464 {
465 USBProxyBackend *pUsbProxyBackend = pHostDevice->i_getUsbProxyBackend();
466 Assert( aDone
467 && pHostDevice->i_getUnistate() == kHostUSBDeviceState_HeldByProxy
468 && pHostDevice->i_getMachine().isNull());
469 devLock.release();
470 alock.release();
471 HRESULT hrc2 = pUsbProxyBackend->runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine);
472 ComAssertComRC(hrc2);
473 alock.acquire();
474 }
475 }
476 }
477
478 return S_OK;
479}
480
481
482// Internals
483/////////////////////////////////////////////////////////////////////////////
484
485
486/**
487 * Sort a list of USB devices.
488 *
489 * @returns Pointer to the head of the sorted doubly linked list.
490 * @param aDevices Head pointer (can be both singly and doubly linked list).
491 */
492static PUSBDEVICE sortDevices(PUSBDEVICE pDevices)
493{
494 PUSBDEVICE pHead = NULL;
495 PUSBDEVICE pTail = NULL;
496 while (pDevices)
497 {
498 /* unlink head */
499 PUSBDEVICE pDev = pDevices;
500 pDevices = pDev->pNext;
501 if (pDevices)
502 pDevices->pPrev = NULL;
503
504 /* find location. */
505 PUSBDEVICE pCur = pTail;
506 while ( pCur
507 && HostUSBDevice::i_compare(pCur, pDev) > 0)
508 pCur = pCur->pPrev;
509
510 /* insert (after pCur) */
511 pDev->pPrev = pCur;
512 if (pCur)
513 {
514 pDev->pNext = pCur->pNext;
515 pCur->pNext = pDev;
516 if (pDev->pNext)
517 pDev->pNext->pPrev = pDev;
518 else
519 pTail = pDev;
520 }
521 else
522 {
523 pDev->pNext = pHead;
524 if (pHead)
525 pHead->pPrev = pDev;
526 else
527 pTail = pDev;
528 pHead = pDev;
529 }
530 }
531
532 LogFlowFuncLeave();
533 return pHead;
534}
535
536
537/**
538 * Process any relevant changes in the attached USB devices.
539 *
540 * This is called from any available USB proxy backends service thread when they discover
541 * a change.
542 */
543void USBProxyService::i_updateDeviceList(USBProxyBackend *pUsbProxyBackend, PUSBDEVICE pDevices)
544{
545 LogFlowThisFunc(("\n"));
546
547 pDevices = sortDevices(pDevices);
548
549 // get a list of all running machines while we're outside the lock
550 // (getOpenedMachines requests higher priority locks)
551 SessionMachinesList llOpenedMachines;
552 mHost->i_parent()->i_getOpenedMachines(llOpenedMachines);
553
554 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
555
556 /*
557 * Compare previous list with the new list of devices
558 * and merge in any changes while notifying Host.
559 */
560 HostUSBDeviceList::iterator it = this->mDevices.begin();
561 while ( it != mDevices.end()
562 || pDevices)
563 {
564 ComObjPtr<HostUSBDevice> pHostDevice;
565
566 if (it != mDevices.end())
567 pHostDevice = *it;
568
569 /*
570 * Assert that the object is still alive (we still reference it in
571 * the collection and we're the only one who calls uninit() on it.
572 */
573 AutoCaller devCaller(pHostDevice.isNull() ? NULL : pHostDevice);
574 AssertComRC(devCaller.rc());
575
576 /*
577 * Lock the device object since we will read/write its
578 * properties. All Host callbacks also imply the object is locked.
579 */
580 AutoWriteLock devLock(pHostDevice.isNull() ? NULL : pHostDevice
581 COMMA_LOCKVAL_SRC_POS);
582
583 /* Skip all devices not belonging to the same backend. */
584 if ( !pHostDevice.isNull()
585 && pHostDevice->i_getUsbProxyBackend() != pUsbProxyBackend)
586 {
587 ++it;
588 continue;
589 }
590
591 /*
592 * Compare.
593 */
594 int iDiff;
595 if (pHostDevice.isNull())
596 iDiff = 1;
597 else
598 {
599 if (!pDevices)
600 iDiff = -1;
601 else
602 iDiff = pHostDevice->i_compare(pDevices);
603 }
604 if (!iDiff)
605 {
606 /*
607 * The device still there, update the state and move on. The PUSBDEVICE
608 * structure is eaten by updateDeviceState / HostUSBDevice::updateState().
609 */
610 PUSBDEVICE pCur = pDevices;
611 pDevices = pDevices->pNext;
612 pCur->pPrev = pCur->pNext = NULL;
613
614 bool fRunFilters = false;
615 SessionMachine *pIgnoreMachine = NULL;
616 devLock.release();
617 alock.release();
618 if (pUsbProxyBackend->updateDeviceState(pHostDevice, pCur, &fRunFilters, &pIgnoreMachine))
619 pUsbProxyBackend->deviceChanged(pHostDevice,
620 (fRunFilters ? &llOpenedMachines : NULL),
621 pIgnoreMachine);
622 alock.acquire();
623 ++it;
624 }
625 else
626 {
627 if (iDiff > 0)
628 {
629 /*
630 * Head of pDevices was attached.
631 */
632 PUSBDEVICE pNew = pDevices;
633 pDevices = pDevices->pNext;
634 pNew->pPrev = pNew->pNext = NULL;
635
636 ComObjPtr<HostUSBDevice> NewObj;
637 NewObj.createObject();
638 NewObj->init(pNew, pUsbProxyBackend);
639 Log(("USBProxyService::processChanges: attached %p {%s} %s / %p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
640 (HostUSBDevice *)NewObj,
641 NewObj->i_getName().c_str(),
642 NewObj->i_getStateName(),
643 pNew,
644 pNew->idVendor,
645 pNew->idProduct,
646 pNew->pszProduct,
647 pNew->pszManufacturer));
648
649 mDevices.insert(it, NewObj);
650
651 devLock.release();
652 alock.release();
653 pUsbProxyBackend->deviceAdded(NewObj, llOpenedMachines, pNew);
654 alock.acquire();
655 }
656 else
657 {
658 /*
659 * Check if the device was actually detached or logically detached
660 * as the result of a re-enumeration.
661 */
662 if (!pHostDevice->i_wasActuallyDetached())
663 ++it;
664 else
665 {
666 it = mDevices.erase(it);
667 devLock.release();
668 alock.release();
669 pUsbProxyBackend->deviceRemoved(pHostDevice);
670 Log(("USBProxyService::processChanges: detached %p {%s}\n",
671 (HostUSBDevice *)pHostDevice,
672 pHostDevice->i_getName().c_str()));
673
674 /* from now on, the object is no more valid,
675 * uninitialize to avoid abuse */
676 devCaller.release();
677 pHostDevice->uninit();
678 alock.acquire();
679 }
680 }
681 }
682 } /* while */
683
684 LogFlowThisFunc(("returns void\n"));
685}
686
687
688/**
689 * Returns the global USB filter list stored in the Host object.
690 *
691 * @returns nothing.
692 * @param pGlobalFilters Where to store the global filter list on success.
693 */
694void USBProxyService::i_getUSBFilters(USBDeviceFilterList *pGlobalFilters)
695{
696 mHost->i_getUSBFilters(pGlobalFilters);
697}
698
699
700/**
701 * Loads the given settings and constructs the additional USB device sources.
702 *
703 * @returns COM status code.
704 * @param llUSBDeviceSources The list of additional device sources.
705 */
706HRESULT USBProxyService::i_loadSettings(const settings::USBDeviceSourcesList &llUSBDeviceSources)
707{
708 HRESULT hrc = S_OK;
709
710 for (settings::USBDeviceSourcesList::const_iterator it = llUSBDeviceSources.begin();
711 it != llUSBDeviceSources.end() && SUCCEEDED(hrc);
712 ++it)
713 {
714 std::vector<com::Utf8Str> vecPropNames, vecPropValues;
715 const settings::USBDeviceSource &src = *it;
716 hrc = createUSBDeviceSource(src.strBackend, src.strName, src.strAddress, vecPropNames, vecPropValues);
717 }
718
719 return hrc;
720}
721
722/**
723 * Saves the additional device sources in the given settings.
724 *
725 * @returns COM status code.
726 * @param llUSBDeviceSources The list of additional device sources.
727 */
728HRESULT USBProxyService::i_saveSettings(settings::USBDeviceSourcesList &llUSBDeviceSources)
729{
730 for (USBProxyBackendList::iterator it = mBackends.begin();
731 it != mBackends.end();
732 ++it)
733 {
734 USBProxyBackend *pUsbProxyBackend = *it;
735
736 /* Host backends are not saved as they are always created during startup. */
737 if (!pUsbProxyBackend->i_getBackend().equals("host"))
738 {
739 settings::USBDeviceSource src;
740
741 src.strBackend = pUsbProxyBackend->i_getBackend();
742 src.strName = pUsbProxyBackend->i_getId();
743 src.strAddress = pUsbProxyBackend->i_getAddress();
744
745 llUSBDeviceSources.push_back(src);
746 }
747 }
748
749 return S_OK;
750}
751
752/**
753 * Searches the list of devices (mDevices) for the given device.
754 *
755 *
756 * @returns Smart pointer to the device on success, NULL otherwise.
757 * @param aId The UUID of the device we're looking for.
758 */
759ComObjPtr<HostUSBDevice> USBProxyService::findDeviceById(IN_GUID aId)
760{
761 Guid Id(aId);
762 ComObjPtr<HostUSBDevice> Dev;
763 for (HostUSBDeviceList::iterator it = mDevices.begin();
764 it != mDevices.end();
765 ++it)
766 if ((*it)->i_getId() == Id)
767 {
768 Dev = (*it);
769 break;
770 }
771
772 return Dev;
773}
774
775/**
776 * Creates a new USB device source.
777 *
778 * @returns COM status code.
779 * @param aBackend The backend to use.
780 * @param aId The ID of the source.
781 * @param aAddress The backend specific address.
782 * @param aPropertyNames Vector of optional property keys the backend supports.
783 * @param aPropertyValues Vector of optional property values the backend supports.
784 */
785HRESULT USBProxyService::createUSBDeviceSource(const com::Utf8Str &aBackend, const com::Utf8Str &aId,
786 const com::Utf8Str &aAddress, const std::vector<com::Utf8Str> &aPropertyNames,
787 const std::vector<com::Utf8Str> &aPropertyValues)
788{
789 HRESULT hrc = S_OK;
790
791 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
792
793 /* Check whether the ID is used first. */
794 for (USBProxyBackendList::iterator it = mBackends.begin();
795 it != mBackends.end();
796 ++it)
797 {
798 USBProxyBackend *pUsbProxyBackend = *it;
799
800 if (aId.equals(pUsbProxyBackend->i_getId()))
801 return setError(VBOX_E_OBJECT_IN_USE,
802 tr("The USB device source \"%s\" exists already"), aId.c_str());
803 }
804
805 /* Create appropriate proxy backend. */
806 if (aBackend.equalsIgnoreCase("USBIP"))
807 {
808 ComObjPtr<USBProxyBackendUsbIp> UsbProxyBackend;
809
810 UsbProxyBackend.createObject();
811 int vrc = UsbProxyBackend->init(this, aId, aAddress);
812 if (RT_FAILURE(vrc))
813 hrc = setError(E_FAIL,
814 tr("Creating the USB device source \"%s\" using backend \"%s\" failed with %Rrc"),
815 aId.c_str(), aBackend.c_str(), vrc);
816 else
817 mBackends.push_back(static_cast<ComObjPtr<USBProxyBackend> >(UsbProxyBackend));
818 }
819 else
820 hrc = setError(VBOX_E_OBJECT_NOT_FOUND,
821 tr("The USB backend \"%s\" is not supported"), aBackend.c_str());
822
823 return hrc;
824}
825
826/*static*/
827HRESULT USBProxyService::setError(HRESULT aResultCode, const char *aText, ...)
828{
829 va_list va;
830 va_start(va, aText);
831 HRESULT rc = VirtualBoxBase::setErrorInternal(aResultCode,
832 COM_IIDOF(IHost),
833 "USBProxyService",
834 Utf8StrFmt(aText, va),
835 false /* aWarning*/,
836 true /* aLogIt*/);
837 va_end(va);
838 return rc;
839}
840
841/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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