Browse Source

V2集成大版本改动,Uart Gps Tcp测试通过,增加了互斥锁

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
3733c0901e
10 changed files with 959 additions and 253 deletions
  1. 3 1
      ARMCC/Makefile
  2. 12 0
      inc/GpsTask.h
  3. 133 0
      inc/TcpTask.h
  4. 15 2
      inc/UartTask.h
  5. 8 181
      inc/app.h
  6. 190 0
      src/GpsTask.c
  7. 6 10
      src/MainTask.c
  8. 540 0
      src/TcpTask.c
  9. 48 57
      src/UartTask.c
  10. 4 2
      src/app.c

+ 3 - 1
ARMCC/Makefile

@@ -19,6 +19,8 @@ obj-y             += PLAT/project/$(TARGET)/apps/qx_app/src/app.o \
 		     		PLAT/project/$(TARGET)/apps/qx_app/src/bsp_custom.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/hal_module_adapter.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/MainTask.o \
-					PLAT/project/$(TARGET)/apps/qx_app/src/UartTask.o
+					PLAT/project/$(TARGET)/apps/qx_app/src/UartTask.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/TcpTask.o  \
+					PLAT/project/$(TARGET)/apps/qx_app/src/GpsTask.o
 
 include $(TOP)/PLAT/tools/scripts/Makefile.rules

+ 12 - 0
inc/GpsTask.h

@@ -0,0 +1,12 @@
+/****************************************************************************
+ *
+ * Copy right:   Qx.Chen jie
+ * File name:    GpsTask.h
+ * Description:  Gps处理任务
+ * History:      2021-03-07
+ *
+ ****************************************************************************/
+//
+extern UINT8 GpsData[16];
+extern osMutexId_t GpsMutex;
+INT32 GpsTaskInit(void);

+ 133 - 0
inc/TcpTask.h

@@ -0,0 +1,133 @@
+/****************************************************************************
+ *
+ * Copy right:   Qx.Chen Jie
+ * File name:    TcpTask.h
+ * Description:  网络发送接收任务
+ * History:      2021-03-07
+ *
+ ****************************************************************************/
+#define PROC_TCP_TASK_STACK_SIZE           (2048)
+#define QMSG_ID_BASE               (0x160) 
+#define QMSG_ID_NW_IP_READY        (QMSG_ID_BASE)
+#define QMSG_ID_NW_IP_SUSPEND      (QMSG_ID_BASE + 1)
+#define QMSG_ID_NW_IP_NOREACHABLE  (QMSG_ID_BASE + 2)
+#define QMSG_ID_SOCK_SENDPKG       (QMSG_ID_BASE + 4)
+#define QMSG_ID_SOCK_RECVPKG       (QMSG_ID_BASE + 5)
+#define QMSG_ID_SOCK_EXIT       	(QMSG_ID_BASE + 6)
+#define APP_EVENT_QUEUE_SIZE    (10)
+
+// #define QX_TCP_IPADRRES				"120.26.68.165"
+// #define QX_TCP_PORT					14319
+#define QX_TCP_IPADRRES				"47.97.127.222"
+#define QX_TCP_PORT					8712
+
+#define BATT_SN_LEN             17
+#define TCP_START_SYM1			0x23
+#define TCP_START_SYM2			0x23
+#define TCP_CMD_SYM				0x02
+#define TCP_ANS_SYM 			0xFE
+
+
+//encrypt methord
+#define TCP_ENCPT_ENABLE		0x00
+#define TCP_ENCPT_DISABLE		0x01
+
+//message type mark
+#define BATTMSG				0x80
+#define GPSMSG				0x82
+
+typedef struct BattInfoType
+{	
+	UINT8	sendTimeUTC[6];
+	UINT8	msgMark;
+	UINT8	msgCollectionTimeUTC[6];
+	UINT8	signalStrength;
+	UINT8	errClass;
+	UINT8	errCode[2];
+	UINT8	battI[2];
+	UINT8	battLinkVol[2];
+	UINT8	battPackVol[2];
+	UINT8   chrgState;
+	UINT8	battSOC;
+	UINT8	battSOH;
+	UINT8	batCellBalenceState[4];	//uint32 should change to uint8[]: each bit stand for 1 cell, up to 1024
+	UINT8	battCellNum;			//uint8 should change to uint16 (0~65535)  //zhengchao
+	UINT8	battCellU[BATT_CELL_VOL_NUM_2];
+	UINT8	battTempNum;			//uint8 should change to uint16 (0~65535)
+	UINT8	battCellTemp[BATT_TEMP_NUM];
+	UINT8	battWorkState;
+	UINT8	battHeatState;
+	UINT8 	battMosTemp;
+	UINT8 	battEnvTemp;
+}BattInfoType;
+typedef struct BattMsgtoTcpType
+{	
+	UINT8 	startSymbol[2];
+	UINT8	cmdSymbol;
+	UINT8	ansSymbol;
+	UINT8	SN[BATT_SN_LEN];
+	UINT8	encryptMethod;
+	UINT8	dataLength[2];	
+	BattInfoType battInfo;	
+	UINT8	CRC;
+}BattMsgtoTcpType;
+typedef struct _GPSInfoType
+{
+	UINT8	sendTimeUTC[6];
+	UINT8	msgMark;
+	UINT8	msgCollectionTimeUTC[6];
+	UINT8	locateMark;
+	UINT8	satelliteNum;
+	UINT8	direction[2];
+	UINT8	speed[2];
+	UINT8	altitude[2];
+	UINT8	latitude[4];
+	UINT8	longitude[4];
+	
+}GPSInfoType;
+
+typedef struct GPSMsgtoTcpType
+{
+	UINT8 	startSymbol[2];
+	UINT8	cmdSymbol;
+	UINT8	ansSymbol;
+	UINT8	SN[BATT_SN_LEN];
+	UINT8	encryptMethod;
+	UINT8	dataLength[2];	
+	GPSInfoType gpsInfo;	
+	UINT8	CRC;
+}GPSMsgtoTcpType;
+typedef enum
+{
+    APP_SOCKET_CONNECTION_CLOSED,
+    APP_SOCKET_CONNECTION_CONNECTING,
+    APP_SOCKET_CONNECTION_CONNECTED,    
+}AppSocketConnectionStatus;
+
+typedef struct AppSocketConnectionContext_Tag
+{
+    INT32 id;
+    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,
+    PROCESS_TCP_STATE_SEND,
+    PROCESS_TCP_STATE_RECV,
+    PROCESS_TCP_STATE_SLEEP
+}process_TCP;
+void TcpTaskInit(void *arg);
+void TcpTaskDeInit(void *arg);

