[12599] | 1 | /* $Id: VBoxManageUSB.cpp 100785 2023-08-03 13:02:17Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[12599] | 3 | * VBoxManage - VirtualBox's command-line interface.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 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
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
| 28 | #include <VBox/com/com.h>
|
---|
| 29 | #include <VBox/com/string.h>
|
---|
| 30 | #include <VBox/com/Guid.h>
|
---|
[7379] | 31 | #include <VBox/com/array.h>
|
---|
[1] | 32 | #include <VBox/com/ErrorInfo.h>
|
---|
[20928] | 33 | #include <VBox/com/errorprint.h>
|
---|
[1] | 34 | #include <VBox/com/VirtualBox.h>
|
---|
| 35 |
|
---|
[17104] | 36 | #include "VBoxManage.h"
|
---|
[1] | 37 |
|
---|
[14619] | 38 | #include <iprt/asm.h>
|
---|
[1] | 39 |
|
---|
[17104] | 40 | using namespace com;
|
---|
[1] | 41 |
|
---|
[92372] | 42 | DECLARE_TRANSLATION_CONTEXT(Usb);
|
---|
| 43 |
|
---|
[1] | 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 | }
|
---|
[62183] | 59 | virtual ~MyUSBDevice() {}
|
---|
[1] | 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);
|
---|
[50117] | 75 | if (guid == Guid(COM_IIDOF(IUnknown)))
|
---|
[1] | 76 | *ppvObject = (IUnknown *)this;
|
---|
[21520] | 77 | #ifdef RT_OS_WINDOWS
|
---|
[50117] | 78 | else if (guid == Guid(COM_IIDOF(IDispatch)))
|
---|
[21520] | 79 | *ppvObject = (IDispatch *)this;
|
---|
| 80 | #endif
|
---|
[50117] | 81 | else if (guid == Guid(COM_IIDOF(IUSBDevice)))
|
---|
[1] | 82 | *ppvObject = (IUSBDevice *)this;
|
---|
| 83 | else
|
---|
| 84 | return E_NOINTERFACE;
|
---|
| 85 | AddRef();
|
---|
| 86 | return S_OK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[63300] | 89 | STDMETHOD(COMGETTER(Id))(OUT_GUID a_pId) { NOREF(a_pId); return E_NOTIMPL; }
|
---|
[1] | 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; }
|
---|
[63300] | 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; }
|
---|
[1] | 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 |
|
---|
[32718] | 124 | Nullable() : mIsNull(true) {}
|
---|
| 125 | Nullable(const T &aValue, bool aIsNull = false)
|
---|
| 126 | : mIsNull(aIsNull), mValue(aValue) {}
|
---|
[1] | 127 |
|
---|
| 128 | bool isNull() const { return mIsNull; };
|
---|
[32718] | 129 | void setNull(bool aIsNull = true) { mIsNull = aIsNull; }
|
---|
[1] | 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 | {
|
---|
[32718] | 151 | USBFilter()
|
---|
| 152 | : mAction(USBDeviceFilterAction_Null)
|
---|
[1] | 153 | {}
|
---|
| 154 |
|
---|
| 155 | Bstr mName;
|
---|
| 156 | Nullable <bool> mActive;
|
---|
| 157 | Bstr mVendorId;
|
---|
| 158 | Bstr mProductId;
|
---|
| 159 | Bstr mRevision;
|
---|
| 160 | Bstr mManufacturer;
|
---|
| 161 | Bstr mProduct;
|
---|
[97804] | 162 | Bstr mPort;
|
---|
[1] | 163 | Bstr mRemote;
|
---|
| 164 | Bstr mSerialNumber;
|
---|
[5713] | 165 | Nullable <ULONG> mMaskedInterfaces;
|
---|
[1] | 166 | USBDeviceFilterAction_T mAction;
|
---|
| 167 | };
|
---|
| 168 |
|
---|
| 169 | enum Action { Invalid, Add, Modify, Remove };
|
---|
| 170 |
|
---|
[32718] | 171 | USBFilterCmd() : mAction(Invalid), mIndex(0), mGlobal(false) {}
|
---|
[1] | 172 |
|
---|
| 173 | Action mAction;
|
---|
[14620] | 174 | uint32_t mIndex;
|
---|
[1] | 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 |
|
---|
[56118] | 182 | RTEXITCODE handleUSBFilter(HandlerArg *a)
|
---|
[3077] | 183 | {
|
---|
[95140] | 184 | HRESULT hrc = S_OK;
|
---|
[1] | 185 | USBFilterCmd cmd;
|
---|
| 186 |
|
---|
| 187 | /* which command? */
|
---|
| 188 | cmd.mAction = USBFilterCmd::Invalid;
|
---|
[100785] | 189 | if (!strcmp(a->argv[0], "add"))
|
---|
[94207] | 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 | }
|
---|
[1] | 204 |
|
---|
| 205 | if (cmd.mAction == USBFilterCmd::Invalid)
|
---|
[100785] | 206 | return errorUnknownSubcommand(a->argv[0]);
|
---|
[1] | 207 |
|
---|
| 208 | /* which index? */
|
---|
[100785] | 209 | if (VINF_SUCCESS != RTStrToUInt32Full(a->argv[1], 10, &cmd.mIndex))
|
---|
[94207] | 210 | return errorSyntax(Usb::tr("Invalid index '%s'"), a->argv[1]);
|
---|
[1] | 211 |
|
---|
[100785] | 212 | if (cmd.mAction == USBFilterCmd::Add || cmd.mAction == USBFilterCmd::Modify)
|
---|
[1] | 213 | {
|
---|
[100785] | 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[] =
|
---|
[1] | 223 | {
|
---|
[100785] | 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 | };
|
---|
[8373] | 238 |
|
---|
[100785] | 239 | int vrc = RTGetOptInit(&GetState, a->argc, a->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 2, 0 /*fFlags*/);
|
---|
| 240 | AssertRCReturn(vrc, RTEXITCODE_FAILURE);
|
---|
[1] | 241 |
|
---|
[100785] | 242 | while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
| 243 | {
|
---|
| 244 | switch (vrc)
|
---|
[1] | 245 | {
|
---|
[100785] | 246 | case 't': // --target
|
---|
| 247 | if (!strcmp(ValueUnion.psz, "global"))
|
---|
[1] | 248 | cmd.mGlobal = true;
|
---|
| 249 | else
|
---|
[100785] | 250 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(ValueUnion.psz).raw(),
|
---|
[56118] | 251 | cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
|
---|
[100785] | 252 | break;
|
---|
| 253 | case 'n': // --name
|
---|
| 254 | cmd.mFilter.mName = ValueUnion.psz;
|
---|
| 255 | break;
|
---|
| 256 | case 'a': // --active
|
---|
| 257 | if (!strcmp(ValueUnion.psz, "yes"))
|
---|
[1] | 258 | cmd.mFilter.mActive = true;
|
---|
[100785] | 259 | else if (!strcmp(ValueUnion.psz, "no"))
|
---|
[1] | 260 | cmd.mFilter.mActive = false;
|
---|
[100785] | 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"))
|
---|
[7207] | 293 | cmd.mFilter.mAction = USBDeviceFilterAction_Ignore;
|
---|
[100785] | 294 | else if (!strcmp(ValueUnion.psz, "hold"))
|
---|
[7207] | 295 | cmd.mFilter.mAction = USBDeviceFilterAction_Hold;
|
---|
[1] | 296 | else
|
---|
[100785] | 297 | return errorArgument(Usb::tr("Invalid USB filter action '%s'"), ValueUnion.psz);
|
---|
| 298 | break;
|
---|
| 299 | default:
|
---|
| 300 | return errorGetOpt(vrc, &ValueUnion);
|
---|
[1] | 301 | }
|
---|
[100785] | 302 | }
|
---|
[1] | 303 |
|
---|
[100785] | 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>)"));
|
---|
[1] | 318 | }
|
---|
[100785] | 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);
|
---|
[1] | 330 |
|
---|
[100785] | 331 | while ((vrc = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
[1] | 332 | {
|
---|
[100785] | 333 | switch (vrc)
|
---|
[1] | 334 | {
|
---|
[100785] | 335 | case 't': // --target
|
---|
| 336 | if (!strcmp(ValueUnion.psz, "global"))
|
---|
[1] | 337 | cmd.mGlobal = true;
|
---|
| 338 | else
|
---|
[100785] | 339 | CHECK_ERROR_RET(a->virtualBox, FindMachine(Bstr(ValueUnion.psz).raw(),
|
---|
[56118] | 340 | cmd.mMachine.asOutParam()), RTEXITCODE_FAILURE);
|
---|
[100785] | 341 | break;
|
---|
| 342 | default:
|
---|
| 343 | return errorGetOpt(vrc, &ValueUnion);
|
---|
[1] | 344 | }
|
---|
| 345 | }
|
---|
[100785] | 346 | // mandatory options
|
---|
| 347 | if (!cmd.mGlobal && !cmd.mMachine)
|
---|
| 348 | return errorSyntax(Usb::tr("Missing required option: --target"));
|
---|
[1] | 349 | }
|
---|
| 350 |
|
---|
| 351 | USBFilterCmd::USBFilter &f = cmd.mFilter;
|
---|
| 352 |
|
---|
[47908] | 353 | ComPtr<IHost> host;
|
---|
| 354 | ComPtr<IUSBDeviceFilters> flts;
|
---|
[1] | 355 | if (cmd.mGlobal)
|
---|
[56118] | 356 | CHECK_ERROR_RET(a->virtualBox, COMGETTER(Host)(host.asOutParam()), RTEXITCODE_FAILURE);
|
---|
[1] | 357 | else
|
---|
| 358 | {
|
---|
| 359 | /* open a session for the VM */
|
---|
[56118] | 360 | CHECK_ERROR_RET(cmd.mMachine, LockMachine(a->session, LockType_Shared), RTEXITCODE_FAILURE);
|
---|
[1] | 361 | /* get the mutable session machine */
|
---|
[16052] | 362 | a->session->COMGETTER(Machine)(cmd.mMachine.asOutParam());
|
---|
[47376] | 363 | /* and get the USB device filters */
|
---|
[56118] | 364 | CHECK_ERROR_RET(cmd.mMachine, COMGETTER(USBDeviceFilters)(flts.asOutParam()), RTEXITCODE_FAILURE);
|
---|
[1] | 365 | }
|
---|
| 366 |
|
---|
| 367 | switch (cmd.mAction)
|
---|
| 368 | {
|
---|
| 369 | case USBFilterCmd::Add:
|
---|
| 370 | {
|
---|
| 371 | if (cmd.mGlobal)
|
---|
| 372 | {
|
---|
[47908] | 373 | ComPtr<IHostUSBDeviceFilter> flt;
|
---|
[32718] | 374 | CHECK_ERROR_BREAK(host, CreateUSBDeviceFilter(f.mName.raw(),
|
---|
| 375 | flt.asOutParam()));
|
---|
[1] | 376 |
|
---|
| 377 | if (!f.mActive.isNull())
|
---|
[32718] | 378 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
[26753] | 379 | if (!f.mVendorId.isEmpty())
|
---|
[32718] | 380 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
[26753] | 381 | if (!f.mProductId.isEmpty())
|
---|
[32718] | 382 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
[26753] | 383 | if (!f.mRevision.isEmpty())
|
---|
[32718] | 384 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
[26753] | 385 | if (!f.mManufacturer.isEmpty())
|
---|
[32718] | 386 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
[100772] | 387 | if (!f.mProduct.isEmpty())
|
---|
| 388 | CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
|
---|
[97804] | 389 | if (!f.mPort.isEmpty())
|
---|
| 390 | CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
|
---|
[26753] | 391 | if (!f.mSerialNumber.isEmpty())
|
---|
[32718] | 392 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
[5713] | 393 | if (!f.mMaskedInterfaces.isNull())
|
---|
[32718] | 394 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
[1] | 395 |
|
---|
[7207] | 396 | if (f.mAction != USBDeviceFilterAction_Null)
|
---|
[32718] | 397 | CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
|
---|
[1] | 398 |
|
---|
[32718] | 399 | CHECK_ERROR_BREAK(host, InsertUSBDeviceFilter(cmd.mIndex, flt));
|
---|
[1] | 400 | }
|
---|
| 401 | else
|
---|
| 402 | {
|
---|
[47908] | 403 | ComPtr<IUSBDeviceFilter> flt;
|
---|
[47376] | 404 | CHECK_ERROR_BREAK(flts, CreateDeviceFilter(f.mName.raw(),
|
---|
[32718] | 405 | flt.asOutParam()));
|
---|
[1] | 406 |
|
---|
| 407 | if (!f.mActive.isNull())
|
---|
[32718] | 408 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
[26753] | 409 | if (!f.mVendorId.isEmpty())
|
---|
[32718] | 410 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
[26753] | 411 | if (!f.mProductId.isEmpty())
|
---|
[32718] | 412 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
[26753] | 413 | if (!f.mRevision.isEmpty())
|
---|
[32718] | 414 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
[26753] | 415 | if (!f.mManufacturer.isEmpty())
|
---|
[32718] | 416 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
[100772] | 417 | if (!f.mProduct.isEmpty())
|
---|
| 418 | CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
|
---|
[97804] | 419 | if (!f.mPort.isEmpty())
|
---|
| 420 | CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
|
---|
[26753] | 421 | if (!f.mRemote.isEmpty())
|
---|
[32718] | 422 | CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
|
---|
[26753] | 423 | if (!f.mSerialNumber.isEmpty())
|
---|
[32718] | 424 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
[5713] | 425 | if (!f.mMaskedInterfaces.isNull())
|
---|
[32718] | 426 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
[1] | 427 |
|
---|
[47376] | 428 | CHECK_ERROR_BREAK(flts, InsertDeviceFilter(cmd.mIndex, flt));
|
---|
[1] | 429 | }
|
---|
| 430 | break;
|
---|
| 431 | }
|
---|
| 432 | case USBFilterCmd::Modify:
|
---|
| 433 | {
|
---|
| 434 | if (cmd.mGlobal)
|
---|
| 435 | {
|
---|
[17394] | 436 | SafeIfaceArray <IHostUSBDeviceFilter> coll;
|
---|
[32718] | 437 | CHECK_ERROR_BREAK(host, COMGETTER(USBDeviceFilters)(ComSafeArrayAsOutParam(coll)));
|
---|
[1] | 438 |
|
---|
[47908] | 439 | ComPtr<IHostUSBDeviceFilter> flt = coll[cmd.mIndex];
|
---|
[17394] | 440 |
|
---|
[26753] | 441 | if (!f.mName.isEmpty())
|
---|
[32718] | 442 | CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
|
---|
[1] | 443 | if (!f.mActive.isNull())
|
---|
[32718] | 444 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
[26753] | 445 | if (!f.mVendorId.isEmpty())
|
---|
[32718] | 446 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
[26753] | 447 | if (!f.mProductId.isEmpty())
|
---|
[32718] | 448 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
[26753] | 449 | if (!f.mRevision.isEmpty())
|
---|
[32718] | 450 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
[26753] | 451 | if (!f.mManufacturer.isEmpty())
|
---|
[32718] | 452 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
[100772] | 453 | if (!f.mProduct.isEmpty())
|
---|
| 454 | CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
|
---|
[97804] | 455 | if (!f.mPort.isEmpty())
|
---|
| 456 | CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
|
---|
[26753] | 457 | if (!f.mSerialNumber.isEmpty())
|
---|
[32718] | 458 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
[5713] | 459 | if (!f.mMaskedInterfaces.isNull())
|
---|
[32718] | 460 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
[1] | 461 |
|
---|
[7207] | 462 | if (f.mAction != USBDeviceFilterAction_Null)
|
---|
[32718] | 463 | CHECK_ERROR_BREAK(flt, COMSETTER(Action)(f.mAction));
|
---|
[1] | 464 | }
|
---|
| 465 | else
|
---|
| 466 | {
|
---|
[17336] | 467 | SafeIfaceArray <IUSBDeviceFilter> coll;
|
---|
[47376] | 468 | CHECK_ERROR_BREAK(flts, COMGETTER(DeviceFilters)(ComSafeArrayAsOutParam(coll)));
|
---|
[1] | 469 |
|
---|
[47908] | 470 | ComPtr<IUSBDeviceFilter> flt = coll[cmd.mIndex];
|
---|
[1] | 471 |
|
---|
[26753] | 472 | if (!f.mName.isEmpty())
|
---|
[32718] | 473 | CHECK_ERROR_BREAK(flt, COMSETTER(Name)(f.mName.raw()));
|
---|
[1] | 474 | if (!f.mActive.isNull())
|
---|
[32718] | 475 | CHECK_ERROR_BREAK(flt, COMSETTER(Active)(f.mActive));
|
---|
[26753] | 476 | if (!f.mVendorId.isEmpty())
|
---|
[32718] | 477 | CHECK_ERROR_BREAK(flt, COMSETTER(VendorId)(f.mVendorId.raw()));
|
---|
[26753] | 478 | if (!f.mProductId.isEmpty())
|
---|
[32718] | 479 | CHECK_ERROR_BREAK(flt, COMSETTER(ProductId)(f.mProductId.raw()));
|
---|
[26753] | 480 | if (!f.mRevision.isEmpty())
|
---|
[32718] | 481 | CHECK_ERROR_BREAK(flt, COMSETTER(Revision)(f.mRevision.raw()));
|
---|
[26753] | 482 | if (!f.mManufacturer.isEmpty())
|
---|
[32718] | 483 | CHECK_ERROR_BREAK(flt, COMSETTER(Manufacturer)(f.mManufacturer.raw()));
|
---|
[100772] | 484 | if (!f.mProduct.isEmpty())
|
---|
| 485 | CHECK_ERROR_BREAK(flt, COMSETTER(Product)(f.mProduct.raw()));
|
---|
[97804] | 486 | if (!f.mPort.isEmpty())
|
---|
| 487 | CHECK_ERROR_BREAK(flt, COMSETTER(Port)(f.mPort.raw()));
|
---|
[26753] | 488 | if (!f.mRemote.isEmpty())
|
---|
[32718] | 489 | CHECK_ERROR_BREAK(flt, COMSETTER(Remote)(f.mRemote.raw()));
|
---|
[26753] | 490 | if (!f.mSerialNumber.isEmpty())
|
---|
[32718] | 491 | CHECK_ERROR_BREAK(flt, COMSETTER(SerialNumber)(f.mSerialNumber.raw()));
|
---|
[5713] | 492 | if (!f.mMaskedInterfaces.isNull())
|
---|
[32718] | 493 | CHECK_ERROR_BREAK(flt, COMSETTER(MaskedInterfaces)(f.mMaskedInterfaces));
|
---|
[1] | 494 | }
|
---|
| 495 | break;
|
---|
| 496 | }
|
---|
| 497 | case USBFilterCmd::Remove:
|
---|
| 498 | {
|
---|
| 499 | if (cmd.mGlobal)
|
---|
| 500 | {
|
---|
[47908] | 501 | ComPtr<IHostUSBDeviceFilter> flt;
|
---|
[32718] | 502 | CHECK_ERROR_BREAK(host, RemoveUSBDeviceFilter(cmd.mIndex));
|
---|
[1] | 503 | }
|
---|
| 504 | else
|
---|
| 505 | {
|
---|
[47908] | 506 | ComPtr<IUSBDeviceFilter> flt;
|
---|
[47376] | 507 | CHECK_ERROR_BREAK(flts, RemoveDeviceFilter(cmd.mIndex, flt.asOutParam()));
|
---|
[1] | 508 | }
|
---|
| 509 | break;
|
---|
| 510 | }
|
---|
| 511 | default:
|
---|
| 512 | break;
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | if (cmd.mMachine)
|
---|
| 516 | {
|
---|
[95140] | 517 | if (SUCCEEDED(hrc))
|
---|
[20834] | 518 | {
|
---|
| 519 | /* commit the session */
|
---|
| 520 | CHECK_ERROR(cmd.mMachine, SaveSettings());
|
---|
| 521 | }
|
---|
| 522 | /* close the session */
|
---|
[31070] | 523 | a->session->UnlockMachine();
|
---|
[1] | 524 | }
|
---|
| 525 |
|
---|
[95140] | 526 | return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
[1] | 527 | }
|
---|
[60089] | 528 |
|
---|
| 529 | RTEXITCODE handleUSBDevSource(HandlerArg *a)
|
---|
| 530 | {
|
---|
[95140] | 531 | HRESULT hrc = S_OK;
|
---|
[60089] | 532 |
|
---|
| 533 | /* at least: 0: command, 1: source id */
|
---|
| 534 | if (a->argc < 2)
|
---|
[94213] | 535 | return errorSyntax(Usb::tr("Not enough parameters"));
|
---|
[60089] | 536 |
|
---|
| 537 | ComPtr<IHost> host;
|
---|
| 538 | if (!strcmp(a->argv[0], "add"))
|
---|
| 539 | {
|
---|
[94213] | 540 | setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_ADD);
|
---|
| 541 |
|
---|
[60089] | 542 | Bstr strBackend;
|
---|
| 543 | Bstr strAddress;
|
---|
| 544 | if (a->argc != 6)
|
---|
[94213] | 545 | return errorSyntax(Usb::tr("Invalid number of parameters"));
|
---|
[60089] | 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
|
---|
[94213] | 560 | return errorSyntax(Usb::tr("Parameter \"%s\" is invalid"), a->argv[i]);
|
---|
[60089] | 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 | {
|
---|
[94213] | 573 | setCurrentSubcommand(HELP_SCOPE_USBDEVSOURCE_REMOVE);
|
---|
[60089] | 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 |
|
---|
[95140] | 578 | return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
[60089] | 579 | }
|
---|
| 580 |
|
---|
[17394] | 581 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|