Browse Source

add: w5500和mobile的IP_READY/IP_LOSE消息增强,以便区分不同的网络设备

Wendal Chen 3 years ago
parent
commit
81b922c2a9

+ 52 - 3
components/ethernet/w5500/luat_lib_w5500.c

@@ -13,6 +13,7 @@
 #include "luat_spi.h"
 #define LUAT_LOG_TAG "w5500"
 #include "luat_log.h"
+#include "luat_msgbus.h"
 
 #include "w5500_def.h"
 #include "luat_network_adapter.h"
@@ -94,7 +95,7 @@ static int l_w5500_config(lua_State *L){
 	{
 		size_t mac_len = 0;
 		const char *mac = luaL_checklstring(L, 4, &mac_len);
-		w5500_set_mac(mac);
+		w5500_set_mac((uint8_t*)mac);
 	}
 
 	w5500_set_param(luaL_optinteger(L, 5, 2000), luaL_optinteger(L, 6, 8), luaL_optinteger(L, 7, 0), 0);
@@ -128,9 +129,9 @@ local mac = w5500.getMAC()
 log.info("w5500 mac", mac:toHex())
 */
 static int l_w5500_get_mac(lua_State *L){
-	uint8_t mac[6];
+	uint8_t mac[6] = {0};
 	w5500_get_mac(mac);
-	lua_pushlstring(L, mac, 6);
+	lua_pushlstring(L, (const char*)mac, 6);
 	return 1;
 }
 
@@ -149,4 +150,52 @@ LUAMOD_API int luaopen_w5500( lua_State *L ) {
     return 1;
 }
 
+static int l_nw_state_handler(lua_State *L, void* ptr) {
+	(void)ptr;
+	rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+	lua_getglobal(L, "sys_pub");
+	if (msg->arg1) {
+/*
+@sys_pub w5500
+已联网
+IP_READY
+@usage
+-- 联网后会发一次这个消息
+sys.subscribe("IP_READY", function(ip, adapter)
+    log.info("w5500", "IP_READY", ip, (adapter or -1) == socket.LWIP_GP)
+end)
+*/
+		lua_pushliteral(L, "IP_READY");
+		uint32_t ip = msg->arg2;
+		lua_pushfstring(L, "%d.%d.%d.%d", (ip) & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
+		lua_pushinteger(L, NW_ADAPTER_INDEX_ETH0);
+		lua_call(L, 3, 0);
+	}
+	else {
+/*
+@sys_pub w5500
+已断网
+IP_LOSE
+@usage
+-- 断网后会发一次这个消息
+sys.subscribe("IP_LOSE", function(adapter)
+    log.info("w5500", "IP_LOSE", (adapter or -1) == socket.ETH0)
+end)
+*/
+		lua_pushliteral(L, "IP_LOSE");
+		lua_pushinteger(L, NW_ADAPTER_INDEX_ETH0);
+		lua_call(L, 2, 0);
+	}
+	return 0;
+}
+
+// W5500的状态回调函数
+void w5500_nw_state_cb(int state, uint32_t ip) {
+	rtos_msg_t msg = {0};
+	msg.handler = l_nw_state_handler;
+	msg.arg1 = state; // READY
+	msg.arg2 = ip;
+	luat_msgbus_put(&msg, 0);
+}
+
 #endif

+ 4 - 0
components/ethernet/w5500/w5500.c

@@ -541,6 +541,8 @@ static int w5500_socket_rx(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data
 	return len;
 }
 
+extern void w5500_nw_state_cb(int state, uint32_t ip);
+
 static void w5500_nw_state(w5500_ctrl_t *w5500)
 {
 	int i;
@@ -554,6 +556,7 @@ static void w5500_nw_state(w5500_ctrl_t *w5500)
 			w5500->socket[0].tx_wait_size = 0;	//dns可以继续发送了
 			w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 1, w5500->self_index);
 			LLOGD("network ready");
+			w5500_nw_state_cb(1, w5500->dhcp_client.temp_ip == 0 ? w5500->static_ip : w5500->dhcp_client.temp_ip);
 			for(i = 0; i < MAX_DNS_SERVER; i++)
 			{
 #ifdef LUAT_USE_LWIP
@@ -580,6 +583,7 @@ static void w5500_nw_state(w5500_ctrl_t *w5500)
 			dns_clear(&w5500->dns_client);
 			w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 0, w5500->self_index);
 			LLOGD("network not ready");
+			w5500_nw_state_cb(0, 0);
 			for(i = 0; i < MAX_SOCK_NUM; i++)
 			{
 				w5500->socket[i].tx_wait_size = 0;

+ 32 - 7
components/mobile/luat_lib_mobile.c

@@ -30,6 +30,7 @@ log.info("simid", mobile.simid())
 #include "luat_msgbus.h"
 
 #include "luat_mobile.h"
+#include "luat_network_adapter.h"
 
 #define LUAT_LOG_TAG "mobile"
 #include "luat_log.h"
@@ -668,6 +669,7 @@ static int l_mobile_event_handle(lua_State* L, void* ptr) {
     LUAT_MOBILE_EVENT_E event;
     uint8_t index;
     uint8_t status;
+    int ret;
 
     rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
     event = msg->arg1;
@@ -765,13 +767,35 @@ end)
 IP_READY
 @usage
 -- 联网后会发一次这个消息
--- 与wlan库不同, 本消息不带ip地址
-sys.subscribe("IP_READY", function()
-    log.info("mobile", "IP_READY")
+sys.subscribe("IP_READY", function(ip, adapter)
+    log.info("mobile", "IP_READY", ip, (adapter or -1) == socket.LWIP_GP)
 end)
 */
             lua_pushstring(L, "IP_READY");
-            lua_call(L, 1, 0);
+            luat_ip_addr_t local_ip, net_mask, gate_way, ipv6;
+            #ifdef LUAT_USE_LWIP
+	        ipv6.type = 0xff;
+	        int ret = network_get_full_local_ip_info(NULL, NW_ADAPTER_INDEX_LWIP_GPRS, &local_ip, &net_mask, &gate_way, &ipv6);
+            #else
+	        void* userdata = NULL;
+	        network_adapter_info* info = network_adapter_fetch(NW_ADAPTER_INDEX_LWIP_GPRS, &userdata);
+	        if (info == NULL)
+		        ret = -1;
+            else
+                ret = info->get_local_ip_info(&local_ip, &net_mask, &gate_way, userdata);
+            #endif
+            if (ret == 0) {
+                #ifdef LUAT_USE_LWIP
+		        lua_pushfstring(L, "%s", ipaddr_ntoa(&local_ip));
+                #else
+                lua_pushfstring(L, "%d.%d.%d.%d", (local_ip.ipv4 >> 24) & 0xFF, (local_ip.ipv4 >> 16) & 0xFF, (local_ip.ipv4 >> 8) & 0xFF, (local_ip.ipv4 >> 0) & 0xFF);
+                #endif
+            }
+            else {
+                lua_pushliteral(L, "0.0.0.0");
+            }
+            lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
+            lua_call(L, 3, 0);
 			break;
         case LUAT_MOBILE_NETIF_LINK_OFF:
             LLOGD("NETIF_LINK_OFF -> IP_LOSE");
@@ -781,12 +805,13 @@ end)
 IP_LOSE
 @usage
 -- 断网后会发一次这个消息
-sys.subscribe("IP_LOSE", function()
-    log.info("mobile", "IP_LOSE")
+sys.subscribe("IP_LOSE", function(adapter)
+    log.info("mobile", "IP_LOSE", (adapter or -1) == socket.LWIP_GP)
 end)
 */
             lua_pushstring(L, "IP_LOSE");
-            lua_call(L, 1, 0);
+            lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
+            lua_call(L, 2, 0);
             break;
 		default:
 			break;

+ 8 - 0
demo/socket/EC618_W5500/main.lua

@@ -65,6 +65,14 @@ w5500.bind(socket.ETH0) -- 固定写法
 --     socket.sntp()
 -- end)
 
+-- 780E和W5500都有IP_READY/IP_LOSE消息,通过adapter区分
+sys.subscribe("IP_READY", function(ip, adapter)
+    log.info("ipready", ip, adapter) 
+end)
+sys.subscribe("IP_LOSE", function(adapter)
+    log.info("iplose", adapter)
+end)
+
 -----------------------------------------------------------------------------
 -- netlab.luatos.com上打开TCP,然后修改IP和端口号,自动回复netlab下发的数据,自收自发测试
 -- 以下端口号均为临时端口, 要改成自己的值