| | |
| | | #include "main.h" |
| | | #include "rs485_data_analysis_pro.h" |
| | | #include "power_manage.h" |
| | | #include "AES128.h" |
| | | #include "system_general_para.h" |
| | | |
| | | static SIZHU_V4_PROTOCOL_HEAD_T protocol_head_up_g ={.frame_start_code = SIZHU_FRAME_HEAD,.data_start_code = SIZHU_DATA_HEAD}; |
| | | static SIZHU_V4_PROTOCOL_NORMAL_DATA_HEAD_T data_head_g; |
| | | static SIZHU_V4_PROTOCOL_TAIL_T proto_tail_up_g = {.frame_end_code = SIZHU_FRAME_TAIL}; |
| | | static SIZHU_V4_REPORT_CTRL_PARA_T report_ctrl_para_g; |
| | | ErrorStatus wrc_sizhuV4_result_flag = FAIL; //×îÖÕµÄͨѶ½á¹ûÅжϣ¬³õʼÖÃFAIL£¬×îÖÕ¸ù¾ÝÐÒéÈ·Èϱ¾´ÎͨѶÊÇ·ñ³É¹¦£¬±ÈÈç˼ÖþµÄÐÒéͨ¹ý½áÊøÖ¡ÅжÏÊÇ·ñ×îÖճɹ¦ |
| | | |
| | | //static uint16_t current_report_data_num = 0; |
| | | |
| | | uint32_t DataReadAddrInit(uint32_t current_save_addr,uint16_t data_num) |
| | | { |
| | | uint32_t read_start_addr ; |
| | | //?»ñÈ¡ÆðʼµØÖ·£¬Ã»Ð´Íê |
| | | read_start_addr = current_save_addr; |
| | | |
| | | return read_start_addr; |
| | | } |
| | | |
| | | void ReadDataFromRecord(uint32_t * read_addr ,uint8_t *buf) |
| | | { |
| | | //?´Ó´æ´¢ÀïÃæ¶ÁÈ¡Êý¾Ýµ½buf,Èç¹ûÅжϴ洢¶ÁÈ¡µÄÊý¾ÝÒì³££¬ÔòÌî³ä0»òÕßÈÝÒ×ʶ±ðµÄÊý¾Ý |
| | | //read_addr++ |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | void SizhuV4SendParaInit(void) |
| | | { |
| | | /*ͨѶÏà¹Ø*/ |
| | | wrc_ctrl_para_g.time_out_time_sec = wrc_para_sizhu_g.comm_timeout_time; //³¬Ê±Ê±¼ä |
| | | wrc_sizhuV4_result_flag = FAIL; |
| | | wrc_ctrl_para_g.wrc_restart_time_sec_cnt = wrc_para_sizhu_g.retransmission_interval ; //ÖÃÖØ±¨¼ä¸ôʱ¼ä |
| | | |
| | | |
| | | /*ÐÒéÏà¹Ø*/ |
| | | protocol_head_up_g.frame_start_code = SIZHU_FRAME_HEAD; |
| | | arrayA_2_arrayB((uint8_t*)equipment_basic_inf_g.serial_number, (uint8_t*)&protocol_head_up_g.device_num, 8, LITTLE_ENDIAN); |
| | | protocol_head_up_g.data_start_code = SIZHU_DATA_HEAD; |
| | |
| | | |
| | | |
| | | #else |
| | | report_ctrl_para_g.current_report_data_num = wrc_data_num_ctrl_para_sizhu_g.report_data_save_num + wrc_data_num_ctrl_para_sizhu_g.report_data_left_num ; |
| | | report_ctrl_para_g.current_report_data_num = (report_ctrl_para_g.current_report_data_num > ONCE_REPORT_MAX_NUM)? ONCE_REPORT_MAX_NUM:report_ctrl_para_g.current_report_data_num; //ÏÞÖÆÉϱ¨ÌõÊý |
| | | //? report_ctrl_para_g.report_data_read_addr = (current_save_addr,report_ctrl_para_g.current_report_data_num); |
| | | wrc_data_num_ctrl_para_sizhu_g.report_data_save_num =0 ; |
| | | wrc_data_num_ctrl_para_sizhu_g.report_data_left_num = report_ctrl_para_g.current_report_data_num; |
| | | //?wrc_data_num_ctrl_para_sizhu_g´æ´¢ |
| | | |
| | | //¼ÆËã×ÜÖ¡Êý¼°×îºóÒ»Ö¡ÌõÊý |
| | | protocol_head_up_g.frame_sum = 1; |
| | | report_ctrl_para_g.last_frame_data_num = report_ctrl_para_g.current_report_data_num; |
| | | while(report_ctrl_para_g.last_frame_data_num > FRAME_DATA_MAX) { |
| | | report_ctrl_para_g.last_frame_data_num -= FRAME_DATA_MAX; |
| | | protocol_head_up_g.frame_sum ++; |
| | | } |
| | | |
| | | protocol_head_up_g.frame_num = 1; |
| | | |
| | | #endif |
| | | |
| | | } |
| | | |
| | | |
| | | void DataAes128EcbEncrypt() |
| | | void DataAes128EcbEncrypt(uint8_t KeyVer,uint8_t *buf_in,uint8_t *buf_out,uint16_t *length) |
| | | { |
| | | uint8_t yushu; |
| | | uint8_t fill_num; |
| | | // *length = ((*length % 16 )!=0) ? ((*length >>4) + 1)*16 : *length; |
| | | /*²»ÊÇ16ÕûÊý±¶£¬ÔòÌî³äÖÁ16µÄÕûÊý±¶£¬È±N¸öÌî³äN¸öN*/ |
| | | yushu = (*length) & 0x0f; |
| | | if(yushu != 0){ |
| | | fill_num = 16 - yushu; |
| | | for(uint8_t count_i=0;count_i < fill_num ;count_i ++){ |
| | | *(buf_in + *length + count_i) = fill_num; |
| | | } |
| | | *length = ((*length >>4) + 1) <<4; |
| | | } |
| | | |
| | | //°´16¸öΪһ×é½øÐмÓÃÜ |
| | | for(uint16_t count_i =0;count_i < (*length >>4);count_i++){ |
| | | |
| | | AES128_ECB_encrypt(buf_in + count_i*16, (u8*)wrc_para_sizhu_g.KVer_AES_128[KeyVer&0x7f], buf_out + count_i*16); |
| | | } |
| | | |
| | | } |
| | | |
| | | void DataAes128EcbDecrypt(uint8_t KeyVer,uint8_t *buf_in,uint8_t *buf_out,uint16_t length) |
| | | { |
| | | if(((length) & 0x0f)!=0) |
| | | return; |
| | | |
| | | //°´16¸öΪһ×é½øÐнâÃÜ |
| | | for(uint16_t count_i =0;count_i < (length >>4);count_i++){ |
| | | |
| | | AES128_ECB_decrypt(buf_in + count_i*16, (u8*)wrc_para_sizhu_g.KVer_AES_128[KeyVer&0x7f], buf_out + count_i*16); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | void SizhuV4SendNormalData(void) |
| | | //·µ»ØÊý¾Ý³¤¶È |
| | | uint16_t SizhuV4SendNormalData2buf(uint8_t * send_buf) |
| | | { |
| | | uint8_t send_buf[1024] = {0}; |
| | | // uint8_t send_buf[1024] = {0}; |
| | | uint16_t crc_addr; |
| | | uint8_t current_frame_data_num; |
| | | SIZHU_V4_PROTOCOL_NORMAL_DATA_PACK_T one_pack_data; |
| | | |
| | | arrayA_2_arrayB((uint8_t*)&sys_clockBCD_g, (uint8_t*)&protocol_head_up_g.device_time, 8, LITTLE_ENDIAN); |
| | | |
| | | //Ìî³äÊý¾Ý |
| | | //²âÊÔ·¢ËÍÒ»Ö¡µ±Ç°Êý¾Ý |
| | | #ifdef WRC_TEST_MODE_1 |
| | | //²âÊÔ·¢ËÍÒ»Ö¡µ±Ç°Êý¾Ý |
| | | protocol_head_up_g.frame_sum = 1; |
| | | protocol_head_up_g.frame_num = 1; |
| | | protocol_head_up_g.data_length = DATA_PACKET_HEAD_LENGTH + DATA_PACKET_LENGTH; |
| | |
| | | arrayA_2_arrayB((uint8_t*)&one_pack_data, (uint8_t*)&send_buf[FIRST_DATA_SHIFT_SIZHU + DATA_PACKET_HEAD_LENGTH], DATA_PACKET_LENGTH, LITTLE_ENDIAN); |
| | | |
| | | #else |
| | | //?´Ó´æ´¢ÀïÃæ¶ÁÈ¡¶³½á¼Ç¼½øÐÐÉϱ¨ |
| | | |
| | | current_frame_data_num = (protocol_head_up_g.frame_num < protocol_head_up_g.frame_sum)? FRAME_DATA_MAX:report_ctrl_para_g.last_frame_data_num; |
| | | |
| | | for(uint8_t count_i =0;count_i < current_frame_data_num;count_i++ ){ |
| | | ReadDataFromRecord(&report_ctrl_para_g.report_data_read_addr ,(uint8_t*)&send_buf[FIRST_DATA_SHIFT_SIZHU + DATA_PACKET_HEAD_LENGTH + count_i * DATA_PACKET_LENGTH]); |
| | | } |
| | | protocol_head_up_g.data_length = DATA_PACKET_HEAD_LENGTH + DATA_PACKET_LENGTH * current_frame_data_num; |
| | | data_head_g.data_pack_num = current_frame_data_num; |
| | | |
| | | #endif |
| | | |
| | | arrayA_2_arrayB((uint8_t*)&protocol_head_up_g, (uint8_t*)&send_buf[0], FIRST_DATA_SHIFT_SIZHU, LITTLE_ENDIAN); |
| | | arrayA_2_arrayB((uint8_t*)&data_head_g, (uint8_t*)&send_buf[FIRST_DATA_SHIFT_SIZHU], DATA_PACKET_HEAD_LENGTH, LITTLE_ENDIAN); |
| | | |
| | | //¼ÓÃÜ |
| | | if((protocol_head_up_g.key_ver & 0x80) == 0x80){ |
| | | DataAes128EcbEncrypt(protocol_head_up_g.key_ver,(uint8_t*)&send_buf[FIRST_DATA_SHIFT_SIZHU],(uint8_t*)&send_buf[FIRST_DATA_SHIFT_SIZHU],&protocol_head_up_g.data_length); |
| | | } |
| | | |
| | | //ÓÉÓÚ¼ÓÃÜÌî³ä»áµ¼ÖÂÊý¾Ý³¤¶È±ä»¯£¬ËùÒÔÖ¡Í·µÄ×ªÒÆÐèÒª·ÅÔÚ¼ÓÃÜÖ®ºó |
| | | arrayA_2_arrayB((uint8_t*)&protocol_head_up_g, (uint8_t*)&send_buf[0], FIRST_DATA_SHIFT_SIZHU, LITTLE_ENDIAN); |
| | | |
| | | //¼ÆËãcrc |
| | | |
| | | crc_addr = protocol_head_up_g.data_length + FIRST_DATA_SHIFT_SIZHU; |
| | | proto_tail_up_g.check_crc = usMBCRC16(0xFFFF, send_buf, crc_addr); |
| | | proto_tail_up_g.frame_end_code = SIZHU_FRAME_TAIL; |
| | | arrayA_2_arrayB((uint8_t*)&proto_tail_up_g, (uint8_t*)&send_buf[crc_addr], FRAME_TAIL_LENGTH, LITTLE_ENDIAN); |
| | | //·¢ËÍ |
| | | return FIRST_DATA_SHIFT_SIZHU + protocol_head_up_g.data_length + FRAME_TAIL_LENGTH; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | |
| | | |
| | | WRC_STATUS SizhuV4ProtocolDataAnalysis(uint8_t *receive_buf) |
| | | WRC_STATUS SizhuV4ProtocolDataAnalysis(uint8_t *receive_buf,uint8_t *ack_buf,uint16_t *ack_buf_length) |
| | | { |
| | | char *buf_p = NULL; |
| | | uint16_t count_i = 0; |
| | |
| | | } |
| | | |
| | | arrayA_2_arrayB((uint8_t*)buf_p, (uint8_t*)&protocol_head_rec, sizeof(protocol_head_rec), LITTLE_ENDIAN); |
| | | arrayA_2_arrayB((uint8_t*)(buf_p + FIRST_DATA_SHIFT_SIZHU + protocol_head_rec.data_length), (uint8_t*)&protocol_head_rec, 2, LITTLE_ENDIAN); |
| | | arrayA_2_arrayB((uint8_t*)(buf_p + FIRST_DATA_SHIFT_SIZHU + protocol_head_rec.data_length), (uint8_t*)&check_crc, 2, LITTLE_ENDIAN); |
| | | |
| | | if(check_crc == usMBCRC16(0xFFFF,(uint8_t *)buf_p,FIRST_DATA_SHIFT_SIZHU + protocol_head_rec.data_length)){ |
| | | uint8_t ack_buf[1024] = {0}; |
| | | |
| | | arrayA_2_arrayB((uint8_t*)&sys_clockBCD_g, (uint8_t*)&protocol_head_rec.device_time, sizeof(sys_clockBCD_g), LITTLE_ENDIAN); |
| | | arrayA_2_arrayB((uint8_t*)&sys_clockBCD_g, (uint8_t*)&protocol_head_up_g.device_time, sizeof(sys_clockBCD_g), LITTLE_ENDIAN); |
| | | |
| | | |
| | | if((protocol_head_rec.key_ver & 0x80)==0x80){ |
| | | //? ¸ù¾ÝÃÜÔ¿ºÅ½âÃÜ |
| | | DataAes128EcbDecrypt(protocol_head_rec.key_ver,(uint8_t*)(buf_p + FIRST_DATA_SHIFT_SIZHU),(uint8_t*)(buf_p + FIRST_DATA_SHIFT_SIZHU),protocol_head_rec.data_length); |
| | | }else{ |
| | | arrayA_2_arrayB((uint8_t*)buf_p, (uint8_t*)&ack_buf[FIRST_DATA_SHIFT_SIZHU], protocol_head_rec.data_length, LITTLE_ENDIAN);//Ã÷ÎÄ |
| | | // arrayA_2_arrayB((uint8_t*)buf_p, (uint8_t*)&ack_buf[FIRST_DATA_SHIFT_SIZHU], protocol_head_rec.data_length, LITTLE_ENDIAN);//Ã÷ÎÄ |
| | | } |
| | | |
| | | switch(protocol_head_rec.cmd_code_u16){ |