1 | /* $Id: VBoxManageUSB.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - VirtualBox's command-line interface.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 |
|
---|
40 | using namespace com;
|
---|
41 |
|
---|
42 | DECLARE_TRANSLATION_CONTEXT(Usb);
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Quick IUSBDevice implementation for detaching / attaching
|
---|
46 | * devices to the USB Controller.
|
---|
47 | */
|
---|
48 | class MyUSBDevice : public IUSBDevice
|
---|
49 | {
|
---|
50 | public:
|
---|
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 |
|
---|
99 | private:
|
---|
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 |
|
---|
119 | template <typename T>
|
---|
120 | class Nullable
|
---|
121 | {
|
---|
122 | public:
|
---|
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 |
|
---|
140 | private:
|
---|
141 |
|
---|
142 | bool mIsNull;
|
---|
143 | T mValue;
|
---|
144 | };
|
---|
145 |
|
---|
146 | /** helper structure to encapsulate USB filter manipulation commands */
|
---|
147 | struct 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 mRemote;
|
---|
163 | Bstr mSerialNumber;
|
---|
164 | Nullable <ULONG> mMaskedInterfaces;
|
---|
165 | USBDeviceFilterAction_T mAction;
|
---|
166 | };
|
---|
167 |
|
---|
168 | enum Action { Invalid, Add, Modify, Remove };
|
---|
169 |
|
---|
170 | USBFilterCmd() : mAction(Invalid), mIndex(0), mGlobal(false) {}
|
---|
171 |
|
---|
172 | Action mAction;
|
---|
173 | uint32_t mIndex;
|
---|
174 | /** flag whether the command target is a global filter */
|
---|
175 | bool mGlobal;
|
---|
176 | /** machine this command is targeted at (null for global filters) */
|
---|
177 | ComPtr<IMachine> mMachine;
|
---|
178 | USBFilter mFilter;
|
---|
179 | };
|
---|
180 |
|
---|
181 | RTEXITCODE handleUSBFilter(HandlerArg *a)
|
---|
182 | {
|
---|
183 | HRESULT hrc = S_OK;
|
---|
184 | USBFilterCmd cmd;
|
---|
185 |
|
---|
186 | /* at least: 0: command, 1: index, 2: --target, 3: <target value> */
|
---|
187 | if (a->argc < 4)
|
---|
188 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
189 |
|
---|
190 | /* which command? */
|
---|
191 | cmd.mAction = USBFilterCmd::Invalid;
|
---|
192 | if (!strcmp(a->argv[0], "add"))
|
---|
193 | {
|
---|
194 | cmd.mAction = USBFilterCmd::Add;
|
---|
195 | setCurrentSubcommand(HELP_SCOPE_USBFILTER_ADD);
|
---|
196 | }
|
---|
197 | else if (!strcmp(a->argv[0], "modify"))
|
---|
198 | {
|
---|
199 | cmd.mAction = USBFilterCmd::Modify;
|
---|
200 | setCurrentSubcommand(HELP_SCOPE_USBFILTER_MODIFY);
|
---|
201 | }
|
---|
202 | else if (!strcmp(a->argv[0], "remove"))
|
---|
203 | {
|
---|
204 | cmd.mAction = USBFilterCmd::Remove;
|
---|
205 | setCurrentSubcommand(HELP_SCOPE_USBFILTER_REMOVE);
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (cmd.mAction == USBFilterCmd::Invalid)
|
---|
209 | return errorSyntax(Usb::tr("Invalid parameter '%s'"), a->argv[0]);
|
---|
210 |
|
---|
211 | /* which index? */
|
---|
212 | if (VINF_SUCCESS != RTStrToUInt32Full(a->argv[1], 10, &cmd.mIndex))
|
---|
213 | return errorSyntax(Usb::tr("Invalid index '%s'"), a->argv[1]);
|
---|
214 |
|
---|
215 | switch (cmd.mAction)
|
---|
216 | {
|
---|
217 | case USBFilterCmd::Add:
|
---|
218 | case USBFilterCmd::Modify:
|
---|
219 | {
|
---|
220 | /* at least: 0: command, 1: index, 2: --target, 3: <target value>, 4: --name, 5: <name value> */
|
---|
221 | if (a->argc < 6)
|
---|
222 | {
|
---|
223 | if (cmd.mAction == USBFilterCmd::Add)
|
---|
224 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
225 |
|
---|
226 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
227 | }
|
---|
228 |
|
---|
229 | // set Active to true by default
|
---|
230 | // (assuming that the user sets up all necessary attributes
|
---|
231 | // at once and wants the filter to be active immediately)
|
---|
232 | if (cmd.mAction == USBFilterCmd::Add)
|
---|
233 | cmd.mFilter.mActive = true;
|
---|
234 |
|
---|
235 | for (int i = 2; i < a->argc; i++)
|
---|
236 | {
|
---|
237 | if ( !strcmp(a->argv[i], "--target")
|
---|
238 | || !strcmp(a->argv[i], "-target"))
|
---|
239 | {
|
---|
240 | if (a->argc <= i + 1 || !*a->argv[i+1])
|
---|
241 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
242 | i++;
|
---|
243 | if (!strcmp(a->argv[i], "global"))
|
---|
244 | cmd.mGlobal = true;
|
---|
245 | else
|
---|
246 | {
|
---|
247 | /* assume it's a UUID of a machine */
|
---|
248 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[i]).raw(),
|
---|
249 | cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else if ( !strcmp(a->argv[i], "--name")
|
---|
253 | || !strcmp(a->argv[i], "-name"))
|
---|
254 | {
|
---|
255 | if (a->argc <= i + 1 || !*a->argv[i+1])
|
---|
256 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
257 | i++;
|
---|
258 | cmd.mFilter.mName = a->argv[i];
|
---|
259 | }
|
---|
260 | else if ( !strcmp(a->argv[i], "--active")
|
---|
261 | || !strcmp(a->argv[i], "-active"))
|
---|
262 | {
|
---|
263 | if (a->argc <= i + 1)
|
---|
264 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
265 | i++;
|
---|
266 | if (!strcmp(a->argv[i], "yes"))
|
---|
267 | cmd.mFilter.mActive = true;
|
---|
268 | else if (!strcmp(a->argv[i], "no"))
|
---|
269 | cmd.mFilter.mActive = false;
|
---|
270 | else
|
---|
271 | return errorArgument(Usb::tr("Invalid --active argument '%s'"), a->argv[i]);
|
---|
272 | }
|
---|
273 | else if ( !strcmp(a->argv[i], "--vendorid")
|
---|
274 | || !strcmp(a->argv[i], "-vendorid"))
|
---|
275 | {
|
---|
276 | if (a->argc <= i + 1)
|
---|
277 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
278 | i++;
|
---|
279 | cmd.mFilter.mVendorId = a->argv[i];
|
---|
280 | }
|
---|
281 | else if ( !strcmp(a->argv[i], "--productid")
|
---|
282 | || !strcmp(a->argv[i], "-productid"))
|
---|
283 | {
|
---|
284 | if (a->argc <= i + 1)
|
---|
285 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
286 | i++;
|
---|
287 | cmd.mFilter.mProductId = a->argv[i];
|
---|
288 | }
|
---|
289 | else if ( !strcmp(a->argv[i], "--revision")
|
---|
290 | || !strcmp(a->argv[i], "-revision"))
|
---|
291 | {
|
---|
292 | if (a->argc <= i + 1)
|
---|
293 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
294 | i++;
|
---|
295 | cmd.mFilter.mRevision = a->argv[i];
|
---|
296 | }
|
---|
297 | else if ( !strcmp(a->argv[i], "--manufacturer")
|
---|
298 | || !strcmp(a->argv[i], "-manufacturer"))
|
---|
299 | {
|
---|
300 | if (a->argc <= i + 1)
|
---|
301 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
302 | i++;
|
---|
303 | cmd.mFilter.mManufacturer = a->argv[i];
|
---|
304 | }
|
---|
305 | else if ( !strcmp(a->argv[i], "--product")
|
---|
306 | || !strcmp(a->argv[i], "-product"))
|
---|
307 | {
|
---|
308 | if (a->argc <= i + 1)
|
---|
309 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
310 | i++;
|
---|
311 | cmd.mFilter.mProduct = a->argv[i];
|
---|
312 | }
|
---|
313 | else if ( !strcmp(a->argv[i], "--remote")
|
---|
314 | || !strcmp(a->argv[i], "-remote"))
|
---|
315 | {
|
---|
316 | if (a->argc <= i + 1)
|
---|
317 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
318 | i++;
|
---|
319 | cmd.mFilter.mRemote = a->argv[i];
|
---|
320 | }
|
---|
321 | else if ( !strcmp(a->argv[i], "--serialnumber")
|
---|
322 | || !strcmp(a->argv[i], "-serialnumber"))
|
---|
323 | {
|
---|
324 | if (a->argc <= i + 1)
|
---|
325 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
326 | i++;
|
---|
327 | cmd.mFilter.mSerialNumber = a->argv[i];
|
---|
328 | }
|
---|
329 | else if ( !strcmp(a->argv[i], "--maskedinterfaces")
|
---|
330 | || !strcmp(a->argv[i], "-maskedinterfaces"))
|
---|
331 | {
|
---|
332 | if (a->argc <= i + 1)
|
---|
333 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
334 | i++;
|
---|
335 | uint32_t u32;
|
---|
336 | int vrc = RTStrToUInt32Full(a->argv[i], 0, &u32);
|
---|
337 | if (RT_FAILURE(vrc))
|
---|
338 | return errorArgument(Usb::tr("Failed to convert the --maskedinterfaces value '%s' to a number, vrc=%Rrc"),
|
---|
339 | a->argv[i], vrc);
|
---|
340 | cmd.mFilter.mMaskedInterfaces = u32;
|
---|
341 | }
|
---|
342 | else if ( !strcmp(a->argv[i], "--action")
|
---|
343 | || !strcmp(a->argv[i], "-action"))
|
---|
344 | {
|
---|
345 | if (a->argc <= i + 1)
|
---|
346 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
347 | i++;
|
---|
348 | if (!strcmp(a->argv[i], "ignore"))
|
---|
349 | cmd.mFilter.mAction = USBDeviceFilterAction_Ignore;
|
---|
350 | else if (!strcmp(a->argv[i], "hold"))
|
---|
351 | cmd.mFilter.mAction = USBDeviceFilterAction_Hold;
|
---|
352 | else
|
---|
353 | return errorArgument(Usb::tr("Invalid USB filter action '%s'"), a->argv[i]);
|
---|
354 | }
|
---|
355 | else
|
---|
356 | return errorSyntax(Usb::tr("Unknown option '%s'"), a->argv[i]);
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (cmd.mAction == USBFilterCmd::Add)
|
---|
360 | {
|
---|
361 | // mandatory/forbidden options
|
---|
362 | if ( cmd.mFilter.mName.isEmpty()
|
---|
363 | ||
|
---|
364 | ( cmd.mGlobal
|
---|
365 | && cmd.mFilter.mAction == USBDeviceFilterAction_Null
|
---|
366 | )
|
---|
367 | || ( !cmd.mGlobal
|
---|
368 | && !cmd.mMachine)
|
---|
369 | || ( cmd.mGlobal
|
---|
370 | && !cmd.mFilter.mRemote.isEmpty())
|
---|
371 | )
|
---|
372 | {
|
---|
373 | return errorSyntax(Usb::tr("Mandatory options not supplied"));
|
---|
374 | }
|
---|
375 | }
|
---|
376 | break;
|
---|
377 | }
|
---|
378 |
|
---|
379 | case USBFilterCmd::Remove:
|
---|
380 | {
|
---|
381 | /* at least: 0: command, 1: index, 2: --target, 3: <target value> */
|
---|
382 | if (a->argc < 4)
|
---|
383 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
384 |
|
---|
385 | for (int i = 2; i < a->argc; i++)
|
---|
386 | {
|
---|
387 | if ( !strcmp(a->argv[i], "--target")
|
---|
388 | || !strcmp(a->argv[i], "-target"))
|
---|
389 | {
|
---|
390 | if (a->argc <= i + 1 || !*a->argv[i+1])
|
---|
391 | return errorArgument(Usb::tr("Missing argument to '%s'"), a->argv[i]);
|
---|
392 | i++;
|
---|
393 | if (!strcmp(a->argv[i], "global"))
|
---|
394 | cmd.mGlobal = true;
|
---|
395 | else
|
---|
396 | {
|
---|
397 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(a->argv[i]).raw(),
|
---|
398 | cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
|
---|
399 | }
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | // mandatory options
|
---|
404 | if (!cmd.mGlobal && !cmd.mMachine)
|
---|
405 | return errorSyntax(Usb::tr("Mandatory options not supplied"));
|
---|
406 |
|
---|
407 | break;
|
---|
408 | }
|
---|
409 |
|
---|
410 | default: break;
|
---|
411 | }
|
---|
412 |
|
---|
413 | USBFilterCmd::USBFilter &f = cmd.mFilter;
|
---|
414 |
|
---|
415 | ComPtr<IHost> host;
|
---|
416 | ComPtr<IUSBDeviceFilters> flts;
|
---|
417 | if (cmd.mGlobal)
|
---|
418 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
|
---|
419 | else
|
---|
420 | {
|
---|
421 | /* open a session for the VM */
|
---|
422 | CHECK_ERROR_RET(cmd.mMachine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
|
---|
423 | /* get the mutable session machine */
|
---|
424 | a->session->COMGETTER(Machine)(cmd.mMachine.asOutParam());
|
---|
425 | /* and get the USB device filters */
|
---|
426 | CHECK_ERROR_RET(cmd.mMachine, COMGETTER(USBDeviceFilters)(flts.asOutParam()), RTEXITCODE_FAILURE);
|
---|
427 | }
|
---|
428 |
|
---|
429 | switch (cmd.mAction)
|
---|
430 | {
|
---|
431 | case USBFilterCmd::Add:
|
---|
432 | {
|
---|
433 | if (cmd.mGlobal)
|
---|
434 | {
|
---|
435 | ComPtr<IHostUSBDeviceFilter> flt;
|
---|
436 | CHECK_ERROR_BREAK(host, CreateUSBDeviceFilter(f.mName.raw(),
|
---|
437 | flt.asOutParam()));
|
---|
438 |
|
---|
439 | if (!f.mActive.isNull())
|
---|
440 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
441 | if (!f.mVendorId.isEmpty())
|
---|
442 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
443 | if (!f.mProductId.isEmpty())
|
---|
444 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
445 | if (!f.mRevision.isEmpty())
|
---|
446 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
447 | if (!f.mManufacturer.isEmpty())
|
---|
448 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
449 | if (!f.mSerialNumber.isEmpty())
|
---|
450 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
451 | if (!f.mMaskedInterfaces.isNull())
|
---|
452 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
453 |
|
---|
454 | if (f.mAction != USBDeviceFilterAction_Null)
|
---|
455 | CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
|
---|
456 |
|
---|
457 | CHECK_ERROR_BREAK(host, InsertUSBDeviceFilter(cmd.mIndex, flt));
|
---|
458 | }
|
---|
459 | else
|
---|
460 | {
|
---|
461 | ComPtr<IUSBDeviceFilter> flt;
|
---|
462 | CHECK_ERROR_BREAK(flts, CreateDeviceFilter(f.mName.raw(),
|
---|
463 | flt.asOutParam()));
|
---|
464 |
|
---|
465 | if (!f.mActive.isNull())
|
---|
466 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
467 | if (!f.mVendorId.isEmpty())
|
---|
468 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
469 | if (!f.mProductId.isEmpty())
|
---|
470 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
471 | if (!f.mRevision.isEmpty())
|
---|
472 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
473 | if (!f.mManufacturer.isEmpty())
|
---|
474 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
475 | if (!f.mRemote.isEmpty())
|
---|
476 | CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
|
---|
477 | if (!f.mSerialNumber.isEmpty())
|
---|
478 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
479 | if (!f.mMaskedInterfaces.isNull())
|
---|
480 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
481 |
|
---|
482 | CHECK_ERROR_BREAK(flts, InsertDeviceFilter(cmd.mIndex, flt));
|
---|
483 | }
|
---|
484 | break;
|
---|
485 | }
|
---|
486 | case USBFilterCmd::Modify:
|
---|
487 | {
|
---|
488 | if (cmd.mGlobal)
|
---|
489 | {
|
---|
490 | SafeIfaceArray <IHostUSBDeviceFilter> coll;
|
---|
491 | CHECK_ERROR_BREAK(host, COMGETTER(USBDeviceFilters)(ComSafeArrayAsOutParam(coll)));
|
---|
492 |
|
---|
493 | ComPtr<IHostUSBDeviceFilter> flt = coll[cmd.mIndex];
|
---|
494 |
|
---|
495 | if (!f.mName.isEmpty())
|
---|
496 | CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
|
---|
497 | if (!f.mActive.isNull())
|
---|
498 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
499 | if (!f.mVendorId.isEmpty())
|
---|
500 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
501 | if (!f.mProductId.isEmpty())
|
---|
502 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
503 | if (!f.mRevision.isEmpty())
|
---|
504 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
505 | if (!f.mManufacturer.isEmpty())
|
---|
506 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
507 | if (!f.mSerialNumber.isEmpty())
|
---|
508 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
509 | if (!f.mMaskedInterfaces.isNull())
|
---|
510 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
511 |
|
---|
512 | if (f.mAction != USBDeviceFilterAction_Null)
|
---|
513 | CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
|
---|
514 | }
|
---|
515 | else
|
---|
516 | {
|
---|
517 | SafeIfaceArray <IUSBDeviceFilter> coll;
|
---|
518 | CHECK_ERROR_BREAK(flts, COMGETTER(DeviceFilters)(ComSafeArrayAsOutParam(coll)));
|
---|
519 |
|
---|
520 | ComPtr<IUSBDeviceFilter> flt = coll[cmd.mIndex];
|
---|
521 |
|
---|
522 | if (!f.mName.isEmpty())
|
---|
523 | CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
|
---|
524 | if (!f.mActive.isNull())
|
---|
525 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
526 | if (!f.mVendorId.isEmpty())
|
---|
527 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
528 | if (!f.mProductId.isEmpty())
|
---|
529 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
530 | if (!f.mRevision.isEmpty())
|
---|
531 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
532 | if (!f.mManufacturer.isEmpty())
|
---|
533 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
534 | if (!f.mRemote.isEmpty())
|
---|
535 | CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
|
---|
536 | if (!f.mSerialNumber.isEmpty())
|
---|
537 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
538 | if (!f.mMaskedInterfaces.isNull())
|
---|
539 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
540 | }
|
---|
541 | break;
|
---|
542 | }
|
---|
543 | case USBFilterCmd::Remove:
|
---|
544 | {
|
---|
545 | if (cmd.mGlobal)
|
---|
546 | {
|
---|
547 | ComPtr<IHostUSBDeviceFilter> flt;
|
---|
548 | CHECK_ERROR_BREAK(host, RemoveUSBDeviceFilter(cmd.mIndex));
|
---|
549 | }
|
---|
550 | else
|
---|
551 | {
|
---|
552 | ComPtr<IUSBDeviceFilter> flt;
|
---|
553 | CHECK_ERROR_BREAK(flts, RemoveDeviceFilter(cmd.mIndex, flt.asOutParam()));
|
---|
554 | }
|
---|
555 | break;
|
---|
556 | }
|
---|
557 | default:
|
---|
558 | break;
|
---|
559 | }
|
---|
560 |
|
---|
561 | if (cmd.mMachine)
|
---|
562 | {
|
---|
563 | if (SUCCEEDED(hrc))
|
---|
564 | {
|
---|
565 | /* commit the session */
|
---|
566 | CHECK_ERROR(cmd.mMachine, SaveSettings());
|
---|
567 | }
|
---|
568 | /* close the session */
|
---|
569 | a->session->UnlockMachine();
|
---|
570 | }
|
---|
571 |
|
---|
572 | return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
573 | }
|
---|
574 |
|
---|
575 | RTEXITCODE handleUSBDevSource(HandlerArg *a)
|
---|
576 | {
|
---|
577 | HRESULT hrc = S_OK;
|
---|
578 |
|
---|
579 | /* at least: 0: command, 1: source id */
|
---|
580 | if (a->argc < 2)
|
---|
581 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
582 |
|
---|
583 | ComPtr<IHost> host;
|
---|
584 | if (!strcmp(a->argv[0], "add"))
|
---|
585 | {
|
---|
586 | setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_ADD);
|
---|
587 |
|
---|
588 | Bstr strBackend;
|
---|
589 | Bstr strAddress;
|
---|
590 | if (a->argc != 6)
|
---|
591 | return errorSyntax(Usb::tr("Invalid number of parameters"));
|
---|
592 |
|
---|
593 | for (int i = 2; i < a->argc; i++)
|
---|
594 | {
|
---|
595 | if (!strcmp(a->argv[i], "--backend"))
|
---|
596 | {
|
---|
597 | i++;
|
---|
598 | strBackend = a->argv[i];
|
---|
599 | }
|
---|
600 | else if (!strcmp(a->argv[i], "--address"))
|
---|
601 | {
|
---|
602 | i++;
|
---|
603 | strAddress = a->argv[i];
|
---|
604 | }
|
---|
605 | else
|
---|
606 | return errorSyntax(Usb::tr("Parameter \"%s\" is invalid"), a->argv[i]);
|
---|
607 | }
|
---|
608 |
|
---|
609 | SafeArray<BSTR> usbSourcePropNames;
|
---|
610 | SafeArray<BSTR> usbSourcePropValues;
|
---|
611 |
|
---|
612 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
|
---|
613 | CHECK_ERROR_RET(host, AddUSBDeviceSource(strBackend.raw(), Bstr(a->argv[1]).raw(), strAddress.raw(),
|
---|
614 | ComSafeArrayAsInParam(usbSourcePropNames), ComSafeArrayAsInParam(usbSourcePropValues)),
|
---|
615 | RTEXITCODE_FAILURE);
|
---|
616 | }
|
---|
617 | else if (!strcmp(a->argv[0], "remove"))
|
---|
618 | {
|
---|
619 | setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_REMOVE);
|
---|
620 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
|
---|
621 | CHECK_ERROR_RET(host, RemoveUSBDeviceSource(Bstr(a->argv[1]).raw()), RTEXITCODE_FAILURE);
|
---|
622 | }
|
---|
623 |
|
---|
624 | return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|