#include "can_process.h" #include "uart_process.h" #include "crc_cal.h" #include "stdlib.h" #include "stdio.h" #include "string.h" #include "encrypt.h" #include "Update.h" #include "ctf_process.h" //命令处理 static void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data) { switch(Command) { //CDL在线检测 case 0x1100: { SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1100, (uint8_t*)NULL); break; } //设备开关机 case 0x2201: { if(Data[0] == 0xF0)//关机 { HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET); } else if(Data[0] == 0xF1)//开机 { HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET); } break; } //复位 case 0x4400: { Refresh_Flag(); __set_FAULTMASK(1);//关闭所有中断 HAL_NVIC_SystemReset(); break; } //随机码和密钥进行校验 case 0x5514: { static uint8_t InputCode[12]; static uint8_t InputKey[8]; static uint8_t OutputCode[12]; static uint8_t SendData[27]; memcpy(InputCode, Data, 12); memcpy(InputKey, Data + 12, 8); CheckCodeCal(InputCode, InputKey, OutputCode); memcpy(SendData, OutputCode, 12); memcpy(SendData + 12, (uint8_t*)"V1.4.0_20200518", 15); SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x551B, SendData); break; } //授权 case 0x6600: { uint8_t SendData[24]; Ctf_CalAndSave(); memcpy(SendData, MAC_ID, 12); memcpy(SendData + 12, Check_Code, 12); SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x6618, SendData); HAL_Delay(200); __set_FAULTMASK(1);//关闭所有中断 HAL_NVIC_SystemReset(); break; } default:break; } } //发送数据 void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t Command, uint8_t* Data) { uint8_t SendBuf[255] = {0}; uint8_t CRC_Buf[255] = {0}; uint32_t CRC_Result = 0x00000000; uint8_t DataLength; DataLength = (uint8_t)(Command & 0xFF); SendBuf[0] = FRAME_BEGIN1; SendBuf[1] = FRAME_BEGIN2; SendBuf[2] = 0x07; SendBuf[3] = 0xFF; SendBuf[4] = Mode; SendBuf[5] = DataLength + 2; SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF); SendBuf[7] = DataLength; memcpy(SendBuf + 8, Data, DataLength); memcpy(CRC_Buf, SendBuf, 8 + DataLength); CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8); SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF); SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF); SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF); SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF); SendBuf[12 + DataLength] = FRAME_END; UART_SendData(ptTx, SendBuf, DataLength + 13); } //串口数据解析 void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut) { uint8_t Mode, CmdLength, DataLength; static uint8_t Data[255], CRC_Buf[255]; uint16_t Cmd, i, ID; uint32_t CrcResult, CrcData; uint8_t FrameBegin1, FrameBegin2; static uint32_t DelayTimeCnt = 0; static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE; if(ptUartRx->ucBufCnt >= 13) { //读取帧头 FrameBegin1 = UART_ReadChar(ptUartRx, 0); CRC_Buf[0] = FrameBegin1; FrameBegin2 = UART_ReadChar(ptUartRx, 1); CRC_Buf[1] = FrameBegin2; if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2)) { CRC_Buf[2] = UART_ReadChar(ptUartRx, 2); CRC_Buf[3] = UART_ReadChar(ptUartRx, 3); ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[3]; //读取帧模式 Mode = UART_ReadChar(ptUartRx, 4); CRC_Buf[4] = Mode; if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT)) { //读取命令段长度和命令字 CmdLength = UART_ReadChar(ptUartRx, 5); CRC_Buf[5] = CmdLength; Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7); CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF); CRC_Buf[7] = (uint8_t)(Cmd & 0xFF); DataLength = UART_ReadChar(ptUartRx, 7); if((CmdLength - DataLength) == 2) { if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte { if(IsWaitRX_Flag == FALSE) { DelayTimeCnt = HAL_GetTick(); IsWaitRX_Flag = TRUE; } if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms { UART_DelChar(ptUartRx, ptUartRx->ucBufCnt); IsWaitRX_Flag = FALSE; } return; } else { IsWaitRX_Flag = FALSE; //接收到完整正确数据包 for(i=0; i