VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/USBDeviceFilterImpl.cpp@ 67954

Last change on this file since 67954 was 65120, checked in by vboxsync, 8 years ago

Main: doxygen fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.6 KB
Line 
1/* $Id: USBDeviceFilterImpl.cpp 65120 2017-01-04 17:10:35Z vboxsync $ */
2/** @file
3 * Implementation of VirtualBox COM components: USBDeviceFilter and HostUSBDeviceFilter
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 "USBDeviceFilterImpl.h"
19#include "USBDeviceFiltersImpl.h"
20#include "MachineImpl.h"
21#include "HostImpl.h"
22
23#include <iprt/cpp/utils.h>
24#include <VBox/settings.h>
25
26#include "AutoStateDep.h"
27#include "AutoCaller.h"
28#include "Logging.h"
29
30////////////////////////////////////////////////////////////////////////////////
31// Internal Helpers
32////////////////////////////////////////////////////////////////////////////////
33
34/**
35 * Converts a USBFilter field into a string.
36 *
37 * (This function is also used by HostUSBDeviceFilter.)
38 *
39 * @param aFilter The filter.
40 * @param aIdx The field index.
41 * @param rstrOut The output string.
42 */
43static void i_usbFilterFieldToString(PCUSBFILTER aFilter, USBFILTERIDX aIdx, Utf8Str &rstrOut)
44{
45 const USBFILTERMATCH matchingMethod = USBFilterGetMatchingMethod(aFilter, aIdx);
46 Assert(matchingMethod != USBFILTERMATCH_INVALID);
47
48 if (USBFilterIsMethodNumeric(matchingMethod))
49 {
50 int value = USBFilterGetNum(aFilter, aIdx);
51 Assert(value >= 0 && value <= 0xffff);
52
53 rstrOut = Utf8StrFmt("%04RX16", (uint16_t)value);
54 }
55 else if (USBFilterIsMethodString(matchingMethod))
56 rstrOut = USBFilterGetString(aFilter, aIdx);
57 else
58 rstrOut.setNull();
59}
60
61/*static*/
62const char* USBDeviceFilter::i_describeUSBFilterIdx(USBFILTERIDX aIdx)
63{
64 switch (aIdx)
65 {
66 case USBFILTERIDX_VENDOR_ID: return tr("Vendor ID");
67 case USBFILTERIDX_PRODUCT_ID: return tr("Product ID");
68 case USBFILTERIDX_DEVICE: return tr("Revision");
69 case USBFILTERIDX_MANUFACTURER_STR: return tr("Manufacturer");
70 case USBFILTERIDX_PRODUCT_STR: return tr("Product");
71 case USBFILTERIDX_SERIAL_NUMBER_STR: return tr("Serial number");
72 case USBFILTERIDX_PORT: return tr("Port number");
73 default: return "";
74 }
75 /* not reached. */
76}
77
78/**
79 * Interprets a string and assigns it to a USBFilter field.
80 *
81 * (This function is also used by HostUSBDeviceFilter.)
82 *
83 * @param aFilter The filter.
84 * @param aIdx The field index.
85 * @param aValue The input string.
86 * @param aErrStr Where to return the error string on failure.
87 *
88 * @return COM status code.
89 * @remark The idea was to have this as a static function, but tr() doesn't wanna work without a class :-/
90 */
91/*static*/ HRESULT USBDeviceFilter::i_usbFilterFieldFromString(PUSBFILTER aFilter,
92 USBFILTERIDX aIdx,
93 const Utf8Str &aValue,
94 Utf8Str &aErrStr)
95{
96 int vrc;
97 if (aValue.isEmpty())
98 vrc = USBFilterSetIgnore(aFilter, aIdx);
99 else
100 {
101 const char *pcszValue = aValue.c_str();
102 if (USBFilterIsNumericField(aIdx))
103 {
104 /* Is it a lonely number? */
105 char *pszNext;
106 uint64_t u64;
107 vrc = RTStrToUInt64Ex(pcszValue, &pszNext, 16, &u64);
108 if (RT_SUCCESS(vrc))
109 pszNext = RTStrStripL(pszNext);
110 if ( vrc == VINF_SUCCESS
111 && !*pszNext)
112 {
113 if (u64 > 0xffff)
114 {
115 // there was a bug writing out "-1" values in earlier versions, which got
116 // written as "FFFFFFFF"; make sure we don't fail on those
117 if (u64 == 0xffffffff)
118 u64 = 0xffff;
119 else
120 {
121 aErrStr = Utf8StrFmt(tr("The %s value '%s' is too big (max 0xFFFF)"),
122 i_describeUSBFilterIdx(aIdx), pcszValue);
123 return E_INVALIDARG;
124 }
125 }
126
127 vrc = USBFilterSetNumExact(aFilter, aIdx, (uint16_t)u64, true /* fMustBePresent */);
128 }
129 else
130 vrc = USBFilterSetNumExpression(aFilter, aIdx, pcszValue, true /* fMustBePresent */);
131 }
132 else
133 {
134 /* Any wildcard in the string? */
135 Assert(USBFilterIsStringField(aIdx));
136 if ( strchr(pcszValue, '*')
137 || strchr(pcszValue, '?')
138 /* || strchr (psz, '[') - later */
139 )
140 vrc = USBFilterSetStringPattern(aFilter, aIdx, pcszValue, true /*fMustBePresent*/);
141 else
142 vrc = USBFilterSetStringExact(aFilter, aIdx, pcszValue, true /*fMustBePresent*/, false /*fPurge*/);
143 }
144 }
145
146 if (RT_FAILURE(vrc))
147 {
148 if (vrc == VERR_INVALID_PARAMETER)
149 {
150 aErrStr = Utf8StrFmt(tr("The %s filter expression '%s' is not valid"), i_describeUSBFilterIdx(aIdx), aValue.c_str());
151 return E_INVALIDARG;
152 }
153 if (vrc == VERR_BUFFER_OVERFLOW)
154 {
155 aErrStr = Utf8StrFmt(tr("Insufficient expression space for the '%s' filter expression '%s'"),
156 i_describeUSBFilterIdx(aIdx), aValue.c_str());
157 return E_FAIL;
158 }
159 AssertRC(vrc);
160 aErrStr = Utf8StrFmt(tr("Encountered unexpected status %Rrc when setting '%s' to '%s'"),
161 vrc, i_describeUSBFilterIdx(aIdx), aValue.c_str());
162 return E_FAIL;
163 }
164
165 return S_OK;
166}
167
168
169////////////////////////////////////////////////////////////////////////////////
170// USBDeviceFilter
171////////////////////////////////////////////////////////////////////////////////
172
173// constructor / destructor
174////////////////////////////////////////////////////////////////////////////////
175
176USBDeviceFilter::USBDeviceFilter()
177 : mParent(NULL),
178 mPeer(NULL)
179{
180}
181
182USBDeviceFilter::~USBDeviceFilter()
183{
184}
185
186HRESULT USBDeviceFilter::FinalConstruct()
187{
188 return BaseFinalConstruct();
189}
190
191void USBDeviceFilter::FinalRelease()
192{
193 uninit();
194 BaseFinalRelease();
195}
196
197// public initializer/uninitializer for internal purposes only
198////////////////////////////////////////////////////////////////////////////////
199
200/**
201 * Initializes the USB device filter object.
202 *
203 * @param aParent Handle of the parent object.
204 * @param data Reference filter settings.
205 */
206HRESULT USBDeviceFilter::init(USBDeviceFilters *aParent,
207 const settings::USBDeviceFilter &data)
208{
209 LogFlowThisFunc(("aParent=%p\n", aParent));
210
211 ComAssertRet(aParent && !data.strName.isEmpty(), E_INVALIDARG);
212
213 /* Enclose the state transition NotReady->InInit->Ready */
214 AutoInitSpan autoInitSpan(this);
215 AssertReturn(autoInitSpan.isOk(), E_FAIL);
216
217 unconst(mParent) = aParent;
218 /* mPeer is left null */
219
220 m_fModified = false;
221
222 bd.allocate();
223 bd->mData.strName = data.strName;
224 bd->mData.fActive = data.fActive;
225 bd->mData.ulMaskedInterfaces = 0;
226
227 /* initialize all filters to any match using null string */
228 USBFilterInit(&bd->mUSBFilter, USBFILTERTYPE_CAPTURE);
229 bd->mRemote = NULL;
230
231 mInList = false;
232
233 /* use setters for the attributes below to reuse parsing errors
234 * handling */
235
236 HRESULT rc = S_OK;
237 do
238 {
239 rc = i_usbFilterFieldSetter(USBFILTERIDX_VENDOR_ID, data.strVendorId);
240 if (FAILED(rc)) break;
241
242 rc = i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_ID, data.strProductId);
243 if (FAILED(rc)) break;
244
245 rc = i_usbFilterFieldSetter(USBFILTERIDX_DEVICE, data.strRevision);
246 if (FAILED(rc)) break;
247
248 rc = i_usbFilterFieldSetter(USBFILTERIDX_MANUFACTURER_STR, data.strManufacturer);
249 if (FAILED(rc)) break;
250
251 rc = i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_STR, data.strProduct);
252 if (FAILED(rc)) break;
253
254 rc = i_usbFilterFieldSetter(USBFILTERIDX_SERIAL_NUMBER_STR, data.strSerialNumber);
255 if (FAILED(rc)) break;
256
257 rc = i_usbFilterFieldSetter(USBFILTERIDX_PORT, data.strPort);
258 if (FAILED(rc)) break;
259
260 rc = COMSETTER(Remote)(Bstr(data.strRemote).raw());
261 if (FAILED(rc)) break;
262
263 rc = COMSETTER(MaskedInterfaces)(data.ulMaskedInterfaces);
264 if (FAILED(rc)) break;
265 }
266 while (0);
267
268 /* Confirm successful initialization when it's the case */
269 if (SUCCEEDED(rc))
270 autoInitSpan.setSucceeded();
271
272 return rc;
273}
274
275/**
276 * Initializes the USB device filter object (short version).
277 *
278 * @param aParent Handle of the parent object.
279 * @param aName Name of the filter.
280 */
281HRESULT USBDeviceFilter::init(USBDeviceFilters *aParent, IN_BSTR aName)
282{
283 LogFlowThisFunc(("aParent=%p\n", aParent));
284
285 ComAssertRet(aParent && aName && *aName, E_INVALIDARG);
286
287 /* Enclose the state transition NotReady->InInit->Ready */
288 AutoInitSpan autoInitSpan(this);
289 AssertReturn(autoInitSpan.isOk(), E_FAIL);
290
291 unconst(mParent) = aParent;
292 /* mPeer is left null */
293
294 m_fModified = false;
295
296 bd.allocate();
297
298 bd->mData.strName = Utf8Str(aName);
299 bd->mData.fActive = FALSE;
300 bd->mData.ulMaskedInterfaces = 0;
301
302 /* initialize all filters to any match using null string */
303 USBFilterInit(&bd->mUSBFilter, USBFILTERTYPE_CAPTURE);
304 bd->mRemote = NULL;
305
306 mInList = false;
307
308 /* Confirm successful initialization */
309 autoInitSpan.setSucceeded();
310
311 return S_OK;
312}
313
314/**
315 * Initializes the object given another object
316 * (a kind of copy constructor). This object shares data with
317 * the object passed as an argument.
318 *
319 * @param aParent Handle of the parent object.
320 * @param aThat
321 * @param aReshare
322 * When false, the original object will remain a data owner.
323 * Otherwise, data ownership will be transferred from the original
324 * object to this one.
325 *
326 * @note This object must be destroyed before the original object
327 * it shares data with is destroyed.
328 *
329 * @note Locks @a aThat object for writing if @a aReshare is @c true, or for
330 * reading if @a aReshare is false.
331 */
332HRESULT USBDeviceFilter::init(USBDeviceFilters *aParent, USBDeviceFilter *aThat,
333 bool aReshare /* = false */)
334{
335 LogFlowThisFunc(("aParent=%p, aThat=%p, aReshare=%RTbool\n",
336 aParent, aThat, aReshare));
337
338 ComAssertRet(aParent && aThat, E_INVALIDARG);
339
340 /* Enclose the state transition NotReady->InInit->Ready */
341 AutoInitSpan autoInitSpan(this);
342 AssertReturn(autoInitSpan.isOk(), E_FAIL);
343
344 unconst(mParent) = aParent;
345
346 m_fModified = false;
347
348 /* sanity */
349 AutoCaller thatCaller(aThat);
350 AssertComRCReturnRC(thatCaller.rc());
351
352 if (aReshare)
353 {
354 AutoWriteLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
355
356 unconst(aThat->mPeer) = this;
357 bd.attach(aThat->bd);
358 }
359 else
360 {
361 unconst(mPeer) = aThat;
362
363 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
364 bd.share(aThat->bd);
365 }
366
367 /* the arbitrary ID field is not reset because
368 * the copy is a shadow of the original */
369
370 mInList = aThat->mInList;
371
372 /* Confirm successful initialization */
373 autoInitSpan.setSucceeded();
374
375 return S_OK;
376}
377
378/**
379 * Initializes the guest object given another guest object
380 * (a kind of copy constructor). This object makes a private copy of data
381 * of the original object passed as an argument.
382 *
383 * @note Locks @a aThat object for reading.
384 */
385HRESULT USBDeviceFilter::initCopy(USBDeviceFilters *aParent, USBDeviceFilter *aThat)
386{
387 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
388
389 ComAssertRet(aParent && aThat, E_INVALIDARG);
390
391 /* Enclose the state transition NotReady->InInit->Ready */
392 AutoInitSpan autoInitSpan(this);
393 AssertReturn(autoInitSpan.isOk(), E_FAIL);
394
395 unconst(mParent) = aParent;
396 /* mPeer is left null */
397
398 m_fModified = false;
399
400 /* sanity */
401 AutoCaller thatCaller(aThat);
402 AssertComRCReturnRC(thatCaller.rc());
403
404 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
405 bd.attachCopy(aThat->bd);
406
407 /* reset the arbitrary ID field
408 * (this field is something unique that two distinct objects, even if they
409 * are deep copies of each other, should not share) */
410 bd->mId = NULL;
411
412 mInList = aThat->mInList;
413
414 /* Confirm successful initialization */
415 autoInitSpan.setSucceeded();
416
417 return S_OK;
418}
419
420/**
421 * Uninitializes the instance and sets the ready flag to FALSE.
422 * Called either from FinalRelease() or by the parent when it gets destroyed.
423 */
424void USBDeviceFilter::uninit()
425{
426 LogFlowThisFunc(("\n"));
427
428 /* Enclose the state transition Ready->InUninit->NotReady */
429 AutoUninitSpan autoUninitSpan(this);
430 if (autoUninitSpan.uninitDone())
431 return;
432
433 mInList = false;
434
435 bd.free();
436
437 unconst(mPeer) = NULL;
438 unconst(mParent) = NULL;
439}
440
441
442// IUSBDeviceFilter properties
443////////////////////////////////////////////////////////////////////////////////
444
445HRESULT USBDeviceFilter::getName(com::Utf8Str &aName)
446{
447 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
448
449 aName = bd->mData.strName;
450
451 return S_OK;
452}
453
454HRESULT USBDeviceFilter::setName(const com::Utf8Str &aName)
455{
456 /* the machine needs to be mutable */
457 AutoMutableOrSavedOrRunningStateDependency adep(mParent->i_getMachine());
458 if (FAILED(adep.rc())) return adep.rc();
459
460 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
461
462 if (bd->mData.strName != aName)
463 {
464 m_fModified = true;
465 ComObjPtr<Machine> pMachine = mParent->i_getMachine();
466
467 bd.backup();
468 bd->mData.strName = aName;
469
470 // leave the lock before informing callbacks
471 alock.release();
472
473 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
474 pMachine->i_setModified(Machine::IsModified_USB);
475 mlock.release();
476
477 return mParent->i_onDeviceFilterChange(this);
478 }
479
480 return S_OK;
481}
482
483HRESULT USBDeviceFilter::getActive(BOOL *aActive)
484{
485 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
486
487 *aActive = bd->mData.fActive;
488
489 return S_OK;
490}
491
492HRESULT USBDeviceFilter::setActive(const BOOL aActive)
493{
494 /* the machine needs to be mutable */
495 AutoMutableOrSavedOrRunningStateDependency adep(mParent->i_getMachine());
496 if (FAILED(adep.rc())) return adep.rc();
497
498 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
499
500 if (bd->mData.fActive != RT_BOOL(aActive))
501 {
502 m_fModified = true;
503 ComObjPtr<Machine> pMachine = mParent->i_getMachine();
504
505 bd.backup();
506 bd->mData.fActive = RT_BOOL(aActive);
507
508 // leave the lock before informing callbacks
509 alock.release();
510
511 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
512 pMachine->i_setModified(Machine::IsModified_USB);
513 mlock.release();
514
515 return mParent->i_onDeviceFilterChange(this, TRUE /* aActiveChanged */);
516 }
517
518 return S_OK;
519}
520
521HRESULT USBDeviceFilter::getVendorId(com::Utf8Str &aVendorId)
522{
523 return i_usbFilterFieldGetter(USBFILTERIDX_VENDOR_ID, aVendorId);
524}
525
526HRESULT USBDeviceFilter::setVendorId(const com::Utf8Str &aVendorId)
527{
528 return i_usbFilterFieldSetter(USBFILTERIDX_VENDOR_ID, aVendorId);
529}
530
531HRESULT USBDeviceFilter::getProductId(com::Utf8Str &aProductId)
532{
533 return i_usbFilterFieldGetter(USBFILTERIDX_PRODUCT_ID, aProductId);
534}
535
536HRESULT USBDeviceFilter::setProductId(const com::Utf8Str &aProductId)
537{
538 return i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_ID, aProductId);
539}
540
541HRESULT USBDeviceFilter::getRevision(com::Utf8Str &aRevision)
542{
543 return i_usbFilterFieldGetter(USBFILTERIDX_DEVICE, aRevision);
544}
545
546HRESULT USBDeviceFilter::setRevision(const com::Utf8Str &aRevision)
547{
548 return i_usbFilterFieldSetter(USBFILTERIDX_DEVICE, aRevision);
549}
550
551HRESULT USBDeviceFilter::getManufacturer(com::Utf8Str &aManufacturer)
552{
553 return i_usbFilterFieldGetter(USBFILTERIDX_MANUFACTURER_STR, aManufacturer);
554}
555
556HRESULT USBDeviceFilter::setManufacturer(const com::Utf8Str &aManufacturer)
557{
558 return i_usbFilterFieldSetter(USBFILTERIDX_MANUFACTURER_STR, aManufacturer);
559}
560
561HRESULT USBDeviceFilter::getProduct(com::Utf8Str &aProduct)
562{
563 return i_usbFilterFieldGetter(USBFILTERIDX_PRODUCT_STR, aProduct);
564}
565
566HRESULT USBDeviceFilter::setProduct(const com::Utf8Str &aProduct)
567{
568 return i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_STR, aProduct);
569}
570
571HRESULT USBDeviceFilter::getSerialNumber(com::Utf8Str &aSerialNumber)
572{
573 return i_usbFilterFieldGetter(USBFILTERIDX_SERIAL_NUMBER_STR, aSerialNumber);
574}
575
576HRESULT USBDeviceFilter::setSerialNumber(const com::Utf8Str &aSerialNumber)
577{
578 return i_usbFilterFieldSetter(USBFILTERIDX_SERIAL_NUMBER_STR, aSerialNumber);
579}
580
581HRESULT USBDeviceFilter::getPort(com::Utf8Str &aPort)
582{
583 return i_usbFilterFieldGetter(USBFILTERIDX_PORT, aPort);
584}
585
586HRESULT USBDeviceFilter::setPort(const com::Utf8Str &aPort)
587{
588 return i_usbFilterFieldSetter(USBFILTERIDX_PORT, aPort);
589}
590
591
592HRESULT USBDeviceFilter::getRemote(com::Utf8Str &aRemote)
593{
594 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
595
596 aRemote = bd->mRemote.string();
597
598 return S_OK;
599}
600
601HRESULT USBDeviceFilter::setRemote(const com::Utf8Str &aRemote)
602{
603 /* the machine needs to be mutable */
604 AutoMutableOrSavedOrRunningStateDependency adep(mParent->i_getMachine());
605 if (FAILED(adep.rc())) return adep.rc();
606 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
607 Bstr bRemote = Bstr(aRemote).raw();
608
609 if (bd->mRemote.string() != bRemote)
610 {
611 BackupableUSBDeviceFilterData::BOOLFilter flt = bRemote;
612 ComAssertRet(!flt.isNull(), E_FAIL);
613 if (!flt.isValid())
614 return setError(E_INVALIDARG,
615 tr("Remote state filter string '%s' is not valid (error at position %d)"),
616 aRemote.c_str(), flt.errorPosition() + 1);
617
618 m_fModified = true;
619 ComObjPtr<Machine> pMachine = mParent->i_getMachine();
620
621 bd.backup();
622 bd->mRemote = flt;
623
624 // leave the lock before informing callbacks
625 alock.release();
626
627 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
628 pMachine->i_setModified(Machine::IsModified_USB);
629 mlock.release();
630
631 return mParent->i_onDeviceFilterChange(this);
632 }
633 return S_OK;
634}
635
636
637HRESULT USBDeviceFilter::getMaskedInterfaces(ULONG *aMaskedIfs)
638{
639 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
640
641 *aMaskedIfs = bd->mData.ulMaskedInterfaces;
642
643 return S_OK;
644}
645
646HRESULT USBDeviceFilter::setMaskedInterfaces(ULONG aMaskedIfs)
647{
648 /* the machine needs to be mutable */
649 AutoMutableOrSavedOrRunningStateDependency adep(mParent->i_getMachine());
650 if (FAILED(adep.rc())) return adep.rc();
651
652 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
653
654 if (bd->mData.ulMaskedInterfaces != aMaskedIfs)
655 {
656 m_fModified = true;
657 ComObjPtr<Machine> pMachine = mParent->i_getMachine();
658
659 bd.backup();
660 bd->mData.ulMaskedInterfaces = aMaskedIfs;
661 // leave the lock before informing callbacks
662 alock.release();
663
664 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
665 pMachine->i_setModified(Machine::IsModified_USB);
666 mlock.release();
667
668 return mParent->i_onDeviceFilterChange(this);
669 }
670
671 return S_OK;
672}
673
674// public methods only for internal purposes
675////////////////////////////////////////////////////////////////////////////////
676
677bool USBDeviceFilter::i_isModified()
678{
679 AutoCaller autoCaller(this);
680 AssertComRCReturn(autoCaller.rc(), false);
681
682 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
683 return m_fModified;
684}
685
686/**
687 * @note Locks this object for writing.
688 */
689void USBDeviceFilter::i_rollback()
690{
691 /* sanity */
692 AutoCaller autoCaller(this);
693 AssertComRCReturnVoid(autoCaller.rc());
694
695 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
696
697 bd.rollback();
698}
699
700/**
701 * @note Locks this object for writing, together with the peer object (also
702 * for writing) if there is one.
703 */
704void USBDeviceFilter::i_commit()
705{
706 /* sanity */
707 AutoCaller autoCaller(this);
708 AssertComRCReturnVoid(autoCaller.rc());
709
710 /* sanity too */
711 AutoCaller peerCaller(mPeer);
712 AssertComRCReturnVoid(peerCaller.rc());
713
714 /* lock both for writing since we modify both (mPeer is "master" so locked
715 * first) */
716 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
717
718 if (bd.isBackedUp())
719 {
720 bd.commit();
721 if (mPeer)
722 {
723 /* attach new data to the peer and reshare it */
724 mPeer->bd.attach(bd);
725 }
726 }
727}
728
729/**
730 * Cancels sharing (if any) by making an independent copy of data.
731 * This operation also resets this object's peer to NULL.
732 *
733 * @note Locks this object for writing, together with the peer object
734 * represented by @a aThat (locked for reading).
735 */
736void USBDeviceFilter::unshare()
737{
738 /* sanity */
739 AutoCaller autoCaller(this);
740 AssertComRCReturnVoid(autoCaller.rc());
741
742 /* sanity too */
743 AutoCaller peerCaller(mPeer);
744 AssertComRCReturnVoid(peerCaller.rc());
745
746 /* peer is not modified, lock it for reading (mPeer is "master" so locked
747 * first) */
748 AutoReadLock rl(mPeer COMMA_LOCKVAL_SRC_POS);
749 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
750
751 if (bd.isShared())
752 {
753 if (!bd.isBackedUp())
754 bd.backup();
755
756 bd.commit();
757 }
758
759 unconst(mPeer) = NULL;
760}
761
762/**
763 * Generic USB filter field getter; converts the field value to UTF-16.
764 *
765 * @param aIdx The field index.
766 * @param aStr Where to store the value.
767 *
768 * @return COM status.
769 */
770HRESULT USBDeviceFilter::i_usbFilterFieldGetter(USBFILTERIDX aIdx, com::Utf8Str &aStr)
771{
772 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
773
774 i_usbFilterFieldToString(&bd->mUSBFilter, aIdx, aStr);
775 return S_OK;
776}
777
778/**
779 * Generic USB filter field setter, expects UTF-8 input.
780 *
781 * @param aIdx The field index.
782 * @param strNew The new value.
783 *
784 * @return COM status.
785 */
786HRESULT USBDeviceFilter::i_usbFilterFieldSetter(USBFILTERIDX aIdx,
787 const com::Utf8Str &strNew)
788{
789 /* the machine needs to be mutable */
790 AutoMutableOrSavedOrRunningStateDependency adep(mParent->i_getMachine());
791 if (FAILED(adep.rc())) return adep.rc();
792
793 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
794
795
796 com::Utf8Str strOld;
797 i_usbFilterFieldToString(&bd->mUSBFilter, aIdx, strOld);
798 if (strOld != strNew)
799 {
800 m_fModified = true;
801 ComObjPtr<Machine> pMachine = mParent->i_getMachine();
802
803 bd.backup();
804
805 com::Utf8Str errStr;
806 HRESULT rc = i_usbFilterFieldFromString(&bd->mUSBFilter, aIdx, strNew, errStr);
807 if (FAILED(rc))
808 {
809 bd.rollback();
810 return setError(rc, "%s", errStr.c_str());
811 }
812
813 // leave the lock before informing callbacks
814 alock.release();
815
816 AutoWriteLock mlock(pMachine COMMA_LOCKVAL_SRC_POS);
817 pMachine->i_setModified(Machine::IsModified_USB);
818 mlock.release();
819
820 return mParent->i_onDeviceFilterChange(this);
821 }
822
823 return S_OK;
824}
825
826
827////////////////////////////////////////////////////////////////////////////////
828// HostUSBDeviceFilter
829////////////////////////////////////////////////////////////////////////////////
830
831// constructor / destructor
832////////////////////////////////////////////////////////////////////////////////
833
834HostUSBDeviceFilter::HostUSBDeviceFilter()
835 : mParent(NULL)
836{
837}
838
839HostUSBDeviceFilter::~HostUSBDeviceFilter()
840{
841}
842
843
844HRESULT HostUSBDeviceFilter::FinalConstruct()
845{
846 return S_OK;
847}
848
849void HostUSBDeviceFilter::FinalRelease()
850{
851 uninit();
852}
853
854// public initializer/uninitializer for internal purposes only
855////////////////////////////////////////////////////////////////////////////////
856
857/**
858 * Initializes the USB device filter object.
859 *
860 * @param aParent Handle of the parent object.
861 * @param data Settings data.
862 */
863HRESULT HostUSBDeviceFilter::init(Host *aParent,
864 const settings::USBDeviceFilter &data)
865{
866 LogFlowThisFunc(("aParent=%p\n", aParent));
867
868 ComAssertRet(aParent && !data.strName.isEmpty(), E_INVALIDARG);
869
870 /* Enclose the state transition NotReady->InInit->Ready */
871 AutoInitSpan autoInitSpan(this);
872 AssertReturn(autoInitSpan.isOk(), E_FAIL);
873
874 unconst(mParent) = aParent;
875
876 /* register with parent early, since uninit() will unconditionally
877 * unregister on failure */
878 mParent->i_addChild(this);
879
880 bd.allocate();
881 bd->mData.strName = data.strName;
882 bd->mData.fActive = data.fActive;
883 USBFilterInit (&bd->mUSBFilter, USBFILTERTYPE_IGNORE);
884 bd->mRemote = NULL;
885 bd->mData.ulMaskedInterfaces = 0;
886
887 mInList = false;
888
889 /* use setters for the attributes below to reuse parsing errors
890 * handling */
891
892 HRESULT rc = S_OK;
893 do
894 {
895 rc = setAction(data.action);
896 if (FAILED(rc)) break;
897
898 rc = i_usbFilterFieldSetter(USBFILTERIDX_VENDOR_ID, data.strVendorId);
899 if (FAILED(rc)) break;
900
901 rc = i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_ID, data.strProductId);
902 if (FAILED(rc)) break;
903
904 rc = i_usbFilterFieldSetter(USBFILTERIDX_DEVICE, data.strRevision);
905 if (FAILED(rc)) break;
906
907 rc = i_usbFilterFieldSetter(USBFILTERIDX_MANUFACTURER_STR, data.strManufacturer);
908 if (FAILED(rc)) break;
909
910 rc = i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_ID, data.strProduct);
911 if (FAILED(rc)) break;
912
913 rc = i_usbFilterFieldSetter(USBFILTERIDX_SERIAL_NUMBER_STR, data.strSerialNumber);
914 if (FAILED(rc)) break;
915
916 rc = i_usbFilterFieldSetter(USBFILTERIDX_PORT, data.strPort);
917 if (FAILED(rc)) break;
918 }
919 while (0);
920
921 /* Confirm successful initialization when it's the case */
922 if (SUCCEEDED(rc))
923 autoInitSpan.setSucceeded();
924
925 return rc;
926}
927
928/**
929 * Initializes the USB device filter object (short version).
930 *
931 * @param aParent Handle of the parent object.
932 * @param aName Filter name.
933 */
934HRESULT HostUSBDeviceFilter::init(Host *aParent, IN_BSTR aName)
935{
936 LogFlowThisFunc(("aParent=%p\n", aParent));
937
938 ComAssertRet(aParent && aName && *aName, E_INVALIDARG);
939
940 /* Enclose the state transition NotReady->InInit->Ready */
941 AutoInitSpan autoInitSpan(this);
942 AssertReturn(autoInitSpan.isOk(), E_FAIL);
943
944 unconst(mParent) = aParent;
945
946 /* register with parent early, since uninit() will unconditionally
947 * unregister on failure */
948 mParent->i_addChild(this);
949
950 bd.allocate();
951
952 bd->mData.strName = Utf8Str(aName);
953 bd->mData.fActive = FALSE;
954 mInList = false;
955 USBFilterInit(&bd->mUSBFilter, USBFILTERTYPE_IGNORE);
956 bd->mRemote = NULL;
957 bd->mData.ulMaskedInterfaces = 0;
958
959 /* Confirm successful initialization */
960 autoInitSpan.setSucceeded();
961
962 return S_OK;
963}
964
965/**
966 * Uninitializes the instance and sets the ready flag to FALSE.
967 * Called either from FinalRelease() or by the parent when it gets destroyed.
968 */
969void HostUSBDeviceFilter::uninit()
970{
971 LogFlowThisFunc(("\n"));
972
973 /* Enclose the state transition Ready->InUninit->NotReady */
974 AutoUninitSpan autoUninitSpan(this);
975 if (autoUninitSpan.uninitDone())
976 return;
977
978 mInList = false;
979
980 bd.free();
981
982 mParent->i_removeChild(this);
983
984 unconst(mParent) = NULL;
985}
986
987/**
988 * Most of the USB bits are protect by one lock to simplify things.
989 * This lock is currently the one of the Host object, which happens
990 * to be our parent.
991 */
992RWLockHandle *HostUSBDeviceFilter::lockHandle() const
993{
994 return mParent->lockHandle();
995}
996
997
998// IUSBDeviceFilter properties
999////////////////////////////////////////////////////////////////////////////////
1000HRESULT HostUSBDeviceFilter::getName(com::Utf8Str &aName)
1001{
1002 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1003
1004 aName = bd->mData.strName;
1005
1006 return S_OK;
1007}
1008
1009
1010HRESULT HostUSBDeviceFilter::setName(const com::Utf8Str &aName)
1011{
1012 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1013
1014 if (bd->mData.strName != aName)
1015 {
1016 bd->mData.strName = aName;
1017
1018 /* leave the lock before informing callbacks */
1019 alock.release();
1020
1021 return mParent->i_onUSBDeviceFilterChange(this);
1022 }
1023
1024 return S_OK;
1025}
1026
1027
1028HRESULT HostUSBDeviceFilter::getActive(BOOL *aActive)
1029{
1030 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1031
1032 *aActive = bd->mData.fActive;
1033
1034 return S_OK;
1035}
1036
1037
1038HRESULT HostUSBDeviceFilter::setActive(BOOL aActive)
1039{
1040 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1041
1042 if (bd->mData.fActive != RT_BOOL(aActive))
1043 {
1044 bd->mData.fActive = RT_BOOL(aActive);
1045
1046 /* leave the lock before informing callbacks */
1047 alock.release();
1048
1049 return mParent->i_onUSBDeviceFilterChange(this, TRUE /* aActiveChanged */);
1050 }
1051
1052 return S_OK;
1053}
1054
1055HRESULT HostUSBDeviceFilter::getVendorId(com::Utf8Str &aVendorId)
1056{
1057 return i_usbFilterFieldGetter(USBFILTERIDX_VENDOR_ID, aVendorId);
1058}
1059
1060HRESULT HostUSBDeviceFilter::setVendorId(const com::Utf8Str &aVendorId)
1061{
1062 return i_usbFilterFieldSetter(USBFILTERIDX_VENDOR_ID, aVendorId);
1063}
1064
1065HRESULT HostUSBDeviceFilter::getProductId(com::Utf8Str &aProductId)
1066{
1067 return i_usbFilterFieldGetter(USBFILTERIDX_PRODUCT_ID, aProductId);
1068}
1069
1070HRESULT HostUSBDeviceFilter::setProductId(const com::Utf8Str &aProductId)
1071{
1072 return i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_ID, aProductId);
1073}
1074
1075HRESULT HostUSBDeviceFilter::getRevision(com::Utf8Str &aRevision)
1076{
1077 return i_usbFilterFieldGetter(USBFILTERIDX_DEVICE, aRevision);
1078}
1079
1080HRESULT HostUSBDeviceFilter::setRevision(const com::Utf8Str &aRevision)
1081{
1082 return i_usbFilterFieldSetter(USBFILTERIDX_DEVICE, aRevision);
1083}
1084
1085HRESULT HostUSBDeviceFilter::getManufacturer(com::Utf8Str &aManufacturer)
1086{
1087 return i_usbFilterFieldGetter(USBFILTERIDX_MANUFACTURER_STR, aManufacturer);
1088}
1089
1090HRESULT HostUSBDeviceFilter::setManufacturer(const com::Utf8Str &aManufacturer)
1091{
1092 return i_usbFilterFieldSetter(USBFILTERIDX_MANUFACTURER_STR, aManufacturer);
1093}
1094
1095HRESULT HostUSBDeviceFilter::getProduct(com::Utf8Str &aProduct)
1096{
1097 return i_usbFilterFieldGetter(USBFILTERIDX_PRODUCT_STR, aProduct);
1098}
1099
1100HRESULT HostUSBDeviceFilter::setProduct(const com::Utf8Str &aProduct)
1101{
1102 return i_usbFilterFieldSetter(USBFILTERIDX_PRODUCT_STR, aProduct);
1103}
1104
1105HRESULT HostUSBDeviceFilter::getSerialNumber(com::Utf8Str &aSerialNumber)
1106{
1107 return i_usbFilterFieldGetter(USBFILTERIDX_SERIAL_NUMBER_STR, aSerialNumber);
1108}
1109
1110HRESULT HostUSBDeviceFilter::setSerialNumber(const com::Utf8Str &aSerialNumber)
1111{
1112 return i_usbFilterFieldSetter(USBFILTERIDX_SERIAL_NUMBER_STR, aSerialNumber);
1113}
1114
1115HRESULT HostUSBDeviceFilter::getPort(com::Utf8Str &aPort)
1116{
1117 return i_usbFilterFieldGetter(USBFILTERIDX_PORT, aPort);
1118}
1119
1120HRESULT HostUSBDeviceFilter::setPort(const com::Utf8Str &aPort)
1121{
1122 return i_usbFilterFieldSetter(USBFILTERIDX_PORT, aPort);
1123}
1124
1125HRESULT HostUSBDeviceFilter::getRemote(com::Utf8Str &aRemote)
1126{
1127 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1128
1129 aRemote = bd->mRemote.string();
1130
1131 return S_OK;
1132}
1133
1134HRESULT HostUSBDeviceFilter::setRemote(const com::Utf8Str & /* aRemote */)
1135{
1136 return setError(E_NOTIMPL,
1137 tr("The remote state filter is not supported by IHostUSBDeviceFilter objects"));
1138}
1139
1140
1141HRESULT HostUSBDeviceFilter::getMaskedInterfaces(ULONG *aMaskedIfs)
1142{
1143 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1144
1145 *aMaskedIfs = bd->mData.ulMaskedInterfaces;
1146
1147 return S_OK;
1148}
1149HRESULT HostUSBDeviceFilter::setMaskedInterfaces(ULONG /* aMaskedIfs */)
1150{
1151 return setError(E_NOTIMPL,
1152 tr("The masked interfaces property is not applicable to IHostUSBDeviceFilter objects"));
1153}
1154
1155// wrapped IHostUSBDeviceFilter properties
1156////////////////////////////////////////////////////////////////////////////////
1157HRESULT HostUSBDeviceFilter::getAction(USBDeviceFilterAction_T *aAction)
1158{
1159 CheckComArgOutPointerValid(aAction);
1160
1161 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1162
1163 switch (USBFilterGetFilterType(&bd->mUSBFilter))
1164 {
1165 case USBFILTERTYPE_IGNORE: *aAction = USBDeviceFilterAction_Ignore; break;
1166 case USBFILTERTYPE_CAPTURE: *aAction = USBDeviceFilterAction_Hold; break;
1167 default: *aAction = USBDeviceFilterAction_Null; break;
1168 }
1169
1170 return S_OK;
1171}
1172
1173
1174HRESULT HostUSBDeviceFilter::setAction(USBDeviceFilterAction_T aAction)
1175{
1176 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1177
1178 USBFILTERTYPE filterType;
1179 switch (aAction)
1180 {
1181 case USBDeviceFilterAction_Ignore: filterType = USBFILTERTYPE_IGNORE; break;
1182 case USBDeviceFilterAction_Hold: filterType = USBFILTERTYPE_CAPTURE; break;
1183 case USBDeviceFilterAction_Null:
1184 return setError(E_INVALIDARG,
1185 tr("Action value InvalidUSBDeviceFilterAction is not permitted"));
1186 default:
1187 return setError(E_INVALIDARG,
1188 tr("Invalid action %d"),
1189 aAction);
1190 }
1191 if (USBFilterGetFilterType(&bd->mUSBFilter) != filterType)
1192 {
1193 int vrc = USBFilterSetFilterType(&bd->mUSBFilter, filterType);
1194 if (RT_FAILURE(vrc))
1195 return setError(E_INVALIDARG,
1196 tr("Unexpected error %Rrc"),
1197 vrc);
1198
1199 /* leave the lock before informing callbacks */
1200 alock.release();
1201
1202 return mParent->i_onUSBDeviceFilterChange(this);
1203 }
1204
1205 return S_OK;
1206}
1207
1208
1209// IHostUSBDeviceFilter properties
1210////////////////////////////////////////////////////////////////////////////////
1211/**
1212 * Generic USB filter field getter.
1213 *
1214 * @param aIdx The field index.
1215 * @param aStr Where to store the value.
1216 *
1217 * @return COM status.
1218 */
1219HRESULT HostUSBDeviceFilter::i_usbFilterFieldGetter(USBFILTERIDX aIdx, com::Utf8Str &aStr)
1220{
1221 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1222 i_usbFilterFieldToString(&bd->mUSBFilter, aIdx, aStr);
1223 return S_OK;
1224}
1225
1226void HostUSBDeviceFilter::i_saveSettings(settings::USBDeviceFilter &data)
1227{
1228 AutoCaller autoCaller(this);
1229 AssertComRCReturnVoid(autoCaller.rc());
1230
1231 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1232 data.strName = bd->mData.strName;
1233 data.fActive = bd->mData.fActive;
1234 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_VENDOR_ID, data.strVendorId);
1235 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_PRODUCT_ID, data.strProductId);
1236 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_DEVICE, data.strRevision);
1237 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_MANUFACTURER_STR, data.strManufacturer);
1238 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_PRODUCT_STR, data.strProduct);
1239 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_SERIAL_NUMBER_STR, data.strSerialNumber);
1240 i_usbFilterFieldToString(&bd->mUSBFilter, USBFILTERIDX_PORT, data.strPort);
1241
1242 COMGETTER(Action)(&data.action);
1243}
1244
1245
1246/**
1247 * Generic USB filter field setter.
1248 *
1249 * @param aIdx The field index.
1250 * @param aStr The new value.
1251 *
1252 * @return COM status.
1253 */
1254HRESULT HostUSBDeviceFilter::i_usbFilterFieldSetter(USBFILTERIDX aIdx, const com::Utf8Str &aStr)
1255{
1256 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1257 Utf8Str strOld;
1258 i_usbFilterFieldToString(&bd->mUSBFilter, aIdx, strOld);
1259 if (strOld != aStr)
1260 {
1261 //bd.backup();
1262 com::Utf8Str errStr;
1263 HRESULT rc = USBDeviceFilter::i_usbFilterFieldFromString(&bd->mUSBFilter, aIdx, aStr, errStr);
1264 if (FAILED(rc))
1265 {
1266 //bd.rollback();
1267 return setError(rc, "%s", errStr.c_str());
1268 }
1269
1270 /* leave the lock before informing callbacks */
1271 alock.release();
1272
1273 return mParent->i_onUSBDeviceFilterChange(this);
1274 }
1275
1276 return S_OK;
1277}
1278/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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