+ 15 - 2
inc/UartTask.h

@@ -6,11 +6,23 @@
  * History:      2021-03-05
  *
  ****************************************************************************/
+typedef struct _UartRedMsg
+{
+	bool UartFlag;
+    UINT8 Header[3];
+    UINT8 data[120];
+    UINT16 len;
+}UartReadMsgType;
+//全局变量输出区
+extern UartReadMsgType UartReadMsg;
+extern osMutexId_t UartMutex;
+//
 
 #define PROC_UART_TASK_STACK_SIZE           (1024)
 #define BMS_ADDRESS_CODE 0x01
 #define UART_READ_CODE 0x03
 #define UART_WRITE_CODE 0x10
+
 typedef struct Uart_Read_Msg_Type
 {
     uint8_t Bms_Address; 
@@ -22,6 +34,7 @@ typedef struct Uart_Read_Msg_Type
     uint8_t CRC_H;
     uint8_t CRC_L;
 }Uart_Read_Msg_Type;
+
 typedef struct Uart_Write_Msg_Type
 {
     uint8_t Bms_Address; 
@@ -44,8 +57,8 @@ typedef struct Uart_Write_Answer_Msg_Type
 typedef enum
 {
     PROCESS_UART_STATE_IDLE = 0,
-    PROCESS_UART_STATE_CHECK,
-    PROCESS_UART_STATE_WORK,
+    PROCESS_UART_STATE_READ,
+    PROCESS_UART_STATE_WRITE,
     PROCESS_UART_STATE_SLEEP
 }process_Uart;
 void UartTaskInit(void* arg);

+ 8 - 181
inc/app.h

@@ -6,6 +6,7 @@
  * History:      Rev1.0   2018-07-12
  *
  ****************************************************************************/
+#include "commontypedef.h"
 #ifndef  APP_H
 #define  APP_H
 #ifdef __cplusplus
@@ -15,163 +16,25 @@ extern "C" {
 #define BAT4830
 #ifdef  BAT4830
     #define BATT_CELL_VOL_NUM  (14)
-    #define BATT_TEMP_NUM  (5)
+    #define BATT_TEMP_NUM  (3)
 #else
     #define BATT_CELL_VOL_NUM  (17)
-    #define BATT_TEMP_NUM  (7)
+    #define BATT_TEMP_NUM  (5)
 #endif
 #define BATT_CELL_VOL_NUM_2 (BATT_CELL_VOL_NUM*2)
 #define BATT_SN_LEN           17
 #define	SWVERSION		0xA21
 #define HWVERSION		0xB22
-
-//--------------------------------------------------------------------------------
-#define QMSG_ID_BASE               (0x160) 
-#define QMSG_ID_NW_IP_READY        (QMSG_ID_BASE)
-#define QMSG_ID_NW_IP_SUSPEND      (QMSG_ID_BASE + 1)
-#define QMSG_ID_NW_IP_NOREACHABLE  (QMSG_ID_BASE + 2)
-#define QMSG_ID_SOCK_SENDPKG       (QMSG_ID_BASE + 4)
-#define QMSG_ID_SOCK_RECVPKG       (QMSG_ID_BASE + 5)
-#define QMSG_ID_SOCK_EXIT       (QMSG_ID_BASE + 6)
-#define QX_TCP_IPADRRES				"47.97.127.222"
-#define QX_TCP_PORT					"8712"
 #define APP_CHARGING_WORK_TIME      30
 #define APP_WAKEUP_WORK_TIME		1
 #define APP_SLEEP_TIME				5
-
-
-
-#define TCP_START_SYM1			0x23
-#define TCP_START_SYM2			0x23
-#define TCP_CMD_SYM				0x02
-#define TCP_ANS_SYM 			0xFE
-
-//encrypt methord
-#define TCP_ENCPT_ENABLE		0x00
-#define TCP_ENCPT_DISABLE		0x01
-
-
-//message type mark
-#define BATTMSG				0x80
-#define GPSMSG				0x82
-
-
-//tcp Send Msg Case period
-#define SENDMSG5s			01
-#define SENDMSG10s			02
-#define SENDMSG20s			04
-#define SENDMSG30s			06
-#define SENDMSG60s			12
-#define SENDMSG180s			36
-#define SENDMSG300s			60
-#define SENDMSG600s			120
-#define SENDMSGPRDMAX		200
-
-
-extern UINT8 IH_appChargEndWorkTime;
-extern UINT8 IH_appWakeupWorkTime;
-extern UINT8 IH_appSleepTime;
-extern UINT8 IH_isBattLocked;
-
-
-extern UINT8 OH_appChargEndWorkTime;
-extern UINT8 OH_appWakeupWorkTime;
-extern UINT8 OH_appSleepTime;
-extern UINT8 OH_isBattLocked;
-extern UINT8 BattSN[BATT_SN_LEN];
-extern CHAR  TcpServerIpV4Address[16];
-extern CHAR  TcpServerPort[5];
-
-typedef enum
-{
-    PROCESS_CAN_STATE_IDLE = 0,
-    PROCESS_CAN_STATE_WORK,
-    PROCESS_CAN_STATE_SLEEP
-}process_CAN;
-
-typedef enum
-{
-    PROCESS_TCP_STATE_IDLE = 0,
-    PROCESS_TCP_STATE_LINK,
-    PROCESS_TCP_STATE_WORK,
-    PROCESS_TCP_STATE_SLEEP
-}process_TCP;
-
-typedef struct _UartRedMsg
+//--------------------------------------------------------------------------------
+typedef struct _UartAnswerMsg
 {
-    void * dataPtr;
+	bool UartFlag;
+    UINT8 dataPtr[8];
     UINT16 len;
-}UartReadMsg;
-
-
-
-typedef struct BattInfoType
-{	
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8	msgCollectionTimeUTC[6];
-	UINT8	signalStrength;
-	UINT8	errClass;
-	UINT8	errCode[2];
-	UINT8	battI[2];
-	UINT8	battLinkVol[2];
-	UINT8	battPackVol[2];
-	UINT8   chrgState;
-	UINT8	battSOC;
-	UINT8	battSOH;
-	UINT8	batCellBalenceState[4];	//uint32 should change to uint8[]: each bit stand for 1 cell, up to 1024
-	UINT8	battCellNum[2];			//uint8 should change to uint16 (0~65535)  //zhengchao
-	UINT8	battCellU[BATT_CELL_VOL_NUM_2];
-	UINT8	battTempNum[2];			//uint8 should change to uint16 (0~65535)
-	UINT8	battCellTemp[BATT_TEMP_NUM];
-	UINT8	battWorkState;
-	UINT8	battHeatState;
-}BattInfoType;
-
-
-
-typedef struct BattMsgtoTcpType
-{	
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];	
-	BattInfoType battInfo;	
-	UINT8	CRC;
-}BattMsgtoTcpType;
-
-
-typedef struct GPSInfoType
-{
-	UINT8	sendTimeUTC[6];
-	UINT8	msgMark;
-	UINT8	msgCollectionTimeUTC[6];
-	UINT8	locateMark;
-	UINT8	satelliteNum;
-	UINT8	direction[2];
-	UINT8	speed[2];
-	UINT8	altitude[2];
-	UINT8	latitude[4];
-	UINT8	longitude[4];
-	
-}GPSInfoType;
-
-
-typedef struct GPSMsgtoTcpType
-{
-	UINT8 	startSymbol[2];
-	UINT8	cmdSymbol;
-	UINT8	ansSymbol;
-	UINT8	SN[BATT_SN_LEN];
-	UINT8	encryptMethod;
-	UINT8	dataLength[2];	
-	GPSInfoType gpsInfo;	
-	UINT8	CRC;
-}GPSMsgtoTcpType;
-
-
+}UartAnswerMsgType;
 
 typedef enum
 {
@@ -182,42 +45,6 @@ typedef enum
     APP_IDLE_STATE,
     APP_WAIT_STATE
 } appRunningState_t;
