Explorar el Código

fix: netdrv,mreport,修正部分问题,但未测试

仍需解决
1. project_name/project_version 不能直接指向lua的字符串, 改成char[]然后memcpy, 高优先级
2. 不要发起基站扫描,获取当前基站信息即可, 高优先级
3. 后续根据air8101的smp代码,适配wifi模组信息的上报, 低优先级.
Wendal Chen hace 4 meses
padre
commit
ea3d345500
Se han modificado 1 ficheros con 49 adiciones y 36 borrados
  1. 49 36
      components/mreport/src/luat_mreport.c

+ 49 - 36
components/mreport/src/luat_mreport.c

@@ -19,31 +19,41 @@
 
 #define MREPORT_DOMAIN "47.94.236.172"
 #define MREPORT_PORT (12388)
-// #define MREPORT_DOMAIN "112.125.89.8"
-// #define MREPORT_PORT (42919)
 
-static struct udp_pcb* mreport_pcb;
-static luat_rtos_timer_t mreport_timer;
-const char *project_name = "unkonw";             // luatos项目名称
-const char *project_version = "0.0.0";                   // luatos项目版本号
-static int s_adapter_index = -1;                     // 网络适配器索引
+typedef struct mreport_ctx {
+    struct udp_pcb* mreport_pcb;
+    luat_rtos_timer_t mreport_timer;
+    const char *project_name;             // luatos项目名称
+    const char *project_version;          // luatos项目版本号
+    int s_adapter_index;                  // 网络适配器索引
+} mreport_ctx_t;
+
+static mreport_ctx_t* s_mreport_ctx;
 
 static void luat_mreport_init(lua_State *L) {
-    if (strcmp(project_name, "unkonw") == 0) {
+    if (s_mreport_ctx == NULL) {
+        s_mreport_ctx = (mreport_ctx_t*)luat_heap_malloc(sizeof(mreport_ctx_t));
+        if (s_mreport_ctx == NULL) {
+            LLOGE("mreport malloc ctx failed");
+            return;
+        }
+        memset(s_mreport_ctx, 0, sizeof(mreport_ctx_t));
+    }
+    if (strcmp(s_mreport_ctx->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);
+            s_mreport_ctx->project_name = luaL_tolstring(L, -1, &project_len);
         }
     }
 
-    if (strcmp(project_version, "0.0.0") == 0) {
+    if (strcmp(s_mreport_ctx->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);
+            s_mreport_ctx->project_version = luaL_tolstring(L, -1, &version_len);
         }
     }
 }
