-
Notifications
You must be signed in to change notification settings - Fork 5
/
usbacc.c
executable file
·339 lines (305 loc) · 6.89 KB
/
usbacc.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
/**
*
* filename: usbacc.c
* brief: Use libusb to emulate usb android open accesory
*
*/
#include <stdio.h>
#include <usb.h>
#include <libusb.h>
#include <string.h>
#include <unistd.h>
#define EP_IN 0x81
#define EP_OUT 0x02
#define LG_VID 0x1004
#define PID 0x633e
#define GOOGLE_VID 0x18d1
#define ACCESSORY_PID 0x2d01
#define ACCESSORY_PID_ALT 0x2d00
#define AOA_BUFF_MAX 16384
#define LEN 2
int init(void);
int deInit(void);
int setupAccessory(void);
int usbSendCtrl(char *buff, int req, int index);
void error(int code);
void status(int code);
void *usbRWHdlr(void * threadarg);
struct libusb_device_handle* handle;
char stop;
char success = 0;
struct usbAccessory {
char* manufacturer;
char* modelName;
char* description;
char* version;
char* uri;
char* serialNumber;
};
struct usbAccessory gadgetAccessory = {
"GitHub",
"RacquetScience",
"Description",
"1.0",
"https://github.com",
"01linuxSerialNo."
};
int main(int argc, char *argv[])
{
pthread_t tid;
if (init() < 0)
return;
if (setupAccessory() < 0) {
fprintf(stdout, "Error setting up accessory\n");
deInit();
return -1;
}
pthread_create(&tid, NULL, usbRWHdlr, NULL);
pthread_join(tid, NULL);
deInit();
fprintf(stdout, "Done, no errors\n");
return 0;
}
void *usbRWHdlr(void * threadarg)
{
int response, transferred;
unsigned int index = 0;
unsigned char inBuff[AOA_BUFF_MAX] = {0};
unsigned char outBuff[AOA_BUFF_MAX] = {0};
for (;;) {
response =
libusb_bulk_transfer(handle, EP_IN, inBuff, sizeof(inBuff), &transferred, 0);
if (response < 0) {
error(response);
return NULL;
}
fprintf(stdout, "msg: %s\n", inBuff);
sprintf(outBuff, "ACK: %07d", index++);
response =
libusb_bulk_transfer(handle, EP_OUT, outBuff, strlen(outBuff), &transferred, 0);
if (response < 0) {
error(response);
return NULL;
}
sleep(1);
}
}
int init()
{
libusb_init(NULL);
if ((handle = libusb_open_device_with_vid_pid(NULL, LG_VID, PID)) == NULL) {
fprintf(stdout, "Problem acquireing handle\n");
return -1;
}
libusb_claim_interface(handle, 0);
return 0;
}
int deInit()
{
if (handle != NULL) {
libusb_release_interface(handle, 0);
libusb_close(handle);
}
libusb_exit(NULL);
return 0;
}
int usbSendCtrl(char *buff, int req, int index)
{
int response = 0;
if (NULL != buff) {
response =
libusb_control_transfer(handle, 0x40, req, 0, index, buff, strlen(buff) + 1 , 0);
} else {
response =
libusb_control_transfer(handle, 0x40, req, 0, index, buff, 0, 0);
}
if (response < 0) {
error(response);
return -1;
}
}
int setupAccessory()
{
unsigned char ioBuffer[2];
int devVersion;
int response;
int tries = 5;
response = libusb_control_transfer(
handle, //handle
0xC0, //bmRequestType
51, //bRequest
0, //wValue
0, //wIndex
ioBuffer, //data
2, //wLength
0 //timeout
);
if (response < 0) {
error(response);
return-1;
}
devVersion = ioBuffer[1] << 8 | ioBuffer[0];
fprintf(stdout,"Verion Code Device: %d \n", devVersion);
usleep(1000);//sometimes hangs on the next transfer :(
if (usbSendCtrl(gadgetAccessory.manufacturer, 52, 0) < 0) {
return -1;
}
if (usbSendCtrl(gadgetAccessory.modelName, 52, 1) < 0) {
return -1;
}
if (usbSendCtrl(gadgetAccessory.description, 52, 2) < 0) {
return -1;
}
if (usbSendCtrl(gadgetAccessory.version, 52, 3) < 0) {
return -1;
}
if (usbSendCtrl(gadgetAccessory.uri, 52, 4) < 0) {
return -1;
}
if (usbSendCtrl(gadgetAccessory.serialNumber, 52, 5) < 0) {
return -1;
}
fprintf(stdout,"Accessory Identification sent\n");
if (usbSendCtrl(NULL, 53, 0) < 0) {
return -1;
}
fprintf(stdout,"Attempted to put device into accessory mode\n");
if (handle != NULL) {
libusb_release_interface (handle, 0);
}
for (;;) {
tries--;
if ((handle = libusb_open_device_with_vid_pid(NULL,
GOOGLE_VID, ACCESSORY_PID)) == NULL) {
if (tries < 0) {
return -1;
}
} else {
break;
}
sleep(1);
}
libusb_claim_interface(handle, 0);
fprintf(stdout, "Interface claimed, ready to transfer data\n");
return 0;
}
void error(int code)
{
fprintf(stdout,"\n");
switch (code) {
case LIBUSB_ERROR_IO:
fprintf(stdout,
"Error: LIBUSB_ERROR_IO\n"
"Input/output error.\n");
break;
case LIBUSB_ERROR_INVALID_PARAM:
fprintf(stdout,
"Error: LIBUSB_ERROR_INVALID_PARAM\n"
"Invalid parameter.\n");
break;
case LIBUSB_ERROR_ACCESS:
fprintf(stdout,
"Error: LIBUSB_ERROR_ACCESS\n"
"Access denied (insufficient permissions).\n");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stdout,
"Error: LIBUSB_ERROR_NO_DEVICE\n"
"No such device (it may have been disconnected).\n");
break;
case LIBUSB_ERROR_NOT_FOUND:
fprintf(stdout,
"Error: LIBUSB_ERROR_NOT_FOUND\n"
"Entity not found.\n");
break;
case LIBUSB_ERROR_BUSY:
fprintf(stdout,
"Error: LIBUSB_ERROR_BUSY\n"
"Resource busy.\n");
break;
case LIBUSB_ERROR_TIMEOUT:
fprintf(stdout,
"Error: LIBUSB_ERROR_TIMEOUT\n"
"Operation timed out.\n");
break;
case LIBUSB_ERROR_OVERFLOW:
fprintf(stdout,
"Error: LIBUSB_ERROR_OVERFLOW\n"
"Overflow.\n");
break;
case LIBUSB_ERROR_PIPE:
fprintf(stdout,
"Error: LIBUSB_ERROR_PIPE\n"
"Pipe error.\n");
break;
case LIBUSB_ERROR_INTERRUPTED:
fprintf(stdout,
"Error:LIBUSB_ERROR_INTERRUPTED\n"
"System call interrupted (perhaps due to signal).\n");
break;
case LIBUSB_ERROR_NO_MEM:
fprintf(stdout,
"Error: LIBUSB_ERROR_NO_MEM\n"
"Insufficient memory.\n");
break;
case LIBUSB_ERROR_NOT_SUPPORTED:
fprintf(stdout,
"Error: LIBUSB_ERROR_NOT_SUPPORTED\n"
"Operation not supported or unimplemented on this platform.\n");
break;
case LIBUSB_ERROR_OTHER:
fprintf(stdout,
"Error: LIBUSB_ERROR_OTHER\n"
"Other error.\n");
break;
default:
fprintf(stdout,
"Error: unkown error\n");
}
}
void status(int code)
{
fprintf(stdout,"\n");
switch (code) {
case LIBUSB_TRANSFER_COMPLETED:
fprintf(stdout,
"Success: LIBUSB_TRANSFER_COMPLETED\n"
"Transfer completed.\n");
break;
case LIBUSB_TRANSFER_ERROR:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_ERROR\n"
"Transfer failed.\n");
break;
case LIBUSB_TRANSFER_TIMED_OUT:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_TIMED_OUT\n"
"Transfer timed out.\n");
break;
case LIBUSB_TRANSFER_CANCELLED:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_CANCELLED\n"
"Transfer was cancelled.\n");
break;
case LIBUSB_TRANSFER_STALL:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_STALL\n"
"For bulk/interrupt endpoints: halt condition detected.\n"
"For control endpoints: control request not supported.\n");
break;
case LIBUSB_TRANSFER_NO_DEVICE:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_NO_DEVICE\n"
"Device was disconnected.\n");
break;
case LIBUSB_TRANSFER_OVERFLOW:
fprintf(stdout,
"Error: LIBUSB_TRANSFER_OVERFLOW\n"
"Device sent more data than requested.\n");
break;
default:
fprintf(stdout,
"Error: unknown error\nTry again(?)\n");
break;
}
}