初级会员

- 积分
- 110
- 金钱
- 110
- 注册时间
- 2019-5-15
- 在线时间
- 34 小时
|

楼主 |
发表于 2020-11-20 08:43:48
|
显示全部楼层
主程序初始化调用Tcp_Server_Init();贴上TCP部分的主要代码如下:
//*****************************************************************************
//
// The TCP Sever Port for GAT920
//
//*****************************************************************************
#define D_TCP_SERVER_PORT 5536
//*****************************************************************************
//
// Used to save the TCP Sever Info
//
//*****************************************************************************
TcpServerCallBackStruct g_stCallBack;
/**
* @b Description
* @n
* The interface to creat a TCP for GAT920
*
* @param[in]
* no parameters
* @param[out]
* no parameters
*
* @retval
* no value
*/
void Tcp_Server_Init(void)
{
struct tcp_pcb* tcp_server_pcb;
g_stCallBack.pConnectPCB = NULL;
g_stCallBack.state = ES_TCPSERVER_LISTEN;
g_stCallBack.ui32CntTimeout = 0;
/* Add a new tcp_server_pcb struct to a tcp server */
tcp_server_pcb = tcp_new();
/* bind server port and all the local IP */
tcp_bind(tcp_server_pcb, IP_ADDR_ANY, D_TCP_SERVER_PORT);
/* listen the tcp_server_pcb */
tcp_server_pcb = tcp_listen(tcp_server_pcb);
g_stCallBack.pListenPCB = tcp_server_pcb;
tcp_arg(tcp_server_pcb,&g_stCallBack);
/* Init the tcp_server_accept callback function */
tcp_accept(tcp_server_pcb, tcp_server_accept);
}
/**
* @b Description
* @n
* The Callback function to accept the tcp connection
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
static err_t tcp_server_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
TcpServerCallBackStruct *stCallBackTmp = arg;
//
// Save the PCB for future reference.
//
stCallBackTmp->pConnectPCB = pcb;
//
// Change TCP state to connected.
//
stCallBackTmp->state = ES_TCPSERVER_ACCEPTED;
stCallBackTmp->ui32CntTimeout = 0;
//
// Acknowledge that we have accepted this connection.
//
tcp_accepted(pcb);
tcp_setprio(pcb,TCP_PRIO_MIN); /* set the priority for pcb */
tcp_recv(pcb,tcp_server_recv); //Init the tcp_recv callback function
tcp_err(pcb,tcp_server_error); //Init the tcp_err callback function
tcp_poll(pcb,tcp_server_poll,1);//Init the tcp_poll callback function
tcp_sent(pcb,tcp_server_sent); //Init the tcp_sent callback function
return ERR_OK;
}
/**
* @b Description
* @n
* The Callback function to handle received data through tcp protocol
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
err_t ret_err;
TcpServerCallBackStruct *stCallBackTmp = arg;
stCallBackTmp->ui32CntTimeout = 0;
if(err != ERR_OK) /* the err is not ERR_OK, free the data buf */
{
if(p)
{
pbuf_free(p);
}
ret_err = err;
}
else if(p != NULL)
{
if(stCallBackTmp->state == ES_TCPSERVER_ACCEPTED)
{
//tcp_write(tpcb,p->payload,p->tot_len,1);
RadarApiMsgBufRcv(p->payload,p->tot_len);
tcp_recved(tpcb,p->tot_len);/* Notify the LWIP get more data */
pbuf_free(p);
}
ret_err = ERR_OK;
}
else if(CLOSE_WAIT == tpcb->state) /* received the TCP close message */
{
RadarApiRcvTCPClose();//20200918
}
else
{
/* None */
}
return ret_err;
}
/**
* @b Description
* @n
* The Callback function to handle the tcp error
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
static void tcp_server_error(void *arg,err_t err)
{
DEBUG_MSG("tcpServerError 0x%08x, %d\n", arg, err);
if(arg!=NULL)
{
mem_free(arg);
}
}
/**
* @b Description
* @n
* The Callback function to handle the tcp sever poll
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
static err_t tcp_server_poll(void *arg, struct tcp_pcb *tpcb)
{
TcpServerCallBackStruct *stCallBackTmp;
stCallBackTmp=(TcpServerCallBackStruct *)arg;
stCallBackTmp->ui32CntTimeout++;
if(stCallBackTmp->ui32CntTimeout > MAX_CONNECT_TIMEOUT)
{
DEBUG_MSG("TCP TimeOut\n");
//tcp_server_close(tpcb,stCallBackTmp);
RadarApiRcvTCPClose();
}
return ERR_OK;
}
/**
* @b Description
* @n
* The Callback function to handle the tcp sever sending data
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
//lwIP tcp_sent callback function (Data is sent when the remote host receives ACK signal)
static err_t tcp_server_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
//TcpServerCallBackStruct *stCallBackTmp;
//stCallBackTmp = (TcpServerCallBackStruct *) arg;
//if(stCallBackTmp->p)
// tcp_server_senddata(tpcb,stCallBackTmp);
return ERR_OK;
}
/**
* @b Description
* @n
* The Callback function to handle the tcp error
*
* @param[in]
*
* @param[out]
* no parameters
*
* @retval
* no value
*/
err_t tcp_server_send_data(uint8_t *data,int iLen)
{
if(g_stCallBack.state != ES_TCPSERVER_ACCEPTED)
return ERR_TIMEOUT;
return tcp_write(g_stCallBack.pConnectPCB, data,iLen, 1);
}
void Tcp_ServerClose(void)
{
if(g_stCallBack.state != ES_TCPSERVER_NONE)
{
tcp_arg(g_stCallBack.pConnectPCB,NULL);
tcp_sent(g_stCallBack.pConnectPCB,NULL);
tcp_recv(g_stCallBack.pConnectPCB,NULL);
tcp_err(g_stCallBack.pConnectPCB,NULL);
tcp_poll(g_stCallBack.pConnectPCB,NULL,0);
tcp_close(g_stCallBack.pConnectPCB);
mem_free(&g_stCallBack);
memset(&g_stCallBack,0,sizeof(g_stCallBack));
}
}
|
|