@@ -66,8 +76,8 @@ static void luat_mreport_sys_basic(cJSON* mreport_data) {
     cJSON_AddNumberToObject(mreport_data, "localtime", t);
 
     // luatos项目信息
-    cJSON_AddStringToObject(mreport_data, "proj", project_name);
-    cJSON_AddStringToObject(mreport_data, "pver", project_version);
+    cJSON_AddStringToObject(mreport_data, "proj", s_mreport_ctx->project_name);
+    cJSON_AddStringToObject(mreport_data, "pver", s_mreport_ctx->project_version);
     
     // rndis
     cJSON_AddNumberToObject(mreport_data, "rndis", 0);
@@ -99,7 +109,7 @@ static void luat_mreport_mobile(cJSON* mreport_data) {
 
     // MUID
     char muid[33] = {0};
-    luat_mobile_get_muid(muid, 32);
+    luat_mobile_get_muid(muid, 32); // TODO 如果不是cat1,要从mcu库取.
     muid[32] = '\0';
     cJSON_AddStringToObject(mreport_data, "muid", muid);
 
@@ -155,6 +165,8 @@ static void luat_mreport_sim_network(cJSON* mreport_data, struct netif* netif) {
     }
 
     // 基站/小区
+    // TODO 改成只获取当前服务小区的信息
+    #if 0
     luat_mobile_get_cell_info_async(5);
     luat_mobile_cell_info_t* cell_info = luat_heap_malloc(sizeof(luat_mobile_cell_info_t));
     if (cell_info == NULL) {
@@ -197,6 +209,7 @@ static void luat_mreport_sim_network(cJSON* mreport_data, struct netif* netif) {
     if (cell_info != NULL) {
         luat_heap_free(cell_info);
     }
+    #endif
 
     // ip地址
     cJSON_AddStringToObject(mreport_data, "ipv4", ip4addr_ntoa(&netif->ip_addr));
@@ -304,10 +317,10 @@ void luat_mreport_send(void) {
     cJSON* mreport_data = cJSON_CreateObject();
     int adapter_id = 0;
 
-    if (s_adapter_index == -1) 
+    if (s_mreport_ctx->s_adapter_index == 0) 
         adapter_id = network_register_get_default();
     else
-        adapter_id = s_adapter_index;
+        adapter_id = s_mreport_ctx->s_adapter_index;
 
 	if (adapter_id < 0 || adapter_id >= NW_ADAPTER_QTY){
 		LLOGE("尚无已注册的网络适配器");
@@ -328,9 +341,9 @@ void luat_mreport_send(void) {
         return;
     }
 
-    if (mreport_pcb == NULL) {
-        mreport_pcb = udp_new();
-        if (mreport_pcb == NULL) {
+    if (s_mreport_ctx->mreport_pcb == NULL) {
+        s_mreport_ctx->mreport_pcb = udp_new();
+        if (s_mreport_ctx->mreport_pcb == NULL) {
             LLOGE("创建,mreport udp pcb 失败, 内存不足?");
             cJSON_Delete(mreport_data);
             return;
@@ -338,7 +351,7 @@ void luat_mreport_send(void) {
     }
     // ipaddr_aton("47.94.236.172", &host);
     ipaddr_aton(MREPORT_DOMAIN, &host);
-    ret = udp_connect(mreport_pcb, &host, MREPORT_PORT);
+    ret = udp_connect(s_mreport_ctx->mreport_pcb, &host, MREPORT_PORT);
     if (ret) {
         LLOGD("udp_connect %d", ret);
         cJSON_Delete(mreport_data);
@@ -347,9 +360,9 @@ void luat_mreport_send(void) {
 
     // 基础信息
     luat_mreport_sys_basic(mreport_data);
-    // 模组信息
+    // 模组信息, TODO 有mobile库的才添加
     luat_mreport_mobile(mreport_data);
-    // sim卡和网络相关
+    // sim卡和网络相关, TODO 有mobile库的才添加
     luat_mreport_sim_network(mreport_data, netif);
     // adc信息
     luat_mreport_adc(mreport_data);
@@ -367,21 +380,21 @@ void luat_mreport_send(void) {
         cJSON_Delete(mreport_data);
         return;
     }
-    LLOGE("mreport json --- len: %d\r\n%s", strlen(json), json);
+    // LLOGE("mreport json --- len: %d\r\n%s", strlen(json), json);
 
     struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, strlen(json), PBUF_RAM);
     if (p == NULL) {
         LLOGE("获取pbuf失败 %d", strlen(json));
-        free(json);
+        luat_heap_free(json);
         cJSON_Delete(mreport_data);
         return;
     }
 
     pbuf_take(p, json, strlen(json));
-    memcpy(&mreport_pcb->local_ip, &netif->ip_addr, sizeof(ip_addr_t));
-    ret = udp_sendto_if(mreport_pcb, p, &host, MREPORT_PORT, netif);
+    memcpy(&s_mreport_ctx->mreport_pcb->local_ip, &netif->ip_addr, sizeof(ip_addr_t));
+    ret = udp_sendto_if(s_mreport_ctx->mreport_pcb, p, &host, MREPORT_PORT, netif);
     pbuf_free(p);
-    free(json);
+    luat_heap_free(json);
     cJSON_Delete(mreport_data);
     if (ret) {
         LLOGD("ret %d", ret);
@@ -394,12 +407,12 @@ static void mreport_timer_cb(void* params) {
 }
 
 void luat_mreport_start(void) {
-    int ret = luat_rtos_timer_create(&mreport_timer);
+    int ret = luat_rtos_timer_create(&s_mreport_ctx->mreport_timer);
     if (ret) {
         LLOGE("luat_rtos_timer_create %d", ret);
         return;
     }
-    ret = luat_rtos_timer_start(mreport_timer, 1*60*1000, 1, mreport_timer_cb, NULL);
+    ret = luat_rtos_timer_start(s_mreport_ctx->mreport_timer, 1*60*1000, 1, mreport_timer_cb, NULL);
     if (ret) {
         LLOGE("luat_rtos_timer_start %d", ret);
     }
@@ -407,15 +420,15 @@ void luat_mreport_start(void) {
 }
 
 void luat_mreport_stop(void) {
-    if (mreport_timer) {
-        luat_stop_rtos_timer(mreport_timer);
-        luat_rtos_timer_delete(mreport_timer);
-        mreport_timer = NULL;
+    if (s_mreport_ctx->mreport_timer) {
+        luat_stop_rtos_timer(s_mreport_ctx->mreport_timer);
+        luat_rtos_timer_delete(s_mreport_ctx->mreport_timer);
+        s_mreport_ctx->mreport_timer = NULL;
     }
 }
 
 void luat_mreport_config(const char* config, int val) {
-    LLOGD("luat_mreport_config %s %d", config, val);
+    // LLOGD("luat_mreport_config %s %d", config, val);
     if (strcmp(config, "enable") == 0) {
         if (val == 0) {
             luat_mreport_stop();
@@ -429,11 +442,11 @@ void luat_mreport_config(const char* config, int val) {
     }
     else if (strcmp(config, "adapter_id") == 0) {
         if (val >= 0 && val < NW_ADAPTER_QTY) {
-			s_adapter_index = val;
+			s_mreport_ctx->s_adapter_index = val;
 		}
         else {
             LLOGE("luat_mreport adapter_id %d error", val);
-            s_adapter_index = network_register_get_default(); // 默认使用默认网络适配器
+            s_mreport_ctx->s_adapter_index = network_register_get_default(); // 默认使用默认网络适配器
         }
     }
 }