Browse Source

update:继续兼容中

alienwalker 3 years ago
parent
commit
e4e9ca9c13

+ 7 - 0
components/ethernet/common/platform_def.h

@@ -6,11 +6,18 @@
 #define platform_random	luat_crypto_trng
 #define platform_send_event luat_send_event_to_task
 #define platform_wait_event luat_wait_event_from_task
+
 #define platform_create_task	luat_thread_start
 #define platform_create_timer	luat_create_rtos_timer
 #define platform_start_timer	luat_start_rtos_timer
 #define platform_stop_timer	luat_stop_rtos_timer
 #define platform_release_timer	luat_release_rtos_timer
+
+#define platform_create_mutex	luat_mutex_create
+#define platform_lock_mutex	luat_mutex_lock
+#define platform_unlock_mutex	luat_mutex_unlock
+#define platform_release_mutex	luat_mutex_release
+
 #define malloc 	luat_heap_malloc
 #define free 	luat_heap_free
 #define msleep	luat_timer_mdelay

+ 13 - 4
components/network/adapter/luat_network_adapter.h

@@ -147,6 +147,7 @@ typedef struct
 	uint64_t ack_size;
 	uint64_t tag;
 #ifdef LUAT_USE_TLS
+	//SSL相关数据均为动态生成的,需要在close的时候释放
     mbedtls_ssl_context *ssl;          /**< mbed TLS control context. */
     mbedtls_ssl_config *config;          /**< mbed TLS configuration context. */
     mbedtls_x509_crt *ca_cert;
@@ -161,14 +162,14 @@ typedef struct
 	HANDLE tls_long_timer;
 	uint32_t tcp_keep_idle;
 	int socket_id;
-	char *domain_name;
+	char *domain_name;			//动态生成的,需要在close的时候释放
 	uint32_t domain_name_len;
 	luat_ip_addr_t remote_ip;
-	luat_dns_ip_result *dns_ip;
-	luat_ip_addr_t *online_ip;
+	luat_dns_ip_result *dns_ip;	//动态生成的,需要在close的时候释放
+	luat_ip_addr_t *online_ip;	//指向某个ip,无需释放
 	uint16_t remote_port;
 	uint16_t local_port;
-	uint8_t *cache_data;
+	uint8_t *cache_data;	//动态生成的,需要在close的时候释放
 	uint32_t cache_len;
 	int tls_timer_state;
 	uint32_t tcp_timeou_ms;
@@ -197,6 +198,14 @@ typedef struct
 	llist_head rx_head;
 	uint32_t rx_wait_size;
 	uint32_t tx_wait_size;
+#ifdef LUAT_USE_LWIP
+	union {
+		struct ip_pcb  *ip;
+		struct tcp_pcb *tcp;
+		struct udp_pcb *udp;
+		struct raw_pcb *raw;
+	} pcb;
+#endif
 	void *param;
 	uint8_t state;
 	uint8_t is_tcp;

+ 318 - 236
components/network/lwip/port/luat_lwip.c

@@ -3,7 +3,7 @@
 #include "luat_mcu.h"
 #include "luat_rtos.h"
 #include "luat_network_adapter.h"
-#define MAX_SOCK_NUM MEMP_NUM_TCP_PCB
+#define MAX_SOCK_NUM LWIP_NUM_SOCKETS
 extern LUAT_WEAK void DBG_Printf(const char* format, ...);
 
 #define NET_DBG(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
@@ -14,11 +14,14 @@ enum
 	EV_LWIP_NETIF_INPUT,
 	EV_LWIP_TCP_TIMER,
 	EV_LWIP_COMMON_TIMER,
-	EV_LWIP_DHCP_TIMER,
-	EV_LWIP_FAST_TIMER,
+	EV_LWIP_SOCKET_DNS,
 	EV_LWIP_SOCKET_CONNECT,
+	EV_LWIP_SOCKET_LISTEN,
+	EV_LWIP_SOCKET_ACCPET,
 	EV_LWIP_SOCKET_CLOSE,
 	EV_LWIP_REQUIRE_DHCP,
+	EV_LWIP_DHCP_TIMER,
+	EV_LWIP_FAST_TIMER,
 	EV_LWIP_NETIF_LINK_STATE,
 	EV_LWIP_MLD6_ON_OFF,
 };
