Просмотр исходного кода

update: 调整mobile.scell函数的返回值为table,一次性返回足够多的信息, 并可以容纳更多信息

Wendal Chen 1 год назад
Родитель
Сommit
7e7c95be64
2 измененных файлов с 86 добавлено и 38 удалено
  1. 67 18
      components/mobile/luat_lib_mobile.c
  2. 19 20
      demo/mobile/main.lua

+ 67 - 18
components/mobile/luat_lib_mobile.c

@@ -541,24 +541,82 @@ static int l_mobile_enbid(lua_State* L) {
     return 1;
 }
 
+
+static inline uint16_t u162bcd(uint16_t src) {
+    uint8_t high = (src >> 8) & 0xFF;
+    uint8_t low  = src & 0xFF;
+    uint16_t dst = 0;
+    dst += (low & 0x0F) + (low >> 4) * 10;
+    dst += ((high & 0x0F) + (high >> 4) * 10) * 100;
+    //LLOGD("src %04X dst %d", src, dst);
+    return dst;
+}
+
 /**
 获取当前服务小区更详细的信息
 @api mobile.scell()
-@return int 服务小区的MCC
-@return int 服务小区的MNC
-@return int 服务小区的下行earfcn
-@return int 服务小区的pci
+@return table 服务小区的信息
 @usage
 -- 本API于 2024.9.12 新增
+log.info("cell", json.encode(mobile.scell()))
+-- 返回值示例
+{
+    "mnc": 11,
+    "mcc": 460,
+    "rssi": -78,
+    "pci": 115,
+    "rsrp": -107,
+    "tac": 30005,
+    "eci": 124045360,
+    "rsrq": -9,
+    "snr": 15,
+    "earfcn": 1850
+}
  */
 static int l_mobile_scell_extern_info(lua_State* L) {
-	luat_mobile_scell_extern_info_t info;
-    luat_mobile_get_extern_service_cell_info(&info);
-    lua_pushinteger(L, info.mcc);
-    lua_pushinteger(L, info.mnc);
+	luat_mobile_scell_extern_info_t info = {0};
+    int ret = 0;
+    ret = luat_mobile_get_extern_service_cell_info(&info);
+    if (ret) {
+        return 0;
+    }
+    lua_newtable(L);
+
+    // 驻网信息相关
+    lua_pushinteger(L, u162bcd(info.mcc));
+    lua_setfield(L, -2, "mcc");
+    lua_pushinteger(L, u162bcd(info.mnc));
+    lua_setfield(L, -2, "mnc");
     lua_pushinteger(L, info.earfcn);
+    lua_setfield(L, -2, "earfcn");
     lua_pushinteger(L, info.pci);
-    return 4;
+    lua_setfield(L, -2, "pci");
+
+    // 基站相关
+    uint32_t eci = 0;
+    uint32_t tac = 0;
+    luat_mobile_get_service_cell_identifier(&eci);
+    lua_pushinteger(L, eci);
+    lua_setfield(L, -2, "eci");
+    
+    luat_mobile_get_service_tac_or_lac(&tac);
+    lua_pushinteger(L, tac);
+    lua_setfield(L, -2, "tac");
+
+    // 信号强度相关的值
+    luat_mobile_signal_strength_info_t sinfo = {0};
+    luat_mobile_get_signal_strength_info(&sinfo);
+
+    lua_pushinteger(L, sinfo.lte_signal_strength.snr);
+    lua_setfield(L, -2, "snr");
+    lua_pushinteger(L, sinfo.lte_signal_strength.rsrp);
+    lua_setfield(L, -2, "rsrp");
+    lua_pushinteger(L, sinfo.lte_signal_strength.rsrq);
+    lua_setfield(L, -2, "rsrq");
+    lua_pushinteger(L, sinfo.lte_signal_strength.rssi);
+    lua_setfield(L, -2, "rssi");
+
+    return 1;
 }
 
 /**
@@ -619,15 +677,6 @@ static int l_mobile_status(lua_State* L) {
     return 1;
 }
 
-static inline uint16_t u162bcd(uint16_t src) {
-    uint8_t high = (src >> 8) & 0xFF;
-    uint8_t low  = src & 0xFF;
-    uint16_t dst = 0;
-    dst += (low & 0x0F) + (low >> 4) * 10;
-    dst += ((high & 0x0F) + (high >> 4) * 10) * 100;
-    //LLOGD("src %04X dst %d", src, dst);
-    return dst;
-}
 
 /**
 获取基站信息

+ 19 - 20
demo/mobile/main.lua

@@ -26,8 +26,8 @@ sys.taskInit(function()
 
 	if rtos.bsp() == "UIS8850BM" then
 		sys.wait(2000)
-	end
-
+	end
+
 	log.info("status", mobile.status())
     local band = zbuff.create(40)
     local band1 = zbuff.create(40)
@@ -53,14 +53,14 @@ sys.taskInit(function()
     for i=0,band1:used()-1 do
         log.info("band", band1[i])
     end
-	-- mobile.vsimInit()
-	-- mobile.flymode(nil,true)
-	-- mobile.vsimOnOff(true)
+	-- mobile.vsimInit()
+	-- mobile.flymode(nil,true)
+	-- mobile.vsimOnOff(true)
 	-- mobile.flymode(nil,false)
     -- mobile.apn(0,2,"") -- 使用默认APN激活CID2
     -- mobile.rtime(3) -- 在无数据交互时,RRC 3秒后自动释放
     -- 下面是配置自动搜索小区间隔,和轮询搜索冲突,开启1个就可以了
-    -- mobile.setAuto(10000,30000, 5) -- SIM暂时脱离后自动恢复,30秒搜索一次周围小区信息
+    -- mobile.setAuto(10000,30000, 5) -- SIM暂时脱离后自动恢复,30秒搜索一次周围小区信息
 	log.info("status", mobile.status())
     sys.wait(2000)
     while 1 do
@@ -69,9 +69,9 @@ sys.taskInit(function()
         local sn = mobile.sn()
         if sn then
             log.info("sn",   sn:toHex())
-        end
+        end
 		log.info("status", mobile.status())
-        
+        
 
         log.info("iccid", mobile.iccid())
         log.info("csq", mobile.csq()) -- 4G模块的CSQ并不能完全代表强度
@@ -81,17 +81,16 @@ sys.taskInit(function()
         log.info("snr", mobile.snr())
         log.info("simid", mobile.simid()) -- 这里是获取当前SIM卡槽
         log.info("apn", mobile.apn(0,1))
-        log.info("ip", socket.localIP())
-		log.info("lua", rtos.meminfo())
-        -- sys内存
+        log.info("ip", socket.localIP())
+		log.info("lua", rtos.meminfo())
+        -- sys内存
         log.info("sys", rtos.meminfo("sys"))
         sys.wait(15000)
     end
 end)
--- 订阅式, 模块本身会周期性查询基站信息,但通常不包含临近小区
-sys.subscribe("SCELL_INFO", function()
-	local mcc,mnc,earfcn,pci = mobile.scell()
-    log.info("scell", mcu.x32(mcc), mcu.x32(mnc), earfcn, pci)
+-- 订阅式, 模块本身会周期性查询基站信息,但通常不包含临近小区
+sys.subscribe("SCELL_INFO", function()
+	log.info("cell", json.encode(mobile.scell()))
 end)
 -- 基站数据的查询
 
@@ -102,12 +101,12 @@ end)
 
 -- 轮询式, 包含临近小区信息,这是手动搜索,和上面的自动搜索冲突,开启一个就行
 sys.taskInit(function()
-    sys.wait(5000)
+    sys.wait(5000)
 	mobile.config(mobile.CONF_SIM_WC_MODE, 2)
     while 1 do
         mobile.reqCellInfo(10)
         sys.wait(11000)
-        log.info("cell", json.encode(mobile.getCellInfo()))
+        log.info("cell", json.encode(mobile.getCellInfo()))
 		mobile.config(mobile.CONF_SIM_WC_MODE, 2)
     end
 end)
@@ -118,9 +117,9 @@ sys.subscribe("SIM_IND", function(status, value)
     log.info("sim status", status)
     if status == 'GET_NUMBER' then
         log.info("number", mobile.number(0))
-    end
-	if status == "SIM_WC" then
-        log.info("sim", "write counter", value)
+    end
+	if status == "SIM_WC" then
+        log.info("sim", "write counter", value)
     end
 end)