-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
186 lines (162 loc) · 5.99 KB
/
client.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
/*
* raw access to the usb device
* To use this program you must unbind the usb device
* which will be claimed by hidraw.
*
* $ echo -n 4-1.5.2:1.0 | sudo tee /sys/bus/usb/drivers/usbhid/unbind
*/
#include <libusb.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#define DEFAULT_STATE 0
#define ADDRESS_STATE 1
#define CONFIG_STATE 2
#define DEVICE 0x80
#define INTERFACE 0x81
#define ENDPOINT 0x82
#define DIRIN 0x80
#define DIROUT 0x00
#define GET_STATUS 0x00
#define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNCH_FRAME 0x0C
struct request
{
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
int shouldfail;
} addressed[] = {
{ DIROUT|DEVICE, GET_STATUS, 0, 0, 2, 0 }, /* 0 */
{ DIROUT|INTERFACE, GET_STATUS, 0, 0, 2, 0 }, /* 1 */
{ DIROUT|ENDPOINT, GET_STATUS, 0, 0, 2, 0 }, /* 2 */
{ DIROUT|ENDPOINT, GET_STATUS, 0, 0x80, 2, 0 }, /* 3 */
{ DIROUT|ENDPOINT, GET_STATUS, 0, 1, 2, 1 }, /* 4 */
{ DIROUT|ENDPOINT, GET_STATUS, 0, 0x81, 2, 1 }, /* 5 */
{ DIROUT|DEVICE, SET_FEATURE, 1, 0, 0, 0 }, /* 6 */
{ DIROUT|INTERFACE, SET_FEATURE, 0, 0, 0, 0 }, /* 7 */
{ DIROUT|ENDPOINT, SET_FEATURE, 1, 0, 0, 0 }, /* 8 */
{ DIROUT|ENDPOINT, SET_FEATURE, 1, 0x80, 0, 0 }, /* 9 */
{ DIROUT|ENDPOINT, SET_FEATURE, 1, 1, 0, 1 }, /* 10 */
{ DIROUT|ENDPOINT, SET_FEATURE, 1, 0x81, 0, 1 }, /* 11 */
{ DIROUT|DEVICE, CLEAR_FEATURE, 1, 0, 0, 0 }, /* 12 */
{ DIROUT|INTERFACE, CLEAR_FEATURE, 0, 0, 0, 0 }, /* 13 */
{ DIROUT|ENDPOINT, CLEAR_FEATURE, 1, 0, 0, 0 }, /* 14 */
{ DIROUT|ENDPOINT, CLEAR_FEATURE, 1, 0x80, 0, 0 }, /* 15 */
{ DIROUT|ENDPOINT, CLEAR_FEATURE, 1, 1, 0, 1 }, /* 16 */
{ DIROUT|ENDPOINT, CLEAR_FEATURE, 1, 0x81, 0, 1 }, /* 17 */
};
static int get_status(libusb_device_handle *handle, uint8_t target, uint8_t idx)
{
uint16_t status;
int r = libusb_control_transfer(
handle,
0x80 | target, /* bmRequestType */
0x00, /* bRequest */
0, /* wValue */
idx, /* wIndex */
(uint8_t *)&status, /* data */
2, /* wLength */
100); /* timeout */
if (r < 0) {
fprintf(stderr, "GET_STATUS target: %2x idx: %2x error\n", target, idx);
return r;
}
return status;
}
static int set_portb(libusb_device_handle *handle, uint8_t value)
{
return libusb_control_transfer(
handle,
LIBUSB_RECIPIENT_DEVICE | LIBUSB_REQUEST_TYPE_VENDOR,
0, /* bRequest (SET) */
value, /* wValue (RB3) */
0, /* wIndex */
NULL, /* data */
0, /* wLength */
100);
}
int main(int argc, char *argv[])
{
libusb_context *ctx = NULL;
if (libusb_init(&ctx) < 0) {
fprintf(stderr, "Cannot initialize libusb\n");
return EXIT_FAILURE;
}
/* set the verbosity level of libusb */
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
libusb_device_handle *handle;
handle = libusb_open_device_with_vid_pid(ctx, 0x04D8, 0x0001);
if (!handle) {
fprintf(stderr, "cannot open the device\n");
goto fail;
}
if (libusb_set_configuration(handle, 1) < 0) {
fprintf(stderr, "cannot set the configuration\n");
goto fail_close;
}
if (libusb_claim_interface(handle, 0) < 0) {
fprintf(stderr, "cannot claim the interface\n");
goto fail_close;
}
int status = get_status(handle, 0, 0);
if (status >= 0) {
fprintf(stdout, "GET_STATUS (device ): %04x\n", status);
}
status = get_status(handle, 1, 0);
if (status >= 0) {
fprintf(stdout, "GET_STATUS (interface0): %04x\n", status);
}
status = get_status(handle, 2, 0x80);
if (status >= 0) {
fprintf(stdout, "GET_STATUS (ep0 IN ): %04x\n", status);
}
status = get_status(handle, 2, 0x00);
if (status >= 0) {
fprintf(stdout, "GET_STATUS (ep0 OUT ): %04x\n", status);
}
/* configured state */
uint8_t buf[512];
for (int i = 0; i < sizeof(addressed)/sizeof(addressed[0]); i++) {
struct request *req = addressed + i;
int r = libusb_control_transfer(
handle,
req->bmRequestType,
req->bRequest,
req->wValue,
req->wIndex,
buf,
req->wLength,
100);
if (req->shouldfail && r == 0) {
fprintf(stderr, "Request %d didn't fail\n", i);
} else if (r < 0) {
fprintf(stderr, "Request %d failed %d\n", i, r);
}
}
for (int i = 0; i < 1000; i++) {
if (set_portb(handle, (i&1) ? 3<<3 : 0) < 0) {
fprintf(stderr, "cannot send vendor request %i\n", i);
}
sleep(1);
}
libusb_close(handle);
libusb_exit(ctx);
return EXIT_SUCCESS;
fail_close:
libusb_close(handle);
fail:
libusb_exit(ctx);
return EXIT_FAILURE;
}