-typedef enum
-{
-    APP_SOCKET_CONNECTION_CLOSED,
-    APP_SOCKET_CONNECTION_CONNECTING,
-    APP_SOCKET_CONNECTION_CONNECTED,    
-}AppSocketConnectionStatus;
-
-typedef struct AppSocketConnectionContext_Tag
-{
-    INT32 id;
-    INT32 status;
-}AppSocketConnectionContext;
-
-typedef struct Fota_Type
-{
-    bool Fota_update_flag ;
-    bool Fota_update_error ;
-    uint32_t Fota_All_Data_Len ;
-    uint32_t Fota_Current_Addres ;
-    uint8_t Fota_Recv_Data_Len ;
-    uint8_t Fota_Recv_Data[100] ;
-    uint32_t Fota_Flash_Addres;
-    uint8_t Fota_CRC ;
-
-}Fota_Type;
-typedef struct  _GPS_INFO
-{
-	uint8_t timedata[6];
-	uint8_t status;
-	uint8_t satellite_num;
-	uint16_t direction;
-	uint16_t speed;
-	uint16_t altitude;
-	uint32_t latitude;
-	uint32_t longitude;
-}GPS_INFO;
 //uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len);
 
 #ifdef __cplusplus

+ 190 - 0
src/GpsTask.c

