Mac OS X gamepad emulator for serial RC transmitters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fooHID.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // fooHID.m
  3. // SerialGamepad
  4. //
  5. // Created by Thomas Buck on 14.12.15.
  6. // Copyright © 2015 xythobuz. All rights reserved.
  7. //
  8. #import <IOKit/IOKitLib.h>
  9. #import "fooHID.h"
  10. #define CHANNELS 6
  11. #define CHANNELMAXIMUM 1022
  12. #define CHANNELOFFSET (CHANNELMAXIMUM / 2)
  13. #define FOOHID_NAME "it_unbit_foohid"
  14. #define FOOHID_CREATE 0
  15. #define FOOHID_DESTROY 1
  16. #define FOOHID_SEND 2
  17. #define FOOHID_LIST 3
  18. #define VIRTUAL_DEVICE_NAME "Virtual Serial Transmitter"
  19. #define VIRTUAL_DEVICE_SN "SN 123456"
  20. int foohidInit();
  21. void foohidClose();
  22. void foohidSend(uint16_t *data);
  23. @implementation fooHID
  24. + (NSInteger)init {
  25. return foohidInit();
  26. }
  27. + (void)close {
  28. return foohidClose();
  29. }
  30. + (void)send:(NSArray *)data {
  31. if ([data count] < CHANNELS) {
  32. NSLog(@"Not enough data values to send (%lu)!\n", (unsigned long)[data count]);
  33. } else {
  34. uint16_t buffer[CHANNELS];
  35. for (int i = 0; i < CHANNELS; i++) {
  36. buffer[i] = [((NSNumber *)[data objectAtIndex:i]) integerValue];
  37. }
  38. foohidSend(buffer);
  39. }
  40. }
  41. @end
  42. struct gamepad_report_t {
  43. int16_t leftX;
  44. int16_t leftY;
  45. int16_t rightX;
  46. int16_t rightY;
  47. int16_t aux1;
  48. int16_t aux2;
  49. };
  50. static io_connect_t connector;
  51. static uint64_t deviceName = 0, deviceNameLength;
  52. static uint64_t deviceSN = 0, deviceSNLength;
  53. static struct gamepad_report_t gamepad;
  54. /*
  55. * This is my USB HID Descriptor for this emulated Gamepad.
  56. * For more informations refer to:
  57. * http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
  58. * http://www.usb.org/developers/hidpage#HID%20Descriptor%20Tool
  59. */
  60. static char report_descriptor[36] = {
  61. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  62. 0x09, 0x05, // USAGE (Game Pad)
  63. 0xa1, 0x01, // COLLECTION (Application)
  64. 0xa1, 0x00, // COLLECTION (Physical)
  65. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  66. 0x09, 0x30, // USAGE (X)
  67. 0x09, 0x31, // USAGE (Y)
  68. 0x09, 0x32, // USAGE (Z)
  69. 0x09, 0x33, // USAGE (Rx)
  70. 0x09, 0x34, // USAGE (Ry)
  71. 0x09, 0x35, // USAGE (Rz)
  72. 0x16, 0x01, 0xfe, // LOGICAL_MINIMUM (-511)
  73. 0x26, 0xff, 0x01, // LOGICAL_MAXIMUM (511)
  74. 0x75, 0x10, // REPORT_SIZE (16)
  75. 0x95, 0x06, // REPORT_COUNT (6)
  76. 0x81, 0x02, // INPUT (Data,Var,Abs)
  77. 0xc0, // END_COLLECTION
  78. 0xc0 // END_COLLECTION
  79. };
  80. int foohidInit() {
  81. NSLog(@"Searching for foohid Kernel extension...\n");
  82. // get a reference to the IOService
  83. io_iterator_t iterator;
  84. kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
  85. IOServiceMatching(FOOHID_NAME), &iterator);
  86. if (ret != KERN_SUCCESS) {
  87. NSLog(@"Unable to access foohid IOService\n");
  88. return 1;
  89. }
  90. int found = 0;
  91. io_service_t service;
  92. while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
  93. ret = IOServiceOpen(service, mach_task_self(), 0, &connector);
  94. if (ret == KERN_SUCCESS) {
  95. found = 1;
  96. break;
  97. }
  98. }
  99. IOObjectRelease(iterator);
  100. if (!found) {
  101. NSLog(@"Unable to open foohid IOService\n");
  102. return 1;
  103. }
  104. NSLog(@"Creating virtual HID device...\n");
  105. if (deviceName == 0) {
  106. deviceName = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  107. deviceNameLength = strlen((char *)deviceName);
  108. }
  109. if (deviceSN == 0) {
  110. deviceSN = (uint64_t)strdup(VIRTUAL_DEVICE_SN);
  111. deviceSNLength = strlen((char *)deviceSN);
  112. }
  113. uint64_t input[8];
  114. input[0] = deviceName;
  115. input[1] = deviceNameLength;
  116. input[2] = (uint64_t)report_descriptor;
  117. input[3] = sizeof(report_descriptor);
  118. input[4] = deviceSN;
  119. input[5] = deviceSNLength;
  120. input[6] = (uint64_t)2; // vendor ID
  121. input[7] = (uint64_t)3; // device ID
  122. ret = IOConnectCallScalarMethod(connector, FOOHID_CREATE, input, 8, NULL, 0);
  123. if (ret != KERN_SUCCESS) {
  124. printf("Unable to create virtual HID device\n");
  125. return 1;
  126. }
  127. return 0;
  128. }
  129. void foohidClose() {
  130. NSLog(@"Destroying virtual HID device\n");
  131. uint64_t input[2];
  132. input[0] = deviceName;
  133. input[1] = deviceNameLength;
  134. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_DESTROY, input, 2, NULL, 0);
  135. if (ret != KERN_SUCCESS) {
  136. NSLog(@"Unable to destroy virtual HID device\n");
  137. }
  138. }
  139. void foohidSend(uint16_t *data) {
  140. for (int i = 0; i < CHANNELS; i++) {
  141. if (data[i] > CHANNELMAXIMUM) {
  142. data[i] = CHANNELMAXIMUM;
  143. }
  144. }
  145. gamepad.leftX = data[3] - CHANNELOFFSET;
  146. gamepad.leftY = data[2] - CHANNELOFFSET;
  147. gamepad.rightX = data[0] - CHANNELOFFSET;
  148. gamepad.rightY = data[1] - CHANNELOFFSET;
  149. gamepad.aux1 = data[4] - CHANNELOFFSET;
  150. gamepad.aux2 = data[5] - CHANNELOFFSET;
  151. /*
  152. NSLog(@"Sending data packet:\n");
  153. NSLog(@"Left X: %d\n", gamepad.leftX);
  154. NSLog(@"Left Y: %d\n", gamepad.leftY);
  155. NSLog(@"Right X: %d\n", gamepad.rightX);
  156. NSLog(@"Right Y: %d\n", gamepad.rightY);
  157. NSLog(@"Aux 1: %d\n", gamepad.aux1);
  158. NSLog(@"Aux 2: %d\n", gamepad.aux2);
  159. */
  160. uint64_t input[4];
  161. input[0] = deviceName;
  162. input[1] = deviceNameLength;
  163. input[2] = (uint64_t)&gamepad;
  164. input[3] = sizeof(struct gamepad_report_t);
  165. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_SEND, input, 4, NULL, 0);
  166. if (ret != KERN_SUCCESS) {
  167. NSLog(@"Unable to send packet to virtual HID device\n");
  168. }
  169. }