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

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