@@ -0,0 +1,190 @@
+/****************************************************************************
+ *
+ * Copy right:   Qx.Chen jie
+ * File name:    GpsTask.c
+ * Description:  Gps处理任务
+ * History:      2021-03-07
+ *
+ ****************************************************************************/
+#include "bsp.h"
+#include "bsp_custom.h"
+#include "osasys.h"
+#include "ostask.h"
+#include "queue.h"
+#include "ps_event_callback.h"
+#include "app.h"
+#include "cmisim.h"
+#include "cmimm.h"
+#include "cmips.h"
+#include "sockets.h"
+#include "psifevent.h"
+#include "ps_lib_api.h"
+#include "lwip/netdb.h"
+//#include <cis_def.h>
+#include "debug_log.h"
+#include "slpman_ec616.h"
+#include "plat_config.h"
+#include "ec_tcpip_api.h"
+#include "app.h"
+#include "MainTask.h"
+#include "GpsTask.h"
+#include "TcpTask.h"
+//全局变量区输入
+extern volatile bool Sleep_flag; 
+//全局变量区输出
+UINT8 GpsData[16];
+osMutexId_t GpsMutex = NULL;
+//线程定义区
+#define GPS_TASK_STACK_SIZE   								 (512)
+static QueueHandle_t norGpsHandle = NULL;
+static osThreadId_t gpsTaskHandle = NULL;
+static StaticTask_t gpsTask = NULL;
+static UINT8 gpsTaskStack[GPS_TASK_STACK_SIZE];
+
+//函数声明区
+void strdel(char * str,char c);
+UINT32 location_handle(char *in1);
+
+static void GpsTask(void* arg)
+{
+    gpsReqMsg msg;
+	char *p=NULL;
+	const char *delim = "\n";
+	char *databuffer[14];
+    UINT32 speedtemp;
+    UINT32 latitude;
+	UINT32 longitude;
+    UINT16 direction;
+    posGGAServiceStart(norGpsHandle);
+    posGGAReset();
+    if(GpsMutex == NULL)
+    {
+        GpsMutex = osMutexNew(NULL);
+    }
+    while(1)
+    {
+        char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
+        osMessageQueueGet(norGpsHandle, &msg, 0, osWaitForever);
+        if (msg.dataPtr)
+        {	
+            osStatus_t result = osMutexAcquire(GpsMutex, osWaitForever);//Gps数据锁	
+            p = strtok(msg.dataPtr,delim);//将信息进行分割
+            // #ifdef USING_PRINTF	
+            //     printf("\nP msgptr data:%s\r\n",p);
+            // #endif
+            int i=0;
+            p = strtok(p,",");//只取第1行的信息RMC
+            //p = strtok(temp,",");//模拟测试
+            if (strcmp(p,"$GNRMC")==0)
+            {			
+                while (p)
+                {	
+                    databuffer[i]=p;
+                    p = strtok(NULL,",");
+                    i++;;
+                }
+                if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
+                {
+                    GpsData[0] = 0x00;
+                }
+                else if (strcmp(databuffer[2],"A")==0)
+                {
+                    GpsData[0] = 0x01;//有效,东经,北纬写定
+                    GpsData[1] = 03;//卫星数目写入1
+                    strdel(databuffer[3],'.');
+                    strdel(databuffer[5],'.');
+                    strdel(databuffer[7],'.');
+                    speedtemp = atol(databuffer[7])*1852/1e5;//节换算单位,1节=1.852km每小时
+                    GpsData[4] = (speedtemp>>8)&0xFF;
+                    GpsData[5] = speedtemp&0xFF; 
+                    latitude =location_handle(databuffer[3]);
+                    GpsData[8] = latitude>> 24;
+                    GpsData[9] = latitude>> 16;
+                    GpsData[10] = latitude>> 8;
+                    GpsData[11] = latitude;
+                    longitude = location_handle(databuffer[5]);
+                    GpsData[12] = longitude>>24;
+                    GpsData[13] = longitude>>16;
+                    GpsData[14] = longitude>>8;
+                    GpsData[15] = longitude;
+                    GpsData[6] = 0x03;
+                    GpsData[7] = 0xE8;
+                    if(speedtemp>=50)//大于5km/h才输出方位
+                    {
+                        direction = atol(databuffer[8]);
+                        GpsData[2] = direction>>8;
+                        GpsData[3] = direction;
+                    }
+                    else
+                    {
+                        GpsData[2] = 0xff;
+                        GpsData[3] = 0xfe;
+                    }
+                }
+            }
+        osMutexRelease(GpsMutex);
+        }
+        if(msg.dataPtr)
+            free(msg.dataPtr);
+        msg.dataPtr=NULL;
+        if (Sleep_flag)
+		{
+            posGGAServiceStop();
+			osThreadExit();
+			break;
+		}
+    }
+}
+INT32 GpsTaskInit(void)
+{
+
+	if(norGpsHandle == NULL)
+	{
+		norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
+		if(norGpsHandle == NULL)
+			return 1;
+	}
+	if(gpsTaskHandle == NULL)
+	{
+		osThreadAttr_t task_attr;
+		memset(&task_attr , 0 , sizeof(task_attr));
+		task_attr.name = "GPS";
+		task_attr.priority = osPriorityBelowNormal6;
+		task_attr.cb_mem = &gpsTask;
+		task_attr.cb_size = sizeof(StaticTask_t);
+		task_attr.stack_mem = gpsTaskStack;
+		task_attr.stack_size =GPS_TASK_STACK_SIZE;
+		memset(& gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
+		gpsTaskHandle = osThreadNew(GpsTask , NULL,&task_attr);
+		if(gpsTaskHandle == NULL)
+			return 1;
+	}
+	
+	return 0;
+}
+/*----------------------------------------*/
+// 字符串删除函数
+void strdel(char * str,char c)
+{
+	char *p = str;
+	while(*str)
+	{
+		if(*str!=c)
+			*p++ = *str;
+		str++;
+	}
+	*p = '\0';
+}
+UINT32 location_handle(char *in1)
+{
+	UINT32 location_temp;
+	UINT32 location_degree;
+	UINT32 location_min;
+	location_temp = atol(in1);
+	location_degree = location_temp/(1e7);
+	location_degree = location_degree*(1e6);
+	location_min = location_temp-location_degree*10;
+	location_min = location_min/6;
+	location_temp = location_degree+location_min;
+	return location_temp;
+}

+ 6 - 10
src/MainTask.c

@@ -19,7 +19,7 @@
 #include "psifevent.h"
 #include "ps_lib_api.h"
 #include "lwip/netdb.h"
-#include <cis_def.h>
+//#include <cis_def.h>
 #include "debug_log.h"
 #include "slpman_ec616.h"
 #include "plat_config.h"
@@ -61,11 +61,6 @@ void work_timer_callback(TimerHandle_t xTimer);
 //主线程任务区
 static void MainTask(void* arg)
 {
-
-    appSetEDRXSettingSync(0,5,1800000);
-    appSetPSMSettingSync(0,3*60*60,10);
-    appSetCFUN(0);
-
     int32_t inParam = 0xAABBCCDD;
     slpManSetPmuSleepMode(true,SLP_HIB_STATE,false);
     slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler);
@@ -84,8 +79,8 @@ static void MainTask(void* arg)
     
     static UINT8 work_time;
     static UINT8 Sleep_Time;
-    Sleep_Time = 1;//睡眠时长 min
-    work_time = 1;//工作时长 min
+    Sleep_Time = APP_SLEEP_TIME;//睡眠时长 min
+    work_time = APP_WAKEUP_WORK_TIME;//工作时长 min
 
     montior_timer = xTimerCreate("montior_timer", 100 / portTICK_RATE_MS, pdTRUE, NULL, montior_timer_callback);
     work_timer = xTimerCreate("work_timer", work_time*60*1000 / portTICK_RATE_MS, pdFALSE, NULL, work_timer_callback);
@@ -106,8 +101,9 @@ static void MainTask(void* arg)
             case PROCESS_STATE_WORK:
             {
                 osDelay(1000);
-                if(Work_timer_end)
+                if(FALSE)
                 {
+                    xTimerStop(montior_timer, 0);
                     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
                 }
                 break;
@@ -115,7 +111,7 @@ static void MainTask(void* arg)
             case PROCESS_STATE_SLEEP:
             {
                 Sleep_flag = true;
-                xTimerStop(montior_timer, 0);
+                osDelay(5000);
                 xTimerStop(work_timer, 0);
                 slpManSlpState_t State;
                 uint8_t cnt;

+ 540 - 0
src/TcpTask.c

@@ -0,0 +1,540 @@
+/****************************************************************************
+ *
+ * Copy right:   Qx.Chen jie
+ * File name:    TcpTask.c
+ * Description:  网络发送接收任务
+ * History:      2021-03-07
+ *
+ ****************************************************************************/
+#include "bsp.h"
+#include "bsp_custom.h"
+#include "osasys.h"
+#include "ostask.h"
+#include "queue.h"
+#include "ps_event_callback.h"
+#include "app.h"
+#include "cmisim.h"
+#include "cmimm.h"
+#include "cmips.h"
+#include "sockets.h"
+#include "psifevent.h"
+#include "ps_lib_api.h"
+#include "lwip/netdb.h"
+//#include <cis_def.h>
+#include "debug_log.h"
+#include "slpman_ec616.h"
+#include "plat_config.h"
+#include "ec_tcpip_api.h"
+#include "MainTask.h"
+#include "TcpTask.h"
+#include "app.h"
+#include "UartTask.h"
+#include "GpsTask.h"
+//全局变量输出区
+
+//全局变量输入区
+extern UINT32 Timer_count;
+extern volatile bool Sleep_flag;
+extern UartReadMsgType UartReadMsg;
+extern osMutexId_t UartMutex;
+extern osMutexId_t GpsMutex;
+extern UINT8 GpsData[16];
+CHAR BattSN[BATT_SN_LEN] = "GYTEST00000000003";//SN仍在测试
+//局部变量申请
+static AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
+static StaticTask_t             gProcess_Tcp_Task_t;
+static UINT8                  gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
+static QueueHandle_t            psEventQueueHandle;//状态转换队列句柄
+static osThreadId_t           TcpTaskId = NULL;
+static process_TCP 		    gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
+#define PROC_TCP_STATE_SWITCH(a)  (gProcess_Tcp_Task = a)
+static   eventCallbackMessage_t *queueItem = NULL;
+static UINT8                    gImsi[16] = {0};
+static UINT32                   gCellID = 0;
+//Tcp线程堆栈申请区
+
+//函数声明区
+static void TcpWorkStatus(void *arg);
+static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait);
+static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen);
+void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent);
+void TcpDataInfoAssembleSend();
+UINT8 bcc_chk(UINT8* data, UINT8 length);
+//线程任务区
+static void TcpTask(void* arg)
+{
+    INT32 connectionId = -1;
+    appSetEDRXSettingSync(0,5,1800000);
+    appSetPSMSettingSync(1,3*60*60,10);
+    appSetCFUN(1);
+    psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*));
+    PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
+    if (psEventQueueHandle == NULL)
+    {
+        return;
+    }
+    while (true)
+    {
+        osDelay(100);
+        if(Sleep_flag)
+        {
+            sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
+            sendQueueMsg(QMSG_ID_SOCK_EXIT, 0);
+        }
+        else if(Timer_count%50==0)
+        {
+            sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0);
+        }
+        if (xQueueReceive(psEventQueueHandle, &queueItem, 0))
+        {
+            switch(queueItem->messageId)
+            {
+                case QMSG_ID_NW_IP_READY:
+                    if(connectionId < 0)
+                    {
+                        connectionId = tcpipConnectionCreate(TCPIP_CONNECTION_PROTOCOL_TCP, PNULL, 0, QX_TCP_IPADRRES, QX_TCP_PORT, socketAppConnectionCallBack);
+                    }
+                    if(connectionId >= 0)
+                    {
+                        socContext.id = connectionId;
+                        socContext.status = APP_SOCKET_CONNECTION_CONNECTED;
+                        #ifdef USING_PRINTF
+                            printf("create connection %u success", socContext.id);
+                        #endif
+                        ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_2, P_INFO, 1, "create connection %u success", socContext.id);
+                    }
+                    else
+                    {
+                        sendQueueMsg(QMSG_ID_NW_IP_NOREACHABLE, 0);
+                        #ifdef USING_PRINTF
+                            printf("create connection fail\n");
+                        #endif
+                        ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_3, P_ERROR, 0, "create connection fail");
+                    }
+                    break;
+                case QMSG_ID_NW_IP_NOREACHABLE:
+                    break;
+                case QMSG_ID_SOCK_SENDPKG:
+                    if (socContext.id >= 0 && socContext.status == APP_SOCKET_CONNECTION_CONNECTED)
+                    {
+                        TcpDataInfoAssembleSend();
+                    }
+                    else
+                    {
+                        sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
+                        #ifdef USING_PRINTF
+                            printf("connection %u or status invalid", socContext.id, socContext.status);
+                        #endif
+                        ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_9, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status); 
+                    }
+                    break;
+                case QMSG_ID_NW_IP_SUSPEND:
+                    if (socContext.id >= 0 && socContext.status != APP_SOCKET_CONNECTION_CLOSED)
+                    {
+                        if(tcpipConnectionClose(socContext.id) < 0)
+                        {
+                            socContext.id = -1;
+                            socContext.status = APP_SOCKET_CONNECTION_CLOSED;
+                            #ifdef USING_PRINTF
+                                printf("close connection %u success", socContext.id);
+                            #endif
+                            ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_4, P_INFO, 1, "close connection %u success", socContext.id);                            
+                        }
+                        else
+                        {
+                            #ifdef USING_PRINTF
+                                printf("close connection %u fail", socContext.id);
+                            #endif
+                            ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_5, P_ERROR, 1, "close connection %u fail", socContext.id);
+                        }
+                    }
+                    else
+                    {
+                        #ifdef USING_PRINTF
+                            printf("connection %u or status invalid", socContext.id, socContext.status);
+                        #endif
+                        ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_6, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status); 
+                    }                    
+                    break;
+                case QMSG_ID_SOCK_EXIT:
+                    #ifdef USING_PRINTF
+                        printf("QMSG_ID_SOCK_EXIT queueItem->messageId:%x\n",queueItem->messageId);
+                    #endif
+                    #ifdef USING_PRINTF
+                        printf("socket exit\n");
+                    #endif
+                    appSetCFUN(0);
+                    free(queueItem);
+                    osDelay(1000);
+                    TcpTaskDeInit(arg);
+                    break;
+            }
+        free(queueItem);
+        }
+    }//while 循环
+}
+//Tcp线程初始化
+void TcpTaskInit(void *arg)
+{
+    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";
+    task_attr.stack_mem = gProcess_Tcp_TaskStack;
+    task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
+    task_attr.priority = osPriorityBelowNormal7;
+    task_attr.cb_mem = &gProcess_Tcp_Task_t;
+    task_attr.cb_size = sizeof(StaticTask_t);
+    TcpTaskId = osThreadNew(TcpTask, NULL, &task_attr);
+}
+void TcpTaskDeInit(void *arg)
+{
+    osThreadTerminate(TcpTaskId);
+    TcpTaskId = NULL;
+}
+
+
+
+/*------------------------------函数区域-----------------------*/
+static void TcpDataInfoAssembleSend()
+{   static UINT8 send_counter = 0;
+    BattMsgtoTcpType BattToTcpInfo;
+    GPSMsgtoTcpType GpsToTcpInfo;
+    OsaUtcTimeTValue TimeStracture;
+    UINT8 csq=0;
+    INT8 snr=0;
+    INT8 rsnr=0;
+    INT16 Batt_current;
+    UINT16 BattU;
+    UINT8 temp=0;
+    UINT8 TEMP_NUM=0;
+    UINT16 DataLen;
+    if(send_counter%1==0)
+    {
+        osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
+        appGetSystemTimeUtcSync(&TimeStracture);
+        appGetSignalInfoSync(&csq,&snr,&rsnr);
+        DataLen= (UINT16)sizeof(BattToTcpInfo.battInfo);
+        BattToTcpInfo.startSymbol[0] = TCP_START_SYM1;
+	    BattToTcpInfo.startSymbol[1] = TCP_START_SYM2;
+	    BattToTcpInfo.cmdSymbol = TCP_CMD_SYM;
+	    BattToTcpInfo.ansSymbol = TCP_ANS_SYM;
+        memcpy(BattToTcpInfo.SN, BattSN,BATT_SN_LEN);
+        BattToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
+	    BattToTcpInfo.dataLength[0] = (DataLen>>8) & 0xFF;
+	    BattToTcpInfo.dataLength[1] = DataLen & 0xFF;
+        BattToTcpInfo.battInfo.sendTimeUTC[0] = ((((TimeStracture.UTCtimer1) >> 16) & 0xFFFF) - 0x07D0) & 0xFF; 	//year
+        BattToTcpInfo.battInfo.sendTimeUTC[1] = ((TimeStracture.UTCtimer1) >> 8 ) & 0xFF;							//month
+        BattToTcpInfo.battInfo.sendTimeUTC[2] = (TimeStracture.UTCtimer1) & 0xFF;									//day
+        BattToTcpInfo.battInfo.sendTimeUTC[3] = ((TimeStracture.UTCtimer2) >> 24 ) & 0xFF; 						    //hour
+        BattToTcpInfo.battInfo.sendTimeUTC[4] = ((TimeStracture.UTCtimer2) >> 16 ) & 0xFF;						    //mins
+        BattToTcpInfo.battInfo.sendTimeUTC[5] = ((TimeStracture.UTCtimer2) >> 8 ) & 0xFF;							//sec
+        BattToTcpInfo.battInfo.msgMark = BATTMSG;
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[0] = ((((TimeStracture.UTCtimer1) >> 16) & 0xFFFF) - 0x07D0) & 0xFF; 	//year
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[1] = ((TimeStracture.UTCtimer1) >> 8 ) & 0xFF;							//month
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[2] = (TimeStracture.UTCtimer1) & 0xFF;									//day
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[3] = ((TimeStracture.UTCtimer2) >> 24 ) & 0xFF; 						    //hour
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[4] = ((TimeStracture.UTCtimer2) >> 16 ) & 0xFF;						    //mins
+        BattToTcpInfo.battInfo.msgCollectionTimeUTC[5] = ((TimeStracture.UTCtimer2) >> 8 ) & 0xFF;							//sec
+        BattToTcpInfo.battInfo.signalStrength = csq ;
+        //故障等级故障代码未定义
+        TEMP_NUM = UartReadMsg.data[3];//TEMP_NUM为温度总检测数量
+        BattToTcpInfo.battInfo.errClass = 0x00;
+        BattToTcpInfo.battInfo.errCode[0] = 0x00;
+	    BattToTcpInfo.battInfo.errCode[1] = 0x00;
+        Batt_current = (UartReadMsg.data[(0x02+BATT_CELL_VOL_NUM)*2]<<8)|(UartReadMsg.data[(0x02+BATT_CELL_VOL_NUM)*2+1]);
+        if (Batt_current>0x8000)
+        {
+            Batt_current = Batt_current|0x7fff;
+            Batt_current = Batt_current/10;
+            Batt_current = 0x2710 - Batt_current;
+            Batt_current = Batt_current;
+        }
+        else
+        {
+            Batt_current = Batt_current/10;
+            Batt_current = Batt_current+0x2710;
+            Batt_current = Batt_current;
+        }
+        BattToTcpInfo.battInfo.battI[0] = Batt_current>>8;
+	    BattToTcpInfo.battInfo.battI[1] = Batt_current & 0xFF;
+        BattU =( (UartReadMsg.data[(0x18+BATT_CELL_VOL_NUM+TEMP_NUM)*2])<<8|(UartReadMsg.data[(0x18+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1]))/10;
+        //电池内外电压保持一致
+        BattToTcpInfo.battInfo.battLinkVol[0] = BattU >> 8;
+	    BattToTcpInfo.battInfo.battLinkVol[1] = BattU & 0xFF;
+	    BattToTcpInfo.battInfo.battPackVol[0] = BattU >> 8;
+	    BattToTcpInfo.battInfo.battPackVol[1] = BattU & 0xFF;
+        temp = ((UartReadMsg.data[(0x09+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1])>>1)&0x03;
+        BattToTcpInfo.battInfo.chrgState = ((temp&0x01)<<01)|(temp>>0x01);
+        BattToTcpInfo.battInfo.battSOC = UartReadMsg.data[(0x0B+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1];
+        BattToTcpInfo.battInfo.battSOH = UartReadMsg.data[(0x0C+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1];
+        memcpy(BattToTcpInfo.battInfo.batCellBalenceState,&UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+TEMP_NUM)*2],4);
+        BattToTcpInfo.battInfo.battCellNum= BATT_CELL_VOL_NUM ;
+        memcpy(BattToTcpInfo.battInfo.battCellU,&UartReadMsg.data[0x04],BATT_CELL_VOL_NUM_2);
+        BattToTcpInfo.battInfo.battTempNum = BATT_TEMP_NUM;
+        for(int i=0; i<BATT_TEMP_NUM; i++)
+	    {
+		    BattToTcpInfo.battInfo.battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];
+	    }
+        BattToTcpInfo.battInfo.battWorkState =UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03;//电池状态,0表示静置,1表示放电,2表示充电
+        BattToTcpInfo.battInfo.battHeatState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+TEMP_NUM)*2+1];
+        BattToTcpInfo.battInfo.battMosTemp = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM)*2+1];
+        BattToTcpInfo.battInfo.battEnvTemp = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+BATT_TEMP_NUM+1)*2+1];
+        BattToTcpInfo.CRC = bcc_chk((UINT8 *)&BattToTcpInfo, sizeof(BattMsgtoTcpType)-1 );
+        osMutexRelease(UartMutex);
+        tcpipConnectionSend(socContext.id, (UINT8 *)&BattToTcpInfo, sizeof(BattToTcpInfo), 0, 0, 0);
+
+    }
+    if(send_counter%6==0)
+    {
+        osStatus_t result = osMutexAcquire(GpsMutex, osWaitForever);
+        appGetSystemTimeUtcSync(&TimeStracture);
+        DataLen= (UINT16)sizeof(GpsToTcpInfo.gpsInfo);
+        GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1;
+	    GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2;
+	    GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM;
+	    GpsToTcpInfo.ansSymbol = TCP_ANS_SYM;
+        memcpy(GpsToTcpInfo.SN, BattSN,BATT_SN_LEN);
+        GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
+	    GpsToTcpInfo.dataLength[0] = (DataLen>>8) & 0xFF;
+	    GpsToTcpInfo.dataLength[1] = DataLen & 0xFF;
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = ((((TimeStracture.UTCtimer1) >> 16) & 0xFFFF) - 0x07D0) & 0xFF; 	//year
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = ((TimeStracture.UTCtimer1) >> 8 ) & 0xFF;							//month
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = (TimeStracture.UTCtimer1) & 0xFF;									//day
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = ((TimeStracture.UTCtimer2) >> 24 ) & 0xFF; 						    //hour
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = ((TimeStracture.UTCtimer2) >> 16 ) & 0xFF;						    //mins
+        GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = ((TimeStracture.UTCtimer2) >> 8 ) & 0xFF;							//sec
+        GpsToTcpInfo.gpsInfo.msgMark = GPSMSG;
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = ((((TimeStracture.UTCtimer1) >> 16) & 0xFFFF) - 0x07D0) & 0xFF; 	//year
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = ((TimeStracture.UTCtimer1) >> 8 ) & 0xFF;							//month
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = (TimeStracture.UTCtimer1) & 0xFF;									//day
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = ((TimeStracture.UTCtimer2) >> 24 ) & 0xFF; 						    //hour
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = ((TimeStracture.UTCtimer2) >> 16 ) & 0xFF;						    //mins
+        GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = ((TimeStracture.UTCtimer2) >> 8 ) & 0xFF;
+        memcpy((UINT8 *)&GpsToTcpInfo.gpsInfo.locateMark, GpsData,sizeof(GpsData));
+        GpsToTcpInfo.CRC = bcc_chk((UINT8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType)-1 );
+        osMutexRelease(GpsMutex);
+        if(GpsToTcpInfo.gpsInfo.locateMark==0x01)
+        {
+            tcpipConnectionSend(socContext.id, (UINT8 *)&GpsToTcpInfo, sizeof(GpsToTcpInfo), 0, 0, 0);
+        }
+    }   
+    if(send_counter>=6*10+1)
+    {
+        send_counter=1;
+    }
+    else
+    {
+        send_counter++;
+    }
+
+    
+}
+//TCP发送校验函数
+UINT8 bcc_chk(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;
+}
+//Tcp 状态转换函数
+static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
+{
+    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");
+        }
+    }
+}
+//Tcp状态注册函数
+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
+            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;
+}
+//Tcpz连接状态回调函数
+void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent)
+{
+    switch(connectionEventType)
+    {
+        case TCPIP_CONNECTION_STATUS_EVENT:
+        {
+            TcpipConnectionStatusInd *statusInd;
+            statusInd = (TcpipConnectionStatusInd *)bodyEvent;
+            if(statusInd != PNULL)
+            {
+                if(statusInd->status == TCPIP_CONNECTION_STATUS_CLOSED)
+                {
+                    #ifdef USING_PRINTF
+                        printf("socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause);
+                    #endif
+                    ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_1, P_ERROR, 2, "socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause);
+                    if(statusInd->connectionId == socContext.id)
+                    {
+                        socContext.id = -1;
+                        socContext.status = APP_SOCKET_CONNECTION_CLOSED;
+                    }
+                }
+                else if(statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTING)
+                {
+                    #ifdef USING_PRINTF
+                        printf("socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId);
+                    #endif
+                    ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_2, P_INFO, 1, "socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId);
+                    if(statusInd->connectionId == socContext.id)
+                    {
+                        socContext.status = APP_SOCKET_CONNECTION_CONNECTING;
+                    }
+                }
+                else if(statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTED)
+                {
+                    #ifdef USING_PRINTF
+                        printf("socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId);
+                    #endif
+                    ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_3, P_ERROR, 1, "socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId);
+                    if(statusInd->connectionId == socContext.id)
+                    {
+                        socContext.status = APP_SOCKET_CONNECTION_CONNECTED;
+                    }
+                }
+            }
+            else
+            {
+                #ifdef USING_PRINTF
+                    printf("socketAppConnectionCallBack invalid connection status event");
+                #endif
+                ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_4, P_ERROR, 0, "socketAppConnectionCallBack invalid connection status event");
+            }
+
+            break;
+        }
+        case TCPIP_CONNECTION_RECEIVE_EVENT:
+        {
+            TcpipConnectionRecvDataInd *rcvInd;
+            rcvInd = (TcpipConnectionRecvDataInd *)bodyEvent;
+            if(rcvInd != PNULL)
+            {
+                uint8_t* Ptr;
+                #ifdef USING_PRINTF
+                
+                    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));
+                #endif
+                ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_5, P_INFO, 2, "socketAppConnectionCallBack socket connection %u receive length %u data", rcvInd->connectionId, rcvInd->length);
+            }
+            else
+            {
+                #ifdef USING_PRINTF
+                    printf( "socketAppConnectionCallBack invalid connection rcv event");
+                #endif
+                ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_6, P_ERROR, 0, "socketAppConnectionCallBack invalid connection rcv event");
+            }
+            break;
+        }
+        case TCPIP_CONNECTION_UL_STATUS_EVENT:
+        {
+            TcpipConnectionUlDataStatusInd *ulStatusInd;
+            ulStatusInd = (TcpipConnectionUlDataStatusInd *)bodyEvent;
+            if(ulStatusInd != PNULL)
+            {
+                if(ulStatusInd->status == Tcpip_Connection_UL_DATA_SUCCESS)
+                {
+                    #ifdef USING_PRINTF
+                        printf( "socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence);
+                    #endif
+                    ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_7, P_INFO, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence);
+                }
+                else if(ulStatusInd->status == Tcpip_Connection_UL_DATA_FAIL)
+                {
+                    #ifdef USING_PRINTF
+                        printf("socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence);
+                    #endif
+                    ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_8, P_WARNING, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence);
+                }
+            }
+            else
+            {
+                #ifdef USING_PRINTF
+                    printf("socketAppConnectionCallBack invalid connection ul status event");
+                #endif
+                ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_9, P_ERROR, 0, "socketAppConnectionCallBack invalid connection ul status event");
+            }
+            break;
+        }
+        default:
+            #ifdef USING_PRINTF
+                printf("socketAppConnectionCallBack invalid event type %u", connectionEventType);
+            #endif
+            ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_10, P_ERROR, 1, "socketAppConnectionCallBack invalid event type %u", connectionEventType);
+            break;
+    }
+    
+}

