⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hbcp.c

📁 在VC6.0上编译通过的蓝牙通信演示程序, COM1口控制.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
Copyright (C) Excelpoint System (Pte) Ltd. 2006

FILE: HBCP.c

DESCRIPTION: APIs for each HBCP command. Event data decoding and reassembling.
*******************************************************************************/

#include "HBCP.h"

#ifdef	WIN32
#include "SerialCom.h"
#pragma pack (1)
#endif

#define SYNC_ENABLE

#define CHECK_SUM
#ifdef CHECK_SUM 
unsigned short gRx_chk_sum =0;		/* sum of all rx data */	
unsigned short gRx_data_sum =0;   /* the sum data sent from BlueCore */
#endif
/*****************************************************************
	External Function & Variable
******************************************************************/
void HardUart_send(unsigned char a)
{
	UInt32	tx_num=0;
	#ifdef	WIN32
	UartDrv_Tx(&a,1, &tx_num);
    #endif
}

/*****************************************************************
	Internal Function & Variable
******************************************************************/
typedef __packed struct _THBCP_RX_INFO
{
	unsigned int	bSync:1;	// Syncronized flag
	unsigned int	bEsc:1;		// Escaped flag
	unsigned char	ucSeqNum;	// current received sequence number
	unsigned char	nLen;		// package length
}HBCP_RX_INFO;

HBCP_RX_INFO	gHBCP_Rx;
unsigned char	gTxSeq=0;	// tx sequencial number
unsigned char	gRxSeq=0;	// rx sequencial number

//Only increase to certain level
#define TXSEQnum	((++gTxSeq)&0x3f)

unsigned char	gHBCP_Rx_Buf[HBCP_RX_BUF_SIZE];

TX_BUF tx_buf;
unsigned char sync_send, sync_send_num;


void FATAL_ERROR(void);
void HBCP_RcvPacket(void);
VOID CALLBACK SyncThread(DWORD dwPrameter);
void HBCP_Sync_Ack(void);

/*****************************************************************
	Common Function Definition
******************************************************************/

/* 	Function: 	HBCP_Physical_Send
	Params:		pStr	The pionter of content to be sent
				ucLen	The data length to be escaped and sent out
	return:		none
*/
#define FLAG_START	0x01	/* first bit */
#define FLAG_END	0x02	/* second bit*/
#define FLAG_FULL	0x03
#define FLAG_MID	0x00

#define HBCP_Physical_Send(str,len)			HBCP_Physical_Send_by_flag(str,len,FLAG_FULL);
#define HBCP_Physical_Send_Start(str,len)	HBCP_Physical_Send_by_flag(str,len,FLAG_START);
#define HBCP_Physical_Send_End(str,len)		HBCP_Physical_Send_by_flag(str,len,FLAG_END);
#define HBCP_Physical_Send_Content(str,len)	HBCP_Physical_Send_by_flag(str,len,FLAG_MID);

void HBCP_Physical_Send_Txbuffer(unsigned char *pStr, unsigned char ucLen);
void HBCP_Send_TxBuffer(unsigned char *pStr, unsigned char ucLen);
									