@@ -28,19 +31,32 @@ extern struct tcp_pcb *tcp_tw_pcbs;
 
 typedef struct
 {
-	uint64_t last_tx_time;
 	struct netif netif;
-	void *user_data;
-	void *task_handle;
-	void *cb_handle;
 	uint8_t network_ready;
 }lwip_netif_ctrl_t;
 
 typedef struct
 {
+	llist_head node;
+	uint64_t tag;	//考虑到socket复用的问题,必须有tag来做比对
+	luat_ip_addr_t ip;
+	uint8_t *data;
+	uint32_t read_pos;
+	uint32_t len;
+	uint16_t port;
+	uint8_t is_sending;
+}socket_data_t;
+
+typedef struct
+{
+	socket_ctrl_t socket[MAX_SOCK_NUM];
 	lwip_netif_ctrl_t lwip_netif[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
 	uint64_t last_sleep_ms;
-	void *lwip_task_handler;
+	uint64_t socket_tag;
+	CBFuncEx_t socket_cb;
+	void *user_data;
+	void *task_handle;
+	HANDLE socket_mutex;
 	HANDLE tcp_timer;//tcp_tmr
 	HANDLE common_timer;//ip_reass_tmr,etharp_tmr,dns_tmr,nd6_tmr,ip6_reass_tmr
 	HANDLE fast_timer;//igmp_tmr,mld6_tmr,autoip_tmr
@@ -50,6 +66,7 @@ typedef struct
 	uint8_t dhcp_timer_active;
 	uint8_t fast_timer_active;
 	uint8_t dhcp_check_cnt;
+	uint8_t next_socket_index;
 }luat_lwip_ctrl_struct;
 
 static luat_lwip_ctrl_struct prvlwip;
@@ -58,7 +75,7 @@ static void luat_lwip_task(void *param);
 
 static LUAT_RT_RET_TYPE luat_lwip_timer_cb(LUAT_RT_CB_PARAM)
 {
-	luat_send_event_to_task(prvlwip.lwip_task_handler, (uint32_t)param, 0, 0, 0);
+	luat_send_event_to_task(prvlwip.task_handle, (uint32_t)param, 0, 0, 0);
 	return LUAT_RT_RET;
 }
 
@@ -72,27 +89,28 @@ uint32_t luat_lwip_rand()
 void luat_lwip_init(void)
 {
 	luat_thread_t thread;
-	prvlwip.tcp_timer = luat_create_rtos_timer(luat_lwip_timer_cb, (void *)EV_LWIP_TCP_TIMER, 0);
-	prvlwip.common_timer = luat_create_rtos_timer(luat_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
-	prvlwip.fast_timer = luat_create_rtos_timer(luat_lwip_timer_cb, (void *)EV_LWIP_FAST_TIMER, 0);
-	prvlwip.dhcp_timer = luat_create_rtos_timer(luat_lwip_timer_cb, (void *)EV_LWIP_DHCP_TIMER, 0);
+	prvlwip.socket_mutex = platform_create_mutex();
+	prvlwip.tcp_timer = platform_create_timer(luat_lwip_timer_cb, (void *)EV_LWIP_TCP_TIMER, 0);
+	prvlwip.common_timer = platform_create_timer(luat_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
+	prvlwip.fast_timer = platform_create_timer(luat_lwip_timer_cb, (void *)EV_LWIP_FAST_TIMER, 0);
+	prvlwip.dhcp_timer = platform_create_timer(luat_lwip_timer_cb, (void *)EV_LWIP_DHCP_TIMER, 0);
 	tcp_ticks = luat_mcu_tick64_ms() / TCP_SLOW_INTERVAL;
 	prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
 	thread.task_fun = luat_lwip_task;
 	thread.name = "lwip";
 	thread.stack_size = 16 * 1024;
 	thread.priority = 60;
-	luat_thread_start(&thread);
-	prvlwip.lwip_task_handler = thread.handle;
+	platform_create_task(&thread);
+	prvlwip.task_handle = thread.handle;
 	lwip_init();
-	luat_start_rtos_timer(prvlwip.common_timer, 1000, 1);
+	platform_start_timer(prvlwip.common_timer, 1000, 1);
 }
 
 void tcp_timer_needed(void)
 {
   if (!prvlwip.tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
 	  prvlwip.tcpip_tcp_timer_active = 1;
-	  luat_start_rtos_timer(prvlwip.tcp_timer, TCP_TMR_INTERVAL, 1);
+	  platform_start_timer(prvlwip.tcp_timer, TCP_TMR_INTERVAL, 1);
   }
 }
 
@@ -130,9 +148,11 @@ static void luat_lwip_task(void *param)
 			prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
 		}
 		netif = (struct netif *)event.Param3;
+
 		switch(event.ID)
 		{
 		case EV_LWIP_SOCKET_TX:
+//			tcp_output(prvlwip.socket[event.Param1].pcb.tcp);
 			break;
 		case EV_LWIP_NETIF_INPUT:
 			if(netif->input((struct pbuf *)event.Param1, netif) != ERR_OK)
@@ -144,12 +164,12 @@ static void luat_lwip_task(void *param)
 			tcp_tmr();
 			if (tcp_active_pcbs || tcp_tw_pcbs)
 			{
-				;
+
 			}
 			else
 			{
 				prvlwip.tcpip_tcp_timer_active = 0;
-				luat_stop_rtos_timer(prvlwip.tcp_timer);
+				platform_stop_timer(prvlwip.tcp_timer);
 			}
 			break;
 		case EV_LWIP_COMMON_TIMER:
@@ -177,12 +197,21 @@ static void luat_lwip_task(void *param)
 				if (!prvlwip.dhcp_timer_active)
 				{
 					prvlwip.dhcp_timer_active = 1;
-					luat_start_rtos_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
+					platform_start_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
 				}
 			}
 #endif
 			break;
-
+		case EV_LWIP_SOCKET_DNS:
+			break;
+		case EV_LWIP_SOCKET_CONNECT:
+			break;
+		case EV_LWIP_SOCKET_LISTEN:
+			break;
+		case EV_LWIP_SOCKET_ACCPET:
+			break;
+		case EV_LWIP_SOCKET_CLOSE:
+			break;
 		case EV_LWIP_DHCP_TIMER:
 #if LWIP_DHCP
 			dhcp_fine_tmr();
@@ -201,7 +230,7 @@ static void luat_lwip_task(void *param)
 			{
 				NET_DBG("stop dhcp timer!");
 				prvlwip.dhcp_timer_active = 0;
-				luat_stop_rtos_timer(prvlwip.dhcp_timer);
+				platform_stop_timer(prvlwip.dhcp_timer);
 			}
 #endif
 			break;
@@ -240,21 +269,18 @@ static void luat_lwip_task(void *param)
 			{
 				NET_DBG("stop fast timer!");
 				prvlwip.fast_timer_active = 0;
-				luat_stop_rtos_timer(prvlwip.fast_timer);
+				platform_stop_timer(prvlwip.fast_timer);
 			}
 
 			break;
-		case EV_LWIP_SOCKET_CONNECT:
-			break;
-		case EV_LWIP_SOCKET_CLOSE:
-			break;
+
 		case EV_LWIP_REQUIRE_DHCP:
 #if LWIP_DHCP
 			dhcp_start(netif);
 			if (!prvlwip.dhcp_timer_active)
 			{
 				prvlwip.dhcp_timer_active = 1;
-				luat_start_rtos_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
+				platform_start_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
 			}
 
 #endif
@@ -269,7 +295,7 @@ static void luat_lwip_task(void *param)
 				if (!prvlwip.fast_timer_active)
 				{
 					prvlwip.fast_timer_active = 1;
-					luat_start_rtos_timer(prvlwip.fast_timer, 100, 1);
+					platform_start_timer(prvlwip.fast_timer, 100, 1);
 				}
 			}
 			else
@@ -283,148 +309,224 @@ static void luat_lwip_task(void *param)
 
 }
 
