hid-example.c 3.8 KB

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