void HBCP_Physical_Send_by_flag(__packed unsigned char * pStr, unsigned char	ucLen, unsigned char flag)
{
	unsigned char	ch;
#ifdef CHECK_SUM
	unsigned short sum=0;
	unsigned char sum_higher_byte=0;
	unsigned char sum_lower_byte=0;
#endif
	if (flag & FLAG_START)
		HardUart_send(HBCP_FLAG);
	while (ucLen--)
	{
		ch = *(pStr++);
		if ( (ch == HBCP_ESCAPE) || (ch == HBCP_FLAG) )
		{
			HardUart_send(HBCP_ESCAPE);
			ch ^= 0x20;
		}
#ifdef CHECK_SUM
		sum+=ch;
#endif
		HardUart_send(ch);		
	}
	
#ifdef CHECK_SUM
	sum = ~sum + 1;/*checksum = ~(Sum of data bytes in data package) + 1*/
	/*
	HardUart_send((unsigned char)((sum&0xff00)>>8));		
	HardUart_send((unsigned char)(sum&0x00ff)); 
	*/
	sum_higher_byte = (unsigned char)((sum&0xff00)>>8);
	sum_lower_byte = (unsigned char)(sum&0x00ff);	
	
	if((sum_higher_byte == HBCP_ESCAPE)||(sum_higher_byte == HBCP_FLAG))
	{
		HardUart_send(HBCP_ESCAPE);
		sum_higher_byte ^= 0x20;
		HardUart_send(sum_higher_byte);	
	}
	else
	{
		HardUart_send(sum_higher_byte);	
	}
	
	if((sum_lower_byte == HBCP_ESCAPE)||(sum_lower_byte == HBCP_FLAG))
	{
		HardUart_send(HBCP_ESCAPE);
		sum_lower_byte ^= 0x20;
		HardUart_send(sum_lower_byte);	
	}
	else
	{
		HardUart_send(sum_lower_byte);	
	}
	
#endif

	if (flag & FLAG_END)
		HardUart_send(HBCP_FLAG);
}

/*****************************************************************
	Host Function Definition
******************************************************************/

/* 	Function: 	HBCP_Send_Command
	Params:		ucCmd	command code with parameters
	return:		none
*/
void HBCP_Send_Command(unsigned char ucCmd, int ucLen, unsigned char *parameter)
{
	__packed struct {
		HBCP_HEAD	head;
		char			pbuf[MAX_TX_BUF_LENGTH];
	}data;

	unsigned char		i;

	if(ucLen > MAX_TX_BUF_LENGTH)  ucLen = MAX_TX_BUF_LENGTH;
	
	data.head.ucType	= ucCmd;
	data.head.ucSeqnum	= TXSEQnum;
	data.head.ucLen		= ucLen;

	for (i=0; i<ucLen; i++)
		data.pbuf[i]	= *(parameter++);
	
	// Wake-up: HBCP_Physical_Send((unsigned char *)&data, HBCP_HEAD_SIZE+ucLen);
	HBCP_Send_TxBuffer((unsigned char *)&data, (unsigned char)(HBCP_HEAD_SIZE + ucLen));
}

/* Wake-up */
/* 	Function: 	HBCP_Send_TxBuffer
	Params:		
	return:		none
    Description: Sends command to tx buffer first
*/

void HBCP_Send_TxBuffer(unsigned char *pStr, unsigned char ucLen)
{
	unsigned char	ch;
	unsigned int	i, cnt=0;
#ifdef CHECK_SUM 
	unsigned short sum=0;
	unsigned char sum_higher_byte=0;
	unsigned char sum_lower_byte=0;
#endif
#ifdef SYNC_ENABLE

	DWORD			dwID;
	HANDLE			thread_handle;
	
    /*Fixed problem:Press cmd key more than 30times pc tool no response;Ver 0.82*/
    if (sync_send == 0)
	{
		thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SyncThread, 0, 0, &dwID);
		if (thread_handle == INVALID_HANDLE_VALUE)
		{
		  printf("\n\nCreateThread failed.\n\n");
		  return;
		}
		else
		{
			sync_send = 1;
			CloseHandle(thread_handle);
		}
	}

#endif

    for (i=0;i<cnt;i++)
    {
    	if ((tx_buf.data[i]==HBCP_ESCAPE) || (tx_buf.data[i]==HBCP_FLAG))
			cnt ++;	// counting characters needs to be escaped
    }
	
#ifdef CHECK_SUM 
	cnt+=2; /* add 2 byte length for chksum*/
#endif

    if ((tx_buf.length + ucLen + cnt + 2)> MAX_TX_BUF_LENGTH)
    {   
        return;
    }
    
    tx_buf.data[tx_buf.length++]=HBCP_FLAG;

	while (ucLen--)
	{
		ch = *(pStr++);
		if ( (ch == HBCP_ESCAPE) || (ch == HBCP_FLAG) )
		{
			tx_buf.data[tx_buf.length++]=HBCP_ESCAPE;
			ch ^= 0x20;
		}
		tx_buf.data[tx_buf.length++] = ch;	
#ifdef CHECK_SUM 
		sum+=ch; /* data sum */
#endif
	}   
	
