From 895b8de45c969f7f49bc8acbf8de5523d7d0dbf6 Mon Sep 17 00:00:00 2001 From: wuliang Date: Fri, 10 Apr 2020 22:16:46 +0800 Subject: [PATCH] Add tcp client/server echo example. @EVB_LN882x --- .../KEIL/wifi_demo/TencentOS_tiny.uvoptx | 274 ++++++++++-------- .../KEIL/wifi_demo/TencentOS_tiny.uvprojx | 10 + examples/wifi_ln882x/tcp_client_echo.c | 166 +++++++++++ examples/wifi_ln882x/tcp_client_echo.h | 16 + examples/wifi_ln882x/tcp_server_echo.c | 164 +++++++++++ examples/wifi_ln882x/tcp_server_echo.h | 12 + examples/wifi_ln882x/wifi_app_ln882x.c | 20 ++ 7 files changed, 537 insertions(+), 125 deletions(-) create mode 100644 examples/wifi_ln882x/tcp_client_echo.c create mode 100644 examples/wifi_ln882x/tcp_client_echo.h create mode 100644 examples/wifi_ln882x/tcp_server_echo.c create mode 100644 examples/wifi_ln882x/tcp_server_echo.h diff --git a/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvoptx b/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvoptx index 72b83e07..b56d87ad 100644 --- a/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvoptx +++ b/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvoptx @@ -297,7 +297,7 @@ examples - 1 + 0 0 0 0 @@ -313,6 +313,30 @@ 0 0 + + 3 + 5 + 1 + 0 + 0 + 0 + ..\..\..\..\examples\wifi_ln882x\tcp_server_echo.c + tcp_server_echo.c + 0 + 0 + + + 3 + 6 + 1 + 0 + 0 + 0 + ..\..\..\..\examples\wifi_ln882x\tcp_client_echo.c + tcp_client_echo.c + 0 + 0 + @@ -323,7 +347,7 @@ 0 4 - 5 + 7 1 0 0 @@ -343,7 +367,7 @@ 0 5 - 6 + 8 1 0 0 @@ -355,7 +379,7 @@ 5 - 7 + 9 1 0 0 @@ -367,7 +391,7 @@ 5 - 8 + 10 1 0 0 @@ -379,7 +403,7 @@ 5 - 9 + 11 1 0 0 @@ -391,7 +415,7 @@ 5 - 10 + 12 1 0 0 @@ -403,7 +427,7 @@ 5 - 11 + 13 1 0 0 @@ -415,7 +439,7 @@ 5 - 12 + 14 1 0 0 @@ -427,7 +451,7 @@ 5 - 13 + 15 1 0 0 @@ -439,7 +463,7 @@ 5 - 14 + 16 1 0 0 @@ -451,7 +475,7 @@ 5 - 15 + 17 1 0 0 @@ -463,7 +487,7 @@ 5 - 16 + 18 1 0 0 @@ -475,7 +499,7 @@ 5 - 17 + 19 1 0 0 @@ -487,7 +511,7 @@ 5 - 18 + 20 1 0 0 @@ -507,7 +531,7 @@ 0 6 - 19 + 21 1 0 0 @@ -519,7 +543,7 @@ 6 - 20 + 22 1 0 0 @@ -531,7 +555,7 @@ 6 - 21 + 23 1 0 0 @@ -543,7 +567,7 @@ 6 - 22 + 24 1 0 0 @@ -555,7 +579,7 @@ 6 - 23 + 25 1 0 0 @@ -567,7 +591,7 @@ 6 - 24 + 26 1 0 0 @@ -579,7 +603,7 @@ 6 - 25 + 27 1 0 0 @@ -591,7 +615,7 @@ 6 - 26 + 28 1 0 0 @@ -603,7 +627,7 @@ 6 - 27 + 29 1 0 0 @@ -615,7 +639,7 @@ 6 - 28 + 30 1 0 0 @@ -627,7 +651,7 @@ 6 - 29 + 31 1 0 0 @@ -639,7 +663,7 @@ 6 - 30 + 32 1 0 0 @@ -651,7 +675,7 @@ 6 - 31 + 33 1 0 0 @@ -663,7 +687,7 @@ 6 - 32 + 34 1 0 0 @@ -675,7 +699,7 @@ 6 - 33 + 35 1 0 0 @@ -687,7 +711,7 @@ 6 - 34 + 36 1 0 0 @@ -699,7 +723,7 @@ 6 - 35 + 37 1 0 0 @@ -711,7 +735,7 @@ 6 - 36 + 38 1 0 0 @@ -723,7 +747,7 @@ 6 - 37 + 39 1 0 0 @@ -735,7 +759,7 @@ 6 - 38 + 40 1 0 0 @@ -747,7 +771,7 @@ 6 - 39 + 41 1 0 0 @@ -759,7 +783,7 @@ 6 - 40 + 42 1 0 0 @@ -771,7 +795,7 @@ 6 - 41 + 43 1 0 0 @@ -783,7 +807,7 @@ 6 - 42 + 44 1 0 0 @@ -803,7 +827,7 @@ 0 7 - 43 + 45 1 0 0 @@ -815,7 +839,7 @@ 7 - 44 + 46 2 0 0 @@ -827,7 +851,7 @@ 7 - 45 + 47 1 0 0 @@ -847,7 +871,7 @@ 0 8 - 46 + 48 5 0 0 @@ -859,7 +883,7 @@ 8 - 47 + 49 5 0 0 @@ -879,7 +903,7 @@ 0 9 - 48 + 50 1 0 0 @@ -899,7 +923,7 @@ 0 10 - 49 + 51 1 0 0 @@ -919,7 +943,7 @@ 0 11 - 50 + 52 1 0 0 @@ -931,7 +955,7 @@ 11 - 51 + 53 1 0 0 @@ -951,7 +975,7 @@ 0 12 - 52 + 54 1 0 0 @@ -971,7 +995,7 @@ 0 13 - 53 + 55 1 0 0 @@ -983,7 +1007,7 @@ 13 - 54 + 56 1 0 0 @@ -995,7 +1019,7 @@ 13 - 55 + 57 1 0 0 @@ -1007,7 +1031,7 @@ 13 - 56 + 58 1 0 0 @@ -1019,7 +1043,7 @@ 13 - 57 + 59 1 0 0 @@ -1031,7 +1055,7 @@ 13 - 58 + 60 1 0 0 @@ -1051,7 +1075,7 @@ 0 14 - 59 + 61 1 0 0 @@ -1063,7 +1087,7 @@ 14 - 60 + 62 1 0 0 @@ -1075,7 +1099,7 @@ 14 - 61 + 63 1 0 0 @@ -1087,7 +1111,7 @@ 14 - 62 + 64 1 0 0 @@ -1099,7 +1123,7 @@ 14 - 63 + 65 1 0 0 @@ -1111,7 +1135,7 @@ 14 - 64 + 66 1 0 0 @@ -1123,7 +1147,7 @@ 14 - 65 + 67 1 0 0 @@ -1135,7 +1159,7 @@ 14 - 66 + 68 1 0 0 @@ -1147,7 +1171,7 @@ 14 - 67 + 69 1 0 0 @@ -1159,7 +1183,7 @@ 14 - 68 + 70 1 0 0 @@ -1171,7 +1195,7 @@ 14 - 69 + 71 1 0 0 @@ -1183,7 +1207,7 @@ 14 - 70 + 72 1 0 0 @@ -1195,7 +1219,7 @@ 14 - 71 + 73 1 0 0 @@ -1215,7 +1239,7 @@ 0 15 - 72 + 74 5 0 0 @@ -1235,7 +1259,7 @@ 0 16 - 73 + 75 1 0 0 @@ -1247,7 +1271,7 @@ 16 - 74 + 76 1 0 0 @@ -1259,7 +1283,7 @@ 16 - 75 + 77 1 0 0 @@ -1271,7 +1295,7 @@ 16 - 76 + 78 1 0 0 @@ -1283,7 +1307,7 @@ 16 - 77 + 79 1 0 0 @@ -1295,7 +1319,7 @@ 16 - 78 + 80 1 0 0 @@ -1307,7 +1331,7 @@ 16 - 79 + 81 1 0 0 @@ -1327,7 +1351,7 @@ 0 17 - 80 + 82 4 0 0 @@ -1339,7 +1363,7 @@ 17 - 81 + 83 4 0 0 @@ -1351,7 +1375,7 @@ 17 - 82 + 84 4 0 0 @@ -1371,7 +1395,7 @@ 0 18 - 83 + 85 1 0 0 @@ -1391,7 +1415,7 @@ 0 19 - 84 + 86 1 0 0 @@ -1403,7 +1427,7 @@ 19 - 85 + 87 1 0 0 @@ -1415,7 +1439,7 @@ 19 - 86 + 88 1 0 0 @@ -1427,7 +1451,7 @@ 19 - 87 + 89 1 0 0 @@ -1439,7 +1463,7 @@ 19 - 88 + 90 1 0 0 @@ -1451,7 +1475,7 @@ 19 - 89 + 91 1 0 0 @@ -1463,7 +1487,7 @@ 19 - 90 + 92 1 0 0 @@ -1475,7 +1499,7 @@ 19 - 91 + 93 1 0 0 @@ -1487,7 +1511,7 @@ 19 - 92 + 94 1 0 0 @@ -1499,7 +1523,7 @@ 19 - 93 + 95 1 0 0 @@ -1511,7 +1535,7 @@ 19 - 94 + 96 1 0 0 @@ -1523,7 +1547,7 @@ 19 - 95 + 97 1 0 0 @@ -1535,7 +1559,7 @@ 19 - 96 + 98 1 0 0 @@ -1547,7 +1571,7 @@ 19 - 97 + 99 1 0 0 @@ -1559,7 +1583,7 @@ 19 - 98 + 100 1 0 0 @@ -1571,7 +1595,7 @@ 19 - 99 + 101 1 0 0 @@ -1583,7 +1607,7 @@ 19 - 100 + 102 1 0 0 @@ -1595,7 +1619,7 @@ 19 - 101 + 103 1 0 0 @@ -1607,7 +1631,7 @@ 19 - 102 + 104 1 0 0 @@ -1619,7 +1643,7 @@ 19 - 103 + 105 1 0 0 @@ -1631,7 +1655,7 @@ 19 - 104 + 106 1 0 0 @@ -1643,7 +1667,7 @@ 19 - 105 + 107 1 0 0 @@ -1655,7 +1679,7 @@ 19 - 106 + 108 1 0 0 @@ -1667,7 +1691,7 @@ 19 - 107 + 109 1 0 0 @@ -1679,7 +1703,7 @@ 19 - 108 + 110 1 0 0 @@ -1699,7 +1723,7 @@ 0 20 - 109 + 111 1 0 0 @@ -1711,7 +1735,7 @@ 20 - 110 + 112 1 0 0 @@ -1723,7 +1747,7 @@ 20 - 111 + 113 1 0 0 @@ -1735,7 +1759,7 @@ 20 - 112 + 114 1 0 0 @@ -1747,7 +1771,7 @@ 20 - 113 + 115 1 0 0 @@ -1759,7 +1783,7 @@ 20 - 114 + 116 1 0 0 @@ -1771,7 +1795,7 @@ 20 - 115 + 117 1 0 0 @@ -1783,7 +1807,7 @@ 20 - 116 + 118 1 0 0 @@ -1803,7 +1827,7 @@ 0 21 - 117 + 119 1 0 0 @@ -1815,7 +1839,7 @@ 21 - 118 + 120 1 0 0 @@ -1835,7 +1859,7 @@ 0 22 - 119 + 121 1 0 0 @@ -1847,7 +1871,7 @@ 22 - 120 + 122 1 0 0 @@ -1859,7 +1883,7 @@ 22 - 121 + 123 5 0 0 @@ -1879,7 +1903,7 @@ 0 23 - 122 + 124 1 0 0 @@ -1891,7 +1915,7 @@ 23 - 123 + 125 1 0 0 @@ -1903,7 +1927,7 @@ 23 - 124 + 126 1 0 0 @@ -1915,7 +1939,7 @@ 23 - 125 + 127 1 0 0 @@ -1927,7 +1951,7 @@ 23 - 126 + 128 1 0 0 @@ -1947,7 +1971,7 @@ 0 24 - 127 + 129 1 0 0 @@ -1967,7 +1991,7 @@ 0 25 - 128 + 130 1 0 0 diff --git a/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvprojx b/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvprojx index 5ac54a1a..df6822f6 100644 --- a/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvprojx +++ b/board/EVB_LN882x/KEIL/wifi_demo/TencentOS_tiny.uvprojx @@ -405,6 +405,16 @@ 1 ..\..\..\..\examples\wifi_ln882x\wifi_app_ln882x.c + + tcp_server_echo.c + 1 + ..\..\..\..\examples\wifi_ln882x\tcp_server_echo.c + + + tcp_client_echo.c + 1 + ..\..\..\..\examples\wifi_ln882x\tcp_client_echo.c + diff --git a/examples/wifi_ln882x/tcp_client_echo.c b/examples/wifi_ln882x/tcp_client_echo.c new file mode 100644 index 00000000..17176a69 --- /dev/null +++ b/examples/wifi_ln882x/tcp_client_echo.c @@ -0,0 +1,166 @@ +#include "cmsis_os.h" +#include "tcp_client_echo.h" + +#include "utils/debug/art_assert.h" +#include "utils/debug/log.h" + +#include "lwip/sockets.h" +#include "lwip/netdb.h" +#include "lwip/netifapi.h" + +#define TCP_CLI_ECHO_STK_SIZE 512*3 +void tcp_client_echo_task_entry(void *arg); +osThreadDef(tcp_client_echo_task_entry, osPriorityNormal, 1, TCP_CLI_ECHO_STK_SIZE); + + +#define BUFSZ 1500 +static char recv_data[BUFSZ]; + +void tcp_client_echo_task_entry(void *arg) +{ + int ret; + int conn; + static uint64_t cli_recv_count = 0; + + struct sockaddr_in target_sockaddr; + ip4_addr_t target_ser_ip; + + int optval = 0; + int len = sizeof(int); + struct timeval tv; + + target_ser_t target_ser = *(target_ser_t*)arg; + + + if(!ipaddr_aton((const char *)target_ser.ip, &target_ser_ip)) + { + LOG(LOG_LVL_ERROR, "[%s, %d]Convert target server ip addr failed.\r\n", __func__, __LINE__); + return; + } + + if ((conn = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + LOG(LOG_LVL_ERROR, "[%s, %d]Socket creat error.\r\n", __func__, __LINE__); + return; + } + + tv.tv_sec = 5; + tv.tv_usec = 0; + setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval)); + tv.tv_sec = 5; + tv.tv_usec = 0; + setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(struct timeval)); + + target_sockaddr.sin_family = AF_INET; + target_sockaddr.sin_port = htons(target_ser.port); + target_sockaddr.sin_addr.s_addr = target_ser_ip.addr; + memset(&(target_sockaddr.sin_zero), 0, sizeof(target_sockaddr.sin_zero)); + + while (1) + { + + if(connect(conn, (struct sockaddr *)&target_sockaddr, sizeof(struct sockaddr)) == -1) + { + LOG(LOG_LVL_ERROR, "[%s, %d]Connect target tcp server faild!\r\n", __func__, __LINE__); + OS_MsDelay(3000); + continue; + } + + LOG(LOG_LVL_ERROR, "[%s, %d]Connect target tcp server successful!\r\n", __func__, __LINE__); + + while (1) + { + //---------------recv data-----------------------// + memset(recv_data,0,sizeof(recv_data)); + + ret = recv(conn, recv_data, BUFSZ-1, 0); + getsockopt(conn, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + if (ret < 0) + { + if(optval == EWOULDBLOCK){ + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (recv timeout)\r\n", __func__, __LINE__ ); + continue; + } else if(optval == ECONNABORTED) { + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (conn abort)\r\n", __func__, __LINE__ ); + closesocket(conn); + break; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (err optval = %d)\r\n", __func__, __LINE__,optval); + closesocket(conn); + break; + } + } + else if(ret == 0) + { + if(optval == ERR_OK){ + LOG(LOG_LVL_ERROR, "[%s, %d] recv = 0 ERR_OK\r\n", __func__, __LINE__); + continue; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] recv = 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + else + { + cli_recv_count += ret; + LOG(LOG_LVL_ERROR, "client_recv_count = %u\r\n", cli_recv_count); + } + + //---------------send data-----------------------// + if(!strstr(recv_data,"TencentOS tiny")) + { + ret = send(conn, "tcp client push massage!", strlen("tcp client push massage!"), 0); + OS_MsDelay(2000); + } + else + { + ret = send(conn, recv_data, ret, 0); + } + + getsockopt(conn, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + if (ret < 0) + { + if(optval == EWOULDBLOCK){ + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (send timeout)\r\n", __func__, __LINE__); + continue; + } else if(optval == ECONNABORTED) { + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (conn abort)\r\n", __func__, __LINE__); + closesocket(conn); + break; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + else if(ret == 0) + { + if(optval == ERR_OK){ + LOG(LOG_LVL_ERROR, "[%s, %d] send = 0 ERR_OK\r\n", __func__, __LINE__); + continue; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] send = 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + } + } + + closesocket(conn); + LOG(LOG_LVL_ERROR, "[%s, %d]tcp client sock closed.\r\n", __func__, __LINE__); +} + + +void tcp_client_echo_task_creat(uint8_t * ser_ip, uint32_t port) +{ + target_ser_t ser = {0}; + ser.ip = ser_ip; + ser.port = port; + + osThreadCreate(osThread(tcp_client_echo_task_entry), &ser); +} + + + diff --git a/examples/wifi_ln882x/tcp_client_echo.h b/examples/wifi_ln882x/tcp_client_echo.h new file mode 100644 index 00000000..09d3bea9 --- /dev/null +++ b/examples/wifi_ln882x/tcp_client_echo.h @@ -0,0 +1,16 @@ +#ifndef __TCP_CLIENT_ECHO_H__ +#define __TCP_CLIENT_ECHO_H__ + +#include "stdint.h" + +typedef struct{ + uint8_t * ip; + uint32_t port; +}target_ser_t; + +void tcp_client_echo_task_creat(uint8_t * ser_ip, uint32_t port); + + + +#endif /* __TCP_CLIENT_ECHO_H__ */ + diff --git a/examples/wifi_ln882x/tcp_server_echo.c b/examples/wifi_ln882x/tcp_server_echo.c new file mode 100644 index 00000000..92fc0385 --- /dev/null +++ b/examples/wifi_ln882x/tcp_server_echo.c @@ -0,0 +1,164 @@ +#include "cmsis_os.h" +#include "tcp_server_echo.h" + +#include "utils/debug/art_assert.h" +#include "utils/debug/log.h" + +#include "lwip/sockets.h" +#include "lwip/netdb.h" +#include "lwip/netifapi.h" + +#define TCP_SER_ECHO_STK_SIZE 512*3 +void tcp_server_echo_task_entry(void *arg); +osThreadDef(tcp_server_echo_task_entry, osPriorityNormal, 1, TCP_SER_ECHO_STK_SIZE); + + +#define BUFSZ 1500 +static char recv_data[BUFSZ]; + +void tcp_server_echo_task_entry(void *arg) +{ + socklen_t sin_size; + int server_sock, conn; + struct sockaddr_in server_addr, client_addr; + bool stop = false; + int ret; + + static uint64_t ser_recv_count = 0; + + static int optval = 0; + int len = sizeof(int); + struct timeval tv; + + uint32_t ser_port = *(uint32_t *)arg; + + if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + LOG(LOG_LVL_ERROR, "[%s, %d]create server_sock failed.\r\n", __func__, __LINE__); + return; + } + + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(ser_port); + server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); + + if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){ + LOG(LOG_LVL_ERROR, "[%s, %d]bind error.\r\n", __func__, __LINE__); + closesocket(server_sock); + return; + } + + if (listen(server_sock, 5) == -1){ + getsockopt(server_sock, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + LOG(LOG_LVL_ERROR, "[%s, %d]listen error.optval=%d\r\n", __func__, __LINE__, optval); + closesocket(server_sock); + return; + } + + LOG(LOG_LVL_ERROR, "[%s, %d]listening on port %d\r\n", __func__, __LINE__, ser_port); + + while (stop != true) + { + ser_recv_count = 0; + sin_size = sizeof(struct sockaddr_in); + + if ((conn = accept(server_sock, (struct sockaddr *)&client_addr, &sin_size)) < 0) + { + getsockopt(server_sock, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + LOG(LOG_LVL_ERROR, "[%s, %d]accept failed.optval=%d\r\n", __func__, __LINE__, optval); + break; + } + + LOG(LOG_LVL_ERROR, "[%s, %d]got a conn from (%s , %d)\r\n", __func__, __LINE__, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + + tv.tv_sec = 3; + tv.tv_usec = 0; + setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval)); + tv.tv_sec = 3; + tv.tv_usec = 0; + setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, (char*)&tv, sizeof(struct timeval)); + + while (1) + { + //---------------recv data-----------------------// + memset(recv_data,0,sizeof(recv_data)); + + ret = recv(conn, recv_data, BUFSZ-1, 0); + getsockopt(conn, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + if (ret < 0) + { + if(optval == EWOULDBLOCK){ + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (recv timeout)\r\n", __func__, __LINE__ ); + continue; + } else if(optval == ECONNABORTED) { + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (conn abort)\r\n", __func__, __LINE__ ); + closesocket(conn); + break; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] recv < 0 (err optval = %d)\r\n", __func__, __LINE__,optval); + closesocket(conn); + break; + } + } + else if(ret == 0) + { + if(optval == ERR_OK){ + LOG(LOG_LVL_ERROR, "[%s, %d] recv = 0 ERR_OK\r\n", __func__, __LINE__); + continue; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] recv = 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + else + { + ser_recv_count += ret; + LOG(LOG_LVL_ERROR, "server_recv_count = %u\r\n", ser_recv_count); + } + + //---------------send data-----------------------// + ret = send(conn, recv_data, ret, 0); + getsockopt(conn, SOL_SOCKET, SO_ERROR, &optval, (socklen_t *)&len); + if (ret < 0) + { + if(optval == EWOULDBLOCK){ + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (send timeout)\r\n", __func__, __LINE__); + continue; + } else if(optval == ECONNABORTED) { + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (conn abort)\r\n", __func__, __LINE__); + closesocket(conn); + break; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] send < 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + else if(ret == 0) + { + if(optval == ERR_OK){ + LOG(LOG_LVL_ERROR, "[%s, %d] send = 0 ERR_OK\r\n", __func__, __LINE__); + continue; + } else { + LOG(LOG_LVL_ERROR, "[%s, %d] send = 0 (err optval = %d)\r\n", __func__, __LINE__, optval); + closesocket(conn); + break; + } + } + } + } + + closesocket(server_sock); + LOG(LOG_LVL_ERROR, "[%s, %d]server_sock closed.\r\n", __func__, __LINE__); +} + + +void tcp_server_echo_task_creat(uint32_t port) +{ + uint32_t server_port = port; + osThreadCreate(osThread(tcp_server_echo_task_entry), &server_port); +} + + + diff --git a/examples/wifi_ln882x/tcp_server_echo.h b/examples/wifi_ln882x/tcp_server_echo.h new file mode 100644 index 00000000..e4d88425 --- /dev/null +++ b/examples/wifi_ln882x/tcp_server_echo.h @@ -0,0 +1,12 @@ +#ifndef __TCP_SERVER_ECHO_H__ +#define __TCP_SERVER_ECHO_H__ + +#include "stdint.h" + + +void tcp_server_echo_task_creat(uint32_t port); + + + +#endif /* __TCP_SERVER_ECHO_H__ */ + diff --git a/examples/wifi_ln882x/wifi_app_ln882x.c b/examples/wifi_ln882x/wifi_app_ln882x.c index ffee5876..d0d6b5af 100644 --- a/examples/wifi_ln882x/wifi_app_ln882x.c +++ b/examples/wifi_ln882x/wifi_app_ln882x.c @@ -12,10 +12,15 @@ #include "utils/system_parameter.h" #include "hal/hal_adc.h" +#include "tcp_server_echo.h" +#include "tcp_client_echo.h" + static OS_Thread_t g_temp_cal_thread; #define TEMP_APP_TASK_STACK_SIZE 4*256 //Byte +volatile uint8_t dhcp_get_ip = 0; + void wifi_init_sta(void) { uint8_t macaddr[6] = {0}, macaddr_default[6] = {0}; @@ -147,12 +152,20 @@ void temp_cal_app_task_entry(void *params) } } +void wifi_event_sta_got_ip_cb(wifi_msg_t * msg) +{ + dhcp_get_ip = 1; +}; + void application_entry(void *arg) { + //wifi chip temperature calibration. if(OS_OK != OS_ThreadCreate(&g_temp_cal_thread, "TempAPP", temp_cal_app_task_entry, NULL, OS_PRIORITY_BELOW_NORMAL, TEMP_APP_TASK_STACK_SIZE)) { ART_ASSERT(1); } + + reg_wifi_msg_callbcak(wifi_manager_get_handle(), WIFI_MSG_ID_STA_DHCP_GOT_IP,wifi_event_sta_got_ip_cb); wifi_mode_enum_t wifi_mode = WIFI_MODE_STATION; @@ -180,6 +193,13 @@ void application_entry(void *arg) ethernetif_get_ip_info(if_index, &ip_info); } + while(!dhcp_get_ip){ + OS_MsDelay(1000); + }; + + tcp_server_echo_task_creat(8087); + tcp_client_echo_task_creat((uint8_t *)"120.76.100.197", 10002);//ͨÐÅè²âÊÔ(IP:120.76.100.197) 10002¶Ë¿Ú + while(1) { tos_task_delay(1*1000);