/******************/
/**产测协议相关功能解析**/
/******************/
#include "mfg_factory.h"
#include "display_common.h"
#include "BL55072A.h"

static RECIVE_USART_DATA_HANDLE 	 static_transparent_msg_info = {0};                               //串口1接受的数据
static RECIVE_USART_DATA_BUFF    	 static_transprent_buff = {0};                                       //透传数据接收的缓存
extern u8 boad_test_falg[6];

u16 g_u16 = 0;

#if 1 // add by dingmz_frc.20201215
extern u8 g_u8Temp_Buffer[];
extern u8 g_u8Temp_Length;
#endif

void uart0_data_deal_init(void)
{
	memset(&static_transparent_msg_info,0,sizeof(RECIVE_USART_DATA_HANDLE));
	memset(&static_transprent_buff,0,sizeof(RECIVE_USART_DATA_BUFF));
}
static TP_u8  uart_recive_write_buff(TP_uc8 *data,TP_uc16 len)
{
    TP_u8 rem_serial = 0;

    if( len <= MAX_USART_RECIVE_LEN ) {
        rem_serial = static_transprent_buff.Download_serial_num;

        if( static_transprent_buff.Download_serial_num > (USART_RECIVE_DEPTHS - 1))
        {
            static_transprent_buff.Download_serial_num = 0;
            rem_serial = static_transprent_buff.Download_serial_num; //add by YWQ
        }
        
        memcpy(&static_transprent_buff.Usart_buff[rem_serial][0],data,len);
        static_transprent_buff.Usart_buff_len[static_transprent_buff.Download_serial_num] = len;
        static_transprent_buff.Download_serial_num++;
        
        if(static_transprent_buff.Download_serial_num == static_transprent_buff.Buff_send_serial_num)
        {
            //printf("data deal too slow");
            static_transprent_buff.Download_serial_num = rem_serial;
			static_transprent_buff.Buff_send_serial_num = rem_serial;
            return 0;
        }
    }
    
    return 1;
}
u8 uart_msg_recive(u8 RcvChar)
{
    //时间位清零，规定时间内接收不到数据，放弃数据
    static_transparent_msg_info.time = 0x00;

    if((RcvChar == SOF_HIGH_BYTE_DEF) && (static_transparent_msg_info.Recive_len == 0x00))     //判断数据头
    {
        static_transparent_msg_info.Data[static_transparent_msg_info.Recive_len ++] = SOF_HIGH_BYTE_DEF;
    }
    else if((RcvChar == SOF_LOW_BYTE_DEF)  &&  (static_transparent_msg_info.Recive_len == 0x01))   //判断数据头
    {
        static_transparent_msg_info.Data[static_transparent_msg_info.Recive_len ++] = SOF_LOW_BYTE_DEF;
    }
	else if(static_transparent_msg_info.Recive_len == 2)
	{
		static_transparent_msg_info.Data[static_transparent_msg_info.Recive_len ++] = RcvChar;
	}
    else if(static_transparent_msg_info.Recive_len == 3)
    {
        if((RcvChar < (MAX_USART_RECIVE_LEN - 5)) && (RcvChar > 4))       //长度在正常值之间
        {
            static_transparent_msg_info.Data[static_transparent_msg_info.Recive_len ++] = RcvChar;
            static_transparent_msg_info.Data_len = RcvChar;
        }
        else 
        {
            static_transparent_msg_info.Recive_len = 0x00;                //这串数据不对
        }
    }
    else if((static_transparent_msg_info.Recive_len > 3) && (static_transparent_msg_info.Recive_len < (MAX_USART_RECIVE_LEN - 2))) //接受数据
    {
        static_transparent_msg_info.Data[static_transparent_msg_info.Recive_len ++] = RcvChar;
        
        if(static_transparent_msg_info.Recive_len > (static_transparent_msg_info.Data_len + 5))
        {
			uart_recive_write_buff((TP_uc8*)static_transparent_msg_info.Data,(TP_uc16)static_transparent_msg_info.Recive_len);
			static_transparent_msg_info.time = 0x00;
            static_transparent_msg_info.Recive_len = 0x00;
			return DEAL_SUCCESS;
        }
    }
    else
    {
        static_transparent_msg_info.Recive_len = 0x00;                //这串数据不对,重新接受
    }
	return DEAL_ERROR;
}
void uart_msg_deal(void)
{
    TP_u8 i = 0;
    
    i = static_transprent_buff.Buff_send_serial_num;
    
    if(static_transprent_buff.Buff_send_serial_num != static_transprent_buff.Download_serial_num)
    {
		if((static_transprent_buff.Buff_send_serial_num >USART_RECIVE_DEPTHS)||(static_transprent_buff.Download_serial_num >USART_RECIVE_DEPTHS))
		{
			return;										//防止内存溢出导致错误值
		}	
        if(static_transprent_buff.Buff_send_serial_num > (USART_RECIVE_DEPTHS - 1))
        {
            static_transprent_buff.Buff_send_serial_num = 0;
            
            i = static_transprent_buff.Buff_send_serial_num; //add by YWQ
        }
        Usart_recive_deal((TP_uc8*)&static_transprent_buff.Usart_buff[i][0],(TP_uc32)static_transprent_buff.Usart_buff_len[i]);
        
        static_transprent_buff.Buff_send_serial_num++;
    }
}

