-
Notifications
You must be signed in to change notification settings - Fork 21
/
usb_read.c
61 lines (45 loc) · 1.19 KB
/
usb_read.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
#include "updater.h"
static int read_block_start(int length)
{
unsigned char transfer[6];
printf(">>> USB: read_block_start (length=%d)\n", length);
transfer[0] = 0x05;//report id
transfer[1] = 0x52;
transfer[2] = 0x00;
transfer[3] = 0x00;
transfer[4] = length & 0xFF;
transfer[5] = (length >> 8) & 0xFF;
return libusb_control_transfer(devh, 0x21, 0x09, 0x0305, 0,
transfer, sizeof(transfer), 100);
}
static int read_block(unsigned char *data, int offset, int length)
{
unsigned char transfer[length + 2];
printf(">>> USB: read_block (offset=%d, length=%d)\n", offset, length);
transfer[0] = 0x06;//report id
transfer[1] = 0x72;
int rc = libusb_control_transfer(devh, 0xa1, 0x01, 0x0306, 0,
transfer, sizeof(transfer), 2000);
if (rc < 0) {
return rc;
}
memcpy(&data[offset], &transfer[2], length);
return 0;
}
int read_bulk(unsigned char *data, int length)
{
int block_size = 2048;
int rc;
rc = read_block_start(length);
if (rc < 0) {
return rc;
}
for (int offset = 0; offset < length; offset += block_size) {
rc = read_block(data, offset, block_size);
if (rc < 0) {
return rc;
}
usleep(10000);
}
return 0;
}