VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/testcase/USBTest.cpp@ 54558

Last change on this file since 54558 was 52840, checked in by vboxsync, 10 years ago

USBTest.cpp: rather use %x instead of %p for pointers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/** @file
2 *
3 * VBox host drivers - USB drivers - Filter & driver installation
4 *
5 * Installation code
6 *
7 * Copyright (C) 2006-2011 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
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <windows.h>
23#include <setupapi.h>
24#include <newdev.h>
25#include <iprt/assert.h>
26#include <iprt/err.h>
27#include <iprt/param.h>
28#include <iprt/path.h>
29#include <iprt/string.h>
30#include <VBox/err.h>
31#include <stdio.h>
32#include <VBox/usblib.h>
33#include <VBox/VBoxDrvCfg-win.h>
34
35/** Handle to the open device. */
36static HANDLE g_hUSBMonitor = INVALID_HANDLE_VALUE;
37/** Flags whether or not we started the service. */
38static bool g_fStartedService = false;
39
40/**
41 * Attempts to start the service, creating it if necessary.
42 *
43 * @returns 0 on success.
44 * @returns -1 on failure.
45 * @param fRetry Indicates retry call.
46 */
47int usbMonStartService(void)
48{
49 HRESULT hr = VBoxDrvCfgSvcStart(USBMON_SERVICE_NAME_W);
50 if (hr != S_OK)
51 {
52 AssertMsgFailed(("couldn't start service, hr (0x%x)\n", hr));
53 return -1;
54 }
55 return 0;
56}
57
58/**
59 * Stops a possibly running service.
60 *
61 * @returns 0 on success.
62 * @returns -1 on failure.
63 */
64int usbMonStopService(void)
65{
66 printf("usbMonStopService\n");
67 /*
68 * Assume it didn't exist, so we'll create the service.
69 */
70 int rc = -1;
71 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
72 DWORD LastError = GetLastError(); NOREF(LastError);
73 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
74 if (hSMgr)
75 {
76 SC_HANDLE hService = OpenServiceW(hSMgr, USBMON_SERVICE_NAME_W, SERVICE_STOP | SERVICE_QUERY_STATUS);
77 if (hService)
78 {
79 /*
80 * Stop the service.
81 */
82 SERVICE_STATUS Status;
83 QueryServiceStatus(hService, &Status);
84 if (Status.dwCurrentState == SERVICE_STOPPED)
85 rc = 0;
86 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
87 {
88 int iWait = 100;
89 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
90 {
91 Sleep(100);
92 QueryServiceStatus(hService, &Status);
93 }
94 if (Status.dwCurrentState == SERVICE_STOPPED)
95 rc = 0;
96 else
97 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
98 }
99 else
100 {
101 DWORD LastError = GetLastError(); NOREF(LastError);
102 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
103 }
104 CloseServiceHandle(hService);
105 }
106 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
107 rc = 0;
108 else
109 {
110 DWORD LastError = GetLastError(); NOREF(LastError);
111 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
112 }
113 CloseServiceHandle(hSMgr);
114 }
115 return rc;
116}
117/**
118 * Release specified USB device to the host.
119 *
120 * @returns VBox status code
121 * @param usVendorId Vendor id
122 * @param usProductId Product id
123 * @param usRevision Revision
124 */
125int usbMonReleaseDevice(USHORT usVendorId, USHORT usProductId, USHORT usRevision)
126{
127 USBSUP_RELEASE release;
128 DWORD cbReturned = 0;
129
130 printf("usbLibReleaseDevice %x %x %x\n", usVendorId, usProductId, usRevision);
131
132 release.usVendorId = usVendorId;
133 release.usProductId = usProductId;
134 release.usRevision = usRevision;
135
136 if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_RELEASE_DEVICE, &release, sizeof(release), NULL, 0, &cbReturned, NULL))
137 {
138 AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
139 return RTErrConvertFromWin32(GetLastError());
140 }
141
142 return VINF_SUCCESS;
143}
144
145
146/**
147 * Add USB device filter
148 *
149 * @returns VBox status code.
150 * @param pszVendor Vendor filter string
151 * @param pszProduct Product filter string
152 * @param pszRevision Revision filter string
153 * @param ppID Pointer to filter id
154 */
155int usbMonInsertFilter(const char *pszVendor, const char *pszProduct, const char *pszRevision, void **ppID)
156{
157 USBFILTER filter;
158 USBSUP_FLTADDOUT flt_add;
159 DWORD cbReturned = 0;
160
161 Assert(g_hUSBMonitor);
162
163 printf("usblibInsertFilter %s %s %s\n", pszVendor, pszProduct, pszRevision);
164
165// strncpy(filter.szVendor, pszVendor, sizeof(filter.szVendor));
166// strncpy(filter.szProduct, pszProduct, sizeof(filter.szProduct));
167// strncpy(filter.szRevision, pszRevision, sizeof(filter.szRevision));
168
169 if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_ADD_FILTER, &filter, sizeof(filter), &flt_add, sizeof(flt_add), &cbReturned, NULL))
170 {
171 AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
172 return RTErrConvertFromWin32(GetLastError());
173 }
174 *ppID = (void *)flt_add.uId;
175 return VINF_SUCCESS;
176}
177
178/**
179 * Remove USB device filter
180 *
181 * @returns VBox status code.
182 * @param aID Filter id
183 */
184int usbMonRemoveFilter (void *aID)
185{
186 uintptr_t uId;
187 DWORD cbReturned = 0;
188
189 Assert(g_hUSBMonitor);
190
191 printf("usblibRemoveFilter %p\n", aID);
192
193 uId = (uintptr_t)aID;
194 if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_REMOVE_FILTER, &uId, sizeof(uId), NULL, 0,&cbReturned, NULL))
195 {
196 AssertMsgFailed(("DeviceIoControl failed with %d\n", GetLastError()));
197 return RTErrConvertFromWin32(GetLastError());
198 }
199 return VINF_SUCCESS;
200}
201
202/**
203 * Initialize the USB monitor
204 *
205 * @returns VBox status code.
206 */
207int usbMonitorInit()
208{
209 int rc;
210 USBSUP_VERSION version = {0};
211 DWORD cbReturned;
212
213 printf("usbproxy: usbLibInit\n");
214
215 g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
216 GENERIC_READ | GENERIC_WRITE,
217 FILE_SHARE_READ | FILE_SHARE_WRITE,
218 NULL, // no SECURITY_ATTRIBUTES structure
219 OPEN_EXISTING, // No special create flags
220 FILE_ATTRIBUTE_SYSTEM,
221 NULL); // No template file
222
223 if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
224 {
225 usbMonStartService();
226
227 g_hUSBMonitor = CreateFile (USBMON_DEVICE_NAME,
228 GENERIC_READ | GENERIC_WRITE,
229 FILE_SHARE_READ | FILE_SHARE_WRITE,
230 NULL, // no SECURITY_ATTRIBUTES structure
231 OPEN_EXISTING, // No special create flags
232 FILE_ATTRIBUTE_SYSTEM,
233 NULL); // No template file
234
235 if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
236 {
237 /* AssertFailed(); */
238 printf("usbproxy: Unable to open filter driver!! (rc=%d)\n", GetLastError());
239 rc = VERR_FILE_NOT_FOUND;
240 goto failure;
241 }
242 }
243
244 /*
245 * Check the version
246 */
247 cbReturned = 0;
248 if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_VERSION, NULL, 0,&version, sizeof(version), &cbReturned, NULL))
249 {
250 printf("usbproxy: Unable to query filter version!! (rc=%d)\n", GetLastError());
251 rc = VERR_VERSION_MISMATCH;
252 goto failure;
253 }
254
255 if (version.u32Major != USBMON_MAJOR_VERSION ||
256 version.u32Minor < USBMON_MINOR_VERSION)
257 {
258 printf("usbproxy: Filter driver version mismatch!!\n");
259 rc = VERR_VERSION_MISMATCH;
260 goto failure;
261 }
262
263 return VINF_SUCCESS;
264
265failure:
266 if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
267 {
268 CloseHandle(g_hUSBMonitor);
269 g_hUSBMonitor = INVALID_HANDLE_VALUE;
270 }
271 return rc;
272}
273
274
275
276/**
277 * Terminate the USB monitor
278 *
279 * @returns VBox status code.
280 */
281int usbMonitorTerm()
282{
283 if (g_hUSBMonitor != INVALID_HANDLE_VALUE)
284 {
285 CloseHandle(g_hUSBMonitor);
286 g_hUSBMonitor = INVALID_HANDLE_VALUE;
287 }
288 /*
289 * If we started the service we might consider stopping it too.
290 *
291 * Since this won't work unless the process starting it is the
292 * last user we might wanna skip this...
293 */
294 if (g_fStartedService)
295 {
296 usbMonStopService();
297 g_fStartedService = false;
298 }
299
300 return VINF_SUCCESS;
301}
302
303
304int __cdecl main(int argc, char **argv)
305{
306 int rc;
307
308 printf("USB test\n");
309
310 rc = usbMonitorInit();
311 AssertRC(rc);
312
313 void *pId1, *pId2;
314
315 usbMonInsertFilter("0529", "0514", "0100", &pId1);
316 usbMonInsertFilter("0A16", "2499", "0100", &pId2);
317
318 printf("Waiting to capture device\n");
319 getchar();
320
321 printf("Releasing device\n");
322 usbMonReleaseDevice(0xA16, 0x2499, 0x100);
323
324 usbMonRemoveFilter(pId1);
325 usbMonRemoveFilter(pId2);
326
327 rc = usbMonitorTerm();
328
329 return 0;
330}
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