링크 ux hidraw 장치 c 언어 버 전 찾기

34228 단어 ubuntu
링크 ux hidraw 장치 c 언어 버 전 찾기
메모: 장치 조작 으로 인해 sudo 권한 이 필요 합 니 다.


///------
///    sudo   
///    /dev   ,   hidraw   ,      
///------







/* Linux */
#include 
#include 
#include 
#include 

#ifndef HIDIOCSFEATURE
#warning Please have your distro update the userspace kernel headers
#define HIDIOCSFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
#define HIDIOCGFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
#endif

/* Unix */
#include 
#include 
#include 
#include 
#include 

/* C */
#include 
#include 
#include 
#include 

const char *bus_str(int bus);


#define HID_RAW "/dev/hidraw"

int main(int argc, char **argv)
{
    printf("\033[1;31;43m run this need sudo !!! \033[0;m  
"
); int fd; int i, res, desc_size = 0; char buf[256]; struct hidraw_report_descriptor rpt_desc; struct hidraw_devinfo info; char device[32] = HID_RAW; DIR *dir = NULL; struct dirent *ptr; /* Open dir /dev. */ dir = opendir("/dev");//------------------------------------------------- /dev 。 if(!dir) { perror("Popen dir failed..."); return -ENOENT; } /* */ while(ptr = readdir(dir)) {//-- /dev 。 if(ptr->d_type != DT_CHR){//-- , DT_UNKNOWN/DT_FIFO/DT_CHR DT_DIR/DT_BLK/DT_REG/DT_LNK/DT_SOCK/DT_WHT。 continue; } if(!strncmp(ptr->d_name, "hidraw", 6)) {//-- hidraw snprintf(device, sizeof(device), "%s/%s", "/dev", ptr->d_name); /* Open the Device with non-blocking reads. In real life, don't use a hard coded path; use libudev instead. */ fd = open(device, O_RDWR|O_NONBLOCK);//-------------------------- hidraw , hidraw_open()。 if (fd < 0) { printf("\033[1;31;43m Unable to open device %s \033[0;m
"
, device); printf("\033[1;31;43m run this need sudo !!! \033[0;m
"
); return 1; } memset(&rpt_desc, 0x0, sizeof(rpt_desc)); memset(&info, 0x0, sizeof(info)); memset(buf, 0x0, sizeof(buf)); printf("

=================================Device %s info=================================
"
, device); /* Get Raw Name */ res = ioctl(fd, HIDIOCGRAWNAME(256), buf);//--------------------- hidraw_ioctl() HIDIOCGRAWNAME。 if (res < 0) perror("HIDIOCGRAWNAME"); else printf("Raw Name: %s
"
, buf); /* Get Physical Location */ res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);//--------------------- HIDIOCGRAWPHYS。 if (res < 0) perror("HIDIOCGRAWPHYS"); else printf("Raw Phys: %s
"
, buf); /* Get Raw Info */ res = ioctl(fd, HIDIOCGRAWINFO, &info);//------------------------- HIDIOCGRAWINFO。 if (res < 0) { perror("HIDIOCGRAWINFO"); } else { printf("Raw Info:
"
); printf("\tbustype: %d (%s)
"
, info.bustype, bus_str(info.bustype)); printf("\tvendor: 0x%04hx
"
, info.vendor); printf("\tproduct: 0x%04hx
"
, info.product); } /* Get Report Descriptor Size */ res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);//------------------ HIDIOCGRDESCSIZE if (res < 0) perror("HIDIOCGRDESCSIZE"); else printf("Report Descriptor Size: %d
"
, desc_size); /* Get Report Descriptor */ rpt_desc.size = desc_size; res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);//------------------------ HIDIOCGRDESC。 if (res < 0) { perror("HIDIOCGRDESC"); } else { printf("Report Descriptor:
"
); for (i = 0; i < rpt_desc.size; i++) printf("%hhx ", rpt_desc.value[i]); puts("
"
); } /* Set Feature */ buf[0] = 0x9; /* Report Number */ buf[1] = 0xff; buf[2] = 0xff; buf[3] = 0xff; res = ioctl(fd, HIDIOCSFEATURE(4), buf);//------------------------- HIDIOCSFEATURE。 if (res < 0) perror("HIDIOCSFEATURE"); else printf("ioctl HIDIOCGFEATURE returned: %d
"
, res); /* Get Feature */ buf[0] = 0x9; /* Report Number */ res = ioctl(fd, HIDIOCGFEATURE(256), buf);//----------------------- HIDIOCGFEATURE。 if (res < 0) { perror("HIDIOCGFEATURE"); } else { printf("ioctl HIDIOCGFEATURE returned: %d
"
, res); printf("Report data (not containing the report number):
\t"
); for (i = 0; i < res; i++) printf("%hhx ", buf[i]); puts("
"
); } /* Send a Report to the Device */ buf[0] = 0x1; /* Report Number */ buf[1] = 0x77; res = write(fd, buf, 2); if (res < 0) { printf("Error: %d
"
, errno); perror("write"); } else { printf("write() wrote %d bytes
"
, res); } /* Get a report from the device */ res = read(fd, buf, 16); if (res < 0) { perror("read"); } else { printf("read() read %d bytes:
\t"
, res); for (i = 0; i < res; i++) printf("%hhx ", buf[i]); puts("
"
); } close(fd); } } return 0; } const char * bus_str(int bus) { switch (bus) { case BUS_USB: return "USB"; break; case BUS_HIL: return "HIL"; break; case BUS_BLUETOOTH: return "Bluetooth"; break; case BUS_VIRTUAL: return "Virtual"; break; default: return "Other"; break; } }

좋은 웹페이지 즐겨찾기