Browse Source

【修改】tcp睡眠新增,睡眠进行测试

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
21bb765bb6
1 changed files with 139 additions and 40 deletions
  1. 139 40
      src/app.c

+ 139 - 40
src/app.c

@@ -27,7 +27,7 @@
 #define PROC_UART_TASK_STACK_SIZE           (1024)
 #define PROC_CAN_TASK_STACK_SIZE           (1024)
 #define PROC_TCP_TASK_STACK_SIZE           (1024)
-
+#define APP_EVENT_QUEUE_SIZE    (10)
 uint8_t deepslpTimerID          = DEEPSLP_TIMER_ID7;
 uint8_t MainSlpHandler          = 0xff;
 uint8_t UartSlpHandler          = 0xfe;
@@ -42,6 +42,11 @@ extern ARM_DRIVER_USART Driver_USART1;
 static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
 volatile bool isRecvTimeout = false;
 volatile bool isRecvComplete = false;
+static volatile UINT32          Event;
+static QueueHandle_t            psEventQueueHandle;
+static UINT8                    gImsi[16] = {0};
+static INT32                    sockfd = -1;
+static UINT32                   gCellID = 0;
 
 uint8_t Batt_Cell_Num = 14;//默认数值14、17
 uint8_t Batt_Cell_Num_2 ;//默认数值
@@ -129,7 +134,6 @@ static void appAfterHib(void *pdata, slpManLpState state)
     #endif
 }
 
-
 static void appBeforeSlp1(void *pdata, slpManLpState state)
 {
     #ifdef USING_PRINTF
@@ -279,11 +283,12 @@ static void Main_Task(void* arg)
                     printf("Main_Sleep\n");
                 #endif
                 FaultDisplay(LED_TURN_ON);
-                slpManDeepSlpTimerStart(deepslpTimerID,60000); 
+                slpManDeepSlpTimerStart(deepslpTimerID,300000); 
                 while(1)
                 {
-                    osDelay(5000/portTICK_PERIOD_MS);
+                    osDelay(30000/portTICK_PERIOD_MS);
                 }
+                break;
             }
         }
 
@@ -565,9 +570,9 @@ static void Uart_Task(void* arg)
             case PROCESS_UART_STATE_SLEEP:
             {
                 slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
-                while(true)
+                while(1)
                 {
-                    osDelay(5000/portTICK_PERIOD_MS);
+                    osDelay(30000/portTICK_PERIOD_MS);
                 }
             }
         }
@@ -786,20 +791,87 @@ static void Can_Task(void* arg)
             case PROCESS_CAN_STATE_SLEEP:
             {
                 slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE); 
-                while(true)
+                while(1)
                 {
-                    osDelay(5000/portTICK_PERIOD_MS);
+                    osDelay(30000/portTICK_PERIOD_MS);
                 }
-                break;
             }
         }
     }
 }
-void TcpCallBack(void)//连接建立回调函数
+static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
 {
-    #ifdef USING_PRINTF
-        printf("[%d]TcpCallBack\n",__LINE__);
-    #endif
+    eventCallbackMessage_t *queueMsg = NULL;
+    queueMsg = malloc(sizeof(eventCallbackMessage_t));
+    queueMsg->messageId = msgId;
+    if (psEventQueueHandle)
+    {
+        if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait))
+        {
+            ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error");
+        }
+    }
+}
+static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
+{
+    CmiSimImsiStr *imsi = NULL;
+    CmiPsCeregInd *cereg = NULL;
+
+    UINT8 rssi = 0, index = 0;
+    NmAtiNetifInfo *netif = NULL;
+
+    switch(eventID)
+    {
+        case NB_URC_ID_SIM_READY:
+        {
+            imsi = (CmiSimImsiStr *)param;
+            memcpy(gImsi, imsi->contents, imsi->length);
+            #ifdef USING_PRINTF
+                printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents);
+            #endif
+            break;
+        }
+        case NB_URC_ID_MM_SIGQ:
+        {
+            rssi = *(UINT8 *)param;
+            #ifdef USING_PRINTF
+                printf("RSSI signal=%d\n",rssi);
+            #endif
+            ECOMM_TRACE(UNILOG_PLA_APP, socketRegisterPSUrcCallback_1, P_INFO, 1, "RSSI signal=%d", rssi);
+            break;
+        }
+        case NB_URC_ID_PS_BEARER_ACTED:
+        {
+            #ifdef USING_PRINTF
+                printf("Default bearer activated\n");
+            #endif
+            break;
+        }
+        case NB_URC_ID_PS_BEARER_DEACTED:
+        {
+            #ifdef USING_PRINTF
+                printf("Default bearer Deactivated\n");
+            #endif
+            break;
+        }
+        case NB_URC_ID_PS_CEREG_CHANGED:
+        {
+            cereg = (CmiPsCeregInd *)param;
+            gCellID = cereg->celId;
+            #ifdef USING_PRINTF
+                printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac);
+            #endif
+            break;
+        }
+        case NB_URC_ID_PS_NETINFO:
+        {
+            netif = (NmAtiNetifInfo *)param;
+            if (netif->netStatus == NM_NETIF_ACTIVATED)
+                sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
+            break;
+        }
+    }
+    return 0;
 }
 uint8_t bcc_chk(uint8_t* data, uint8_t length)//发送bcc校验函数
 {
@@ -866,9 +938,14 @@ static void Tcp_Task(void* arg)
 {
     CHAR SN[] = "GYTEST00000000003";//SN应写到osfile里面
     CHAR   serverip[] = "47.97.127.222";
-    UINT16 serverport = 8712;
-    int TcpConnectID = 1;
-    int TcpSendID = -1;
+    CHAR  serverport[] = "8712";
+    struct addrinfo hints, *server_res;
+    int TcpsendID = -1;
+    int TcpconnectID = -1;
+    memset( &hints, 0, sizeof( hints ) );
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_protocol = IPPROTO_TCP;
+    psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*));
     int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定tcp发送的最大数值,以电池数据为上线
     slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler);
     slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE);