+ 48 - 57
src/UartTask.c

@@ -19,7 +19,7 @@
 #include "psifevent.h"
 #include "ps_lib_api.h"
 #include "lwip/netdb.h"
-#include <cis_def.h>
+//#include <cis_def.h>
 #include "debug_log.h"
 #include "slpman_ec616.h"
 #include "plat_config.h"
@@ -28,11 +28,14 @@
 #include "UartTask.h"
 #include "MainTask.h"
 #include "app.h"
-//全局变量测试区
-UINT8 UDSService=1;
-QueueHandle_t UartReadQueueHandle = NULL;
-UartReadMsg Uart_Recv_Msg_Test;
+//全局变量输入区
+extern UINT32 Timer_count;
+extern volatile bool Sleep_flag; 
+//全局变量输出区
+UartReadMsgType UartReadMsg;
+osMutexId_t UartMutex = NULL;//Uart数据锁
 //
+UINT8 Uart_Write_Flag=0;
 extern ARM_DRIVER_USART Driver_USART1;
 static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
 volatile bool isRecvTimeout = false;
@@ -64,7 +67,10 @@ static void UartTask(void* arg)
     UINT16  Uart_Uds_LEN;
     UINT16  Uart_Recv_LEN;
     Uart_Read_Msg_Type Uart_Read_Msg;
