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