-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhid.c
258 lines (209 loc) · 5.42 KB
/
hid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* This file is part of hid_mapper.
*
* hid_mapper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* hid_mapper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hid_mapper. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Thibault Kummer <[email protected]>
*/
#include <hid.h>
#include <log.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#define SYSFS_HIDRAW_CLASS_PATH "/sys/class/hidraw"
void init_hid_device(struct st_hid_device *hid_device)
{
int i;
hid_device->num_interfaces = 0;
for(i=0;i<HID_MAX_INTERFACES;i++)
hid_device->interface_fd[i] = -1;
hid_device->interface_fd_max = -1;
}
static int get_file_contents(const char *filename,char *buf, unsigned int buf_len)
{
int fd,re;
fd = open(filename,O_RDONLY);
if(fd<0)
return -1;
re = read(fd,buf,buf_len);
close(fd);
if(re<0 || re>=buf_len)
return -1;
buf[re] = '\0';
if(re>0 && buf[re-1]=='\n')
buf[re-1] = '\0';
return 0;
}
int lookup_hid_product(int lookup_mode,const char *manufacturer,const char *product,struct st_hid_device *hid_device)
{
DIR *dh;
struct dirent *entry;
char buf[256],filename[PATH_MAX],symlink_filename[PATH_MAX];
int re;
if(manufacturer!=0)
hid_device->num_interfaces = 0;
// Open sys FS to lookup all HID devices
dh = opendir(SYSFS_HIDRAW_CLASS_PATH);
if(dh==0)
return -1;
while(entry = readdir(dh))
{
re = snprintf(filename,PATH_MAX,SYSFS_HIDRAW_CLASS_PATH "/%s",entry->d_name);
if(re>=PATH_MAX)
{
closedir(dh);
return -1;
}
if(entry->d_type!=DT_LNK)
continue;
re = readlink(filename,symlink_filename,PATH_MAX);
if(re>0 && re<PATH_MAX)
{
buf[re] = '\0';
if(manufacturer==0)
info("Found HID device at /dev/%s",entry->d_name);
// Read vendor name
if(lookup_mode==LOOKUP_MODE_NAME)
re = snprintf(filename,PATH_MAX,SYSFS_HIDRAW_CLASS_PATH "/%s/../../../../manufacturer",symlink_filename);
else if(lookup_mode==LOOKUP_MODE_ID)
re = snprintf(filename,PATH_MAX,SYSFS_HIDRAW_CLASS_PATH "/%s/../../../../idVendor",symlink_filename);
if(re>=PATH_MAX)
{
closedir(dh);
return -1;
}
if(get_file_contents(filename,buf,256)<0)
{
if(manufacturer==0)
printf(" Manufacturer : Unknown\n");
else
{
closedir(dh);
return -1;
}
}
else
{
if(manufacturer==0)
printf(" Manufacturer : %s\n",buf);
else if(strcmp(buf,manufacturer)!=0)
continue;
}
// Read product name
if(lookup_mode==LOOKUP_MODE_NAME)
re = snprintf(filename,PATH_MAX,SYSFS_HIDRAW_CLASS_PATH "/%s/../../../../product",symlink_filename);
else if(lookup_mode==LOOKUP_MODE_ID)
re = snprintf(filename,PATH_MAX,SYSFS_HIDRAW_CLASS_PATH "/%s/../../../../idProduct",symlink_filename);
if(re>=PATH_MAX)
{
closedir(dh);
return -1;
}
if(get_file_contents(filename,buf,256)<0)
{
if(manufacturer==0)
{
printf(" Product name : Unknown\n\n");
continue;
}
else
{
closedir(dh);
return -1;
}
}
if(manufacturer==0)
printf(" Product name : %s\n\n",buf);
else if(strcmp(buf,product)==0)
{
if(hid_device->num_interfaces>=HID_MAX_INTERFACES)
{
closedir(dh);
return -2;
}
// We found one interface of HID device
re = snprintf(hid_device->interface_device[hid_device->num_interfaces],PATH_MAX,"/dev/%s",entry->d_name);
if(re>=PATH_MAX)
{
closedir(dh);
return -1;
}
hid_device->num_interfaces++;
}
}
}
closedir(dh);
if(manufacturer==0)
return 0;
if(hid_device->num_interfaces>0)
return 0;
return -1;
}
int open_hid_device(struct st_hid_device *hid_device)
{
int i;
hid_device->interface_fd_max = -1;
for(i=0;i<hid_device->num_interfaces;i++)
{
hid_device->interface_fd[i] = open(hid_device->interface_device[i],O_RDONLY);
if(hid_device->interface_fd[i]<0)
{
close_hid_device(hid_device);
return -1;
}
if(hid_device->interface_fd[i]>hid_device->interface_fd_max)
hid_device->interface_fd_max = hid_device->interface_fd[i];
info("Opened HID interface on %s",hid_device->interface_device[i]);
}
return 0;
}
int close_hid_device(struct st_hid_device *hid_device)
{
int i,exit_code;
exit_code = 0;
for(i=0;i<hid_device->num_interfaces;i++)
{
if(hid_device->interface_device[i]>=0)
{
if(close(hid_device->interface_fd[i])<0)
exit_code = -1;
else
hid_device->interface_fd[i] = -1;
}
}
return exit_code;
}
int read_hid_event(struct st_hid_device *hid_device,char *event,unsigned int *length)
{
fd_set rfds;
int i,re;
FD_ZERO(&rfds);
for(i=0;i<hid_device->interface_fd[i];i++)
FD_SET(hid_device->interface_fd[i],&rfds);
re = select(hid_device->interface_fd_max+1,&rfds,NULL,NULL,NULL);
if(re<0)
return -1;
for(i=0;i<hid_device->interface_fd[i];i++)
{
if(FD_ISSET(hid_device->interface_fd[i],&rfds))
{
re = read(hid_device->interface_fd[i],event,*length);
if(re<=0)
return -1;
*length = re;
}
}
return 0;
}