forked from xuos/xiuos
fixed socket bug and optimize with lwip flag and other codes
This commit is contained in:
parent
c0de7ca44c
commit
169c3c3c5d
|
@ -38,7 +38,7 @@
|
|||
|
||||
char tcp_socket_ip[] = {192, 168, 250, 252};
|
||||
|
||||
#define TCP_BUF_SIZE 1024
|
||||
#define TCP_DEMO_BUF_SIZE 65535
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
|
@ -46,17 +46,15 @@ char tcp_socket_ip[] = {192, 168, 250, 252};
|
|||
|
||||
static void tcp_recv_demo(void *arg)
|
||||
{
|
||||
lw_print("tcp_recv_demo start.\n");
|
||||
|
||||
int fd = -1;
|
||||
char *recv_buf;
|
||||
struct sockaddr_in tcp_addr, server_addr;
|
||||
int fd = -1, clientfd;
|
||||
int recv_len;
|
||||
char *recv_buf;
|
||||
struct sockaddr_in tcp_addr;
|
||||
socklen_t addr_len;
|
||||
|
||||
while(1)
|
||||
{
|
||||
recv_buf = (char *)malloc(TCP_BUF_SIZE);
|
||||
recv_buf = (char *)malloc(TCP_DEMO_BUF_SIZE);
|
||||
if (recv_buf == NULL)
|
||||
{
|
||||
lw_print("No memory\n");
|
||||
|
@ -81,16 +79,30 @@ static void tcp_recv_demo(void *arg)
|
|||
goto __exit;
|
||||
}
|
||||
|
||||
lw_print("tcp bind sucess, start to receive.\n");
|
||||
lw_print("tcp bind success, start to receive.\n");
|
||||
lw_print("\n\nLocal Port:%d\n\n", LWIP_LOCAL_PORT);
|
||||
|
||||
// setup socket fd as listening mode
|
||||
if (listen(fd, 5) != 0 )
|
||||
{
|
||||
lw_print("Unable to listen\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
// accept client connection
|
||||
clientfd = accept(fd, (struct sockaddr *)&tcp_addr, (socklen_t*)&addr_len);
|
||||
lw_print("client %s connected\n", inet_ntoa(tcp_addr.sin_addr));
|
||||
|
||||
while(1)
|
||||
{
|
||||
memset(recv_buf, 0, TCP_BUF_SIZE);
|
||||
recv_len = recvfrom(fd, recv_buf, TCP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
|
||||
lw_pr_info("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
|
||||
lw_pr_info("Receive data : %s\n\n", recv_buf);
|
||||
sendto(fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
|
||||
memset(recv_buf, 0, TCP_DEMO_BUF_SIZE);
|
||||
recv_len = recvfrom(clientfd, recv_buf, TCP_DEMO_BUF_SIZE, 0, (struct sockaddr *)&tcp_addr, &addr_len);
|
||||
if(recv_len > 0)
|
||||
{
|
||||
lw_pr_info("Receive from : %s\n", inet_ntoa(tcp_addr.sin_addr));
|
||||
lw_pr_info("Receive data : %d - %s\n\n", recv_len, recv_buf);
|
||||
}
|
||||
sendto(clientfd, recv_buf, recv_len, 0, (struct sockaddr*)&tcp_addr, addr_len);
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
@ -116,7 +128,7 @@ void tcp_socket_recv_run(int argc, char *argv[])
|
|||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
sys_thread_new("tcp_recv_demo", tcp_recv_demo, NULL, 4096, 15);
|
||||
sys_thread_new("tcp_recv_demo", tcp_recv_demo, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
|
@ -125,10 +137,11 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) |
|
|||
static void tcp_send_demo(void *arg)
|
||||
{
|
||||
int cnt = LWIP_DEMO_TIMES;
|
||||
lw_print("tcp_send_demo start.\n");
|
||||
int fd = -1;
|
||||
char send_msg[128];
|
||||
|
||||
lw_print("%s start\n", __func__);
|
||||
|
||||
memset(send_msg, 0, sizeof(send_msg));
|
||||
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0)
|
||||
|
@ -150,14 +163,14 @@ static void tcp_send_demo(void *arg)
|
|||
}
|
||||
|
||||
lw_print("tcp connect success, start to send.\n");
|
||||
lw_print("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
|
||||
lw_pr_info("\n\nTarget Port:%d\n\n", tcp_sock.sin_port);
|
||||
|
||||
while (cnt --)
|
||||
{
|
||||
lw_print("Lwip client is running.\n");
|
||||
snprintf(send_msg, sizeof(send_msg), "TCP test package times %d\r\n", cnt);
|
||||
sendto(fd, send_msg, strlen(send_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr));
|
||||
lw_print("Send tcp msg: %s ", send_msg);
|
||||
lw_pr_info("Send tcp msg: %s ", send_msg);
|
||||
MdelayKTask(1000);
|
||||
}
|
||||
|
||||
|
@ -178,8 +191,8 @@ void tcp_socket_send_run(int argc, char *argv[])
|
|||
}
|
||||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
sys_thread_new("tcp socket", tcp_send_demo, NULL, 4096, 25);
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_socket_ip);
|
||||
sys_thread_new("tcp socket", tcp_send_demo, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
* @date 2021.11.11
|
||||
*/
|
||||
|
||||
#include "sys_arch.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/def.h"
|
||||
#include "lwip/mem.h"
|
||||
|
@ -70,9 +71,10 @@
|
|||
#include "lwip/igmp.h"
|
||||
#include "lwip/mld6.h"
|
||||
|
||||
//#define USE_RTOS 1
|
||||
//#define FSL_RTOS_FREE_RTOS
|
||||
//#define FSL_RTOS_XIUOS
|
||||
#ifdef FSL_RTOS_XIUOS
|
||||
#define USE_RTOS 1
|
||||
#define FSL_RTOS_FREE_RTOS
|
||||
#endif
|
||||
|
||||
#if USE_RTOS && defined(FSL_RTOS_FREE_RTOS)
|
||||
|
||||
|
@ -118,8 +120,6 @@ typedef struct EventGroupDef_t
|
|||
|
||||
struct EventGroupDef_t;
|
||||
typedef struct EventGroupDef_t * EventGroupHandle_t;
|
||||
#else
|
||||
int lwip_sempahore;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
@ -189,9 +189,7 @@ static void ethernet_callback(ENET_Type *base, enet_handle_t *handle, enet_event
|
|||
ethernetif_input(netif);
|
||||
break;
|
||||
case kENET_TxEvent:
|
||||
#ifdef FSL_RTOS_XIUOS
|
||||
|
||||
#else
|
||||
#ifndef FSL_RTOS_XIUOS
|
||||
{
|
||||
portBASE_TYPE taskToWake = pdFALSE;
|
||||
|
||||
|
@ -218,8 +216,7 @@ static void ethernet_callback(ENET_Type *base, enet_handle_t *handle, enet_event
|
|||
break;
|
||||
}
|
||||
|
||||
// KSemaphoreAbandon(ethernetif->enetSemaphore);
|
||||
KSemaphoreAbandon(lwip_sempahore);
|
||||
KSemaphoreAbandon(ethernetif->enetSemaphore);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -303,56 +300,6 @@ err_t ethernetif_mld_mac_filter(struct netif *netif, const ip6_addr_t *group,
|
|||
}
|
||||
#endif
|
||||
|
||||
#define netifINTERFACE_TASK_STACK_SIZE ( 4096 )
|
||||
|
||||
/**
|
||||
* This function is the ethernetif_input task, it is processed when a packet
|
||||
* is ready to be read from the interface. It uses the function low_level_input()
|
||||
* that should handle the actual reception of bytes from the network
|
||||
* interface. Then the type of the received packet is determined and
|
||||
* the appropriate input function is called.
|
||||
*
|
||||
* @param netif the lwip network interface structure for this ethernetif
|
||||
*/
|
||||
//void eth_input( void * pvParameters )
|
||||
//{
|
||||
// struct pbuf *p;
|
||||
//
|
||||
// for( ;; )
|
||||
// {
|
||||
// if (KSemaphoreObtain( lwip_sempahore, WAITING_FOREVER)==EOK)
|
||||
// {
|
||||
// p = low_level_input( s_pxNetIf );
|
||||
//
|
||||
// if (ERR_OK != s_pxNetIf->input( p, s_pxNetIf))
|
||||
// {
|
||||
// KPrintf("netif input return not OK ! \n");
|
||||
// pbuf_free(p);
|
||||
// p=NULL;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void low_level_init()
|
||||
//{
|
||||
// /* create the task that handles the ETH_MAC */
|
||||
// uint32 thr_id = KTaskCreate((signed char*) "eth_input",
|
||||
// eth_input,
|
||||
// NULL,
|
||||
// netifINTERFACE_TASK_STACK_SIZE,
|
||||
// 15);
|
||||
// if (thr_id >= 0)
|
||||
// {
|
||||
// StartupKTask(thr_id);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// KPrintf("Eth create failed !");
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes ENET driver.
|
||||
*/
|
||||
|
@ -397,7 +344,6 @@ void ethernetif_enet_init(struct netif *netif, struct ethernetif *ethernetif,
|
|||
{
|
||||
ethernetif->enetSemaphore = KSemaphoreCreate(0);
|
||||
}
|
||||
// lwip_sempahore = KSemaphoreCreate(0);
|
||||
#else
|
||||
ethernetif->enetTransmitAccessEvent = xEventGroupCreate();
|
||||
#endif
|
||||
|
@ -474,7 +420,6 @@ static err_t enet_send_frame(struct ethernetif *ethernetif, unsigned char *data,
|
|||
{
|
||||
#ifdef FSL_RTOS_XIUOS
|
||||
KSemaphoreObtain(ethernetif->enetSemaphore, portMAX_DELAY);
|
||||
// KSemaphoreObtain(lwip_sempahore, portMAX_DELAY);
|
||||
#else
|
||||
xEventGroupWaitBits(ethernetif->enetTransmitAccessEvent, ethernetif->txFlag, pdTRUE, (BaseType_t) false,
|
||||
portMAX_DELAY);
|
||||
|
|
|
@ -84,11 +84,9 @@
|
|||
char lwip_ipaddr[] = {192, 168, 250, 253};
|
||||
char lwip_netmask[] = {255, 255, 255, 0};
|
||||
char lwip_gwaddr[] = {192, 168, 250, 252};
|
||||
|
||||
int is_lwip_test = 0; //for lwip input thread
|
||||
char lwip_flag = 0;
|
||||
|
||||
x_ticks_t lwip_sys_now;
|
||||
static int lwip_init_flag = 0;
|
||||
|
||||
struct sys_timeouts {
|
||||
struct sys_timeo *next;
|
||||
|
@ -491,7 +489,7 @@ void lwip_config_net(char *ip, char *mask, char *gw)
|
|||
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
|
||||
};
|
||||
|
||||
if(lwip_init_flag)
|
||||
if(chk_lwip_bit(LWIP_INIT_FLAG))
|
||||
{
|
||||
lw_print("lw: [%s] already ...\n", __func__);
|
||||
|
||||
|
@ -507,7 +505,7 @@ void lwip_config_net(char *ip, char *mask, char *gw)
|
|||
netif_set_up(&gnetif);
|
||||
return;
|
||||
}
|
||||
lwip_init_flag = 1;
|
||||
set_lwip_bit(LWIP_INIT_FLAG);
|
||||
|
||||
lw_print("lw: [%s] start ...\n", __func__);
|
||||
|
||||
|
@ -522,7 +520,7 @@ void lwip_config_net(char *ip, char *mask, char *gw)
|
|||
netif_set_default(&gnetif);
|
||||
netif_set_up(&gnetif);
|
||||
|
||||
if(is_lwip_test)
|
||||
if(chk_lwip_bit(LWIP_PRINT_FLAG))
|
||||
{
|
||||
lw_pr_info("\r\n************************************************\r\n");
|
||||
lw_pr_info(" Network Configuration\r\n");
|
||||
|
@ -553,12 +551,13 @@ void lwip_config_tcp(char *ip, char *mask, char *gw)
|
|||
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
|
||||
};
|
||||
|
||||
if(lwip_init_flag)
|
||||
if(chk_lwip_bit(LWIP_INIT_FLAG))
|
||||
{
|
||||
lw_print("lw: [%s] already ...\n", __func__);
|
||||
return;
|
||||
}
|
||||
lwip_init_flag = 1;
|
||||
|
||||
set_lwip_bit(LWIP_INIT_FLAG);
|
||||
|
||||
tcpip_init(NULL, NULL);
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@
|
|||
|
||||
#define LWIP_DEMO_TIMES 3
|
||||
#define LWIP_TASK_STACK_SIZE 4096
|
||||
#define LWIP_TASK_PRIO 15
|
||||
#define LWIP_DEMO_TASK_PRIO 20
|
||||
|
||||
/* MAC address configuration. */
|
||||
#define configMAC_ADDR {0x02, 0x12, 0x13, 0x10, 0x15, 0x11}
|
||||
|
@ -82,16 +82,23 @@ typedef int32 sys_mbox_t;
|
|||
typedef int32 sys_thread_t;
|
||||
typedef x_base sys_prot_t;
|
||||
|
||||
#define MS_PER_SYSTICK_F407 1000/TICK_PER_SECOND
|
||||
#define MS_PER_SYSTICK_F407 (1000 / TICK_PER_SECOND)
|
||||
|
||||
//debug rtos with IRQ
|
||||
//#define FSL_RTOS_XIUOS
|
||||
|
||||
extern char lwip_flag;
|
||||
|
||||
#define LWIP_INIT_FLAG (1 << 0)
|
||||
#define LWIP_PRINT_FLAG (1 << 1)
|
||||
|
||||
#define set_lwip_bit(__bit) lwip_flag |= (__bit)
|
||||
#define clr_lwip_bit(__bit) lwip_flag &= ~(__bit)
|
||||
#define chk_lwip_bit(__bit) ((lwip_flag & (__bit)) == (__bit))
|
||||
|
||||
extern char lwip_ipaddr[];
|
||||
extern char lwip_netmask[];
|
||||
extern char lwip_gwaddr[];
|
||||
extern int is_lwip_test;
|
||||
extern int lwip_sempahore;
|
||||
extern struct netif gnetif;
|
||||
|
||||
void lwip_tcp_init(void);
|
||||
|
|
|
@ -133,7 +133,7 @@ void lwip_dhcp_test(void)
|
|||
ETH_BSP_Config();
|
||||
|
||||
lwip_config_net(ip_addr, ip_addr, ip_addr);
|
||||
is_lwip_test = 1;
|
||||
set_lwip_bit(LWIP_PRINT_FLAG);
|
||||
|
||||
dhcp_start(&gnetif);
|
||||
|
||||
|
@ -167,7 +167,7 @@ void lwip_dhcp_test(void)
|
|||
}
|
||||
}
|
||||
|
||||
is_lwip_test = 0;
|
||||
clr_lwip_bit(LWIP_PRINT_FLAG);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -101,11 +101,13 @@ void lwip_ping_thread(int argc, char *argv[])
|
|||
return;
|
||||
}
|
||||
}
|
||||
#if (LWIP_DHCP) && (PING_USE_SOCKETS)
|
||||
else
|
||||
{
|
||||
get_url_ip(argv[1]);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
lw_print("lw: [%s] argc %d\n", __func__, argc);
|
||||
|
|
|
@ -105,7 +105,7 @@ void lwip_tcp_send_run(int argc, char *argv[])
|
|||
|
||||
ETH_BSP_Config();
|
||||
lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr);
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, LWIP_TASK_STACK_SIZE, LWIP_TASK_PRIO);
|
||||
sys_thread_new("tcp send", lwip_tcp_send_thread, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3),
|
||||
|
|
|
@ -105,10 +105,6 @@ static u32_t ping_time;
|
|||
static struct raw_pcb *ping_pcb;
|
||||
#endif /* PING_USE_SOCKETS */
|
||||
|
||||
#define PING_THREAD_STACKSIZE 4096
|
||||
#define PING_THREAD_PRIO 15
|
||||
|
||||
|
||||
/** Prepare a echo ICMP request */
|
||||
static void
|
||||
ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
||||
|
@ -412,7 +408,7 @@ ping_init(const ip_addr_t* ping_addr)
|
|||
ping_target = ping_addr;
|
||||
|
||||
#if PING_USE_SOCKETS
|
||||
th = sys_thread_new("ping_thread", ping_thread, NULL, PING_THREAD_STACKSIZE, PING_THREAD_PRIO);
|
||||
th = sys_thread_new("ping_thread", ping_thread, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
|
||||
lw_print("lw: [%s] new thread %d addr %#x\n", __func__, th, (*ping_addr).addr);
|
||||
#else /* PING_USE_SOCKETS */
|
||||
ping_raw_init();
|
||||
|
@ -476,6 +472,7 @@ int lwip_ping_recv(int s, int *ttl)
|
|||
return len;
|
||||
}
|
||||
|
||||
#if (LWIP_DHCP) && (PING_USE_SOCKETS)
|
||||
int get_url_ip(char* url)
|
||||
{
|
||||
#if LWIP_VERSION_MAJOR >= 2U
|
||||
|
@ -539,6 +536,6 @@ int get_url_ip(char* url)
|
|||
lwip_close(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* LWIP_RAW */
|
||||
|
|
Loading…
Reference in New Issue