Mac OS X gamepad emulator for serial RC transmitters
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

foohid.c 8.0KB

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