-#if 0
+static int luat_lwip_del_data_cache(void *p, void *u)
+{
+	socket_data_t *pdata = (socket_data_t *)p;
+	free(pdata->data);
+	return LIST_DEL;
+}
 
-static int lwip_check_socket(lwip_netif_ctrl_t *lwip, int socket_id, uint64_t tag)
+static int luat_lwip_next_data_cache(void *p, void *u)
 {
-	if (w5500 != prv_w5500_ctrl) return -1;
-	if (socket_id < 1 || socket_id >= MAX_SOCK_NUM) return -1;
-	if (w5500->socket[socket_id].tag != tag) return -1;
-	if (!w5500->socket[socket_id].in_use) return -1;
+	socket_ctrl_t *socket = (socket_ctrl_t *)u;
+	socket_data_t *pdata = (socket_data_t *)p;
+	if (socket->tag != pdata->tag)
+	{
+		DBG("tag error");
+		free(pdata->data);
+		return LIST_DEL;
+	}
+	return LIST_FIND;
+}
+
+
+static socket_data_t * luat_lwip_create_data_node(uint8_t socket_id, uint8_t *data, uint32_t len, luat_ip_addr_t *remote_ip, uint16_t remote_port)
+{
+	socket_data_t *p = (socket_data_t *)malloc(sizeof(socket_data_t));
+	if (p)
+	{
+		memset(p, 0, sizeof(socket_data_t));
+		p->len = len;
+		p->port = remote_port;
+		if (remote_ip)
+		{
+			p->ip = *remote_ip;
+		}
+		else
+		{
+			p->ip.type = 0xff;
+		}
+		p->tag = prvlwip.socket[socket_id].tag;
+		if (data && len)
+		{
+			p->data = malloc(len);
+			if (p->data)
+			{
+				memcpy(p->data, data, len);
+			}
+			else
+			{
+				free(p);
+				return NULL;
+			}
+		}
+	}
+	return p;
+}
+
+static int luat_lwip_check_socket(void *user_data, int socket_id, uint64_t tag)
+{
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
+	if (socket_id >= MAX_SOCK_NUM) return -1;
+	if (prvlwip.socket[socket_id].tag != tag) return -1;
+	if (!prvlwip.socket[socket_id].in_use) return -1;
 	return 0;
 }
 
