VirtualBox

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

Last change on this file since 100684 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.8 KB
Line 
1/* $Id: VBoxManageUSB.cpp 98103 2023-01-17 14:15:46Z 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.mPort.isEmpty())
458 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
459 if (!f.mSerialNumber.isEmpty())
460 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
461 if (!f.mMaskedInterfaces.isNull())
462 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
463
464 if (f.mAction != USBDeviceFilterAction_Null)
465 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
466
467 CHECK_ERROR_BREAK(host, InsertUSBDeviceFilter(cmd.mIndex, flt));
468 }
469 else
470 {
471 ComPtr<IUSBDeviceFilter> flt;
472 CHECK_ERROR_BREAK(flts, CreateDeviceFilter(f.mName.raw(),
473 flt.asOutParam()));
474
475 if (!f.mActive.isNull())
476 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
477 if (!f.mVendorId.isEmpty())
478 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
479 if (!f.mProductId.isEmpty())
480 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
481 if (!f.mRevision.isEmpty())
482 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
483 if (!f.mManufacturer.isEmpty())
484 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
485 if (!f.mPort.isEmpty())
486 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
487 if (!f.mRemote.isEmpty())
488 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
489 if (!f.mSerialNumber.isEmpty())
490 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
491 if (!f.mMaskedInterfaces.isNull())
492 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
493
494 CHECK_ERROR_BREAK(flts, InsertDeviceFilter(cmd.mIndex, flt));
495 }
496 break;
497 }
498 case USBFilterCmd::Modify:
499 {
500 if (cmd.mGlobal)
501 {
502 SafeIfaceArray <IHostUSBDeviceFilter> coll;
503 CHECK_ERROR_BREAK(host, COMGETTER(USBDeviceFilters)(ComSafeArrayAsOutParam(coll)));
504
505 ComPtr<IHostUSBDeviceFilter> flt = coll[cmd.mIndex];
506
507 if (!f.mName.isEmpty())
508 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
509 if (!f.mActive.isNull())
510 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
511 if (!f.mVendorId.isEmpty())
512 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
513 if (!f.mProductId.isEmpty())
514 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
515 if (!f.mRevision.isEmpty())
516 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
517 if (!f.mManufacturer.isEmpty())
518 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
519 if (!f.mPort.isEmpty())
520 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
521 if (!f.mSerialNumber.isEmpty())
522 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
523 if (!f.mMaskedInterfaces.isNull())
524 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
525
526 if (f.mAction != USBDeviceFilterAction_Null)
527 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
528 }
529 else
530 {
531 SafeIfaceArray <IUSBDeviceFilter> coll;
532 CHECK_ERROR_BREAK(flts, COMGETTER(DeviceFilters)(ComSafeArrayAsOutParam(coll)));
533
534 ComPtr<IUSBDeviceFilter> flt = coll[cmd.mIndex];
535
536 if (!f.mName.isEmpty())
537 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
538 if (!f.mActive.isNull())
539 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
540 if (!f.mVendorId.isEmpty())
541 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
542 if (!f.mProductId.isEmpty())
543 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
544 if (!f.mRevision.isEmpty())
545 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
546 if (!f.mManufacturer.isEmpty())
547 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
548 if (!f.mPort.isEmpty())
549 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
550 if (!f.mRemote.isEmpty())
551 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
552 if (!f.mSerialNumber.isEmpty())
553 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
554 if (!f.mMaskedInterfaces.isNull())
555 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
556 }
557 break;
558 }
559 case USBFilterCmd::Remove:
560 {
561 if (cmd.mGlobal)
562 {
563 ComPtr<IHostUSBDeviceFilter> flt;
564 CHECK_ERROR_BREAK(host, RemoveUSBDeviceFilter(cmd.mIndex));
565 }
566 else
567 {
568 ComPtr<IUSBDeviceFilter> flt;
569 CHECK_ERROR_BREAK(flts, RemoveDeviceFilter(cmd.mIndex, flt.asOutParam()));
570 }
571 break;
572 }
573 default:
574 break;
575 }
576
577 if (cmd.mMachine)
578 {
579 if (SUCCEEDED(hrc))
580 {
581 /* commit the session */
582 CHECK_ERROR(cmd.mMachine, SaveSettings());
583 }
584 /* close the session */
585 a->session->UnlockMachine();
586 }
587
588 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
589}
590
591RTEXITCODE handleUSBDevSource(HandlerArg *a)
592{
593 HRESULT hrc = S_OK;
594
595 /* at least: 0: command, 1: source id */
596 if (a->argc < 2)
597 return errorSyntax(Usb::tr("Not enough parameters"));
598
599 ComPtr<IHost> host;
600 if (!strcmp(a->argv[0], "add"))
601 {
602 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_ADD);
603
604 Bstr strBackend;
605 Bstr strAddress;
606 if (a->argc != 6)
607 return errorSyntax(Usb::tr("Invalid number of parameters"));
608
609 for (int i = 2; i < a->argc; i++)
610 {
611 if (!strcmp(a->argv[i], "--backend"))
612 {
613 i++;
614 strBackend = a->argv[i];
615 }
616 else if (!strcmp(a->argv[i], "--address"))
617 {
618 i++;
619 strAddress = a->argv[i];
620 }
621 else
622 return errorSyntax(Usb::tr("Parameter \"%s\" is invalid"), a->argv[i]);
623 }
624
625 SafeArray<BSTR> usbSourcePropNames;
626 SafeArray<BSTR> usbSourcePropValues;
627
628 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
629 CHECK_ERROR_RET(host, AddUSBDeviceSource(strBackend.raw(), Bstr(a->argv[1]).raw(), strAddress.raw(),
630 ComSafeArrayAsInParam(usbSourcePropNames), ComSafeArrayAsInParam(usbSourcePropValues)),
631 RTEXITCODE_FAILURE);
632 }
633 else if (!strcmp(a->argv[0], "remove"))
634 {
635 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_REMOVE);
636 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
637 CHECK_ERROR_RET(host, RemoveUSBDeviceSource(Bstr(a->argv[1]).raw()), RTEXITCODE_FAILURE);
638 }
639
640 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
641}
642
643/* 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