VirtualBox

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

Last change on this file was 100785, checked in by vboxsync, 12 months ago

Frontends/VBoxManage: Update 'VBoxManage usbfilter ...' to utilize
RTGetOpt() for command option parsing. bugref:10452

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.4 KB
Line 
1/* $Id: VBoxManageUSB.cpp 100785 2023-08-03 13:02:17Z 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 /* which command? */
188 cmd.mAction = USBFilterCmd::Invalid;
189 if (!strcmp(a->argv[0], "add"))
190 {
191 cmd.mAction = USBFilterCmd::Add;
192 setCurrentSubcommand(HELP_SCOPE_USBFILTER_ADD);
193 }
194 else if (!strcmp(a->argv[0], "modify"))
195 {
196 cmd.mAction = USBFilterCmd::Modify;
197 setCurrentSubcommand(HELP_SCOPE_USBFILTER_MODIFY);
198 }
199 else if (!strcmp(a->argv[0], "remove"))
200 {
201 cmd.mAction = USBFilterCmd::Remove;
202 setCurrentSubcommand(HELP_SCOPE_USBFILTER_REMOVE);
203 }
204
205 if (cmd.mAction == USBFilterCmd::Invalid)
206 return errorUnknownSubcommand(a->argv[0]);
207
208 /* which index? */
209 if (VINF_SUCCESS != RTStrToUInt32Full(a->argv[1], 10, &cmd.mIndex))
210 return errorSyntax(Usb::tr("Invalid index '%s'"), a->argv[1]);
211
212 if (cmd.mAction == USBFilterCmd::Add || cmd.mAction == USBFilterCmd::Modify)
213 {
214 // set Active to true by default
215 // (assuming that the user sets up all necessary attributes
216 // at once and wants the filter to be active immediately)
217 if (cmd.mAction == USBFilterCmd::Add)
218 cmd.mFilter.mActive = true;
219
220 RTGETOPTSTATE GetState;
221 RTGETOPTUNION ValueUnion;
222 static const RTGETOPTDEF s_aOptions[] =
223 {
224 { "--target", 't', RTGETOPT_REQ_STRING },
225 { "--name", 'n', RTGETOPT_REQ_STRING },
226 { "--active", 'a', RTGETOPT_REQ_STRING },
227 { "--vendorid", 'v', RTGETOPT_REQ_STRING },
228 { "--productid", 'p', RTGETOPT_REQ_STRING },
229 { "--revision", 'r', RTGETOPT_REQ_STRING },
230 { "--manufacturer", 'm', RTGETOPT_REQ_STRING },
231 { "--product", 'P', RTGETOPT_REQ_STRING },
232 { "--serialnumber", 's', RTGETOPT_REQ_STRING },
233 { "--port", 'o', RTGETOPT_REQ_STRING },
234 { "--remote", 'R', RTGETOPT_REQ_STRING },
235 { "--maskedinterfaces", 'M', RTGETOPT_REQ_UINT32 },
236 { "--action", 'A', RTGETOPT_REQ_STRING }
237 };
238
239 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 2, 0 /*fFlags*/);
240 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
241
242 while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
243 {
244 switch (vrc)
245 {
246 case 't': // --target
247 if (!strcmp(ValueUnion.psz, "global"))
248 cmd.mGlobal = true;
249 else
250 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(ValueUnion.psz).raw(),
251 cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
252 break;
253 case 'n': // --name
254 cmd.mFilter.mName = ValueUnion.psz;
255 break;
256 case 'a': // --active
257 if (!strcmp(ValueUnion.psz, "yes"))
258 cmd.mFilter.mActive = true;
259 else if (!strcmp(ValueUnion.psz, "no"))
260 cmd.mFilter.mActive = false;
261 else
262 return errorArgument(Usb::tr("Invalid --active argument '%s'"), ValueUnion.psz);
263 break;
264 case 'v': // --vendorid
265 cmd.mFilter.mVendorId = ValueUnion.psz;
266 break;
267 case 'p': // --productid
268 cmd.mFilter.mProductId = ValueUnion.psz;
269 break;
270 case 'r': // --revision
271 cmd.mFilter.mRevision = ValueUnion.psz;
272 break;
273 case 'm': // --manufacturer
274 cmd.mFilter.mManufacturer = ValueUnion.psz;
275 break;
276 case 'P': // --product
277 cmd.mFilter.mProduct = ValueUnion.psz;
278 break;
279 case 's': // --serialnumber
280 cmd.mFilter.mSerialNumber = ValueUnion.psz;
281 break;
282 case 'o': // --port
283 cmd.mFilter.mPort = ValueUnion.psz;
284 break;
285 case 'R': // --remote
286 cmd.mFilter.mRemote = ValueUnion.psz;
287 break;
288 case 'M': // --maskedinterfaces
289 cmd.mFilter.mMaskedInterfaces = ValueUnion.u32;
290 break;
291 case 'A': // --action
292 if (!strcmp(ValueUnion.psz, "ignore"))
293 cmd.mFilter.mAction = USBDeviceFilterAction_Ignore;
294 else if (!strcmp(ValueUnion.psz, "hold"))
295 cmd.mFilter.mAction = USBDeviceFilterAction_Hold;
296 else
297 return errorArgument(Usb::tr("Invalid USB filter action '%s'"), ValueUnion.psz);
298 break;
299 default:
300 return errorGetOpt(vrc, &ValueUnion);
301 }
302 }
303
304 // mandatory/forbidden options
305 if (!cmd.mGlobal && !cmd.mMachine)
306 return errorSyntax(Usb::tr("Missing required option: --target"));
307
308 if (cmd.mAction == USBFilterCmd::Add)
309 {
310 if (cmd.mFilter.mName.isEmpty())
311 return errorSyntax(Usb::tr("Missing required option: --name"));
312
313 if (cmd.mGlobal && cmd.mFilter.mAction == USBDeviceFilterAction_Null)
314 return errorSyntax(Usb::tr("Missing required option: --action"));
315
316 if (cmd.mGlobal && !cmd.mFilter.mRemote.isEmpty())
317 return errorSyntax(Usb::tr("Option --remote applies to VM filters only (--target=<uuid|vmname>)"));
318 }
319 }
320 else if (cmd.mAction == USBFilterCmd::Remove)
321 {
322 RTGETOPTSTATE GetState;
323 RTGETOPTUNION ValueUnion;
324 static const RTGETOPTDEF s_aOptions[] =
325 {
326 { "--target", 't', RTGETOPT_REQ_STRING }
327 };
328 int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 2, 0 /*fFlags*/);
329 AssertRCReturn(vrc, RTEXITCODE_FAILURE);
330
331 while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
332 {
333 switch (vrc)
334 {
335 case 't': // --target
336 if (!strcmp(ValueUnion.psz, "global"))
337 cmd.mGlobal = true;
338 else
339 CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(ValueUnion.psz).raw(),
340 cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
341 break;
342 default:
343 return errorGetOpt(vrc, &ValueUnion);
344 }
345 }
346 // mandatory options
347 if (!cmd.mGlobal && !cmd.mMachine)
348 return errorSyntax(Usb::tr("Missing required option: --target"));
349 }
350
351 USBFilterCmd::USBFilter &f = cmd.mFilter;
352
353 ComPtr<IHost> host;
354 ComPtr<IUSBDeviceFilters> flts;
355 if (cmd.mGlobal)
356 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
357 else
358 {
359 /* open a session for the VM */
360 CHECK_ERROR_RET(cmd.mMachine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
361 /* get the mutable session machine */
362 a->session->COMGETTER(Machine)(cmd.mMachine.asOutParam());
363 /* and get the USB device filters */
364 CHECK_ERROR_RET(cmd.mMachine, COMGETTER(USBDeviceFilters)(flts.asOutParam()), RTEXITCODE_FAILURE);
365 }
366
367 switch (cmd.mAction)
368 {
369 case USBFilterCmd::Add:
370 {
371 if (cmd.mGlobal)
372 {
373 ComPtr<IHostUSBDeviceFilter> flt;
374 CHECK_ERROR_BREAK(host, CreateUSBDeviceFilter(f.mName.raw(),
375 flt.asOutParam()));
376
377 if (!f.mActive.isNull())
378 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
379 if (!f.mVendorId.isEmpty())
380 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
381 if (!f.mProductId.isEmpty())
382 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
383 if (!f.mRevision.isEmpty())
384 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
385 if (!f.mManufacturer.isEmpty())
386 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
387 if (!f.mProduct.isEmpty())
388 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
389 if (!f.mPort.isEmpty())
390 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
391 if (!f.mSerialNumber.isEmpty())
392 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
393 if (!f.mMaskedInterfaces.isNull())
394 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
395
396 if (f.mAction != USBDeviceFilterAction_Null)
397 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
398
399 CHECK_ERROR_BREAK(host, InsertUSBDeviceFilter(cmd.mIndex, flt));
400 }
401 else
402 {
403 ComPtr<IUSBDeviceFilter> flt;
404 CHECK_ERROR_BREAK(flts, CreateDeviceFilter(f.mName.raw(),
405 flt.asOutParam()));
406
407 if (!f.mActive.isNull())
408 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
409 if (!f.mVendorId.isEmpty())
410 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
411 if (!f.mProductId.isEmpty())
412 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
413 if (!f.mRevision.isEmpty())
414 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
415 if (!f.mManufacturer.isEmpty())
416 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
417 if (!f.mProduct.isEmpty())
418 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
419 if (!f.mPort.isEmpty())
420 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
421 if (!f.mRemote.isEmpty())
422 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
423 if (!f.mSerialNumber.isEmpty())
424 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
425 if (!f.mMaskedInterfaces.isNull())
426 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
427
428 CHECK_ERROR_BREAK(flts, InsertDeviceFilter(cmd.mIndex, flt));
429 }
430 break;
431 }
432 case USBFilterCmd::Modify:
433 {
434 if (cmd.mGlobal)
435 {
436 SafeIfaceArray <IHostUSBDeviceFilter> coll;
437 CHECK_ERROR_BREAK(host, COMGETTER(USBDeviceFilters)(ComSafeArrayAsOutParam(coll)));
438
439 ComPtr<IHostUSBDeviceFilter> flt = coll[cmd.mIndex];
440
441 if (!f.mName.isEmpty())
442 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
443 if (!f.mActive.isNull())
444 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
445 if (!f.mVendorId.isEmpty())
446 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
447 if (!f.mProductId.isEmpty())
448 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
449 if (!f.mRevision.isEmpty())
450 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
451 if (!f.mManufacturer.isEmpty())
452 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
453 if (!f.mProduct.isEmpty())
454 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
455 if (!f.mPort.isEmpty())
456 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
457 if (!f.mSerialNumber.isEmpty())
458 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
459 if (!f.mMaskedInterfaces.isNull())
460 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
461
462 if (f.mAction != USBDeviceFilterAction_Null)
463 CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
464 }
465 else
466 {
467 SafeIfaceArray <IUSBDeviceFilter> coll;
468 CHECK_ERROR_BREAK(flts, COMGETTER(DeviceFilters)(ComSafeArrayAsOutParam(coll)));
469
470 ComPtr<IUSBDeviceFilter> flt = coll[cmd.mIndex];
471
472 if (!f.mName.isEmpty())
473 CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
474 if (!f.mActive.isNull())
475 CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
476 if (!f.mVendorId.isEmpty())
477 CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
478 if (!f.mProductId.isEmpty())
479 CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
480 if (!f.mRevision.isEmpty())
481 CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
482 if (!f.mManufacturer.isEmpty())
483 CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
484 if (!f.mProduct.isEmpty())
485 CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
486 if (!f.mPort.isEmpty())
487 CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
488 if (!f.mRemote.isEmpty())
489 CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
490 if (!f.mSerialNumber.isEmpty())
491 CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
492 if (!f.mMaskedInterfaces.isNull())
493 CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
494 }
495 break;
496 }
497 case USBFilterCmd::Remove:
498 {
499 if (cmd.mGlobal)
500 {
501 ComPtr<IHostUSBDeviceFilter> flt;
502 CHECK_ERROR_BREAK(host, RemoveUSBDeviceFilter(cmd.mIndex));
503 }
504 else
505 {
506 ComPtr<IUSBDeviceFilter> flt;
507 CHECK_ERROR_BREAK(flts, RemoveDeviceFilter(cmd.mIndex, flt.asOutParam()));
508 }
509 break;
510 }
511 default:
512 break;
513 }
514
515 if (cmd.mMachine)
516 {
517 if (SUCCEEDED(hrc))
518 {
519 /* commit the session */
520 CHECK_ERROR(cmd.mMachine, SaveSettings());
521 }
522 /* close the session */
523 a->session->UnlockMachine();
524 }
525
526 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
527}
528
529RTEXITCODE handleUSBDevSource(HandlerArg *a)
530{
531 HRESULT hrc = S_OK;
532
533 /* at least: 0: command, 1: source id */
534 if (a->argc < 2)
535 return errorSyntax(Usb::tr("Not enough parameters"));
536
537 ComPtr<IHost> host;
538 if (!strcmp(a->argv[0], "add"))
539 {
540 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_ADD);
541
542 Bstr strBackend;
543 Bstr strAddress;
544 if (a->argc != 6)
545 return errorSyntax(Usb::tr("Invalid number of parameters"));
546
547 for (int i = 2; i < a->argc; i++)
548 {
549 if (!strcmp(a->argv[i], "--backend"))
550 {
551 i++;
552 strBackend = a->argv[i];
553 }
554 else if (!strcmp(a->argv[i], "--address"))
555 {
556 i++;
557 strAddress = a->argv[i];
558 }
559 else
560 return errorSyntax(Usb::tr("Parameter \"%s\" is invalid"), a->argv[i]);
561 }
562
563 SafeArray<BSTR> usbSourcePropNames;
564 SafeArray<BSTR> usbSourcePropValues;
565
566 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
567 CHECK_ERROR_RET(host, AddUSBDeviceSource(strBackend.raw(), Bstr(a->argv[1]).raw(), strAddress.raw(),
568 ComSafeArrayAsInParam(usbSourcePropNames), ComSafeArrayAsInParam(usbSourcePropValues)),
569 RTEXITCODE_FAILURE);
570 }
571 else if (!strcmp(a->argv[0], "remove"))
572 {
573 setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_REMOVE);
574 CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
575 CHECK_ERROR_RET(host, RemoveUSBDeviceSource(Bstr(a->argv[1]).raw()), RTEXITCODE_FAILURE);
576 }
577
578 return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
579}
580
581/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use