获取输入设备的vid和pid

时间:2023-03-08 21:28:39

一、获取/dev/input/event16设备的vid和pid

testhid.c

  1. #include <linux/types.h>
  2. #include <linux/input.h>
  3. #include <linux/hidraw.h>
  4. #include <sys/ioctl.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <poll.h>
  10. #include <time.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. int main(){
  18. char filename[64];
  19. strcpy(filename,"/dev/input/event16");
  20. char name[80];
  21. int fd = open(filename, O_RDWR /*| O_NONBLOCK*/);
  22. if(fd <= 0) {
  23. printf("TK----------->>>>open error\n");
  24. return -1;
  25. }
  26. /////////
  27. struct input_id inputId;
  28. int rc = ioctl(fd, EVIOCGID, &inputId);
  29. printf("TK-------->>>>info.vendor is 0x%x\n",inputId.vendor);
  30. printf("TK-------->>>>info.product is 0x%x\n",inputId.product);
  31. printf("TK-------->>>>info.bustype is 0x%x\n",inputId.bustype);
  32. /*
  33. struct hidraw_devinfo info;
  34. int rc = ioctl(fd, HIDIOCGRAWINFO, &info);
  35. printf("TK-------->>>>info.vendor is 0x%x\n",info.vendor);
  36. printf("TK-------->>>>info.product is 0x%x\n",info.product);
  37. printf("TK-------->>>>info.bustype is 0x%x\n",info.bustype);
  38. */
  39. ///////////
  40. struct hidraw_report_descriptor descriptor;
  41. rc = ioctl(fd, HIDIOCGRDESC, &descriptor);
  42. printf("TK-------->>>>descriptor.size is 0x%04x\n",descriptor.size);
  43. //////
  44. int descriptorSize=0;
  45. rc = ioctl(fd, HIDIOCGRDESCSIZE, &descriptorSize);
  46. printf("TK-------->>>>descriptorSize is 0x%04x\n",descriptorSize);
  47. //////
  48. name[sizeof(name) - 1] = '\0';
  49. if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
  50. name[0] = '\0';
  51. }
  52. printf("TK------->>>name is %s\n",name);
  53. //////
  54. close(fd);
  55. return 0;
  56. }

二、编译

Android.mk

  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_SRC_FILES:= \
  4. testhid.c
  5. LOCAL_SHARED_LIBRARIES := \
  6. libutils
  7. LOCAL_MODULE:= testinput
  8. LOCAL_MODULE_TAGS := optional
  9. include $(BUILD_EXECUTABLE)

三、运行

    1. TK-------->>>>info.vendor is 0x0
    2. TK-------->>>>info.product is 0x1
    3. TK-------->>>>info.bustype is 0x19
    4. TK-------->>>>descriptor.size is 0x0000
    5. TK-------->>>>descriptorSize is 0x0000
    6. TK------->>>name is Power Button