2 * Hidraw Userspace Example
4 * Copyright (c) 2010 Alan Ott <alan@signal11.us>
5 * Copyright (c) 2010 Signal 11 Software
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using hidraw.
13 #include <linux/types.h>
14 #include <linux/input.h>
15 #include <linux/hidraw.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
30 const char *bus_str(int bus);
32 int main(int argc, char **argv)
35 int i, res, desc_size = 0;
37 struct hidraw_report_descriptor rpt_desc;
38 struct hidraw_devinfo info;
40 /* Open the Device with non-blocking reads. In real life,
41 don't use a hard coded path; use libudev instead. */
42 fd = open("/dev/hidraw0", O_RDWR|O_NONBLOCK);
45 perror("Unable to open device");
49 memset(&rpt_desc, 0x0, sizeof(rpt_desc));
50 memset(&info, 0x0, sizeof(info));
51 memset(buf, 0x0, sizeof(buf));
53 /* Get Report Descriptor Size */
54 res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
56 perror("HIDIOCGRDESCSIZE");
58 printf("Report Descriptor Size: %d\n", desc_size);
60 /* Get Report Descriptor */
61 rpt_desc.size = desc_size;
62 res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
64 perror("HIDIOCGRDESC");
66 printf("Report Descriptor:\n");
67 for (i = 0; i < rpt_desc.size; i++)
68 printf("%hhx ", rpt_desc.value[i]);
73 res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
75 perror("HIDIOCGRAWNAME");
77 printf("Raw Name: %s\n", buf);
79 /* Get Physical Location */
80 res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
82 perror("HIDIOCGRAWPHYS");
84 printf("Raw Phys: %s\n", buf);
87 res = ioctl(fd, HIDIOCGRAWINFO, &info);
89 perror("HIDIOCGRAWINFO");
91 printf("Raw Info:\n");
92 printf("\tbustype: %d (%s)\n",
93 info.bustype, bus_str(info.bustype));
94 printf("\tvendor: 0x%04hx\n", info.vendor);
95 printf("\tproduct: 0x%04hx\n", info.product);
99 buf[0] = 0x9; /* Report Number */
103 res = ioctl(fd, HIDIOCSFEATURE(4), buf);
105 perror("HIDIOCSFEATURE");
107 printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
110 buf[0] = 0x9; /* Report Number */
111 res = ioctl(fd, HIDIOCGFEATURE(256), buf);
113 perror("HIDIOCGFEATURE");
115 printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
116 printf("Report data (not containing the report number):\n\t");
117 for (i = 0; i < res; i++)
118 printf("%hhx ", buf[i]);
122 /* Send a Report to the Device */
123 buf[0] = 0x1; /* Report Number */
125 res = write(fd, buf, 2);
127 printf("Error: %d\n", errno);
130 printf("write() wrote %d bytes\n", res);
133 /* Get a report from the device */
134 res = read(fd, buf, 16);
138 printf("read() read %d bytes:\n\t", res);
139 for (i = 0; i < res; i++)
140 printf("%hhx ", buf[i]);