-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISP808.c
269 lines (212 loc) · 6.15 KB
/
ISP808.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
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (either version 2 or
* version 3) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/
// Use at your own risk.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libusb.h>
#define VID 0x04fc
#define PID 0x1528
#define TIMEOUT 1000
libusb_device_handle *device_handle;
void control_out(int request, int value, int index, unsigned char* buf,
int length) {
int n = libusb_control_transfer(device_handle, LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE, request,
value, index, buf, length, TIMEOUT);
if (n < 0) {
fprintf(stderr, "Control transfer failed: ");
switch (n) {
case LIBUSB_ERROR_TIMEOUT:
fprintf(stderr, "transfer timed out");
break;
case LIBUSB_ERROR_PIPE:
fprintf(stderr, "control request was not supported "
"by the device");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stderr, "device has been disconnected");
break;
default:
fprintf(stderr, "error %d", n);
break;
}
fprintf(stderr, "\n");
} else if (n > 0) {
fprintf(stderr, "Write %d bytes:", n);
int i;
for (i = 0; i < n; i++)
fprintf(stderr, " %02x", buf[i]);
fprintf(stderr, "\n");
}
}
void control_in_vendor_device(int request, int value, int index, int length) {
unsigned char *buf = calloc(length, sizeof(unsigned char));
int n = libusb_control_transfer(device_handle, LIBUSB_ENDPOINT_IN
| LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE, request,
value, index, buf, length, TIMEOUT);
if (n < 0) {
fprintf(stderr, "Control transfer failed: ");
switch (n) {
case LIBUSB_ERROR_TIMEOUT:
fprintf(stderr, "transfer timed out");
break;
case LIBUSB_ERROR_PIPE:
fprintf(stderr, "control request was not supported "
"by the device");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stderr, "device has been disconnected");
break;
default:
fprintf(stderr, "error %d", n);
break;
}
fprintf(stderr, "\n");
} else if (n > 0) {
fprintf(stderr, "Received %d bytes:", n);
int i;
for (i = 0; i < n; i++)
fprintf(stderr, " %02x", buf[i]);
fprintf(stderr, "\n");
}
free(buf);
}
void GetVersion() {
control_in_vendor_device(0x20, 0, 0, 8);
}
void GetVersion1() {
control_in_vendor_device(0x20, 0, 1, 8);
}
void GetVersion2() {
control_in_vendor_device(0x20, 0, 2, 8);
}
void bulk_transfer_error(int code) {
fprintf(stderr, "Bulk transfer failed: ");
switch (code) {
case LIBUSB_ERROR_TIMEOUT:
fprintf(stderr, "transfer timed out");
break;
case LIBUSB_ERROR_PIPE:
fprintf(stderr, "endpoint halted");
break;
case LIBUSB_ERROR_OVERFLOW:
fprintf(stderr, "device sent too much data");
break;
case LIBUSB_ERROR_NO_DEVICE:
fprintf(stderr, "device has been disconnected");
break;
default:
fprintf(stderr, "error %d", code);
break;
}
fprintf(stderr, "\n");
}
int bulk_read(int endpoint, unsigned char *buf, int length) {
int ret, n;
if ((ret = libusb_bulk_transfer(device_handle, LIBUSB_ENDPOINT_IN
| endpoint, buf, length, &n, TIMEOUT)) < 0)
bulk_transfer_error(ret);
return n;
}
int bulk_write(int endpoint, unsigned char *buf, int length) {
int ret, n;
if ((ret = libusb_bulk_transfer(device_handle, LIBUSB_ENDPOINT_OUT
| endpoint, buf, length, &n, TIMEOUT)) < 0)
bulk_transfer_error(ret);
if (n)
fprintf(stderr, "Sent %d bytes\n", n);
return n;
}
int main(int argc, char **argv) {
FILE *pFile = NULL;
int size;
long lSize;
unsigned char *buffer;
unsigned char *buf2 = calloc(300, sizeof(unsigned char));
if (argc < 2) {
fprintf(stderr, "Missing upload filename \n\n");
fprintf(stderr, "Usage: \n");
fprintf(stderr, "ISP808 <uploadfile> \n");
exit(1);;
}
pFile = fopen(argv[1], "rb");
if (pFile == NULL) {
fprintf(stderr, "ERROR: Unable to open file %s \n", argv[1]);
exit(1);
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = calloc(lSize, sizeof(unsigned char));
if (buffer == NULL) {
fputs("Memory error", stderr);
exit(2);
}
size = fread(buffer, 1, lSize, pFile);
if (size != lSize) {
fputs("Reading error", stderr);
exit(3);
}
libusb_context *context;
libusb_init(&context);
libusb_set_debug(context, 3);
device_handle = libusb_open_device_with_vid_pid(context, VID, PID);
if (!device_handle) {
fprintf(stderr, "ERROR: Unable to find device "
"(Vendor ID = %04x, Product ID = %04x)\n", VID, PID);
fprintf(stderr, "Press the Mode Button and connect the cam to USB \n");
return 1;
}
libusb_claim_interface(device_handle, 2);
libusb_set_interface_alt_setting(device_handle, 0, 0);
usleep(100);
// Get the installed version
fprintf(stdout, "\nRead 808 Version \n");
GetVersion();
GetVersion1();
GetVersion2();
fprintf(stdout, "\nStart Uploading the file: %s \n",argv[1]);
char defout[] = { 0x00, 0x80, 0x04, 0x00, 0x2d, 0x10, 0x00, 0x00 };
control_in_vendor_device(0x70, 0, 0, 5);
usleep(100);
control_out(0xfd, 0, 0x4f3, defout, 8);
usleep(200); // Fix me
// Download the code
fprintf(stdout, "\nDownload Code.... \n");
bulk_write(3, buffer, lSize);
usleep(200);
char defout2[] = { 0x00, 0x80, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
control_out(0xfd, 0, 0x4f1, defout2, 0x10);
usleep(100);
// Dont know why
bulk_write(3, buf2, 256);
fprintf(stdout, "\nDownload Ready.... \n");
usleep(100);
if (pFile)
fclose(pFile);
free(buffer);
libusb_release_interface(device_handle, 0);
libusb_close(device_handle);
libusb_exit(context);
return 0;
}