-    UartReadMsg UartQueueMsg;
+    if(UartMutex == NULL)
+    {
+        UartMutex = osMutexNew(NULL);
+    }
     while (1)
     {
         switch (gProcess_Uart_Task)
@@ -72,29 +78,25 @@ static void UartTask(void* arg)
             case PROCESS_UART_STATE_IDLE:
             {
                 osDelay(100);
-                if (Timer_count%50==0)
+                if(Sleep_flag)
+                {
+                    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
+                }
+                else if(Timer_count%50==0)
                 {
                     #ifdef USING_PRINTF
                         printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);
                     #endif
-                    if(UDSService==1)
-                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_CHECK);
+                    if(Uart_Write_Flag==0)
+                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
                     else
-                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
+                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
                 }
                 break;
             }
-            case PROCESS_UART_STATE_CHECK:
+            case PROCESS_UART_STATE_READ:
             {
-                if(UartReadQueueHandle==NULL)
-                {
-                    UartReadQueueHandle = osMessageQueueNew(1,sizeof(UartReadMsg), NULL);
-                    if(UartReadQueueHandle==NULL)
-                    {
-                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
-                        break;
-                    }
-                }
+                osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
                 Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM;//按照协议里面的0x21+X+N的结束地址
                 Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
                 Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
@@ -103,33 +105,24 @@ static void UartTask(void* arg)
                 Uart_Read_Msg.Reg_Num_H = Reg_Num>>8;
                 Uart_Read_Msg.Reg_Num_L = Reg_Num;
                 Uart_Uds_LEN = Reg_Num*2;
-                UartQueueMsg.dataPtr = malloc(Uart_Uds_LEN+1);
-                memset(UartQueueMsg.dataPtr,0x00,Uart_Uds_LEN);
-                Uart_Recv_LEN = Uart_Transmit_func((UINT8 *)&Uart_Read_Msg,UartQueueMsg.dataPtr);
-                UartQueueMsg.len = Uart_Recv_LEN;
-                osMessageQueuePut(UartReadQueueHandle,&UartQueueMsg,0,1000);
-                osDelay(1000);
-                osMessageQueueGet(UartReadQueueHandle,&Uart_Recv_Msg_Test,0,1000);
-                 #ifdef USING_PRINTF
-                    printf("Uart_Recv_Queue-%d: ",Uart_Recv_Msg_Test.len);
-                    for(int i=0;i<Uart_Recv_Msg_Test.len;i++)
-                    {
-                        printf("%x ",*((UINT8 *)Uart_Recv_Msg_Test.dataPtr+i));
-                    }
-                    printf("\n");
-                #endif
-                if(Uart_Recv_Msg_Test.dataPtr)
-                    free(Uart_Recv_Msg_Test.dataPtr);
-                Uart_Recv_Msg_Test.dataPtr = PNULL;
+                memset(UartReadMsg.Header,0x00,Uart_Uds_LEN);
+                Uart_Recv_LEN = Uart_Transmit_func((UINT8 *)&Uart_Read_Msg,UartReadMsg.Header);
+                UartReadMsg.len = Uart_Recv_LEN;
                 PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+                osMutexRelease(UartMutex);
                 break;
             }