@@ -876,6 +953,7 @@ static void Tcp_Task(void* arg)
     uint8_t Tcp_Index = 0;
     uint8_t* TcpSendBuffer=NULL;
     volatile bool Tcp_Flag=false;
+    eventCallbackMessage_t *queueItem = NULL;
     while(true)
     {
         switch(gProcess_Tcp_Task)
@@ -887,12 +965,46 @@ static void Tcp_Task(void* arg)
             }
             case PROCESS_TCP_STATE_LINK:
             {
-                while(TcpConnectID<0)//建立连接
+                if(TcpconnectID<0)
+                {
+                    if (xQueueReceive(psEventQueueHandle, &queueItem, portMAX_DELAY))
+                    {
+                        switch(queueItem->messageId)
+                        {
+                            case QMSG_ID_NW_IP_READY:
+                            {
+                                if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
+                                {
+                                    #ifdef USING_PRINTF
+                                        printf("TCP connect unresolved dns\n");
+                                    #endif
+                                }
+                                sockfd = socket(AF_INET, SOCK_STREAM, 0);
+                                TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
+                                if(TcpconnectID<0)
+                                {
+                                    #ifdef USING_PRINTF
+                                        printf("Connect failed!%d\n",TcpconnectID);
+                                    #endif
+                                    close(sockfd);
+                                    PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
+                                    break;
+                                }
+                                else
+                                {
+                                    sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0);
+                                    PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
+                                }
+                                break;
+                            }
+                        }
+                        free(queueItem);
+                    }
+                }
+                else 
                 {
-                    TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
-                    osDelay(100);
+                    PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
                 }
-                PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
                 break;
             }
             case PROCESS_TCP_STATE_WORK:
@@ -905,15 +1017,12 @@ static void Tcp_Task(void* arg)
                     {
                         case 0://发送登录信息
                         {
-                            #ifdef USING_PRINTF
-                                printf("[%d]case 0\n",__LINE__);
-                            #endif
                             Tcp_Index=1;
                             break;
                         }
                         case 1://发送电池数据
                         {
-                            memset(TcpSendBuffer,0x00,92);
+                            memset(TcpSendBuffer,0x00,NB_send_len);
                             *(TcpSendBuffer+0) = 0x23;
                             *(TcpSendBuffer+1) = 0x23;
                             *(TcpSendBuffer+2) = 0x02;
@@ -933,10 +1042,10 @@ static void Tcp_Task(void* arg)
                                 }
                                 printf("\n ");
                             #endif
-                            TcpSendID  = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
+                            TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
                             //发送失败暂时不写
                             #ifdef USING_PRINTF
-                                printf("ConnectID:%d,TcpSend:%d,data length:%d,Data:  ",TcpConnectID,TcpSendID,NB_send_len);
+                                printf("ConnectID:%d,TcpSend:%d,data length:%d,Data:  ",sockfd,TcpsendID,NB_send_len);
                                 for (int i = 0; i < NB_send_len; i++)
                                 {
                                     printf("%x ",*(TcpSendBuffer+i));
@@ -944,24 +1053,15 @@ static void Tcp_Task(void* arg)
                                 printf("\n ");
                             #endif
                             Tcp_Index=2;
-                            #ifdef USING_PRINTF
-                                printf("[%d]case 1\n",__LINE__);
-                            #endif
                             break;
                         }
                         case 2:
                         {
-                            #ifdef USING_PRINTF
-                                printf("[%d]case 2\n",__LINE__);
-                            #endif
                             Tcp_Index=3;
                             break;
                         }
                         default:
                         {
-                            #ifdef USING_PRINTF
-                                printf("[%d]case default\n",__LINE__);
-                            #endif
                             PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
                             Tcp_Index = 0;
                             Tcp_Flag = true;
@@ -974,13 +1074,11 @@ static void Tcp_Task(void* arg)
             case PROCESS_TCP_STATE_SLEEP:
             {
                 slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE); 
-                tcpipConnectionClose(TcpConnectID); 
-                TcpConnectID = -1;
-                while(true)
+                closesocket(sockfd);
+                while(1)
                 {
-                    osDelay(5000/portTICK_PERIOD_MS);
+                    osDelay(30000/portTICK_PERIOD_MS);
                 }
-                break;
             }
         }
     }
@@ -1043,6 +1141,7 @@ void Can_Task_Init()
 void Tcp_Task_Init()
 {
     osThreadAttr_t task_attr;
+    registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
     memset(&task_attr,0,sizeof(task_attr));
     memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
     task_attr.name = "Tcp_Task";