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
* Ugly hack to work around failing compilation on systems that don't
19
* yet populate new version of hidraw.h to userspace.
21
* If you need this, please have your distro update the kernel headers.
23
#ifndef HIDIOCSFEATURE
24
#define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
25
#define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
29
#include <sys/ioctl.h>
30
#include <sys/types.h>
41
const char *bus_str(int bus);
43
int main(int argc, char **argv)
46
int i, res, desc_size = 0;
48
struct hidraw_report_descriptor rpt_desc;
49
struct hidraw_devinfo info;
51
/* Open the Device with non-blocking reads. In real life,
52
don't use a hard coded path; use libudev instead. */
53
fd = open("/dev/hidraw0", O_RDWR|O_NONBLOCK);
56
perror("Unable to open device");
60
memset(&rpt_desc, 0x0, sizeof(rpt_desc));
61
memset(&info, 0x0, sizeof(info));
62
memset(buf, 0x0, sizeof(buf));
64
/* Get Report Descriptor Size */
65
res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
67
perror("HIDIOCGRDESCSIZE");
69
printf("Report Descriptor Size: %d\n", desc_size);
71
/* Get Report Descriptor */
72
rpt_desc.size = desc_size;
73
res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
75
perror("HIDIOCGRDESC");
77
printf("Report Descriptor:\n");
78
for (i = 0; i < rpt_desc.size; i++)
79
printf("%hhx ", rpt_desc.value[i]);
84
res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
86
perror("HIDIOCGRAWNAME");
88
printf("Raw Name: %s\n", buf);
90
/* Get Physical Location */
91
res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
93
perror("HIDIOCGRAWPHYS");
95
printf("Raw Phys: %s\n", buf);
98
res = ioctl(fd, HIDIOCGRAWINFO, &info);
100
perror("HIDIOCGRAWINFO");
102
printf("Raw Info:\n");
103
printf("\tbustype: %d (%s)\n",
104
info.bustype, bus_str(info.bustype));
105
printf("\tvendor: 0x%04hx\n", info.vendor);
106
printf("\tproduct: 0x%04hx\n", info.product);
110
buf[0] = 0x9; /* Report Number */
114
res = ioctl(fd, HIDIOCSFEATURE(4), buf);
116
perror("HIDIOCSFEATURE");
118
printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
121
buf[0] = 0x9; /* Report Number */
122
res = ioctl(fd, HIDIOCGFEATURE(256), buf);
124
perror("HIDIOCGFEATURE");
126
printf("ioctl HIDIOCGFEATURE returned: %d\n", res);
127
printf("Report data (not containing the report number):\n\t");
128
for (i = 0; i < res; i++)
129
printf("%hhx ", buf[i]);
133
/* Send a Report to the Device */
134
buf[0] = 0x1; /* Report Number */
136
res = write(fd, buf, 2);
138
printf("Error: %d\n", errno);
141
printf("write() wrote %d bytes\n", res);
144
/* Get a report from the device */
145
res = read(fd, buf, 16);
149
printf("read() read %d bytes:\n\t", res);
150
for (i = 0; i < res; i++)
151
printf("%hhx ", buf[i]);