VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/darwin/testcase/tstOpenUSBDev.cpp@ 72986

Last change on this file since 72986 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: tstOpenUSBDev.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
3 * Testcase that attempts to locate and open the specfied device.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <mach/mach.h>
32#include <Carbon/Carbon.h>
33#include <IOKit/IOKitLib.h>
34#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
35#include <IOKit/scsi/SCSITaskLib.h>
36#include <mach/mach_error.h>
37#include <IOKit/usb/IOUSBLib.h>
38#include <IOKit/IOCFPlugIn.h>
39
40#include <iprt/mem.h>
41#include <iprt/string.h>
42#include <iprt/process.h>
43#include <iprt/assert.h>
44#include <iprt/thread.h>
45#include <iprt/getopt.h>
46#include <iprt/initterm.h>
47#include <iprt/stream.h>
48
49/**
50 * Gets an unsigned 32-bit integer value.
51 *
52 * @returns Success indicator (true/false).
53 * @param DictRef The dictionary.
54 * @param KeyStrRef The key name.
55 * @param pu32 Where to store the key value.
56 */
57static bool tstDictGetU32(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint32_t *pu32)
58{
59 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
60 if (ValRef)
61 {
62 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt32Type, pu32))
63 return true;
64 }
65 *pu32 = 0;
66 return false;
67}
68
69
70/**
71 * Gets an unsigned 64-bit integer value.
72 *
73 * @returns Success indicator (true/false).
74 * @param DictRef The dictionary.
75 * @param KeyStrRef The key name.
76 * @param pu64 Where to store the key value.
77 */
78static bool tstDictGetU64(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint64_t *pu64)
79{
80 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
81 if (ValRef)
82 {
83 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt64Type, pu64))
84 return true;
85 }
86 *pu64 = 0;
87 return false;
88}
89
90
91static int tstDoWork(io_object_t USBDevice, const char *argv0)
92{
93 /*
94 * Create a plugin interface for the device and query its IOUSBDeviceInterface.
95 */
96 int vrc = VINF_SUCCESS;
97 SInt32 Score = 0;
98 IOCFPlugInInterface **ppPlugInInterface = NULL;
99 IOReturn irc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
100 kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
101 if (irc == kIOReturnSuccess)
102 {
103 IOUSBDeviceInterface245 **ppDevI = NULL;
104 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
105 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
106 (LPVOID *)&ppDevI);
107 irc = IODestroyPlugInInterface(ppPlugInInterface); Assert(irc == kIOReturnSuccess);
108 ppPlugInInterface = NULL;
109 if (hrc == S_OK)
110 {
111 /*
112 * Try open the device for exclusive access.
113 */
114 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
115 if (irc == kIOReturnExclusiveAccess)
116 {
117 RTThreadSleep(20);
118 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
119 }
120 if (irc == kIOReturnSuccess)
121 {
122#if 0
123 /*
124 * Re-enumerate the device and bail out.
125 */
126 irc = (*ppDevI)->USBDeviceReEnumerate(ppDevI, 0);
127 if (irc != kIOReturnSuccess)
128 {
129 vrc = RTErrConvertFromDarwinIO(irc);
130 RTPrintf("%s: Failed to re-enumerate the device, irc=%#x (vrc=%Rrc).\n", argv0, irc, vrc);
131 }
132#endif
133
134 (*ppDevI)->USBDeviceClose(ppDevI);
135 }
136 else if (irc == kIOReturnExclusiveAccess)
137 {
138 vrc = VERR_SHARING_VIOLATION;
139 RTPrintf("%s: The device is being used by another process (irc=kIOReturnExclusiveAccess)\n", argv0);
140 }
141 else
142 {
143 vrc = VERR_OPEN_FAILED;
144 RTPrintf("%s: Failed to open the device, irc=%#x (vrc=%Rrc).\n", argv0, irc, vrc);
145 }
146 }
147 else
148 {
149 vrc = VERR_OPEN_FAILED;
150 RTPrintf("%s: Failed to create plugin interface for the device, hrc=%#x (vrc=%Rrc).\n", argv0, hrc, vrc);
151 }
152
153 (*ppDevI)->Release(ppDevI);
154 }
155 else
156 {
157 vrc = RTErrConvertFromDarwinIO(irc);
158 RTPrintf("%s: Failed to open the device, plug-in creation failed with irc=%#x (vrc=%Rrc).\n", argv0, irc, vrc);
159 }
160
161 return vrc;
162}
163
164
165static int tstSyntax(const char *argv0)
166{
167 RTPrintf("syntax: %s [criteria]\n"
168 "\n"
169 "Criteria:\n"
170 " -l <location>\n"
171 " -s <session>\n"
172 , argv0);
173 return 1;
174}
175
176
177int main(int argc, char **argv)
178{
179 RTR3InitExe(argc, &argv, 0);
180
181 /*
182 * Show help if not arguments.
183 */
184 if (argc <= 1)
185 return tstSyntax(argv[0]);
186
187 /*
188 * Parse arguments.
189 */
190 static const RTGETOPTDEF g_aOptions[] =
191 {
192 { "--location", 'l', RTGETOPT_REQ_UINT32 },
193 { "--session", 's', RTGETOPT_REQ_UINT64 },
194 };
195
196 kern_return_t krc;
197 uint64_t u64SessionId = 0;
198 uint32_t u32LocationId = 0;
199
200 int ch;
201 RTGETOPTUNION ValueUnion;
202 RTGETOPTSTATE GetState;
203 RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0 /* fFlags */);
204 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
205 {
206 switch (ch)
207 {
208 case 'l':
209 u32LocationId = ValueUnion.u32;
210 break;
211 case 's':
212 u64SessionId = ValueUnion.u64;
213 break;
214 case 'h':
215 return tstSyntax(argv[0]);
216 case 'V':
217 RTPrintf("$Revision: 69500 $\n");
218 return 0;
219
220 default:
221 return RTGetOptPrintError(ch, &ValueUnion);
222 }
223 }
224
225 /*
226 * Open the master port.
227 */
228 mach_port_t MasterPort = MACH_PORT_NULL;
229 krc = IOMasterPort(MACH_PORT_NULL, &MasterPort);
230 if (krc != KERN_SUCCESS)
231 {
232 RTPrintf("%s: IOMasterPort -> %x\n", argv[0], krc);
233 return 1;
234 }
235
236 /*
237 * Iterate the USB devices and find all that matches.
238 */
239 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
240 if (!RefMatchingDict)
241 {
242 RTPrintf("%s: IOServiceMatching failed\n", argv[0]);
243 return 1;
244 }
245
246 io_iterator_t USBDevices = IO_OBJECT_NULL;
247 IOReturn irc = IOServiceGetMatchingServices(MasterPort, RefMatchingDict, &USBDevices);
248 if (irc != kIOReturnSuccess)
249 {
250 RTPrintf("%s: IOServiceGetMatchingServices -> %#x\n", argv[0], irc);
251 return 1;
252 }
253 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
254
255 unsigned cDevices = 0;
256 unsigned cMatches = 0;
257 io_object_t USBDevice;
258 while ((USBDevice = IOIteratorNext(USBDevices)))
259 {
260 cDevices++;
261 CFMutableDictionaryRef PropsRef = 0;
262 krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
263 if (krc == KERN_SUCCESS)
264 {
265 uint64_t u64CurSessionId;
266 uint32_t u32CurLocationId;
267 if ( ( !u64SessionId
268 || ( tstDictGetU64(PropsRef, CFSTR("sessionID"), &u64CurSessionId)
269 && u64CurSessionId == u64SessionId))
270 && ( !u32LocationId
271 || ( tstDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32CurLocationId)
272 && u32CurLocationId == u32LocationId))
273 )
274 {
275 cMatches++;
276 CFRelease(PropsRef);
277 tstDoWork(USBDevice, argv[0]);
278 }
279 else
280 CFRelease(PropsRef);
281 }
282 IOObjectRelease(USBDevice);
283 }
284 IOObjectRelease(USBDevices);
285
286 /*
287 * Bitch if we didn't find anything matching the criteria.
288 */
289 if (!cMatches)
290 RTPrintf("%s: No matching devices found from a total of %d.\n", argv[0], cDevices);
291 return !cMatches;
292}
293
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