1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox IHostUSBDevice COM interface implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ____H_HOSTUSBDEVICEIMPL
|
---|
19 | #define ____H_HOSTUSBDEVICEIMPL
|
---|
20 |
|
---|
21 | #include "VirtualBoxBase.h"
|
---|
22 | #include "USBDeviceFilterImpl.h"
|
---|
23 | /* #include "USBProxyService.h" circular on Host/HostUSBDevice, the includer
|
---|
24 | * must include this. */
|
---|
25 | #include "Collection.h"
|
---|
26 |
|
---|
27 | #include <VBox/usb.h>
|
---|
28 |
|
---|
29 | class SessionMachine;
|
---|
30 | class USBProxyService;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Object class used to hold Host USB Device properties.
|
---|
34 | */
|
---|
35 | class ATL_NO_VTABLE HostUSBDevice :
|
---|
36 | public VirtualBoxBaseNEXT,
|
---|
37 | public VirtualBoxSupportErrorInfoImpl <HostUSBDevice, IHostUSBDevice>,
|
---|
38 | public VirtualBoxSupportTranslation <HostUSBDevice>,
|
---|
39 | public IHostUSBDevice
|
---|
40 | {
|
---|
41 | public:
|
---|
42 |
|
---|
43 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT (HostUSBDevice)
|
---|
44 |
|
---|
45 | DECLARE_NOT_AGGREGATABLE(HostUSBDevice)
|
---|
46 |
|
---|
47 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
48 |
|
---|
49 | BEGIN_COM_MAP(HostUSBDevice)
|
---|
50 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
51 | COM_INTERFACE_ENTRY(IHostUSBDevice)
|
---|
52 | COM_INTERFACE_ENTRY(IUSBDevice)
|
---|
53 | END_COM_MAP()
|
---|
54 |
|
---|
55 | NS_DECL_ISUPPORTS
|
---|
56 |
|
---|
57 | DECLARE_EMPTY_CTOR_DTOR (HostUSBDevice)
|
---|
58 |
|
---|
59 | HRESULT FinalConstruct();
|
---|
60 | void FinalRelease();
|
---|
61 |
|
---|
62 | // public initializer/uninitializer for internal purposes only
|
---|
63 | HRESULT init(PUSBDEVICE aUsb, USBProxyService *aUSBProxyService);
|
---|
64 | void uninit();
|
---|
65 |
|
---|
66 | // IUSBDevice properties
|
---|
67 | STDMETHOD(COMGETTER(Id))(GUIDPARAMOUT aId);
|
---|
68 | STDMETHOD(COMGETTER(VendorId))(USHORT *aVendorId);
|
---|
69 | STDMETHOD(COMGETTER(ProductId))(USHORT *aProductId);
|
---|
70 | STDMETHOD(COMGETTER(Revision))(USHORT *aRevision);
|
---|
71 | STDMETHOD(COMGETTER(Manufacturer))(BSTR *aManufacturer);
|
---|
72 | STDMETHOD(COMGETTER(Product))(BSTR *aProduct);
|
---|
73 | STDMETHOD(COMGETTER(SerialNumber))(BSTR *aSerialNumber);
|
---|
74 | STDMETHOD(COMGETTER(Address))(BSTR *aAddress);
|
---|
75 | STDMETHOD(COMGETTER(Port))(USHORT *aPort);
|
---|
76 | STDMETHOD(COMGETTER(Remote))(BOOL *aRemote);
|
---|
77 |
|
---|
78 | // IHostUSBDevice properties
|
---|
79 | STDMETHOD(COMGETTER(State))(USBDeviceState_T *aState);
|
---|
80 |
|
---|
81 | /** Additional internal states.
|
---|
82 | * The async detach stuff for Darwin is a two stage journey with a variation
|
---|
83 | * (filters) depending on who won the race to lock the Host object.
|
---|
84 | *
|
---|
85 | * @remark Trying out mac os x style enum naming convention here. nice or what?
|
---|
86 | */
|
---|
87 | typedef enum
|
---|
88 | {
|
---|
89 | /** Nothing is pending here, check mPendingState. */
|
---|
90 | kNothingPending,
|
---|
91 | /** 1st stage of the detch, waiting for the logical detach notification. */
|
---|
92 | kDetachingPendingDetach,
|
---|
93 | /** 1st stage of the detch, waiting for the logical detach notification - re-run filters.
|
---|
94 | * Prev: kDetachingPendingDetach */
|
---|
95 | kDetachingPendingDetachFilters,
|
---|
96 | /** 2nd stage of the detach, waiting for the logical attach notification.
|
---|
97 | * Prev: kDetachingPendingDetach */
|
---|
98 | kDetachingPendingAttach,
|
---|
99 | /** 2nd stage of the detach, waiting for the logical attach notification - re-run filters.
|
---|
100 | * Prev: kDetachingPendingDetachFilters */
|
---|
101 | kDetachingPendingAttachFilters
|
---|
102 | } InternalState;
|
---|
103 |
|
---|
104 | // public methods only for internal purposes
|
---|
105 |
|
---|
106 | /** @note Must be called from under the object read lock. */
|
---|
107 | const Guid &id() const { return mId; }
|
---|
108 |
|
---|
109 | /** @note Must be called from under the object read lock. */
|
---|
110 | USBDeviceState_T state() const { return mState; }
|
---|
111 |
|
---|
112 | /** @note Must be called from under the object read lock. */
|
---|
113 | USBDeviceState_T pendingState() const { return mPendingState; }
|
---|
114 |
|
---|
115 | /** @note Must be called from under the object read lock. */
|
---|
116 | InternalState pendingStateEx() const { return mPendingStateEx; }
|
---|
117 |
|
---|
118 | /** @note Must be called from under the object read lock. */
|
---|
119 | ComObjPtr <SessionMachine> &machine() { return mMachine; }
|
---|
120 |
|
---|
121 | /** @note Must be called from under the object read lock. */
|
---|
122 | bool isStatePending() const { return mIsStatePending; }
|
---|
123 |
|
---|
124 | /** @note Must be called from under the object read lock. */
|
---|
125 | PCUSBDEVICE usbData() const { return mUsb; }
|
---|
126 |
|
---|
127 | Utf8Str name();
|
---|
128 |
|
---|
129 | bool requestCapture (SessionMachine *aMachine);
|
---|
130 | void requestRelease();
|
---|
131 | void requestHold();
|
---|
132 |
|
---|
133 | void setHeld();
|
---|
134 | void onDetachedPhys();
|
---|
135 |
|
---|
136 | void handlePendingStateChange();
|
---|
137 | void cancelPendingState(bool aTimeout = false);
|
---|
138 |
|
---|
139 | bool isMatch (const USBDeviceFilter::Data &aData);
|
---|
140 |
|
---|
141 | int compare (PCUSBDEVICE aDev2);
|
---|
142 | static int compare (PCUSBDEVICE aDev1, PCUSBDEVICE aDev2,
|
---|
143 | bool aIsStrict = true);
|
---|
144 |
|
---|
145 | bool updateState (PCUSBDEVICE aDev);
|
---|
146 |
|
---|
147 | void checkForAsyncTimeout();
|
---|
148 |
|
---|
149 | bool setLogicalReconnect (InternalState aStage);
|
---|
150 |
|
---|
151 | // for VirtualBoxSupportErrorInfoImpl
|
---|
152 | static const wchar_t *getComponentName() { return L"HostUSBDevice"; }
|
---|
153 |
|
---|
154 | private:
|
---|
155 |
|
---|
156 | const Guid mId;
|
---|
157 | USBDeviceState_T mState;
|
---|
158 | USBDeviceState_T mPendingState;
|
---|
159 | /** Same as mPendingState but for the internal states. */
|
---|
160 | InternalState mPendingStateEx;
|
---|
161 | /** RTTimeNanoTS() of when mIsStatePending was set or mDetaching changed
|
---|
162 | * from kNotDetaching. For operations that cannot be cancelled it's 0. */
|
---|
163 | uint64_t mPendingSince;
|
---|
164 | ComObjPtr <SessionMachine> mMachine;
|
---|
165 | bool mIsStatePending : 1;
|
---|
166 |
|
---|
167 | /** Pointer to the USB Proxy Service instance. */
|
---|
168 | USBProxyService *mUSBProxyService;
|
---|
169 |
|
---|
170 | /** Pointer to the USB Device structure owned by this device.
|
---|
171 | * Only used for host devices. */
|
---|
172 | PUSBDEVICE mUsb;
|
---|
173 |
|
---|
174 | friend class USBProxyService;
|
---|
175 | #ifdef RT_OS_LINUX
|
---|
176 | friend class USBProxyServiceLinux;
|
---|
177 | #endif
|
---|
178 | #ifdef RT_OS_DARWIN
|
---|
179 | /** One-shot filter id. */
|
---|
180 | void *mOneShotId;
|
---|
181 |
|
---|
182 | friend class USBProxyServiceDarwin;
|
---|
183 | #endif
|
---|
184 | };
|
---|
185 |
|
---|
186 |
|
---|
187 | COM_DECL_READONLY_ENUM_AND_COLLECTION_BEGIN (HostUSBDevice)
|
---|
188 |
|
---|
189 | STDMETHOD(FindById) (INPTR GUIDPARAM aId, IHostUSBDevice **aDevice)
|
---|
190 | {
|
---|
191 | Guid idToFind = aId;
|
---|
192 | if (idToFind.isEmpty())
|
---|
193 | return E_INVALIDARG;
|
---|
194 | if (!aDevice)
|
---|
195 | return E_POINTER;
|
---|
196 |
|
---|
197 | *aDevice = NULL;
|
---|
198 | Vector::value_type found;
|
---|
199 | Vector::iterator it = vec.begin();
|
---|
200 | while (!found && it != vec.end())
|
---|
201 | {
|
---|
202 | Guid id;
|
---|
203 | (*it)->COMGETTER(Id) (id.asOutParam());
|
---|
204 | if (id == idToFind)
|
---|
205 | found = *it;
|
---|
206 | ++ it;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if (!found)
|
---|
210 | return setError (E_INVALIDARG, HostUSBDeviceCollection::tr (
|
---|
211 | "Could not find a USB device with UUID {%s}"),
|
---|
212 | idToFind.toString().raw());
|
---|
213 |
|
---|
214 | return found.queryInterfaceTo (aDevice);
|
---|
215 | }
|
---|
216 |
|
---|
217 | STDMETHOD(FindByAddress) (INPTR BSTR aAddress, IHostUSBDevice **aDevice)
|
---|
218 | {
|
---|
219 | if (!aAddress)
|
---|
220 | return E_INVALIDARG;
|
---|
221 | if (!aDevice)
|
---|
222 | return E_POINTER;
|
---|
223 |
|
---|
224 | *aDevice = NULL;
|
---|
225 | Vector::value_type found;
|
---|
226 | Vector::iterator it = vec.begin();
|
---|
227 | while (!found && it != vec.end())
|
---|
228 | {
|
---|
229 | Bstr address;
|
---|
230 | (*it)->COMGETTER(Address) (address.asOutParam());
|
---|
231 | if (address == aAddress)
|
---|
232 | found = *it;
|
---|
233 | ++ it;
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (!found)
|
---|
237 | return setError (E_INVALIDARG, HostUSBDeviceCollection::tr (
|
---|
238 | "Could not find a USB device with address '%ls'"),
|
---|
239 | aAddress);
|
---|
240 |
|
---|
241 | return found.queryInterfaceTo (aDevice);
|
---|
242 | }
|
---|
243 |
|
---|
244 | COM_DECL_READONLY_ENUM_AND_COLLECTION_END (HostUSBDevice)
|
---|
245 |
|
---|
246 |
|
---|
247 | #endif // ____H_HOSTUSBDEVICEIMPL
|
---|