From e0de3d1e4691d639595c87672ba0a6da71efe568 Mon Sep 17 00:00:00 2001 From: Allenn Date: Thu, 14 Dec 2023 11:42:19 +0800 Subject: [PATCH] feat: keep client connect request after timeout --- .../XiZi_IIoT/board/ch32v307vct6/board.c | 2 + .../ethernet/connect_ether.c | 38 +++++-------------- .../ethernet/test/wch_tcp_test.c | 33 ++-------------- .../include/connect_ether.h | 4 +- 4 files changed, 19 insertions(+), 58 deletions(-) diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/board.c b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/board.c index 5ff2e9855..1f36ec74a 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/board.c +++ b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/board.c @@ -67,7 +67,9 @@ void InitBoardHardware() InitHwUart(); InstallConsole("uart1", "uart1_drv", "uart1_dev1"); +#ifdef BSP_USING_ETH InitHwEth(); +#endif KPrintf("consle init completed.\n"); KPrintf("board initialization......\n"); diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/connect_ether.c b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/connect_ether.c index ae20610cc..b4c52ba63 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/connect_ether.c +++ b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/connect_ether.c @@ -103,28 +103,6 @@ void WCHNET_CreateTcpSocket(uint8_t* DESIP, uint16_t srcport, uint16_t desport, mStopIfError(i); } -/********************************************************************* - * @fn WCHNET_CreateTcpSocketListen - * - * @brief Create TCP Socket for Listening - * - * @return none - */ -void WCHNET_CreateTcpSocketListen(uint16_t srcport, uint8_t* SocketId) -{ - uint8_t i; - SOCK_INF TmpSocketInf; - - memset((void*)&TmpSocketInf, 0, sizeof(SOCK_INF)); - TmpSocketInf.SourPort = srcport; - TmpSocketInf.ProtoType = PROTO_TYPE_TCP; - i = WCHNET_SocketCreat(SocketId, &TmpSocketInf); - KPrintf("SocketIdForListen %d\r\n", *SocketId); - mStopIfError(i); - i = WCHNET_SocketListen(*SocketId); // listen for connections - mStopIfError(i); -} - /********************************************************************* * @fn WCHNET_DataLoopback * @@ -164,9 +142,9 @@ void WCHNET_DataLoopback(uint8_t id) * @param socketid - socket id. * intstat - interrupt status * - * @return none + * @return 0 or TIME_OUT */ -void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) +int WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) { uint8_t i; @@ -208,7 +186,9 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) } } KPrintf("TCP Timeout\r\n"); + return TIME_OUT; } + return 0; } /********************************************************************* @@ -216,9 +196,9 @@ void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat) * * @brief Global Interrupt Handle * - * @return none + * @return 0 or SockInt */ -void WCHNET_HandleGlobalInt(void) +int WCHNET_HandleGlobalInt(void) { uint8_t intstat; uint16_t i; @@ -242,10 +222,12 @@ void WCHNET_HandleGlobalInt(void) if (intstat & GINT_STAT_SOCKET) { // socket related interrupt for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) { socketint = WCHNET_GetSocketInt(i); - if (socketint) - WCHNET_HandleSockInt(i, socketint); + if (socketint) { + return WCHNET_HandleSockInt(i, socketint); + } } } + return 0; } uint8_t InitHwEth() diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/test/wch_tcp_test.c b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/test/wch_tcp_test.c index a855fc890..298c1b37e 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/test/wch_tcp_test.c +++ b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/ethernet/test/wch_tcp_test.c @@ -9,7 +9,6 @@ uint16_t desport = 1000; // destination port uint16_t srcport = 1000; // source port uint8_t SocketId; -uint8_t SocketIdForListen; // Socket for Listening /********************************************************************* * @fn TCP client @@ -30,7 +29,9 @@ int Tcp_Client(void) /*Query the Ethernet global interrupt, * if there is an interrupt, call the global interrupt handler*/ if (WCHNET_QueryGlobalInt()) { - WCHNET_HandleGlobalInt(); + if (WCHNET_HandleGlobalInt() == TIME_OUT) { + WCHNET_CreateTcpSocket(DESIP, srcport, desport, &SocketId); + } } } } @@ -43,30 +44,4 @@ int test_tcp_client(int argc, char* argv[]) } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), - test_tcp_client, test_tcp_client, test tcp client); - -int Tcp_Server(void) -{ - WCHNET_CreateTcpSocketListen(srcport, &SocketIdForListen); // Create TCP Socket for Listening - - while (1) { - /*Ethernet library main task function, - * which needs to be called cyclically*/ - WCHNET_MainTask(); - /*Query the Ethernet global interrupt, - * if there is an interrupt, call the global interrupt handler*/ - if (WCHNET_QueryGlobalInt()) { - WCHNET_HandleGlobalInt(); - } - } -} - -int test_tcp_server(int argc, char* argv[]) -{ - KPrintf("TCPServer Test\r\n"); - Tcp_Server(); - return 0; -} - -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), - test_tcp_server, test_tcp_server, test tcp server); \ No newline at end of file + test_tcp_client, test_tcp_client, test tcp client); \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/include/connect_ether.h b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/include/connect_ether.h index 13ee80495..074822c5d 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/include/connect_ether.h +++ b/Ubiquitous/XiZi_IIoT/board/ch32v307vct6/third_party_driver/include/connect_ether.h @@ -31,7 +31,9 @@ uint8_t InitHwEth(); void WCHNET_CreateTcpSocket(uint8_t* DESIP, uint16_t srcport, uint16_t desport, uint8_t* SocketId); void WCHNET_CreateTcpSocketListen(uint16_t srcport, uint8_t* SocketId); -void WCHNET_HandleGlobalInt(void); +int WCHNET_HandleGlobalInt(void); + +#define TIME_OUT -1 #ifdef __cplusplus }