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.4KB

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