#ifdef CHECK_SUM 
	sum = ~sum + 1;/*checksum = ~(Sum of data bytes in data package) + 1*/

	//tx_buf.data[tx_buf.length++]=(unsigned char)((sum & 0xff00)>>8);/* send the higher 8bits firstly*/
	//tx_buf.data[tx_buf.length++]=(unsigned char)(sum & 0x00ff);/* send the lower 8bits */
	
	sum_higher_byte = (unsigned char)((sum&0xff00)>>8);
	sum_lower_byte = (unsigned char)(sum&0x00ff);	
	
	if((sum_higher_byte == HBCP_ESCAPE)||(sum_higher_byte == HBCP_FLAG))
	{
		tx_buf.data[tx_buf.length++]=HBCP_ESCAPE;
		sum_higher_byte ^= 0x20;
		tx_buf.data[tx_buf.length++] = sum_higher_byte;	
	}
	else
	{
		tx_buf.data[tx_buf.length++] = sum_higher_byte;	
	}
	
	if((sum_lower_byte == HBCP_ESCAPE)||(sum_lower_byte == HBCP_FLAG))
	{
		tx_buf.data[tx_buf.length++]=HBCP_ESCAPE;
		sum_lower_byte ^= 0x20;
		tx_buf.data[tx_buf.length++] = sum_lower_byte;	
	}	
	else
	{
		tx_buf.data[tx_buf.length++] = sum_lower_byte;	
	}
	
#endif
    tx_buf.data[tx_buf.length++]=HBCP_FLAG;	

#ifndef SYNC_ENABLE
	HBCP_Sync_Ack();
#endif
}

void HBCP_Physical_Send_Txbuffer(unsigned char *pStr, unsigned char ucLen)
{
printf("SEND:\n");
    while (ucLen --)
    {
printf("%0.2\n",*pStr);
    	HardUart_send(*(pStr++));
    }
}

void HBCP_Send_Sync(void)
{
	HBCP_HEAD		head;
	
	head.ucType		= HBCP_CMD_SYNC;
	head.ucSeqnum	= TXSEQnum;
	head.ucLen		= 0;
	
	HBCP_Physical_Send((unsigned char *)&head, HBCP_HEAD_SIZE);	
}


/*******************************************************************
	HBCP RX Handler
*******************************************************************/
// BUGHBCP HBCP_EXCEPTION
void InitHBCPState(void)
{
	// Initial HBCP
	gHBCP_Rx.bSync		= 0;	// loss of syn
	gHBCP_Rx.bEsc		= 0;	// not escaped
	gHBCP_Rx.ucSeqNum	= 0xff; // unknow
	gHBCP_Rx.nLen		= 0;
#ifdef CHECK_SUM
	gRx_chk_sum = 0;
	gRx_data_sum = 0;
#endif		
}

void Init_HBCP(void)
{
	// BUGHBCP HBCP_EXCEPTION
	InitHBCPState();

    //initialise transmit buffer
    tx_buf.length = 0;
    memset(tx_buf.data, 0, MAX_TX_BUF_LENGTH);//clear tx buffer to 0
    
    sync_send = 0;
	sync_send_num = 0;
    
}

void Close_Sync_Thread(void)
{
	sync_send = 0;
	sync_send_num = 0;
}


void HBCP_Receive(int ucLen, unsigned char *pStr)
{
	unsigned char	ch;
	
	while (ucLen--)
	{
		ch = *(pStr++);

		if (!gHBCP_Rx.bSync)
		{
			// No Sync
			if ( ch == HBCP_FLAG)
			{
				// Reach Sync
				gHBCP_Rx.bSync	= 1;
				// Initial context
				gHBCP_Rx.nLen	= 0;
#ifdef CHECK_SUM 
				gRx_chk_sum = 0;
				gRx_data_sum = 0;
#endif
				
			}else
			{
				// Discard any data if not in Sync
			}
		}else
		{
			// Sync
			if (!gHBCP_Rx.bEsc)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -