天天看点

获取输入设备的vid和pid

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

testhid.c

[cpp] view plain copy 

#include <linux/types.h>  

#include <linux/input.h>  

#include <linux/hidraw.h>  

#include <sys/ioctl.h>  

#include <sys/types.h>  

#include <sys/stat.h>  

#include <fcntl.h>  

#include <unistd.h>  

#include <poll.h>  

#include <time.h>  

#include <math.h>  

#include <stdio.h>  

#include <stdint.h>  

#include <string.h>  

#include <stdlib.h>  

#include <errno.h>  

int main(){  

  char filename[64];  

  strcpy(filename,"/dev/input/event16");  

  char name[80];  

  int fd = open(filename, O_RDWR /*| O_NONBLOCK*/);  

  if(fd <= 0) {  

    printf("TK----------->>>>open error\n");  

    return -1;  

  }  

  /////////  

  struct input_id inputId;  

  int rc = ioctl(fd, EVIOCGID, &inputId);  

  printf("TK-------->>>>info.vendor is 0x%x\n",inputId.vendor);  

  printf("TK-------->>>>info.product is 0x%x\n",inputId.product);  

  printf("TK-------->>>>info.bustype is 0x%x\n",inputId.bustype);  

  /* 

  struct hidraw_devinfo info; 

  int rc = ioctl(fd, HIDIOCGRAWINFO, &info); 

  printf("TK-------->>>>info.vendor is 0x%x\n",info.vendor); 

  printf("TK-------->>>>info.product is 0x%x\n",info.product); 

  printf("TK-------->>>>info.bustype is 0x%x\n",info.bustype); 

  */  

  ///////////  

  struct hidraw_report_descriptor descriptor;  

  rc = ioctl(fd, HIDIOCGRDESC, &descriptor);  

  printf("TK-------->>>>descriptor.size is 0x%04x\n",descriptor.size);  

  //////  

  int descriptorSize=0;  

  rc = ioctl(fd, HIDIOCGRDESCSIZE, &descriptorSize);  

  printf("TK-------->>>>descriptorSize is 0x%04x\n",descriptorSize);    

  name[sizeof(name) - 1] = '\0';  

  if(ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {  

    name[0] = '\0';  

  printf("TK------->>>name is %s\n",name);  

  close(fd);  

  return 0;  

}  

二、编译

Android.mk

[plain] view plain copy 

LOCAL_PATH:= $(call my-dir)  

include $(CLEAR_VARS)  

LOCAL_SRC_FILES:= \  

    testhid.c  

LOCAL_SHARED_LIBRARIES := \  

    libutils   

LOCAL_MODULE:= testinput  

LOCAL_MODULE_TAGS := optional  

include $(BUILD_EXECUTABLE)  

三、运行

TK-------->>>>info.vendor is 0x0  

TK-------->>>>info.product is 0x1  

TK-------->>>>info.bustype is 0x19  

TK-------->>>>descriptor.size is 0x0000  

TK-------->>>>descriptorSize is 0x0000  

TK------->>>name is Power Button 

继续阅读