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.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. * you can do whatever you want with this stuff. If we meet some day, and you
  6. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. * ----------------------------------------------------------------------------
  8. */
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <IOKit/IOKitLib.h>
  15. #include "serial.h"
  16. #define BAUDRATE 115200
  17. #define PACKETSIZE 18
  18. #define HEADERBYTES 2
  19. #define HEADERBYTE_A 85
  20. #define HEADERBYTE_B 252
  21. #define CHECKSUMBYTES 2
  22. #define PAYLOADBYTES (PACKETSIZE - HEADERBYTES - CHECKSUMBYTES)
  23. #define CHANNELS 6
  24. #define CHANNELMAXIMUM 1022
  25. #define FOOHID_NAME "it_unbit_foohid"
  26. #define FOOHID_CREATE 0
  27. #define FOOHID_DESTROY 1
  28. #define FOOHID_SEND 2
  29. #define FOOHID_LIST 3
  30. #define VIRTUAL_DEVICE_NAME "Virtual Serial Transmitter"
  31. struct gamepad_report_t {
  32. int16_t leftX;
  33. int16_t leftY;
  34. int16_t rightX;
  35. int16_t rightY;
  36. int16_t aux1;
  37. int16_t aux2;
  38. };
  39. static int running = 1;
  40. static io_iterator_t iterator;
  41. static io_service_t service;
  42. static io_connect_t connect;
  43. static uint64_t input[4];
  44. static struct gamepad_report_t gamepad;
  45. /*
  46. * This is my USB HID Descriptor for this emulated Gamepad.
  47. * For more informations refer to:
  48. * http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
  49. * http://www.usb.org/developers/hidpage#HID%20Descriptor%20Tool
  50. */
  51. static char report_descriptor[36] = {
  52. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  53. 0x09, 0x05, // USAGE (Game Pad)
  54. 0xa1, 0x01, // COLLECTION (Application)
  55. 0xa1, 0x00, // COLLECTION (Physical)
  56. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  57. 0x09, 0x30, // USAGE (X)
  58. 0x09, 0x31, // USAGE (Y)
  59. 0x09, 0x32, // USAGE (Z)
  60. 0x09, 0x33, // USAGE (Rx)
  61. 0x09, 0x34, // USAGE (Ry)
  62. 0x09, 0x35, // USAGE (Rz)
  63. 0x16, 0x01, 0xfe, // LOGICAL_MINIMUM (-511)
  64. 0x26, 0xff, 0x01, // LOGICAL_MAXIMUM (511)
  65. 0x75, 0x10, // REPORT_SIZE (16)
  66. 0x95, 0x06, // REPORT_COUNT (6)
  67. 0x81, 0x02, // INPUT (Data,Var,Abs)
  68. 0xc0, // END_COLLECTION
  69. 0xc0 // END_COLLECTION
  70. };
  71. static int foohidInit() {
  72. printf("Searching for foohid Kernel extension...\n");
  73. // get a reference to the IOService
  74. kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
  75. IOServiceMatching("it_unbit_foohid"), &iterator);
  76. if (ret != KERN_SUCCESS) {
  77. printf("Unable to access foohid IOService\n");
  78. return 1;
  79. }
  80. int found = 0;
  81. while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
  82. ret = IOServiceOpen(service, mach_task_self(), 0, &connect);
  83. if (ret == KERN_SUCCESS) {
  84. found = 1;
  85. break;
  86. }
  87. }
  88. IOObjectRelease(iterator);
  89. if (!found) {
  90. printf("Unable to open foohid IOService\n");
  91. return 1;
  92. }
  93. printf("Creating virtual HID device...\n");
  94. input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  95. input[1] = strlen((char*)input[0]);
  96. input[2] = (uint64_t)report_descriptor;
  97. input[3] = sizeof(report_descriptor);
  98. uint32_t output_count = 1;
  99. uint64_t output = 0;
  100. ret = IOConnectCallScalarMethod(connect, FOOHID_CREATE, input, 4, &output, &output_count);
  101. if (ret != KERN_SUCCESS) {
  102. printf("Unable to create virtual HID device\n");
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static void foohidClose() {
  108. printf("Destroying virtual HID device\n");
  109. uint32_t output_count = 1;
  110. uint64_t output = 0;
  111. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_DESTROY, input, 2, &output, &output_count);
  112. if (ret != KERN_SUCCESS) {
  113. printf("Unable to destroy virtual HID device\n");
  114. }
  115. }
  116. static void foohidSend(uint16_t *data) {
  117. for (int i = 0; i < CHANNELS; i++) {
  118. if (data[i] > CHANNELMAXIMUM) {
  119. data[i] = CHANNELMAXIMUM;
  120. }
  121. }
  122. gamepad.leftX = data[0] - 511;
  123. gamepad.leftY = data[1] - 511;
  124. gamepad.rightX = data[2] - 511;
  125. gamepad.rightY = data[3] - 511;
  126. gamepad.aux1 = data[4] - 511;
  127. gamepad.aux2 = data[5] - 511;
  128. /*
  129. printf("Sending data packet:\n");
  130. printf("Left X: %d\n", gamepad.leftX);
  131. printf("Left Y: %d\n", gamepad.leftY);
  132. printf("Right X: %d\n", gamepad.rightX);
  133. printf("Right Y: %d\n", gamepad.rightY);
  134. printf("Aux 1: %d\n", gamepad.aux1);
  135. printf("Aux 2: %d\n", gamepad.aux2);
  136. */
  137. input[2] = (uint64_t)&gamepad;
  138. input[3] = sizeof(struct gamepad_report_t);
  139. uint32_t output_count = 1;
  140. uint64_t output = 0;
  141. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_SEND, input, 4, &output, &output_count);
  142. if (ret != KERN_SUCCESS) {
  143. printf("Unable to send packet to virtual HID device\n");
  144. }
  145. }
  146. static void signalHandler(int signo) {
  147. running = 0;
  148. printf("\n");
  149. }
  150. int main(int argc, char* argv[]) {
  151. if (argc != 2) {
  152. printf("Usage:\n\t%s /dev/serial_port\n", argv[0]);
  153. return 1;
  154. }
  155. printf("Opening serial port...\n");
  156. int fd = serialOpen(argv[1], BAUDRATE);
  157. if (fd == -1) {
  158. return 1;
  159. }
  160. if (foohidInit() != 0) {
  161. serialClose(fd);
  162. return 1;
  163. }
  164. if (signal(SIGINT, signalHandler) == SIG_ERR) {
  165. perror("Couldn't register signal handler");
  166. return 1;
  167. }
  168. while (running != 0) {
  169. if (serialHasChar(fd)) {
  170. unsigned char c1;
  171. serialReadChar(fd, (char*)&c1);
  172. if (c1 == HEADERBYTE_A) {
  173. // Found first byte of protocol start
  174. while (!serialHasChar(fd)) {
  175. if (running == 0) {
  176. serialClose(fd);
  177. return 0;
  178. }
  179. }
  180. unsigned char c2;
  181. serialReadChar(fd, (char*)&c2);
  182. if (c2 == HEADERBYTE_B) {
  183. // Protocol start has been found, read payload
  184. unsigned char data[PAYLOADBYTES];
  185. int read = 0;
  186. while ((read < PAYLOADBYTES) && (running != 0)) {
  187. read += serialReadRaw(fd, (char*)data + read, PAYLOADBYTES - read);
  188. }
  189. // Read 16bit checksum
  190. unsigned char checksumData[CHECKSUMBYTES];
  191. read = 0;
  192. while ((read < CHECKSUMBYTES) && (running != 0)) {
  193. read += serialReadRaw(fd, (char*)checksumData + read,
  194. CHECKSUMBYTES - read);
  195. }
  196. // Check if checksum matches
  197. uint16_t checksum = 0;
  198. for (int i = 0; i < PAYLOADBYTES; i++) {
  199. checksum += data[i];
  200. }
  201. if (checksum != ((checksumData[0] << 8) | checksumData[1])) {
  202. printf("Wrong checksum: %d != %d\n",
  203. checksum, ((checksumData[0] << 8) | checksumData[1]));
  204. } else {
  205. // Decode channel values
  206. uint16_t buff[CHANNELS + 1];
  207. for (int i = 0; i < (CHANNELS + 1); i++) {
  208. buff[i] = data[2 * i] << 8;
  209. buff[i] |= data[(2 * i) + 1];
  210. buff[i] -= 1000;
  211. }
  212. foohidSend(buff);
  213. }
  214. }
  215. }
  216. }
  217. usleep(1000);
  218. }
  219. printf("Closing serial port...\n");
  220. serialClose(fd);
  221. foohidClose();
  222. return 0;
  223. }