-
Notifications
You must be signed in to change notification settings - Fork 2
/
IOWarriorLib.c
857 lines (727 loc) · 30.8 KB
/
IOWarriorLib.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
* IOWarriorLib.c
* IOWarriorHIDTest
*
* Created by ilja on Sun Dec 29 2002.
* $Id: IOWarriorLib.c,v 1.7 2004/11/22 16:33:44 ilja Exp $
*
*/
#include "IOWarriorLib.h"
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
// uncomment next line to see IOWarriorLib debug messages
//#define IOWarriorDebug 1
//Global variables
static IONotificationPortRef gNotifyPort;
static io_iterator_t gIOWarriorAddedIter;
static io_iterator_t gIOWarriorRemovedIter;
static IOWarriorListNode* gIOWarriorList; // linked list of IOWarrior interfaces already discovered
static short gIOWarriorListDirty; // is the IOWarrior interface list list up-to-date or dirty
IOWarriorDeviceCallbackFunctionPtr gIOWarriorCallBackPtr;
void* gIOWarriorCallBackRefCon;
// Prototype declarations
void IOWarriorAdded(void *refCon, io_iterator_t iterator);
void IOWarriorRemoved(void *refCon, io_iterator_t iterator);
int IOWarriorWrite (int inWarriorIndex, int inInterfaceIndex, int inSize, void* inData);
int IOWarriorRead (int inWarriorIndex, int inInterfaceIndex, int inReportID, int inSize, void* outData);
io_iterator_t IOWarriorFindHIDDevices (const mach_port_t masterPort, UInt32 usagePage, UInt32 usage);
void IOWarriorDiscoverInterfaces ();
CFMutableDictionaryRef IOWarriorSetUpHIDMatchingDictionary (UInt32 usagePage, UInt32 usage);
IOWarriorHIDDeviceInterface** IOWarriorCreateHIDDeviceInterface (io_object_t hidDevice);
void byteSwap (void* ioData, int inCount);
void IOWarriorAddInterfaceToList (IOWarriorHIDDeviceInterface** inInterface,
int inInterfaceType, CFStringRef inDeviceSerialNumber);
void IOWarriorClearInterfaceList ();
IOWarriorHIDDeviceInterface** IOWarriorGetInterface (int inWarriorIndex,int inInterfaceIndex);
IOWarriorListNode* IOWarriorGetInterfaceListNode (int inWarriorIndex,int inInterfaceIndex);
void IOWarriorRebuildInterfaceList ();
// Debug utilities
void PrintNotificationMessage (char* msg);
void PrintErrMsg (char * msg);
void PrintErrMsgIfIOErr (long expr, char * msg);
IONotificationPortRef GetNotificationPort ()
{
return gNotifyPort;
}
int IOWarriorInit ()
{
SInt32 usbVendor = kIOWarriorVendorID;
SInt32 usbProduct = kIOWarrior40DeviceID;
kern_return_t result;
mach_port_t masterPort;
CFMutableDictionaryRef matchingDict;
CFRunLoopSourceRef runLoopSource;
gIOWarriorList = NULL;
gIOWarriorListDirty = 1;
gIOWarriorCallBackPtr = NULL;
//Create a master port for communication with the I/O Kit
result = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (result || !masterPort)
{
return -1;
}
//To set up asynchronous notifications, create a notification port and
//add its run loop event source to the programs run loop
gNotifyPort = IONotificationPortCreate(masterPort);
runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
kCFRunLoopDefaultMode);
//Set up matching dictionary for class IOUSBDevice and its subclasses
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict)
{
//printf("Couldn't create a USB matching dictionary\n");
mach_port_deallocate(mach_task_self(), masterPort);
return -1;
}
//Add the vendor and product IDs to the matching dictionary
//This is the second key of the first table in the USB Common Class
//Specification
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
//Now set up two notifications: one to be called when a raw device
//is first matched by the I/O Kit and another to be called when the
//device is terminated
//Notification of first match
CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification, matchingDict,
IOWarriorAdded, NULL, &gIOWarriorAddedIter);
//Iterate over set of matching devices to access already-present devices
//and to arm the notification
IOWarriorAdded(NULL, gIOWarriorAddedIter);
//Notification of termination
CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOTerminatedNotification, matchingDict,
IOWarriorRemoved, NULL, &gIOWarriorRemovedIter);
//Iterate over set of matching devices to release each one and to
//arm the notification
IOWarriorRemoved(NULL, gIOWarriorRemovedIter);
CFRelease(matchingDict);
// do the same for IOWarrior24 devices
usbProduct = kIOWarrior24DeviceID;
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
CFRetain(matchingDict);
CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification, matchingDict,
IOWarriorAdded, NULL, &gIOWarriorAddedIter);
IOWarriorAdded(NULL, gIOWarriorAddedIter);
matchingDict = (CFMutableDictionaryRef) CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOTerminatedNotification, matchingDict,
IOWarriorRemoved, NULL, &gIOWarriorRemovedIter);
IOWarriorRemoved(NULL, gIOWarriorRemovedIter);
CFRelease(matchingDict);
// do the same for IOWarrior56 devices
usbProduct = kIOWarrior56DeviceID;
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
CFRetain(matchingDict);
CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification, matchingDict,
IOWarriorAdded, NULL, &gIOWarriorAddedIter);
IOWarriorAdded(NULL, gIOWarriorAddedIter);
matchingDict = (CFMutableDictionaryRef) CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOTerminatedNotification, matchingDict,
IOWarriorRemoved, NULL, &gIOWarriorRemovedIter);
IOWarriorRemoved(NULL, gIOWarriorRemovedIter);
CFRelease(matchingDict);
// do the same for IOWarrior24PV devices
usbProduct = kIOWarrior24PVDeviceID;
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductName),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
CFRetain(matchingDict);
CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification, matchingDict,
IOWarriorAdded, NULL, &gIOWarriorAddedIter);
IOWarriorAdded(NULL, gIOWarriorAddedIter);
matchingDict = (CFMutableDictionaryRef) CFRetain(matchingDict);
result = IOServiceAddMatchingNotification(gNotifyPort,
kIOTerminatedNotification, matchingDict,
IOWarriorRemoved, NULL, &gIOWarriorRemovedIter);
IOWarriorRemoved(NULL, gIOWarriorRemovedIter);
CFRelease(matchingDict);
//Finished with master port
mach_port_deallocate(mach_task_self(), masterPort);
return 0;
}
// Returns 1 if at least one IOWarrior is connected to this system. Returns 0 if no IOWarrior device could be discovered.
int IOWarriorIsPresent ()
{
return (IOWarriorCountInterfaces () > 0);
}
// Returns the number of IOWarriors connected to this system
int IOWarriorCountInterfaces ()
{
int result = 0;
IOWarriorListNode* theNode;
if (gIOWarriorListDirty)
{
IOWarriorRebuildInterfaceList ();
}
theNode = gIOWarriorList;
while (NULL != theNode)
{
result++;
theNode = theNode->nextNode;
}
return result;
}
// writes an 4 byte buffer to interface 0 of the first discovered IOWarrior40
int IOWarriorWriteInterface0 (void *inData)
{
IOWarriorHIDDeviceInterface** interface = IOWarriorFirstInterfaceOfType (kIOWarrior40Interface0);
if (interface)
{
return IOWarriorWriteToInterface (interface, 4, inData);
}
return -1;
}
// reads 4 byte from interface 0 of the first discovered IOWarrior40
int IOWarriorReadInterface0 (void *outData)
{
IOWarriorHIDDeviceInterface** interface = IOWarriorFirstInterfaceOfType (kIOWarrior40Interface0);
if (interface)
{
return IOWarriorReadFromInterface (interface, 0, 4, outData);
}
return -1;
}
// writes an 7 byte buffer to interface 1 of the first discovered IOWarrior40. Since passing simply 7 bytes and an reportID fails when doing the setReport call in IOWarriorWrite, reportID and data are merged into a single buffer which is passed to IOWarriorWrite using reportID 0
int IOWarriorWriteInterface1 (int inReportID, void *inData)
{
IOWarriorHIDDeviceInterface** interface = IOWarriorFirstInterfaceOfType (kIOWarrior40Interface1);
if (interface)
{
char buffer[8];
buffer[0] = inReportID;
memcpy (&buffer[1], inData, 7);
return IOWarriorWriteToInterface (interface, 8, buffer);
}
return -1;
}
// reads 7 bytes from interface 1 of the first discovered IOWarrior
int IOWarriorReadInterface1 (int inReportID, void *outData)
{
IOWarriorHIDDeviceInterface** interface = IOWarriorFirstInterfaceOfType (kIOWarrior40Interface1);
if (interface)
{
return IOWarriorReadFromInterface (interface, inReportID, 7, outData);
}
return -1;
}
// write inSize bytes at inData to interface inInterfaceIndex of device inWarriorIndex.
int IOWarriorWrite (int inWarriorIndex, int inInterfaceIndex, int inSize, void* inData)
{
IOWarriorHIDDeviceInterface** interface;
IOReturn ioReturnValue = kIOReturnSuccess;
interface = IOWarriorGetInterface (inWarriorIndex, inInterfaceIndex);
if (interface)
{
ioReturnValue = IOWarriorWriteToInterface (interface, inSize, inData);
}
else
return -1;
return ioReturnValue;
}
int IOWarriorWriteToInterface (IOWarriorHIDDeviceInterface** inInterface, int inSize, void* inData)
{
IOReturn ioReturnValue = kIOReturnSuccess;
// Open interface
ioReturnValue = IOWarriorOpenInterfaceIfNecessary (inInterface);
if (ioReturnValue != kIOReturnSuccess)
{
PrintErrMsgIfIOErr (ioReturnValue, "Could not open hid device interface");
(*inInterface)->Release (inInterface);
return ioReturnValue;
}
ioReturnValue = (*inInterface)->setReport (inInterface, kIOHIDReportTypeOutput, 0, inData, inSize, 50, NULL, NULL, NULL);
if (ioReturnValue != kIOReturnSuccess)
{
PrintErrMsgIfIOErr (ioReturnValue, "Could not write setReport on hid device interface");
return ioReturnValue;
}
return kIOReturnSuccess;
}
int IOWarriorSetInterruptCallback (IOWarriorHIDDeviceInterface** inInterface, void* inBuffer, UInt32 inBufferSize,
IOHIDReportCallbackFunction inCallbackPtr, void* inRefCon)
{
#ifdef kIOHIDDeviceInterfaceID122
int ioReturnValue;
CFRunLoopSourceRef sourceRef;
mach_port_t port;
CFRunLoopRef rl;
ioReturnValue = (*inInterface)->open (inInterface, 0);
ioReturnValue = (*inInterface)->createAsyncPort (inInterface, &port);
ioReturnValue = (*inInterface)->createAsyncEventSource (inInterface, &sourceRef);
rl = CFRunLoopGetCurrent ();
CFRunLoopAddSource (rl, sourceRef, kCFRunLoopDefaultMode);
ioReturnValue = (*inInterface)->setInterruptReportHandlerCallback (inInterface, inBuffer, inBufferSize, inCallbackPtr, inBuffer, inRefCon);
return ioReturnValue;
#else
return -1;
#endif
}
int IOWarriorReadFromInterface (IOWarriorHIDDeviceInterface** inInterface, int inReportID, int inSize, void* outData)
{
IOReturn ioReturnValue = kIOReturnSuccess;
UInt32 dataSize = inSize;
ioReturnValue = IOWarriorOpenInterfaceIfNecessary (inInterface);
if (ioReturnValue != kIOReturnSuccess)
{
PrintErrMsgIfIOErr (ioReturnValue, "Could not open hid device interface");
return ioReturnValue;
}
// read from interface
ioReturnValue = (*inInterface)->getReport (inInterface, kIOHIDReportTypeInput,
inReportID, outData, &dataSize, 100, NULL, NULL, NULL);
if (ioReturnValue != kIOReturnSuccess)
{
PrintErrMsgIfIOErr (ioReturnValue, "Could not call getReport on hid device interface");
return ioReturnValue;
}
return ioReturnValue;
}
IOReturn IOWarriorOpenInterfaceIfNecessary (IOWarriorHIDDeviceInterface** inInterface)
{
IOWarriorListNode *listNode;
IOReturn ioReturnValue = kIOReturnSuccess;
listNode = IOWarriorListNodeForInterface (inInterface);
if (false == listNode->interfaceOpen)
{
ioReturnValue = (*inInterface)->open (inInterface, 0);
if (ioReturnValue != kIOReturnSuccess)
return ioReturnValue;
listNode->interfaceOpen = true;
}
return ioReturnValue;
}
IOReturn IOWarriorCloseInterfaceIfNecessary(IOWarriorHIDDeviceInterface** inInterface)
{
IOWarriorListNode *listNode;
IOReturn ioReturnValue = kIOReturnSuccess;
listNode = IOWarriorListNodeForInterface (inInterface);
if (true == listNode->interfaceOpen)
{
ioReturnValue = (*inInterface)->close (inInterface);
if (kIOReturnSuccess == ioReturnValue)
{
listNode->interfaceOpen = false;
}
}
return ioReturnValue;
}
IOWarriorListNode* IOWarriorListNodeForInterface (IOWarriorHIDDeviceInterface** inInterface)
{
IOWarriorListNode *currentNode = gIOWarriorList;
while (currentNode)
{
if (currentNode->ioWarriorHIDInterface == inInterface )
{
return currentNode;
}
currentNode = currentNode->nextNode;
}
return nil;
}
// read inSize bytes from interface inInterfaceIndex of device inWarriorIndex to outData.
int IOWarriorRead (int inWarriorIndex, int inInterfaceIndex, int inReportID, int inSize, void* outData)
{
IOWarriorHIDDeviceInterface** interface;
IOReturn ioReturnValue = kIOReturnSuccess;
interface = IOWarriorGetInterface (inWarriorIndex, inInterfaceIndex);
if (interface)
{
ioReturnValue = IOWarriorReadFromInterface (interface, inReportID, inSize, outData);
}
else
return -1;
return ioReturnValue;
}
/* Returns a pointer to interface inInterfaceIndex of device inWarriorIndex, returns NULL if interface could not be found. */
IOWarriorListNode* IOWarriorGetInterfaceListNode (int inWarriorIndex,int inInterfaceIndex)
{
if (gIOWarriorListDirty)
{
IOWarriorRebuildInterfaceList ();
}
return IOWarriorInterfaceListNodeAtIndex ((2 * inWarriorIndex) + inInterfaceIndex);
}
/* Clears the list of interface nodes and rebuilds it. */
void IOWarriorRebuildInterfaceList ()
{
IOWarriorClearInterfaceList ();
IOWarriorDiscoverInterfaces ();
gIOWarriorListDirty = false;
}
/* Returns a pointer to interface inInterfaceIndex of device inWarriorIndex, returns NULL if interface could not be found. */
IOWarriorHIDDeviceInterface** IOWarriorGetInterface (int inWarriorIndex,int inInterfaceIndex)
{
IOWarriorListNode* node;
node = IOWarriorGetInterfaceListNode (inWarriorIndex, inInterfaceIndex);
if (node)
return node->ioWarriorHIDInterface;
return NULL;
}
/* Populates the linked list of known IOWarrior Interfaces. It makes the assumption that the hidObjectIterator object returns all interface 0 just before interface 1 for each connected IOWarrior. Caller is responsible for releasing interface by calling (*interface)->Release (interface). */
void IOWarriorDiscoverInterfaces ()
{
mach_port_t masterPort = nil;
io_iterator_t hidObjectIterator = nil;
IOReturn ioReturnValue;
IOWarriorHIDDeviceInterface** result = nil;
//NSLog (@"entering IOWarriorDiscoverInterfaces");
ioReturnValue = IOMasterPort (bootstrap_port, &masterPort);
if (ioReturnValue)
{
PrintErrMsgIfIOErr (ioReturnValue, "Couldn't create a master I/O Kit Port.");
return;
}
hidObjectIterator = IOWarriorFindHIDDevices (masterPort, kHIDPage_GenericDesktop, 0);
if (hidObjectIterator != nil)
{
io_object_t hidDevice = nil;
IOReturn ioReturnValue = kIOReturnSuccess;
int iteratorIndex= 0;
while ((hidDevice = IOIteratorNext (hidObjectIterator)))
{
kern_return_t err;
CFMutableDictionaryRef properties = 0;
//NSLog (@"iterating found device...");
//Create a CF dictionary representation of the I/O Registry entryÕs properties
//NSLog (@"before IORegistryEntryCreateCFProperties");
err = IORegistryEntryCreateCFProperties (hidDevice, &properties, kCFAllocatorDefault, kNilOptions);
//NSLog (@"after IORegistryEntryCreateCFProperties");
if ((err == KERN_SUCCESS) && properties)
{
CFTypeRef vendorIDRef;
CFTypeRef deviceIDRef;
CFTypeRef serialNumberRef;
vendorIDRef = CFDictionaryGetValue (properties, CFSTR(kIOHIDVendorIDKey));
deviceIDRef = CFDictionaryGetValue (properties, CFSTR(kIOHIDProductIDKey));
serialNumberRef = CFDictionaryGetValue (properties, CFSTR(kIOHIDSerialNumberKey));
if (vendorIDRef && deviceIDRef)
{
SInt16 vendorID = 0;
SInt16 deviceID = 0;
CFNumberGetValue (vendorIDRef, kCFNumberSInt16Type, &vendorID);
CFNumberGetValue (deviceIDRef, kCFNumberSInt16Type, &deviceID);
// if the HID Device is an IOWarrior
if (vendorID == kIOWarriorVendorID &&
(deviceID == kIOWarrior40DeviceID ||
deviceID == kIOWarrior24DeviceID ||
deviceID == kIOWarrior56DeviceID ||
deviceID == kIOWarrior24PVDeviceID))
{
PrintNotificationMessage ("found IOWarrior HID device\n");
result = IOWarriorCreateHIDDeviceInterface (hidDevice);
if (NULL != result)
{
int interfaceType;
CFStringRef mySerialNumberRef;
interfaceType = iteratorIndex % 2; // gives an IOWarrior40 interface
if (kIOWarrior24DeviceID == deviceID)
interfaceType += 2; // gives an IOWarrior24 interface
else if (kIOWarrior56DeviceID == deviceID)
interfaceType += 4; // compute interface type for IOWarrior 56
else if (kIOWarrior24PVDeviceID == deviceID)
interfaceType += 6;
mySerialNumberRef = NULL;
if (NULL == serialNumberRef)
{
// if device didn't supply us with a serial number, lets create our own
mySerialNumberRef = CFStringCreateWithCString (NULL, "0", kCFStringEncodingMacRoman);
}
else
{
mySerialNumberRef = CFStringCreateCopy (NULL, serialNumberRef);
}
IOWarriorAddInterfaceToList (result, interfaceType, serialNumberRef);
CFRelease (mySerialNumberRef);
iteratorIndex++;
}
}
else
PrintNotificationMessage ("HID device found, not IOWarrior\n");
}
CFRelease (properties);
}
ioReturnValue = IOObjectRelease (hidDevice);
}
IOObjectRelease (hidObjectIterator);
}
if (masterPort)
mach_port_deallocate (mach_task_self (), masterPort);
gIOWarriorListDirty = false; // mark interface list as up-to-data
//NSLog (@"leaving IOWarriorDiscoverInterfaces");
}
/* Returns an iterator object, which can be used to iterate through all hid devices available on the machine. You have to release the iterator after usage be calling IOObjectRelease (hidObjectIterator).*/
io_iterator_t IOWarriorFindHIDDevices (const mach_port_t masterPort, UInt32 usagePage, UInt32 usage)
{
CFMutableDictionaryRef hidMatchDictionary = NULL;
IOReturn ioReturnValue = kIOReturnSuccess;
io_iterator_t hidObjectIterator;
// Set up matching dictionary to search the I/O Registry for HID devices we are interested in. Dictionary reference is NULL if error.
//NSLog (@"entering IOWarriorFindHIDDevices");
hidMatchDictionary = IOWarriorSetUpHIDMatchingDictionary (usagePage, usage);
if (NULL == hidMatchDictionary)
{
PrintErrMsg ("Couldn't create a matching dictionary.");
return nil;
}
// Now search I/O Registry for matching devices.
ioReturnValue = IOServiceGetMatchingServices (masterPort, hidMatchDictionary, &hidObjectIterator);
// If error, print message and hang (for debugging purposes).
if ((ioReturnValue != kIOReturnSuccess) | (hidObjectIterator == nil))
{
PrintErrMsg ("Couldn't create a HID object iterator.");
return nil;
}
// IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref.
hidMatchDictionary = NULL;
//NSLog (@"leaving IOWarriorFindHIDDevices");
return hidObjectIterator;
}
// Called by IOKit when an IOWarrior was attached to the system
void IOWarriorAdded(void *refCon, io_iterator_t iterator)
{
io_service_t usbDevice;
while (usbDevice = IOIteratorNext(iterator))
{
PrintNotificationMessage ("Discovered IOWarrior device\n");
IOObjectRelease(usbDevice);
}
gIOWarriorListDirty = 1;
if (NULL != gIOWarriorCallBackPtr)
(*gIOWarriorCallBackPtr )(gIOWarriorCallBackRefCon);
}
// Called by IOKit when an IOWarrior was removed from the system
void IOWarriorRemoved(void *refCon, io_iterator_t iterator)
{
io_service_t usbDevice;
while (usbDevice = IOIteratorNext(iterator))
{
PrintNotificationMessage ("IOWarrior device removed\n");
IOObjectRelease(usbDevice);
}
gIOWarriorListDirty = 1;
if (NULL != gIOWarriorCallBackPtr)
(*gIOWarriorCallBackPtr )(gIOWarriorCallBackRefCon);
}
CFMutableDictionaryRef IOWarriorSetUpHIDMatchingDictionary (UInt32 usagePage, UInt32 usage)
{
CFNumberRef refUsage = NULL,
refUsagePage = NULL;
CFNumberRef vendorIDRef = NULL;
int vendorID = kIOWarriorVendorID;
CFMutableDictionaryRef refHIDMatchDictionary = NULL;
// Set up a matching dictionary to search I/O Registry by class name for all IOWarrior devices.
refHIDMatchDictionary = IOServiceMatching (kIOHIDDeviceKey);
if (refHIDMatchDictionary != NULL)
{
// Add key for device type (joystick, in this case) to refine the matching dictionary.
refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
CFDictionarySetValue (refHIDMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
CFDictionarySetValue (refHIDMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
// retained by dictionary
CFRelease (refUsage);
CFRelease (refUsagePage);
// Add key for vendor, ommit device id key so we see all IOWarrior Devices
vendorIDRef = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &vendorID);
CFDictionarySetValue (refHIDMatchDictionary, CFSTR (kIOHIDVendorIDKey), vendorIDRef);
// retained by dictionary
CFRelease (vendorIDRef);
}
else
PrintErrMsg ("Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
return refHIDMatchDictionary;
}
IOWarriorHIDDeviceInterface ** IOWarriorCreateHIDDeviceInterface (io_object_t hidDevice)
{
io_name_t className;
IOCFPlugInInterface** plugInInterface = NULL;
HRESULT plugInResult = S_OK;
SInt32 score = 0;
IOReturn ioReturnValue = kIOReturnSuccess;
IOWarriorHIDDeviceInterface** pphidDeviceInterface = NULL;
//NSLog (@"enterting IOWarriorCreateHIDDeviceInterface");
ioReturnValue = IOObjectGetClass (hidDevice, className);
PrintErrMsgIfIOErr (ioReturnValue, "Failed to get class name.");
PrintNotificationMessage ("Creating interface for device\n");
//NSLog (@" before IOCreatePlugInInterfaceForService" );
ioReturnValue = IOCreatePlugInInterfaceForService (hidDevice,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface,
&score);
//NSLog (@" after IOCreatePlugInInterfaceForService" );
if (ioReturnValue == kIOReturnSuccess)
{
// Call a method of the intermediate plug-in to create the device interface
//NSLog (@" before QueryInterface" );
plugInResult = (*plugInInterface)->QueryInterface (plugInInterface,
CFUUIDGetUUIDBytes (kIOWarriorHIDDeviceInterfaceID),
(void *) &pphidDeviceInterface);
//NSLog (@" after QueryInterface" );
if (plugInResult != S_OK)
PrintErrMsg ("Couldn't query HID class device interface from plugInInterface");
(*plugInInterface)->Release (plugInInterface);
}
else
PrintErrMsg ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.");
//NSLog (@"leaving IOWarriorCreateHIDDeviceInterface");
return pphidDeviceInterface;
}
void PrintNotificationMessage (char* msg)
{
#ifdef IOWarriorDebug
printf (msg);
#endif
}
void PrintErrMsg (char * msg)
{
fprintf (stderr, "%s\n", msg);
fflush (stderr);
}
void PrintErrMsgIfIOErr (long expr, char * msg)
{
IOReturn err = (expr);
if (err != kIOReturnSuccess)
{
fprintf (stderr, "%s - %s(%x,%d)\n",
msg, mach_error_string (err), err, err & 0xffffff);
fflush(stderr);
}
}
// Reverses the byte order of ioData, so that byte 0 becomes byte (inCount - 1).
void byteSwap (void* ioData, int inCount)
{
char* temp;
char* source = (char*) ioData;
int i;
temp = malloc (inCount);
memcpy (temp, source, inCount);
for (i = 0; i < inCount; i++)
{
source[i] = temp[inCount -1 - i];
}
free (temp);
}
// appends a newly discovered IOWarrior interface to the linked list of known IOWarrior devices
void IOWarriorAddInterfaceToList (IOWarriorHIDDeviceInterface** inInterface, int inInterfaceType, CFStringRef inDeviceSerialNumber)
{
IOWarriorListNode *newNode;
IOWarriorListNode *currentNode;
#ifdef IOWarriorDebug
printf ("adding IOWarrior interface with interface type %d and serialNumber %d\n", inInterfaceType, inDeviceSerialNumber);
#endif
// first construct a new node
newNode = malloc (sizeof (IOWarriorListNode));
newNode->ioWarriorHIDInterface = inInterface;
newNode->interfaceType = inInterfaceType;
newNode->interfaceOpen = false;
if (NULL != inDeviceSerialNumber)
CFRetain (inDeviceSerialNumber);
newNode->serialNumber = inDeviceSerialNumber;
newNode->nextNode = NULL;
// if the list is currently empty, the new node will become the head node
if (NULL == gIOWarriorList)
{
gIOWarriorList = newNode;
}
else
{
// insert the new node at the end of the linked list
currentNode = gIOWarriorList;
while (NULL != currentNode->nextNode)
{
currentNode = currentNode->nextNode;
}
currentNode->nextNode = newNode;
}
}
// clears the list of known devices
void IOWarriorClearInterfaceList ()
{
IOWarriorListNode *currentNode;
IOWarriorListNode *nextNode;
IOWarriorHIDDeviceInterface** interface;
currentNode = gIOWarriorList;
while (currentNode)
{
nextNode = currentNode->nextNode;
interface = currentNode->ioWarriorHIDInterface;
(*interface)->Release (interface);
if (currentNode->serialNumber)
CFRelease (currentNode->serialNumber);
free (currentNode);
currentNode = nextNode;
}
gIOWarriorList = NULL;
}
// returns the list node element at index inIndex, returns NULL if inIndex out ouf bounds
IOWarriorListNode* IOWarriorInterfaceListNodeAtIndex (int inIndex)
{
IOWarriorListNode *currentNode;
int i;
currentNode = gIOWarriorList;
for (i = 0; i < inIndex && currentNode;i++)
{
currentNode = currentNode->nextNode;
}
return currentNode;
}
void IOWarriorSetDeviceCallback (IOWarriorDeviceCallbackFunctionPtr inCallbackPtr, void* inRefCon)
{
gIOWarriorCallBackPtr = inCallbackPtr;
gIOWarriorCallBackRefCon = inRefCon;
}
IOWarriorHIDDeviceInterface** IOWarriorFirstInterfaceOfType (int inInterfaceType)
{
IOWarriorListNode *currentNode;
if (gIOWarriorListDirty)
{
IOWarriorRebuildInterfaceList ();
}
currentNode = gIOWarriorList;
while (currentNode)
{
if (currentNode->interfaceType == inInterfaceType)
{
return currentNode->ioWarriorHIDInterface;
}
currentNode = currentNode->nextNode;
}
return NULL;
}