-
Notifications
You must be signed in to change notification settings - Fork 4
/
pc-usb.c
269 lines (241 loc) · 7.62 KB
/
pc-usb.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
#include <stdio.h>
#include <usb.h>
#include <libusb-1.0/libusb.h>
#include <string.h>
#include <unistd.h>
#define IN 0x83
#define OUT 0x03
#define VID 0x18D1
#define PID 0x4E12
#define ACCESSORY_PID 0x2D01
#define ACCESSORY_PID_ALT 0x2D00
#define BUFFER 1024
static int mainPhase();
static int isUsbAccessory (void);
static int init(void);
static int deInit(void);
static void error(int code);
static int setupAccessory(
const char* manufacturer,
const char* modelName,
const char* description,
const char* version,
const char* uri,
const char* serialNumber);
//static
static struct libusb_device_handle* handle;
static char stop;
static char success = 0;
int main (int argc, char *argv[]){
if (isUsbAccessory() < 0) {
if(init() < 0)
return;
if(setupAccessory(
"Poly's Factory",
"Android Oepn Accessory Demo",
"Android Oepn Accessory Demo",
"1.0",
"http://d.hatena.ne.jp/thorikawa/",
"0000000012345678") < 0) {
fprintf(stdout, "Error setting up accessory\n");
deInit();
return -1;
}
}
if(mainPhase() < 0){
fprintf(stdout, "Error during main phase\n");
deInit();
return -1;
}
deInit();
fprintf(stdout, "Done, no errors\n");
return 0;
}
static int mainPhase(){
unsigned char buffer[BUFFER];
int response = 0;
static int transferred;
int i;
int index=1;
fprintf(stdout, "start main Phase\n");
while (libusb_bulk_transfer(handle, IN, buffer, BUFFER, &transferred, 0) == 0) {
int offset;
// 8文字単位で読み込む
for(offset = 0; offset < transferred; offset += 8){
// 最初の4文字(端末の傾き)を読み込む
char tmp = buffer[offset+4];
buffer[offset+4] = '\0';
FILE *fp;
fp = fopen("/workspace/box2d-with-usb/angle.txt", "w");
if (fp != NULL) {
buffer[transferred] = '\0';
fprintf(stdout, "%s\n", buffer+offset);
fprintf(fp, "%s", buffer+offset);
fclose(fp);
} else {
fprintf(stderr, "file error1\n");
}
buffer[4] = tmp;
// 続く4文字(ボールが追加されたかどうか)を読み込む
tmp = buffer[offset+8];
buffer[offset+8] = '\0';
char flag[4];
int addBall = atoi(buffer+offset+4);
if(addBall){
FILE *fp2;
fp2 = fopen("/workspace/box2d-with-usb/ball.txt", "w");
if (fp2 != NULL) {
fprintf(stdout, "add ball\n");
fprintf(fp2, "%d", index);
fclose(fp2);
} else {
fprintf(stderr, "file error2\n");
}
index++;
}
}
}
error(response);
return -1;
}
static int isUsbAccessory () {
// 端末がすでにUSB Accessory Modeかどうかを判定する
int res;
libusb_init(NULL);
if((handle = libusb_open_device_with_vid_pid(NULL, VID, ACCESSORY_PID)) == NULL) {
fprintf(stdout, "Device is not USB Accessory Mode\n");
res = -1;
} else {
// already usb accessory mode
fprintf(stdout, "Device is already USB Accessory Mode\n");
libusb_claim_interface(handle, 0);
res = 0;
}
return res;
}
static int init(){
if((handle = libusb_open_device_with_vid_pid(NULL, VID, PID)) == NULL){
fprintf(stdout, "Problem acquireing handle\n");
return -1;
}
libusb_claim_interface(handle, 0);
return 0;
}
static int deInit(){
if(handle != NULL)
libusb_release_interface (handle, 0);
libusb_exit(NULL);
return 0;
}
static int setupAccessory(
const char* manufacturer,
const char* modelName,
const char* description,
const char* version,
const char* uri,
const char* serialNumber){
unsigned char ioBuffer[2];
int devVersion;
int response;
int tries = 5;
// DeviceがAndroid accessory protocolをサポートしているか判定する
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 :(
// Accessory Identificationを送信する
response = libusb_control_transfer(handle,0x40,52,0,0,(char*)manufacturer,strlen(manufacturer),0);
if(response < 0){error(response);return -1;}
response = libusb_control_transfer(handle,0x40,52,0,1,(char*)modelName,strlen(modelName)+1,0);
if(response < 0){error(response);return -1;}
response = libusb_control_transfer(handle,0x40,52,0,2,(char*)description,strlen(description)+1,0);
if(response < 0){error(response);return -1;}
response = libusb_control_transfer(handle,0x40,52,0,3,(char*)version,strlen(version)+1,0);
if(response < 0){error(response);return -1;}
response = libusb_control_transfer(handle,0x40,52,0,4,(char*)uri,strlen(uri)+1,0);
if(response < 0){error(response);return -1;}
response = libusb_control_transfer(handle,0x40,52,0,5,(char*)serialNumber,strlen(serialNumber)+1,0);
if(response < 0){error(response);return -1;}
fprintf(stdout,"Accessory Identification sent\n", devVersion);
// DeviceをAccessory modeにする
response = libusb_control_transfer(handle,0x40,53,0,0,NULL,0,0);
if(response < 0){error(response);return -1;}
fprintf(stdout,"Attempted to put device into accessory mode\n", devVersion);
if(handle != NULL){
libusb_close(handle);
}
fprintf(stdout, "connect to new PID...\n");
for(;;){ //attempt to connect to new PID, if that doesn't work try ACCESSORY_PID_ALT
tries--;
if((handle = libusb_open_device_with_vid_pid(NULL, VID, ACCESSORY_PID)) == NULL){
if(tries < 0){
return -1;
}
}else{
break;
}
sleep(1);
}
// Interface #0をhandleに紐づける
fprintf(stdout, "claim usb accessory I/O interface\n");
response = libusb_claim_interface(handle, 0);
if(response < 0){error(response);return -1;}
fprintf(stdout, "Interface claimed, ready to transfer data\n");
return 0;
}
static void error(int code){
fprintf(stdout,"\n");
switch(code){
case LIBUSB_ERROR_IO:
fprintf(stdout,"Error: LIBUSB_ERROR_IO\nInput/output error.\n");
break;
case LIBUSB_ERROR_INVALID_PARAM:
fprintf(stdout,"Error: LIBUSB_ERROR_INVALID_PARAM\nInvalid parameter.\n");
break;
case LIBUSB_ERROR_ACCESS:
fprintf(stdout,"Error: LIBUSB_ERROR_ACCESS\nAccess denied (insufficient permissions).\n");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stdout,"Error: LIBUSB_ERROR_NO_DEVICE\nNo such device (it may have been disconnected).\n");
break;
case LIBUSB_ERROR_NOT_FOUND:
fprintf(stdout,"Error: LIBUSB_ERROR_NOT_FOUND\nEntity not found.\n");
break;
case LIBUSB_ERROR_BUSY:
fprintf(stdout,"Error: LIBUSB_ERROR_BUSY\nResource busy.\n");
break;
case LIBUSB_ERROR_TIMEOUT:
fprintf(stdout,"Error: LIBUSB_ERROR_TIMEOUT\nOperation timed out.\n");
break;
case LIBUSB_ERROR_OVERFLOW:
fprintf(stdout,"Error: LIBUSB_ERROR_OVERFLOW\nOverflow.\n");
break;
case LIBUSB_ERROR_PIPE:
fprintf(stdout,"Error: LIBUSB_ERROR_PIPE\nPipe error.\n");
break;
case LIBUSB_ERROR_INTERRUPTED:
fprintf(stdout,"Error:LIBUSB_ERROR_INTERRUPTED\nSystem call interrupted (perhaps due to signal).\n");
break;
case LIBUSB_ERROR_NO_MEM:
fprintf(stdout,"Error: LIBUSB_ERROR_NO_MEM\nInsufficient memory.\n");
break;
case LIBUSB_ERROR_NOT_SUPPORTED:
fprintf(stdout,"Error: LIBUSB_ERROR_NOT_SUPPORTED\nOperation not supported or unimplemented on this platform.\n");
break;
case LIBUSB_ERROR_OTHER:
fprintf(stdout,"Error: LIBUSB_ERROR_OTHER\nOther error.\n");
break;
default:
fprintf(stdout, "Error: unkown error\n");
}
}