VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageUSB.cpp@ 100772

Last change on this file since 100772 was 100772, checked in by vboxsync, 10 months ago

include/VBox/usbfilter.h,HostDrivers/VBoxUSB/USBFilter: IUSBDeviceFilter:
USB device interval filters don't work. bugref:10452

Main/Host,Main/USBDeviceFilter: Adding or removing global USB device
filters causes memory corruption wihch can lead to a deadlock or a SEGV
as the list of global USB device filters (llChildren) changes while
the list is being walked.

Frontends/VBoxManage: 'VBoxManage list usbfilters' doesn't display the
'Port' value of the device filter.

Frontends/VBoxManage: 'VBoxManage add usbfilter' and 'VBoxManage modify
usbfilter' both ignore the --product="Value" option.

Main/VirtualBox.xidl: Improve the IUSBDeviceFilter wording to make
things clearer in the 'VirtualBox Programming Guide and Reference Guide'
aka SDKRef.pdf.

HostDrivers/VBoxUSB/testcase/tstUSBFilter: Include a variety of USB
device filter entries which include the 'int:' prefix to fully exercise
the interval filter parsing code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.3 KB
Line 
1/* $Id: VBoxManageUSB.cpp 100772 2023-08-01 17:34:48Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/Guid.h>
31#include <VBox/com/array.h>
32#include <VBox/com/ErrorInfo.h>
33#include <VBox/com/errorprint.h>
34#include <VBox/com/VirtualBox.h>
35
36#include "VBoxManage.h"
37
38#include <iprt/asm.h>
39
40using namespace com;
41
42DECLARE_TRANSLATION_CONTEXT(Usb);
43
44/**
45 * Quick IUSBDevice implementation for detaching / attaching
46 * devices to the USB Controller.
47 */
48class MyUSBDevice : public IUSBDevice
49{
50public:
51 // public initializer/uninitializer for internal purposes only
52 MyUSBDevice(uint16_t a_u16VendorId, uint16_t a_u16ProductId, uint16_t a_bcdRevision, uint64_t a_u64SerialHash, const char *a_pszComment)
53 : m_usVendorId(a_u16VendorId), m_usProductId(a_u16ProductId),
54 m_bcdRevision(a_bcdRevision), m_u64SerialHash(a_u64SerialHash),
55 m_bstrComment(a_pszComment),
56 m_cRefs(0)
57 {
58 }
59 virtual ~MyUSBDevice() {}
60
61 STDMETHOD_(ULONG, AddRef)(void)
62 {
63 return ASMAtomicIncU32(&m_cRefs);
64 }
65 STDMETHOD_(ULONG, Release)(void)
66 {
67 ULONG cRefs = ASMAtomicDecU32(&m_cRefs);
68 if (!cRefs)
69 delete this;
70 return cRefs;
71 }
72 STDMETHOD(QueryInterface)(const IID &iid, void **ppvObject)
73 {
74 Guid guid(iid);
75 if (guid == Guid(COM_IIDOF(IUnknown)))
76 *ppvObject = (IUnknown *)this;
77#ifdef RT_OS_WINDOWS
78 else if (guid == Guid(COM_IIDOF(IDispatch)))
79 *ppvObject = (IDispatch *)this;
80#endif
81 else if (guid == Guid(COM_IIDOF(IUSBDevice)))
82 *ppvObject = (IUSBDevice *)this;
83 else
84 return E_NOINTERFACE;
85 AddRef();
86 return S_OK;
87 }
88
89 STDMETHOD(COMGETTER(Id))(OUT_GUID a_pId) { NOREF(a_pId); return E_NOTIMPL; }
90 STDMETHOD(COMGETTER(VendorId))(USHORT *a_pusVendorId) { *a_pusVendorId = m_usVendorId; return S_OK; }
91 STDMETHOD(COMGETTER(ProductId))(USHORT *a_pusProductId) { *a_pusProductId = m_usProductId; return S_OK; }
92 STDMETHOD(COMGETTER(Revision))(USHORT *a_pusRevision) { *a_pusRevision = m_bcdRevision; return S_OK; }
93 STDMETHOD(COMGETTER(SerialHash))(ULONG64 *a_pullSerialHash) { *a_pullSerialHash = m_u64SerialHash; return S_OK; }
94 STDMETHOD(COMGETTER(Manufacturer))(BSTR *a_pManufacturer) { NOREF(a_pManufacturer); return E_NOTIMPL; }
95 STDMETHOD(COMGETTER(Product))(BSTR *a_pProduct) { NOREF(a_pProduct); return E_NOTIMPL; }
96 STDMETHOD(COMGETTER(SerialNumber))(BSTR *a_pSerialNumber) { NOREF(a_pSerialNumber); return E_NOTIMPL; }
97 STDMETHOD(COMGETTER(Address))(BSTR *a_pAddress) { NOREF(a_pAddress); return E_NOTIMPL; }
98
99private:
100 /** The vendor id of this USB device. */
101 USHORT m_usVendorId;
102 /** The product id of this USB device. */
103 USHORT m_usProductId;
104 /** The product revision number of this USB device.
105 * (high byte = integer; low byte = decimal) */
106 USHORT m_bcdRevision;
107 /** The USB serial hash of the device. */
108 uint64_t m_u64SerialHash;
109 /** The user comment string. */
110 Bstr m_bstrComment;
111 /** Reference counter. */
112 uint32_t volatile m_cRefs;
113};
114
115
116// types
117///////////////////////////////////////////////////////////////////////////////
118
119template <typename T>
120class Nullable
121{
122public:
123
124 Nullable() : mIsNull(true) {}
125 Nullable(const T &aValue, bool aIsNull = false)
126 : mIsNull(aIsNull), mValue(aValue) {}
127
128 bool isNull() const { return mIsNull; };
129 void setNull(bool aIsNull = true) { mIsNull = aIsNull; }
130
131 operator const T&() const { return mValue; }
132
133 Nullable &operator= (const T &aValue)
134 {
135 mValue = aValue;
136 mIsNull = false;
137 return *this;
138 }
139
140private:
141
142 bool mIsNull;
143 T mValue;
144};
145
146/** helper structure to encapsulate USB filter manipulation commands */
147struct USBFilterCmd
148{
149 struct USBFilter
150 {
151 USBFilter()
152 : mAction(USBDeviceFilterAction_Null)
153 {}
154
155 Bstr mName;
156 Nullable <bool> mActive;
157 Bstr mVendorId;
158 Bstr mProductId;
159 Bstr mRevision;
160 Bstr mManufacturer;
161 Bstr mProduct;
162 Bstr mPort;
163 Bstr mRemote;
164 Bstr mSerialNumber;
165 Nullable <ULONG> mMaskedInterfaces;
166 USBDeviceFilterAction_T mAction;
167 };
168
169 enum Action { Invalid, Add, Modify, Remove };
170
171 USBFilterCmd() : mAction(Invalid), mIndex(0), mGlobal(false) {}
172
173 Action mAction;
174 uint32_t mIndex;
175 /** flag whether the command target is a global filter */
176 bool mGlobal;
177 /** machine this command is targeted at (null for global filters) */
178 ComPtr<IMachine> mMachine;
179 USBFilter mFilter;
180};
181
182RTEXITCODE handleUSBFilter(HandlerArg *a)
183{
184 HRESULT hrc = S_OK;
185 USBFilterCmd cmd;
186
187 /* at least: 0: command, 1: index, 2: --target, 3: <target value> */
188 if (a->argc < 4)
189 return errorSyntax(Usb::tr("Not enough parameters"));
190
191 /* which command? */
192 cmd.mAction = USBFilterCmd::Invalid;
193 if (!strcmp(a->argv[0], "add"))
194 {
195 cmd.mAction = USBFilterCmd::Add;
196 setCurrentSubcommand(HELP_SCOPE_USBFILTER_ADD);
197 }
198 else if (!strcmp(a->argv[0], "modify"))
199 {
200 cmd.mAction = USBFilterCmd::Modify;
201 setCurrentSubcommand(HELP_SCOPE_USBFILTER_MODIFY);
202 }
203 else if (!strcmp(a->argv[0], "remove"))
204 {
205 cmd.mAction = USBFilterCmd::Remove;
206 setCurrentSubcommand(HELP_SCOPE_USBFILTER_REMOVE);
207 }
208
209 if (cmd.mAction == USBFilterCmd::Invalid)
210 return errorSyntax(Usb::tr("Invalid parameter '%s'"), a->argv[0]);
211
212 /* which index? */
213 if (VINF_SUCCESS != RTStrToUInt32Full(a->argv[1], 10, &cmd.mIndex))
214 return errorSyntax(Usb::tr("Invalid index '%s'"), a->argv[1]);
215
216 switch (cmd.mAction)
217 {
218 case USBFilterCmd::Add:
219 case USBFilterCmd::Modify:
220 {
221 /* at least: 0: command, 1: index, 2: --target, 3: <target value>, 4: --name, 5: <name value> */
222 if (a->argc < 6)
223 {
224 if (cmd.mAction == USBFilterCmd::Add)
225 return errorSyntax(Usb::tr("Not enough parameters"));
226
227 return errorSyntax(Usb::tr("Not enough parameters"));
228 }
229
230 // set Active to true by default
231 // (assuming that the user sets up all necessary attributes
232 // at once and wants the filter to be active immediately)
233 if (cmd.mAction == USBFilterCmd::Add)
234 cmd.mFilter.mActive = true;
235
236 for (int i = 2; i < a->argc; i++)
237 {
238 if ( !strcmp(a->argv[i], "--target")
239 || !strcmp(a->argv[i], "-target"))
240 {
241 if (a->argc <= i + 1 || !*a->argv[i+1])
242 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
243 i++;
244 if (!strcmp(a->argv[i], "global"))
245 cmd.mGlobal = true;
246 else
247 {
248 /* assume it's a UUID of a machine */
249 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[i]).raw(),
250 cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
251 }
252 }
253 else if ( !strcmp(a->argv[i], "--name")
254 || !strcmp(a->argv[i], "-name"))
255 {
256 if (a->argc <= i + 1 || !*a->argv[i+1])
257 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
258 i++;
259 cmd.mFilter.mName = a->argv[i];
260 }
261 else if ( !strcmp(a->argv[i], "--active")
262 || !strcmp(a->argv[i], "-active"))
263 {
264 if (a->argc <= i + 1)
265 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
266 i++;
267 if (!strcmp(a->argv[i], "yes"))
268 cmd.mFilter.mActive = true;
269 else if (!strcmp(a->argv[i], "no"))
270 cmd.mFilter.mActive = false;
271 else
272 return errorArgument(Usb::tr("Invalid --active argument '%s'"), a->argv[i]);
273 }
274 else if ( !strcmp(a->argv[i], "--vendorid")
275 || !strcmp(a->argv[i], "-vendorid"))
276 {
277 if (a->argc <= i + 1)
278 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
279 i++;
280 cmd.mFilter.mVendorId = a->argv[i];
281 }
282 else if ( !strcmp(a->argv[i], "--productid")
283 || !strcmp(a->argv[i], "-productid"))
284 {
285 if (a->argc <= i + 1)
286 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
287 i++;
288 cmd.mFilter.mProductId = a->argv[i];
289 }
290 else if ( !strcmp(a->argv[i], "--revision")
291 || !strcmp(a->argv[i], "-revision"))
292 {
293 if (a->argc <= i + 1)
294 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
295 i++;
296 cmd.mFilter.mRevision = a->argv[i];
297 }
298 else if ( !strcmp(a->argv[i], "--manufacturer")
299 || !strcmp(a->argv[i], "-manufacturer"))
300 {
301 if (a->argc <= i + 1)
302 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
303 i++;
304 cmd.mFilter.mManufacturer = a->argv[i];
305 }
306 else if (!strcmp(a->argv[i], "--port"))
307 {
308 if (a->argc <= i + 1)
309 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
310 i++;
311 cmd.mFilter.mPort = a->argv[i];
312 }
313 else if ( !strcmp(a->argv[i], "--product")
314 || !strcmp(a->argv[i], "-product"))
315 {
316 if (a->argc <= i + 1)
317 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
318 i++;
319 cmd.mFilter.mProduct = a->argv[i];
320 }
321 else if ( !strcmp(a->argv[i], "--remote")
322 || !strcmp(a->argv[i], "-remote"))
323 {
324 if (a->argc <= i + 1)
325 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
326 i++;
327 cmd.mFilter.mRemote = a->argv[i];
328 }
329 else if ( !strcmp(a->argv[i], "--serialnumber")
330 || !strcmp(a->argv[i], "-serialnumber"))
331 {
332 if (a->argc <= i + 1)
333 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
334 i++;
335 cmd.mFilter.mSerialNumber = a->argv[i];
336 }
337 else if ( !strcmp(a->argv[i], "--maskedinterfaces")
338 || !strcmp(a->argv[i], "-maskedinterfaces"))
339 {
340 if (a->argc <= i + 1)
341 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
342 i++;
343 uint32_t u32;
344 int vrc = RTStrToUInt32Full(a->argv[i], 0, &u32);
345 if (RT_FAILURE(vrc))
346 return errorArgument(Usb::tr("Failed to convert the --maskedinterfaces value '%s' to a number, vrc=%Rrc"),
347 a->argv[i], vrc);
348 cmd.mFilter.mMaskedInterfaces = u32;
349 }
350 else if ( !strcmp(a->argv[i], "--action")
351 || !strcmp(a->argv[i], "-action"))
352 {
353 if (a->argc <= i + 1)
354 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
355 i++;
356 if (!strcmp(a->argv[i], "ignore"))
357 cmd.mFilter.mAction = USBDeviceFilterAction_Ignore;
358 else if (!strcmp(a->argv[i], "hold"))
359 cmd.mFilter.mAction = USBDeviceFilterAction_Hold;
360 else
361 return errorArgument(Usb::tr("Invalid USB filter action '%s'"), a->argv[i]);
362 }
363 else
364 return errorSyntax(Usb::tr("Unknown option '%s'"), a->argv[i]);
365 }
366
367 if (cmd.mAction == USBFilterCmd::Add)
368 {
369 // mandatory/forbidden options
370 if ( cmd.mFilter.mName.isEmpty()
371 ||
372 ( cmd.mGlobal
373 && cmd.mFilter.mAction == USBDeviceFilterAction_Null
374 )
375 || ( !cmd.mGlobal
376 && !cmd.mMachine)
377 || ( cmd.mGlobal
378 && !cmd.mFilter.mRemote.isEmpty())
379 )
380 {
381 return errorSyntax(Usb::tr("Mandatory options not supplied"));
382 }
383 }
384 break;
385 }
386
387 case USBFilterCmd::Remove:
388 {
389 /* at least: 0: command, 1: index, 2: --target, 3: <target value> */
390 if (a->argc < 4)
391 return errorSyntax(Usb::tr("Not enough parameters"));
392
393 for (int i = 2; i < a->argc; i++)
394 {
395 if ( !strcmp(a->argv[i], "--target")
396 || !strcmp(a->argv[i], "-target"))
397 {
398 if (a->argc <= i + 1 || !*a->argv[i+1])
399 return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
400 i++;
401 if (!strcmp(a->argv[i], "global"))
402 cmd.mGlobal = true;
403 else
404 {
405 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[i]).raw(),
406 cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
407 }
408 }
409 }
410
411 // mandatory options
412 if (!cmd.mGlobal && !cmd.mMachine)
413 return errorSyntax(Usb::tr("Mandatory options not supplied"));
414
415 break;
416 }
417
418 default: break;
419 }
420
421 USBFilterCmd::USBFilter &f = cmd.mFilter;
422
423 ComPtr<IHost> host;
424 ComPtr<IUSBDeviceFilters> flts;
425 if (cmd.mGlobal)
426 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
427 else
428 {
429 /* open a session for the VM */
430 CHECK_ERROR_RET(cmd.mMachine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
431 /* get the mutable session machine */
432 a->session->COMGETTER(Machine)(cmd.mMachine.asOutParam());
433 /* and get the USB device filters */
434 CHECK_ERROR_RET(cmd.mMachine, COMGETTER(USBDeviceFilters)(flts.asOutParam()), RTEXITCODE_FAILURE);
435 }
436
437 switch (cmd.mAction)
438 {
439 case USBFilterCmd::Add:
440 {
441 if (cmd.mGlobal)
442 {
443 ComPtr<IHostUSBDeviceFilter> flt;
444 CHECK_ERROR_BREAK(host, CreateUSBDeviceFilter(f.mName.raw(),
445 flt.asOutParam()));
446
447 if (!f.mActive.isNull())
448 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
449 if (!f.mVendorId.isEmpty())
450 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
451 if (!f.mProductId.isEmpty())
452 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
453 if (!f.mRevision.isEmpty())
454 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
455 if (!f.mManufacturer.isEmpty())
456 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
457 if (!f.mProduct.isEmpty())
458 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
459 if (!f.mPort.isEmpty())
460 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
461 if (!f.mSerialNumber.isEmpty())
462 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
463 if (!f.mMaskedInterfaces.isNull())
464 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
465
466 if (f.mAction != USBDeviceFilterAction_Null)
467 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
468
469 CHECK_ERROR_BREAK(host, InsertUSBDeviceFilter(cmd.mIndex, flt));
470 }
471 else
472 {
473 ComPtr<IUSBDeviceFilter> flt;
474 CHECK_ERROR_BREAK(flts, CreateDeviceFilter(f.mName.raw(),
475 flt.asOutParam()));
476
477 if (!f.mActive.isNull())
478 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
479 if (!f.mVendorId.isEmpty())
480 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
481 if (!f.mProductId.isEmpty())
482 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
483 if (!f.mRevision.isEmpty())
484 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
485 if (!f.mManufacturer.isEmpty())
486 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
487 if (!f.mProduct.isEmpty())
488 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
489 if (!f.mPort.isEmpty())
490 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
491 if (!f.mRemote.isEmpty())
492 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
493 if (!f.mSerialNumber.isEmpty())
494 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
495 if (!f.mMaskedInterfaces.isNull())
496 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
497
498 CHECK_ERROR_BREAK(flts, InsertDeviceFilter(cmd.mIndex, flt));
499 }
500 break;
501 }
502 case USBFilterCmd::Modify:
503 {
504 if (cmd.mGlobal)
505 {
506 SafeIfaceArray <IHostUSBDeviceFilter> coll;
507 CHECK_ERROR_BREAK(host, COMGETTER(USBDeviceFilters)(ComSafeArrayAsOutParam(coll)));
508
509 ComPtr<IHostUSBDeviceFilter> flt = coll[cmd.mIndex];
510
511 if (!f.mName.isEmpty())
512 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
513 if (!f.mActive.isNull())
514 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
515 if (!f.mVendorId.isEmpty())
516 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
517 if (!f.mProductId.isEmpty())
518 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
519 if (!f.mRevision.isEmpty())
520 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
521 if (!f.mManufacturer.isEmpty())
522 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
523 if (!f.mProduct.isEmpty())
524 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
525 if (!f.mPort.isEmpty())
526 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
527 if (!f.mSerialNumber.isEmpty())
528 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
529 if (!f.mMaskedInterfaces.isNull())
530 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
531
532 if (f.mAction != USBDeviceFilterAction_Null)
533 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
534 }
535 else
536 {
537 SafeIfaceArray <IUSBDeviceFilter> coll;
538 CHECK_ERROR_BREAK(flts, COMGETTER(DeviceFilters)(ComSafeArrayAsOutParam(coll)));
539
540 ComPtr<IUSBDeviceFilter> flt = coll[cmd.mIndex];
541
542 if (!f.mName.isEmpty())
543 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
544 if (!f.mActive.isNull())
545 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
546 if (!f.mVendorId.isEmpty())
547 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
548 if (!f.mProductId.isEmpty())
549 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
550 if (!f.mRevision.isEmpty())
551 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
552 if (!f.mManufacturer.isEmpty())
553 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
554 if (!f.mProduct.isEmpty())
555 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
556 if (!f.mPort.isEmpty())
557 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
558 if (!f.mRemote.isEmpty())
559 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
560 if (!f.mSerialNumber.isEmpty())
561 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
562 if (!f.mMaskedInterfaces.isNull())
563 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
564 }
565 break;
566 }
567 case USBFilterCmd::Remove:
568 {
569 if (cmd.mGlobal)
570 {
571 ComPtr<IHostUSBDeviceFilter> flt;
572 CHECK_ERROR_BREAK(host, RemoveUSBDeviceFilter(cmd.mIndex));
573 }
574 else
575 {
576 ComPtr<IUSBDeviceFilter> flt;
577 CHECK_ERROR_BREAK(flts, RemoveDeviceFilter(cmd.mIndex, flt.asOutParam()));
578 }
579 break;
580 }
581 default:
582 break;
583 }
584
585 if (cmd.mMachine)
586 {
587 if (SUCCEEDED(hrc))
588 {
589 /* commit the session */
590 CHECK_ERROR(cmd.mMachine, SaveSettings());
591 }
592 /* close the session */
593 a->session->UnlockMachine();
594 }
595
596 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
597}
598
599RTEXITCODE handleUSBDevSource(HandlerArg *a)
600{
601 HRESULT hrc = S_OK;
602
603 /* at least: 0: command, 1: source id */
604 if (a->argc < 2)
605 return errorSyntax(Usb::tr("Not enough parameters"));
606
607 ComPtr<IHost> host;
608 if (!strcmp(a->argv[0], "add"))
609 {
610 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_ADD);
611
612 Bstr strBackend;
613 Bstr strAddress;
614 if (a->argc != 6)
615 return errorSyntax(Usb::tr("Invalid number of parameters"));
616
617 for (int i = 2; i < a->argc; i++)
618 {
619 if (!strcmp(a->argv[i], "--backend"))
620 {
621 i++;
622 strBackend = a->argv[i];
623 }
624 else if (!strcmp(a->argv[i], "--address"))
625 {
626 i++;
627 strAddress = a->argv[i];
628 }
629 else
630 return errorSyntax(Usb::tr("Parameter \"%s\" is invalid"), a->argv[i]);
631 }
632
633 SafeArray<BSTR> usbSourcePropNames;
634 SafeArray<BSTR> usbSourcePropValues;
635
636 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
637 CHECK_ERROR_RET(host, AddUSBDeviceSource(strBackend.raw(), Bstr(a->argv[1]).raw(), strAddress.raw(),
638 ComSafeArrayAsInParam(usbSourcePropNames), ComSafeArrayAsInParam(usbSourcePropValues)),
639 RTEXITCODE_FAILURE);
640 }
641 else if (!strcmp(a->argv[0], "remove"))
642 {
643 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_REMOVE);
644 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
645 CHECK_ERROR_RET(host, RemoveUSBDeviceSource(Bstr(a->argv[1]).raw()), RTEXITCODE_FAILURE);
646 }
647
648 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
649}
650
651/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use