-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathdev.c
412 lines (339 loc) · 13 KB
/
dev.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "dev.h"
#include "hid_utility.h"
#include "utility.h"
#include <hidapi.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/**
* @brief Print information of devices
*
* When vendorid and productid are 0, then all are listed.
* If they are specified, information about all usagepages and interfaces of the respective device are listed
*
* @param vendorid
* @param productid
*/
static void print_devices(unsigned short vendorid, unsigned short productid)
{
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(vendorid, productid);
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n VendorID: %#06hx\n ProductID: %#06hx\n path: %s\n serial_number: %ls",
cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf(" Interface: %d\n", cur_dev->interface_number);
printf(" Usage-Page: 0x%hx Usageid: 0x%hx\n", cur_dev->usage_page, cur_dev->usage);
printf("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
}
/**
* @brief check if number inside range
*
* @param number to check
* @param low lower bound
* @param high upper bound
* @return int 0 if in range; 1 if too high; -1 if too low
*/
int check_range(int number, int low, int high)
{
if (number > high)
return 1;
if (number < low)
return -1;
return 0;
}
static void print_help()
{
printf("HeadsetControl Developer menu. Take caution.\n\n");
printf("Usage\n");
printf(" headsetcontrol --dev -- PARAMETERS\n");
printf("\n");
printf("Parameters\n");
printf(" --list\n"
"\tLists all HID devices when used without --device\n"
"\tWhen used with --device, searches for a specific device, and prints all interfaces and usageids\n");
printf(" --device VENDORID:PRODUCTID\n"
"\te.g. --device 1234:5678 or --device 0x4D2:0x162E\n"
"\tRequired for most parameters\n");
printf(" --interface INTERFACEID\n"
"\tWhich interface of the device to use. 0 is default\n");
printf(" --usage USAGEPAGE:USAGEID\n"
"\tSpecifies an usage-page and usageid. 0:0 is default\n"
"\tImportant for Windows. Ignored on Mac/Linux\n");
printf("\n");
printf(" --send DATA\n"
"\tSend data to specified device\n");
printf(" --send-feature DATA\n"
"\tSend a feature report to the device. The first byte is the reportid\n");
printf(" --sleep microsecounds\n"
"\tSleep for x microsecounds after sending\n");
printf(" --receive\n"
"\tTry to receive data from device. Can be combined with --timeout\n");
printf(" --timeout\n"
"\t--timeout in millisecounds for --receive\n");
printf(" --receive-feature REPORTID\n"
"\tTry to receive a report for REPORTID.\n");
printf(" --repeat SECS\n"
"\tRepeat command every SECS.\n");
printf("\n");
printf(" --dev-help\n"
"\tThis menu\n");
printf("\n");
printf("HINTS\n");
printf("\tsend and receive can be combined\n");
printf("\t--send does not return anything on success and is always executed first\n");
printf("\tDATA can be specified as single bytes, either as decimal or hex, seperated by spaces or commas or newlines\n");
printf("\n");
printf("EXAMPLEs\n");
printf(" headsetcontrol --dev -- --list\n");
printf(" headsetcontrol --dev -- --list --device 0x1b1c:0x1b27\n");
printf(" headsetcontrol --dev -- --device 0x1b1c:0x1b27 --send \"0xC9, 0x64\" --receive --timeout 10\n");
}
int dev_main(int argc, char* argv[])
{
int vendorid = 0;
int productid = 0;
int interfaceid = 0;
int usagepage = 0;
int usageid = 0;
int send = 0;
int send_feature = 0;
int sleep_time = -1;
int receive = 0;
int receivereport = 0;
int timeout = -1;
int repeat_seconds = 0;
int print_deviceinfo = 0;
#define BUFFERLENGTH 1024
unsigned char* sendbuffer = calloc(BUFFERLENGTH, sizeof(char));
unsigned char* sendreportbuffer = calloc(BUFFERLENGTH, sizeof(char));
unsigned char* receivebuffer = malloc(sizeof(char) * BUFFERLENGTH);
unsigned char* receivereportbuffer = malloc(sizeof(char) * BUFFERLENGTH);
struct option opts[] = {
{ "device", required_argument, NULL, 'd' },
{ "interface", required_argument, NULL, 'i' },
{ "usage", required_argument, NULL, 'u' },
{ "list", no_argument, NULL, 'l' },
{ "send", required_argument, NULL, 's' },
{ "send-feature", required_argument, NULL, 'f' },
{ "sleep", required_argument, NULL, 'm' },
{ "receive", no_argument, NULL, 'r' },
{ "receive-feature", required_argument, NULL, 'g' },
{ "timeout", required_argument, NULL, 't' },
{ "dev-help", no_argument, NULL, 'h' },
{ "repeat", required_argument, NULL, 0 },
{ 0, 0, 0, 0 }
};
int option_index = 0;
optind = 1; // getopt_long requires resetting this variable, we already used it in main()
int c;
while ((c = getopt_long(argc, argv, "d:i:lu:s:m:f:rg:th", opts, &option_index)) != -1) {
switch (c) {
case 'd': { // --device vendorid:productid
int ret = get_two_ids(optarg, &vendorid, &productid);
if (ret) {
fprintf(stderr, "You must supply a vendorid:productid pair in the --device / d parameter.\n\tE.g. --device 1234:5678 or --device 0x4D2:0x162E\n");
return 1;
}
if (check_range(vendorid, 1, 65535) != 0 || check_range(productid, 1, 65535) != 0) {
fprintf(stderr, "Vendor and Productid must be between 1 and 65535 or 0x1 and 0xffff\n");
return 1;
}
break;
}
case 'i': { // --interface interfaceid
interfaceid = strtol(optarg, NULL, 0);
if (interfaceid < 0) {
fprintf(stderr, "The interfaceid you supplied is invalid\n");
return 1;
}
break;
}
case 'u': { // --usage usagepage:usageid
int ret = get_two_ids(optarg, &usagepage, &usageid);
if (ret) {
fprintf(stderr, "You must supply a usagepage:usageid pair in the --usage / u parameter.\n\tE.g. --usage 1234:5678 or --usage 0x4D2:0x162E\n");
return 1;
}
if (check_range(vendorid, 1, 65535) != 0 || check_range(productid, 1, 65535) != 0) {
fprintf(stderr, "Usagepage and Usageid must be between 1 and 65535 or 0x1 and 0xffff\n");
return 1;
}
break;
}
case 's': { // --send string
int size = get_byte_data_from_parameter(optarg, sendbuffer, BUFFERLENGTH);
if (size < 0) {
fprintf(stderr, "Data to send larger than %d\n", BUFFERLENGTH);
return 1;
}
if (size == 0) {
fprintf(stderr, "No data specified to --send\n");
return 1;
}
send = size;
break;
}
case 'f': { // --send-feature string
int size = get_byte_data_from_parameter(optarg, sendreportbuffer, BUFFERLENGTH);
if (size < 0) {
fprintf(stderr, "Data to send for feature report larger than %d\n", BUFFERLENGTH);
return 1;
}
if (size == 0) {
fprintf(stderr, "No data specified to --send-feature\n");
return 1;
}
send_feature = size;
break;
}
case 'm': { // --sleep
sleep_time = strtol(optarg, NULL, 10);
if (sleep_time < 0) {
fprintf(stderr, "--sleep must be positive\n");
return 1;
}
break;
}
case 'r': { // --receive
receive = 1;
break;
}
case 'g': { // --receive-feature [reportid]
int reportid = 0;
reportid = strtol(optarg, NULL, 10);
if (reportid > 255 || reportid < 0) {
fprintf(stderr, "The reportid for --receive-feature must be smaller than 255\n");
return 1;
}
// the first byte of the receivereport buffer must be set for hidapi
receivereportbuffer[0] = reportid;
break;
}
case 't': { // --timeout timeout
timeout = strtol(optarg, NULL, 10);
if (timeout < -1) {
fprintf(stderr, "--timeout cannot be smaller than -1\n");
return 1;
}
break;
}
case 0: {
if (strcmp(opts[option_index].name, "repeat") == 0) { // --repeat SECS
repeat_seconds = strtol(optarg, NULL, 10);
if (repeat_seconds < 1) {
fprintf(stderr, "--repeat SECS cannot be smaller than 1\n");
return 1;
}
}
break;
}
case 'l': { // --list [vendorid:productid]
print_deviceinfo = 1;
break;
}
case 'h': {
print_help();
break;
}
default:
printf("Invalid argument %c\n", c);
}
}
if (argc <= 1)
print_help();
if (print_deviceinfo)
print_devices(vendorid, productid);
if (!(send || send_feature || receive || receivereport))
goto cleanup;
if (!vendorid || !productid) {
fprintf(stderr, "You must supply a vendor/productid pair via the parameter --device\n");
return 1;
}
char* hid_path = get_hid_path(vendorid, productid, interfaceid, usagepage, usageid);
hid_device* device_handle = NULL;
if (hid_path == NULL) {
fprintf(stderr, "Could not find a device with this parameters:\n");
fprintf(stderr, "\t Vendor (%#x) Product (%#x) Interface (%#x) UsagePage (%#x) UsageID (%#x)\n",
vendorid, productid, interfaceid, usagepage, usageid);
}
if (send && send_feature) {
fprintf(stderr, "Warning: --send and --send-feature specified at the same time\n");
}
if (receive && receivereport) {
fprintf(stderr, "Warning: --receive and --receive-feature specified at the same time\n");
}
device_handle = hid_open_path(hid_path);
if (device_handle == NULL) {
fprintf(stderr, "Couldn't open device.\n");
terminate_hid(&device_handle, &hid_path);
return 1;
}
do {
if (send) {
int ret = hid_write(device_handle, (const unsigned char*)sendbuffer, send);
if (ret < 0) {
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
}
}
if (send_feature) {
int ret = hid_send_feature_report(device_handle, (const unsigned char*)sendreportbuffer, send_feature);
if (ret < 0) {
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
}
}
if (sleep_time >= 0) { // also allow 0 as a minimum sleep
usleep(sleep_time * 1000);
}
if (receive) {
int read = hid_read_timeout(device_handle, receivebuffer, BUFFERLENGTH, timeout);
if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivebuffer[i]);
}
printf("\n");
}
}
if (receivereport) {
int read = hid_get_feature_report(device_handle, receivereportbuffer, BUFFERLENGTH);
if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivereportbuffer[i]);
}
printf("\n");
}
}
sleep(repeat_seconds);
} while (repeat_seconds);
terminate_hid(&device_handle, &hid_path);
cleanup:
free(receivereportbuffer);
free(receivebuffer);
free(sendreportbuffer);
free(sendbuffer);
return 0;
}