Преглед изворни кода

add: netdrv,mreport,遥测添加可指定网络适配器的配置

??? пре 4 месеци
родитељ
комит
7eb94fe561
2 измењених фајлова са 64 додато и 24 уклоњено
  1. 58 19
      components/mreport/src/luat_mreport.c
  2. 6 5
      components/network/netdrv/binding/luat_lib_netdrv.c

+ 58 - 19
components/mreport/src/luat_mreport.c

@@ -25,7 +25,28 @@
 static struct udp_pcb* mreport_pcb;
 static struct udp_pcb* mreport_pcb;
 static luat_rtos_timer_t mreport_timer;
 static luat_rtos_timer_t mreport_timer;
 const char *project_name = "unkonw";             // luatos项目名称
 const char *project_name = "unkonw";             // luatos项目名称
-const char *project_version = "";                   // luatos项目版本号
+const char *project_version = "0.0.0";                   // luatos项目版本号
+static int s_adapter_index = -1;                     // 网络适配器索引
+
+static void luat_mreport_init(lua_State *L) {
+    if (strcmp(project_name, "unkonw") == 0) {
+        lua_getglobal(L, "PROJECT");
+        if (LUA_TSTRING == lua_type(L, -1))
+        {
+            size_t project_len;
+            project_name = luaL_tolstring(L, -1, &project_len);
+        }
+    }
+
+    if (strcmp(project_version, "0.0.0") == 0) {
+        lua_getglobal(L, "VERSION");
+        if (LUA_TSTRING == lua_type(L, -1))
+        {
+            size_t version_len;
+            project_version = luaL_tolstring(L, -1, &version_len);
+        }
+    }
+}
 
 
 static inline uint16_t u162bcd(uint16_t src) {
 static inline uint16_t u162bcd(uint16_t src) {
     uint8_t high = (src >> 8) & 0xFF;
     uint8_t high = (src >> 8) & 0xFF;
@@ -281,22 +302,28 @@ void luat_mreport_send(void) {
     int ret = 0;
     int ret = 0;
     size_t olen = 0;
     size_t olen = 0;
     cJSON* mreport_data = cJSON_CreateObject();
     cJSON* mreport_data = cJSON_CreateObject();
+    int adapter_id = 0;
+
+    if (s_adapter_index == -1) 
+        adapter_id = network_register_get_default();
+    else
+        adapter_id = s_adapter_index;
 
 
-    int adapter_index = network_register_get_default();
-	if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY){
+	if (adapter_id < 0 || adapter_id >= NW_ADAPTER_QTY){
 		LLOGE("尚无已注册的网络适配器");
 		LLOGE("尚无已注册的网络适配器");
         cJSON_Delete(mreport_data);
         cJSON_Delete(mreport_data);
 		return;
 		return;
 	}
 	}
-    luat_netdrv_t* netdrv = luat_netdrv_get(adapter_index);
+    luat_netdrv_t* netdrv = luat_netdrv_get(adapter_id);
     if (netdrv == NULL || netdrv->netif == NULL) {
     if (netdrv == NULL || netdrv->netif == NULL) {
+        LLOGE("当前使用的网络适配器id:%d, 还没初始化", adapter_id);
         cJSON_Delete(mreport_data);
         cJSON_Delete(mreport_data);
         return;
         return;
     }
     }
 
 
     struct netif *netif = netdrv->netif;
     struct netif *netif = netdrv->netif;
     if (ip_addr_isany(&netif->ip_addr)) {
     if (ip_addr_isany(&netif->ip_addr)) {
-        LLOGD("还没联网");
+        LLOGE("当前使用的网络适配器id:%d, 还没分配到ip地址", adapter_id);
         cJSON_Delete(mreport_data);
         cJSON_Delete(mreport_data);
         return;
         return;
     }
     }
@@ -400,23 +427,35 @@ void luat_mreport_config(const char* config, int val) {
             LLOGE("luat_mreport enable %d error", val);
             LLOGE("luat_mreport enable %d error", val);
         }
         }
     }
     }
+    else if (strcmp(config, "adapter_id") == 0) {
+        if (val >= 0 && val < NW_ADAPTER_QTY) {
+			s_adapter_index = val;
+		}
+        else {
+            LLOGE("luat_mreport adapter_id %d error", val);
+            s_adapter_index = network_register_get_default(); // 默认使用默认网络适配器
+        }
+    }
 }
 }
 
 
 int l_mreport_config(lua_State *L) {
 int l_mreport_config(lua_State *L) {
-    char* config = luaL_checkstring(L, 1);
-    int value = lua_toboolean(L, 2);
-    lua_getglobal(L, "PROJECT");
-    size_t version_len, project_len;
-    if (LUA_TSTRING == lua_type(L, -1))
-    {
-    	project_name = luaL_tolstring(L, -1, &project_len);
+    int num_args = lua_gettop(L);
+    if (num_args == 0) {
+        luat_mreport_send();
     }
     }
-    lua_getglobal(L, "VERSION");
-    if (LUA_TSTRING == lua_type(L, -1))
-    {
-    	project_version = luaL_tolstring(L, -1, &version_len);
+    else if (num_args == 2) {
+        char* config = luaL_checkstring(L, 1);
+        int value = -1;
+        if (lua_isboolean(L, 2))
+        {
+            value = lua_toboolean(L, 2);
+        }
+        else if (lua_isnumber(L, 2))
+        {
+            value = lua_tonumber(L, 2);
+        }
+        luat_mreport_init(L);
+        luat_mreport_config(config, value);
     }
     }
-
-    luat_mreport_config(config, value);
     return 0;
     return 0;
-}
+}

+ 6 - 5
components/network/netdrv/binding/luat_lib_netdrv.c

@@ -380,7 +380,7 @@ static int l_netdrv_debug(lua_State *L) {
 }
 }
 
 
 /*
 /*
-设置遥测功能(还未实现全部功能)
+设置遥测功能,开启后,会自动上报设备信息,2025/9/25启用
 @api netdrv.mreport(config, value)
 @api netdrv.mreport(config, value)
 @string 配置项
 @string 配置项
 @boolean 设置功能开关
 @boolean 设置功能开关
@@ -390,13 +390,14 @@ static int l_netdrv_debug(lua_State *L) {
 netdrv.mreport("enable", true)
 netdrv.mreport("enable", true)
 netdrv.mreport("enable", false)
 netdrv.mreport("enable", false)
 
 
+-- 设置使用的网络适配器
+netdrv.mreport("adapter_id", socket.LWIP_GP)
+netdrv.mreport("adapter_id", socket.LWIP_STA)
+netdrv.mreport("adapter_id", socket.LWIP_ETH)
+
 -- 立即上报一次, 无参数的方式调用
 -- 立即上报一次, 无参数的方式调用
 netdrv.mreport()
 netdrv.mreport()
 
 
--- 设置自定义数据
-netdrv.mreport("custom", {abc=1234})
--- 清除自定义数据
-netdrv.mreport("custom")
 */
 */
 extern int l_mreport_config(lua_State* L);
 extern int l_mreport_config(lua_State* L);