1 | /* $Id: USBProxyService.cpp 31891 2010-08-24 07:58:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service (base) class.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Oracle Corporation confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include "USBProxyService.h"
|
---|
14 | #include "HostUSBDeviceImpl.h"
|
---|
15 | #include "HostImpl.h"
|
---|
16 | #include "MachineImpl.h"
|
---|
17 | #include "VirtualBoxImpl.h"
|
---|
18 |
|
---|
19 | #include "AutoCaller.h"
|
---|
20 | #include "Logging.h"
|
---|
21 |
|
---|
22 | #include <VBox/err.h>
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/semaphore.h>
|
---|
25 | #include <iprt/thread.h>
|
---|
26 | #include <iprt/mem.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Initialize data members.
|
---|
32 | */
|
---|
33 | USBProxyService::USBProxyService(Host *aHost)
|
---|
34 | : mHost(aHost), mThread(NIL_RTTHREAD), mTerminate(false), mLastError(VINF_SUCCESS), mDevices()
|
---|
35 | {
|
---|
36 | LogFlowThisFunc(("aHost=%p\n", aHost));
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Initialize the object.
|
---|
42 | *
|
---|
43 | * Child classes should override and call this method
|
---|
44 | *
|
---|
45 | * @returns S_OK on success, or COM error status on fatal error.
|
---|
46 | */
|
---|
47 | HRESULT USBProxyService::init(void)
|
---|
48 | {
|
---|
49 | return S_OK;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Empty destructor.
|
---|
55 | */
|
---|
56 | USBProxyService::~USBProxyService()
|
---|
57 | {
|
---|
58 | LogFlowThisFunc(("\n"));
|
---|
59 | Assert(mThread == NIL_RTTHREAD);
|
---|
60 | mDevices.clear();
|
---|
61 | mTerminate = true;
|
---|
62 | mHost = NULL;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Query if the service is active and working.
|
---|
68 | *
|
---|
69 | * @returns true if the service is up running.
|
---|
70 | * @returns false if the service isn't running.
|
---|
71 | */
|
---|
72 | bool USBProxyService::isActive(void)
|
---|
73 | {
|
---|
74 | return mThread != NIL_RTTHREAD;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Get last error.
|
---|
80 | * Can be used to check why the proxy !isActive() upon construction.
|
---|
81 | *
|
---|
82 | * @returns VBox status code.
|
---|
83 | */
|
---|
84 | int USBProxyService::getLastError(void)
|
---|
85 | {
|
---|
86 | return mLastError;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get last error message.
|
---|
92 | * Can be used to check why the proxy !isActive() upon construction as an
|
---|
93 | * extension to getLastError(). May return a NULL error.
|
---|
94 | *
|
---|
95 | * @param
|
---|
96 | * @returns VBox status code.
|
---|
97 | */
|
---|
98 | HRESULT USBProxyService::getLastErrorMessage(BSTR *aError)
|
---|
99 | {
|
---|
100 | AssertPtrReturn(aError, E_POINTER);
|
---|
101 | mLastErrorMessage.cloneTo(aError);
|
---|
102 | return S_OK;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * We're using the Host object lock.
|
---|
108 | *
|
---|
109 | * This is just a temporary measure until all the USB refactoring is
|
---|
110 | * done, probably... For now it help avoiding deadlocks we don't have
|
---|
111 | * time to fix.
|
---|
112 | *
|
---|
113 | * @returns Lock handle.
|
---|
114 | */
|
---|
115 | RWLockHandle *USBProxyService::lockHandle() const
|
---|
116 | {
|
---|
117 | return mHost->lockHandle();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Gets the collection of USB devices, slave of Host::USBDevices.
|
---|
123 | *
|
---|
124 | * This is an interface for the HostImpl::USBDevices property getter.
|
---|
125 | *
|
---|
126 | *
|
---|
127 | * @param aUSBDevices Where to store the pointer to the collection.
|
---|
128 | *
|
---|
129 | * @returns COM status code.
|
---|
130 | *
|
---|
131 | * @remarks The caller must own the write lock of the host object.
|
---|
132 | */
|
---|
133 | HRESULT USBProxyService::getDeviceCollection(ComSafeArrayOut(IHostUSBDevice *, aUSBDevices))
|
---|
134 | {
|
---|
135 | AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
136 | CheckComArgOutSafeArrayPointerValid(aUSBDevices);
|
---|
137 |
|
---|
138 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
139 |
|
---|
140 | SafeIfaceArray<IHostUSBDevice> Collection (mDevices);
|
---|
141 | Collection.detachTo(ComSafeArrayOutArg(aUSBDevices));
|
---|
142 |
|
---|
143 | return S_OK;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Request capture of a specific device.
|
---|
149 | *
|
---|
150 | * This is in an interface for SessionMachine::CaptureUSBDevice(), which is
|
---|
151 | * an internal worker used by Console::AttachUSBDevice() from the VM process.
|
---|
152 | *
|
---|
153 | * When the request is completed, SessionMachine::onUSBDeviceAttach() will
|
---|
154 | * be called for the given machine object.
|
---|
155 | *
|
---|
156 | *
|
---|
157 | * @param aMachine The machine to attach the device to.
|
---|
158 | * @param aId The UUID of the USB device to capture and attach.
|
---|
159 | *
|
---|
160 | * @returns COM status code and error info.
|
---|
161 | *
|
---|
162 | * @remarks This method may operate synchronously as well as asynchronously. In the
|
---|
163 | * former case it will temporarily abandon locks because of IPC.
|
---|
164 | */
|
---|
165 | HRESULT USBProxyService::captureDeviceForVM(SessionMachine *aMachine, IN_GUID aId)
|
---|
166 | {
|
---|
167 | ComAssertRet(aMachine, E_INVALIDARG);
|
---|
168 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Translate the device id into a device object.
|
---|
172 | */
|
---|
173 | ComObjPtr<HostUSBDevice> pHostDevice = findDeviceById(aId);
|
---|
174 | if (pHostDevice.isNull())
|
---|
175 | return setError(E_INVALIDARG,
|
---|
176 | tr("The USB device with UUID {%RTuuid} is not currently attached to the host"), Guid(aId).raw());
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * Try to capture the device
|
---|
180 | */
|
---|
181 | AutoWriteLock DevLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
|
---|
182 | return pHostDevice->requestCaptureForVM(aMachine, true /* aSetError */);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Notification from VM process about USB device detaching progress.
|
---|
188 | *
|
---|
189 | * This is in an interface for SessionMachine::DetachUSBDevice(), which is
|
---|
190 | * an internal worker used by Console::DetachUSBDevice() from the VM process.
|
---|
191 | *
|
---|
192 | * @param aMachine The machine which is sending the notification.
|
---|
193 | * @param aId The UUID of the USB device is concerns.
|
---|
194 | * @param aDone \a false for the pre-action notification (necessary
|
---|
195 | * for advancing the device state to avoid confusing
|
---|
196 | * the guest).
|
---|
197 | * \a true for the post-action notification. The device
|
---|
198 | * will be subjected to all filters except those of
|
---|
199 | * of \a Machine.
|
---|
200 | *
|
---|
201 | * @returns COM status code.
|
---|
202 | *
|
---|
203 | * @remarks When \a aDone is \a true this method may end up doing IPC to other
|
---|
204 | * VMs when running filters. In these cases it will temporarily
|
---|
205 | * abandon its locks.
|
---|
206 | */
|
---|
207 | HRESULT USBProxyService::detachDeviceFromVM(SessionMachine *aMachine, IN_GUID aId, bool aDone)
|
---|
208 | {
|
---|
209 | LogFlowThisFunc(("aMachine=%p{%s} aId={%RTuuid} aDone=%RTbool\n",
|
---|
210 | aMachine,
|
---|
211 | aMachine->getName().c_str(),
|
---|
212 | Guid(aId).raw(),
|
---|
213 | aDone));
|
---|
214 |
|
---|
215 | // get a list of all running machines while we're outside the lock
|
---|
216 | // (getOpenedMachines requests locks which are incompatible with the lock of the machines list)
|
---|
217 | SessionMachinesList llOpenedMachines;
|
---|
218 | mHost->parent()->getOpenedMachines(llOpenedMachines);
|
---|
219 |
|
---|
220 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
221 |
|
---|
222 | ComObjPtr<HostUSBDevice> pHostDevice = findDeviceById(aId);
|
---|
223 | ComAssertRet(!pHostDevice.isNull(), E_FAIL);
|
---|
224 | AutoWriteLock DevLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Work the state machine.
|
---|
228 | */
|
---|
229 | LogFlowThisFunc(("id={%RTuuid} state=%s aDone=%RTbool name={%s}\n",
|
---|
230 | pHostDevice->getId().raw(), pHostDevice->getStateName(), aDone, pHostDevice->getName().c_str()));
|
---|
231 | bool fRunFilters = false;
|
---|
232 | HRESULT hrc = pHostDevice->onDetachFromVM(aMachine, aDone, &fRunFilters);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Run filters if necessary.
|
---|
236 | */
|
---|
237 | if ( SUCCEEDED(hrc)
|
---|
238 | && fRunFilters)
|
---|
239 | {
|
---|
240 | Assert(aDone && pHostDevice->getUnistate() == kHostUSBDeviceState_HeldByProxy && pHostDevice->getMachine().isNull());
|
---|
241 | HRESULT hrc2 = runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine);
|
---|
242 | ComAssertComRC(hrc2);
|
---|
243 | }
|
---|
244 | return hrc;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Apply filters for the machine to all eligible USB devices.
|
---|
250 | *
|
---|
251 | * This is in an interface for SessionMachine::CaptureUSBDevice(), which
|
---|
252 | * is an internal worker used by Console::AutoCaptureUSBDevices() from the
|
---|
253 | * VM process at VM startup.
|
---|
254 | *
|
---|
255 | * Matching devices will be attached to the VM and may result IPC back
|
---|
256 | * to the VM process via SessionMachine::onUSBDeviceAttach() depending
|
---|
257 | * on whether the device needs to be captured or not. If capture is
|
---|
258 | * required, SessionMachine::onUSBDeviceAttach() will be called
|
---|
259 | * asynchronously by the USB proxy service thread.
|
---|
260 | *
|
---|
261 | * @param aMachine The machine to capture devices for.
|
---|
262 | *
|
---|
263 | * @returns COM status code, perhaps with error info.
|
---|
264 | *
|
---|
265 | * @remarks Write locks the host object and may temporarily abandon
|
---|
266 | * its locks to perform IPC.
|
---|
267 | */
|
---|
268 | HRESULT USBProxyService::autoCaptureDevicesForVM(SessionMachine *aMachine)
|
---|
269 | {
|
---|
270 | LogFlowThisFunc(("aMachine=%p{%s}\n",
|
---|
271 | aMachine,
|
---|
272 | aMachine->getName().c_str()));
|
---|
273 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
274 | AutoWriteLock mlock(aMachine COMMA_LOCKVAL_SRC_POS);
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * Make a copy of the list because we might have to exit and
|
---|
278 | * re-enter the lock protecting it. (This will not make copies
|
---|
279 | * of any HostUSBDevice objects, only reference them.)
|
---|
280 | */
|
---|
281 | HostUSBDeviceList ListCopy = mDevices;
|
---|
282 |
|
---|
283 | for (HostUSBDeviceList::iterator it = ListCopy.begin();
|
---|
284 | it != ListCopy.end();
|
---|
285 | ++it)
|
---|
286 | {
|
---|
287 | ComObjPtr<HostUSBDevice> device = *it;
|
---|
288 | AutoWriteLock devLock(device COMMA_LOCKVAL_SRC_POS);
|
---|
289 | if ( device->getUnistate() == kHostUSBDeviceState_HeldByProxy
|
---|
290 | || device->getUnistate() == kHostUSBDeviceState_Unused
|
---|
291 | || device->getUnistate() == kHostUSBDeviceState_Capturable)
|
---|
292 | runMachineFilters(aMachine, device);
|
---|
293 | }
|
---|
294 |
|
---|
295 | return S_OK;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Detach all USB devices currently attached to a VM.
|
---|
301 | *
|
---|
302 | * This is in an interface for SessionMachine::DetachAllUSBDevices(), which
|
---|
303 | * is an internal worker used by Console::powerDown() from the VM process
|
---|
304 | * at VM startup, and SessionMachine::uninit() at VM abend.
|
---|
305 | *
|
---|
306 | * This is, like #detachDeviceFromVM(), normally a two stage journey
|
---|
307 | * where \a aDone indicates where we are. In addition we may be called
|
---|
308 | * to clean up VMs that have abended, in which case there will be no
|
---|
309 | * preparatory call. Filters will be applied to the devices in the final
|
---|
310 | * call with the risk that we have to do some IPC when attaching them
|
---|
311 | * to other VMs.
|
---|
312 | *
|
---|
313 | * @param aMachine The machine to detach devices from.
|
---|
314 | *
|
---|
315 | * @returns COM status code, perhaps with error info.
|
---|
316 | *
|
---|
317 | * @remarks Write locks the host object and may temporarily abandon
|
---|
318 | * its locks to perform IPC.
|
---|
319 | */
|
---|
320 | HRESULT USBProxyService::detachAllDevicesFromVM(SessionMachine *aMachine, bool aDone, bool aAbnormal)
|
---|
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->parent()->getOpenedMachines(llOpenedMachines);
|
---|
326 |
|
---|
327 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
328 |
|
---|
329 | /*
|
---|
330 | * Make a copy of the device list (not the HostUSBDevice objects, just
|
---|
331 | * the list) since we may end up performing IPC and temporarily have
|
---|
332 | * to abandon locks when applying filters.
|
---|
333 | */
|
---|
334 | HostUSBDeviceList ListCopy = mDevices;
|
---|
335 |
|
---|
336 | for (HostUSBDeviceList::iterator It = ListCopy.begin();
|
---|
337 | It != ListCopy.end();
|
---|
338 | ++It)
|
---|
339 | {
|
---|
340 | ComObjPtr<HostUSBDevice> pHostDevice = *It;
|
---|
341 | AutoWriteLock devLock(pHostDevice COMMA_LOCKVAL_SRC_POS);
|
---|
342 | if (pHostDevice->getMachine() == aMachine)
|
---|
343 | {
|
---|
344 | /*
|
---|
345 | * Same procedure as in detachUSBDevice().
|
---|
346 | */
|
---|
347 | bool fRunFilters = false;
|
---|
348 | HRESULT hrc = pHostDevice->onDetachFromVM(aMachine, aDone, &fRunFilters, aAbnormal);
|
---|
349 | if ( SUCCEEDED(hrc)
|
---|
350 | && fRunFilters)
|
---|
351 | {
|
---|
352 | Assert(aDone && pHostDevice->getUnistate() == kHostUSBDeviceState_HeldByProxy && pHostDevice->getMachine().isNull());
|
---|
353 | HRESULT hrc2 = runAllFiltersOnDevice(pHostDevice, llOpenedMachines, aMachine);
|
---|
354 | ComAssertComRC(hrc2);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | return S_OK;
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Runs all the filters on the specified device.
|
---|
365 | *
|
---|
366 | * All filters mean global and active VM, with the exception of those
|
---|
367 | * belonging to \a aMachine. If a global ignore filter matched or if
|
---|
368 | * none of the filters matched, the device will be released back to
|
---|
369 | * the host.
|
---|
370 | *
|
---|
371 | * The device calling us here will be in the HeldByProxy, Unused, or
|
---|
372 | * Capturable state. The caller is aware that locks held might have
|
---|
373 | * to be abandond because of IPC and that the device might be in
|
---|
374 | * almost any state upon return.
|
---|
375 | *
|
---|
376 | *
|
---|
377 | * @returns COM status code (only parameter & state checks will fail).
|
---|
378 | * @param aDevice The USB device to apply filters to.
|
---|
379 | * @param aIgnoreMachine The machine to ignore filters from (we've just
|
---|
380 | * detached the device from this machine).
|
---|
381 | *
|
---|
382 | * @note The caller is expected to own both the device and Host write locks,
|
---|
383 | * and be prepared that these locks may be abandond temporarily.
|
---|
384 | */
|
---|
385 | HRESULT USBProxyService::runAllFiltersOnDevice(ComObjPtr<HostUSBDevice> &aDevice,
|
---|
386 | SessionMachinesList &llOpenedMachines,
|
---|
387 | SessionMachine *aIgnoreMachine)
|
---|
388 | {
|
---|
389 | LogFlowThisFunc(("{%s} ignorning=%p\n", aDevice->getName().c_str(), aIgnoreMachine));
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * Verify preconditions.
|
---|
393 | */
|
---|
394 | AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
|
---|
395 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), E_FAIL);
|
---|
396 | AssertMsgReturn(aDevice->isCapturableOrHeld(), ("{%s} %s\n", aDevice->getName().c_str(), aDevice->getStateName()), E_FAIL);
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Get the lists we'll iterate.
|
---|
400 | */
|
---|
401 | Host::USBDeviceFilterList globalFilters;
|
---|
402 |
|
---|
403 | mHost->getUSBFilters(&globalFilters);
|
---|
404 |
|
---|
405 | /*
|
---|
406 | * Run global filters filerts first.
|
---|
407 | */
|
---|
408 | bool fHoldIt = false;
|
---|
409 | for (Host::USBDeviceFilterList::const_iterator it = globalFilters.begin();
|
---|
410 | it != globalFilters.end();
|
---|
411 | ++it)
|
---|
412 | {
|
---|
413 | AutoWriteLock filterLock(*it COMMA_LOCKVAL_SRC_POS);
|
---|
414 | const HostUSBDeviceFilter::Data &data = (*it)->getData();
|
---|
415 | if (aDevice->isMatch(data))
|
---|
416 | {
|
---|
417 | USBDeviceFilterAction_T action = USBDeviceFilterAction_Null;
|
---|
418 | (*it)->COMGETTER(Action)(&action);
|
---|
419 | if (action == USBDeviceFilterAction_Ignore)
|
---|
420 | {
|
---|
421 | /*
|
---|
422 | * Release the device to the host and we're done.
|
---|
423 | */
|
---|
424 | aDevice->requestReleaseToHost();
|
---|
425 | return S_OK;
|
---|
426 | }
|
---|
427 | if (action == USBDeviceFilterAction_Hold)
|
---|
428 | {
|
---|
429 | /*
|
---|
430 | * A device held by the proxy needs to be subjected
|
---|
431 | * to the machine filters.
|
---|
432 | */
|
---|
433 | fHoldIt = true;
|
---|
434 | break;
|
---|
435 | }
|
---|
436 | AssertMsgFailed(("action=%d\n", action));
|
---|
437 | }
|
---|
438 | }
|
---|
439 | globalFilters.clear();
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Run the per-machine filters.
|
---|
443 | */
|
---|
444 | for (SessionMachinesList::const_iterator it = llOpenedMachines.begin();
|
---|
445 | it != llOpenedMachines.end();
|
---|
446 | ++it)
|
---|
447 | {
|
---|
448 | ComObjPtr<SessionMachine> pMachine = *it;
|
---|
449 |
|
---|
450 | /* Skip the machine the device was just detached from. */
|
---|
451 | if ( aIgnoreMachine
|
---|
452 | && pMachine == aIgnoreMachine)
|
---|
453 | continue;
|
---|
454 |
|
---|
455 | /* runMachineFilters takes care of checking the machine state. */
|
---|
456 | if (runMachineFilters(pMachine, aDevice))
|
---|
457 | {
|
---|
458 | LogFlowThisFunc(("{%s} attached to %p\n", aDevice->getName().c_str(), (void *)pMachine));
|
---|
459 | return S_OK;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * No matching machine, so request hold or release depending
|
---|
465 | * on global filter match.
|
---|
466 | */
|
---|
467 | if (fHoldIt)
|
---|
468 | aDevice->requestHold();
|
---|
469 | else
|
---|
470 | aDevice->requestReleaseToHost();
|
---|
471 | return S_OK;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Runs the USB filters of the machine on the device.
|
---|
477 | *
|
---|
478 | * If a match is found we will request capture for VM. This may cause
|
---|
479 | * us to temporary abandon locks while doing IPC.
|
---|
480 | *
|
---|
481 | * @param aMachine Machine whose filters are to be run.
|
---|
482 | * @param aDevice The USB device in question.
|
---|
483 | * @returns @c true if the device has been or is being attached to the VM, @c false otherwise.
|
---|
484 | *
|
---|
485 | * @note Caller must own the USB and device locks for writing.
|
---|
486 | * @note Locks aMachine for reading.
|
---|
487 | */
|
---|
488 | bool USBProxyService::runMachineFilters(SessionMachine *aMachine, ComObjPtr<HostUSBDevice> &aDevice)
|
---|
489 | {
|
---|
490 | LogFlowThisFunc(("{%s} aMachine=%p \n", aDevice->getName().c_str(), aMachine));
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * Validate preconditions.
|
---|
494 | */
|
---|
495 | AssertReturn(aMachine, false);
|
---|
496 | AssertReturn(isWriteLockOnCurrentThread(), false);
|
---|
497 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), false);
|
---|
498 | /* Let HostUSBDevice::requestCaptureToVM() validate the state. */
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Do the job.
|
---|
502 | */
|
---|
503 | ULONG ulMaskedIfs;
|
---|
504 | if (aMachine->hasMatchingUSBFilter(aDevice, &ulMaskedIfs))
|
---|
505 | {
|
---|
506 | /* try to capture the device */
|
---|
507 | HRESULT hrc = aDevice->requestCaptureForVM(aMachine, false /* aSetError */, ulMaskedIfs);
|
---|
508 | return SUCCEEDED(hrc)
|
---|
509 | || hrc == E_UNEXPECTED /* bad device state, give up */;
|
---|
510 | }
|
---|
511 |
|
---|
512 | return false;
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * A filter was inserted / loaded.
|
---|
518 | *
|
---|
519 | * @param aFilter Pointer to the inserted filter.
|
---|
520 | * @return ID of the inserted filter
|
---|
521 | */
|
---|
522 | void *USBProxyService::insertFilter(PCUSBFILTER aFilter)
|
---|
523 | {
|
---|
524 | // return non-NULL to fake success.
|
---|
525 | NOREF(aFilter);
|
---|
526 | return (void *)1;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * A filter was removed.
|
---|
532 | *
|
---|
533 | * @param aId ID of the filter to remove
|
---|
534 | */
|
---|
535 | void USBProxyService::removeFilter(void *aId)
|
---|
536 | {
|
---|
537 | NOREF(aId);
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * A VM is trying to capture a device, do necessary preperations.
|
---|
543 | *
|
---|
544 | * @returns VBox status code.
|
---|
545 | * @param aDevice The device in question.
|
---|
546 | */
|
---|
547 | int USBProxyService::captureDevice(HostUSBDevice *aDevice)
|
---|
548 | {
|
---|
549 | NOREF(aDevice);
|
---|
550 | return VERR_NOT_IMPLEMENTED;
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Notification that an async captureDevice() operation completed.
|
---|
556 | *
|
---|
557 | * This is used by the proxy to release temporary filters.
|
---|
558 | *
|
---|
559 | * @returns VBox status code.
|
---|
560 | * @param aDevice The device in question.
|
---|
561 | * @param aSuccess Whether it succeeded or failed.
|
---|
562 | */
|
---|
563 | void USBProxyService::captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
|
---|
564 | {
|
---|
565 | NOREF(aDevice);
|
---|
566 | NOREF(aSuccess);
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * The device is going to be detached from a VM.
|
---|
572 | *
|
---|
573 | * @param aDevice The device in question.
|
---|
574 | */
|
---|
575 | void USBProxyService::detachingDevice(HostUSBDevice *aDevice)
|
---|
576 | {
|
---|
577 | NOREF(aDevice);
|
---|
578 | }
|
---|
579 |
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * A VM is releasing a device back to the host.
|
---|
583 | *
|
---|
584 | * @returns VBox status code.
|
---|
585 | * @param aDevice The device in question.
|
---|
586 | */
|
---|
587 | int USBProxyService::releaseDevice(HostUSBDevice *aDevice)
|
---|
588 | {
|
---|
589 | NOREF(aDevice);
|
---|
590 | return VERR_NOT_IMPLEMENTED;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Notification that an async releaseDevice() operation completed.
|
---|
596 | *
|
---|
597 | * This is used by the proxy to release temporary filters.
|
---|
598 | *
|
---|
599 | * @returns VBox status code.
|
---|
600 | * @param aDevice The device in question.
|
---|
601 | * @param aSuccess Whether it succeeded or failed.
|
---|
602 | */
|
---|
603 | void USBProxyService::releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
|
---|
604 | {
|
---|
605 | NOREF(aDevice);
|
---|
606 | NOREF(aSuccess);
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | // Internals
|
---|
611 | /////////////////////////////////////////////////////////////////////////////
|
---|
612 |
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Starts the service.
|
---|
616 | *
|
---|
617 | * @returns VBox status.
|
---|
618 | */
|
---|
619 | int USBProxyService::start(void)
|
---|
620 | {
|
---|
621 | int rc = VINF_SUCCESS;
|
---|
622 | if (mThread == NIL_RTTHREAD)
|
---|
623 | {
|
---|
624 | /*
|
---|
625 | * Force update before starting the poller thread.
|
---|
626 | */
|
---|
627 | rc = wait(0);
|
---|
628 | if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED || RT_SUCCESS(rc))
|
---|
629 | {
|
---|
630 | processChanges();
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Create the poller thread which will look for changes.
|
---|
634 | */
|
---|
635 | mTerminate = false;
|
---|
636 | rc = RTThreadCreate(&mThread, USBProxyService::serviceThread, this,
|
---|
637 | 0, RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "USBPROXY");
|
---|
638 | AssertRC(rc);
|
---|
639 | if (RT_SUCCESS(rc))
|
---|
640 | LogFlowThisFunc(("started mThread=%RTthrd\n", mThread));
|
---|
641 | else
|
---|
642 | mThread = NIL_RTTHREAD;
|
---|
643 | }
|
---|
644 | mLastError = rc;
|
---|
645 | }
|
---|
646 | else
|
---|
647 | LogFlowThisFunc(("already running, mThread=%RTthrd\n", mThread));
|
---|
648 | return rc;
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Stops the service.
|
---|
654 | *
|
---|
655 | * @returns VBox status.
|
---|
656 | */
|
---|
657 | int USBProxyService::stop(void)
|
---|
658 | {
|
---|
659 | int rc = VINF_SUCCESS;
|
---|
660 | if (mThread != NIL_RTTHREAD)
|
---|
661 | {
|
---|
662 | /*
|
---|
663 | * Mark the thread for termination and kick it.
|
---|
664 | */
|
---|
665 | ASMAtomicXchgSize(&mTerminate, true);
|
---|
666 | rc = interruptWait();
|
---|
667 | AssertRC(rc);
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * Wait for the thread to finish and then update the state.
|
---|
671 | */
|
---|
672 | rc = RTThreadWait(mThread, 60000, NULL);
|
---|
673 | if (rc == VERR_INVALID_HANDLE)
|
---|
674 | rc = VINF_SUCCESS;
|
---|
675 | if (RT_SUCCESS(rc))
|
---|
676 | {
|
---|
677 | LogFlowThisFunc(("stopped mThread=%RTthrd\n", mThread));
|
---|
678 | mThread = NIL_RTTHREAD;
|
---|
679 | mTerminate = false;
|
---|
680 | }
|
---|
681 | else
|
---|
682 | {
|
---|
683 | AssertRC(rc);
|
---|
684 | mLastError = rc;
|
---|
685 | }
|
---|
686 | }
|
---|
687 | else
|
---|
688 | LogFlowThisFunc(("not active\n"));
|
---|
689 |
|
---|
690 | return rc;
|
---|
691 | }
|
---|
692 |
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * The service thread created by start().
|
---|
696 | *
|
---|
697 | * @param Thread The thread handle.
|
---|
698 | * @param pvUser Pointer to the USBProxyService instance.
|
---|
699 | */
|
---|
700 | /*static*/ DECLCALLBACK(int) USBProxyService::serviceThread(RTTHREAD /* Thread */, void *pvUser)
|
---|
701 | {
|
---|
702 | USBProxyService *pThis = (USBProxyService *)pvUser;
|
---|
703 | LogFlowFunc(("pThis=%p\n", pThis));
|
---|
704 | pThis->serviceThreadInit();
|
---|
705 | int rc = VINF_SUCCESS;
|
---|
706 |
|
---|
707 | /*
|
---|
708 | * Processing loop.
|
---|
709 | */
|
---|
710 | for (;;)
|
---|
711 | {
|
---|
712 | rc = pThis->wait(RT_INDEFINITE_WAIT);
|
---|
713 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
|
---|
714 | break;
|
---|
715 | if (pThis->mTerminate)
|
---|
716 | break;
|
---|
717 | pThis->processChanges();
|
---|
718 | }
|
---|
719 |
|
---|
720 | pThis->serviceThreadTerm();
|
---|
721 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
722 | return rc;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * First call made on the service thread, use it to do
|
---|
728 | * thread initialization.
|
---|
729 | *
|
---|
730 | * The default implementation in USBProxyService just a dummy stub.
|
---|
731 | */
|
---|
732 | void USBProxyService::serviceThreadInit(void)
|
---|
733 | {
|
---|
734 | }
|
---|
735 |
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Last call made on the service thread, use it to do
|
---|
739 | * thread termination.
|
---|
740 | */
|
---|
741 | void USBProxyService::serviceThreadTerm(void)
|
---|
742 | {
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Wait for a change in the USB devices attached to the host.
|
---|
748 | *
|
---|
749 | * The default implementation in USBProxyService just a dummy stub.
|
---|
750 | *
|
---|
751 | * @returns VBox status code. VERR_INTERRUPTED and VERR_TIMEOUT are considered
|
---|
752 | * harmless, while all other error status are fatal.
|
---|
753 | * @param aMillies Number of milliseconds to wait.
|
---|
754 | */
|
---|
755 | int USBProxyService::wait(RTMSINTERVAL aMillies)
|
---|
756 | {
|
---|
757 | return RTThreadSleep(RT_MIN(aMillies, 250));
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Interrupt any wait() call in progress.
|
---|
763 | *
|
---|
764 | * The default implementation in USBProxyService just a dummy stub.
|
---|
765 | *
|
---|
766 | * @returns VBox status.
|
---|
767 | */
|
---|
768 | int USBProxyService::interruptWait(void)
|
---|
769 | {
|
---|
770 | return VERR_NOT_IMPLEMENTED;
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Sort a list of USB devices.
|
---|
776 | *
|
---|
777 | * @returns Pointer to the head of the sorted doubly linked list.
|
---|
778 | * @param aDevices Head pointer (can be both singly and doubly linked list).
|
---|
779 | */
|
---|
780 | static PUSBDEVICE sortDevices(PUSBDEVICE pDevices)
|
---|
781 | {
|
---|
782 | PUSBDEVICE pHead = NULL;
|
---|
783 | PUSBDEVICE pTail = NULL;
|
---|
784 | while (pDevices)
|
---|
785 | {
|
---|
786 | /* unlink head */
|
---|
787 | PUSBDEVICE pDev = pDevices;
|
---|
788 | pDevices = pDev->pNext;
|
---|
789 | if (pDevices)
|
---|
790 | pDevices->pPrev = NULL;
|
---|
791 |
|
---|
792 | /* find location. */
|
---|
793 | PUSBDEVICE pCur = pTail;
|
---|
794 | while ( pCur
|
---|
795 | && HostUSBDevice::compare(pCur, pDev) > 0)
|
---|
796 | pCur = pCur->pPrev;
|
---|
797 |
|
---|
798 | /* insert (after pCur) */
|
---|
799 | pDev->pPrev = pCur;
|
---|
800 | if (pCur)
|
---|
801 | {
|
---|
802 | pDev->pNext = pCur->pNext;
|
---|
803 | pCur->pNext = pDev;
|
---|
804 | if (pDev->pNext)
|
---|
805 | pDev->pNext->pPrev = pDev;
|
---|
806 | else
|
---|
807 | pTail = pDev;
|
---|
808 | }
|
---|
809 | else
|
---|
810 | {
|
---|
811 | pDev->pNext = pHead;
|
---|
812 | if (pHead)
|
---|
813 | pHead->pPrev = pDev;
|
---|
814 | else
|
---|
815 | pTail = pDev;
|
---|
816 | pHead = pDev;
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | return pHead;
|
---|
821 | }
|
---|
822 |
|
---|
823 |
|
---|
824 | /**
|
---|
825 | * Process any relevant changes in the attached USB devices.
|
---|
826 | *
|
---|
827 | * Except for the first call, this is always running on the service thread.
|
---|
828 | */
|
---|
829 | void USBProxyService::processChanges(void)
|
---|
830 | {
|
---|
831 | LogFlowThisFunc(("\n"));
|
---|
832 |
|
---|
833 | /*
|
---|
834 | * Get the sorted list of USB devices.
|
---|
835 | */
|
---|
836 | PUSBDEVICE pDevices = getDevices();
|
---|
837 | pDevices = sortDevices(pDevices);
|
---|
838 |
|
---|
839 | // get a list of all running machines while we're outside the lock
|
---|
840 | // (getOpenedMachines requests locks which are incompatible with the lock of the machines list)
|
---|
841 | SessionMachinesList llOpenedMachines;
|
---|
842 | mHost->parent()->getOpenedMachines(llOpenedMachines);
|
---|
843 |
|
---|
844 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Compare previous list with the previous list of devices
|
---|
848 | * and merge in any changes while notifying Host.
|
---|
849 | */
|
---|
850 | HostUSBDeviceList::iterator It = this->mDevices.begin();
|
---|
851 | while ( It != mDevices.end()
|
---|
852 | || pDevices)
|
---|
853 | {
|
---|
854 | ComObjPtr<HostUSBDevice> pHostDevice;
|
---|
855 |
|
---|
856 | if (It != mDevices.end())
|
---|
857 | pHostDevice = *It;
|
---|
858 |
|
---|
859 | /*
|
---|
860 | * Assert that the object is still alive (we still reference it in
|
---|
861 | * the collection and we're the only one who calls uninit() on it.
|
---|
862 | */
|
---|
863 | AutoCaller devCaller(pHostDevice.isNull() ? NULL : pHostDevice);
|
---|
864 | AssertComRC(devCaller.rc());
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * Lock the device object since we will read/write its
|
---|
868 | * properties. All Host callbacks also imply the object is locked.
|
---|
869 | */
|
---|
870 | AutoWriteLock devLock(pHostDevice.isNull() ? NULL : pHostDevice
|
---|
871 | COMMA_LOCKVAL_SRC_POS);
|
---|
872 |
|
---|
873 | /*
|
---|
874 | * Compare.
|
---|
875 | */
|
---|
876 | int iDiff;
|
---|
877 | if (pHostDevice.isNull())
|
---|
878 | iDiff = 1;
|
---|
879 | else
|
---|
880 | {
|
---|
881 | if (!pDevices)
|
---|
882 | iDiff = -1;
|
---|
883 | else
|
---|
884 | iDiff = pHostDevice->compare(pDevices);
|
---|
885 | }
|
---|
886 | if (!iDiff)
|
---|
887 | {
|
---|
888 | /*
|
---|
889 | * The device still there, update the state and move on. The PUSBDEVICE
|
---|
890 | * structure is eaten by updateDeviceState / HostUSBDevice::updateState().
|
---|
891 | */
|
---|
892 | PUSBDEVICE pCur = pDevices;
|
---|
893 | pDevices = pDevices->pNext;
|
---|
894 | pCur->pPrev = pCur->pNext = NULL;
|
---|
895 |
|
---|
896 | bool fRunFilters = false;
|
---|
897 | SessionMachine *pIgnoreMachine = NULL;
|
---|
898 | if (updateDeviceState(pHostDevice, pCur, &fRunFilters, &pIgnoreMachine))
|
---|
899 | deviceChanged(pHostDevice,
|
---|
900 | (fRunFilters ? &llOpenedMachines : NULL),
|
---|
901 | pIgnoreMachine);
|
---|
902 | It++;
|
---|
903 | }
|
---|
904 | else
|
---|
905 | {
|
---|
906 | if (iDiff > 0)
|
---|
907 | {
|
---|
908 | /*
|
---|
909 | * Head of pDevices was attached.
|
---|
910 | */
|
---|
911 | PUSBDEVICE pNew = pDevices;
|
---|
912 | pDevices = pDevices->pNext;
|
---|
913 | pNew->pPrev = pNew->pNext = NULL;
|
---|
914 |
|
---|
915 | ComObjPtr<HostUSBDevice> NewObj;
|
---|
916 | NewObj.createObject();
|
---|
917 | NewObj->init(pNew, this);
|
---|
918 | Log(("USBProxyService::processChanges: attached %p {%s} %s / %p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
|
---|
919 | (HostUSBDevice *)NewObj,
|
---|
920 | NewObj->getName().c_str(),
|
---|
921 | NewObj->getStateName(),
|
---|
922 | pNew,
|
---|
923 | pNew->idVendor,
|
---|
924 | pNew->idProduct,
|
---|
925 | pNew->pszProduct,
|
---|
926 | pNew->pszManufacturer));
|
---|
927 |
|
---|
928 | mDevices.insert(It, NewObj);
|
---|
929 |
|
---|
930 | /* Not really necessary to lock here, but make Assert checks happy. */
|
---|
931 | AutoWriteLock newDevLock(NewObj COMMA_LOCKVAL_SRC_POS);
|
---|
932 | deviceAdded(NewObj, llOpenedMachines, pNew);
|
---|
933 | }
|
---|
934 | else
|
---|
935 | {
|
---|
936 | /*
|
---|
937 | * Check if the device was actually detached or logically detached
|
---|
938 | * as the result of a re-enumeration.
|
---|
939 | */
|
---|
940 | if (!pHostDevice->wasActuallyDetached())
|
---|
941 | It++;
|
---|
942 | else
|
---|
943 | {
|
---|
944 | It = mDevices.erase(It);
|
---|
945 | deviceRemoved(pHostDevice);
|
---|
946 | Log(("USBProxyService::processChanges: detached %p {%s}\n",
|
---|
947 | (HostUSBDevice *)pHostDevice,
|
---|
948 | pHostDevice->getName().c_str()));
|
---|
949 |
|
---|
950 | /* from now on, the object is no more valid,
|
---|
951 | * uninitialize to avoid abuse */
|
---|
952 | devCaller.release();
|
---|
953 | pHostDevice->uninit();
|
---|
954 | }
|
---|
955 | }
|
---|
956 | }
|
---|
957 | } /* while */
|
---|
958 |
|
---|
959 | LogFlowThisFunc(("returns void\n"));
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * Get a list of USB device currently attached to the host.
|
---|
965 | *
|
---|
966 | * The default implementation in USBProxyService just a dummy stub.
|
---|
967 | *
|
---|
968 | * @returns Pointer to a list of USB devices.
|
---|
969 | * The list nodes are freed individually by calling freeDevice().
|
---|
970 | */
|
---|
971 | PUSBDEVICE USBProxyService::getDevices(void)
|
---|
972 | {
|
---|
973 | return NULL;
|
---|
974 | }
|
---|
975 |
|
---|
976 |
|
---|
977 | /**
|
---|
978 | * Performs the required actions when a device has been added.
|
---|
979 | *
|
---|
980 | * This means things like running filters and subsequent capturing and
|
---|
981 | * VM attaching. This may result in IPC and temporary lock abandonment.
|
---|
982 | *
|
---|
983 | * @param aDevice The device in question.
|
---|
984 | * @param aUSBDevice The USB device structure.
|
---|
985 | */
|
---|
986 | void USBProxyService::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice,
|
---|
987 | SessionMachinesList &llOpenedMachines,
|
---|
988 | PUSBDEVICE aUSBDevice)
|
---|
989 | {
|
---|
990 | /*
|
---|
991 | * Validate preconditions.
|
---|
992 | */
|
---|
993 | AssertReturnVoid(isWriteLockOnCurrentThread());
|
---|
994 | AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
|
---|
995 | LogFlowThisFunc(("aDevice=%p name={%s} state=%s id={%RTuuid}\n",
|
---|
996 | (HostUSBDevice *)aDevice,
|
---|
997 | aDevice->getName().c_str(),
|
---|
998 | aDevice->getStateName(),
|
---|
999 | aDevice->getId().raw()));
|
---|
1000 |
|
---|
1001 | /*
|
---|
1002 | * Run filters on the device.
|
---|
1003 | */
|
---|
1004 | if (aDevice->isCapturableOrHeld())
|
---|
1005 | {
|
---|
1006 | HRESULT rc = runAllFiltersOnDevice(aDevice, llOpenedMachines, NULL /* aIgnoreMachine */);
|
---|
1007 | AssertComRC(rc);
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | NOREF(aUSBDevice);
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 |
|
---|
1014 | /**
|
---|
1015 | * Remove device notification hook for the OS specific code.
|
---|
1016 | *
|
---|
1017 | * This is means things like
|
---|
1018 | *
|
---|
1019 | * @param aDevice The device in question.
|
---|
1020 | */
|
---|
1021 | void USBProxyService::deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice)
|
---|
1022 | {
|
---|
1023 | /*
|
---|
1024 | * Validate preconditions.
|
---|
1025 | */
|
---|
1026 | AssertReturnVoid(isWriteLockOnCurrentThread());
|
---|
1027 | AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
|
---|
1028 | LogFlowThisFunc(("aDevice=%p name={%s} state=%s id={%RTuuid}\n",
|
---|
1029 | (HostUSBDevice *)aDevice,
|
---|
1030 | aDevice->getName().c_str(),
|
---|
1031 | aDevice->getStateName(),
|
---|
1032 | aDevice->getId().raw()));
|
---|
1033 |
|
---|
1034 | /*
|
---|
1035 | * Detach the device from any machine currently using it,
|
---|
1036 | * reset all data and uninitialize the device object.
|
---|
1037 | */
|
---|
1038 | aDevice->onPhysicalDetached();
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 |
|
---|
1042 | /**
|
---|
1043 | * Implement fake capture, ++.
|
---|
1044 | *
|
---|
1045 | * @returns true if there is a state change.
|
---|
1046 | * @param pDevice The device in question.
|
---|
1047 | * @param pUSBDevice The USB device structure for the last enumeration.
|
---|
1048 | * @param aRunFilters Whether or not to run filters.
|
---|
1049 | */
|
---|
1050 | bool USBProxyService::updateDeviceStateFake(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
|
---|
1051 | {
|
---|
1052 | *aRunFilters = false;
|
---|
1053 | *aIgnoreMachine = NULL;
|
---|
1054 | AssertReturn(aDevice, false);
|
---|
1055 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), false);
|
---|
1056 |
|
---|
1057 | /*
|
---|
1058 | * Just hand it to the device, it knows best what needs to be done.
|
---|
1059 | */
|
---|
1060 | return aDevice->updateStateFake(aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 |
|
---|
1064 | /**
|
---|
1065 | * Updates the device state.
|
---|
1066 | *
|
---|
1067 | * This is responsible for calling HostUSBDevice::updateState().
|
---|
1068 | *
|
---|
1069 | * @returns true if there is a state change.
|
---|
1070 | * @param aDevice The device in question.
|
---|
1071 | * @param aUSBDevice The USB device structure for the last enumeration.
|
---|
1072 | * @param aRunFilters Whether or not to run filters.
|
---|
1073 | * @param aIgnoreMachine Machine to ignore when running filters.
|
---|
1074 | */
|
---|
1075 | bool USBProxyService::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
|
---|
1076 | {
|
---|
1077 | AssertReturn(aDevice, false);
|
---|
1078 | AssertReturn(aDevice->isWriteLockOnCurrentThread(), false);
|
---|
1079 |
|
---|
1080 | return aDevice->updateState(aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 |
|
---|
1084 | /**
|
---|
1085 | * Handle a device which state changed in some siginificant way.
|
---|
1086 | *
|
---|
1087 | * This means things like running filters and subsequent capturing and
|
---|
1088 | * VM attaching. This may result in IPC and temporary lock abandonment.
|
---|
1089 | *
|
---|
1090 | * @param aDevice The device.
|
---|
1091 | * @param pllOpenedMachines list of running session machines (VirtualBox::getOpenedMachines()); if NULL, we don't run filters
|
---|
1092 | * @param aIgnoreMachine Machine to ignore when running filters.
|
---|
1093 | */
|
---|
1094 | void USBProxyService::deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList *pllOpenedMachines, SessionMachine *aIgnoreMachine)
|
---|
1095 | {
|
---|
1096 | /*
|
---|
1097 | * Validate preconditions.
|
---|
1098 | */
|
---|
1099 | AssertReturnVoid(isWriteLockOnCurrentThread());
|
---|
1100 | AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
|
---|
1101 | LogFlowThisFunc(("aDevice=%p name={%s} state=%s id={%RTuuid} aRunFilters=%RTbool aIgnoreMachine=%p\n",
|
---|
1102 | (HostUSBDevice *)aDevice,
|
---|
1103 | aDevice->getName().c_str(),
|
---|
1104 | aDevice->getStateName(),
|
---|
1105 | aDevice->getId().raw(),
|
---|
1106 | (pllOpenedMachines != NULL), // used to be "bool aRunFilters"
|
---|
1107 | aIgnoreMachine));
|
---|
1108 |
|
---|
1109 | /*
|
---|
1110 | * Run filters if requested to do so.
|
---|
1111 | */
|
---|
1112 | if (pllOpenedMachines)
|
---|
1113 | {
|
---|
1114 | HRESULT rc = runAllFiltersOnDevice(aDevice, *pllOpenedMachines, aIgnoreMachine);
|
---|
1115 | AssertComRC(rc);
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 |
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * Free all the members of a USB device returned by getDevice().
|
---|
1123 | *
|
---|
1124 | * @param pDevice Pointer to the device.
|
---|
1125 | */
|
---|
1126 | /*static*/ void
|
---|
1127 | USBProxyService::freeDeviceMembers(PUSBDEVICE pDevice)
|
---|
1128 | {
|
---|
1129 | RTStrFree((char *)pDevice->pszManufacturer);
|
---|
1130 | pDevice->pszManufacturer = NULL;
|
---|
1131 | RTStrFree((char *)pDevice->pszProduct);
|
---|
1132 | pDevice->pszProduct = NULL;
|
---|
1133 | RTStrFree((char *)pDevice->pszSerialNumber);
|
---|
1134 | pDevice->pszSerialNumber = NULL;
|
---|
1135 |
|
---|
1136 | RTStrFree((char *)pDevice->pszAddress);
|
---|
1137 | pDevice->pszAddress = NULL;
|
---|
1138 | #ifdef RT_OS_WINDOWS
|
---|
1139 | RTStrFree(pDevice->pszAltAddress);
|
---|
1140 | pDevice->pszAltAddress = NULL;
|
---|
1141 | RTStrFree(pDevice->pszHubName);
|
---|
1142 | pDevice->pszHubName = NULL;
|
---|
1143 | #elif defined(RT_OS_SOLARIS)
|
---|
1144 | RTStrFree(pDevice->pszDevicePath);
|
---|
1145 | pDevice->pszDevicePath = NULL;
|
---|
1146 | #endif
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 |
|
---|
1150 | /**
|
---|
1151 | * Free one USB device returned by getDevice().
|
---|
1152 | *
|
---|
1153 | * @param pDevice Pointer to the device.
|
---|
1154 | */
|
---|
1155 | /*static*/ void
|
---|
1156 | USBProxyService::freeDevice(PUSBDEVICE pDevice)
|
---|
1157 | {
|
---|
1158 | freeDeviceMembers(pDevice);
|
---|
1159 | RTMemFree(pDevice);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Initializes a filter with the data from the specified device.
|
---|
1165 | *
|
---|
1166 | * @param aFilter The filter to fill.
|
---|
1167 | * @param aDevice The device to fill it with.
|
---|
1168 | */
|
---|
1169 | /*static*/ void
|
---|
1170 | USBProxyService::initFilterFromDevice(PUSBFILTER aFilter, HostUSBDevice *aDevice)
|
---|
1171 | {
|
---|
1172 | PCUSBDEVICE pDev = aDevice->mUsb;
|
---|
1173 | int vrc;
|
---|
1174 |
|
---|
1175 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_VENDOR_ID, pDev->idVendor, true); AssertRC(vrc);
|
---|
1176 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_PRODUCT_ID, pDev->idProduct, true); AssertRC(vrc);
|
---|
1177 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_DEVICE_REV, pDev->bcdDevice, true); AssertRC(vrc);
|
---|
1178 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_DEVICE_CLASS, pDev->bDeviceClass, true); AssertRC(vrc);
|
---|
1179 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_DEVICE_SUB_CLASS, pDev->bDeviceSubClass, true); AssertRC(vrc);
|
---|
1180 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_DEVICE_PROTOCOL, pDev->bDeviceProtocol, true); AssertRC(vrc);
|
---|
1181 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_PORT, pDev->bPort, true); AssertRC(vrc);
|
---|
1182 | vrc = USBFilterSetNumExact(aFilter, USBFILTERIDX_BUS, pDev->bBus, true); AssertRC(vrc);
|
---|
1183 | if (pDev->pszSerialNumber)
|
---|
1184 | {
|
---|
1185 | vrc = USBFilterSetStringExact(aFilter, USBFILTERIDX_SERIAL_NUMBER_STR, pDev->pszSerialNumber, true);
|
---|
1186 | AssertRC(vrc);
|
---|
1187 | }
|
---|
1188 | if (pDev->pszProduct)
|
---|
1189 | {
|
---|
1190 | vrc = USBFilterSetStringExact(aFilter, USBFILTERIDX_PRODUCT_STR, pDev->pszProduct, true);
|
---|
1191 | AssertRC(vrc);
|
---|
1192 | }
|
---|
1193 | if (pDev->pszManufacturer)
|
---|
1194 | {
|
---|
1195 | vrc = USBFilterSetStringExact(aFilter, USBFILTERIDX_MANUFACTURER_STR, pDev->pszManufacturer, true);
|
---|
1196 | AssertRC(vrc);
|
---|
1197 | }
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 |
|
---|
1201 | /**
|
---|
1202 | * Searches the list of devices (mDevices) for the given device.
|
---|
1203 | *
|
---|
1204 | *
|
---|
1205 | * @returns Smart pointer to the device on success, NULL otherwise.
|
---|
1206 | * @param aId The UUID of the device we're looking for.
|
---|
1207 | */
|
---|
1208 | ComObjPtr<HostUSBDevice> USBProxyService::findDeviceById(IN_GUID aId)
|
---|
1209 | {
|
---|
1210 | Guid Id(aId);
|
---|
1211 | ComObjPtr<HostUSBDevice> Dev;
|
---|
1212 | for (HostUSBDeviceList::iterator It = mDevices.begin();
|
---|
1213 | It != mDevices.end();
|
---|
1214 | ++It)
|
---|
1215 | if ((*It)->getId() == Id)
|
---|
1216 | {
|
---|
1217 | Dev = (*It);
|
---|
1218 | break;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | return Dev;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | /*static*/
|
---|
1225 | HRESULT USBProxyService::setError(HRESULT aResultCode, const char *aText, ...)
|
---|
1226 | {
|
---|
1227 | va_list va;
|
---|
1228 | va_start(va, aText);
|
---|
1229 | HRESULT rc = VirtualBoxBase::setErrorInternal(aResultCode,
|
---|
1230 | COM_IIDOF(IHost),
|
---|
1231 | "USBProxyService",
|
---|
1232 | Utf8StrFmt(aText, va),
|
---|
1233 | false /* aWarning*/,
|
---|
1234 | true /* aLogIt*/);
|
---|
1235 | va_end(va);
|
---|
1236 | return rc;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|