#include "Uwater_Aes_128.h"
#include "UWater_DataTools_App.h"
#include "type.h"


u16 HexToBcd16(u16 value)
{
    u16 rtval, temp;
    u8 i;
    rtval = 0;
    i = 0;
    while (value)
    {
        temp = value % 10;
        rtval |= (temp << i);
        i += 4;
        value /= 10;
    }
    return rtval;
}

u8 BCD2HEX(u8 bcd_data) //BCD转为HEX子程序
{
    unsigned char temp;
    temp = ((bcd_data>>4) * 10 + (bcd_data & 0x0F));//2020-10-31
    return temp;
}

u8 HEX2BCD(u8 hex_data) //HEX转为BCD子程序
{
    unsigned char temp;
    temp = (hex_data / 10 * 16 + (hex_data % 10));//2020-10-31
    return temp;
}

u32 HexToBcd32(u32 value)
{
    u32 rtval, temp;
    u8 i;
    rtval = 0;
    i = 0;
    while (value)
    {
        temp = value % 10;
        rtval |= (temp << i);
        i += 4;
        value /= 10;
    }
    return rtval;
}

u32 Gp30DataToBcd(u32 data, u32 times)
{
    float tempf;
    u32 temp32;
    tempf = ((float) data / 65536);
    temp32 = (u32) (tempf * times);
    return (HexToBcd32(temp32));
}

u16 FramHexToBcd16(u16 value) 
{
    u16 rtval, temp;
    u8 i;
    rtval = 0;
    i = 0;
    while (value)
    {
        temp = value % 10;
        rtval |= (temp << i);
        i += 4;
        value /= 10;
    }
    return rtval;
}

void fram_tools_writeU32ByBuf(u8 *pU32Buf, u8 *pSrc, u8 dir)
{
    int temp_i=0;
    if(dir == 0)//data0-1-2-3
		{
				for(temp_i=0;temp_i<4;temp_i++)
				{
						pU32Buf[temp_i] = *pSrc++;
				}
		}
		else//data3-2-1-0
		{
				for(temp_i=0;temp_i<4;temp_i++)
				{
						pU32Buf[3-temp_i] = *pSrc++;
				}
		}
}
/*******************************************************************************
 * @fun_name:	check_apply_addr
 * @brief   :   通讯地址判断
 * @param[in] :	None
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
u8 check_apply_addr(u8 *p_buf,u8 *des_buf)
{
    u8 tmp_i = 0;
    for (tmp_i = 0; tmp_i < 7; tmp_i++)
    {
        if (*(p_buf + tmp_i) != *(des_buf + tmp_i))//s_met_info.met_addr[tmp_i])
        {
            return ERROR;
        }
    }
    return SUCCESS;
}
/*******************************************************************************
 * @fun_name:	check_cs
 * @brief   :   cs校验计算
 * @param[in] :	*p;len
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
u8 check_cs(u8* start_sddr, u8 datas_len)
{
    u8 tmp_i = 0;
    u8 tmp_cs = 0;
    for (tmp_i = 0; tmp_i < datas_len; tmp_i++)
    {
        tmp_cs += *(start_sddr + tmp_i);
    }
    return tmp_cs;
}
/*******************************************************************************
 * @fun_name:	AES_decrypt
 * @brief   :    
 * @param[in] :	 
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
void AES_decrypt(u8 *buf, u8 len, u8 *key)
 {
	u8 p;
	u8 key1[16];
	u8 k,l;
	u8 start_data_length;     //填补之前原数据域字节长度  
	u8 recdata_length;       //填补之后需要发送的字节长度  
	start_data_length=len;
	//recdata_length=(start_data_length/16+1)*16;     

	recdata_length=start_data_length/16;  
	for(k=0;k<recdata_length;k++)
	{
		u8 state[16];
		for(l=0;l<16;l++)
		state[l]=buf[k*16 + l];
		for(p=0;p<16;p++)
		{
			key1[p]  = key[p];
		}
		aes_enc_dec(state,key1,1); //添加解密              
		for(l=0;l<16;l++)
		{
			buf[k*16 + l] =state[l];
		}
	}      
 }
 
 void SYNB_AES_decrypt(u8 *buf, u16 len, u8 *key)
 {
	u8 p;
	u8 key1[16];
	u16 k,l;
	u16 start_data_length;     //填补之前原数据域字节长度  
	u16 recdata_length;       //填补之后需要发送的字节长度  
	start_data_length=len;//s_frame_rcv_arr[s_frame_struct.head_site +11];
	recdata_length=(start_data_length/16+1)*16;     
	for(k=0;k<(recdata_length/16);k++)
	{
		u8 state[16];
		for(l=0;l<16;l++)
		state[l]=buf[k*16 + l];//s_frame_rcv_arr[k*16+s_frame_struct.head_site +12 + l];
		for(p=0;p<16;p++)
		{
			key1[p]  = key[p];
		}
		aes_enc_dec(state,key1,1); //添加解密              
		for(l=0;l<16;l++)
		{
			buf[k*16 + l] =state[l];//s_frame_rcv_arr[k*16+s_frame_struct.head_site +12 + l] =state[l];
		}
	}      
 }
 /*******************************************************************************
 * @fun_name:	AES_encrypt
 * @brief   :    
 * @param[in] :	 加密
 * @param[out]: None
 * @retval    : None
 * @other     :
 ******************************************************************************/
