|
@@ -41,51 +41,29 @@ typedef enum
|
|
|
static process_Main gProcess_Main_Task = PROCESS_STATE_IDLE;
|
|
|
#define PROC_MAIN_STATE_SWITCH(a) (gProcess_Main_Task = a)
|
|
|
volatile bool Sleep_Flag = false;//主线程睡眠标志位
|
|
|
-//Uart线程定义
|
|
|
-#define PROC_UART_TASK_STACK_SIZE (1024)
|
|
|
-static StaticTask_t gProcess_Uart_Task_t;
|
|
|
-static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
|
|
|
|
|
|
-#define Uart_Send_LEN (8)
|
|
|
-#define Uart_Rece_LEN (50)
|
|
|
-#define battbuffer_len (110)
|
|
|
-#define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE
|
|
|
-extern ARM_DRIVER_USART Driver_USART1;
|
|
|
-static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
|
|
|
-volatile bool isRecvTimeout = false;
|
|
|
-volatile bool isRecvComplete = false;
|
|
|
+//Tcp线程定义
|
|
|
+#define PROC_TCP_TASK_STACK_SIZE (1024)
|
|
|
+static StaticTask_t gProcess_Tcp_Task_t;
|
|
|
+static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
|
|
|
+#define APP_EVENT_QUEUE_SIZE (10)
|
|
|
+volatile uint8_t Tcp_statuts = 0;
|
|
|
typedef enum
|
|
|
{
|
|
|
- PROCESS_UART_STATE_IDLE = 0,
|
|
|
- PROCESS_UART_STATE_CHECK,
|
|
|
- PROCESS_UART_STATE_WORK,
|
|
|
- PROCESS_UART_STATE_SLEEP
|
|
|
-}process_Uart;
|
|
|
-static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
|
|
|
-#define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
|
|
|
-volatile bool Uart_Sleep_State = false;
|
|
|
-uint8_t UartSlpHandler = 0xfe;
|
|
|
+ PROCESS_TCP_STATE_IDLE = 0,
|
|
|
+ PROCESS_TCP_STATE_LINK,
|
|
|
+ PROCESS_TCP_STATE_WORK,
|
|
|
+ PROCESS_TCP_STATE_SLEEP
|
|
|
+}process_TCP;
|
|
|
+static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
|
|
|
+#define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
|
|
|
+
|
|
|
//Fota线程定义
|
|
|
#define PROC_FOTA_TASK_STACK_SIZE (1024)
|
|
|
static StaticTask_t gProcess_Fota_Task_t;
|
|
|
static UINT8 gProcess_Fota_TaskStack[PROC_FOTA_TASK_STACK_SIZE];
|
|
|
Fota_Type Fota_Struct;
|
|
|
-//CAN线程定义
|
|
|
-
|
|
|
-//Tcp线程定义
|
|
|
-
|
|
|
-//GPS线程定义
|
|
|
-#define PROC_GPS_TASK_STACK_SIZE (1024)
|
|
|
-static QueueHandle_t gpsMsgQueue = NULL;
|
|
|
-static QueueHandle_t norGpsHandle = NULL;
|
|
|
-static osThreadId_t gpsTaskHandle = NULL;
|
|
|
-static UINT8 gProcess_Gps_TaskStack[PROC_GPS_TASK_STACK_SIZE];
|
|
|
-static StaticTask_t gProcess_Gps_Task_t = NULL;
|
|
|
-GPS_INFO Gps_Data;
|
|
|
|
|
|
-//函数声明区
|
|
|
-void Uart_func(void);
|
|
|
-void USART_callback(uint32_t event);
|
|
|
//睡眠进出函数
|
|
|
static void appBeforeHib(void *pdata, slpManLpState state)
|
|
|
{
|
|
@@ -209,9 +187,6 @@ static void Main_Task(void* arg)
|
|
|
Uart_time = 0;
|
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
if(Fota_Struct.Fota_update)
|
|
|
{
|
|
|
sleep_count = 0;
|
|
@@ -273,602 +248,230 @@ static void Main_Task(void* arg)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-//Uart校验程序
|
|
|
-unsigned int crc_chk(uint8_t* data, uint8_t length)
|
|
|
-{
|
|
|
- int j;
|
|
|
- uint16_t reg_crc=0xFFFF;
|
|
|
- while(length--)
|
|
|
- {
|
|
|
- reg_crc ^= *data++;
|
|
|
- for(j=0;j<8;j++)
|
|
|
- {
|
|
|
- if(reg_crc & 0x01)
|
|
|
- {
|
|
|
- reg_crc=(reg_crc>>1) ^ 0xA001;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- reg_crc=reg_crc >>1;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return reg_crc;
|
|
|
-}
|
|
|
-//Uart发送接收函数
|
|
|
-uint8_t* Uart_Receive_send_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len)
|
|
|
+//NB事件回调函数
|
|
|
+static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
|
|
|
{
|
|
|
- uint16_t CRC_Rece_buffer;
|
|
|
- uint16_t CRC_chk_buffer;
|
|
|
- uint8_t Uart_Send_buffer[8];
|
|
|
- Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
|
|
|
- Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
|
|
|
- Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
|
|
|
- Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
|
|
|
- Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
|
|
|
- Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
|
|
|
- CRC_chk_buffer = crc_chk(Uart_Send_buffer,6);
|
|
|
- Uart_Send_buffer[6] = CRC_chk_buffer;
|
|
|
- Uart_Send_buffer[7] = CRC_chk_buffer>>8;
|
|
|
- uint32_t timeout = 0x00000000;
|
|
|
- USARTdrv->Send(Uart_Send_buffer,8);
|
|
|
- USARTdrv->Receive(Uart_Rece_buffer,Data_Len);
|
|
|
- while((isRecvTimeout == false) && (isRecvComplete == false))
|
|
|
+ CmiSimImsiStr *imsi = NULL;
|
|
|
+ CmiPsCeregInd *cereg = NULL;
|
|
|
+
|
|
|
+ UINT8 rssi = 0, index = 0;
|
|
|
+ NmAtiNetifInfo *netif = NULL;
|
|
|
+
|
|
|
+ switch(eventID)
|
|
|
{
|
|
|
- timeout++;
|
|
|
- osDelay(100);
|
|
|
- if (timeout>=10)
|
|
|
+ case NB_URC_ID_SIM_READY:
|
|
|
{
|
|
|
- timeout =0;
|
|
|
- isRecvTimeout = true;
|
|
|
+ imsi = (CmiSimImsiStr *)param;
|
|
|
+ memcpy(gImsi, imsi->contents, imsi->length);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents);
|
|
|
+ #endif
|
|
|
break;
|
|
|
}
|
|
|
- }
|
|
|
- if (isRecvComplete == true)
|
|
|
- {
|
|
|
- isRecvComplete = false;
|
|
|
- CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2);
|
|
|
- CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2);
|
|
|
- if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
|
|
|
+ case NB_URC_ID_MM_SIGQ:
|
|
|
{
|
|
|
- return Uart_Rece_buffer;//此处指针移位出现重启问题
|
|
|
+ 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;
|
|
|
}
|
|
|
- else //接收数据的校验不过
|
|
|
+ case NB_URC_ID_PS_BEARER_ACTED:
|
|
|
{
|
|
|
- USARTdrv->Uninitialize();
|
|
|
- USARTdrv->Initialize(USART_callback);
|
|
|
- USARTdrv->PowerControl(ARM_POWER_FULL);
|
|
|
- USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
|
|
|
- ARM_USART_DATA_BITS_8 |
|
|
|
- ARM_USART_PARITY_NONE |
|
|
|
- ARM_USART_STOP_BITS_1 |
|
|
|
- ARM_USART_FLOW_CONTROL_NONE, 9600);
|
|
|
- memset(Uart_Rece_buffer,0xff,Data_Len);
|
|
|
- return Uart_Rece_buffer;
|
|
|
+ #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);
|
|
|
+ Tcp_statuts = 1;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- isRecvTimeout = false;
|
|
|
- return Uart_Rece_buffer;
|
|
|
- }
|
|
|
+ return 0;
|
|
|
}
|
|
|
-//Uart回调程序
|
|
|
-void USART_callback(uint32_t event)
|
|
|
+static void Tcp_Task(void* arg)
|
|
|
{
|
|
|
- if(event & ARM_USART_EVENT_RX_TIMEOUT)
|
|
|
+ CHAR serverip[] = "120.26.68.165";
|
|
|
+ CHAR serverport[] = "14319";
|
|
|
+ struct addrinfo hints, *server_res;
|
|
|
+ int TcpsendID = -1;
|
|
|
+ memset( &hints, 0, sizeof( hints ) );
|
|
|
+ hints.ai_socktype = SOCK_STREAM;
|
|
|
+ hints.ai_protocol = IPPROTO_TCP;
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
|
|
|
+ static INT32 sockfd = -1;
|
|
|
+ int TcpconnectID = -1;
|
|
|
+ uint8_t* TcpSendBuffer=NULL;
|
|
|
+ uint8_t Reconnect_Num = 0;//重新连接次数
|
|
|
+ appSetEDRXSettingSync(0,5,1800000);
|
|
|
+ appSetPSMSettingSync(1,3*60*60,10);
|
|
|
+ slpManWakeSrc_e Wakeup_source;
|
|
|
+ Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
|
|
|
+ if(Wakeup_source==0)
|
|
|
{
|
|
|
- isRecvTimeout = true;
|
|
|
+ Tcp_statuts = 0;
|
|
|
}
|
|
|
- if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
|
|
|
+ else
|
|
|
{
|
|
|
- isRecvComplete = true;
|
|
|
+ Tcp_statuts = 1;
|
|
|
}
|
|
|
-}
|
|
|
-//Uart读取函数
|
|
|
-void Uart_func(void)
|
|
|
-{
|
|
|
- Uart_Receive_Type Uart_Receive_Msg;
|
|
|
- Uart_Receive_Msg.Bms_Address = 0x01;
|
|
|
- Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
|
|
|
- int Rece_index = 0;
|
|
|
- uint8_t *Uart_Rece_buffer = NULL;
|
|
|
- volatile bool Uart_task = false;
|
|
|
- volatile uint8_t Data_Len;
|
|
|
- Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
|
|
|
- while(!Uart_task)
|
|
|
+ appSetCFUN(1);
|
|
|
+ while(true)
|
|
|
{
|
|
|
- switch (Rece_index)
|
|
|
+ switch(gProcess_Tcp_Task)
|
|
|
{
|
|
|
- case 0://current
|
|
|
+ case PROCESS_TCP_STATE_IDLE:
|
|
|
{
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = 0x01;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 1://cell votage
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x02;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 2://temprature
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 3://batt votage
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = 0x01;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 4://soc,soh
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = 0x04;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 5://均衡
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = 0x02;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- case 6://最高最低温度和加热和充放电MOS控制
|
|
|
- {
|
|
|
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Begin_L = 0x19+Batt_Cell_Num+Batt_Temp_Num;
|
|
|
- Uart_Receive_Msg.Reg_Num_H = 0x00;
|
|
|
- Uart_Receive_Msg.Reg_Num_L = 0x04;
|
|
|
- Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5;
|
|
|
- memset(Uart_Rece_buffer,0x00,Data_Len);
|
|
|
- Uart_Rece_buffer = Uart_Receive_send_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len);
|
|
|
- break;
|
|
|
- }
|
|
|
- default:
|
|
|
- {
|
|
|
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
- Uart_task = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- Rece_index++;
|
|
|
- }
|
|
|
- free(Uart_Rece_buffer);
|
|
|
-}
|
|
|
-//Uart线程
|
|
|
-static void Uart_Task(void* arg)
|
|
|
-{
|
|
|
- USARTdrv->Initialize(USART_callback);
|
|
|
- USARTdrv->PowerControl(ARM_POWER_FULL);
|
|
|
- USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
|
|
|
- ARM_USART_DATA_BITS_8 |
|
|
|
- ARM_USART_PARITY_NONE |
|
|
|
- ARM_USART_STOP_BITS_1 |
|
|
|
- ARM_USART_FLOW_CONTROL_NONE, 9600);
|
|
|
- slpManApplyPlatVoteHandle("UARTSLP",&UartSlpHandler);
|
|
|
- slpManPlatVoteDisableSleep(UartSlpHandler, SLP_SLP2_STATE);
|
|
|
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
- uint16_t temp;
|
|
|
- while (true)
|
|
|
- {
|
|
|
- switch (gProcess_Uart_Task)
|
|
|
- {
|
|
|
- case PROCESS_UART_STATE_IDLE:
|
|
|
- {
|
|
|
- NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
|
|
|
- break;
|
|
|
- }
|
|
|
- case PROCESS_UART_STATE_WORK:
|
|
|
- {
|
|
|
- BSP_QSPI_Read_Safe(&temp,0x318000,2);
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("V0003!,Fota_flag:%c%c\n",temp,temp>>8);
|
|
|
- #endif
|
|
|
- //Uart_func();//读取函数运行
|
|
|
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
+ osDelay(100);
|
|
|
break;
|
|
|
}
|
|
|
- case PROCESS_UART_STATE_SLEEP:
|
|
|
+ case PROCESS_TCP_STATE_LINK:
|
|
|
{
|
|
|
- slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
|
|
|
- Uart_Sleep_State = true;
|
|
|
- USARTdrv->PowerControl(ARM_POWER_LOW);
|
|
|
#ifdef USING_PRINTF
|
|
|
- printf("UART_STATE_SLEEP\n");
|
|
|
+ printf("TCP_statuts:%d\n",Tcp_statuts);
|
|
|
#endif
|
|
|
- while(1)
|
|
|
+ switch(Tcp_statuts)
|
|
|
{
|
|
|
- osDelay(3000/portTICK_PERIOD_MS);
|
|
|
+ case 0://0代表网络为驻网前
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("TCP Not Ready\n");
|
|
|
+ #endif
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 1://1代表网络驻网
|
|
|
+ {
|
|
|
+ if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("TCP connect unresolved dns\n");
|
|
|
+ #endif
|
|
|
+ Tcp_statuts=0;
|
|
|
+ }
|
|
|
+ Tcp_statuts=2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2://2代表尝试建立连接
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("Connecting...!%d\n",TcpconnectID);
|
|
|
+ #endif
|
|
|
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if(sockfd<0)
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("socket create error\n");
|
|
|
+ #endif
|
|
|
+ Tcp_statuts=1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
|
|
|
+ if(TcpconnectID<0 && errno != EINPROGRESS)
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("Connect failed!%d,Reconnect:%d\n",TcpconnectID,Reconnect_Num);
|
|
|
+ #endif
|
|
|
+ close(sockfd);
|
|
|
+ Tcp_statuts=2;
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
|
|
|
+ Reconnect_Num++;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Tcp_statuts=3;
|
|
|
+ Reconnect_Num = 0;
|
|
|
+ }
|
|
|
+ if(Reconnect_Num>=20)//重新连接
|
|
|
+ {
|
|
|
+ appSetCFUN(0);
|
|
|
+ osDelay(100);
|
|
|
+ appSetCFUN(1);
|
|
|
+ PSInitEventCallbackTable();
|
|
|
+ Tcp_statuts=1;
|
|
|
+ Reconnect_Num = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 3://3代表连接建立成功
|
|
|
+ {
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 4://4代表连接断开
|
|
|
+ {
|
|
|
+ Tcp_statuts=2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-uint8_t Fota_crc_chk(uint8_t* data,uint8_t length)
|
|
|
-{
|
|
|
- uint8_t reg_crc=0x00;
|
|
|
- while(length--)
|
|
|
- {
|
|
|
- reg_crc ^= *data++;
|
|
|
- }
|
|
|
- return reg_crc;
|
|
|
-}
|
|
|
-//Fota线程
|
|
|
-static void Fota_Task(void* arg)
|
|
|
-{
|
|
|
- Fota_Struct.Fota_Current_Addres = 0x00000000;
|
|
|
- Fota_Struct.Fota_Recv_Data_Len=100;
|
|
|
- uint8_t Uart_Rece_buffer[110];
|
|
|
- uint8_t temp[1];
|
|
|
- uint8_t Uart_Send_buffer[6] = {0x00,0x00,0x00,0x00,0x64,0x01};
|
|
|
- uint8_t timeout = 0x00;
|
|
|
- uint8_t Fota_Crc;
|
|
|
- uint32_t Recv_Address;
|
|
|
- Fota_Struct.Fota_Flash_Addres = 0x2A0000;
|
|
|
- osDelay(1000);
|
|
|
- USARTdrv->Send(Uart_Send_buffer,6);
|
|
|
- while (true)
|
|
|
- {
|
|
|
- osDelay(500);
|
|
|
- USARTdrv->Receive(Uart_Rece_buffer,110);
|
|
|
- Fota_Struct.Fota_update = FALSE;
|
|
|
- while((isRecvTimeout == false) && (isRecvComplete == false))
|
|
|
- {
|
|
|
- timeout++;
|
|
|
- osDelay(100);
|
|
|
- if (timeout>=50)
|
|
|
- {
|
|
|
- timeout =0;
|
|
|
- isRecvTimeout = true;
|
|
|
break;
|
|
|
}
|
|
|
- }
|
|
|
- if (isRecvComplete == true)
|
|
|
- {
|
|
|
- isRecvComplete == false;
|
|
|
- Fota_Struct.Fota_All_Data_Len = Uart_Rece_buffer[0]<<24|Uart_Rece_buffer[1]<<16|Uart_Rece_buffer[2]<<8|Uart_Rece_buffer[3];
|
|
|
- Fota_Crc = Fota_crc_chk(&Uart_Rece_buffer[9],Fota_Struct.Fota_Recv_Data_Len);
|
|
|
- Recv_Address = Uart_Rece_buffer[4]<<24|Uart_Rece_buffer[5]<<16|Uart_Rece_buffer[6]<<8|Uart_Rece_buffer[7];
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Recv_Address:%x!\t",Recv_Address);
|
|
|
- printf("Fota_Current_Addres:%x!\n",Fota_Struct.Fota_Current_Addres);
|
|
|
- #endif
|
|
|
- if ((Fota_Crc==Uart_Rece_buffer[Fota_Struct.Fota_Recv_Data_Len+9])&&(Fota_Struct.Fota_Current_Addres==Recv_Address))
|
|
|
+ case PROCESS_TCP_STATE_WORK:
|
|
|
{
|
|
|
+ TcpSendBuffer = (uint8_t *)malloc(1);
|
|
|
+ *(TcpSendBuffer+0) = 0x23;
|
|
|
#ifdef USING_PRINTF
|
|
|
- printf("Fota_Crc:%x!\n",Fota_Crc);
|
|
|
- printf("Fota_Current_Addres:%x!\n",Fota_Struct.Fota_Current_Addres);
|
|
|
+ printf("TCP_STATE_WORK!\n");
|
|
|
#endif
|
|
|
- Fota_Struct.Fota_update = TRUE;
|
|
|
- BSP_QSPI_Erase_Safe(Fota_Struct.Fota_Flash_Addres+Fota_Struct.Fota_Current_Addres,Fota_Struct.Fota_Recv_Data_Len);
|
|
|
- BSP_QSPI_Write_Safe(Uart_Rece_buffer+9,Fota_Struct.Fota_Flash_Addres+Fota_Struct.Fota_Current_Addres,Fota_Struct.Fota_Recv_Data_Len);
|
|
|
- }
|
|
|
- Fota_Struct.Fota_Current_Addres = Recv_Address + Fota_Struct.Fota_Recv_Data_Len;
|
|
|
- if (Fota_Struct.Fota_Current_Addres + Fota_Struct.Fota_Recv_Data_Len >Fota_Struct.Fota_All_Data_Len)
|
|
|
- {
|
|
|
- Fota_Struct.Fota_Recv_Data_Len = Fota_Struct.Fota_All_Data_Len - Fota_Struct.Fota_Current_Addres;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Fota_Struct.Fota_Recv_Data_Len = 0x64;
|
|
|
+ TcpsendID = send(sockfd, TcpSendBuffer, 1, 0 );
|
|
|
+ free(TCPSendBuffer);
|
|
|
+ break;
|
|
|
}
|
|
|
- if(Fota_Struct.Fota_Recv_Data_Len==0)
|
|
|
+ case PROCESS_TCP_STATE_SLEEP:
|
|
|
{
|
|
|
+ closesocket(sockfd);
|
|
|
+ //deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数
|
|
|
#ifdef USING_PRINTF
|
|
|
- printf("Done!\n");
|
|
|
+ printf("TCP_STATE_SLEEP\n");
|
|
|
#endif
|
|
|
+ Tcp_Sleep_Flag = TRUE;
|
|
|
appSetCFUN(0);
|
|
|
- #ifdef USING_PRINTF
|
|
|
- for(int i=0;i<Fota_Struct.Fota_All_Data_Len;i++)
|
|
|
- {
|
|
|
- BSP_QSPI_Read_Safe(temp,Fota_Struct.Fota_Flash_Addres+i,1);
|
|
|
- printf("%X ",temp[0]);
|
|
|
- }
|
|
|
- #endif
|
|
|
- osDelay(1000);
|
|
|
- EC_SystemReset();
|
|
|
-
|
|
|
- }
|
|
|
- Uart_Send_buffer[0] = Fota_Struct.Fota_Current_Addres>>24;
|
|
|
- Uart_Send_buffer[1] = Fota_Struct.Fota_Current_Addres>>16;
|
|
|
- Uart_Send_buffer[2] = Fota_Struct.Fota_Current_Addres>>8;
|
|
|
- Uart_Send_buffer[3] = Fota_Struct.Fota_Current_Addres;
|
|
|
- Uart_Send_buffer[4] = Fota_Struct.Fota_Recv_Data_Len;
|
|
|
- USARTdrv->Send(Uart_Send_buffer,6);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- USARTdrv->Send(Uart_Send_buffer,6);
|
|
|
- isRecvTimeout == false;
|
|
|
- memset(Uart_Rece_buffer,0,110);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-// 字符串删除函数
|
|
|
-void strdel(char * str,char c)
|
|
|
-{
|
|
|
- char *p = str;
|
|
|
- while(*str)
|
|
|
- {
|
|
|
- if(*str!=c)
|
|
|
- *p++ = *str;
|
|
|
- str++;
|
|
|
- }
|
|
|
- *p = '\0';
|
|
|
-}
|
|
|
-//位置信息处理
|
|
|
-uint32_t location_handle(char *in1)
|
|
|
-{
|
|
|
- uint32_t location_temp;
|
|
|
- uint32_t location_degree;
|
|
|
- uint32_t 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;
|
|
|
-}
|
|
|
-//Gps线程
|
|
|
-static void Gps_Task(void* arg)
|
|
|
-{
|
|
|
- gpsReqMsg msg;
|
|
|
- GPS_INFO Gps_buffer;
|
|
|
- Gps_buffer.latitude = 0xffffffff;
|
|
|
- Gps_buffer.longitude = 0xffffffff;
|
|
|
- char *p=NULL;
|
|
|
- const char *delim = "\n";
|
|
|
- char *databuffer[14];
|
|
|
- int data_index;
|
|
|
- osStatus_t Queue_flag;
|
|
|
- /**
|
|
|
- *databuffer内容格式:
|
|
|
- * 定位标识,时间,位置有效标识,纬度,纬度指示,经度,经度指示,地面速率,地面航向,日期
|
|
|
- **/
|
|
|
- //posGGAServiceStart(norGpsHandle);
|
|
|
- while(1)
|
|
|
- {
|
|
|
- osDelay(1000);
|
|
|
- //Queue_flag = osMessageQueueGet(norGpsHandle, &msg, 0, 2000);
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Queue_flag [%d]\r\n",Queue_flag);
|
|
|
- #endif
|
|
|
- if(Queue_flag==0)
|
|
|
- {
|
|
|
- /*
|
|
|
- handle event
|
|
|
- */
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("GpsProcess [%d] %s\r\n",msg.len,msg.dataPtr);
|
|
|
- #endif
|
|
|
- if(msg.dataPtr)
|
|
|
- free(msg.dataPtr);
|
|
|
- msg.dataPtr=NULL;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**模拟测试结果解析正确
|
|
|
- char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("temp:%s\n ",temp);
|
|
|
- #endif
|
|
|
- p = strtok(temp,",");//模拟测试
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("p:%s\r\n",p);
|
|
|
- #endif
|
|
|
- if (strcmp(p,"$GNRMC")==0)
|
|
|
- {
|
|
|
- data_index = 0;
|
|
|
- while (p)
|
|
|
- {
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("%s",p);
|
|
|
- #endif
|
|
|
- databuffer[data_index]=p;
|
|
|
- p = strtok(NULL,",");
|
|
|
- data_index++;;
|
|
|
- }
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("databuffer:\t");
|
|
|
- for (int i = 0; i < data_index; i++)
|
|
|
- {
|
|
|
- printf("%s-",databuffer[i]);
|
|
|
- }
|
|
|
- printf("\n");
|
|
|
- #endif
|
|
|
- if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
|
|
|
- {
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Gpsdata invalide\n");
|
|
|
- #endif
|
|
|
- Gps_buffer.status = 0x00;
|
|
|
- Gps_buffer.speed = 0xffff;
|
|
|
- Gps_buffer.direction= 0xffff;
|
|
|
- }
|
|
|
- else if (strcmp(databuffer[2],"A")==0)
|
|
|
- {
|
|
|
- uint32_t speedtemp;
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Gpsdata valide\n");
|
|
|
- #endif
|
|
|
- Gps_buffer.status = 0x01;//有效,东经,北纬写定
|
|
|
- Gps_buffer.satellite_num = 03;//卫星数目写入1
|
|
|
- strdel(databuffer[3],'.');
|
|
|
- strdel(databuffer[5],'.');
|
|
|
- strdel(databuffer[7],'.');
|
|
|
- speedtemp = atol(databuffer[7])*1852;//节换算单位,1节=1.852km每小时
|
|
|
- Gps_buffer.speed = speedtemp/1e5;
|
|
|
- Gps_buffer.latitude = location_handle(databuffer[3]);
|
|
|
- Gps_buffer.longitude = location_handle(databuffer[5]);
|
|
|
- Gps_buffer.altitude = 00;
|
|
|
- if(Gps_buffer.speed>=100)//大于10km/h才输出方位
|
|
|
+ while(1)
|
|
|
{
|
|
|
- Gps_buffer.direction = atol(databuffer[8]);
|
|
|
+ osDelay(30000/portTICK_PERIOD_MS);
|
|
|
}
|
|
|
- Gps_buffer.direction = 0xfffe;
|
|
|
}
|
|
|
- #ifdef USING_PRINTF
|
|
|
- printf("Gps_buffer:%x,%x,%x,%x",Gps_buffer.direction,Gps_buffer.speed,Gps_buffer.latitude,Gps_buffer.longitude);
|
|
|
- #endif
|
|
|
}
|
|
|
- **/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // //char temp[] = "$GNGGA,082626.00,2939.91801,N,10637.09500,E,1,03,2.34,-20.3,M,,M,,*40";
|
|
|
- // Queue_flag = osMessageQueueGet(norGpsHandle, &msg, 0, 1000);
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("Queue_flag:%d\n ",Queue_flag);
|
|
|
- // #endif
|
|
|
- // if (msg.dataPtr)
|
|
|
- // {
|
|
|
- // void *msgtemp = msg.dataPtr;//取指针
|
|
|
- // p = strtok(msg.dataPtr,delim);//将信息进行分割
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("%s ",msg.dataPtr);
|
|
|
- // #endif
|
|
|
- // p = strtok(p,",");//只取第1行的信息RMC
|
|
|
- // p = strtok(temp,",");//模拟测试
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("p:%s\r\n",p);
|
|
|
- // #endif
|
|
|
- // if (strcmp(p,"$GNRMC")==0)
|
|
|
- // {
|
|
|
- // while (p)
|
|
|
- // {
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("p:%s\r\n",p);
|
|
|
- // #endif
|
|
|
- // databuffer[i]=p;
|
|
|
- // p = strtok(NULL,",");
|
|
|
- // i++;;
|
|
|
- // }
|
|
|
- // if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
|
|
|
- // {
|
|
|
-
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("Gpsdata invalide\n");
|
|
|
- // #endif
|
|
|
- // Gps_buffer.status = 0x00;
|
|
|
- // Gps_buffer.speed = 0xffff;
|
|
|
- // Gps_buffer.direction= 0xffff;
|
|
|
-
|
|
|
- // }
|
|
|
- // else if (strcmp(databuffer[2],"A")==0)
|
|
|
- // {
|
|
|
- // uint32_t speedtemp;
|
|
|
- // #ifdef USING_PRINTF
|
|
|
- // printf("Gpsdata valide\n");
|
|
|
- // #endif
|
|
|
- // Gps_buffer.status = 0x01;//有效,东经,北纬写定
|
|
|
- // Gps_buffer.satellite_num = 03;//卫星数目写入1
|
|
|
- // strdel(databuffer[3],'.');
|
|
|
- // strdel(databuffer[5],'.');
|
|
|
- // strdel(databuffer[7],'.');
|
|
|
- // speedtemp = atol(databuffer[7])*1852;//节换算单位,1节=1.852km每小时
|
|
|
- // Gps_buffer.speed = speedtemp/1e5;
|
|
|
- // Gps_buffer.latitude = location_handle(databuffer[3]);
|
|
|
- // Gps_buffer.longitude = location_handle(databuffer[5]);
|
|
|
- // Gps_buffer.altitude = 00;
|
|
|
- // if(Gps_buffer.speed>=100)//大于10km/h才输出方位
|
|
|
- // {
|
|
|
- // Gps_buffer.direction = atol(databuffer[8]);
|
|
|
- // }
|
|
|
- // Gps_buffer.direction = 0xfffe;
|
|
|
- // }
|
|
|
- // osMessageQueuePut(gpsMsgQueue, &Gps_buffer, 0, 1000);
|
|
|
- // }
|
|
|
- // free(msg.dataPtr);
|
|
|
- // msg.dataPtr=NULL;
|
|
|
- // }
|
|
|
- // msg.dataPtr=NULL;
|
|
|
- // if (Sleep_Flag)
|
|
|
- // {
|
|
|
- // posGGAServiceStop();
|
|
|
- // osThreadExit();
|
|
|
- // break;
|
|
|
- // }
|
|
|
}
|
|
|
}
|
|
|
-//Uart线程初始化
|
|
|
-void Uart_Task_Init()
|
|
|
-{
|
|
|
- osThreadAttr_t task_attr;
|
|
|
- memset(&task_attr,0,sizeof(task_attr));
|
|
|
- memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
|
|
|
- task_attr.name = "Uart_Task";
|
|
|
- task_attr.stack_mem = gProcess_Uart_TaskStack;
|
|
|
- task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
|
|
|
- task_attr.priority = osPriorityBelowNormal7;
|
|
|
- task_attr.cb_mem = &gProcess_Uart_Task_t;
|
|
|
- task_attr.cb_size = sizeof(StaticTask_t);
|
|
|
- osThreadNew(Uart_Task, NULL, &task_attr);
|
|
|
-}
|
|
|
-void Fota_Task_Init()
|
|
|
-{
|
|
|
- osThreadAttr_t task_attr;
|
|
|
- memset(&task_attr,0,sizeof(task_attr));
|
|
|
- memset(gProcess_Fota_TaskStack, 0xA5, PROC_FOTA_TASK_STACK_SIZE);
|
|
|
- task_attr.name = "Fota_Task";
|
|
|
- task_attr.stack_mem = gProcess_Fota_TaskStack;
|
|
|
- task_attr.stack_size = PROC_FOTA_TASK_STACK_SIZE;
|
|
|
- task_attr.priority = osPriorityBelowNormal7;
|
|
|
- task_attr.cb_mem = &gProcess_Fota_Task_t;
|
|
|
- task_attr.cb_size = sizeof(StaticTask_t);
|
|
|
- osThreadNew(Fota_Task, NULL, &task_attr);
|
|
|
-}
|
|
|
-//Gps线程初始化
|
|
|
-void Gps_Task_Init()
|
|
|
+//Tcp线程初始化
|
|
|
+void Tcp_Task_Init()
|
|
|
{
|
|
|
- if(norGpsHandle == NULL)
|
|
|
- {
|
|
|
- norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
|
|
|
- }
|
|
|
osThreadAttr_t task_attr;
|
|
|
+ registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
|
|
|
memset(&task_attr,0,sizeof(task_attr));
|
|
|
- memset(gProcess_Gps_TaskStack, 0xA5, PROC_GPS_TASK_STACK_SIZE);
|
|
|
- task_attr.name = "Gps_Task";
|
|
|
- task_attr.stack_mem = gProcess_Gps_TaskStack;
|
|
|
- task_attr.stack_size = PROC_GPS_TASK_STACK_SIZE;
|
|
|
+ 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_Gps_Task_t;
|
|
|
+ task_attr.cb_mem = &gProcess_Tcp_TaskStack;
|
|
|
task_attr.cb_size = sizeof(StaticTask_t);
|
|
|
- osThreadNew(Gps_Task, NULL, &task_attr);
|
|
|
+ osThreadNew(Tcp_Task, NULL, &task_attr);
|
|
|
}
|
|
|
//主任务线程初始化
|
|
|
void Main_Task_Init()
|
|
@@ -893,9 +496,7 @@ void Main_Task_Init()
|
|
|
void appInit(void *arg)
|
|
|
{
|
|
|
Main_Task_Init();
|
|
|
- Uart_Task_Init();
|
|
|
- Fota_Task_Init();
|
|
|
- //Gps_Task_Init();
|
|
|
+ Tcp_Task_Init();
|
|
|
}
|
|
|
//主函数入口
|
|
|
void main_entry(void)
|