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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <stdlib.h>
  15. #include <IOKit/IOKitLib.h>
  16. #include "serial.h"
  17. #define BAUDRATE 115200
  18. #define PACKETSIZE 18
  19. #define HEADERBYTES 2
  20. #define HEADERBYTE_A 85
  21. #define HEADERBYTE_B 252
  22. #define CHECKSUMBYTES 2
  23. #define PAYLOADBYTES (PACKETSIZE - HEADERBYTES - CHECKSUMBYTES)
  24. #define CHANNELS 6
  25. #define TESTCHANNEL 2
  26. #define CHANNELMAXIMUM 1022
  27. #define FOOHID_NAME "it_unbit_foohid"
  28. #define FOOHID_CREATE 0
  29. #define FOOHID_DESTROY 1
  30. #define FOOHID_SEND 2
  31. #define FOOHID_LIST 3
  32. #define VIRTUAL_DEVICE_NAME "FrSky Joystick"
  33. #define VIRTUAL_DEVICE_SERIAL "00000000001B"
  34. #define VIRTUAL_VID 0x0483
  35. #define VIRTUAL_PID 0x5710
  36. struct gamepad_report_t {
  37. //bit 0 - button 1, bit 1 - button 2, ..., mapped to channels 9-16, on if channel > 0
  38. uint8_t buttons1;
  39. uint8_t buttons2; // mapped to channels 17-24, on if channel > 0
  40. uint8_t buttons3; // mapped to channels 25-32, on if channel > 0
  41. int8_t X; //analog value, mapped to channel 1
  42. int8_t Y; //analog value, mapped to channel 2
  43. int8_t Z; //analog value, mapped to channel 3
  44. int8_t Rx; //analog value, mapped to channel 4
  45. int8_t Ry; //analog value, mapped to channel 5
  46. int8_t Rz; //analog value, mapped to channel 6
  47. int8_t S1; //analog value, mapped to channel 7
  48. int8_t S2; //analog value, mapped to channel 8
  49. };
  50. static int running = 1;
  51. static io_iterator_t iterator;
  52. static io_service_t service;
  53. static io_connect_t connect;
  54. #define input_count 8
  55. static uint64_t input[input_count];
  56. static struct gamepad_report_t gamepad;
  57. // From Taranis:
  58. // https://github.com/opentx/opentx/blob/03b65b06b6cec2d2b64dfb0f436eda155274841d/radio/src/targets/common/arm/stm32/usbd_hid_joystick.c
  59. static const uint8_t report_descriptor[] = {
  60. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  61. 0x09, 0x05, // USAGE (Game Pad)
  62. 0xa1, 0x01, // COLLECTION (Application)
  63. 0xa1, 0x00, // COLLECTION (Physical)
  64. 0x05, 0x09, // USAGE_PAGE (Button)
  65. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  66. 0x29, 0x18, // USAGE_MAXIMUM (Button 24)
  67. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  68. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  69. 0x95, 0x18, // REPORT_COUNT (24)
  70. 0x75, 0x01, // REPORT_SIZE (1)
  71. 0x81, 0x02, // INPUT (Data,Var,Abs)
  72. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  73. 0x09, 0x30, // USAGE (X)
  74. 0x09, 0x31, // USAGE (Y)
  75. 0x09, 0x32, // USAGE (Z)
  76. 0x09, 0x33, // USAGE (Rx)
  77. 0x09, 0x34, // USAGE (Ry)
  78. 0x09, 0x35, // USAGE (Rz)
  79. 0x09, 0x36, // USAGE (Slider)
  80. 0x09, 0x36, // USAGE (Slider)
  81. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  82. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  83. 0x75, 0x08, // REPORT_SIZE (8)
  84. 0x95, 0x08, // REPORT_COUNT (8)
  85. 0x81, 0x02, // INPUT (Data,Var,Abs)
  86. 0xc0, // END_COLLECTION
  87. 0xc0 // END_COLLECTION
  88. };
  89. static int foohidPrintDevices() {
  90. uint32_t output_count = 2;
  91. uint64_t output[2] = { 0, 0 };
  92. uint16_t buf_len = 4096;
  93. char *buf = malloc(buf_len);
  94. if (!buf) {
  95. printf("memory error\n");
  96. return 1;
  97. }
  98. uint64_t input[2];
  99. while (1) {
  100. input[0] = (uint64_t) buf;
  101. input[1] = (uint64_t) buf_len;
  102. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_LIST, input, 2, output, &output_count);
  103. if (ret != KERN_SUCCESS) {
  104. free(buf);
  105. printf("unable to list hid devices\n");
  106. return 1;
  107. }
  108. // all is fine
  109. if (output[0] == 0) {
  110. printf("--\n");
  111. printf("fooHID devices: (%llu)\n", output[1]);
  112. char *ptr = buf;
  113. for(uint64_t i = 0; i < output[1]; i++) {
  114. printf("%s\n", ptr);
  115. ptr += strlen(ptr) + 1;
  116. }
  117. printf("--\n");
  118. free(buf);
  119. return ret;
  120. }
  121. // realloc memory
  122. buf_len = output[0];
  123. char *tmp = realloc(buf, buf_len);
  124. if (!tmp) {
  125. free(buf);
  126. printf("unable to allocate memory\n");
  127. return 1;
  128. }
  129. buf = tmp;
  130. }
  131. return 0;
  132. }
  133. static int foohidInit() {
  134. printf("Searching for foohid Kernel extension...\n");
  135. // get a reference to the IOService
  136. kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
  137. IOServiceMatching(FOOHID_NAME), &iterator);
  138. if (ret != KERN_SUCCESS) {
  139. printf("Unable to access foohid IOService\n");
  140. return 1;
  141. }
  142. int found = 0;
  143. while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
  144. ret = IOServiceOpen(service, mach_task_self(), 0, &connect);
  145. if (ret == KERN_SUCCESS) {
  146. found = 1;
  147. break;
  148. }
  149. }
  150. IOObjectRelease(iterator);
  151. if (!found) {
  152. printf("Unable to open foohid IOService\n");
  153. return 1;
  154. }
  155. //foohidPrintDevices();
  156. printf("Creating virtual HID device...\n");
  157. input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  158. input[1] = strlen((char*)input[0]);
  159. input[2] = (uint64_t)report_descriptor;
  160. input[3] = sizeof(report_descriptor);
  161. input[4] = (uint64_t)strdup(VIRTUAL_DEVICE_SERIAL);
  162. input[5] = strlen((char*)input[4]);
  163. input[6] = (uint64_t)VIRTUAL_VID;
  164. input[7] = (uint64_t)VIRTUAL_PID;
  165. ret = IOConnectCallScalarMethod(connect, FOOHID_CREATE, input, input_count, NULL, 0);
  166. if (ret != KERN_SUCCESS) {
  167. printf("Unable to create virtual HID device\n");
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static void foohidClose() {
  173. printf("Destroying virtual HID device\n");
  174. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_DESTROY, input, 2, NULL, 0);
  175. if (ret != KERN_SUCCESS) {
  176. printf("Unable to destroy virtual HID device\n");
  177. }
  178. }
  179. static void foohidSend(uint16_t *data) {
  180. for (int i = 0; i < CHANNELS; i++) {
  181. if (data[i] > CHANNELMAXIMUM) {
  182. data[i] = CHANNELMAXIMUM;
  183. }
  184. }
  185. gamepad.X = (data[3] - 511) / 4;
  186. gamepad.Y = (data[2] - 511) / 4;
  187. gamepad.Z = (data[0] - 511) / 4;
  188. gamepad.Rx = (data[1] - 511) / 4;
  189. gamepad.Ry = (data[4] - 511) / 4;
  190. gamepad.Rz = (data[5] - 511) / 4;
  191. input[2] = (uint64_t)&gamepad;
  192. input[3] = sizeof(struct gamepad_report_t);
  193. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_SEND, input, 4, NULL, 0);
  194. if (ret != KERN_SUCCESS) {
  195. printf("Unable to send packet to virtual HID device\n");
  196. }
  197. }
  198. static void signalHandler(int signo) {
  199. running = 0;
  200. printf("\n");
  201. }
  202. int main(int argc, char* argv[]) {
  203. if (argc != 2) {
  204. printf("Usage:\n\t%s /dev/serial_port\n", argv[0]);
  205. return 1;
  206. }
  207. printf("Opening serial port...\n");
  208. int fd = serialOpen(argv[1], BAUDRATE);
  209. if (fd == -1) {
  210. return 1;
  211. }
  212. if (foohidInit() != 0) {
  213. serialClose(fd);
  214. return 1;
  215. }
  216. if (signal(SIGINT, signalHandler) == SIG_ERR) {
  217. perror("Couldn't register signal handler");
  218. return 1;
  219. }
  220. printf("Entering main-loop...\n");
  221. while (running != 0) {
  222. if (serialHasChar(fd)) {
  223. unsigned char c1;
  224. serialReadChar(fd, (char*)&c1);
  225. if (c1 == HEADERBYTE_A) {
  226. // Found first byte of protocol start
  227. while (!serialHasChar(fd)) {
  228. if (running == 0) {
  229. serialClose(fd);
  230. return 0;
  231. }
  232. }
  233. unsigned char c2;
  234. serialReadChar(fd, (char*)&c2);
  235. if (c2 == HEADERBYTE_B) {
  236. // Protocol start has been found, read payload
  237. unsigned char data[PAYLOADBYTES];
  238. int read = 0;
  239. while ((read < PAYLOADBYTES) && (running != 0)) {
  240. read += serialReadRaw(fd, (char*)data + read, PAYLOADBYTES - read);
  241. }
  242. // Read 16bit checksum
  243. unsigned char checksumData[CHECKSUMBYTES];
  244. read = 0;
  245. while ((read < CHECKSUMBYTES) && (running != 0)) {
  246. read += serialReadRaw(fd, (char*)checksumData + read,
  247. CHECKSUMBYTES - read);
  248. }
  249. // Check if checksum matches
  250. uint16_t checksum = 0;
  251. for (int i = 0; i < PAYLOADBYTES; i++) {
  252. checksum += data[i];
  253. }
  254. if (checksum != ((checksumData[0] << 8) | checksumData[1])) {
  255. printf("Wrong checksum: %d != %d\n",
  256. checksum, ((checksumData[0] << 8) | checksumData[1]));
  257. } else {
  258. // Decode channel values
  259. uint16_t buff[CHANNELS + 1];
  260. for (int i = 0; i < (CHANNELS + 1); i++) {
  261. buff[i] = data[2 * i] << 8;
  262. buff[i] |= data[(2 * i) + 1];
  263. if (i < CHANNELS) {
  264. buff[i] -= 1000;
  265. }
  266. }
  267. // Check Test Channel Value
  268. if (buff[CHANNELS] != buff[TESTCHANNEL]) {
  269. printf("Wrong test channel value: %d != %d\n",
  270. buff[CHANNELS], buff[TESTCHANNEL]);
  271. }
  272. foohidSend(buff);
  273. }
  274. }
  275. }
  276. }
  277. usleep(1000);
  278. }
  279. printf("Closing serial port...\n");
  280. serialClose(fd);
  281. foohidClose();
  282. return 0;
  283. }