Browse Source

版本号为1.2.2.0,新增:加密UART发送和接收判定

CHENJIE-PC\QiXiang_CHENJIE 3 years ago
parent
commit
9b14694fae
3 changed files with 84 additions and 12 deletions
  1. 3 1
      inc/UartTask.h
  2. 2 2
      inc/app.h
  3. 79 9
      src/UartTask.c

+ 3 - 1
inc/UartTask.h

@@ -32,6 +32,7 @@ extern QueueHandle_t UartWriteCmdHandle;
 #define BMS_ADDRESS_CODE 0x01
 #define UART_READ_CODE 0x03
 #define UART_WRITE_CODE 0x10
+#define UART_ENCRYPT_CODE 0x05
 
 typedef struct Uart_Read_Msg_Type
 {
@@ -66,7 +67,8 @@ typedef struct Uart_Write_Answer_Msg_Type
 }Uart_Write_Answer_Msg_Type;
 typedef enum
 {
-    PROCESS_UART_STATE_IDLE = 0,
+    PROCESS_UART_STATE_ENCRYPT = 0,
+    PROCESS_UART_STATE_IDLE,
     PROCESS_UART_STATE_READ,
     PROCESS_UART_STATE_WRITE,
     PROCESS_UART_STATE_UPDATE,

+ 2 - 2
inc/app.h

@@ -34,8 +34,8 @@ extern "C" {
 #define HWVERSION		    0x0102    //硬件主版本,现为V1.2板
 #define	BLSWVERSION		0x01020000    //BootLoader版本号V1.2.0.0
 #define	DRVSWVERSION		0x01040000     //驱动层版本号V1.4.0.0
-//#define	APPSWVERSION		0x0102011E     
-#define	APPSWVERSION		0x02000003
+#define	APPSWVERSION		0x01020200     
+//#define	APPSWVERSION		0x02000003
 //--------------------------------------------------------------------------------
 
 #define APP_CONFIG_FILE_LATEST_VERSION 0

+ 79 - 9
src/UartTask.c

@@ -28,6 +28,7 @@
 #include "hal_module_adapter.h"
 #include "UartTask.h"
 #include "MainTask.h"
+#include <stdlib.h>
 #include "app.h"
 #include "numeric.h"
 #include "Fota.h"
@@ -65,6 +66,10 @@ void battErrorStateDisplay(void);
 void battLockStateDisplay(UINT8 lockState);
 void SP_BMS_Update_Service(void);
 BOOL BattHeaterSwitch(UINT8* heaterSwitch);
+UINT16  encryptionAlgorithm (UINT16 plainText);
+UINT8  decryptionAlgorithm (UINT16 cipherText);
+UINT8 Uart_Encrypt_Send(void);
+
 //BMS升级函数声明
 UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len);
 void SP_BMS_Update_Service();
@@ -80,7 +85,7 @@ static void UartTask(void* arg)
                       ARM_USART_PARITY_NONE |
                       ARM_USART_STOP_BITS_1 |
                       ARM_USART_FLOW_CONTROL_NONE, 9600);
-    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
     UINT16  Reg_Num = 0;
     UINT16  Uart_Uds_LEN;
     UINT16  Uart_Recv_LEN;
@@ -101,6 +106,21 @@ static void UartTask(void* arg)
     {
         switch (gProcess_Uart_Task)
         {
+			case PROCESS_UART_STATE_ENCRYPT:
+			{
+				UINT8 EncryptFlag=0x00;
+				UINT8 EncryptCount=0;
+				while(EncryptFlag!=0x01&&EncryptCount<=5)
+				{
+					EncryptFlag = Uart_Encrypt_Send();
+					EncryptCount++;
+				}
+				PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+				#ifdef USING_PRINTF
+                    printf("[%d]PROCESS_UART_STATE_ENCRYPT Done\n",__LINE__);
+                #endif
+				break;
+			}
             case PROCESS_UART_STATE_IDLE:
             {
                 osDelay(100);
@@ -819,24 +839,74 @@ UINT8  decryptionAlgorithm (UINT16 cipherText)
 	return (UINT8)plainText;
 }
 
-UINT16  encryptionAlgorithm (UINT8 plainText)
+UINT16  encryptionAlgorithm (UINT16 plainText)
 {
-	UINT16 cipherText = 1;	
+	UINT16 cipherText = 1; 
 	UINT16 privateKeyE = 37507;
 	UINT16 privateKeyN = 10961;
 	plainText = plainText % privateKeyN;
 	while(privateKeyE >0)
 	{
-		if(privateKeyE % 2 ==1)
-		{			
-			cipherText = cipherText * plainText % privateKeyN;
-		}
-		privateKeyE = privateKeyE/2;		
-		cipherText = (plainText * plainText) % privateKeyN;
+	if(privateKeyE % 2 ==1)
+	{   
+	cipherText =  ( cipherText * plainText) % privateKeyN;
+	}
+	privateKeyE = privateKeyE/2;  
+	plainText = (plainText * plainText) % privateKeyN;
 	}
 	return cipherText;
 }
+UINT8 Uart_Encrypt_Send()
+{
+	UINT8 SeedNumberArrray[4]={0x38,0x56,0xfe,0xac};
+	UINT16 EncodeNumberArray[4];
+	UINT8 UartEncryptBuffer[17];
+	UINT8 UartDecryptBuffer[5];
+	UINT16 CRC_chk_buffer;
+	UINT8 timeCount = 0;
+	UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
+	UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
+	UartEncryptBuffer[2] = 0x0c;
+	for(int i=0;i<4;i++)
+	{
+		SeedNumberArrray[i]=rand();
+		EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
+		UartEncryptBuffer[i+3] = SeedNumberArrray[i];
+		UartEncryptBuffer[i*2+7] = EncodeNumberArray[i]>>8;
+		UartEncryptBuffer[i*2+8] = EncodeNumberArray[i];
+	}
+	CRC_chk_buffer = crc_chk(UartEncryptBuffer,17-2);
+	UartEncryptBuffer[15] = CRC_chk_buffer;
+	UartEncryptBuffer[16] = CRC_chk_buffer>>8;
+	USARTdrv->Send(UartEncryptBuffer,17);
+	USARTdrv->Receive(UartDecryptBuffer,5); 	
+	while((isRecvTimeout == false) && (isRecvComplete == false))
+	{
+		timeCount++;
+		osDelay(100);
+		if (timeCount>=10)
+		{
+			timeCount =0;
+			isRecvTimeout = true;
+			break;
+		}
+	}
+	if (isRecvComplete == true)
+	{
+		isRecvComplete = false;
+		return UartDecryptBuffer[2];
+	}
+	else
+	{
+		isRecvTimeout = false;
+		return 0x03;
+	}
+}
+
+
+
 
+/*-----------------------------------------------------------------------------*/
 void SP_BMS_Update_Service() //超力源BMS升级服务
 {