u8 Usart_recive_deal(TP_uc8 *data, TP_uc8 len )
{
	PROTOCOL_DEAL    Usart_protocol_deal = {0};
	u8 arg_data[20] = {0};

    //printf("66:len = %d\r\n", len);
	if((data == NULL) || (len < 6) || (len > MAX_USART_RECIVE_LEN))
	{
		//printf("non valied msg,discard it all\r\n");
		return DEAL_ERROR;
	}
    //printf("77\r\n");
	Usart_protocol_deal.SEQ = BYTE_MOVE(*(data + 5),*(data + 6));	
//	for(i = 0;i<len;i++)
//		printf("data[%d] = %x",i,data[i] );
//	printf("len = %x,crc = %2x,%2x\r\n",len,CreateCrc16_Self(data+ 4,len - 6),BYTE_MOVE(*(data + len -2),*(data + len -1)));
	if(BYTE_MOVE(*(data + len -2),*(data + len -1))  != CreateCrc16_Self(data+ 4,len - 6))                //CRC校验
    {                                                                                                //校验不对
        send_crc_error_data(Usart_protocol_deal.SEQ);
        return DEAL_ERROR;
	}
    
    Usart_protocol_deal.Opcode = BYTE_MOVE(*(data + 9),*(data + 10));                                  //赋值功能码
    Usart_protocol_deal.Channel_ID = *(data + 8);
    Usart_protocol_deal.ControlField = 0x20;
    Usart_protocol_deal.ARG = (data + 11);
    Usart_protocol_deal.ARG_len = (*(data + 7) - 3);    //根据协议，此处应该 data + 2

	memset(arg_data,0,sizeof(arg_data)/sizeof(arg_data[0]));
	//printf("opcode = %2x,crc = %2x\r\n",Usart_protocol_deal.Opcode,CreateCrc16_Self(data+ 4,len - 6));
	switch(Usart_protocol_deal.Opcode)
	{
		case OPCODE_DEVICE_VERSION_QUERY:
		{
			arg_data[0] = 0;
			arg_data[1] = 0x10;
			arg_data[2] = Oled_Display_Info.zigbee_Version;
			arg_data[3] = Oled_Display_Info.mcu_Version;
			arg_data[4] = 0x10;
			ack_info(Usart_protocol_deal,arg_data,5);
		}
			break;
		case OPCODE_DEVICE_EUI64_QUERY://ED01
		{
			u8 all_ff[8] ={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff} ;
			arg_data[0] = 0;	
			if(memcmp(Oled_Display_Info.zigbee_mac,all_ff,8) == 0)					//未读取到mac，重新读取一次
				Query_Device_Info(arg_data[0]);
			memcpy(&arg_data[1],Oled_Display_Info.zigbee_mac,8);//表示v1.0
			ack_info(Usart_protocol_deal,arg_data,9);
		}
			break;
		case OPCODE_EXIT_FACTORY_TEST_MODE:
		{
			arg_data[0] = 0;
			arg_data[1] = Usart_protocol_deal.ARG[0];
			ack_info(Usart_protocol_deal,arg_data,2);
		}
			break;
		case OPCODE_KEY_TEST_QUERY:
		{
			arg_data[0] = 0;
			arg_data[1] = boad_test_falg[0];
			arg_data[2] = boad_test_falg[1];
			arg_data[3] = boad_test_falg[2];
			arg_data[4] = boad_test_falg[3];
			arg_data[5] = boad_test_falg[4];
			arg_data[6] = boad_test_falg[5];
			ack_info(Usart_protocol_deal,arg_data,7);
		}
			break;
		case OPCODE_SCREEN_OFF:
		{
			BL55072A_Clear();									//息屏
			LCD_CONTROL(0);		
			Operate_Led_Button(Oled_Display_Info.device_type,0);	
			arg_data[0] = 0;
			arg_data[1] = 0;
			Oled_Display_Info.recv_lcd_off = 0x85;
			ack_info(Usart_protocol_deal,arg_data,2);
		}
			break;
		case OPCODE_TEMPTURE_QUERY:
		{
			s16 temper = 0;
			if(Oled_Display_Info.device_type == 1)
			{
				temper = Oled_Display_Info.Air_Device.real_temperature ;
			}
			else if(Oled_Display_Info.device_type == 2)
			{
				temper = Oled_Display_Info.FLOOR_Device.real_temperature ;
			}
			else if(Oled_Display_Info.device_type == 3)
			{
				temper = Oled_Display_Info.FAN_Device.real_temperature ;
			}
			arg_data[0] = 0;
			arg_data[1] = (temper>>8)&0xff;
			arg_data[2] = temper&0xff;
			ack_info(Usart_protocol_deal,arg_data,3);
		}
			break;
	}
    return DEAL_SUCCESS;
}

