hid-example.c 3.4 KB

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