-static int w5500_socket_check(int socket_id, uint64_t tag, void *user_data)
+static int luat_lwip_socket_check(int socket_id, uint64_t tag, void *user_data)
 {
-	return w5500_check_socket(user_data, socket_id, tag);
+	return luat_lwip_check_socket(user_data, socket_id, tag);
 }
 
 
-static uint8_t w5500_check_ready(void *user_data)
+static uint8_t luat_lwip_check_ready(void *user_data)
 {
-	return ((lwip_netif_ctrl_t *)user_data)->network_ready;
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
+	return prvlwip.lwip_netif[(uint32_t)user_data].network_ready;
 }
 
-static int w5500_create_soceket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
+static int luat_lwip_create_soceket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
 {
-	if (user_data != prv_w5500_ctrl) return -1;
-	int i, socket_id;
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
+	int i, socket_id, error;
 	socket_id = -1;
-	W5500_LOCK;
-	if (!prv_w5500_ctrl->socket[prv_w5500_ctrl->next_socket_index].in_use)
+	platform_lock_mutex(prvlwip.socket_mutex);
+	if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
 	{
-		socket_id = prv_w5500_ctrl->next_socket_index;
-		prv_w5500_ctrl->next_socket_index++;
+		socket_id = prvlwip.next_socket_index;
+		prvlwip.next_socket_index++;
 	}
 	else
 	{
-		for (i = 1; i < MAX_SOCK_NUM; i++)
+		for (i = 0; i < MAX_SOCK_NUM; i++)
 		{
-			if (!prv_w5500_ctrl->socket[i].in_use)
+			if (!prvlwip.socket[i].in_use)
 			{
 				socket_id = i;
-				prv_w5500_ctrl->next_socket_index = i + 1;
+				prvlwip.next_socket_index = i + 1;
 				break;
 			}
 		}
 	}
-	if (prv_w5500_ctrl->next_socket_index >= MAX_SOCK_NUM)
+	if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
 	{
-		prv_w5500_ctrl->next_socket_index = 1;
+		prvlwip.next_socket_index = 0;
 	}
-	if (socket_id > 0)
+	if (socket_id >= 0)
 	{
-		prv_w5500_ctrl->tag++;
-		*tag = prv_w5500_ctrl->tag;
-
-		prv_w5500_ctrl->socket[socket_id].in_use = 1;
-		prv_w5500_ctrl->socket[socket_id].tag = *tag;
-		prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
-		prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
-		prv_w5500_ctrl->socket[socket_id].param = param;
-		prv_w5500_ctrl->socket[socket_id].is_tcp = is_tcp;
-		prv_w5500_ctrl->socket[socket_id].rx_waiting = 0;
-		llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
-		llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
+		prvlwip.socket_tag++;
+		*tag = prvlwip.socket_tag;
+		prvlwip.socket[socket_id].in_use = 1;
+		prvlwip.socket[socket_id].tag = *tag;
+		prvlwip.socket[socket_id].param = param;
+		prvlwip.socket[socket_id].is_tcp = is_tcp;
+		llist_traversal(&prvlwip.socket[socket_id].rx_head, luat_lwip_del_data_cache, NULL);
+		if (prvlwip.socket[socket_id].pcb.ip)
+		{
+			if (prvlwip.socket[socket_id].is_tcp)
+			{
+				error = tcp_close(prvlwip.socket[socket_id].pcb.tcp);
+				if (error)
+				{
+					NET_DBG("tcp close error %d", error);
+				}
+			}
+			else
+			{
+				udp_remove(prvlwip.socket[socket_id].pcb.udp);
+			}
+			prvlwip.socket[socket_id].pcb.ip = NULL;
+		}
 	}
-	W5500_UNLOCK;
+	platform_unlock_mutex(prvlwip.socket_mutex);
 	return socket_id;
 }
 
 //作为client绑定一个port,并连接remote_ip和remote_port对应的server
-static int w5500_socket_connect_ex(int socket_id, uint64_t tag,  uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
+static int luat_lwip_socket_connect_ex(int socket_id, uint64_t tag,  uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
 	PV_Union uPV;
 	uPV.u16[0] = local_port;
 	uPV.u16[1] = remote_port;
-	W5500_LOCK;
-	llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
-	llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
-	W5500_UNLOCK;
-	platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CONNECT, socket_id, remote_ip->ipv4, uPV.u32);
-	uPV.u32 = remote_ip->ipv4;
-//	DBG("%u.%u.%u.%u", uPV.u8[0], uPV.u8[1], uPV.u8[2], uPV.u8[3]);
+	//llist_traversal(&prvlwip.socket[socket_id].rx_head, luat_lwip_del_data_cache, NULL);
+	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, uPV.u32);
 	return 0;
 }
 //作为server绑定一个port,开始监听
