Sfoglia il codice sorgente

网上fota升级测试完成,加入协议模拟测试。

CHENJIE-PC\QiXiang_CHENJIE 4 anni fa
parent
commit
356075d0f8
7 ha cambiato i file con 171 aggiunte e 42 eliminazioni
  1. 2 1
      ARMCC/Makefile
  2. 15 0
      inc/Fota.h
  3. 12 14
      inc/TcpTask.h
  4. 1 1
      inc/app.h
  5. 98 0
      src/Fota.c
  6. 11 13
      src/MainTask.c
  7. 32 13
      src/TcpTask.c

+ 2 - 1
ARMCC/Makefile

@@ -23,6 +23,7 @@ obj-y             += PLAT/project/$(TARGET)/apps/qx_app/src/app.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/TcpTask.o  \
 					PLAT/project/$(TARGET)/apps/qx_app/src/GpsTask.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/CANTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/UDSService.o
+					PLAT/project/$(TARGET)/apps/qx_app/src/UDSService.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/Fota.o
 
 include $(TOP)/PLAT/tools/scripts/Makefile.rules

+ 15 - 0
inc/Fota.h

@@ -6,3 +6,18 @@
  * History:      2021-03-15
  * 
  ****************************************************************************/
+#include "bsp_custom.h"
+#define Fota_Addres_Begin (0x2A0000)
+typedef struct _Fota_Type
+{
+    bool Fota_update_flag ;
+    bool Fota_update_error ;
+    UINT32 Fota_All_Data_Len ;
+    UINT32 Fota_Current_Addres ;
+    UINT8 Fota_Recv_Data_Len ;
+    UINT8 Fota_Recv_Data[100] ;
+    UINT32 Fota_Flash_Addres;
+    UINT8 Fota_CRC ;
+
+}Fota_Type;
+void Fota_Func(UINT8 *DataPtr,INT32 connectId);

+ 12 - 14
inc/TcpTask.h

@@ -6,6 +6,9 @@
  * History:      2021-03-07
  *
  ****************************************************************************/
+
+extern UINT32 TcpService;
+
 #define PROC_TCP_TASK_STACK_SIZE           (2048)
 #define QMSG_ID_BASE               (0x160) 
 #define QMSG_ID_NW_IP_READY        (QMSG_ID_BASE)
@@ -24,15 +27,20 @@
 #define BATT_SN_LEN             17
 #define TCP_START_SYM1			0x23
 #define TCP_START_SYM2			0x23
-#define TCP_CMD_SYM				0x02
+#define TCP_CMD_SYM				0x02	//实时信息上报是0x02
 #define TCP_ANS_SYM 			0xFE
 
+/*-------以下为下行指令定义区----------*/
+#define TCP_QUERY_SYM				0x80
+#define TCP_SETCMD_SYM				0x81
+#define TCP_CONCMD_SYM				0x82
+/*----------------end----------------*/
 
 //encrypt methord
 #define TCP_ENCPT_ENABLE		0x00
 #define TCP_ENCPT_DISABLE		0x01
 
-//message type mark
+//message type mark 0x02上报数据单元定义区
 #define BATTMSG				0x80
 #define GPSMSG				0x82
 #define VERSIONMSG			0x86
@@ -112,6 +120,8 @@ typedef struct _VerInfoType
 	UINT8	BLVersion[4];
 	UINT8	DRVVersion[4];
 	UINT8	APPVersion[4];
+	CHAR	BmsType;
+	CHAR	BmsInfo;
 }VerInfoType;
 typedef struct VersionMsgtoTcpType
 {
@@ -138,18 +148,6 @@ typedef struct AppSocketConnectionContext_Tag
     INT32 status;
 }AppSocketConnectionContext;
 