void ack_info(PROTOCOL_DEAL recv_data, TP_uc8* arg, TP_u8 len)
{
    PROTOCOL_SEND_MSG send_data;  
    
    TP_u16 send_data_SEQ;
    
    send_data.ARG          = arg;
    send_data.ARG_len      = len;
    send_data.Channel_ID   = recv_data.Channel_ID;
    send_data.ControlField = recv_data.ControlField;
    send_data.Opcode       = recv_data.Opcode;
    
    send_data_SEQ          = recv_data.SEQ;
    
    uart_protocol_send(send_data, send_data_SEQ);
}

void proximity_triagger_report(void)
{
	PROTOCOL_SEND_MSG send_data;  
    TP_u8 KK_Buf[2] = {0};
    TP_u16 send_data_SEQ;
    
    send_data.ARG          = KK_Buf;
    send_data.ARG_len      = 1;
    send_data.Channel_ID   = 0;
    send_data.ControlField = 0x20;
    send_data.Opcode       = OPCODE_PROXIMITY_TRIAGGER;
    
    send_data_SEQ          = g_u16++;
    
    uart_protocol_send(send_data, send_data_SEQ);
}

void uart_protocol_send(PROTOCOL_SEND_MSG send_protocol,TP_u16 SEQ)
{
    //TP_u8 KK_Buf[125] = {0};
	TP_u16 KK_CRC16_Num = 0;  //计算校验值
   
	//memset(KK_Buf,0,sizeof(KK_Buf));
    g_u8Temp_Buffer[0] = 0xAA;
    g_u8Temp_Buffer[1] = 0x55;	
	g_u8Temp_Buffer[2] = (send_protocol.ARG_len + 7)>>8;      //长度 7
    g_u8Temp_Buffer[3] = (send_protocol.ARG_len + 7)&0xff;      //长度 7
    g_u8Temp_Buffer[5] = (SEQ>>8) & 0xff;      //序列号H
    g_u8Temp_Buffer[6] = (SEQ & 0xff);         //序列号L
    g_u8Temp_Buffer[4] = send_protocol.ControlField;  //控制域:  不需要ack

	g_u8Temp_Buffer[7] = send_protocol.ARG_len+3;  //长度
    g_u8Temp_Buffer[8] = send_protocol.Channel_ID;  //下行协议:  信道00
    g_u8Temp_Buffer[9] = ((send_protocol.Opcode >> 8) & 0xff);  //下行协议:  Opcode_H
    g_u8Temp_Buffer[10] = (send_protocol.Opcode & 0xff);  //下行协议:  Opcode_L
    memcpy(&g_u8Temp_Buffer[11],send_protocol.ARG,send_protocol.ARG_len);
		//KK_Buf[9] = 0X00;

    KK_CRC16_Num = CreateCrc16_Self((TP_uc8*)&g_u8Temp_Buffer[4],send_protocol.ARG_len + 7);
    g_u8Temp_Buffer[send_protocol.ARG_len + 11] = (KK_CRC16_Num>>8) & 0xff;  //校验值 CRC_H
    g_u8Temp_Buffer[send_protocol.ARG_len + 12] = KK_CRC16_Num & 0xff;  //校验值 CRC_L

    UART0_Send_Bytes(g_u8Temp_Buffer,send_protocol.ARG_len + 13);
}
/*
接收到的数据crc错误回复
*/
static void send_crc_error_data(TP_u16 seq_data)
{
    TP_u8 send_data[9];
    TP_u16 crc;
    send_data[0] = 0xAA;
    send_data[1] = 0x55;
	send_data[2] = 0x00;
    send_data[3] = 0x03;
    send_data[4] = 0x22;
    send_data[5] = (seq_data >> 8);
    send_data[6] = (seq_data & 0xff);
    crc = CreateCrc16_Self(&send_data[4],3);
    send_data[7] = (crc >> 8);
    send_data[8] = (crc & 0xff);
    UART0_Send_Bytes(send_data,9);
}

/*
接收到的数据crc错误回复
*/
TP_u16 CreateCrc16_Self(const u8* msg, TP_u8 len) 
{
    TP_u16 crc = 0xFFFF;
    TP_u32 i, j = 0;

    for (i = 0; i < len; i++) 
    {
        for (j = 0; j < 8; j++) 
        {
            //强制转换 ADD BY YWQ
            TP_bool c15 = (TP_bool)((crc >> 15 & 1) == 1);
            TP_bool bit = (TP_bool)((msg[i] >> (7 - j) & 1) == 1);
            
            crc <<= 1;
            
            if (c15 ^ bit) 
            {
                crc ^= 0x1021;
            }
        }
    }
    return crc;
}
