فهرست منبع

利用Tcp更新完成,但是存在睡眠问题。

CHENJIE-PC\QiXiang_CHENJIE 4 سال پیش
والد
کامیت
a12e953a52
3فایلهای تغییر یافته به همراه120 افزوده شده و 27 حذف شده
  1. 1 1
      inc/hal_module_adapter.h
  2. 117 22
      src/app.c
  3. 2 4
      src/bsp_custom.c

+ 1 - 1
inc/hal_module_adapter.h

@@ -7,7 +7,7 @@ extern "C" {
 #include "commontypedef.h"
 #include "sensor.h"
 #include "can.h"
-#define USING_PRINTF 
+#define USING_PRINTF
 #define GPS_DATA_RECV_BUFFER_SIZE     						(1024)
 
 #define I2C_INT1_REQ_BITMAP    								 (0x1)

+ 117 - 22
src/app.c

@@ -44,9 +44,9 @@ static StaticTask_t             gProcess_Tcp_Task_t;
 static UINT8                  gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
 static volatile UINT32          Event;
 static UINT8                    gImsi[16] = {0};
-static INT32                    sockfd = -1;
 static UINT32                   gCellID = 0;
 static UINT8  Tcp_serviece;
+static UINT8 Tcp_Send_anwser_buffer[10];
 static QueueHandle_t            psEventQueueHandle;//队列句柄
 #define APP_EVENT_QUEUE_SIZE    (10)//队列大小
 static AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
@@ -55,6 +55,9 @@ Fota_Type Fota_S={0,0,0,0,0,0,0,0};
 //函数声明
 void Tcp_Recv_info(uint8_t* p);
 void Tcp_Recv_fota(uint8_t* p);
+uint8_t Fota_crc_chk(uint8_t* data,uint8_t length);
+void Tcp_Send_info();
+void Tcp_Send_anwser();
 //睡眠进出函数
 static void appBeforeHib(void *pdata, slpManLpState state)
 {
@@ -124,6 +127,7 @@ static void Main_Task(void* arg)
     UINT16 Can_index = 0;
     UINT16 Uart_index = 0;
     UINT16 Tcp_index  = 0;
+    uint8_t Tcp_index_temp = 5;
     uint32_t sleep_index = 0;
     uint32_t sleep_time_flag;
     int32_t inParam = 0xAABBCCDD;
@@ -196,12 +200,24 @@ static void Main_Task(void* arg)
                 Uart_index++;
                 Tcp_index++;
                 sleep_index++;
-                if(Tcp_index>5*100 && socContext.id>=0)
+                if(Tcp_index>Tcp_index_temp*100)
                 {
+                    #ifdef USING_PRINTF
+                        printf("[%d]work flag V2\r\n",__LINE__);
+                    #endif
                     sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0);
                     Tcp_index = 0;
                 }
-                if (sleep_index >=sleep_time_flag*100)
+                if (Tcp_serviece==1)
+                {
+                    sleep_index = 0;
+                    Tcp_index_temp = 2;
+                }
+                else
+                {
+                    Tcp_index_temp = 5;
+                }
+                if (sleep_index >=sleep_time_flag*100 &&Tcp_serviece==0)
                 {
                     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
                     sleep_index = 0;
@@ -380,7 +396,7 @@ void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent)
                     Ptr=rcvInd->data;
                     printf("socketAppConnectionCallBack socket connection %u receive length %u data:", rcvInd->connectionId, rcvInd->length);
                     for(int i = 0;i<rcvInd->length;i++)
-                        printf("%x",*(Ptr+i));
+                        printf("%x  ",*(Ptr+i));
                 #endif
                 Tcp_Recv_info(Ptr);//Tcp 接收
                 ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_5, P_INFO, 2, "socketAppConnectionCallBack socket connection %u receive length %u data", rcvInd->connectionId, rcvInd->length);
