#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> 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; }