hid read c Linux,代码阅读 - Linux\kernel\linux-5.0\samples\hidraw\hid-example.c

// SPDX-License-Identifier: GPL-2.0

/*

* Hidraw Userspace Example

*

* Copyright (c) 2010 Alan Ott * Copyright (c) 2010 Signal 11 Software

*

* The code may be used by anyone for any purpose,

* and can serve as a starting point for developing

* applications using hidraw.

*/

/* Linux */

#include

#include

#include

/*

* Ugly hack to work around failing compilation on systems that don't

* yet populate new version of hidraw.h to userspace.

*/

#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);

int main(int argc, char **argv)

{

int fd;

int i, res, desc_size = 0;

char buf[256];

struct hidraw_report_descriptor rpt_desc;

struct hidraw_devinfo info;

char *device = "/dev/hidraw0";

if (argc > 1)

device = argv[1];

/* 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);

if (fd < 0) {

perror("Unable to open device");

return 1;

}

memset(&rpt_desc, 0x0, sizeof(rpt_desc));

memset(&info, 0x0, sizeof(info));

memset(buf, 0x0, sizeof(buf));

/* Get Report Descriptor Size */

res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);

if (res < 0)

perror("HIDIOCGRDESCSIZE");

else

printf("Report Descriptor Size: %d\n", desc_size);

/* Get Report Descriptor */

rpt_desc.size = desc_size;

res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);

if (res < 0) {

perror("HIDIOCGRDESC");

} else {

printf("Report Descriptor:\n");

for (i = 0; i < rpt_desc.size; i++)

printf("%hhx ", rpt_desc.value[i]);

puts("\n");

}

/* Get Raw Name */

res = ioctl(fd, HIDIOCGRAWNAME(256), buf);

if (res < 0)

perror("HIDIOCGRAWNAME");

else

printf("Raw Name: %s\n", buf);

/* Get Physical Location */

res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);

if (res < 0)

perror("HIDIOCGRAWPHYS");

else

printf("Raw Phys: %s\n", buf);

/* Get Raw Info */

res = ioctl(fd, HIDIOCGRAWINFO, &info);

if (res < 0) {

perror("HIDIOCGRAWINFO");

} else {

printf("Raw Info:\n");

printf("\tbustype: %d (%s)\n",

info.bustype, bus_str(info.bustype));

printf("\tvendor: 0x%04hx\n", info.vendor);

printf("\tproduct: 0x%04hx\n", info.product);

}

/* Set Feature */

buf[0] = 0x9; /* Report Number */

buf[1] = 0xff;

buf[2] = 0xff;

buf[3] = 0xff;

res = ioctl(fd, HIDIOCSFEATURE(4), buf);

if (res < 0)

perror("HIDIOCSFEATURE");

else

printf("ioctl HIDIOCSFEATURE returned: %d\n", res);

/* Get Feature */

buf[0] = 0x9; /* Report Number */

res = ioctl(fd, HIDIOCGFEATURE(256), buf);

if (res < 0) {

perror("HIDIOCGFEATURE");

} else {

printf("ioctl HIDIOCGFEATURE returned: %d\n", res);

printf("Report data (not containing the report number):\n\t");

for (i = 0; i < res; i++)

printf("%hhx ", buf[i]);

puts("\n");

}

/* 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\n", errno);

perror("write");

} else {

printf("write() wrote %d bytes\n", res);

}

/* Get a report from the device */

res = read(fd, buf, 16);

if (res < 0) {

perror("read");

} else {

printf("read() read %d bytes:\n\t", res);

for (i = 0; i < res; i++)

printf("%hhx ", buf[i]);

puts("\n");

}

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值