Blame view

samples/hidraw/hid-example.c 3.87 KB
c54ea4918   Alan Ott   HID: Documentatio...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   * Hidraw Userspace Example
   *
   * Copyright (c) 2010 Alan Ott <alan@signal11.us>
   * 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 <linux/types.h>
  #include <linux/input.h>
  #include <linux/hidraw.h>
cb3e85fe1   Jiri Kosina   HID: hidraw: fix ...
16
17
18
  /*
   * Ugly hack to work around failing compilation on systems that don't
   * yet populate new version of hidraw.h to userspace.
cb3e85fe1   Jiri Kosina   HID: hidraw: fix ...
19
20
   */
  #ifndef HIDIOCSFEATURE
f37130533   Jiri Kosina   HID: hidraw: warn...
21
  #warning Please have your distro update the userspace kernel headers
cb3e85fe1   Jiri Kosina   HID: hidraw: fix ...
22
23
24
  #define HIDIOCSFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
  #define HIDIOCGFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
  #endif
c54ea4918   Alan Ott   HID: Documentatio...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /* Unix */
  #include <sys/ioctl.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <unistd.h>
  
  /* C */
  #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;
04303f8ec   Pavel Machek   HID: samples/hidr...
47
48
49
50
  	char *device = "/dev/hidraw0";
  
  	if (argc > 1)
  		device = argv[1];
c54ea4918   Alan Ott   HID: Documentatio...
51
52
53
  
  	/* Open the Device with non-blocking reads. In real life,
  	   don't use a hard coded path; use libudev instead. */
04303f8ec   Pavel Machek   HID: samples/hidr...
54
  	fd = open(device, O_RDWR|O_NONBLOCK);
c54ea4918   Alan Ott   HID: Documentatio...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  
  	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
  ", 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:
  ");
  		for (i = 0; i < rpt_desc.size; i++)
  			printf("%hhx ", rpt_desc.value[i]);
  		puts("
  ");
  	}
  
  	/* Get Raw Name */
  	res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
  	if (res < 0)
  		perror("HIDIOCGRAWNAME");
  	else
  		printf("Raw Name: %s
  ", buf);
  
  	/* Get Physical Location */
  	res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
  	if (res < 0)
  		perror("HIDIOCGRAWPHYS");
  	else
  		printf("Raw Phys: %s
  ", buf);
  
  	/* Get Raw Info */
  	res = ioctl(fd, HIDIOCGRAWINFO, &info);
  	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);
  	}
  
  	/* 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 HIDIOCGFEATURE returned: %d
  ", 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
  ", 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;
  	}
  }