1 | /* $Id: HostHardwareLinux.h 31652 2010-08-13 14:17:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Classes for handling hardware detection under Linux.
|
---|
4 | *
|
---|
5 | * Please feel free to expand these to work for other systems (Solaris!) or to
|
---|
6 | * add new ones for other systems.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2008-2009 Oracle Corporation
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | * available from http://www.virtualbox.org. This file is free software;
|
---|
14 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | * General Public License (GPL) as published by the Free Software
|
---|
16 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef ____H_HOSTHARDWARELINUX
|
---|
22 | # define ____H_HOSTHARDWARELINUX
|
---|
23 |
|
---|
24 | #include <iprt/err.h>
|
---|
25 | #include <iprt/cpp/ministring.h>
|
---|
26 | #include <vector>
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Class for probing and returning information about host DVD and floppy
|
---|
30 | * drives. To use this class, create an instance, call one of the update
|
---|
31 | * methods to do the actual probing and use the iterator methods to get the
|
---|
32 | * result of the probe.
|
---|
33 | */
|
---|
34 | class VBoxMainDriveInfo
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | /** Structure describing a host drive */
|
---|
38 | struct DriveInfo
|
---|
39 | {
|
---|
40 | /** The device node of the drive. */
|
---|
41 | iprt::MiniString mDevice;
|
---|
42 | /** A unique identifier for the device, if available. This should be
|
---|
43 | * kept consistant accross different probing methods of a given
|
---|
44 | * platform if at all possible. */
|
---|
45 | iprt::MiniString mUdi;
|
---|
46 | /** A textual description of the drive. */
|
---|
47 | iprt::MiniString mDescription;
|
---|
48 |
|
---|
49 | /** Constructors */
|
---|
50 | DriveInfo(const iprt::MiniString &aDevice,
|
---|
51 | const iprt::MiniString &aUdi = "",
|
---|
52 | const iprt::MiniString &aDescription = "")
|
---|
53 | : mDevice(aDevice),
|
---|
54 | mUdi(aUdi),
|
---|
55 | mDescription(aDescription)
|
---|
56 | { }
|
---|
57 | };
|
---|
58 |
|
---|
59 | /** List (resp vector) holding drive information */
|
---|
60 | typedef std::vector<DriveInfo> DriveInfoList;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Search for host floppy drives and rebuild the list, which remains empty
|
---|
64 | * until the first time this method is called.
|
---|
65 | * @returns iprt status code
|
---|
66 | */
|
---|
67 | int updateFloppies();
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Search for host DVD drives and rebuild the list, which remains empty
|
---|
71 | * until the first time this method is called.
|
---|
72 | * @returns iprt status code
|
---|
73 | */
|
---|
74 | int updateDVDs();
|
---|
75 |
|
---|
76 | /** Get the first element in the list of floppy drives. */
|
---|
77 | DriveInfoList::const_iterator FloppyBegin()
|
---|
78 | {
|
---|
79 | return mFloppyList.begin();
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Get the last element in the list of floppy drives. */
|
---|
83 | DriveInfoList::const_iterator FloppyEnd()
|
---|
84 | {
|
---|
85 | return mFloppyList.end();
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Get the first element in the list of DVD drives. */
|
---|
89 | DriveInfoList::const_iterator DVDBegin()
|
---|
90 | {
|
---|
91 | return mDVDList.begin();
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Get the last element in the list of DVD drives. */
|
---|
95 | DriveInfoList::const_iterator DVDEnd()
|
---|
96 | {
|
---|
97 | return mDVDList.end();
|
---|
98 | }
|
---|
99 | private:
|
---|
100 | /** The list of currently available floppy drives */
|
---|
101 | DriveInfoList mFloppyList;
|
---|
102 | /** The list of currently available DVD drives */
|
---|
103 | DriveInfoList mDVDList;
|
---|
104 | };
|
---|
105 |
|
---|
106 | /** Convenience typedef. */
|
---|
107 | typedef VBoxMainDriveInfo::DriveInfoList DriveInfoList;
|
---|
108 | /** Convenience typedef. */
|
---|
109 | typedef VBoxMainDriveInfo::DriveInfo DriveInfo;
|
---|
110 |
|
---|
111 |
|
---|
112 | /** Vector type for the list of interfaces. */
|
---|
113 | #define VECTOR_TYPE char *
|
---|
114 | #define VECTOR_TYPENAME USBInterfaceList
|
---|
115 | static inline void USBInterfaceListCleanup(char **ppsz)
|
---|
116 | {
|
---|
117 | RTStrFree(*ppsz);
|
---|
118 | }
|
---|
119 | #define VECTOR_DESTRUCTOR USBInterfaceListCleanup
|
---|
120 | #include "vector.h"
|
---|
121 |
|
---|
122 | /** Structure describing a host USB device */
|
---|
123 | typedef struct USBDeviceInfo
|
---|
124 | {
|
---|
125 | /** The device node of the device. */
|
---|
126 | char *mDevice;
|
---|
127 | /** The system identifier of the device. Specific to the probing
|
---|
128 | * method. */
|
---|
129 | char *mSysfsPath;
|
---|
130 | /** Type for the list of interfaces. */
|
---|
131 | USBInterfaceList mInterfaces;
|
---|
132 | } USBDeviceInfo;
|
---|
133 |
|
---|
134 |
|
---|
135 | /** Destructor. */
|
---|
136 | static inline void USBDevInfoCleanup(USBDeviceInfo *pSelf)
|
---|
137 | {
|
---|
138 | RTStrFree(pSelf->mDevice);
|
---|
139 | RTStrFree(pSelf->mSysfsPath);
|
---|
140 | pSelf->mDevice = pSelf->mSysfsPath = NULL;
|
---|
141 | USBInterfaceList_cleanup(&pSelf->mInterfaces);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** Constructor - the strings will be duplicated. */
|
---|
146 | static inline int USBDevInfoInit(USBDeviceInfo *pSelf, const char *aDevice,
|
---|
147 | const char *aSystemID)
|
---|
148 | {
|
---|
149 | pSelf->mDevice = aDevice ? RTStrDup(aDevice) : NULL;
|
---|
150 | pSelf->mSysfsPath = aSystemID ? RTStrDup(aSystemID) : NULL;
|
---|
151 | if ( !USBInterfaceList_init(&pSelf->mInterfaces)
|
---|
152 | || (aDevice && !pSelf->mDevice) || (aSystemID && ! pSelf->mSysfsPath))
|
---|
153 | {
|
---|
154 | USBDevInfoCleanup(pSelf);
|
---|
155 | return 0;
|
---|
156 | }
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /** Vector type holding device information */
|
---|
162 | #define VECTOR_TYPE USBDeviceInfo
|
---|
163 | #define VECTOR_TYPENAME USBDeviceInfoList
|
---|
164 | #define VECTOR_DESTRUCTOR USBDevInfoCleanup
|
---|
165 | #include "vector.h"
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Class for probing and returning information about host USB devices.
|
---|
170 | * To use this class, create an instance, call the update methods to do the
|
---|
171 | * actual probing and use the iterator methods to get the result of the probe.
|
---|
172 | */
|
---|
173 | typedef struct VBoxMainUSBDeviceInfo
|
---|
174 | {
|
---|
175 | /** The list of currently available USB devices */
|
---|
176 | USBDeviceInfoList mDeviceList;
|
---|
177 | } VBoxMainUSBDeviceInfo;
|
---|
178 |
|
---|
179 | /** Constructor */
|
---|
180 | static inline int VBoxMainUSBDevInfoInit(VBoxMainUSBDeviceInfo *pSelf)
|
---|
181 | {
|
---|
182 | return USBDeviceInfoList_init(&pSelf->mDeviceList);
|
---|
183 | }
|
---|
184 |
|
---|
185 | /** Destructor */
|
---|
186 | static inline void VBoxMainUSBDevInfoCleanup(VBoxMainUSBDeviceInfo *pSelf)
|
---|
187 | {
|
---|
188 | USBDeviceInfoList_cleanup(&pSelf->mDeviceList);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Search for host USB devices and rebuild the list, which remains empty
|
---|
193 | * until the first time this method is called.
|
---|
194 | * @returns iprt status code
|
---|
195 | */
|
---|
196 | int USBDevInfoUpdateDevices(VBoxMainUSBDeviceInfo *pSelf);
|
---|
197 |
|
---|
198 |
|
---|
199 | /** Get the first element in the list of USB devices. */
|
---|
200 | static inline const USBDeviceInfoList_iterator *USBDevInfoBegin
|
---|
201 | (VBoxMainUSBDeviceInfo *pSelf)
|
---|
202 | {
|
---|
203 | return USBDeviceInfoList_begin(&pSelf->mDeviceList);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /** Get the last element in the list of USB devices. */
|
---|
208 | static inline const USBDeviceInfoList_iterator *USBDevInfoEnd
|
---|
209 | (VBoxMainUSBDeviceInfo *pSelf)
|
---|
210 | {
|
---|
211 | return USBDeviceInfoList_end(&pSelf->mDeviceList);
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /** Implementation of the hotplug waiter class below */
|
---|
216 | class VBoxMainHotplugWaiterImpl
|
---|
217 | {
|
---|
218 | public:
|
---|
219 | VBoxMainHotplugWaiterImpl(void) {}
|
---|
220 | virtual ~VBoxMainHotplugWaiterImpl(void) {}
|
---|
221 | /** @copydoc VBoxMainHotplugWaiter::Wait */
|
---|
222 | virtual int Wait(RTMSINTERVAL cMillies) = 0;
|
---|
223 | /** @copydoc VBoxMainHotplugWaiter::Interrupt */
|
---|
224 | virtual void Interrupt(void) = 0;
|
---|
225 | /** @copydoc VBoxMainHotplugWaiter::getStatus */
|
---|
226 | virtual int getStatus(void) = 0;
|
---|
227 | };
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Class for waiting for a hotplug event. To use this class, create an
|
---|
231 | * instance and call the @a Wait() method, which blocks until an event or a
|
---|
232 | * user-triggered interruption occurs. Call @a Interrupt() to interrupt the
|
---|
233 | * wait before an event occurs.
|
---|
234 | */
|
---|
235 | class VBoxMainHotplugWaiter
|
---|
236 | {
|
---|
237 | /** Class implementation. */
|
---|
238 | VBoxMainHotplugWaiterImpl *mImpl;
|
---|
239 | public:
|
---|
240 | /** Constructor. Responsible for selecting the implementation. */
|
---|
241 | VBoxMainHotplugWaiter (void);
|
---|
242 | /** Destructor. */
|
---|
243 | ~VBoxMainHotplugWaiter (void)
|
---|
244 | {
|
---|
245 | delete mImpl;
|
---|
246 | }
|
---|
247 | /**
|
---|
248 | * Wait for a hotplug event.
|
---|
249 | *
|
---|
250 | * @returns VINF_SUCCESS if an event occurred or if Interrupt() was called.
|
---|
251 | * @returns VERR_TRY_AGAIN if the wait failed but this might (!) be a
|
---|
252 | * temporary failure.
|
---|
253 | * @returns VERR_NOT_SUPPORTED if the wait failed and will definitely not
|
---|
254 | * succeed if retried.
|
---|
255 | * @returns Possibly other iprt status codes otherwise.
|
---|
256 | * @param cMillies How long to wait for at most.
|
---|
257 | */
|
---|
258 | int Wait (RTMSINTERVAL cMillies)
|
---|
259 | {
|
---|
260 | return mImpl->Wait(cMillies);
|
---|
261 | }
|
---|
262 | /**
|
---|
263 | * Interrupts an active wait. In the current implementation, the wait
|
---|
264 | * may not return until up to two seconds after calling this method.
|
---|
265 | */
|
---|
266 | void Interrupt (void)
|
---|
267 | {
|
---|
268 | mImpl->Interrupt();
|
---|
269 | }
|
---|
270 |
|
---|
271 | int getStatus(void)
|
---|
272 | {
|
---|
273 | return mImpl ? mImpl->getStatus() : VERR_NO_MEMORY;
|
---|
274 | }
|
---|
275 | };
|
---|
276 |
|
---|
277 | #endif /* ____H_HOSTHARDWARELINUX */
|
---|
278 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|