-            case PROCESS_UART_STATE_WORK:
+            case PROCESS_UART_STATE_WRITE:
             {
                 break;
             }
             case PROCESS_UART_STATE_SLEEP:
             {
+                USARTdrv->PowerControl(ARM_POWER_LOW);
+                while(TRUE)
+                {
+                    osDelay(60000/portTICK_PERIOD_MS);
+                }
                 break;
             }
 
@@ -202,14 +195,14 @@ UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
     *(Uart_Read_Msg+6) = CRC_chk_buffer;
     *(Uart_Read_Msg+7)  = CRC_chk_buffer>>8;
     USARTdrv->Send(Uart_Read_Msg,8);
-    #ifdef USING_PRINTF
-        printf("Uart_Send_buffer:  ");
-        for(int i=0;i<8;i++)
-        {
-            printf("%x ",*(Uart_Read_Msg+i));
-        }
-        printf("\n");
-    #endif
+    // #ifdef USING_PRINTF
+    //     printf("Uart_Send_buffer:  ");
+    //     for(int i=0;i<8;i++)
+    //     {
+    //         printf("%x ",*(Uart_Read_Msg+i));
+    //     }
+    //     printf("\n");
+    // #endif
     USARTdrv->Receive(Uart_Recv_Buffer,Data_Len);
     while((isRecvTimeout == false) && (isRecvComplete == false))
     {
@@ -234,14 +227,14 @@ UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
         isRecvComplete = false;
         CRC_Rece_buffer =*(Uart_Recv_Buffer+Data_Len-1)<<8|*(Uart_Recv_Buffer+Data_Len-2);
         CRC_chk_buffer = crc_chk(Uart_Recv_Buffer,Data_Len-2);
-        #ifdef USING_PRINTF
-            printf("Uart_Rece_buffer after Crc: ");
-            for(int i=0;i<Data_Len;i++)
-            {
-            printf("%x ",*(Uart_Recv_Buffer+i));
-            }
-            printf("\ncrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
-        #endif
+        // #ifdef USING_PRINTF
+        //     printf("Uart_Rece_buffer after Crc: ");
+        //     for(int i=0;i<Data_Len;i++)
+        //     {
+        //     printf("%x ",*(Uart_Recv_Buffer+i));
+        //     }
+        //     printf("\tcrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
+        // #endif
         if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
         {
             return Data_Len;//此处指针移位出现重启问题
@@ -267,6 +260,4 @@ UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
         isRecvTimeout = false;
         return 0;
     }
-
-
 }

+ 4 - 2
src/app.c

@@ -17,7 +17,7 @@
 #include "psifevent.h"
 #include "ps_lib_api.h"
 #include "lwip/netdb.h"
-#include <cis_def.h>
+//#include <cis_def.h>
 #include "debug_log.h"
 #include "slpman_ec616.h"
 #include "plat_config.h"
@@ -27,7 +27,7 @@
 #include "app.h"
 #include "MainTask.h"
 #include "UartTask.h"
-
+#include "TcpTask.h"
 void appInit(void *arg)
 {
     #ifdef USING_PRINTF	
@@ -35,6 +35,8 @@ void appInit(void *arg)
     #endif
     MainTaskInit(arg);
     UartTaskInit(arg);
+    TcpTaskInit(arg);
+    GpsTaskInit();
 }
 //主函数入口
 void main_entry(void)