@@ -435,29 +451,86 @@ void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent)
 }
 void Tcp_Recv_fota(uint8_t* P)
 {
+    Fota_S.Fota_Flash_Addres = 0x2A0000;
+    uint8_t Fota_Crc = 00;
+    uint8_t send_temp[10];
+    uint8_t temp[1];
     #ifdef USING_PRINTF
         printf("fota data handle\n");
     #endif
     if(*(P+3)==0x01)
     {
-        Fota_S.Fota_All_Data_Len = *(P+5)<<24|*(P+6)<<16|*(P+7)<<8|*(P+8);
+        Fota_S.Fota_All_Data_Len = *(P+4)<<24|*(P+5)<<16|*(P+6)<<8|*(P+7);
+        send_temp[0]=0x82;
+        send_temp[1]=0x01;
+        memcpy(Tcp_Send_anwser_buffer,send_temp,2);
     }
     else if(*(P+3)==0x02)
     {
-        Fota_S.Fota_Current_Addres = *(P+9)<<24|*(P+10)<<16|*(P+11)<<8|*(P+12);
-        Fota_S.Fota_Recv_Data_Len = *(P+13);
-        memcpy(Fota_S.Fota_Recv_Data,*(P+14),*(P+13));
+        Fota_S.Fota_Current_Addres = *(P+8)<<24|*(P+9)<<16|*(P+10)<<8|*(P+11);
+        Fota_S.Fota_Recv_Data_Len = *(P+12);
+        memcpy(Fota_S.Fota_Recv_Data,(P+13),*(P+12));
+        Fota_Crc = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
+        #ifdef USING_PRINTF
+            printf("Crc:%x,%x!,data_len:%d\n",Fota_Crc,*(P+Fota_S.Fota_Recv_Data_Len+13),Fota_S.Fota_Recv_Data_Len);
+        #endif
+        if(Fota_Crc==*(P+Fota_S.Fota_Recv_Data_Len+13))
+        {
+            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);
+            send_temp[0]=0x82;
+            send_temp[1]=0x01;
+            memcpy(Tcp_Send_anwser_buffer,send_temp,2);
+        }
+    }
+    else if(*(P+3)==0x03)
+    {
+        send_temp[0]=0x82;
+        send_temp[1]=0x01;
+        memcpy(Tcp_Send_anwser_buffer,send_temp,2);
+        #ifdef USING_PRINTF
+            printf("Done!\n");
+        #endif
+        close(socContext.id);
+        appSetCFUN(0);
+        #ifdef USING_PRINTF
+            for(int i=0;i<Fota_S.Fota_All_Data_Len;i++)
+            {
+                BSP_QSPI_Read_Safe(temp,Fota_S.Fota_Flash_Addres+i,1);
+                printf("%X ",temp[0]);
+            }
+        #endif
+        osDelay(5000);
+        EC_SystemReset();
     }
+    #ifdef USING_PRINTF
+        printf("updating\n");
+    #endif
 }
 void Tcp_Recv_info(uint8_t* P)
 {
-    switch(*(P))
+    uint8_t send_temp[10],switch_flag;
+    int len;
+    #ifdef USING_PRINTF
+        printf("P0:0x%x !\n",*(P+0));
+        printf("P1:0x%x !\n",*(P+1));
+        printf("P2:0x%x !\n",*(P+2));
+    #endif
+    switch_flag = *(P+0);
+    #ifdef USING_PRINTF
+        printf("switch_flag:%x !\n",switch_flag);
+    #endif
+    switch(switch_flag)
     {
         case 0x80://查询
             Tcp_serviece=01;
             if(*(P+2)==0x08)
             {
-                tcpipConnectionSend(socContext.id, {08,01,08,00}, 5, 0, 0, 0);
+                send_temp[0] = 0x80;
+                send_temp[1] = 0x01;
+                send_temp[2] = 0x08;
+                send_temp[3] = 0x02;
+                memcpy(Tcp_Send_anwser_buffer,send_temp,4);
             }
             break;
         case 0x82://控制
@@ -483,21 +556,38 @@ uint8_t Fota_crc_chk(uint8_t* data,uint8_t length)
     }
     return reg_crc;
 }