-static int w5500_socket_listen(int socket_id, uint64_t tag,  uint16_t local_port, void *user_data)
+static int luat_lwip_socket_listen(int socket_id, uint64_t tag,  uint16_t local_port, void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
-	platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_LISTEN, socket_id, local_port, NULL);
+	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_LISTEN, socket_id, local_port, NULL);
 	return 0;
 }
 //作为server接受一个client
-static int w5500_socket_accept(int socket_id, uint64_t tag,  luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
+static int luat_lwip_socket_accept(int socket_id, uint64_t tag,  luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
 {
-	uint8_t temp[16];
-	int result = w5500_check_socket(user_data, socket_id, tag);
-	if (result) return result;
-	w5500_xfer(user_data, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg, temp, 6);
-	remote_ip->is_ipv6 = 0;
-	remote_ip->ipv4 = BytesGetLe32(temp);
-	*remote_port = BytesGetBe16(temp + 4);
-	DBG("client %d.%d.%d.%d, %u", temp[0], temp[1], temp[2], temp[3], *remote_port);
+//	uint8_t temp[16];
+//	int result = luat_lwip_check_socket(user_data, socket_id, tag);
+//	if (result) return result;
+//	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_ACCPET, socket_id, local_port, NULL);
+//	remote_ip->is_ipv6 = 0;
+//	remote_ip->ipv4 = BytesGetLe32(temp);
+//	*remote_port = BytesGetBe16(temp + 4);
+//	NET_DBG("client %d.%d.%d.%d, %u", temp[0], temp[1], temp[2], temp[3], *remote_port);
 	return 0;
 }
 //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
-static int w5500_socket_disconnect_ex(int socket_id, uint64_t tag,  void *user_data)
+static int luat_lwip_socket_disconnect_ex(int socket_id, uint64_t tag,  void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
-	platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CLOSE, socket_id, 0, 0);
+	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 0, 0);
 	return 0;
 }
 
-static int w5500_socket_force_close(int socket_id, void *user_data)
+static int luat_lwip_socket_force_close(int socket_id, void *user_data)
 {
-	W5500_LOCK;
-	w5500_socket_close(prv_w5500_ctrl, socket_id);
-	if (prv_w5500_ctrl->socket[socket_id].in_use)
+	int error;
+	if (prvlwip.socket[socket_id].pcb.ip)
+	{
+		if (prvlwip.socket[socket_id].is_tcp)
+		{
+			error = tcp_close(prvlwip.socket[socket_id].pcb.tcp);
+			if (error)
+			{
+				NET_DBG("tcp close error %d", error);
+			}
+		}
+		else
+		{
+			udp_remove(prvlwip.socket[socket_id].pcb.udp);
+		}
+		prvlwip.socket[socket_id].pcb.ip = NULL;
+	}
+
+
+	if (prvlwip.socket[socket_id].in_use)
 	{
-		prv_w5500_ctrl->socket[socket_id].in_use = 0;
-		prv_w5500_ctrl->socket[socket_id].tag = 0;
-		llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
-		llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
-		prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
-		prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
-		prv_w5500_ctrl->socket[socket_id].param = NULL;
+		prvlwip.socket[socket_id].in_use = 0;
+		prvlwip.socket[socket_id].tag = 0;
+		prvlwip.socket[socket_id].param = NULL;
+		llist_traversal(&prvlwip.socket[socket_id].rx_head, luat_lwip_del_data_cache, NULL);
 	}
-	W5500_UNLOCK;
 	return 0;
 }
 
-static int w5500_socket_close_ex(int socket_id, uint64_t tag,  void *user_data)
+static int luat_lwip_socket_close_ex(int socket_id, uint64_t tag,  void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
-	return w5500_socket_force_close(socket_id, user_data);
+	platform_lock_mutex(prvlwip.socket_mutex);
+	luat_lwip_socket_force_close(socket_id, user_data);
+	platform_unlock_mutex(prvlwip.socket_mutex);
+	return 0;
 
 }
 
