-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheject_v2.cpp
172 lines (139 loc) · 4.37 KB
/
eject_v2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <CoreServices/CoreServices.h>
#include <CoreFoundation/CFString.h>
#include <IOKit/IOKitLib.h>
#include <mach/mach_port.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFBundle.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/IOMessage.h>
#include <IOKit/usb/IOUSBLib.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
typedef unsigned int UINT;
bool IoRegistryGetProperty(io_service_t io_service, CFStringRef cfPropertyName,
UInt32 &lProperty)
{
bool bResult = false;
// Get property from io-registry
CFTypeRef cfTypeReference = IORegistryEntryCreateCFProperty(io_service, cfPropertyName, kCFAllocatorDefault, kNilOptions);
// Convert property to UInt32
if( cfTypeReference )
{
bResult = CFNumberGetValue( (CFNumberRef)cfTypeReference, kCFNumberSInt32Type, &lProperty);
CFRelease( cfTypeReference );
}
return bResult;
}
bool IoRegistryGetProperty(io_service_t io_service, CFStringRef cfPropertyName, char *prop)
{
bool bResult = false;
// Get property from io-registry
CFTypeRef cfTypeReference = IORegistryEntryCreateCFProperty(io_service, cfPropertyName,
kCFAllocatorDefault, kNilOptions);
if (!cfTypeReference)
return false;
CFStringGetCString((CFStringRef)cfTypeReference, prop, 1024, kCFStringEncodingUTF8);
CFRelease(cfTypeReference);
return true;
}
static UINT GetUsbLocation(io_service_t usb)
{
UINT locationID = 0;
IoRegistryGetProperty(usb, CFSTR(kUSBDevicePropertyLocationID), locationID);
return locationID;
}
static io_service_t GetUsbService(UINT uLocationId)
{
io_iterator_t deviceIterator = 0;
io_service_t usbDevice = 0;
CFMutableDictionaryRef dictRef = IOServiceMatching(kIOUSBDeviceClassName/*"IOUSBHostDevice"*/);
IOServiceGetMatchingServices(kIOMasterPortDefault, dictRef, &deviceIterator);
while (IOIteratorIsValid(deviceIterator)) {
usbDevice = IOIteratorNext(deviceIterator);
if (!usbDevice)
break;
if (GetUsbLocation(usbDevice) == uLocationId)
break;
IOObjectRelease(usbDevice);
usbDevice = 0;
}
IOObjectRelease(deviceIterator);
return usbDevice;
}
static void dump_usb()
{
io_iterator_t deviceIterator = 0;
CFMutableDictionaryRef dictRef = IOServiceMatching(kIOUSBDeviceClassName);
IOServiceGetMatchingServices(kIOMasterPortDefault, dictRef, &deviceIterator);
while (IOIteratorIsValid(deviceIterator)) {
io_service_t usbDevice = IOIteratorNext(deviceIterator);
if (!usbDevice)
break;
char sFriendlyName[1024];
IoRegistryGetProperty( usbDevice, CFSTR("USB Product Name"), sFriendlyName);
printf("0x%08x %s\n", GetUsbLocation(usbDevice), sFriendlyName);
IOObjectRelease(usbDevice);
usbDevice = 0;
}
IOObjectRelease(deviceIterator);
}
static IOReturn GetUSBDeviceInterface(io_service_t usbDevice, IOUSBDeviceInterface650 ***iface)
{
IOReturn ret;
SInt32 score = 0;
IOCFPlugInInterface **plugInInterface = NULL;
IOCFPlugInInterface ** interface;
IUnknownVTbl ** iunknown;
ret = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID, &plugInInterface, &score);
if (ret != kIOReturnSuccess || !plugInInterface) {
printf("Failed to create PluginInterface: 0x%x\n", ret);
return ret;
}
(*plugInInterface)->QueryInterface(plugInInterface,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID650),
(LPVOID*)iface);
(*plugInInterface)->Release(plugInInterface);
return 0;
}
int main(int argc, char *argv[])
{
kern_return_t r;
IOUSBDeviceInterface650 **iface;
if (argc < 2) {
dump_usb();
return 0;
}
char *endptr;
UINT uLocationId = strtol(argv[1], &endptr, 0);
printf("searching for location 0x%08x\n", uLocationId);
io_service_t usbDevice = GetUsbService(uLocationId);
if (0 == usbDevice) {
printf("not found\n");
return -1;
}
bool do_release = false;
if (argc > 2 && 0 == strcmp(argv[2], "release"))
do_release = true;
//r = IOServiceAuthorize(usbDevice, kIOServiceInteractionAllowed);
//assert(0 == r);
r = GetUSBDeviceInterface(usbDevice, &iface);
if (0 != r) {
printf("Failed to get device interface: 0x%x\n", r);
return -1;
}
IOObjectRelease(usbDevice);
if (do_release) {
printf("release device\n");
r = (*iface)->USBDeviceReEnumerate(iface,
kUSBReEnumerateReleaseDeviceMask);
}
else {
printf("capture device\n");
r = (*iface)->USBDeviceReEnumerate(iface,
kUSBReEnumerateCaptureDeviceMask);
}
printf("reenumerate result 0x%x\n", r);
return 0;
}