-void Tcp_Send_fota()
+void Tcp_Send_anwser()
 {
-    tcpipConnectionSend(socContext.id, "fota", 5, 0, 0, 0);
+    int len;
+    len = tcpipConnectionSend(socContext.id, Tcp_Send_anwser_buffer, 10, 0, 0, 0);
+    if(len > 0)
+    {
+        #ifdef USING_PRINTF
+            printf("connection %u send anwser len %u success\n", socContext.id, len);
+        #endif
+    }
+    #ifdef USING_PRINTF
+        printf("connection %u send anwser len %u end\n", socContext.id, len);
+    #endif
+    Tcp_serviece=00;
 }
 void Tcp_Send_info()
 {
     static UINT8 send_counter=0;   
     int len;
+    uint8_t send_temp[10];
     if(send_counter%1==0)
     {
-        len = tcpipConnectionSend(socContext.id, {02,00,80}, 4, 0, 0, 0);
+        send_temp[0] = 0x02;
+        send_temp[1] = 0x00;
+        send_temp[2] = 0x80;
+        len = tcpipConnectionSend(socContext.id, send_temp, 3, 0, 0, 0);
     }
     if(send_counter%6==0)
     {
-        len = tcpipConnectionSend(socContext.id, {01 00}, 3, 0, 0, 0);
+        send_temp[0] = 0x01;
+        send_temp[1] = 0x00;
+        len = tcpipConnectionSend(socContext.id, send_temp, 2, 0, 0, 0);
     }
     if(send_counter>=6*10+1)
     {
@@ -507,18 +597,17 @@ void Tcp_Send_info()
     {
         send_counter++;
     }
-
     if(len > 0)
     {
         #ifdef USING_PRINTF
-            printf("connection %u send len %u success", socContext.id, len);
+            printf("connection %u send len %u success\n", socContext.id, len);
         #endif
         ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_7, P_INFO, 2, "connection %u send len %u success", socContext.id, len);  
     }
     else
     {
         #ifdef USING_PRINTF
-            printf("connection %u sent data fail", socContext.id);
+            printf("connection %u sent data fail\n", socContext.id);
         #endif
         ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_8, P_ERROR, 1, "connection %u sent data fail", socContext.id);
     }
@@ -544,7 +633,7 @@ static void Tcp_Task(void* arg)
         if (xQueueReceive(psEventQueueHandle, &queueItem, portMAX_DELAY))
         {
             #ifdef USING_PRINTF
-                printf("Queue receive->0x%x\n", queueItem->messageId);
+                //printf("Queue receive->0x%x\n", queueItem->messageId);
             #endif
             switch(queueItem->messageId)
             {
@@ -576,6 +665,13 @@ static void Tcp_Task(void* arg)
                         {
                             Tcp_Send_info();
                         }
+                        else if(Tcp_serviece==1)
+                        {
+                            Tcp_Send_anwser();
+                        }
+                        #ifdef USING_PRINTF
+                            printf("QMSG_ID_SOCK_SENDPKG Running\n");
+                        #endif
                     }
                     else
                     {
@@ -617,10 +713,9 @@ static void Tcp_Task(void* arg)
                     #ifdef USING_PRINTF
                         printf("socket disconnect\n");
                     #endif
-                    if (sockfd >= 0)
+                    if (socContext.id >= 0)
                     {
-                       close(sockfd);
-                       sockfd = -1;
+                       close(socContext.id);
                     }
                     appSetCFUN(0);
                     free(queueItem);
@@ -640,7 +735,7 @@ void Tcp_Task_Init()
     task_attr.name = "Tcp_Task";
     task_attr.stack_mem = gProcess_Tcp_TaskStack;
     task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
-    task_attr.priority = osPriorityNormal;
+    task_attr.priority = osPriorityBelowNormal7;
     task_attr.cb_mem = &gProcess_Tcp_Task_t;
     task_attr.cb_size = sizeof(StaticTask_t);
     osThreadNew(Tcp_Task, NULL, &task_attr);

+ 2 - 4
src/bsp_custom.c

@@ -14,8 +14,6 @@
 #include "plat_config.h"
 #include "debug_log.h"
 
-//#define USING_PRINTF
-
 #ifdef BL_FILE_LOG
 extern uint8_t lockoutState;
 #endif
@@ -156,8 +154,8 @@ void BSP_CustomInit(void)
 #else
     if(rawFlashPlatConfig && (rawFlashPlatConfig->logControl != 0 ))
     {
-        SetUnilogUart(PORT_USART_0, rawFlashPlatConfig->uartBaudRate, true);
-        uniLogInitStart(UART_0_FOR_UNILOG);
+        SetUnilogUart(PORT_USART_1, rawFlashPlatConfig->uartBaudRate, true);
+        uniLogInitStart(UART_1_FOR_UNILOG);
         ECOMM_STRING(UNILOG_PLA_STRING, build_info, P_SIG, "%s", getBuildInfo());
     }
 #endif