-static uint32_t w5500_socket_read_data(uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
+static uint32_t luat_lwip_socket_read_data(uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
 {
 	uint32_t dummy_len;
 	dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
@@ -440,204 +542,184 @@ static uint32_t w5500_socket_read_data(uint8_t *buf, uint32_t *read_len, uint32_
 	return dummy_len;
 }
 
-static int w5500_socket_receive(int socket_id, uint64_t tag,  uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
+static int luat_lwip_socket_receive(int socket_id, uint64_t tag,  uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
-	W5500_LOCK;
+
 	uint32_t read_len = 0;
 	if (buf)
 	{
-		socket_data_t *p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
+		socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, luat_lwip_next_data_cache, &prvlwip.socket[socket_id]);
 
-		if (prv_w5500_ctrl->socket[socket_id].is_tcp)
+		if (prvlwip.socket[socket_id].is_tcp)
 		{
 			while((read_len < len) && p)
 			{
-				prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
-				p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
+				prvlwip.socket[socket_id].rx_wait_size -= luat_lwip_socket_read_data(buf + read_len, &read_len, len, p);
+				p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, luat_lwip_next_data_cache, &prvlwip.socket[socket_id]);
 			}
 		}
 		else
 		{
-			prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
+			prvlwip.socket[socket_id].rx_wait_size -= luat_lwip_socket_read_data(buf + read_len, &read_len, len, p);
 		}
-		if (llist_empty(&prv_w5500_ctrl->socket[socket_id].rx_head))
+		if (llist_empty(&prvlwip.socket[socket_id].rx_head))
 		{
-			prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
+			prvlwip.socket[socket_id].rx_wait_size = 0;
 		}
 	}
 	else
 	{
-		read_len = prv_w5500_ctrl->socket[socket_id].rx_wait_size;
-	}
-	W5500_UNLOCK;
-	if ((prv_w5500_ctrl->socket[socket_id].rx_wait_size < SOCK_BUF_LEN) && prv_w5500_ctrl->socket[socket_id].rx_waiting)
-	{
-		prv_w5500_ctrl->socket[socket_id].rx_waiting = 0;
-		DBG("read waiting data");
-		w5500_sys_socket_callback(prv_w5500_ctrl, socket_id, Sn_IR_RECV);
+		read_len = prvlwip.socket[socket_id].rx_wait_size;
 	}
+
 	return read_len;
 }
-static int w5500_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
+static int luat_lwip_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
+	int result = luat_lwip_check_socket(user_data, socket_id, tag);
 	if (result) return result;
-	if (prv_w5500_ctrl->socket[socket_id].tx_wait_size >= SOCK_BUF_LEN) return 0;
-
-	socket_data_t *p = w5500_create_data_node(prv_w5500_ctrl, socket_id, buf, len, remote_ip, remote_port);
-	if (p)
-	{
-		W5500_LOCK;
-		llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].tx_head);
-		prv_w5500_ctrl->socket[socket_id].tx_wait_size += len;
-		W5500_UNLOCK;
-		platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_TX, socket_id, 0, 0);
-		result = len;
-	}
-	else
-	{
-		result = -1;
-	}
+	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_TX, socket_id, buf, len);
+	result = len;
 	return result;
 }
 
-void w5500_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
+void luat_lwip_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
 {
-	if (user_data != prv_w5500_ctrl) return;
-	int socket_list[MAX_SOCK_NUM] = {0,0,0,0,0,0,0,0};
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
+	int socket_list[MAX_SOCK_NUM];
+	memset(socket_list, 0, sizeof(socket_list));
 	uint32_t i;
-	for(i = 1; i < num + 1; i++)
+	for(i = 0; i < num + 1; i++)
 	{
 		if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
 		{
 			socket_list[vaild_socket_list[i]] = 1;
 		}
-		DBG("%d,%d",i,vaild_socket_list[i]);
+		NET_DBG("%d,%d",i,vaild_socket_list[i]);
 	}
-	for(i = 1; i < MAX_SOCK_NUM; i++)
+	platform_lock_mutex(prvlwip.socket_mutex);
+	for(i = 0; i < MAX_SOCK_NUM; i++)
 	{
-		DBG("%d,%d",i,socket_list[i]);
+		NET_DBG("%d,%d",i,socket_list[i]);
 		if ( !socket_list[i] )
 		{
-			W5500_LOCK;
-			prv_w5500_ctrl->socket[i].in_use = 0;
-			prv_w5500_ctrl->socket[i].tag = 0;
-			llist_traversal(&prv_w5500_ctrl->socket[i].tx_head, w5500_del_data_cache, NULL);
-			llist_traversal(&prv_w5500_ctrl->socket[i].rx_head, w5500_del_data_cache, NULL);
-			prv_w5500_ctrl->socket[i].rx_wait_size = 0;
-			prv_w5500_ctrl->socket[i].tx_wait_size = 0;
-			w5500_socket_close(prv_w5500_ctrl, i);
-			W5500_UNLOCK;
+			luat_lwip_socket_force_close(i, user_data);
 		}
 	}
+	platform_unlock_mutex(prvlwip.socket_mutex);
 }
 
-static int w5500_getsockopt(int socket_id, uint64_t tag,  int level, int optname, void *optval, uint32_t *optlen, void *user_data)
+static int luat_lwip_getsockopt(int socket_id, uint64_t tag,  int level, int optname, void *optval, uint32_t *optlen, void *user_data)
 {
 	return -1;
 }
