test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*******************************************************
  2. HIDAPI - Multi-Platform library for
  3. communication with HID devices.
  4. Alan Ott
  5. Signal 11 Software
  6. libusb/hidapi Team
  7. Copyright 2022.
  8. This contents of this file may be used by anyone
  9. for any reason without any conditions and may be
  10. used as a starting point for your own applications
  11. which use HIDAPI.
  12. ********************************************************/
  13. #include <stdio.h>
  14. #include <wchar.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <hidapi.h>
  18. // Headers needed for sleeping.
  19. #ifdef _WIN32
  20. #include <windows.h>
  21. #else
  22. #include <unistd.h>
  23. #endif
  24. // Fallback/example
  25. #ifndef HID_API_MAKE_VERSION
  26. #define HID_API_MAKE_VERSION(mj, mn, p) (((mj) << 24) | ((mn) << 8) | (p))
  27. #endif
  28. #ifndef HID_API_VERSION
  29. #define HID_API_VERSION HID_API_MAKE_VERSION(HID_API_VERSION_MAJOR, HID_API_VERSION_MINOR, HID_API_VERSION_PATCH)
  30. #endif
  31. //
  32. // Sample using platform-specific headers
  33. #if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
  34. #include <hidapi_darwin.h>
  35. #endif
  36. #if defined(_WIN32) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
  37. #include <hidapi_winapi.h>
  38. #endif
  39. #if defined(USING_HIDAPI_LIBUSB) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
  40. #include <hidapi_libusb.h>
  41. #endif
  42. //
  43. const char *hid_bus_name(hid_bus_type bus_type) {
  44. static const char *const HidBusTypeName[] = {
  45. "Unknown",
  46. "USB",
  47. "Bluetooth",
  48. "I2C",
  49. "SPI",
  50. };
  51. if ((int)bus_type < 0)
  52. bus_type = HID_API_BUS_UNKNOWN;
  53. if ((int)bus_type >= (int)(sizeof(HidBusTypeName) / sizeof(HidBusTypeName[0])))
  54. bus_type = HID_API_BUS_UNKNOWN;
  55. return HidBusTypeName[bus_type];
  56. }
  57. void print_device(struct hid_device_info *cur_dev) {
  58. printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
  59. printf("\n");
  60. printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
  61. printf(" Product: %ls\n", cur_dev->product_string);
  62. printf(" Release: %hx\n", cur_dev->release_number);
  63. printf(" Interface: %d\n", cur_dev->interface_number);
  64. printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
  65. printf(" Bus type: %d (%s)\n", cur_dev->bus_type, hid_bus_name(cur_dev->bus_type));
  66. printf("\n");
  67. }
  68. void print_hid_report_descriptor_from_device(hid_device *device) {
  69. unsigned char descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];
  70. int res = 0;
  71. printf(" Report Descriptor: ");
  72. res = hid_get_report_descriptor(device, descriptor, sizeof(descriptor));
  73. if (res < 0) {
  74. printf("error getting: %ls", hid_error(device));
  75. }
  76. else {
  77. printf("(%d bytes)", res);
  78. }
  79. for (int i = 0; i < res; i++) {
  80. if (i % 10 == 0) {
  81. printf("\n");
  82. }
  83. printf("0x%02x, ", descriptor[i]);
  84. }
  85. printf("\n");
  86. }
  87. void print_hid_report_descriptor_from_path(const char *path) {
  88. hid_device *device = hid_open_path(path);
  89. if (device) {
  90. print_hid_report_descriptor_from_device(device);
  91. hid_close(device);
  92. }
  93. else {
  94. printf(" Report Descriptor: Unable to open device by path\n");
  95. }
  96. }
  97. void print_devices(struct hid_device_info *cur_dev) {
  98. for (; cur_dev; cur_dev = cur_dev->next) {
  99. print_device(cur_dev);
  100. }
  101. }
  102. void print_devices_with_descriptor(struct hid_device_info *cur_dev) {
  103. for (; cur_dev; cur_dev = cur_dev->next) {
  104. print_device(cur_dev);
  105. print_hid_report_descriptor_from_path(cur_dev->path);
  106. }
  107. }
  108. int main(int argc, char* argv[])
  109. {
  110. (void)argc;
  111. (void)argv;
  112. int res;
  113. unsigned char buf[256];
  114. #define MAX_STR 255
  115. wchar_t wstr[MAX_STR];
  116. hid_device *handle;
  117. int i;
  118. struct hid_device_info *devs;
  119. printf("hidapi test/example tool. Compiled with hidapi version %s, runtime version %s.\n", HID_API_VERSION_STR, hid_version_str());
  120. if (HID_API_VERSION == HID_API_MAKE_VERSION(hid_version()->major, hid_version()->minor, hid_version()->patch)) {
  121. printf("Compile-time version matches runtime version of hidapi.\n\n");
  122. }
  123. else {
  124. printf("Compile-time version is different than runtime version of hidapi.\n]n");
  125. }
  126. if (hid_init())
  127. return -1;
  128. #if defined(__APPLE__) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 12, 0)
  129. // To work properly needs to be called before hid_open/hid_open_path after hid_init.
  130. // Best/recommended option - call it right after hid_init.
  131. hid_darwin_set_open_exclusive(0);
  132. #endif
  133. devs = hid_enumerate(0x0, 0x0);
  134. print_devices_with_descriptor(devs);
  135. hid_free_enumeration(devs);
  136. // Set up the command buffer.
  137. memset(buf,0x00,sizeof(buf));
  138. buf[0] = 0x01;
  139. buf[1] = 0x81;
  140. // Open the device using the VID, PID,
  141. // and optionally the Serial number.
  142. ////handle = hid_open(0x4d8, 0x3f, L"12345");
  143. handle = hid_open(0x4d8, 0x3f, NULL);
  144. if (!handle) {
  145. printf("unable to open device\n");
  146. hid_exit();
  147. return 1;
  148. }
  149. // Read the Manufacturer String
  150. wstr[0] = 0x0000;
  151. res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
  152. if (res < 0)
  153. printf("Unable to read manufacturer string\n");
  154. printf("Manufacturer String: %ls\n", wstr);
  155. // Read the Product String
  156. wstr[0] = 0x0000;
  157. res = hid_get_product_string(handle, wstr, MAX_STR);
  158. if (res < 0)
  159. printf("Unable to read product string\n");
  160. printf("Product String: %ls\n", wstr);
  161. // Read the Serial Number String
  162. wstr[0] = 0x0000;
  163. res = hid_get_serial_number_string(handle, wstr, MAX_STR);
  164. if (res < 0)
  165. printf("Unable to read serial number string\n");
  166. printf("Serial Number String: (%d) %ls\n", wstr[0], wstr);
  167. print_hid_report_descriptor_from_device(handle);
  168. struct hid_device_info* info = hid_get_device_info(handle);
  169. if (info == NULL) {
  170. printf("Unable to get device info\n");
  171. } else {
  172. print_devices(info);
  173. }
  174. // Read Indexed String 1
  175. wstr[0] = 0x0000;
  176. res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
  177. if (res < 0)
  178. printf("Unable to read indexed string 1\n");
  179. printf("Indexed String 1: %ls\n", wstr);
  180. // Set the hid_read() function to be non-blocking.
  181. hid_set_nonblocking(handle, 1);
  182. // Try to read from the device. There should be no
  183. // data here, but execution should not block.
  184. res = hid_read(handle, buf, 17);
  185. // Send a Feature Report to the device
  186. buf[0] = 0x2;
  187. buf[1] = 0xa0;
  188. buf[2] = 0x0a;
  189. buf[3] = 0x00;
  190. buf[4] = 0x00;
  191. res = hid_send_feature_report(handle, buf, 17);
  192. if (res < 0) {
  193. printf("Unable to send a feature report.\n");
  194. }
  195. memset(buf,0,sizeof(buf));
  196. // Read a Feature Report from the device
  197. buf[0] = 0x2;
  198. res = hid_get_feature_report(handle, buf, sizeof(buf));
  199. if (res < 0) {
  200. printf("Unable to get a feature report: %ls\n", hid_error(handle));
  201. }
  202. else {
  203. // Print out the returned buffer.
  204. printf("Feature Report\n ");
  205. for (i = 0; i < res; i++)
  206. printf("%02x ", (unsigned int) buf[i]);
  207. printf("\n");
  208. }
  209. memset(buf,0,sizeof(buf));
  210. // Toggle LED (cmd 0x80). The first byte is the report number (0x1).
  211. buf[0] = 0x1;
  212. buf[1] = 0x80;
  213. res = hid_write(handle, buf, 17);
  214. if (res < 0) {
  215. printf("Unable to write(): %ls\n", hid_error(handle));
  216. }
  217. // Request state (cmd 0x81). The first byte is the report number (0x1).
  218. buf[0] = 0x1;
  219. buf[1] = 0x81;
  220. hid_write(handle, buf, 17);
  221. if (res < 0) {
  222. printf("Unable to write()/2: %ls\n", hid_error(handle));
  223. }
  224. // Read requested state. hid_read() has been set to be
  225. // non-blocking by the call to hid_set_nonblocking() above.
  226. // This loop demonstrates the non-blocking nature of hid_read().
  227. res = 0;
  228. i = 0;
  229. while (res == 0) {
  230. res = hid_read(handle, buf, sizeof(buf));
  231. if (res == 0) {
  232. printf("waiting...\n");
  233. }
  234. if (res < 0) {
  235. printf("Unable to read(): %ls\n", hid_error(handle));
  236. break;
  237. }
  238. i++;
  239. if (i >= 10) { /* 10 tries by 500 ms - 5 seconds of waiting*/
  240. printf("read() timeout\n");
  241. break;
  242. }
  243. #ifdef _WIN32
  244. Sleep(500);
  245. #else
  246. usleep(500*1000);
  247. #endif
  248. }
  249. if (res > 0) {
  250. printf("Data read:\n ");
  251. // Print out the returned buffer.
  252. for (i = 0; i < res; i++)
  253. printf("%02x ", (unsigned int) buf[i]);
  254. printf("\n");
  255. }
  256. hid_close(handle);
  257. /* Free static HIDAPI objects. */
  258. hid_exit();
  259. #ifdef _WIN32
  260. system("pause");
  261. #endif
  262. return 0;
  263. }