VirtualBox

source: vbox/trunk/src/VBox/Main/USBProxyService.cpp@ 2988

Last change on this file since 2988 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/** @file
2 * VirtualBox USB Proxy Service (base) class.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#include "USBProxyService.h"
22#include "Logging.h"
23
24#include <VBox/err.h>
25#include <iprt/asm.h>
26#include <iprt/semaphore.h>
27
28
29
30/** @todo add the required locking. */
31
32/**
33 * Initialize data members.
34 */
35USBProxyService::USBProxyService (Host *aHost)
36 : mHost (aHost), mThread (NIL_RTTHREAD), mTerminate (false), mDevices (), mLastError (VINF_SUCCESS)
37{
38 LogFlowMember (("USBProxyService::USBProxyService: aHost=%p\n", aHost));
39}
40
41
42/**
43 * Empty destructor.
44 */
45USBProxyService::~USBProxyService()
46{
47 LogFlowMember (("USBProxyService::~USBProxyService: \n"));
48 Assert (mThread == NIL_RTTHREAD);
49 mDevices.clear();
50 mTerminate = true;
51 mHost = NULL;
52}
53
54
55bool USBProxyService::isActive (void)
56{
57 return mThread != NIL_RTTHREAD;
58}
59
60
61int USBProxyService::getLastError (void)
62{
63 return mLastError;
64}
65
66
67int USBProxyService::start (void)
68{
69 int rc = VINF_SUCCESS;
70 if (mThread == NIL_RTTHREAD)
71 {
72 /*
73 * Force update before starting the poller thread.
74 */
75 wait (0);
76 processChanges ();
77
78 /*
79 * Create the poller thread which will look for changes.
80 */
81 mTerminate = false;
82 rc = RTThreadCreate (&mThread, USBProxyService::serviceThread, this,
83 0, RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "USBPROXY");
84 AssertRC (rc);
85 if (VBOX_SUCCESS (rc))
86 LogFlow (("USBProxyService::start: started mThread=%RTthrd\n", mThread));
87 else
88 {
89 mThread = NIL_RTTHREAD;
90 mLastError = rc;
91 }
92 }
93 else
94 LogFlow (("USBProxyService::start: already running, mThread=%RTthrd\n", mThread));
95 return rc;
96}
97
98
99int USBProxyService::stop (void)
100{
101 int rc = VINF_SUCCESS;
102 if (mThread != NIL_RTTHREAD)
103 {
104 /*
105 * Mark the thread for termination and kick it.
106 */
107 ASMAtomicXchgSize (&mTerminate, true);
108 rc = interruptWait();
109 AssertRC (rc);
110
111 /*
112 * Wait for the thread to finish and then update the state.
113 */
114 rc = RTThreadWait (mThread, 60000, NULL);
115 if (rc == VERR_INVALID_HANDLE)
116 rc = VINF_SUCCESS;
117 if (VBOX_SUCCESS (rc))
118 {
119 LogFlowMember (("USBProxyService::stop: stopped mThread=%RTthrd\n", mThread));
120 mThread = NIL_RTTHREAD;
121 mTerminate = false;
122 }
123 else
124 {
125 AssertRC (rc);
126 mLastError = rc;
127 }
128 }
129 else
130 LogFlowMember (("USBProxyService::stop: not active\n"));
131
132 return rc;
133}
134
135
136/**
137 * Sort a list of USB devices.
138 *
139 * @returns Pointer to the head of the sorted doubly linked list.
140 * @param aDevices Head pointer (can be both singly and doubly linked list).
141 */
142static PUSBDEVICE sortDevices (PUSBDEVICE pDevices)
143{
144 PUSBDEVICE pHead = NULL;
145 PUSBDEVICE pTail = NULL;
146 while (pDevices)
147 {
148 /* unlink head */
149 PUSBDEVICE pDev = pDevices;
150 pDevices = pDev->pNext;
151 if (pDevices)
152 pDevices->pPrev = NULL;
153
154 /* find location. */
155 PUSBDEVICE pCur = pTail;
156 while ( pCur
157 && HostUSBDevice::compare (pCur, pDev) > 0)
158 pCur = pCur->pPrev;
159
160 /* insert (after pCur) */
161 pDev->pPrev = pCur;
162 if (pCur)
163 {
164 pDev->pNext = pCur->pNext;
165 pCur->pNext = pDev;
166 if (pDev->pNext)
167 pDev->pNext->pPrev = pDev;
168 else
169 pTail = pDev;
170 }
171 else
172 {
173 pDev->pNext = pHead;
174 if (pHead)
175 pHead->pPrev = pDev;
176 else
177 pTail = pDev;
178 pHead = pDev;
179 }
180 }
181
182 return pHead;
183}
184
185
186void USBProxyService::processChanges (void)
187{
188 LogFlowMember (("USBProxyService::processChanges: \n"));
189
190 /*
191 * Get the sorted list of USB devices.
192 */
193 PUSBDEVICE pDevices = getDevices();
194 if (pDevices)
195 {
196 pDevices = sortDevices (pDevices);
197
198 /*
199 * Compare previous list with the previous list of devices
200 * and merge in any changes while notifying Host.
201 */
202 HostUSBDeviceList::iterator It = this->mDevices.begin();
203 while ( It != mDevices.end()
204 || pDevices)
205 {
206 /*
207 * Compare.
208 */
209 ComObjPtr <HostUSBDevice> DevPtr;
210 int iDiff;
211 if (It == mDevices.end())
212 iDiff = 1;
213 else
214 {
215 DevPtr = *It;
216 if (!pDevices)
217 iDiff = -1;
218 else
219 iDiff = DevPtr->compare (pDevices);
220 }
221 if (!iDiff)
222 {
223 /*
224 * Device still there, update the state and move on.
225 */
226 if (updateDeviceState (DevPtr, pDevices))
227 mHost->onUSBDeviceStateChanged (DevPtr);
228 It++;
229 PUSBDEVICE pFree = pDevices;
230 pDevices = pDevices->pNext; /* treated as singly linked */
231 freeDevice (pFree);
232 }
233 else
234 {
235 if (iDiff > 0)
236 {
237 /*
238 * Head of pDevices was attached.
239 */
240 PUSBDEVICE pNew = pDevices;
241 pDevices = pDevices->pNext;
242 pNew->pPrev = pNew->pNext = NULL;
243
244 ComObjPtr <HostUSBDevice> NewObj;
245 NewObj.createObject();
246 NewObj->init (pNew, this);
247 Log (("USBProxyService::processChanges: attached %p/%p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
248 (HostUSBDevice *)NewObj, pNew, pNew->idVendor, pNew->idProduct, pNew->pszProduct, pNew->pszManufacturer));
249
250 mDevices.insert (It, NewObj);
251 mHost->onUSBDeviceAttached (NewObj);
252 }
253 else
254 {
255 /*
256 * DevPtr was detached, unless there is a pending async request.
257 */
258 /** @todo add a timeout here. */
259 if (!DevPtr->isStatePendingUnlocked())
260 {
261 It = mDevices.erase (It);
262 mHost->onUSBDeviceDetached (DevPtr);
263 Log (("USBProxyService::processChanges: detached %p\n", (HostUSBDevice *)DevPtr)); /** @todo add details .*/
264 }
265 /* else: operation pending */
266 }
267 }
268 } /* while */
269 }
270 else
271 {
272 /* All devices were detached */
273 HostUSBDeviceList::iterator It = this->mDevices.begin();
274 while (It != mDevices.end())
275 {
276 ComObjPtr <HostUSBDevice> DevPtr = *It;
277 /*
278 * DevPtr was detached.
279 */
280 It = mDevices.erase (It);
281 mHost->onUSBDeviceDetached (DevPtr);
282 Log (("USBProxyService::processChanges: detached %p\n", (HostUSBDevice *)DevPtr)); /** @todo add details .*/
283 }
284 }
285
286 LogFlowMember (("USBProxyService::processChanges: returns void\n"));
287}
288
289
290/*static*/ DECLCALLBACK (int) USBProxyService::serviceThread (RTTHREAD Thread, void *pvUser)
291{
292 USBProxyService *pThis = (USBProxyService *)pvUser;
293 LogFlow (("USBProxyService::serviceThread: pThis=%p\n", pThis));
294 pThis->serviceThreadInit();
295
296 /*
297 * Processing loop.
298 */
299 for (;;)
300 {
301 pThis->wait (RT_INDEFINITE_WAIT);
302 if (pThis->mTerminate)
303 break;
304 pThis->processChanges();
305 }
306
307 pThis->serviceThreadTerm();
308 LogFlow (("USBProxyService::serviceThread: returns VINF_SUCCESS\n"));
309 return VINF_SUCCESS;
310}
311
312
313/*static*/ void USBProxyService::freeDevice (PUSBDEVICE pDevice)
314{
315 PUSBCONFIG pCfg = pDevice->paConfigurations;
316 unsigned cCfgs = pDevice->bNumConfigurations;
317 while (cCfgs-- > 0)
318 {
319 PUSBINTERFACE pIf = pCfg->paInterfaces;
320 unsigned cIfs = pCfg->bNumInterfaces;
321 while (cIfs-- > 0)
322 {
323 RTMemFree (pIf->paEndpoints);
324 pIf->paEndpoints = NULL;
325 RTStrFree ((char *)pIf->pszDriver);
326 pIf->pszDriver = NULL;
327 RTStrFree ((char *)pIf->pszInterface);
328 pIf->pszInterface = NULL;
329 /* next */
330 pIf++;
331 }
332 RTMemFree (pCfg->paInterfaces);
333 pCfg->paInterfaces = NULL;
334 RTStrFree ((char *)pCfg->pszConfiguration);
335 pCfg->pszConfiguration = NULL;
336
337 /* next */
338 pCfg++;
339 }
340 RTMemFree (pDevice->paConfigurations);
341 pDevice->paConfigurations = NULL;
342
343 RTStrFree ((char *)pDevice->pszManufacturer);
344 pDevice->pszManufacturer = NULL;
345 RTStrFree ((char *)pDevice->pszProduct);
346 pDevice->pszProduct = NULL;
347 RTStrFree ((char *)pDevice->pszSerialNumber);
348 pDevice->pszSerialNumber = NULL;
349
350 RTStrFree ((char *)pDevice->pszAddress);
351 pDevice->pszAddress = NULL;
352
353 RTMemFree (pDevice);
354
355}
356
357
358/* static */ uint64_t USBProxyService::calcSerialHash (const char *aSerial)
359{
360 if (!aSerial)
361 aSerial = "";
362
363 register const uint8_t *pu8 = (const uint8_t *)aSerial;
364 register uint64_t u64 = 14695981039346656037ULL;
365 for (;;)
366 {
367 register uint8_t u8 = *pu8;
368 if (!u8)
369 break;
370 u64 = (u64 * 1099511628211ULL) ^ u8;
371 pu8++;
372 }
373
374 return u64;
375}
376
377
378bool USBProxyService::updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice)
379{
380 if (aDevice->isStatePendingUnlocked())
381 {
382 switch (aDevice->pendingStateUnlocked())
383 {
384 case USBDeviceState_USBDeviceCaptured: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_GUEST; break;
385 case USBDeviceState_USBDeviceHeld: aUSBDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY; break;
386 case USBDeviceState_USBDeviceAvailable: aUSBDevice->enmState = USBDEVICESTATE_UNUSED; break;
387 case USBDeviceState_USBDeviceUnavailable: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST; break;
388 case USBDeviceState_USBDeviceBusy: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; break;
389 default:
390 AssertMsgFailed(("%d\n", aDevice->pendingStateUnlocked()));
391 break;
392 }
393 }
394
395 return USBProxyService::updateDeviceState (aDevice, aUSBDevice);
396}
397
398
399
400/* Stubs which the host specific classes overrides: */
401
402
403int USBProxyService::wait (unsigned aMillies)
404{
405 return RTThreadSleep (250);
406}
407
408
409int USBProxyService::interruptWait (void)
410{
411 return VERR_NOT_IMPLEMENTED;
412}
413
414
415PUSBDEVICE USBProxyService::getDevices (void)
416{
417 return NULL;
418}
419
420
421void USBProxyService::serviceThreadInit (void)
422{
423}
424
425
426void USBProxyService::serviceThreadTerm (void)
427{
428}
429
430
431/**
432 * The default implementation returns non-NULL to emulate successful insertions
433 * for those subclasses that don't reimplement this method.
434 */
435void *USBProxyService::insertFilter (IUSBDeviceFilter * /* aFilter */)
436{
437 // return non-NULL to prevent failed assertions in Main
438 return (void *) 1;
439}
440
441
442void USBProxyService::removeFilter (void * /* aID */)
443{
444}
445
446
447int USBProxyService::captureDevice (HostUSBDevice *pDevice)
448{
449 return VERR_NOT_IMPLEMENTED;
450}
451
452
453int USBProxyService::holdDevice (HostUSBDevice *pDevice)
454{
455 return VERR_NOT_IMPLEMENTED;
456}
457
458
459int USBProxyService::releaseDevice (HostUSBDevice *pDevice)
460{
461 return VERR_NOT_IMPLEMENTED;
462}
463
464
465int USBProxyService::resetDevice (HostUSBDevice *pDevice)
466{
467 return VERR_NOT_IMPLEMENTED;
468}
469
470
471bool USBProxyService::updateDeviceState (HostUSBDevice *pDevice, PUSBDEVICE pUSBDevice)
472{
473 return pDevice->updateState (pUSBDevice);
474}
475
Note: See TracBrowser for help on using the repository browser.

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