bbmux.c
1.7 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#define BBPLAYER
#include <errno.h>
#include <fcntl.h>
#include "bbrdb.h"
#include "bbmux.h"
#include "rdb.h"
#define MAX_PIPE_DATA 0x40
HANDLE hUsbThread;
HANDLE hUsbReadEvent;
extern HANDLE hUsbRead;
extern int usb_verbose;
int recv_next(unsigned int data);
DWORD WINAPI do_usb_read(LPVOID lpParameter)
{
unsigned long readLen , i;
unsigned int *datap;
unsigned char recv_data[MAX_PIPE_DATA];
/* Because RDB protocal,
we can only read small block */
while (1) {
if (hUsbRead == NULL) /* Usb pipe cannot be opened */
return -1;
if (ReadFile(hUsbRead, recv_data, MAX_PIPE_DATA, &readLen, NULL)) {
/* Deal with Recv data here */
if (readLen & 0x3)
printf("Warning: Wrong RDB length %d %x \n", readLen, readLen);
for (i=0; i<readLen; i+=4) {
datap = (unsigned int *) (recv_data+i);
recv_next(*datap);
if (usb_verbose)
fprintf(stderr, "Got %d bytes from USB: %02x %02x %02x %02x\n",
readLen, recv_data[i], recv_data[i+1],
recv_data[i+2], recv_data[i+3]);
}
} else {
/* XXX whs should handle Read failure case */
hUsbRead = NULL;
return -1;
}
}
}
int bb_usb_read_end()
{
if (hUsbThread) {
TerminateThread(hUsbThread, -1);
CloseHandle(hUsbThread);
hUsbThread = NULL;
}
return 0;
}
int bb_usb_read_start()
{
unsigned int dwUsbId;
hUsbThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) do_usb_read,
NULL, 0, &dwUsbId);
hUsbReadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hUsbThread)
return -1;
return 0;
}