-static int w5500_setsockopt(int socket_id, uint64_t tag,  int level, int optname, const void *optval, uint32_t optlen, void *user_data)
+static int luat_lwip_setsockopt(int socket_id, uint64_t tag,  int level, int optname, const void *optval, uint32_t optlen, void *user_data)
 {
 	return -1;
 }
-static int w5500_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
+static int luat_lwip_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
 {
-	if (user_data != prv_w5500_ctrl) return -1;
-
-	if (prv_w5500_ctrl->static_ip)
-	{
-		ip->ipv4 = prv_w5500_ctrl->static_ip;
-		ip->is_ipv6 = 0;
-		submask->ipv4 = prv_w5500_ctrl->static_submask;
-		submask->is_ipv6 = 0;
-		gateway->ipv4 = prv_w5500_ctrl->static_gateway;
-		gateway->is_ipv6 = 0;
-		return 0;
-	}
-	else
-	{
-		if (!prv_w5500_ctrl->ip_ready)
-		{
-			return -1;
-		}
-		ip->ipv4 = prv_w5500_ctrl->dhcp_client.ip;
-		ip->is_ipv6 = 0;
-		submask->ipv4 = prv_w5500_ctrl->dhcp_client.submask;
-		submask->is_ipv6 = 0;
-		gateway->ipv4 = prv_w5500_ctrl->dhcp_client.gateway;
-		gateway->is_ipv6 = 0;
-		return 0;
-	}
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
+
+//	if (prvlwip.static_ip)
+//	{
+//		ip->ipv4 = prvlwip.static_ip;
+//		ip->is_ipv6 = 0;
+//		submask->ipv4 = prvlwip.static_submask;
+//		submask->is_ipv6 = 0;
+//		gateway->ipv4 = prvlwip.static_gateway;
+//		gateway->is_ipv6 = 0;
+//		return 0;
+//	}
+//	else
+//	{
+//		if (!prvlwip.ip_ready)
+//		{
+//			return -1;
+//		}
+//		ip->ipv4 = prvlwip.dhcp_client.ip;
+//		ip->is_ipv6 = 0;
+//		submask->ipv4 = prvlwip.dhcp_client.submask;
+//		submask->is_ipv6 = 0;
+//		gateway->ipv4 = prvlwip.dhcp_client.gateway;
+//		gateway->is_ipv6 = 0;
+//		return 0;
+//	}
 }
 
-static int w5500_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
+static int luat_lwip_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
 {
-	int result = w5500_check_socket(user_data, socket_id, tag);
-	if (result) return result;
-	switch (cmd)
-	{
-	case NW_CMD_AUTO_HEART_TIME:
-		value = (value + 4) / 5;
-		platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_REG_OP, W5500_SOCKET_KEEP_TIME, socket_id, value);
-		break;
-	default:
-		return -1;
-	}
 	return 0;
 }
 
-static int w5500_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
+static int luat_lwip_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
 {
-	if (user_data != prv_w5500_ctrl) return -1;
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
 	char *prv_domain_name = (char *)malloc(len);
 	memcpy(prv_domain_name, domain_name, len);
-	platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_DNS, prv_domain_name, len, param);
+	platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS, prv_domain_name, len, param);
+	return 0;
+}
+
+static int luat_lwip_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
+{
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
+//	if (server_index >= MAX_DNS_SERVER) return -1;
+//	prvlwip.dns_client.dns_server[server_index] = *ip;
+//	prvlwip.dns_client.is_static_dns[server_index] = 1;
 	return 0;
 }
 
