bbmux.c 1.7 KB
#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;
}