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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // get a reference to the IOService
  72. kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
  73. IOServiceMatching("it_unbit_foohid"), &iterator);
  74. if (ret != KERN_SUCCESS) {
  75. printf("Unable to access foohid IOService\n");
  76. return 1;
  77. }
  78. int found = 0;
  79. while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
  80. ret = IOServiceOpen(service, mach_task_self(), 0, &connect);
  81. if (ret == KERN_SUCCESS) {
  82. found = 1;
  83. break;
  84. }
  85. }
  86. IOObjectRelease(iterator);
  87. if (!found) {
  88. printf("Unable to open foohid IOService\n");
  89. return 1;
  90. }
  91. input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  92. input[1] = strlen((char*)input[0]);
  93. input[2] = (uint64_t)report_descriptor;
  94. input[3] = sizeof(report_descriptor);
  95. uint32_t output_count = 1;
  96. uint64_t output = 0;
  97. ret = IOConnectCallScalarMethod(connect, FOOHID_CREATE, input, 4, &output, &output_count);
  98. if (ret != KERN_SUCCESS) {
  99. printf("Unable to create virtual HID device\n");
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. static void foohidClose() {
  105. uint32_t output_count = 1;
  106. uint64_t output = 0;
  107. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_DESTROY, input, 2, &output, &output_count);
  108. if (ret != KERN_SUCCESS) {
  109. printf("Unable to destroy virtual HID device\n");
  110. }
  111. }
  112. static void foohidSend(uint16_t *data) {
  113. input[2] = (uint64_t)&gamepad;
  114. input[3] = sizeof(struct gamepad_report_t);
  115. for (int i = 0; i < CHANNELS; i++) {
  116. if (data[i] > CHANNELMAXIMUM) {
  117. data[i] = CHANNELMAXIMUM;
  118. }
  119. }
  120. gamepad.leftX = data[0] - 511;
  121. gamepad.leftY = data[1] - 511;
  122. gamepad.rightX = data[2] - 511;
  123. gamepad.rightY = data[3] - 511;
  124. gamepad.aux1 = data[4] - 511;
  125. gamepad.aux2 = data[5] - 511;
  126. uint32_t output_count = 1;
  127. uint64_t output = 0;
  128. kern_return_t ret = IOConnectCallScalarMethod(connect, FOOHID_SEND, input, 4, &output, &output_count);
  129. if (ret != KERN_SUCCESS) {
  130. printf("Unable to send packet to virtual HID device\n");
  131. }
  132. }
  133. static void signalHandler(int signo) {
  134. running = 0;
  135. }
  136. int main(int argc, char* argv[]) {
  137. if (argc != 2) {
  138. printf("Usage:\n\t%s /dev/serial_port\n", argv[0]);
  139. return 1;
  140. }
  141. int fd = serialOpen(argv[1], BAUDRATE);
  142. if (fd == -1) {
  143. return 1;
  144. }
  145. if (foohidInit() != 0) {
  146. serialClose(fd);
  147. return 1;
  148. }
  149. if (signal(SIGINT, signalHandler) == SIG_ERR) {
  150. perror("Couldn't register signal handler");
  151. return 1;
  152. }
  153. while (running != 0) {
  154. if (serialHasChar(fd)) {
  155. unsigned char c1;
  156. serialReadChar(fd, (char*)&c1);
  157. if (c1 == HEADERBYTE_A) {
  158. // Found first byte of protocol start
  159. while (!serialHasChar(fd)) {
  160. if (running == 0) {
  161. serialClose(fd);
  162. return 0;
  163. }
  164. }
  165. unsigned char c2;
  166. serialReadChar(fd, (char*)&c2);
  167. if (c2 == HEADERBYTE_B) {
  168. // Protocol start has been found, read payload
  169. unsigned char data[PAYLOADBYTES];
  170. int read = 0;
  171. while ((read < PAYLOADBYTES) && (running != 0)) {
  172. read += serialReadRaw(fd, (char*)data + read, PAYLOADBYTES - read);
  173. }
  174. // Read 16bit checksum
  175. unsigned char checksumData[CHECKSUMBYTES];
  176. read = 0;
  177. while ((read < CHECKSUMBYTES) && (running != 0)) {
  178. read += serialReadRaw(fd, (char*)checksumData + read,
  179. CHECKSUMBYTES - read);
  180. }
  181. // Check if checksum matches
  182. uint16_t checksum = 0;
  183. for (int i = 0; i < PAYLOADBYTES; i++) {
  184. checksum += data[i];
  185. }
  186. if (checksum != ((checksumData[0] << 8) | checksumData[1])) {
  187. printf("Wrong checksum: %d != %d\n",
  188. checksum, ((checksumData[0] << 8) | checksumData[1]));
  189. } else {
  190. // Decode channel values
  191. uint16_t buff[CHANNELS + 1];
  192. for (int i = 0; i < (CHANNELS + 1); i++) {
  193. buff[i] = data[2 * i] << 8;
  194. buff[i] |= data[(2 * i) + 1];
  195. }
  196. foohidSend(buff);
  197. }
  198. }
  199. }
  200. }
  201. }
  202. serialClose(fd);
  203. foohidClose();
  204. return 0;
  205. }