-static int w5500_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
+static int luat_lwip_set_mac(uint8_t *mac, void *user_data)
+{
+	return -1;
+}
+static int luat_lwip_set_static_ip(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
+{
+	return -1;
+}
+
+static int32_t luat_lwip_dummy_callback(void *pData, void *pParam)
 {
-	if (user_data != prv_w5500_ctrl) return -1;
-	if (server_index >= MAX_DNS_SERVER) return -1;
-	prv_w5500_ctrl->dns_client.dns_server[server_index] = *ip;
-	prv_w5500_ctrl->dns_client.is_static_dns[server_index] = 1;
 	return 0;
 }
 
-static void w5500_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
-{
-	if (user_data != prv_w5500_ctrl) return ;
-	((lwip_netif_ctrl_t *)user_data)->socket_cb = cb_fun?cb_fun:w5500_dummy_callback;
-	((lwip_netif_ctrl_t *)user_data)->user_data = param;
-}
-
-static network_adapter_info prv_w5500_adapter =
-{
-		.check_ready = lwip_check_ready,
-		.create_soceket = w5500_create_soceket,
-		.socket_connect = w5500_socket_connect_ex,
-		.socket_listen = w5500_socket_listen,
-		.socket_accept = w5500_socket_accept,
-		.socket_disconnect = w5500_socket_disconnect_ex,
-		.socket_close = w5500_socket_close_ex,
-		.socket_force_close = w5500_socket_force_close,
-		.socket_receive = w5500_socket_receive,
-		.socket_send = w5500_socket_send,
-		.socket_check = w5500_socket_check,
-		.socket_clean = w5500_socket_clean,
-		.getsockopt = w5500_getsockopt,
-		.setsockopt = w5500_setsockopt,
-		.user_cmd = w5500_user_cmd,
-		.dns = w5500_dns,
-		.set_dns_server = w5500_set_dns_server,
-		.get_local_ip_info = w5500_get_local_ip_info,
-		.socket_set_callback = w5500_socket_set_callback,
+static void luat_lwip_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
+{
+	if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
+	prvlwip.socket_cb = cb_fun?cb_fun:luat_lwip_dummy_callback;
+	prvlwip.user_data = param;
+}
+
+static network_adapter_info prv_luat_lwip_adapter =
+{
+		.check_ready = luat_lwip_check_ready,
+		.create_soceket = luat_lwip_create_soceket,
+		.socket_connect = luat_lwip_socket_connect_ex,
+		.socket_listen = luat_lwip_socket_listen,
+		.socket_accept = luat_lwip_socket_accept,
+		.socket_disconnect = luat_lwip_socket_disconnect_ex,
+		.socket_close = luat_lwip_socket_close_ex,
+		.socket_force_close = luat_lwip_socket_force_close,
+		.socket_receive = luat_lwip_socket_receive,
+		.socket_send = luat_lwip_socket_send,
+		.socket_check = luat_lwip_socket_check,
+		.socket_clean = luat_lwip_socket_clean,
+		.getsockopt = luat_lwip_getsockopt,
+		.setsockopt = luat_lwip_setsockopt,
+		.user_cmd = luat_lwip_user_cmd,
+		.dns = luat_lwip_dns,
+		.set_dns_server = luat_lwip_set_dns_server,
+		.set_mac = luat_lwip_set_mac,
+		.set_static_ip = luat_lwip_set_static_ip,
+		.get_local_ip_info = luat_lwip_get_local_ip_info,
+		.socket_set_callback = luat_lwip_socket_set_callback,
 		.name = "lwip",
 		.max_socket_num = MAX_SOCK_NUM,
 		.no_accept = 0,
-		.is_posix = 0,
+		.is_posix = 1,
 };
 
-#endif
+

+ 8 - 3
components/network/lwip/port/lwipopts.h

@@ -100,9 +100,8 @@
 #define LWIP_NETBUF_RECVINFO            0
 #define LWIP_TCP                        1
 #define TCP_TTL                         (IP_DEFAULT_TTL)
-#define TCP_WND                         (4 * TCP_MSS)
-#define TCP_MAXRTX                      12
-#define TCP_SYNMAXRTX                   6
+
+
 #define TCP_QUEUE_OOSEQ                 (LWIP_TCP)
 #define LWIP_TCP_SACK_OUT               0
 #define LWIP_TCP_MAX_SACK_NUM           4
@@ -319,6 +318,12 @@ typedef uint32_t sys_prot_t;
 
 #define LWIP_RAND() ((uint32_t)luat_lwip_rand())
 #define LWIP_NUM_SOCKETS	16
+
+#define TCP_MAXRTX                      12
+#define TCP_SYNMAXRTX                   6
+#define TCP_SND_BUF                     (8 * TCP_MSS)
+#define TCP_WND                         (32 * TCP_MSS)
+#define MEM_SIZE 						8192 * 3
 #ifdef LWIP_USER_CONFIG_FILE
 #include LWIP_USER_CONFIG_FILE
 #endif

+ 7 - 0
luat/include/luat_rtos.h

@@ -59,6 +59,10 @@ LUAT_RET luat_sem_delete(luat_sem_t* semaphore);
 LUAT_RET luat_sem_take(luat_sem_t* semaphore,uint32_t timeout);
 LUAT_RET luat_sem_release(luat_sem_t* semaphore);
 
+void *luat_mutex_create(void);
+LUAT_RET luat_mutex_lock(void *mutex);
+LUAT_RET luat_mutex_unlock(void *mutex);
+void luat_mutex_release(void *mutex);
 
 /* ----------------------------------- queue ----------------------------------- */
 
@@ -79,4 +83,7 @@ void *luat_create_rtos_timer(void *cb, void *param, void *task_handle);
 int luat_start_rtos_timer(void *timer, uint32_t ms, uint8_t is_repeat);
 void luat_stop_rtos_timer(void *timer);
 void luat_release_rtos_timer(void *timer);
+
+void luat_task_suspend_all(void);
+void luat_task_resume_all(void);
 #endif