void AES_encrypt(u8 num,u8 *buf, u16 len, u8 *key)
 {
     u8 key2[16];
     u16 start_data_length;     //填补之前原数据域字节长度  
     u8 adddata_length;        //填补的字节长度  
     u16 sentdata_length;       //填补之后需要发送的字节长度  
     u8 add_data;              //填补的数据，取16的余数
    switch(num)
    {        
        case 0:
        {
            start_data_length=len;
            sentdata_length=(start_data_length/16+1)*16;
            adddata_length=sentdata_length-start_data_length;
            add_data=adddata_length;
            u8 j;
            for(j=start_data_length;j<sentdata_length;j++)   //填充发送区
            {
                buf[j]=add_data; //s_frame_send_buf[j+14]=add_data;
            }
     
            u8 k,p; 
         
            for(k=0;k<(sentdata_length/16);k++)
            {
                u8 state[16];
                for(p=0;p<16;p++)
                    state[p]=buf[k*16+p];
                for(p=0;p<16;p++)
                {
                   key2[p]  = key[p];
                }
                aes_enc_dec(state,key2,0);                
                for(p=0;p<16;p++)
                buf[k*16+p]=state[p];//s_frame_send_buf[k*16+14+p]=state[p];
            }
            break;
        }
        case 1:
        {
            start_data_length=len;
            sentdata_length=(start_data_length/16+1)*16;
            adddata_length=sentdata_length-start_data_length;

            add_data=adddata_length;
            u16 j;
           for(j=start_data_length;j<sentdata_length;j++)   //填充发送区
            {
                buf[j]=add_data;//s_frame_send_buf[j+15]=add_data;
            }
            u8 k,p; 
            for(k=0;k<(sentdata_length/16);k++)
            {
                u8 state[16];
                for(p=0;p<16;p++)
                {
                    state[p]=buf[k*16+p];//s_frame_send_buf[k*16+15+p];
                }
                for(p=0;p<16;p++)
                {
                    key2[p]  = key[p];
                }
                aes_enc_dec(state,key2,0);                
                for(p=0;p<16;p++)
                {
                    buf[k*16+p]=state[p];//s_frame_send_buf[k*16+15+p]=state[p];
                }                   
            }
            break;
        }
    }      
 }
 
//CRC16计算方法1:使用2个256长度的校验表
static u16 wCRCTalbeAbs[] = {0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000,
    0x9C01, 0x8801, 0x4400};

u16 CRC16(u8* pchMsg, u16 wDataLen)
{
    u16 wCRC = 0xFFFF;
    u16 i;
    u8 chChar;
    for(i = 0; i < wDataLen; i++)
    {
        chChar = *pchMsg++;
        wCRC = wCRCTalbeAbs[(chChar ^ wCRC) & 15] ^ (wCRC >> 4);
        wCRC = wCRCTalbeAbs[((chChar >> 4) ^ wCRC) & 15] ^ (wCRC >> 4);
    }
    return wCRC;
}
//ASCIL转HEX
//ASCII码转HEX：
//ASCII码:0-9 --> HEX:0x0-0x9 --> Dec:0-9
//ASCII码:a/A-f/F --> HEX:0xA-0xF --> Dec:10-15
uint8_t AsciiToHex(uint8_t Ascii_Byte)
{	
    if(Ascii_Byte & 0x80) 
	{
		return Ascii_Byte;
	}
	if((Ascii_Byte >= '0')&&(Ascii_Byte <= '9'))
	{
		return Ascii_Byte - '0'; //0-9
	}
	else if( (Ascii_Byte >= 'a')&&(Ascii_Byte <= 'f') )
	{
		return Ascii_Byte - 'a' + 10; //10-15
	}
	else if( (Ascii_Byte >= 'A')&&(Ascii_Byte <= 'F') )
	{
		return Ascii_Byte - 'A' + 10; //10-15
	}
	return Ascii_Byte;
}



