/*******************************************************************************
|
* File Name : TCPClient.c
|
* Description :
|
* Created on : 2021?1?9?
|
* Author : www.hido-studio.com
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Include Files *
|
*******************************************************************************/
|
#include "TCPClient.h"
|
#include "Socket.h"
|
#include "HIDO_Debug.h"
|
#include "Internet.h"
|
#include "string.h"
|
#include "HIDO_Timer.h"
|
#include "HIDO_Util.h"
|
//#include "Battery.h"
|
#include "global_param.h"
|
#include "AppConfig.h"
|
//#include "OTA.h"
|
//#include "Log.h"
|
#include "Uart.h"
|
|
/*******************************************************************************
|
* Macro *
|
*******************************************************************************/
|
#define STRCMP(cmd, header) strncmp((HIDO_CHAR *)_u8Data, header, sizeof(header) - 1)
|
/*******************************************************************************
|
* Type Definition *
|
*******************************************************************************/
|
|
|
/*******************************************************************************
|
* Local Variable *
|
*******************************************************************************/
|
HIDO_INT32 l_i32TCPClientID = 0;
|
E_TCPClientState l_eTCPClientState = TCP_CLIENT_STATE_IDLE; /* 导出供PathTest使用 */
|
static HIDO_UINT32 l_u32HeartBeatTick = 0;
|
static HIDO_UINT8 l_au8CmdBuff[1024];
|
static HIDO_BOOL l_bTCPPaused = HIDO_FALSE; /* TCP暂停标志(路径下载期间) */
|
static E_TCPClientState l_eSavedTCPState = TCP_CLIENT_STATE_IDLE; /* 保存的TCP状态 */
|
|
/*******************************************************************************
|
* Local Function Declaration *
|
*******************************************************************************/
|
HIDO_INT32 TCPClient_Heartbeat(HIDO_VOID);
|
|
/*******************************************************************************
|
* Local Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name : TCPClient_DataProc
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021?1?9?
|
*******************************************************************************/
|
static HIDO_INT32 TCPClient_DataProc(HIDO_UINT8 *_u8Data, HIDO_UINT32 _u32Len)
|
{
|
|
l_u32HeartBeatTick = HIDO_TimerGetTick();
|
if(_u32Len!=0)
|
if(Uart_Send(UART_ID_GPS, _u8Data, _u32Len) != HIDO_OK)
|
{
|
HIDO_Debug("RTK Data Send Error\r\n");
|
}
|
|
HIDO_Debug("%uB RTK Data Sent\r\n", _u32Len);
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : TCPClient_SocketEventProc
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021?1?9?
|
*******************************************************************************/
|
static HIDO_VOID TCPClient_SocketEventProc(HIDO_INT32 _i32SockID, E_SocketEvent _eEvent, HIDO_VOID *_pData)
|
{
|
switch(_eEvent)
|
{
|
case SOCKET_EVENT_CONNECT_FAILED:
|
{
|
if(l_eTCPClientState == TCP_CLIENT_STATE_CONNECTING)
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
}
|
|
break;
|
}
|
case SOCKET_EVENT_CONNECTED:
|
{
|
if(l_eTCPClientState == TCP_CLIENT_STATE_CONNECTING)
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_CONNECTED;
|
}
|
break;
|
}
|
case SOCKET_EVENT_CLOSED:
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
break;
|
}
|
case SOCKET_EVENT_RECV_DATA:
|
{
|
HIDO_INT32 i32Ret = 0;
|
HIDO_UINT32 u32RecvLen = 0;
|
|
i32Ret = Socket_Recv(l_i32TCPClientID, l_au8CmdBuff, sizeof(l_au8CmdBuff) - 1, &u32RecvLen);
|
if(HIDO_OK == i32Ret)
|
{
|
TCPClient_DataProc(l_au8CmdBuff, u32RecvLen);
|
}
|
break;
|
}
|
default:
|
{
|
break;
|
}
|
}
|
}
|
|
/*******************************************************************************
|
* Global Function *
|
*******************************************************************************/
|
/*******************************************************************************
|
* Function Name :
|
* Description :
|
* Input :
|
* Output :
|
* Return :
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021?1?9?
|
*******************************************************************************/
|
u8 TCPfail_flag = 0;
|
u32 TCPfailetimer;
|
HIDO_INT32 TCPClient_Poll(void)
|
{
|
// 如果TCP被暂停(路径下载期间),不进行重连操作
|
if (l_bTCPPaused) {
|
return HIDO_OK;
|
}
|
|
if (Internet_IsIPReady() == HIDO_TRUE)
|
{
|
HIDO_UINT32 u32CurTick = HIDO_TimerGetTick();
|
if (TCP_CLIENT_STATE_IDLE == l_eTCPClientState)
|
{
|
if (!TCPfail_flag)
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_CONNECTING;
|
|
HIDO_UtilSnprintf((HIDO_CHAR *) l_au8CmdBuff, sizeof(l_au8CmdBuff), "%u.%u.%u.%u", g_com_map[TCP_IP_0],
|
g_com_map[TCP_IP_1], g_com_map[TCP_IP_2], g_com_map[TCP_IP_3]);
|
|
Socket_Connect(l_i32TCPClientID, (HIDO_CHAR *) l_au8CmdBuff, g_com_map[TCP_PORT]);
|
}
|
else
|
{
|
if (u32CurTick - TCPfailetimer > 60000 * 60)
|
{
|
TCPfail_flag = 0;
|
}
|
}
|
}
|
else if (TCP_CLIENT_STATE_CONNECTED == l_eTCPClientState)
|
{
|
|
if ((u32CurTick - l_u32HeartBeatTick) >= 10000)
|
{
|
l_u32HeartBeatTick = u32CurTick;
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
}
|
}
|
}
|
else
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
}
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : TCPClient_Init
|
* Description : TCP??????
|
* Input : None
|
* Output : None
|
* Return : HIDO_OK ??,HIDO_ERR ??
|
* Author : www.hido-studio.com
|
* Modified Date: : 2021?1?9?
|
*******************************************************************************/
|
HIDO_INT32 TCPClient_Init(void)
|
{
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
l_bTCPPaused = HIDO_FALSE;
|
Socket_Create(&l_i32TCPClientID, SOCKET_TYPE_TCP, TCPClient_SocketEventProc, HIDO_NULL);
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : TCPClient_Pause
|
* Description : 暂停TCP连接(用于路径下载期间避免差分数据干扰)
|
* Input : None
|
* Output : None
|
* Return : HIDO_OK
|
*******************************************************************************/
|
HIDO_INT32 TCPClient_Pause(void)
|
{
|
if (l_bTCPPaused) {
|
HIDO_Debug("[TCPClient] Already paused\r\n");
|
return HIDO_OK;
|
}
|
|
HIDO_Debug("[TCPClient] Pausing TCP connection...\r\n");
|
|
// 保存当前状态
|
l_eSavedTCPState = l_eTCPClientState;
|
|
// 如果当前已连接,关闭连接
|
if (l_eTCPClientState == TCP_CLIENT_STATE_CONNECTED ||
|
l_eTCPClientState == TCP_CLIENT_STATE_CONNECTING) {
|
Socket_Close(l_i32TCPClientID);
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
HIDO_Debug("[TCPClient] TCP connection closed\r\n");
|
}
|
|
// 设置暂停标志
|
l_bTCPPaused = HIDO_TRUE;
|
|
return HIDO_OK;
|
}
|
|
/*******************************************************************************
|
* Function Name : TCPClient_Resume
|
* Description : 恢复TCP连接(路径下载完成后)
|
* Input : None
|
* Output : None
|
* Return : HIDO_OK
|
*******************************************************************************/
|
HIDO_INT32 TCPClient_Resume(void)
|
{
|
if (!l_bTCPPaused) {
|
HIDO_Debug("[TCPClient] Not paused, no need to resume\r\n");
|
return HIDO_OK;
|
}
|
|
HIDO_Debug("[TCPClient] Resuming TCP connection...\r\n");
|
|
// 清除暂停标志
|
l_bTCPPaused = HIDO_FALSE;
|
|
// 重置状态为IDLE,让Poll函数自动重连
|
l_eTCPClientState = TCP_CLIENT_STATE_IDLE;
|
|
HIDO_Debug("[TCPClient] TCP will reconnect automatically\r\n");
|
|
return HIDO_OK;
|
}
|