VirtualBox

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

Last change on this file since 83531 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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