-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmac-hid-dump.c
139 lines (113 loc) · 3.51 KB
/
mac-hid-dump.c
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
/*
* mac-hid-dump -- Dump HID Report Descriptors on MacOS
* Sort of a MacOS version of `usbhid-dump`
*
* Borrows heavily from libusb/hidapi/mac/hid.c
*
* 2021, Tod Kurt, [email protected]
*/
#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/IOKitLib.h>
#include <CoreFoundation/CoreFoundation.h>
static int32_t get_int_property(IOHIDDeviceRef device, CFStringRef key)
{
CFTypeRef ref;
int32_t value;
ref = IOHIDDeviceGetProperty(device, key);
if (ref) {
if (CFGetTypeID(ref) == CFNumberGetTypeID()) {
CFNumberGetValue((CFNumberRef) ref, kCFNumberSInt32Type, &value);
return value;
}
}
return 0;
}
static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t *buf, size_t len)
{
CFStringRef str;
if (!len)
return 0;
str = (CFStringRef) IOHIDDeviceGetProperty(device, prop);
buf[0] = 0;
if (str) {
CFIndex str_len = CFStringGetLength(str);
CFRange range;
CFIndex used_buf_len;
CFIndex chars_copied;
len --;
range.location = 0;
range.length = ((size_t) str_len > len)? len: (size_t) str_len;
chars_copied = CFStringGetBytes(str,
range,
kCFStringEncodingUTF32LE,
(char) '?',
FALSE,
(UInt8*)buf,
len * sizeof(wchar_t),
&used_buf_len);
if (chars_copied <= 0)
buf[0] = 0;
else
buf[chars_copied] = 0;
return 0;
}
else
return -1;
}
static unsigned short get_vendor_id(IOHIDDeviceRef device)
{
return get_int_property(device, CFSTR(kIOHIDVendorIDKey));
}
static unsigned short get_product_id(IOHIDDeviceRef device)
{
return get_int_property(device, CFSTR(kIOHIDProductIDKey));
}
static int get_serial_number(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDSerialNumberKey), buf, len);
}
static int get_manufacturer_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDManufacturerKey), buf, len);
}
static int get_product_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
{
return get_string_property(device, CFSTR(kIOHIDProductKey), buf, len);
}
int main(void)
{
IOHIDManagerRef mgr;
int i;
mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
IOHIDManagerSetDeviceMatching(mgr, NULL);
IOHIDManagerOpen(mgr, kIOHIDOptionsTypeNone);
CFSetRef device_set = IOHIDManagerCopyDevices(mgr);
CFIndex num_devices = CFSetGetCount(device_set);
IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
CFSetGetValues(device_set, (const void **) device_array);
//printf("got %ld devices\n", num_devices);
printf("mac-hid-dump:\n");
for (i = 0; i < num_devices; i++) {
IOHIDDeviceRef dev = device_array[i];
wchar_t str1[256], str2[256];
get_manufacturer_string( dev, str1, sizeof(str1) );
get_product_string( dev, str2, sizeof(str2) );
printf("%04hX %04hX: %ls - %ls\n",
get_vendor_id(dev), get_product_id(dev), str1, str2);
CFDataRef cfprop = (CFDataRef)IOHIDDeviceGetProperty(dev,
CFSTR(kIOHIDReportDescriptorKey));
printf("DESCRIPTOR:\n ");
uint8_t pbuf[1024];
if( cfprop != NULL) {
long len = CFDataGetLength(cfprop);
CFDataGetBytes( cfprop, CFRangeMake(0,len), pbuf);
for( int i=0; i< len; i++) {
printf("%02x ", pbuf[i]);
printf( (i % 16 == 15) ? "\n " : " ");
}
printf("\n (%ld bytes)\n", len);
}
}
return 0;
}