stm32_ota/USER/USER_TCP/user_tcp.c

177 lines
7.2 KiB
C
Raw Normal View History

2023-12-15 06:28:06 +00:00
#include "user_tcp.h"
#include "userapp.h"
/********************************************************************************
* @file gate_tcp.c
* @author <EFBFBD>̳ϿƼ<EFBFBD> Mr.Wang
* @version V1.0.0
* @date 11-Dec-2018
* @brief <EFBFBD>Quectelģ<EFBFBD><EFBFBD>EC20<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP/IPӲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
******************************************************************************
* @attention
* Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
* contextID:<EFBFBD><EFBFBD>·ID connetcID<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ID channal<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
* EC20ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·ID<EFBFBD><EFBFBD>Χ1~16<EFBFBD><EFBFBD>ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·ID<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP<EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>Χ0~11<EFBFBD><EFBFBD>
* ÿһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·ID<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>12<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>ƹ<EFBFBD><EFBFBD>TCP/IPЭ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·1D<EFBFBD><EFBFBD><EFBFBD><EFBFBD>contextID=1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>
* ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><EFBFBD>Զ<EFBFBD>̨<EFBFBD><EFBFBD>ͬIP<EFBFBD>Ͷ˿ڵķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ChannalP_s<EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD>ÿһ·<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
/*****************************************
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD>ⲿʹ<EFBFBD>õij<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************/
FrameQueue_s sTcp0Queue ; //<2F><>ҪTCPͨ<50><CDA8>0 <20><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ˻<DAB4><CBBB>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>OS<4F><53><EFBFBD>и<EFBFBD>TCP<43><50><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һֱ<D2BB>ڷ<EFBFBD><DAB7><EFBFBD>TCP<43><50><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/*****************************************
*usertcp<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************/
/*****************************************
*usertcpȫ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************/
ChannalP_s sChannal0 = { TCP_CONNECTID0, TCP_CONNECTID0_SERVERIP0, TCP_CONNECTID0_SERVERPORT0, TCP_CONNECTID0_LOCALPORT0 } ; //TCPͨ<50><CDA8>0<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
/******connectId************serverIP*******************serverPort*****************localPort */
//ChannalP_s s_Channal1 = { TCP_CONNECTID1, TCP_CONNECTID1_SERVERIP, TCP_CONNECTID1_SERVERPORT, TCP_CONNECTID1_LOCALPORT }; //TCPͨ<50><CDA8>1
//ChannalP_s s_Channal2 = { TCP_CONNECTID2, TCP_CONNECTID2_SERVERIP, TCP_CONNECTID2_SERVERPORT, TCP_CONNECTID2_LOCALPORT }; //TCPͨ<50><CDA8>2
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void SetAppTcpIP(ChannalP_s *psChannal, char* ip)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP
**************************************************************************************************/
void SetAppTcpIP(ChannalP_s *psChannal, char* ip)
{
memset(psChannal->serverIP , 0, MAX_IP_LEN ) ;
strcpy((char*)psChannal->serverIP, ip) ;
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void SetAppTcpPort(ChannalP_s *psChannal, char* port)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PORT
**************************************************************************************************/
void SetAppTcpPort(ChannalP_s *psChannal, char* port)
{
//memset(psChannal->serverPort , 0, MAX_IP_LEN ) ;
//strcpy((char*)psChannal->serverIP, ip) ;
psChannal->serverPort = atoi(port) ;
}
/*********************************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>RunResult AppTcpInit(void)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD>Ͽ<EFBFBD>Socket ȥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>PDP
*********************************************************************************************************************/
void TcpDisconnetc(void)
{
Close_Socket(TCP_CONNECTID0) ; //<2F>Ͽ<EFBFBD>Socket
Deact_Context(TCP_CONTEXTID) ; //ȥ<><C8A5><EFBFBD><EFBFBD>TCP<43><50>·<EFBFBD><C2B7>PDP
}
/*********************************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>RunResult AppTcpInit(void)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>PDP<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>TCP0<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>SOCKET<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP0ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>TCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CHANNAL0_MAX_NUM<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵӲ<EFBFBD><EFBFBD>ɨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*********************************************************************************************************************/
RunResult AppTcpInit(void)
{
RunResult runResult = TIMEOUT ;
runResult = Tcp_PDP_Init() ; //TCP<43><50>·PDP<44><50>ʼ<EFBFBD><CABC>
if( RUNOK == runResult)
{
runResult = TIMEOUT ;
runResult = Tcp_Channal_Init(&sChannal0) ;
if( RUNOK == runResult)
{
AppLogPrintf( "TCP Server<65><72><EFBFBD>ӳɹ<D3B3>:%s:%d <20><>", sChannal0.serverIP, sChannal0.serverPort) ;
return(runResult) ;
}
else
{
ErrorLogPrintf("TCP Server<65><72><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>:%s:%d <20><>", sChannal0.serverIP, sChannal0.serverPort) ;
}
}
else
{
return(runResult) ;
}
return(runResult) ;
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> RunResult TcpUpdata( UPDATATYPE_e updataType, char *format, ...)
* ˵ <EFBFBD><EFBFBD><EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD>TCP<EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><EFBFBD><EFBFBD>TCP SERVER
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 updataType: UPDATATYPE_eö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @arg ERRORRAME
* @arg WARNINGFRAME
* @arg HEARTFRAME
* @arg LOGFRAME
* @arg UCMDBACKFRAME
* @param2 char *format,... <EFBFBD>ɱ<EFBFBD><EFBFBD>α<EFBFBD><EFBFBD><EFBFBD>
* @param3 ... <EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 status: RunResultö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TcpUpdata( BOOTTOTCPFRAME, "I am heart %d .", 5) ;
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD> (L0142MAC:31FFD405524E353728902251;00&I am heart 5 .$X)
* ע <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>TCP_LOADBUF_MAXLEN
************************************************************************************************************/
RunResult TcpUpdata( UPDATATYPE_e updataType, char *format, ...)
{
static uint8_t counter = 0x30 ;
RunResult status = RUNERR ;
char *upDataBuf = (char*)portMalloc(TCP_LOADBUF_MAXLEN+sizeof(TcpFrame_S)+32) ; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ泤<DAB4><E6B3A4>Ϊ<EFBFBD><CEAA><EFBFBD>س<EFBFBD><D8B3><EFBFBD>+TcpFrame_S<5F><EFBFBD><EFBFBD><E5B3A4>
TcpFrame_S *sTcpFrameData = (TcpFrame_S *)upDataBuf ; //tcp<63><70><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>
va_list ap;
va_start(ap, format);
int outLen = vsnprintf(&(sTcpFrameData->loadHead)+1 , TCP_LOADBUF_MAXLEN, (const char*)format, ap ) ; //<2F><><EFBFBD><EFBFBD>Ϊֹ<CEAA><D6B9><EFBFBD><EFBFBD><EFBFBD>еIJ<D0B5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>ܵ<EFBFBD>upDataBuf<75><66>
if((outLen<=0)||( outLen > TCP_LOADBUF_MAXLEN))
{
ErrorLogPrintf("%s,%d:upDataBuf spillover<65><72>",__FILE__, __LINE__) ;
va_end (ap);
portFree(upDataBuf) ;
return RUNERR ;
}
sTcpFrameData->head = '(' ;
sTcpFrameData->frameType = updataType ;
sTcpFrameData->loadLen[0] = outLen/100 + 0x30;
sTcpFrameData->loadLen[1] = outLen%100/10 + 0x30 ;
sTcpFrameData->loadLen[2] = outLen%10 + 0x30;
sTcpFrameData->frameNum = counter ;
counter ++ ;
if( counter > 0x39 ) //<2F><>ֹcounter<65>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
counter = 0x30 ;
}
strncpy(sTcpFrameData->macHead, "MAC:", 4) ;
GetDeviceMacAddress(sTcpFrameData->macid, STRMACID) ;
sTcpFrameData->macTail = ';' ;
memset(sTcpFrameData->cmdCode, 0x30, 2) ;
sTcpFrameData->loadHead = '&' ;
// for(int n =0; n<outLen; n++) //<2F><>loadBuf<75>е<EFBFBD>0x0D 0x0A<30>滻Ϊ*<2A><><EFBFBD><EFBFBD>ֹת<D6B9><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOG<4F><47><EFBFBD>ҡ<EFBFBD>
// {
// if( (*(sTcpFrameData->loadBuf+n) == 0x0D ) || (*(sTcpFrameData->loadBuf+n) == 0x0A) )
// {
// *(sTcpFrameData->loadBuf+n) = '*' ;
// }
// }
strncat(upDataBuf, "$X)\r\n", 3) ; //TcpFrame_S<5F><EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>ƴ<EFBFBD>ӵ<EFBFBD> upDataBufβ<66><CEB2>
// if( RW_OK != InsertQueueMemData(&sTcp0Queue, upDataBuf, strlen(upDataBuf)))
// {
// ErrorLogPrintf("%s,%d:sTcp0Queue spillover<65><72>", __FILE__, __LINE__) ;
// }
if( TCPBIT_2 == true ) //<2F><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>
{
status = Tcp_SendData(&sChannal0, (uint8_t*)upDataBuf, strlen(upDataBuf)) ;
}
AppLogPrintf(upDataBuf) ;
portFree(upDataBuf) ;
return (status) ;
}