/******************************************************************************* * File Name : TCPClient.c * Description : * Created on : 2021?1?9? * Author : www.hido-studio.com *******************************************************************************/ /******************************************************************************* * Include Files * *******************************************************************************/ #include "TCPClient_1.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 * *******************************************************************************/ typedef enum { TCP_CLIENT_STATE_IDLE, TCP_CLIENT_STATE_CONNECTING, TCP_CLIENT_STATE_CONNECTED, }E_TCPClientState; /******************************************************************************* * Local Variable * *******************************************************************************/ static HIDO_INT32 l_i32TCPClientID = 1; static E_TCPClientState l_eTCPClientState = TCP_CLIENT_STATE_IDLE; static HIDO_UINT32 l_u32HeartBeatTick = 0; static HIDO_UINT8 l_au8CmdBuff[1024]; /******************************************************************************* * 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? *******************************************************************************/ //uint8_t ceshidata[1000]={0,1,2,3,4,5,6,7,8,9}; //uint16_t ceshichangdu; static HIDO_INT32 TCPClient_DataProc(HIDO_UINT8 *_u8Data, HIDO_UINT32 _u32Len) { l_u32HeartBeatTick = HIDO_TimerGetTick(); // memcpy(ceshidata,_u8Data,_u32Len); // ceshichangdu=_u32Len; if(_u32Len!=0) if(uart_send(UART_ID1, _u8Data, _u32Len,NULL) != 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_1(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? *******************************************************************************/ static uint8_t TCPfail_flag = 0; static uint32_t TCPfailetimer; HIDO_INT32 TCPClient_Poll_1(void) { 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_1(void) { l_eTCPClientState = TCP_CLIENT_STATE_IDLE; Socket_Create(&l_i32TCPClientID, SOCKET_TYPE_TCP, TCPClient_SocketEventProc_1, HIDO_NULL); return HIDO_OK; }