Add tcp client/server echo example. @EVB_LN882x
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -405,6 +405,16 @@
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\examples\wifi_ln882x\wifi_app_ln882x.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>tcp_server_echo.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\examples\wifi_ln882x\tcp_server_echo.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>tcp_client_echo.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\examples\wifi_ln882x\tcp_client_echo.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
|
166
examples/wifi_ln882x/tcp_client_echo.c
Normal file
166
examples/wifi_ln882x/tcp_client_echo.c
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
16
examples/wifi_ln882x/tcp_client_echo.h
Normal file
16
examples/wifi_ln882x/tcp_client_echo.h
Normal file
@@ -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__ */
|
||||
|
164
examples/wifi_ln882x/tcp_server_echo.c
Normal file
164
examples/wifi_ln882x/tcp_server_echo.c
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
12
examples/wifi_ln882x/tcp_server_echo.h
Normal file
12
examples/wifi_ln882x/tcp_server_echo.h
Normal file
@@ -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__ */
|
||||
|
@@ -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);//ͨ<><CDA8>è<EFBFBD><C3A8><EFBFBD><EFBFBD>(IP:120.76.100.197) 10002<30>˿<EFBFBD>
|
||||
|
||||
while(1)
|
||||
{
|
||||
tos_task_delay(1*1000);
|
||||
|
Reference in New Issue
Block a user