Guest User

HID Descriptor dump

a guest
Jan 2nd, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. /*
  2.  * Dump Hidraw Descriptor
  3.  *
  4.  * adapted from:
  5.  * Hidraw Userspace Example
  6.  *
  7.  */
  8.  
  9. /* Linux */
  10. #include <linux/types.h>
  11. #include <linux/input.h>
  12. #include <linux/hidraw.h>
  13.  
  14. /*
  15.  * Ugly hack to work around failing compilation on systems that don't
  16.  * yet populate new version of hidraw.h to userspace.
  17.  */
  18. #ifndef HIDIOCSFEATURE
  19. #warning Please have your distro update the userspace kernel headers
  20. #define HIDIOCSFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
  21. #define HIDIOCGFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
  22. #endif
  23.  
  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.  
  31. /* C */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <errno.h>
  36.  
  37. const char *
  38. bus_str(int bus)
  39. {
  40.   switch (bus) {
  41.   case BUS_USB:
  42.     return "USB";
  43.     break;
  44.   case BUS_HIL:
  45.     return "HIL";
  46.     break;
  47.   case BUS_BLUETOOTH:
  48.     return "Bluetooth";
  49.     break;
  50.   case BUS_VIRTUAL:
  51.     return "Virtual";
  52.     break;
  53.   default:
  54.     return "Other";
  55.     break;
  56.   }
  57. }
  58.  
  59. /*
  60.  
  61. Usage: hid-desc /dev/hidraw1 etc.
  62.  
  63.  */
  64.  
  65. int main(int argc, char **argv)
  66. {
  67.   int fd;
  68.   int i, res, desc_size = 0;
  69.   char buf[256];
  70.   struct hidraw_report_descriptor rpt_desc;
  71.   struct hidraw_devinfo info;
  72.   char *device = "/dev/hidraw0";
  73.  
  74.   if (argc > 1)
  75.     device = argv[1];
  76.  
  77.   /* Open the Device with non-blocking reads. In real life,
  78.      don't use a hard coded path; use libudev instead. */
  79.   fd = open(device, O_RDWR|O_NONBLOCK);
  80.  
  81.   if (fd < 0) {
  82.     perror("Unable to open device");
  83.     return 1;
  84.   }
  85.  
  86.   memset(&rpt_desc, 0x0, sizeof(rpt_desc));
  87.   memset(&info, 0x0, sizeof(info));
  88.   memset(buf, 0x0, sizeof(buf));
  89.  
  90.   /* Get Report Descriptor Size */
  91.   res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
  92.   if (res < 0)
  93.     perror("HIDIOCGRDESCSIZE");
  94.   else
  95.     printf("Report Descriptor Size: %d\n", desc_size);
  96.  
  97.   /* Get Report Descriptor */
  98.   rpt_desc.size = desc_size;
  99.   res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
  100.   if (res < 0) {
  101.     perror("HIDIOCGRDESC");
  102.   } else {
  103.     printf("Report Descriptor:\n");
  104.     for (i = 0; i < rpt_desc.size; i++)
  105.       printf("%02hhx ", rpt_desc.value[i]);
  106.     puts("\n");
  107.   }
  108.  
  109.   /* Get Raw Name */
  110.   res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
  111.   if (res < 0)
  112.     perror("HIDIOCGRAWNAME");
  113.   else
  114.     printf("Raw Name: %s\n", buf);
  115.  
  116.   /* Get Physical Location */
  117.   res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
  118.   if (res < 0)
  119.     perror("HIDIOCGRAWPHYS");
  120.   else
  121.     printf("Raw Phys: %s\n", buf);
  122.  
  123.   /* Get Raw Info */
  124.   res = ioctl(fd, HIDIOCGRAWINFO, &info);
  125.   if (res < 0) {
  126.     perror("HIDIOCGRAWINFO");
  127.   } else {
  128.     printf("Raw Info:\n");
  129.     printf("\tbustype: %d (%s)\n",
  130.       info.bustype, bus_str(info.bustype));
  131.     printf("\tvendor: 0x%04hx\n", info.vendor);
  132.     printf("\tproduct: 0x%04hx\n", info.product);
  133.   }
  134.  
  135. #if 0
  136.   /* Get a report from the device */
  137.   res = read(fd, buf, 16);
  138.   if (res < 0) {
  139.     perror("read");
  140.   } else {
  141.     printf("read() read %d bytes:\n\t", res);
  142.     for (i = 0; i < res; i++)
  143.       printf("%hhx ", buf[i]);
  144.     puts("\n");
  145.   }
  146. #endif
  147.  
  148.   close(fd);
  149.   return 0;
  150. }
Add Comment
Please, Sign In to add comment