forked from Gadgetoid/pi400kb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pikmreciever.c
367 lines (305 loc) · 7.81 KB
/
pikmreciever.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "pikmreciever.h"
#include "include/create-gadget-hid.h"
#include "include/remove-gadget-hid.h"
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <linux/input.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdbool.h>
#include <poll.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
// Submodule
#include <usbg/usbg.h>
// Library: apt install libhidapi
#include <hidapi/hidapi.h>
#define EVIOC_GRAB 1
#define EVIOC_UNGRAB 0
int hid_output;
volatile int running = 0;
volatile int grabbed = 0;
int res;
int device_fd;
int uinput_device_fd;
struct hid_buf device_buf;
uint16_t device_vid;
uint16_t device_pid;
struct hidraw_report_descriptor report_desc;
char device_dev[20];
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;
}
}
void signal_handler(int dummy)
{
running = 0;
}
bool modprobe_libcomposite()
{
pid_t pid;
pid = fork();
if (pid < 0)
return false;
if (pid == 0)
{
char *const argv[] = {"modprobe", "libcomposite"};
execv("/usr/sbin/modprobe", argv);
exit(0);
}
waitpid(pid, NULL, 0);
}
bool trigger_hook()
{
int ret;
char buf[4096];
snprintf(buf, sizeof(buf), "%s %u", HOOK_PATH, grabbed ? 1u : 0u);
ret = system(buf);
}
int find_hidraw_device()
{
int fd;
int res;
int desc_size = 0;
char path[20];
char buf[256];
struct hidraw_devinfo hidinfo;
struct hidraw_report_descriptor rpt_desc;
#define MAX_STR 255
wchar_t wstr[MAX_STR];
hid_device *handle;
for (int x = 0; x < 16; x++)
{
sprintf(path, "/dev/hidraw%d", x);
if ((fd = open(path, O_RDWR | O_NONBLOCK)) == -1)
{
continue;
}
memset(&rpt_desc, 0x0, sizeof(rpt_desc));
memset(&hidinfo, 0x0, sizeof(hidinfo));
memset(buf, 0x0, sizeof(buf));
/* Get Raw Info */
res = ioctl(fd, HIDIOCGRAWINFO, &hidinfo);
if (res < 0)
{
perror("HIDIOCGRAWINFO");
}
else
{
printf("Found a device at %s:\n", path);
printf("\tRaw Info:\n");
printf("\t\tbustype: %d (%s)\n", hidinfo.bustype, bus_str(hidinfo.bustype));
printf("\t\tvid: 0x%04hx\n", hidinfo.vendor);
printf("\t\tpid: 0x%04hx\n", hidinfo.product);
device_vid = hidinfo.vendor;
device_pid = hidinfo.product;
sprintf(device_dev, "%s", path);
/* Get Physical Location */
res = ioctl(fd, HIDIOCGRAWPHYS(256), buf);
if (res >= 0)
printf("\t\tRaw Phys: %s\n", buf);
/* Get Raw Name */
res = ioctl(fd, HIDIOCGRAWNAME(256), buf);
if (res >= 0)
printf("\t\tRaw Name: %s\n", buf);
/* Get Report Descriptor Size */
res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size);
if (res >= 0)
{
printf("\t\tReport Descriptor Size: %d\n", desc_size);
/* Get Report Descriptor */
rpt_desc.size = desc_size;
res = ioctl(fd, HIDIOCGRDESC, &rpt_desc);
if (res >= 0)
{
int new_line = 1;
printf("\t\tReport Descriptor:\n");
for (int i = 0; i < rpt_desc.size; i++)
{
printf("%d. 0x%02hx ", i, rpt_desc.value[i]);
new_line++;
if (new_line > 2 && rpt_desc.value[i + 1] == 0x00)
new_line--;
else if (rpt_desc.value[i + 1] == 0xc0)
new_line = 3;
if (new_line > 2)
{
printf("\n");
new_line = 1;
}
}
puts("\n");
}
report_desc = rpt_desc;
}
return fd;
}
close(fd);
}
return -1;
}
int grab(char *dev)
{
printf("Grabbing: %s\n", dev);
int fd = open(dev, O_RDONLY);
ioctl(fd, EVIOCGRAB, EVIOC_UNGRAB);
usleep(500000);
ioctl(fd, EVIOCGRAB, EVIOC_GRAB);
return fd;
}
void ungrab(int fd)
{
ioctl(fd, EVIOCGRAB, EVIOC_UNGRAB);
close(fd);
}
void printhex(unsigned char *buf, size_t len)
{
for (int x = 0; x < len; x++)
{
printf("%x ", buf[x]);
}
printf("\n");
}
void ungrab_both()
{
printf("Releasing Device\n");
if (uinput_device_fd > -1)
{
ungrab(uinput_device_fd);
}
grabbed = 0;
trigger_hook();
}
void grab_both()
{
printf("Grabbing Device\n");
if (device_fd > -1)
{
uinput_device_fd = grab(device_dev);
}
if (uinput_device_fd > -1)
{
grabbed = 1;
}
trigger_hook();
}
void send_empty_hid_reports_both()
{
ssize_t res;
if (device_fd > -1)
{
#ifndef NO_OUTPUT
memset(device_buf.data, 0, KEYBOARD_HID_REPORT_SIZE);
res = write(hid_output, (unsigned char *)&device_buf, KEYBOARD_HID_REPORT_SIZE + 1);
#endif
}
}
int main()
{
modprobe_libcomposite();
device_buf.report_id = 1;
device_fd = find_hidraw_device();
if (device_fd == -1)
{
printf("No device to forward, bailing out!\n");
return 1;
}
#ifndef NO_OUTPUT
// Remove previous device if available
res = remove_gadget(&device_vid, &device_pid);
// Recreate device using new detail
res = initUSB(&device_vid, &device_pid, &report_desc);
if (res != USBG_SUCCESS && res != USBG_ERROR_EXIST && res != USBG_ERROR_BUSY)
{
return 1;
}
#endif
grab_both();
#ifndef NO_OUTPUT
do
{
hid_output = open("/dev/hidg0", O_WRONLY | O_NDELAY);
} while (hid_output == -1 && errno == EINTR);
if (hid_output == -1)
{
printf("Error opening /dev/hidg0 for writing.\n");
return 1;
}
#endif
printf("Running...\n");
running = 1;
signal(SIGINT, signal_handler);
struct pollfd pollFd[1];
pollFd[0].fd = device_fd;
pollFd[0].events = POLLIN;
while (running)
{
poll(pollFd, 1, -1);
if (device_fd > -1)
{
int c = read(device_fd, device_buf.data, KEYBOARD_HID_REPORT_SIZE);
if (c == KEYBOARD_HID_REPORT_SIZE)
{
printf("K:");
printhex(device_buf.data, KEYBOARD_HID_REPORT_SIZE);
#ifndef NO_OUTPUT
if (grabbed)
{
res = write(hid_output, (unsigned char *)&device_buf, KEYBOARD_HID_REPORT_SIZE + 1);
usleep(1000);
}
#endif
// Trap Ctrl + Raspberry and toggle capture on/off
if (device_buf.data[0] == 0x09)
{
if (grabbed)
{
ungrab_both();
send_empty_hid_reports_both();
}
else
{
grab_both();
}
}
// Trap Ctrl + Shift + Raspberry and exit
if (device_buf.data[0] == 0x0b)
{
running = 0;
break;
}
}
}
}
ungrab_both();
send_empty_hid_reports_both();
#ifndef NO_OUTPUT
printf("Cleanup USB\n");
cleanupUSB();
#endif
return 0;
}