VirtualBox

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

Last change on this file was 107515, checked in by vboxsync, 7 weeks ago

src/VBox/Frontends/VBoxManage/VBoxManageUSB.cpp: Fixed warning found by Parfait (uninitialized attributes). jiraref:VBP-1424

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