VirtualBox

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

Last change on this file since 52849 was 50117, checked in by vboxsync, 11 years ago

Main+Frontends: clear out some cruft code, outdated EventQueue stuff and whitespace cleanup

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