123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include <linux/types.h>
- #include <linux/input.h>
- #include <linux/hidraw.h>
- #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
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- 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];
-
- 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));
-
- res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
- if (res < 0)
- perror("HIDIOCGRDESCSIZE");
- else
- printf("Report Descriptor Size: %d\n", desc_size);
-
- 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");
- }
-
- res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
- if (res < 0)
- perror("HIDIOCGRAWNAME");
- else
- printf("Raw Name: %s\n", buf);
-
- res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
- if (res < 0)
- perror("HIDIOCGRAWPHYS");
- else
- printf("Raw Phys: %s\n", buf);
-
- 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);
- }
-
- buf[0] = 0x9;
- buf[1] = 0xff;
- buf[2] = 0xff;
- buf[3] = 0xff;
- res = ioctl(fd, HIDIOCSFEATURE(4), buf);
- if (res < 0)
- perror("HIDIOCSFEATURE");
- else
- printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
-
- buf[0] = 0x9;
- 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");
- }
-
- buf[0] = 0x1;
- 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);
- }
-
- 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;
- }
- }
|