微信蓝牙协议栈移植

微信蓝牙一般是基于串口的,所以只跟串口底层相关,也就是只需要将串口发送和接收两个函数填充到协议栈对外接口函数就行了。

先把微信SDK包加载在我们自己的项目中,整个移植过程我们只需要改动proto.c文件里边5个接口函数。

微信蓝牙协议栈移植

1、先把串口准备好

#include "uart.h"
#include "uart_config.h"

enum BLE_TRAN_FSM
{
    bMagicNumber = 0,
    bVer,
    nLenghth_hi,
    nLenghth_low,
    bdata,
};  


unsigned char recv_from_ble_buf[60]= {0};
unsigned char m_frame_buffer[256]={0};    //定义BLE帧结构,长度20个字节

unsigned char rxbuf[256]={0};
unsigned char rx_idx=0;
unsigned short uart0_rx_len;
unsigned short uart0_rx_cnt=0;
unsigned short uart0_buf_num=0;
static unsigned char uart0_tran_state = 0;
unsigned char uart0_tran_complt = 0;

void UART0_Init(void)
{   
}

void uart0_putchar(unsigned char dat)
{
}

unsigned char UART0_Put_N_Byte (unsigned char* data_buf,unsigned char data_len)
{
    unsigned char i;     
    //BT_EN_OFF;    
    for (i = 0; i < data_len; i++)
    {
        uart0_putchar(data_buf[i]);
    }    
    //BT_EN_ON;
    return data_len;
}

//拷贝串口缓冲区数据
unsigned char  UART0_Get_Buf(unsigned char* data_buf)
{
    unsigned char i;
    if(uart0_tran_complt)       
    {        
        uart0_buf_num = uart0_rx_len;
        for(i=0;i<uart0_rx_len;i++)
        {
            *data_buf ++ = rxbuf[i];
        }
        uart0_tran_complt = 0; 
        uart0_rx_len = 0;               
    }
    else 
    { 
        uart0_buf_num = 0;
    }
    return uart0_buf_num;
} 

void UART0_ISR(void)
{        
    rxbuf[rx_idx]=UART0_D;
    rx_idx+=1;   
    switch(uart0_tran_state) 
    {
        case bMagicNumber:
            if(MAGIC_NUMBER_ID == rxbuf[0]) 
            {             
                uart0_tran_state = bVer; 
            }
            else
            {
                uart0_tran_state = bMagicNumber; rx_idx=0;
            }
            break;
        case bVer:
            if(DEVEICE_VERSION_ID == rxbuf[1])
            {
                uart0_tran_state = nLenghth_hi;
                //uart0_putstring("uart0_tran_state : bVer \r\n");
                //预留用一个定时器来做超时检测
                //uart0_timeout = 20;                      
            }
            else
            {
                uart0_tran_state = bMagicNumber; rx_idx=0;
            }
            break;
        
        case nLenghth_hi:                   
            uart0_tran_state = nLenghth_low;
        
        break;  
        
        case nLenghth_low:       
            uart0_rx_len = (unsigned short) ((rxbuf[2]<<8)|rxbuf[3]); 
            uart0_tran_state = bdata;
        break; 
        
        case bdata:      
            if(rx_idx == uart0_rx_len) 
            {              
                uart0_tran_complt = 1;
                uart0_tran_state = bMagicNumber;
                rx_idx=0;
           
            }
            
            //串口超时之后再添加  
            //if(uart0_timeout) 
            //{
            //    for(iRev=0;iRev<20;iRev++) {rxbuf[]=0;rx_idx=0;}          
            //}
        
        
        break;
        
        default:
            uart0_tran_state = bMagicNumber; rx_idx=0;
        
        break;
    }           
}

  


2、将proto.c文件里边

    void Auth(unsigned char *pBuf , unsigned short  int Length);

        void Unpack_Auth_Response(unsigned char *buf , unsigned short  int Length );

        void Unpack_Init_Response(unsigned char *buf , unsigned short  int Length );

        void SendDataToManuf(unsigned char *buf , unsigned short  int Length);

        void Unpack_SendDataToManufacturerSvrResponse(unsigned char *buf , unsigned short  int Length );

5个函数中的串口发送函数换成我们自己的。

微信蓝牙协议栈移植

3、我们接收厂商服务器的数据都在

void Unpack_ManufacturerSvrSendDataPush(unsigned char *buf , unsigned short  int Length )

函数你解析.

微信蓝牙协议栈移植

 

到此结束,就这么简单。

 

微信蓝牙协议栈移植

上一篇:【转】微信红包随机算法初探


下一篇:linux下curl编程