-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbbox_macosx.c
392 lines (328 loc) · 10.9 KB
/
usbbox_macosx.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* usbbox_macosx.c
* lib-test
*
* Created by John Montgomery on 19/01/2008.
* Copyright 2008 ioLab. All rights reserved.
*
*/
#include "usbbox.h"
#include <stdio.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/hid/IOHIDLib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <pthread.h>
#include <time.h>
#define EXPORT __attribute__((visibility("default")))
#define READ_BUFFER_SIZE 338
typedef struct {
usb_box_message* msgs;
int capacity;
int length;
} message_queue;
typedef struct {
int is_open;
io_object_t dev;
IOHIDDeviceInterface122 **interface;
char buffer[READ_BUFFER_SIZE];
message_queue queue;
pthread_t read_thread;
pthread_mutex_t mutex;
} usb_box_macosx;
static void queue_allocate(message_queue* queue) {
queue->capacity=100;
queue->length=0;
queue->msgs=malloc(sizeof(usb_box_message)*queue->capacity);
}
static void queue_free(message_queue* queue) {
free(queue->msgs);
}
static void queue_push(message_queue* queue, usb_box_message msg) {
if ( queue->length >= queue->capacity ) {
/* resize queue to bee big enough */
int new_capacity = queue->capacity * 2;
usb_box_message* new_msgs = realloc(queue->msgs,sizeof(usb_box_message)*new_capacity);
if ( new_msgs ) {
queue->msgs = new_msgs;
queue->capacity = new_capacity;
}
else {
/* can't re-allocate any memory */
perror("could not increase size of message queue - message lost");
return;
}
}
/* copy message onto end of queue */
memcpy(*(queue->msgs + queue->length), msg, sizeof(usb_box_message));
queue->length++;
}
static int queue_pop(message_queue* queue, usb_box_message* msg_ptr) {
if ( queue->length <= 0 ) {
return 0;
}
/* copy message from front of queue */
memcpy(*msg_ptr, *queue->msgs, sizeof(usb_box_message));
queue->length--;
/* move messages down by 1 */
queue->msgs = memmove(queue->msgs,queue->msgs+1,queue->length);
return 1;
}
/* sync with mutex when popping and pushing messages onto boxes queue. */
static int pop_msg(usb_box_macosx* box, usb_box_message* msg_ptr) {
int result = -1;
pthread_mutex_lock(&(box->mutex));
if ( queue_pop(&(box->queue),msg_ptr) ) {
result=0;
}
pthread_mutex_unlock(&(box->mutex));
return result;
}
static void push_msg(usb_box_macosx* box, usb_box_message msg) {
pthread_mutex_lock(&(box->mutex));
queue_push(&(box->queue),msg);
pthread_mutex_unlock(&(box->mutex));
}
static int _last_error = 0;
static void set_last_error(int err) {
_last_error=err;
}
static void message_callback(void *arg, IOReturn result, void *refcon, void *sender, uint32_t size) {
usb_box_macosx* box = (usb_box_macosx*)arg;
usb_box_message msg;
memcpy(msg, box->buffer, sizeof(usb_box_message));
push_msg(box, msg);
}
/*main message loop*/
static void *read_messages(void* arg) {
CFRunLoopSourceRef eventSource;
mach_port_t port;
SInt32 reason;
usb_box_macosx* box = (usb_box_macosx*)arg;
(*(box->interface))->createAsyncPort(box->interface, &port);
(*(box->interface))->createAsyncEventSource(box->interface, &eventSource);
(*(box->interface))->setInterruptReportHandlerCallback(
box->interface,
box->buffer,
READ_BUFFER_SIZE,
message_callback,
box,
NULL);
(*(box->interface))->startAllQueues(box->interface);
CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
while( box->is_open ) {
pthread_testcancel();
reason = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, 0);
}
return 0;
}
static void open_box(usb_box_macosx* box) {
SInt32 score;
IOCFPlugInInterface **plugInInterface;
IOCreatePlugInInterfaceForService(
box->dev,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface,
&score
);
(*plugInInterface)->QueryInterface(
plugInInterface,
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
(LPVOID)&(box->interface)
);
(*plugInInterface)->Release(plugInInterface);
(*(box->interface))->open(box->interface, 0);
queue_allocate(&box->queue);
pthread_mutex_init(&(box->mutex),NULL);
box->is_open=1;
pthread_create(&(box->read_thread), NULL, read_messages, box);
}
static void close_box(usb_box_macosx* box) {
/* TODO halt threads etc */
box->is_open = 0;
pthread_cancel(box->read_thread);
pthread_join(box->read_thread, NULL);
pthread_mutex_destroy(&(box->mutex));
queue_free(&box->queue);
(*(box->interface))->close(box->interface);
}
/*
* return the last known error that occurred when performing
* a call to the box
*/
EXPORT int usb_box_last_error(void) {
return _last_error;
}
/*
* find the usb box and return a device handle for it or NULL if not such device found (or
* an error occurs)
* a successful call to usb_box_open must be accompanied by a call
* to usb_box_close when the device has been finished with
*/
EXPORT usb_box usb_box_open(void) {
IOReturn result = kIOReturnSuccess;
io_iterator_t hidObjectIterator = 0;
io_object_t hidDevice = IO_OBJECT_NULL;
int found = 0;
CFMutableDictionaryRef hidMatchDictionary = 0;
CFMutableDictionaryRef hidProperties = 0;
hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
result = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
if ((result != kIOReturnSuccess) || (hidObjectIterator == 0)) {
perror("Can't obtain an IO iterator\n");
set_last_error(-1);
return NULL;
}
while ((hidDevice = IOIteratorNext(hidObjectIterator))) {
hidProperties = 0;
int vendor = 0, product = 0;
result = IORegistryEntryCreateCFProperties(hidDevice, &hidProperties, kCFAllocatorDefault, kNilOptions);
if ((result == KERN_SUCCESS) && hidProperties) {
CFNumberRef vendorRef, productRef;
vendorRef = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDVendorIDKey));
productRef = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductIDKey));
if (vendorRef) {
CFNumberGetValue(vendorRef, kCFNumberIntType, &vendor);
CFRelease(vendorRef);
}
if (productRef) {
CFNumberGetValue(productRef, kCFNumberIntType, &product);
CFRelease(productRef);
}
}
if ( vendor == USB_BOX_VENDOR_ID && product == USB_BOX_PRODUCT_ID ) {
found=1;
break;
}
else {
IOObjectRelease(hidDevice);
}
}
IOObjectRelease(hidObjectIterator);
/* no error either found box ok or didn't */
set_last_error(0);
if ( found ) {
usb_box_macosx* box = malloc(sizeof(usb_box_macosx));
box->is_open=0;
box->dev = hidDevice;
open_box(box);
return (usb_box)box;
}
return NULL;
}
/*
* close the usb box handle and free up any resource associated with it.
*/
EXPORT void usb_box_close(usb_box box) {
if ( box ) {
usb_box_macosx* osx_box = (usb_box_macosx*)box;
if ( osx_box->is_open ) {
/* close the device */
close_box(osx_box);
}
if ( osx_box->dev != IO_OBJECT_NULL ) {
/* release the box and null the reference */
IOObjectRelease(osx_box->dev);
osx_box->dev = IO_OBJECT_NULL;
}
free(box);
}
}
/*
* send a message to the box (8 bytes)
* 0 indicates the message was sent ok.
* any other value indicates an error
*/
EXPORT int usb_box_write(usb_box box, usb_box_message msg) {
if ( box ) {
usb_box_macosx* osx_box = (usb_box_macosx*)box;
(*(osx_box->interface))->setReport(
osx_box->interface, // self
kIOHIDReportTypeOutput, // report type
0, // report ID
msg, // buffer
sizeof(msg), // size
100, // timeout (in ms)
0, // callback function
0, // ... and arguments
0
);
}
return 0;
}
long time_diff_micro(struct timeval begin, struct timeval end) {
long sec_diff = end.tv_sec - begin.tv_sec;
long usec_diff = end.tv_usec - begin.tv_usec;
long total_usec_diff = sec_diff*1000000 + usec_diff;
return total_usec_diff;
}
/*
* try to read a message from the box.
* use a timeout of 0 to return immediately (polling).
*
* 0 indicates the message was read successfully within
* timeout milliseconds into the usb_box_message passed into
* the function.
* -1 indicates no message was read within the timeout.
* any other return value indicates an error.
*/
EXPORT int usb_box_read(usb_box box, usb_box_message* msg_ptr, int timeout) {
if ( box ) {
usb_box_macosx* osx_box = (usb_box_macosx*)box;
int result = -1, repeat=0;
struct timeval begin, end;
do {
result = pop_msg(osx_box, msg_ptr);
if ( result != 0 ) {
/* nothing on the queue */
if ( timeout <= 0 ) {
/* special case, no timeout, so just quit
the loop straight away. */
break;
}
/*otherwise we'll update the time and sleep for a little while*/
if (!repeat) {
/* first time through */
gettimeofday(&begin, NULL);
}
gettimeofday(&end, NULL);
/* yield */
usleep(100);
repeat++;
}
}
while( result != 0 && time_diff_micro(begin,end) < (timeout*1000) );
return result;
}
return -1;
}
int usb_box_sprintf_message(char* str, usb_box_message msg) {
int i;
int bytes[USB_BOX_MESSAGE_SIZE];
for ( i = 0; i < USB_BOX_MESSAGE_SIZE; i++ ) {
bytes[i]=msg[i];
}
return sprintf(str, "0x%X,0x%X,0x%X,0x%X,0x%X,0x%X,0x%X,0x%X",bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7]);
}
/* insert data into msg_ptr based on string representation (8 c-style 2-digit hex numbers) separated by commas*/
int usb_box_sscanf_message(const char* str, usb_box_message* msg_ptr) {
int i;
int bytes[USB_BOX_MESSAGE_SIZE];
int res = sscanf(
str,
"0x%2X,0x%2X,0x%2X,0x%2X,0x%2X,0x%2X,0x%2X,0x%2X",
&(bytes[0]),
&(bytes[1]),
&(bytes[2]),
&(bytes[3]),
&(bytes[4]),
&(bytes[5]),
&(bytes[6]),
&(bytes[7])
);
for ( i = 0; i < USB_BOX_MESSAGE_SIZE; i++ ) {
(*msg_ptr)[i]=bytes[i];
}
return res;
}