hid-example.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /* Open the Device with non-blocking reads. In real life,
  44. don't use a hard coded path; use libudev instead. */
  45. fd = open("/dev/hidraw0", O_RDWR|O_NONBLOCK);
  46. if (fd < 0) {
  47. perror("Unable to open device");
  48. return 1;
  49. }
  50. memset(&rpt_desc, 0x0, sizeof(rpt_desc));
  51. memset(&info, 0x0, sizeof(info));
  52. memset(buf, 0x0, sizeof(buf));
  53. /* Get Report Descriptor Size */
  54. res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
  55. if (res < 0)
  56. perror("HIDIOCGRDESCSIZE");
  57. else
  58. printf("Report Descriptor Size: %d\n", desc_size);
  59. /* Get Report Descriptor */
  60. rpt_desc.size = desc_size;
  61. res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
  62. if (res < 0) {
  63. perror("HIDIOCGRDESC");
  64. } else {
  65. printf("Report Descriptor:\n");
  66. for (i = 0; i < rpt_desc.size; i++)
  67. printf("%hhx ", rpt_desc.value[i]);
  68. puts("\n");
  69. }
  70. /* Get Raw Name */
  71. res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
  72. if (res < 0)
  73. perror("HIDIOCGRAWNAME");
  74. else
  75. printf("Raw Name: %s\n", buf);
  76. /* Get Physical Location */
  77. res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
  78. if (res < 0)
  79. perror("HIDIOCGRAWPHYS");
  80. else
  81. printf("Raw Phys: %s\n", buf);
  82. /* Get Raw Info */
  83. res = ioctl(fd, HIDIOCGRAWINFO, &info);
  84. if (res < 0) {
  85. perror("HIDIOCGRAWINFO");
  86. } else {
  87. printf("Raw Info:\n");
  88. printf("\tbustype: %d (%s)\n",
  89. info.bustype, bus_str(info.bustype));
  90. printf("\tvendor: 0x%04hx\n", info.vendor);
  91. printf("\tproduct: 0x%04hx\n", info.product);
  92. }
  93. /* Set Feature */
  94. buf[0] = 0x9; /* Report Number */
  95. buf[1] = 0xff;
  96. buf[2] = 0xff;
  97. buf[3] = 0xff;
  98. res = ioctl(fd, HIDIOCSFEATURE(4), buf);
  99. if (res < 0)
  100. perror("HIDIOCSFEATURE");
  101. else
  102. printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
  103. /* Get Feature */
  104. buf[0] = 0x9; /* Report Number */
  105. res = ioctl(fd, HIDIOCGFEATURE(256), buf);
  106. if (res < 0) {
  107. perror("HIDIOCGFEATURE");
  108. } else {
  109. printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
  110. printf("Report data (not containing the report number):\n\t");
  111. for (i = 0; i < res; i++)
  112. printf("%hhx ", buf[i]);
  113. puts("\n");
  114. }
  115. /* Send a Report to the Device */
  116. buf[0] = 0x1; /* Report Number */
  117. buf[1] = 0x77;
  118. res = write(fd, buf, 2);
  119. if (res < 0) {
  120. printf("Error: %d\n", errno);
  121. perror("write");
  122. } else {
  123. printf("write() wrote %d bytes\n", res);
  124. }
  125. /* Get a report from the device */
  126. res = read(fd, buf, 16);
  127. if (res < 0) {
  128. perror("read");
  129. } else {
  130. printf("read() read %d bytes:\n\t", res);
  131. for (i = 0; i < res; i++)
  132. printf("%hhx ", buf[i]);
  133. puts("\n");
  134. }
  135. close(fd);
  136. return 0;
  137. }
  138. const char *
  139. bus_str(int bus)
  140. {
  141. switch (bus) {
  142. case BUS_USB:
  143. return "USB";
  144. break;
  145. case BUS_HIL:
  146. return "HIL";
  147. break;
  148. case BUS_BLUETOOTH:
  149. return "Bluetooth";
  150. break;
  151. case BUS_VIRTUAL:
  152. return "Virtual";
  153. break;
  154. default:
  155. return "Other";
  156. break;
  157. }
  158. }