hid-example.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Hidraw Userspace Example
  3. *
  4. * Copyright (c) 2010 Alan Ott <alan@signal11.us>
  5. * Copyright (c) 2010 Signal 11 Software
  6. *
  7. * The code may be used by anyone for any purpose,
  8. * and can serve as a starting point for developing
  9. * applications using hidraw.
  10. */
  11. /* Linux */
  12. #include <linux/types.h>
  13. #include <linux/input.h>
  14. #include <linux/hidraw.h>
  15. /*
  16. * Ugly hack to work around failing compilation on systems that don't
  17. * yet populate new version of hidraw.h to userspace.
  18. */
  19. #ifndef HIDIOCSFEATURE
  20. #warning Please have your distro update the userspace kernel headers
  21. #define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
  22. #define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
  23. #endif
  24. /* Unix */
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. /* C */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <errno.h>
  35. const char *bus_str(int bus);
  36. int main(int argc, char **argv)
  37. {
  38. int fd;
  39. int i, res, desc_size = 0;
  40. char buf[256];
  41. struct hidraw_report_descriptor rpt_desc;
  42. struct hidraw_devinfo info;
  43. char *device = "/dev/hidraw0";
  44. if (argc > 1)
  45. device = argv[1];
  46. /* Open the Device with non-blocking reads. In real life,
  47. don't use a hard coded path; use libudev instead. */
  48. fd = open(device, O_RDWR|O_NONBLOCK);
  49. if (fd < 0) {
  50. perror("Unable to open device");
  51. return 1;
  52. }
  53. memset(&rpt_desc, 0x0, sizeof(rpt_desc));
  54. memset(&info, 0x0, sizeof(info));
  55. memset(buf, 0x0, sizeof(buf));
  56. /* Get Report Descriptor Size */
  57. res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
  58. if (res < 0)
  59. perror("HIDIOCGRDESCSIZE");
  60. else
  61. printf("Report Descriptor Size: %d\n", desc_size);
  62. /* Get Report Descriptor */
  63. rpt_desc.size = desc_size;
  64. res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
  65. if (res < 0) {
  66. perror("HIDIOCGRDESC");
  67. } else {
  68. printf("Report Descriptor:\n");
  69. for (i = 0; i < rpt_desc.size; i++)
  70. printf("%hhx ", rpt_desc.value[i]);
  71. puts("\n");
  72. }
  73. /* Get Raw Name */
  74. res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
  75. if (res < 0)
  76. perror("HIDIOCGRAWNAME");
  77. else
  78. printf("Raw Name: %s\n", buf);
  79. /* Get Physical Location */
  80. res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
  81. if (res < 0)
  82. perror("HIDIOCGRAWPHYS");
  83. else
  84. printf("Raw Phys: %s\n", buf);
  85. /* Get Raw Info */
  86. res = ioctl(fd, HIDIOCGRAWINFO, &info);
  87. if (res < 0) {
  88. perror("HIDIOCGRAWINFO");
  89. } else {
  90. printf("Raw Info:\n");
  91. printf("\tbustype: %d (%s)\n",
  92. info.bustype, bus_str(info.bustype));
  93. printf("\tvendor: 0x%04hx\n", info.vendor);
  94. printf("\tproduct: 0x%04hx\n", info.product);
  95. }
  96. /* Set Feature */
  97. buf[0] = 0x9; /* Report Number */
  98. buf[1] = 0xff;
  99. buf[2] = 0xff;
  100. buf[3] = 0xff;
  101. res = ioctl(fd, HIDIOCSFEATURE(4), buf);
  102. if (res < 0)
  103. perror("HIDIOCSFEATURE");
  104. else
  105. printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
  106. /* Get Feature */
  107. buf[0] = 0x9; /* Report Number */
  108. res = ioctl(fd, HIDIOCGFEATURE(256), buf);
  109. if (res < 0) {
  110. perror("HIDIOCGFEATURE");
  111. } else {
  112. printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
  113. printf("Report data (not containing the report number):\n\t");
  114. for (i = 0; i < res; i++)
  115. printf("%hhx ", buf[i]);
  116. puts("\n");
  117. }
  118. /* Send a Report to the Device */
  119. buf[0] = 0x1; /* Report Number */
  120. buf[1] = 0x77;
  121. res = write(fd, buf, 2);
  122. if (res < 0) {
  123. printf("Error: %d\n", errno);
  124. perror("write");
  125. } else {
  126. printf("write() wrote %d bytes\n", res);
  127. }
  128. /* Get a report from the device */
  129. res = read(fd, buf, 16);
  130. if (res < 0) {
  131. perror("read");
  132. } else {
  133. printf("read() read %d bytes:\n\t", res);
  134. for (i = 0; i < res; i++)
  135. printf("%hhx ", buf[i]);
  136. puts("\n");
  137. }
  138. close(fd);
  139. return 0;
  140. }
  141. const char *
  142. bus_str(int bus)
  143. {
  144. switch (bus) {
  145. case BUS_USB:
  146. return "USB";
  147. break;
  148. case BUS_HIL:
  149. return "HIL";
  150. break;
  151. case BUS_BLUETOOTH:
  152. return "Bluetooth";
  153. break;
  154. case BUS_VIRTUAL:
  155. return "Virtual";
  156. break;
  157. default:
  158. return "Other";
  159. break;
  160. }
  161. }