-typedef struct Fota_Type
-{
-    bool Fota_update_flag ;
-    bool Fota_update_error ;
-    UINT32 Fota_All_Data_Len ;
-    UINT32 Fota_Current_Addres ;
-    UINT8 Fota_Recv_Data_Len ;
-    UINT8 Fota_Recv_Data[100] ;
-    UINT32 Fota_Flash_Addres;
-    UINT8 Fota_CRC ;
-
-}Fota_Type;
 typedef enum
 {
     PROCESS_TCP_STATE_IDLE = 0,

+ 1 - 1
inc/app.h

@@ -27,7 +27,7 @@ extern "C" {
 #define HWVERSION		    0x0102    //NB硬件主版本,现为V1.2板
 #define	BLSWVERSION		0x01020000    //BootLoader版本号V1.2.0.0
 #define	DRVSWVERSION		0x01030000     //驱动层版本号V1.3.0.0
-#define	APPSWVERSION		0x01020101    //应用层版本号V1.2.1.0
+#define	APPSWVERSION		0x01020102       
 #define APP_CONFIG_FILE_LATEST_VERSION 2
 #define APP_CONFIG_FILE_NAME  "qxappconfig.nvm"
 //--------------------------------------------------------------------------------

+ 98 - 0
src/Fota.c

@@ -7,3 +7,101 @@
  *
  ****************************************************************************/
 #include "Fota.h"
+#include "ec_tcpip_api.h"
+#include "ps_lib_api.h"
+#include "MainTask.h"
+#include "TcpTask.h"
+#include "os_exception.h"
+extern AppNVMDataType AppNVMData;
+static Fota_Type Fota_S;
+static UINT8 bcc_chk_fota(UINT8* data, UINT8 length);
+static UINT8 Fota_crc_chk(UINT8* data,UINT8 length);
+void Fota_Func(UINT8 *DataPtr,INT32 connectId)
+{
+    UINT8 Fota_Answer[42];
+    UINT8 Fota_Cmd;
+    Fota_S.Fota_Flash_Addres = Fota_Addres_Begin;
+    if(*(DataPtr+30)==0x01)
+    {
+        Fota_S.Fota_update_flag = true;
+        Fota_Cmd = *(DataPtr+31);
+        Fota_Answer[0] = TCP_START_SYM1;
+        Fota_Answer[1] = TCP_START_SYM2;
+        Fota_Answer[2] = TCP_CONCMD_SYM;
+        switch (Fota_Cmd)
+        {
+            case 0x01:
+            {
+                Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
+                Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
+                Fota_Answer[3] = 0x01;
+                memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
+                Fota_Answer[21] = TCP_ENCPT_DISABLE;
+                Fota_Answer[22] = 0x00;
+                Fota_Answer[23] = 0x12;
+                memcpy(&Fota_Answer[24],(DataPtr+24),18);
+                Fota_Answer[42] =  bcc_chk_fota(Fota_Answer,41);
+                tcpipConnectionSend(connectId,Fota_Answer,42,0,0,0);
+                break;
+            }
+            case 0x02:
+            {
+                Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
+                Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
+                Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
+                memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
+                Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
+                if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
+                {
+                    BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
+                    BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
+                    Fota_Answer[3] = 0x01;
+                }
+                else//数据校验失败
+                {
+                    Fota_Answer[3] = 0x02;
+                }
+                memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
+                Fota_Answer[21] = TCP_ENCPT_DISABLE;
+                Fota_Answer[22] = 0x00;
+                Fota_Answer[23] = 0x12;
+                memcpy(&Fota_Answer[24],(DataPtr+24),18);
+                Fota_Answer[42] =  bcc_chk_fota(Fota_Answer,41);
+                tcpipConnectionSend(connectId,Fota_Answer,42,0,0,0);
+                break;
+            }
+            case 0x03:
+            {
+                break;
+            }
+            default:
+                break;
+        }
+    }
+    else
+    {
+        Fota_S.Fota_update_flag = false;
+
+    }
+
+}
+static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
+{
+    UINT8 bcc_chk_return = 0x00;
+    UINT8 count = 0;
+    while (count<length)
+    {
+        bcc_chk_return^=data[count];
+        count++;
+    }
+    return  bcc_chk_return;
+}
+static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
+{  
+    UINT8 reg_crc=0x00;
+    while(length--)
+    { 
+        reg_crc ^= *data++;
+    }
+    return reg_crc;
+}

+ 11 - 13
src/MainTask.c

@@ -28,13 +28,13 @@
 #include "timers.h"
 #include "app.h"
 #include "MainTask.h"
-
+#include "TcpTask.h"
 
 CHAR BattSN[BATT_SN_LEN] = "GY606000000000001";//SN仍在测试
 //全局变量
 UINT32 Timer_count;//每100ms加1
 volatile bool Sleep_flag = false;//睡眠标志位
-
+extern UINT32 TcpService;
 AppNVMDataType AppNVMData;
 
 //主线程堆栈声明区
@@ -107,7 +107,7 @@ static void MainTask(void* arg)
 
 	
     montior_timer = xTimerCreate("montior_timer", 100 / portTICK_RATE_MS, pdTRUE, NULL, montior_timer_callback);
-    work_timer = xTimerCreate("work_timer", AppNVMData.wakeupWorkTime*60*1000 / portTICK_RATE_MS, pdFALSE, NULL, work_timer_callback);
+    work_timer = xTimerCreate("work_timer", AppNVMData.wakeupWorkTime*60*1000 / portTICK_RATE_MS, pdTRUE, NULL, work_timer_callback);
 
 	
 	
@@ -128,11 +128,18 @@ static void MainTask(void* arg)
             case PROCESS_STATE_WORK:
             {
                 osDelay(1000);
-                if(FALSE)
+                if(Work_timer_end)
                 {
+                    #ifdef USING_PRINTF
+                    printf("Main Task,Work_timer_end!\n");
+                    #endif
                     xTimerStop(montior_timer, 0);
                     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
                 }
+                if(TcpService!=0x00)
+                {
+                    xTimerReset(work_timer,0);
+                }
                 break;
             }
             case PROCESS_STATE_SLEEP:
@@ -175,9 +182,6 @@ static void MainTask(void* arg)
             default:
             {
                 PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
-                #ifdef USING_PRINTF
-                    printf("default!\n");
-                #endif
                 break;
             }
         }
@@ -211,12 +215,6 @@ void MainTaskDeInit(void *arg)
 //定时器回调函数区
 void montior_timer_callback(TimerHandle_t xTimer)
 {
-    #ifdef USING_PRINTF
-        if (Timer_count%50==0)
-        {
-            printf("Main Task,Batt_Cell_Num:%d,%d!\n",BATT_CELL_VOL_NUM,Timer_count);
-        }
-    #endif
     Timer_count++;
     if(Timer_count>100000*100)
     {

+ 32 - 13
src/TcpTask.c

@@ -32,7 +32,7 @@
 #include "GpsTask.h"
 #include "Fota.h"
 //全局变量输出区
-
+UINT32 TcpService = 0;
 //全局变量输入区
 extern UINT32 Timer_count;
 extern volatile bool Sleep_flag;
@@ -41,6 +41,7 @@ extern osMutexId_t UartMutex;
 extern osMutexId_t GpsMutex;
 extern UINT8 GpsData[16];
 extern AppNVMDataType AppNVMData;
+
 //局部变量申请
 AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
 static StaticTask_t             gProcess_Tcp_Task_t;
@@ -52,7 +53,6 @@ static process_TCP 		    gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
 static   eventCallbackMessage_t *queueItem = NULL;
 static UINT8                    gImsi[16] = {0};
 static UINT32                   gCellID = 0;
-static UINT32                   TcpService = 0;
 static QueueHandle_t TcpRecvHandle = NULL;
 //Tcp线程堆栈申请区
 
@@ -138,6 +138,7 @@ static void TcpTask(void* arg)
                     break;
                 case QMSG_ID_SOCK_RECVPKG:
                     TcpDataInfoRecvHandle();
+                    osDelay(100);
                     TcpService=0x00;
                     break;
                 case QMSG_ID_NW_IP_SUSPEND:
@@ -205,10 +206,7 @@ void TcpTaskDeInit(void *arg)
     osThreadTerminate(TcpTaskId);
     TcpTaskId = NULL;
 }
-
-
-
-/*------------------------------函数区域-----------------------*/
+/*------------------------------函数区域----------------------------------------------------------------------------------------------*/
 static void TcpDataInfoAssembleSend()
 {   static UINT32 send_counter = 0;
     BattMsgtoTcpType BattToTcpInfo;
@@ -385,6 +383,8 @@ static void TcpDataInfoAssembleSend()
         VerMsgToTcpInfo.VerInfo.APPVersion[1] = (APPSWVERSION>>16)& 0xFF;
         VerMsgToTcpInfo.VerInfo.APPVersion[2] = (APPSWVERSION>>8)& 0xFF;
         VerMsgToTcpInfo.VerInfo.APPVersion[3] = (APPSWVERSION)& 0xFF;
+        VerMsgToTcpInfo.VerInfo.BmsType = 'C';
+        VerMsgToTcpInfo.VerInfo.BmsInfo = 'A';
         VerMsgToTcpInfo.CRC = bcc_chk((UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo)-1 );
         tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0);
     }
@@ -406,22 +406,41 @@ static void TcpDataInfoRecvHandle()
     TcpService = 0x01;//应答标志
     osMessageQueueGet(TcpRecvHandle,&TcpRecvData,0,0);
     osDelay(100);
+    UINT8 Tcp_Cmd;
+    UINT8 Control_Cmd;
     UINT8 *Ptr;
     if(TcpRecvData != PNULL)
     {
-        #ifdef USING_PRINTF
-            printf("TcpRecvData is vacant\n");
-        #endif
         #ifdef USING_PRINTF
         Ptr = TcpRecvData->data;
         printf("TcpDataInfoRecvHandle socket connection %u receive length %u data:", TcpRecvData->connectionId, TcpRecvData->length);
         for(int i = 0;i<TcpRecvData->length;i++)
-            printf("%x  ",*(Ptr+i));
+            printf("%d-%x  ",i,*(Ptr+i));
         #endif
-        free(TcpRecvData); 
-    }
-    
+        if((*(Ptr+0)==TCP_START_SYM1)&&(*(Ptr+1)==TCP_START_SYM2))//服务器起始信息
+        {
+            Tcp_Cmd = *(Ptr+2);//命令标志
+            switch (Tcp_Cmd)
+            {
+            case TCP_QUERY_SYM:
+                break;
+            case TCP_SETCMD_SYM:
+                break;
+            case TCP_CONCMD_SYM:
+            {
+                if(*(Ptr+30)==0x01)//远程升级命令
+                {
+                    Fota_Func(Ptr,socContext.id);
+                }
+                break;
+            }
+            default:
+                break;
+            }
+        }
 
+    free(TcpRecvData); 
+    }
 }
 //TCP发送校验函数
 UINT8 bcc_chk(UINT8* data, UINT8 length)