luat_lib_mobile.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. @module mobile
  3. @summary 蜂窝网络
  4. @version 1.0
  5. @date 2022.8.9
  6. @demo mobile
  7. @tag LUAT_USE_MOBILE
  8. @usage
  9. -- 简单演示
  10. log.info("imei", mobile.imei())
  11. log.info("imsi", mobile.imsi())
  12. local sn = mobile.sn()
  13. if sn then
  14. log.info("sn", sn:toHex())
  15. end
  16. log.info("muid", mobile.muid())
  17. log.info("iccid", mobile.iccid())
  18. log.info("csq", mobile.csq())
  19. log.info("rssi", mobile.rssi())
  20. log.info("rsrq", mobile.rsrq())
  21. log.info("rsrp", mobile.rsrp())
  22. log.info("snr", mobile.snr())
  23. log.info("simid", mobile.simid())
  24. */
  25. #include "luat_base.h"
  26. #include "luat_mem.h"
  27. #include "luat_rtos.h"
  28. #include "luat_msgbus.h"
  29. #include "luat_mobile.h"
  30. #include "luat_network_adapter.h"
  31. #define LUAT_LOG_TAG "mobile"
  32. #include "luat_log.h"
  33. extern void luat_cc_start_speech(uint32_t param);
  34. extern void luat_cc_play_tone(uint32_t param);
  35. #ifdef LUAT_USE_AIRLINK
  36. #include "luat_airlink.h"
  37. #endif
  38. /**
  39. 获取IMEI
  40. @api mobile.imei(index)
  41. @int 编号,默认0. 在支持双卡的模块上才会出现0或1的情况
  42. @return string 当前的IMEI值,若失败返回nil
  43. @usgae
  44. -- 注意, 当前所有模块只支持单待,所以IMEI总是同一个
  45. */
  46. static int l_mobile_imei(lua_State* L) {
  47. char buff[24] = {0};
  48. // size_t len = 0;
  49. // size_t wlen = 0;
  50. int ret = 0;
  51. int index = luaL_optinteger(L, 1, 0);
  52. ret = luat_mobile_get_imei(index, buff, 24);
  53. // if (lua_isstring(L, 2)) {
  54. // const char* wbuff = luaL_checklstring(L, 2, &wlen);
  55. // if (wlen >= 15) {
  56. // ret = luat_mobile_set_imei(index, wbuff, wlen);
  57. // LLOGI("IMEI write %d %s ret %d", index, wbuff, ret);
  58. // }
  59. // }
  60. if (ret > 0) {
  61. buff[23] = 0x00; // 确保能结束
  62. lua_pushlstring(L, buff, strlen(buff));
  63. }
  64. else
  65. lua_pushnil(L);
  66. return 1;
  67. }
  68. /**
  69. 获取IMSI
  70. @api mobile.imsi(index)
  71. @int 编号,默认0. 在支持双卡的模块上才会出现0或1的情况
  72. @return string 当前的IMSI值,若失败返回nil
  73. @usgae
  74. -- 注意, 当前所有模块只支持单待,所以IMSI总是同一个
  75. */
  76. static int l_mobile_imsi(lua_State* L) {
  77. char buff[24] = {0};
  78. // size_t len = 0;
  79. // size_t wlen = 0;
  80. int ret = 0;
  81. int index = luaL_optinteger(L, 1, 0);
  82. ret = luat_mobile_get_imsi(index, buff, 24);
  83. // if (lua_isstring(L, 2)) {
  84. // const char* wbuff = luaL_checklstring(L, 2, &wlen);
  85. // if (wlen >= 1) {
  86. // ret = luat_mobile_set_imsi(index, wbuff, wlen);
  87. // LLOGI("IMSI write %d %s ret %d", index, wbuff, ret);
  88. // }
  89. // }
  90. if (ret > 0){
  91. buff[23] = 0x00; // 确保能结束
  92. lua_pushlstring(L, buff, strlen(buff));
  93. }
  94. else
  95. lua_pushnil(L);
  96. return 1;
  97. }
  98. /**
  99. 获取SN
  100. @api mobile.sn()
  101. @return string 当前的SN值,若失败返回nil. 注意, SN可能包含不可见字符
  102. @usage
  103. -- 注意, 出厂未必有写SN
  104. -- 一般用途的唯一id, 可以用mobile.imei()代替
  105. -- 如需要真正的唯一ID, 使用 mcu.unique_id()
  106. */
  107. static int l_mobile_sn(lua_State* L) {
  108. char buff[32] = {0};
  109. // size_t len = 0;
  110. // size_t wlen = 0;
  111. int ret = 0;
  112. ret = luat_mobile_get_sn(buff, 32);
  113. // if (lua_isstring(L, 1)) {
  114. // const char* wbuff = luaL_checklstring(L, 1, &wlen);
  115. // if (wlen >= 1) {
  116. // ret = luat_mobile_set_sn(wbuff, wlen);
  117. // LLOGI("SN write %d %s ret %d", index, wbuff, ret);
  118. // }
  119. // }
  120. if (ret > 0) {
  121. //buff[63] = 0x00; // 确保能结束
  122. lua_pushlstring(L, buff, ret);
  123. }
  124. else
  125. lua_pushnil(L);
  126. return 1;
  127. }
  128. /**
  129. 获取MUID
  130. @api mobile.muid()
  131. @return string 当前的MUID值,若失败返回nil
  132. */
  133. static int l_mobile_muid(lua_State* L) {
  134. char buff[33] = {0};
  135. // size_t len = 0;
  136. // size_t wlen = 0;
  137. int ret = 0;
  138. ret = luat_mobile_get_muid(buff, 32);
  139. if (lua_isstring(L, 1)) {
  140. // const char* wbuff = luaL_checklstring(L, 1, &wlen);
  141. // if (wlen >= 15) {
  142. // ret = luat_mobile_set_muid(index, wbuff, wlen);
  143. // LLOGI("SN write %d %s ret %d", index, wbuff, ret);
  144. // }
  145. }
  146. if (ret > 0) {
  147. lua_pushlstring(L, buff, strlen(buff));
  148. }
  149. else
  150. lua_pushnil(L);
  151. return 1;
  152. }
  153. /**
  154. 获取或设置ICCID
  155. @api mobile.iccid(id)
  156. @int SIM卡的编号, 例如0, 1, 默认0
  157. @return string ICCID值,若失败返回nil
  158. */
  159. static int l_mobile_iccid(lua_State* L) {
  160. char buff[24] = {0};
  161. // size_t len = 0;
  162. // size_t wlen = 0;
  163. int ret = 0;
  164. int index = luaL_optinteger(L, 1, 0);
  165. ret = luat_mobile_get_iccid(index, buff, 24);
  166. if (ret > 0) {
  167. buff[23] = 0x00; // 确保能结束
  168. lua_pushlstring(L, buff, strlen(buff));
  169. }
  170. else
  171. lua_pushnil(L);
  172. return 1;
  173. }
  174. /**
  175. 获取手机卡号,注意,只有写入了手机号才能读出,因此有可能读出来是空的
  176. @api mobile.number(id)
  177. @int SIM卡的编号, 例如0, 1, 默认0
  178. @return string number值,若失败返回nil
  179. */
  180. static int l_mobile_number(lua_State* L) {
  181. char buff[24] = {0};
  182. // size_t len = 0;
  183. // size_t wlen = 0;
  184. int ret = 0;
  185. int index = luaL_optinteger(L, 1, 0);
  186. ret = luat_mobile_get_sim_number(index, buff, 24);
  187. if (ret > 0) {
  188. buff[23] = 0x00; // 确保能结束
  189. lua_pushlstring(L, buff, strlen(buff));
  190. }
  191. else
  192. lua_pushnil(L);
  193. return 1;
  194. }
  195. /**
  196. 获取当前SIM卡槽,或者切换卡槽
  197. @api mobile.simid(id)
  198. @int SIM卡的编号, 例如0, 1, 如果支持双卡,例如Air780EXXX,可以填2来自适应,但是会占用掉4个IO(gpio4/5/6/23)。如果不填就直接读取当前卡槽
  199. @boolean 是否优先用SIM0,只有SIM卡编号写2自适应才有用!!!。true优先用SIM0,false则由具体平台决定,支持双卡双待SIM0优先,不支持的是上一次检测到的优先,默认是false,必须在开机就配置,否则就无效了
  200. @return int 当前sim卡槽编号,若失败返回-1
  201. @usage
  202. mobile.simid(0) -- 固定使用SIM0
  203. mobile.simid(1) -- 固件使用SIM1
  204. mobile.simid(2) -- 自动识别SIM0, SIM1, 优先级看具体平台
  205. mobile.simid(2, true) -- -- 自动识别SIM0, SIM1, 且SIM0优先
  206. -- 提醒, 自动识别是会增加时间的
  207. */
  208. static int l_mobile_simid(lua_State* L) {
  209. // char buff[24] = {0};
  210. int ret = 0;
  211. int id = 0;
  212. if (lua_isinteger(L, 1)) {
  213. id = luaL_checkinteger(L, 1);
  214. ret = luat_mobile_set_sim_id(id);
  215. LLOGI("sim set to %d , ret %d", id, ret);
  216. }
  217. if (LUA_TBOOLEAN == lua_type(L, 2)) {
  218. if (lua_toboolean(L, 2)) {
  219. luat_mobile_set_sim_detect_sim0_first();
  220. }
  221. }
  222. ret = luat_mobile_get_sim_id(&id);
  223. if (ret == 0) {
  224. lua_pushinteger(L, id);
  225. }
  226. else {
  227. lua_pushinteger(L, -1);
  228. }
  229. return 1;
  230. }
  231. /**
  232. 检测当前SIM卡是否准备好,对SIM卡的PIN码做相关操作
  233. @api mobile.simPin(id,operation,pin1,pin2)
  234. @int SIM卡的编号, 例如0, 1, 支持双卡双待的才需要选择
  235. @int PIN码操作类型,只能是mobile.PIN_XXXX,不操作就留空
  236. @string 更换pin时操作的pin码,或者验证操作的pin码,或者解锁pin码时的PUK,4~8字节
  237. @string 更换pin码操作时的新的pin码,解锁pin码时的新PIN,4~8字节
  238. @return boolean 当无PIN操作时,返回SIM卡是否准备好,有PIN操作时,返回是否成功
  239. @usage
  240. local cpin_is_ready = mobile.simPin() -- 当前sim卡是否准备好,一般返回false就是没卡
  241. local succ = mobile.simPin(0, mobile.PIN_VERIFY, "1234") -- 输入pin码验证
  242. */
  243. static int l_mobile_sim_pin(lua_State* L) {
  244. char old[9] = {0};
  245. char new[9] = {0};
  246. int id = luaL_optinteger(L, 1, 0);
  247. int operation = luaL_optinteger(L, 2, -1);
  248. size_t old_len, new_len;
  249. if (lua_isstring(L, 3))
  250. {
  251. const char *old_pin = lua_tolstring(L, 3, &old_len);
  252. memcpy(old, old_pin, (old_len > 8)?8:old_len);
  253. }
  254. if (lua_isstring(L, 4))
  255. {
  256. const char *new_pin = lua_tolstring(L, 4, &new_len);
  257. memcpy(new, new_pin, (new_len > 8)?8:new_len);
  258. }
  259. if (operation != -1)
  260. {
  261. lua_pushboolean(L, (luat_mobile_set_sim_pin(id, operation, old, new) == 0));
  262. }
  263. else
  264. {
  265. lua_pushboolean(L, (luat_mobile_get_sim_ready(id) == 1));
  266. }
  267. return 1;
  268. }
  269. /**
  270. 设置RRC自动释放时间间隔,当开启时后,遇到极弱信号+频繁数据操作可能会引起网络严重故障,因此需要额外设置自动重启协议栈
  271. @api mobile.rtime(time, auto_reset_stack, data_first)
  272. @int RRC自动释放时间,等同于Air724的AT+RTIME,单位秒,写0或者不写则是停用,不要超过20秒,没有意义
  273. @boolean 网络遇到严重故障时尝试自动恢复,和飞行模式/SIM卡切换冲突,true开启,false关闭,留空时,如果设置了时间则自动开启。本参数于2023年9月14日已废弃
  274. @boolean 是否启用数据传输优化,true启用,false关闭,留空为false,开启后必须等到TCP数据ACK或者超时失败,或者socket CONNECT完成(无论成功或者失败)才允许RRC提前释放,可能会增加功耗。本参数于2024年8月12日启用
  275. @return nil 无返回值
  276. @usage
  277. mobile.rtime(3) --与基站无数据交互3秒后提前释放RRC
  278. mobile.rtime(3,nil,true) --启用数据传输优化,与基站无数据交互3秒后,提前释放RRC
  279. */
  280. extern void net_lwip_check_switch(uint8_t onoff);
  281. static int l_mobile_set_rrc_auto_release_time(lua_State* L) {
  282. uint8_t release_time = luaL_optinteger(L, 1, 0);
  283. if (LUA_TBOOLEAN == lua_type(L, 3)) {
  284. net_lwip_check_switch(lua_toboolean(L, 3));
  285. }
  286. uint32_t idle_time = luaL_optinteger(L, 4, 0);
  287. if (idle_time >= 10)
  288. {
  289. luat_mobile_set_auto_rrc(release_time, idle_time);
  290. }
  291. else
  292. {
  293. luat_mobile_set_rrc_auto_release_time(release_time);
  294. }
  295. return 0;
  296. }
  297. /**
  298. 设置一些辅助周期性或者自动功能,目前支持SIM卡暂时脱离后恢复,周期性获取小区信息,网络遇到严重故障时尝试自动恢复
  299. @api mobile.setAuto(check_sim_period, get_cell_period, search_cell_time, auto_reset_stack, network_check_period)
  300. @int SIM卡自动恢复时间,单位毫秒,建议5000~10000,和飞行模式/SIM卡切换冲突,不能再同一时间使用,必须错开执行。写0或者不写则是关闭功能
  301. @int 周期性获取小区信息的时间间隔,单位毫秒。获取小区信息会增加部分功耗。写0或者不写则是关闭功能
  302. @int 每次搜索小区时最大搜索时间,单位秒。不要超过8秒
  303. @boolean 网络遇到严重故障时尝试自动恢复,和飞行模式/SIM卡切换冲突,true开启,false关闭,开始状态是false,留空则不做改变
  304. @int 设置定时检测网络是否正常并且在检测到长时间无网时通过重启协议栈来恢复,无网恢复时长,单位ms,建议60000以上,为网络搜索网络保留足够的时间,留空则不做更改
  305. @return nil 无返回值
  306. */
  307. static int l_mobile_set_auto_work(lua_State* L) {
  308. luat_mobile_set_period_work(luaL_optinteger(L, 2, 0), luaL_optinteger(L, 1, 0), luaL_optinteger(L, 3, 0));
  309. if (LUA_TBOOLEAN == lua_type(L, 4)) {
  310. luat_mobile_fatal_error_auto_reset_stack(lua_toboolean(L, 4));
  311. }
  312. if (lua_isinteger(L, 5)) {
  313. luat_mobile_set_check_network_period(luaL_optinteger(L, 5, 0));
  314. }
  315. return 0;
  316. }
  317. /**
  318. 获取或设置APN,设置APN必须在入网前就设置好,比如在SIM卡识别完成前就设置好
  319. @api mobile.apn(index, cid, new_apn_name, user_name, password, ip_type, protocol, is_del)
  320. @int 编号,默认0. 在支持双卡的模块上才会出现0或1的情况
  321. @int cid, 默认0,如果要用非默认APN来激活,必须>0
  322. @string 新的APN,不填就是获取APN, 填了就是设置APN, 是否支持设置取决于底层实现
  323. @string 新的APN的username,如果APN不是空,那必须填写,如果没有留个空字符串""。如果APN是空的,那可以nil
  324. @string 新的APN的password,如果APN不是空,那必须填写,如果没有留个空字符串""。如果APN是空的,那可以nil
  325. @int 激活APN时的IP TYPE,1=IPV4 2=IPV6 3=IPV4V6,默认是1
  326. @int 激活APN时,如果需要username和password,就要写鉴权协议类型,1~3,默认3,代表1和2都尝试一下。不需要鉴权的写0
  327. @boolean 是否删除APN,true是,其他都否,只有参数3新的APN不是string的时候才有效果
  328. @return string 如果网络注册成功,返回注册用的APN值,反之是nil。设置好不会立刻有返回值,需要等网络注册成功
  329. @usage
  330. -- 注意, 在国内, 公网卡基本上都不需要设置APN, 专网卡才需要设置
  331. mobile.apn(0,1,"cmiot","","",nil,0)
  332. -- 专网卡设置的demo,name,user,password联系卡商获取
  333. -- 设置后, 再次立即获取, 并不会返回设置的值, 要等联网成功 - 设置好不会立刻有返回值,需要等网络注册成功
  334. mobile.apn(0,1,"name","user","password",nil,3)
  335. */
  336. static int l_mobile_apn(lua_State* L) {
  337. char buff[64] = {0};
  338. // size_t len = 0;
  339. size_t wlen = 0;
  340. int ret = 0;
  341. int index = luaL_optinteger(L, 1, 0);
  342. int cid = luaL_optinteger(L, 2, 0);
  343. ret = luat_mobile_get_apn(index, cid, buff, sizeof(buff) - 1);
  344. if (lua_isstring(L, 3)) {
  345. const char* wbuff = luaL_checklstring(L, 3, &wlen);
  346. size_t user_name_len = 0;
  347. size_t password_len = 0;
  348. const char* user_name = luaL_checklstring(L, 4, &user_name_len);
  349. const char* password = luaL_checklstring(L, 5, &password_len);
  350. uint8_t ip_type = luaL_optinteger(L, 6, 1);
  351. uint8_t protocol = luaL_optinteger(L, 7, 3);
  352. if (!user_name_len && !password_len)
  353. {
  354. protocol = 0;
  355. }
  356. if (wlen) {
  357. luat_mobile_user_apn_auto_active(index, cid, ip_type, protocol, wbuff, wlen, user_name, user_name_len, password, password_len);
  358. }
  359. else
  360. {
  361. luat_mobile_user_apn_auto_active(index, cid, ip_type, 0xff, NULL, 0, NULL, 0, NULL, 0);
  362. }
  363. }
  364. else
  365. {
  366. if (lua_isboolean(L, 8) && lua_toboolean(L, 8))
  367. {
  368. luat_mobile_del_apn(index, cid, 0);
  369. }
  370. }
  371. if (ret > 0) {
  372. lua_pushlstring(L, buff, strlen(buff));
  373. }
  374. else
  375. lua_pushnil(L);
  376. return 1;
  377. }
  378. /**
  379. 是否默认开启IPV6功能,必须在LTE网络连接前就设置好
  380. @api mobile.ipv6(onff)
  381. @boolean 开关 true开启 false 关闭
  382. @return boolean true 当前是开启的,false 当前是关闭的
  383. @usage
  384. -- 注意, 开启ipv6后, 开机联网会慢2~3秒
  385. */
  386. static int l_mobile_ipv6(lua_State* L) {
  387. // char buff[24] = {0};
  388. // uint8_t onoff;
  389. if (LUA_TBOOLEAN == lua_type(L, 1)) {
  390. luat_mobile_set_default_pdn_ipv6(lua_toboolean(L, 1));
  391. }
  392. lua_pushboolean(L, luat_mobile_get_default_pdn_ipv6());
  393. return 1;
  394. }
  395. /**
  396. 获取csq
  397. @api mobile.csq()
  398. @return int 当前CSQ值, 若失败返回0. 范围 0 - 31, 越大越好
  399. @usage
  400. -- 注意, 4G模块的CSQ值仅供参考, rsrp/rsrq才是真正的信号强度指标
  401. */
  402. static int l_mobile_csq(lua_State* L) {
  403. // luat_mobile_signal_strength_info_t info = {0};
  404. uint8_t csq = 0;
  405. if (luat_mobile_get_signal_strength(&csq) == 0) {
  406. lua_pushinteger(L, (int)csq);
  407. }
  408. else {
  409. lua_pushinteger(L, 0);
  410. }
  411. return 1;
  412. }
  413. /**
  414. 获取rssi
  415. @api mobile.rssi()
  416. @return int 当前rssi值,若失败返回0. 范围 0 到 -114, 越小越好
  417. */
  418. static int l_mobile_rssi(lua_State* L) {
  419. luat_mobile_signal_strength_info_t info = {0};
  420. if (luat_mobile_get_signal_strength_info(&info) == 0) {
  421. lua_pushinteger(L, info.lte_signal_strength.rssi);
  422. }
  423. else {
  424. lua_pushinteger(L, 0);
  425. }
  426. return 1;
  427. }
  428. /**
  429. 获取rsrp,参考信号接收功率
  430. @api mobile.rsrp()
  431. @return int 当前rsrp值,若失败返回0. 取值范围: -44 ~ -140 ,值越大越好
  432. */
  433. static int l_mobile_rsrp(lua_State* L) {
  434. luat_mobile_signal_strength_info_t info = {0};
  435. if (luat_mobile_get_signal_strength_info(&info) == 0) {
  436. lua_pushinteger(L, info.lte_signal_strength.rsrp);
  437. }
  438. else {
  439. lua_pushinteger(L, 0);
  440. }
  441. return 1;
  442. }
  443. /**
  444. 获取rsrq,参考信号发送功率
  445. @api mobile.rsrq()
  446. @return int 当前rsrq值,若失败返回0. 取值范围: -3 ~ -19.5 ,值越大越好
  447. */
  448. static int l_mobile_rsrq(lua_State* L) {
  449. luat_mobile_signal_strength_info_t info = {0};
  450. if (luat_mobile_get_signal_strength_info(&info) == 0) {
  451. lua_pushinteger(L, info.lte_signal_strength.rsrq);
  452. }
  453. else {
  454. lua_pushinteger(L, 0);
  455. }
  456. return 1;
  457. }
  458. /**
  459. 获取snr,信噪比
  460. @api mobile.snr()
  461. @return int 当前snq值,若失败返回0.范围 0 - 30, 越大越好
  462. */
  463. static int l_mobile_snr(lua_State* L) {
  464. luat_mobile_signal_strength_info_t info = {0};
  465. if (luat_mobile_get_signal_strength_info(&info) == 0) {
  466. lua_pushinteger(L, info.lte_signal_strength.snr);
  467. }
  468. else {
  469. lua_pushinteger(L, 0);
  470. }
  471. return 1;
  472. }
  473. /**
  474. 获取当前服务小区的ECI(E-UTRAN Cell Identifier)
  475. @api mobile.eci()
  476. @return int 当前eci值,若失败返回-1
  477. */
  478. static int l_mobile_eci(lua_State* L) {
  479. uint32_t eci;
  480. if (luat_mobile_get_service_cell_identifier(&eci) == 0) {
  481. lua_pushinteger(L, eci);
  482. }
  483. else {
  484. lua_pushinteger(L, -1);
  485. }
  486. return 1;
  487. }
  488. /**
  489. 获取当前服务小区的TAC或者LAC
  490. @api mobile.tac()
  491. @return int 当前eci值,若失败返回-1. 如果尚未注册到网络,会返回0
  492. @usage
  493. -- 本API于 2023.7.9 新增
  494. */
  495. static int l_mobile_tac(lua_State* L) {
  496. uint16_t tac;
  497. if (luat_mobile_get_service_tac_or_lac(&tac) == 0) {
  498. lua_pushinteger(L, tac);
  499. }
  500. else {
  501. lua_pushinteger(L, -1);
  502. }
  503. return 1;
  504. }
  505. /**
  506. 获取当前服务小区的eNBID(eNodeB Identifier)
  507. @api mobile.enbid()
  508. @return int 当前enbid值,若失败返回-1
  509. */
  510. static int l_mobile_enbid(lua_State* L) {
  511. uint32_t eci;
  512. if (luat_mobile_get_service_cell_identifier(&eci) == 0) {
  513. lua_pushinteger(L, eci>>8);
  514. }
  515. else {
  516. lua_pushinteger(L, -1);
  517. }
  518. return 1;
  519. }
  520. static inline uint16_t u162bcd(uint16_t src) {
  521. uint8_t high = (src >> 8) & 0xFF;
  522. uint8_t low = src & 0xFF;
  523. uint16_t dst = 0;
  524. dst += (low & 0x0F) + (low >> 4) * 10;
  525. dst += ((high & 0x0F) + (high >> 4) * 10) * 100;
  526. //LLOGD("src %04X dst %d", src, dst);
  527. return dst;
  528. }
  529. /**
  530. 获取当前服务小区更详细的信息
  531. @api mobile.scell()
  532. @return table 服务小区的信息
  533. @usage
  534. -- 本API于 2024.9.12 新增
  535. log.info("cell", json.encode(mobile.scell()))
  536. -- 返回值示例
  537. {
  538. "mnc": 11,
  539. "mcc": 460,
  540. "rssi": -78,
  541. "pci": 115,
  542. "rsrp": -107,
  543. "tac": 30005,
  544. "eci": 124045360,
  545. "cid": 124045360,
  546. "rsrq": -9,
  547. "snr": 15,
  548. "earfcn": 1850
  549. }
  550. */
  551. static int l_mobile_scell_extern_info(lua_State* L) {
  552. luat_mobile_scell_extern_info_t info = {0};
  553. int ret = 0;
  554. ret = luat_mobile_get_extern_service_cell_info(&info);
  555. if (ret) {
  556. return 0;
  557. }
  558. lua_newtable(L);
  559. // 驻网信息相关
  560. lua_pushinteger(L, u162bcd(info.mcc));
  561. lua_setfield(L, -2, "mcc");
  562. lua_pushinteger(L, u162bcd(info.mnc));
  563. lua_setfield(L, -2, "mnc");
  564. lua_pushinteger(L, info.earfcn);
  565. lua_setfield(L, -2, "earfcn");
  566. lua_pushinteger(L, info.pci);
  567. lua_setfield(L, -2, "pci");
  568. // 基站相关
  569. uint32_t eci = 0;
  570. uint16_t tac = 0;
  571. luat_mobile_get_service_cell_identifier(&eci);
  572. lua_pushinteger(L, eci);
  573. lua_setfield(L, -2, "eci");
  574. lua_pushinteger(L, eci);
  575. lua_setfield(L, -2, "cid");
  576. luat_mobile_get_service_tac_or_lac(&tac);
  577. lua_pushinteger(L, tac);
  578. lua_setfield(L, -2, "tac");
  579. // 信号强度相关的值
  580. luat_mobile_signal_strength_info_t sinfo = {0};
  581. luat_mobile_get_signal_strength_info(&sinfo);
  582. lua_pushinteger(L, sinfo.lte_signal_strength.snr);
  583. lua_setfield(L, -2, "snr");
  584. lua_pushinteger(L, sinfo.lte_signal_strength.rsrp);
  585. lua_setfield(L, -2, "rsrp");
  586. lua_pushinteger(L, sinfo.lte_signal_strength.rsrq);
  587. lua_setfield(L, -2, "rsrq");
  588. lua_pushinteger(L, sinfo.lte_signal_strength.rssi);
  589. lua_setfield(L, -2, "rssi");
  590. return 1;
  591. }
  592. /**
  593. 进出飞行模式
  594. @api mobile.flymode(index, enable)
  595. @int 编号,默认0. 在支持双卡的模块上才会出现0或1的情况
  596. @bool 是否设置为飞行模式,true为设置, false为退出,可选
  597. @return bool 原飞行模式的状态
  598. */
  599. static int l_mobile_flymode(lua_State* L) {
  600. int index = luaL_optinteger(L, 1, 0);
  601. int flymode = luat_mobile_get_flymode(index);
  602. if (lua_isboolean(L, 2)) {
  603. luat_mobile_set_flymode(index, lua_toboolean(L, 2));
  604. }
  605. lua_pushboolean(L, flymode == 0 ? 0 : 1);
  606. return 1;
  607. }
  608. /**
  609. 配置基站同步时间开关,默认开启
  610. @api mobile.syncTime(enable)
  611. @bool 开启,true开启, false关闭, nil不设置
  612. @return bool 当前开关状态
  613. @usage
  614. mobile.syncTime() --获取当前开关状态
  615. mobile.syncTime(false) --关闭基站同步时间
  616. */
  617. static int l_mobile_sync_time(lua_State* L) {
  618. if (lua_isboolean(L, 1)) {
  619. luat_mobile_set_sync_time(lua_toboolean(L, 1));
  620. }
  621. lua_pushboolean(L, luat_mobile_get_sync_time());
  622. return 1;
  623. }
  624. /**
  625. 获取网络状态
  626. @api mobile.status()
  627. @return int 当前网络状态
  628. @usage
  629. -- 状态描述
  630. -- 0:网络未注册
  631. -- 1:网络已注册
  632. -- 2:正在搜网中
  633. -- 3:网络注册被拒绝
  634. -- 4:网络状态未知
  635. -- 5:漫游,且已注册
  636. -- 6:仅SMS可用
  637. -- 7:仅SMS可用,且漫游状态
  638. -- 8:仅紧急呼叫. 注意, 国内不支持此状态,模块也不支持紧急呼叫
  639. -- 不推荐使用本API判断联网状态, 建议使用socket.localIP()来判断
  640. */
  641. static int l_mobile_status(lua_State* L) {
  642. int LUAT_MOBILE_REGISTER_STATUS_E = luat_mobile_get_register_status();
  643. lua_pushinteger(L, LUAT_MOBILE_REGISTER_STATUS_E);
  644. return 1;
  645. }
  646. /**
  647. 获取基站信息
  648. @api mobile.getCellInfo()
  649. @return table 包含基站数据的数组
  650. @usage
  651. -- 注意: 从2023.06.20开始, 需要主动请求一次reqCellInfo才会有基站数据.
  652. --示例输出(原始数据是table, 下面是json格式化后的内容)
  653. --[[
  654. [
  655. {"rsrq":-10,"rssi":-55,"cid":124045360,"mnc":17,"pci":115,"earfcn":1850,"snr":15,"rsrp":-85,"mcc":1120,"tdd":0},
  656. {"pci":388,"rsrq":-11,"mnc":17,"earfcn":2452,"snr":5,"rsrp":-67,"mcc":1120,"cid":124045331},
  657. {"pci":100,"rsrq":-9,"mnc":17,"earfcn":75,"snr":17,"rsrp":-109,"mcc":1120,"cid":227096712}
  658. ]
  659. ]]
  660. mobile.reqCellInfo(60)
  661. -- 订阅
  662. sys.subscribe("CELL_INFO_UPDATE", function()
  663. log.info("cell", json.encode(mobile.getCellInfo()))
  664. end)
  665. -- 定期轮训式
  666. sys.taskInit(function()
  667. sys.wait(3000)
  668. while 1 do
  669. mobile.reqCellInfo(15)
  670. sys.waitUntil("CELL_INFO_UPDATE", 15000)
  671. log.info("cell", json.encode(mobile.getCellInfo()))
  672. end
  673. end)
  674. */
  675. static int l_mobile_get_cell_info(lua_State* L) {
  676. lua_newtable(L);
  677. luat_mobile_cell_info_t* info = luat_heap_malloc(sizeof(luat_mobile_cell_info_t));
  678. if (info == NULL) {
  679. LLOGE("out of memory when malloc cell_info");
  680. return 1;
  681. }
  682. int ret = luat_mobile_get_last_notify_cell_info(info);
  683. if (ret != 0) {
  684. LLOGI("none cell info found %d", ret);
  685. goto exit;
  686. }
  687. //LLOGD("cid %d neighbor %d", info->lte_service_info.cid, info->lte_neighbor_info_num);
  688. // 当前仅返回lte信息
  689. if (info->lte_info_valid == 0 || info->lte_service_info.cid == 0) {
  690. if (0 == luat_mobile_get_service_cell_identifier(&info->lte_service_info.cid) && info->lte_service_info.cid) {
  691. LLOGW("请先调用 mobile.reqCellInfo()!!");
  692. }
  693. else {
  694. LLOGI("lte cell info not found");
  695. }
  696. goto exit;
  697. }
  698. lua_newtable(L);
  699. lua_pushinteger(L, info->lte_service_info.pci);
  700. lua_setfield(L, -2, "pci");
  701. lua_pushinteger(L, info->lte_service_info.cid);
  702. lua_setfield(L, -2, "cid");
  703. lua_pushinteger(L, info->lte_service_info.earfcn);
  704. lua_setfield(L, -2, "earfcn");
  705. lua_pushinteger(L, info->lte_service_info.rsrp);
  706. lua_setfield(L, -2, "rsrp");
  707. lua_pushinteger(L, info->lte_service_info.rsrq);
  708. lua_setfield(L, -2, "rsrq");
  709. lua_pushinteger(L, info->lte_service_info.rssi);
  710. lua_setfield(L, -2, "rssi");
  711. lua_pushinteger(L, info->lte_service_info.is_tdd);
  712. lua_setfield(L, -2, "tdd");
  713. lua_pushinteger(L, info->lte_service_info.snr);
  714. lua_setfield(L, -2, "snr");
  715. lua_pushinteger(L, u162bcd(info->lte_service_info.mcc));
  716. lua_setfield(L, -2, "mcc");
  717. lua_pushinteger(L, u162bcd(info->lte_service_info.mnc));
  718. lua_setfield(L, -2, "mnc");
  719. lua_pushinteger(L, info->lte_service_info.tac);
  720. lua_setfield(L, -2, "tac");
  721. lua_pushinteger(L, info->lte_service_info.band);
  722. lua_setfield(L, -2, "band");
  723. lua_pushinteger(L, info->lte_service_info.ulbandwidth);
  724. lua_setfield(L, -2, "ulbandwidth");
  725. lua_pushinteger(L, info->lte_service_info.dlbandwidth);
  726. lua_setfield(L, -2, "dlbandwidth");
  727. lua_seti(L, -2, 1);
  728. if (info->lte_neighbor_info_num > 0) {
  729. for (size_t i = 0; i < info->lte_neighbor_info_num; i++)
  730. {
  731. lua_settop(L, 1);
  732. //LLOGD("add neighbor %d", i);
  733. lua_newtable(L);
  734. lua_pushinteger(L, info->lte_info[i].pci);
  735. lua_setfield(L, -2, "pci");
  736. lua_pushinteger(L, info->lte_info[i].cid);
  737. lua_setfield(L, -2, "cid");
  738. if (0x8850 == info->version)
  739. {
  740. lua_pushinteger(L, info->lte_info[i].rssi);
  741. lua_setfield(L, -2, "rssi");
  742. lua_pushinteger(L, info->lte_info[i].celltype);
  743. lua_setfield(L, -2, "celltype");
  744. lua_pushinteger(L, info->lte_info[i].bandwidth);
  745. lua_setfield(L, -2, "bandwidth");
  746. }
  747. lua_pushinteger(L, info->lte_info[i].earfcn);
  748. lua_setfield(L, -2, "earfcn");
  749. lua_pushinteger(L, info->lte_info[i].rsrp);
  750. lua_setfield(L, -2, "rsrp");
  751. lua_pushinteger(L, info->lte_info[i].rsrq);
  752. lua_setfield(L, -2, "rsrq");
  753. lua_pushinteger(L, u162bcd(info->lte_info[i].mcc));
  754. lua_setfield(L, -2, "mcc");
  755. lua_pushinteger(L, u162bcd(info->lte_info[i].mnc));
  756. lua_setfield(L, -2, "mnc");
  757. lua_pushinteger(L, info->lte_info[i].snr);
  758. lua_setfield(L, -2, "snr");
  759. lua_pushinteger(L, info->lte_info[i].tac);
  760. lua_setfield(L, -2, "tac");
  761. lua_seti(L, -2, i + 2);
  762. }
  763. }
  764. lua_settop(L, 1);
  765. exit:
  766. luat_heap_free(info);
  767. return 1;
  768. }
  769. /**
  770. 发起基站信息查询,含临近小区
  771. @api mobile.reqCellInfo(timeout)
  772. @int 超时时长,单位秒,默认15. 最少5, 最高60
  773. @return nil 无返回值
  774. @usage
  775. -- 参考 mobile.getCellInfo 函数
  776. */
  777. static int l_mobile_request_cell_info(lua_State* L) {
  778. int timeout = luaL_optinteger(L, 1, 15);
  779. if (timeout > 60)
  780. timeout = 60;
  781. else if (timeout < 5)
  782. timeout = 5;
  783. luat_mobile_get_cell_info_async(timeout);
  784. return 0;
  785. }
  786. /**
  787. 锁定/解锁小区,仅用于外场测试,没接触过的,或者生产环境中请勿使用
  788. @api mobile.lockCell(mode, earfcn, pci)
  789. @int 操作码 0删除优先的频点,1设置优先频点,2锁定小区,3解锁小区
  790. @int 下行频点
  791. @int phycellid
  792. @return bool 成功true 失败false
  793. @usage
  794. mobile.lockCell(2,1860,32) --锁定小区
  795. mobile.lockCell(3) --解锁小区
  796. */
  797. static int l_mobile_lock_cell(lua_State* L) {
  798. lua_pushboolean(L, !luat_mobile_lock_cell(luaL_optinteger(L, 1, 4), luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, 0)));
  799. return 1;
  800. }
  801. /**
  802. 重启协议栈
  803. @api mobile.reset()
  804. @return nil 无返回值
  805. @usage
  806. -- 重启LTE协议栈
  807. mobile.reset()
  808. */
  809. static int l_mobile_reset(lua_State* L) {
  810. luat_mobile_reset_stack();
  811. return 0;
  812. }
  813. /**
  814. 数据量流量处理
  815. @api mobile.dataTraffic(clearUplink, clearDownlink)
  816. @boolean 清空上行流量累计值,true清空,其他忽略
  817. @boolean 清空下行流量累计值,true清空,其他忽略
  818. @return int 上行流量GB
  819. @return int 上行流量B
  820. @return int 下行流量GB
  821. @return int 下行流量B
  822. @usage
  823. -- 获取上下行流量累计值
  824. -- 上行流量值Byte = uplinkGB * 1024 * 1024 * 1024 + uplinkB
  825. -- 下行流量值Byte = downlinkGB * 1024 * 1024 * 1024 + downlinkB
  826. local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
  827. -- 清空上下行流量累计值
  828. mobile.dataTraffic(true, true)
  829. -- 仅记录开机后的流量,复位/重启会归零
  830. */
  831. static int l_mobile_data_traffic(lua_State* L) {
  832. uint64_t uplink;
  833. uint64_t downlink;
  834. uint8_t clear_uplink = 0;
  835. uint8_t clear_downlink = 0;
  836. volatile uint32_t temp;
  837. if (LUA_TBOOLEAN == lua_type(L, 1)) {
  838. clear_uplink = lua_toboolean(L, 1);
  839. }
  840. if (LUA_TBOOLEAN == lua_type(L, 2)) {
  841. clear_downlink = lua_toboolean(L, 2);
  842. }
  843. luat_mobile_get_ip_data_traffic(&uplink, &downlink);
  844. if (clear_uplink || clear_downlink) {
  845. luat_mobile_clear_ip_data_traffic(clear_uplink, clear_downlink);
  846. }
  847. temp = (uint32_t)(uplink >> 30);
  848. lua_pushinteger(L, temp);
  849. temp = (((uint32_t)uplink) & 0x3FFFFFFF);
  850. lua_pushinteger(L, temp);
  851. temp = (uint32_t)(downlink >> 30);
  852. lua_pushinteger(L, temp);
  853. temp = (((uint32_t)downlink) & 0x3FFFFFFF);
  854. lua_pushinteger(L, temp);
  855. return 4;
  856. }
  857. /**
  858. 网络特殊配置
  859. @api mobile.config(item, value)
  860. @int 配置项目,看mobile.CONF_XXX
  861. @int 配置值,根据具体配置的item决定
  862. @return boolean 是否成功
  863. @usage
  864. --针对不同平台有不同的配置,谨慎使用,目前只有Air780EXXX系列支持
  865. -- Air780EXXX配置小区重选信号差值门限,不能大于15dbm,必须在飞行模式下才能用
  866. mobile.flymode(0,true)
  867. mobile.config(mobile.CONF_RESELTOWEAKNCELL, 15)
  868. mobile.config(mobile.CONF_STATICCONFIG, 1) --开启网络静态优化
  869. mobile.flymode(0,false)
  870. -- Air780EXXX设置SIM写入次数的统计
  871. -- 关闭统计
  872. mobile.config(mobile.CONF_SIM_WC_MODE, 0)
  873. -- 开启统计, 默认也是开启的.
  874. mobile.config(mobile.CONF_SIM_WC_MODE, 1)
  875. -- 读取统计值,异步, 需要通过系统消息SIM_IND获取
  876. sys.subscribe("SIM_IND", function(stats, value)
  877. log.info("SIM_IND", stats)
  878. if stats == "SIM_WC" then
  879. log.info("sim", "write counter", value)
  880. end
  881. end)
  882. mobile.config(mobile.CONF_SIM_WC_MODE, 2)
  883. -- 清空统计值
  884. mobile.config(mobile.CONF_SIM_WC_MODE, 3)
  885. */
  886. static int l_mobile_config(lua_State* L) {
  887. uint8_t item = luaL_optinteger(L, 1, 0);
  888. uint32_t value = luaL_optinteger(L, 2, 0);
  889. if (!item)
  890. {
  891. lua_pushboolean(L, 0);
  892. }
  893. else
  894. {
  895. lua_pushboolean(L, !luat_mobile_config(item, value));
  896. }
  897. return 1;
  898. }
  899. #include "luat_uart.h"
  900. #include "luat_zbuff.h"
  901. /**
  902. 获取当前使用/支持的band
  903. @api mobile.getBand(band, is_default)
  904. @zbuff 输出band
  905. @boolean true默认支持,false当前支持的,默认是false,当前是预留功能,不要写true
  906. @return boolean 成功返回true,失败放回false
  907. @usage
  908. local buff = zbuff.create(40)
  909. mobile.getBand(buff) --输出当前使用的band,band号放在buff内,buff[0],buff[1],buff[2] .. buff[buff:used() - 1]
  910. log.info("当前使用的band:")
  911. --轮训方式打印所用band
  912. for i=0,band:used()-1 do
  913. log.info("band", band[i])
  914. end
  915. */
  916. static int l_mobile_get_band(lua_State* L) {
  917. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  918. uint8_t total_num;
  919. int re;
  920. if (buff->len < 40)
  921. {
  922. __zbuff_resize(buff, 40);
  923. }
  924. if (lua_isboolean(L, 2) && lua_toboolean(L, 2))
  925. {
  926. re = luat_mobile_get_support_band(buff->addr, &total_num);
  927. }
  928. else
  929. {
  930. re = luat_mobile_get_band(buff->addr, &total_num);
  931. }
  932. buff->used = total_num;
  933. lua_pushboolean(L, !re);
  934. return 1;
  935. }
  936. /**
  937. 设置使用的band
  938. @api mobile.setBand(band, num)
  939. @zbuff 输入使用的band
  940. @int band数量
  941. @return boolean 成功返回true,失败放回false
  942. @usage
  943. local buff = zbuff.create(40)
  944. buff[0] = 3
  945. buff[1] = 5
  946. buff[2] = 8
  947. buff[3] = 40
  948. mobile.setBand(buff, 4) --设置使用的band一共4个,为3,5,8,40
  949. */
  950. static int l_mobile_set_band(lua_State* L) {
  951. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  952. int num = luaL_optinteger(L, 2, 1);
  953. lua_pushboolean(L, !luat_mobile_set_band(buff->addr, num));
  954. return 1;
  955. }
  956. /**
  957. RF测试开关和配置
  958. @api mobile.nstOnOff(onoff, uart_id)
  959. @boolean true开启测试模式,false关闭
  960. @int 串口号
  961. @return nil 无返回值
  962. @usage
  963. mobile.nstOnOff(true, uart.VUART_0) --打开测试模式,并且用虚拟串口发送结果
  964. mobile.nstOnOff(false) --关闭测试模式
  965. */
  966. static int l_mobile_nst_test_onoff(lua_State* L) {
  967. luat_mobile_rf_test_mode(luaL_optinteger(L, 2, LUAT_VUART_ID_0), lua_toboolean(L, 1));
  968. return 0;
  969. }
  970. /**
  971. RF测试数据输入
  972. @api mobile.nstInput(data)
  973. @string or zbuff 用户从串口获取的数据,注意,当获取完所有数据后,需要再传一个nil来作为传输结束
  974. @return nil 无返回值
  975. @usage
  976. mobile.nstInput(uart_data)
  977. mobile.nstInput(nil)
  978. */
  979. static int l_mobile_nst_data_input(lua_State* L) {
  980. size_t len = 0;
  981. const char *buf = NULL;
  982. if(lua_isuserdata(L, 1))
  983. {
  984. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  985. len = buff->used;
  986. buf = buff->addr;
  987. }
  988. else if (lua_isstring(L, 1))
  989. {
  990. buf = lua_tolstring(L, 1, &len);//取出字符串数据
  991. }
  992. luat_mobile_rf_test_input(buf, len);
  993. return 0;
  994. }
  995. /**
  996. 初始化内置默认虚拟卡功能(不可用)
  997. @api mobile.vsimInit()
  998. @return nil 无返回值
  999. @usage
  1000. mobile.vsimInit()
  1001. */
  1002. static int l_mobile_init_vsim(lua_State* L) {
  1003. luat_mobile_softsim_init_default();
  1004. return 0;
  1005. }
  1006. /**
  1007. 切换内置虚拟卡和外置实体卡,2024年8月13日启用,虚拟卡需要固件支持,否则切换后无网络,需要在飞行模式下切换,或者切换后重启协议栈
  1008. @api mobile.vsimOnOff(enable)
  1009. @bool 开启,true开启, false关闭
  1010. @return nil 无返回值
  1011. @usage
  1012. mobile.vsimOnOff(true) --使用内置虚拟卡
  1013. mobile.vsimOnOff(false) --使用外置实体卡
  1014. */
  1015. static int l_mobile_vsim_onoff(lua_State* L) {
  1016. if (lua_isboolean(L, 1)) {
  1017. luat_mobile_softsim_onoff(lua_toboolean(L, 1));
  1018. }
  1019. return 0;
  1020. }
  1021. /**
  1022. 初始化自定义APN列表,主要用于海外SIM卡
  1023. @api mobile.apnTableInit()
  1024. @return nil 无返回值
  1025. @usage
  1026. mobile.apnTableInit()
  1027. */
  1028. static int l_mobile_init_apn_table(lua_State* L) {
  1029. luat_mobile_init_auto_apn_by_plmn();
  1030. return 0;
  1031. }
  1032. /**
  1033. 往自定义APN列表添加一条APN信息,主要用于海外SIM卡
  1034. @api mobile.apnTableAdd(mcc, mnc, ip_type, protocol, apn_name, user_name, password)
  1035. @int MCC码,16进制BCD码
  1036. @int MNC码,16进制BCD码
  1037. @int 激活APN时的IP TYPE,1=IPV4 2=IPV6 3=IPV4V6,默认是1
  1038. @int 激活APN时,如果需要username和password,就要写鉴权协议类型,1~3,默认3,代表1和2都尝试一下。不需要鉴权的写0
  1039. @string APN name,不能为空
  1040. @string APN的username
  1041. @string APN的password
  1042. @return nil 无返回值
  1043. @usage
  1044. mobile.apnTableInit() -- 先初始化,必须放在SIM卡识别完成前加入,最好就是写在开头
  1045. mobile.apnTableAdd(0x460,0x00,3,0,"cmiot","","") -- 单独添加一条APN信息,必须放在SIM卡识别完成前加入,最好就是写在开头,移动公网卡设置APN为cmiot(一般不用设置,这里只是举个例子)
  1046. */
  1047. static int l_mobile_add_apn_table(lua_State* L) {
  1048. size_t name_len = 0;
  1049. size_t user_len = 0;
  1050. size_t password_len = 0;
  1051. uint16_t mcc = luaL_optinteger(L, 1, 0x460);
  1052. uint16_t mnc = luaL_optinteger(L, 2, 0);
  1053. uint8_t ip_type = luaL_optinteger(L, 3, 3);
  1054. uint8_t protocol = luaL_optinteger(L, 4, 3);
  1055. const char* name = luaL_checklstring(L, 5, &name_len);
  1056. const char* user = luaL_optlstring(L, 6, "", &user_len);
  1057. const char* password = luaL_optlstring(L, 7, "", &password_len);
  1058. luat_mobile_add_auto_apn_item(mcc, mnc, ip_type, protocol, name, name_len, user, user_len, password, password_len, 1);
  1059. return 0;
  1060. }
  1061. /**
  1062. 打印自定义APN列表里的一条信息,在没有拿到卡的情况下,测试一下对应的APN信息是否和运营商提供的匹配
  1063. @api mobile.apnTablePrint(mcc, mnc)
  1064. @int MCC码,16进制BCD码
  1065. @int MNC码,16进制BCD码
  1066. @return nil 无返回值
  1067. @usage
  1068. mobile.apnTableInit()
  1069. mobile.apnTablePrint(0x202, 0x01)
  1070. */
  1071. static int l_mobile_print_apn_table(lua_State* L) {
  1072. uint16_t mcc = luaL_optinteger(L, 1, 0x460);
  1073. uint16_t mnc = luaL_optinteger(L, 2, 0);
  1074. luat_mobile_print_apn_by_mcc_mnc(mcc, mnc);
  1075. return 0;
  1076. }
  1077. #include "rotable2.h"
  1078. static const rotable_Reg_t reg_mobile[] = {
  1079. {"status", ROREG_FUNC(l_mobile_status)},
  1080. {"imei", ROREG_FUNC(l_mobile_imei)},
  1081. {"imsi", ROREG_FUNC(l_mobile_imsi)},
  1082. {"sn", ROREG_FUNC(l_mobile_sn)},
  1083. {"iccid", ROREG_FUNC(l_mobile_iccid)},
  1084. {"number", ROREG_FUNC(l_mobile_number)},
  1085. {"muid", ROREG_FUNC(l_mobile_muid)},
  1086. {"apn", ROREG_FUNC(l_mobile_apn)},
  1087. {"ipv6", ROREG_FUNC(l_mobile_ipv6)},
  1088. {"csq", ROREG_FUNC(l_mobile_csq)},
  1089. {"rssi", ROREG_FUNC(l_mobile_rssi)},
  1090. {"rsrq", ROREG_FUNC(l_mobile_rsrq)},
  1091. {"rsrp", ROREG_FUNC(l_mobile_rsrp)},
  1092. {"snr", ROREG_FUNC(l_mobile_snr)},
  1093. {"eci", ROREG_FUNC(l_mobile_eci)},
  1094. {"tac", ROREG_FUNC(l_mobile_tac)},
  1095. {"scell", ROREG_FUNC(l_mobile_scell_extern_info)},
  1096. {"enbid", ROREG_FUNC(l_mobile_enbid)},
  1097. {"flymode", ROREG_FUNC(l_mobile_flymode)},
  1098. {"simid", ROREG_FUNC(l_mobile_simid)},
  1099. {"simPin", ROREG_FUNC(l_mobile_sim_pin)},
  1100. {"rtime", ROREG_FUNC(l_mobile_set_rrc_auto_release_time)},
  1101. {"setAuto", ROREG_FUNC(l_mobile_set_auto_work)},
  1102. {"getCellInfo", ROREG_FUNC(l_mobile_get_cell_info)},
  1103. {"reqCellInfo", ROREG_FUNC(l_mobile_request_cell_info)},
  1104. {"lockCell", ROREG_FUNC(l_mobile_lock_cell)},
  1105. {"reset", ROREG_FUNC(l_mobile_reset)},
  1106. {"dataTraffic", ROREG_FUNC(l_mobile_data_traffic)},
  1107. {"config", ROREG_FUNC(l_mobile_config)},
  1108. {"getBand", ROREG_FUNC(l_mobile_get_band)},
  1109. {"setBand", ROREG_FUNC(l_mobile_set_band)},
  1110. {"nstOnOff", ROREG_FUNC(l_mobile_nst_test_onoff)},
  1111. {"nstInput", ROREG_FUNC(l_mobile_nst_data_input)},
  1112. {"syncTime", ROREG_FUNC(l_mobile_sync_time)},
  1113. {"vsimInit", ROREG_FUNC(l_mobile_init_vsim)},
  1114. {"vsimOnOff", ROREG_FUNC(l_mobile_vsim_onoff)},
  1115. {"apnTableInit", ROREG_FUNC(l_mobile_init_apn_table)},
  1116. {"apnTableAdd", ROREG_FUNC(l_mobile_add_apn_table)},
  1117. {"apnTablePrint", ROREG_FUNC(l_mobile_print_apn_table)},
  1118. //@const UNREGISTER number 未注册
  1119. {"UNREGISTER", ROREG_INT(LUAT_MOBILE_STATUS_UNREGISTER)},
  1120. //@const REGISTERED number 已注册
  1121. {"REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_REGISTERED)},
  1122. //@const SEARCH number 正在搜索中
  1123. {"SEARCH", ROREG_INT(LUAT_MOBILE_STATUS_SEARCHING)},
  1124. //@const DENIED number 注册被拒绝
  1125. {"DENIED", ROREG_INT(LUAT_MOBILE_STATUS_DENIED)},
  1126. //@const UNKNOW number 未知
  1127. {"UNKNOW", ROREG_INT(LUAT_MOBILE_STATUS_UNKNOW)},
  1128. //@const REGISTERED_ROAMING number 已注册,漫游
  1129. {"REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_REGISTERED_ROAMING)},
  1130. //@const SMS_ONLY_REGISTERED number 已注册,仅SMS
  1131. {"SMS_ONLY_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED)},
  1132. //@const SMS_ONLY_REGISTERED_ROAMING number 已注册,漫游,仅SMS
  1133. {"SMS_ONLY_REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED_ROAMING)},
  1134. //@const EMERGENCY_REGISTERED number 已注册,紧急服务
  1135. {"EMERGENCY_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_EMERGENCY_REGISTERED)},
  1136. //@const CSFB_NOT_PREFERRED_REGISTERED number 已注册,非主要服务
  1137. {"CSFB_NOT_PREFERRED_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED)},
  1138. //@const CSFB_NOT_PREFERRED_REGISTERED_ROAMING number 已注册,非主要服务,漫游
  1139. {"CSFB_NOT_PREFERRED_REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED_ROAMING)},
  1140. //@const CONF_RESELTOWEAKNCELL number 小区重选信号差值门限,需要飞行模式设置
  1141. {"CONF_RESELTOWEAKNCELL", ROREG_INT(MOBILE_CONF_RESELTOWEAKNCELL)},
  1142. //@const CONF_STATICCONFIG number 网络静态模式优化,需要飞行模式设置
  1143. {"CONF_STATICCONFIG", ROREG_INT(MOBILE_CONF_STATICCONFIG)},
  1144. //@const CONF_QUALITYFIRST number 网络切换以信号质量优先,需要飞行模式设置,0不开,1开启,2开启并加速切换,功耗会增加
  1145. {"CONF_QUALITYFIRST", ROREG_INT(MOBILE_CONF_QUALITYFIRST)},
  1146. //@const CONF_USERDRXCYCLE number LTE跳paging,需要飞行模式设置,谨慎使用,0是不设置,1~7增大或减小DrxCycle周期倍数,1:1/8倍 2:1/4倍 3:1/2倍 4:2倍 5:4倍 6:8倍 7:16倍,8~12配置固定的DrxCycle周期,仅当该周期大于网络分配的DrxCycle周期时该配置才会生效,8:320ms 9:640ms 10:1280ms 11:2560ms 12:5120ms
  1147. {"CONF_USERDRXCYCLE", ROREG_INT(MOBILE_CONF_USERDRXCYCLE)},
  1148. //@const CONF_T3324MAXVALUE number PSM模式中的T3324时间,单位S
  1149. {"CONF_T3324MAXVALUE", ROREG_INT(MOBILE_CONF_T3324MAXVALUE)},
  1150. //@const CONF_PSM_MODE number PSM模式开关,0关,1开
  1151. {"CONF_PSM_MODE", ROREG_INT(MOBILE_CONF_PSM_MODE)},
  1152. //@const CONF_CE_MODE number attach模式,0为EPS ONLY 2为混合,遇到IMSI detach脱网问题,设置为0,注意设置为EPS ONLY时会取消短信功能
  1153. {"CONF_CE_MODE", ROREG_INT(MOBILE_CONF_CE_MODE)},
  1154. //@const CONF_SIM_WC_MODE number SIM写入次数的配置和读取
  1155. {"CONF_SIM_WC_MODE", ROREG_INT(MOBILE_CONF_SIM_WC_MODE)},
  1156. //@const CONF_FAKE_CELL_BARTIME number 伪基站禁止接入的时间,取值为0时取消,0xffff永久
  1157. {"CONF_FAKE_CELL_BARTIME", ROREG_INT(MOBILE_CONF_FAKE_CELL_BARTIME)},
  1158. //@const CONF_RESET_TO_FACTORY number 删除已保存的协议栈参数,重启后会使用默认配置
  1159. {"CONF_RESET_TO_FACTORY", ROREG_INT(MOBILE_CONF_RESET_TO_FACTORY)},
  1160. //@const CONF_USB_ETHERNET number 蜂窝网络模块的usb以太网卡控制,bit0开关,1开0关,bit1模式,1NAT0独立IP(在usb以太网卡开启前可以修改,开启过就不行),bit2协议1ECM,0RNDIS,飞行模式里设置
  1161. {"CONF_USB_ETHERNET", ROREG_INT(MOBILE_CONF_USB_ETHERNET)},
  1162. //@const CONF_DISABLE_NCELL_MEAS number 关闭邻区测量 1关,0开,除了功耗测试外不建议使用
  1163. {"CONF_DISABLE_NCELL_MEAS", ROREG_INT(MOBILE_CONF_DISABLE_NCELL_MEAS)},
  1164. //@const CONF_MAX_TX_POWER number 设置最大发射功率,0~23,必须在每次RRC=1时调用,RRC=0后会自动清除配置
  1165. {"CONF_MAX_TX_POWER", ROREG_INT(MOBILE_CONF_MAX_TX_POWER)},
  1166. //@const PIN_VERIFY number 验证PIN码操作
  1167. {"PIN_VERIFY", ROREG_INT(LUAT_SIM_PIN_VERIFY)},
  1168. //@const PIN_CHANGE number 更换PIN码操作
  1169. {"PIN_CHANGE", ROREG_INT(LUAT_SIM_PIN_CHANGE)},
  1170. //@const PIN_ENABLE number 使能PIN码验证
  1171. {"PIN_ENABLE", ROREG_INT(LUAT_SIM_PIN_ENABLE)},
  1172. //@const PIN_DISABLE number 关闭PIN码验证
  1173. {"PIN_DISABLE", ROREG_INT(LUAT_SIM_PIN_DISABLE)},
  1174. //@const PIN_UNBLOCK number 解锁PIN码
  1175. {"PIN_UNBLOCK", ROREG_INT(LUAT_SIM_PIN_UNBLOCK)},
  1176. {NULL, ROREG_INT(0)}
  1177. };
  1178. LUAMOD_API int luaopen_mobile( lua_State *L ) {
  1179. luat_newlib2(L, reg_mobile);
  1180. return 1;
  1181. }
  1182. static int l_mobile_event_handle(lua_State* L, void* ptr) {
  1183. LUAT_MOBILE_EVENT_E event = {0};
  1184. uint8_t index = 0;
  1185. uint8_t status = 0;
  1186. int ret = 0;
  1187. int16_t sIntraSearchP, sNonIntraSearchP, sIntraSearchQ, sNonIntraSearchQ;
  1188. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  1189. event = msg->arg1;
  1190. index = msg->arg2 >> 8;
  1191. status = msg->arg2 & 0xFF;
  1192. // luat_mobile_cell_info_t cell_info;
  1193. // luat_mobile_signal_strength_info_t signal_info;
  1194. // uint8_t csq, i;
  1195. // char imsi[20];
  1196. // char iccid[24] = {0};
  1197. if (lua_getglobal(L, "sys_pub") != LUA_TFUNCTION) {
  1198. return 0;
  1199. };
  1200. switch(event)
  1201. {
  1202. case LUAT_MOBILE_EVENT_CFUN:
  1203. break;
  1204. case LUAT_MOBILE_EVENT_SIM:
  1205. /*
  1206. @sys_pub mobile
  1207. sim卡状态变化
  1208. SIM_IND
  1209. @usage
  1210. sys.subscribe("SIM_IND", function(status, value)
  1211. -- status的取值有:
  1212. -- RDY SIM卡就绪, value为nil
  1213. -- NORDY 无SIM卡, value为nil
  1214. -- SIM_PIN 需要输入PIN, value为nil
  1215. -- GET_NUMBER 获取到电话号码(不一定有值), value为nil
  1216. -- SIM_WC SIM卡的写入次数统计,掉电归0, value为统计值
  1217. log.info("sim status", status, value)
  1218. end)
  1219. */
  1220. switch (status)
  1221. {
  1222. case LUAT_MOBILE_SIM_READY:
  1223. lua_pushstring(L, "SIM_IND");
  1224. lua_pushstring(L, "RDY");
  1225. lua_call(L, 2, 0);
  1226. break;
  1227. case LUAT_MOBILE_NO_SIM:
  1228. lua_pushstring(L, "SIM_IND");
  1229. lua_pushstring(L, "NORDY");
  1230. lua_call(L, 2, 0);
  1231. break;
  1232. case LUAT_MOBILE_SIM_NEED_PIN:
  1233. lua_pushstring(L, "SIM_IND");
  1234. lua_pushstring(L, "SIM_PIN");
  1235. lua_call(L, 2, 0);
  1236. break;
  1237. case LUAT_MOBILE_SIM_NUMBER:
  1238. lua_pushstring(L, "SIM_IND");
  1239. lua_pushstring(L, "GET_NUMBER");
  1240. lua_call(L, 2, 0);
  1241. break;
  1242. case LUAT_MOBILE_SIM_WC:
  1243. lua_pushstring(L, "SIM_IND");
  1244. lua_pushstring(L, "SIM_WC");
  1245. uint32_t tmp = (uint32_t)ptr;
  1246. lua_pushinteger(L, tmp);
  1247. lua_call(L, 3, 0);
  1248. break;
  1249. default:
  1250. break;
  1251. }
  1252. break;
  1253. case LUAT_MOBILE_EVENT_REGISTER_STATUS:
  1254. break;
  1255. case LUAT_MOBILE_EVENT_CELL_INFO:
  1256. switch (status)
  1257. {
  1258. case LUAT_MOBILE_CELL_INFO_UPDATE:
  1259. /*
  1260. @sys_pub mobile
  1261. 基站数据已更新
  1262. CELL_INFO_UPDATE
  1263. @usage
  1264. -- 订阅式
  1265. sys.subscribe("CELL_INFO_UPDATE", function()
  1266. log.info("cell", json.encode(mobile.getCellInfo()))
  1267. end)
  1268. */
  1269. lua_pushstring(L, "CELL_INFO_UPDATE");
  1270. lua_call(L, 1, 0);
  1271. break;
  1272. case LUAT_MOBILE_SERVICE_CELL_UPDATE:
  1273. /*
  1274. @sys_pub mobile
  1275. 服务小区额外信息更新
  1276. SCELL_INFO
  1277. @usage
  1278. -- 订阅式
  1279. sys.subscribe("SCELL_INFO", function()
  1280. log.info("service cell", mobile.scell()))
  1281. end)
  1282. */
  1283. lua_pushstring(L, "SCELL_INFO");
  1284. lua_call(L, 1, 0);
  1285. default:
  1286. break;
  1287. }
  1288. break;
  1289. case LUAT_MOBILE_EVENT_PDP:
  1290. LLOGD("cid%d, state%d", index, status);
  1291. break;
  1292. case LUAT_MOBILE_EVENT_NETIF:
  1293. switch (status)
  1294. {
  1295. case LUAT_MOBILE_NETIF_LINK_ON:
  1296. LLOGD("NETIF_LINK_ON -> IP_READY");
  1297. /*
  1298. @sys_pub mobile
  1299. 已联网
  1300. IP_READY
  1301. @usage
  1302. -- 联网后会发一次这个消息
  1303. sys.subscribe("IP_READY", function(ip, adapter)
  1304. log.info("mobile", "IP_READY", ip, (adapter or -1) == socket.LWIP_GP)
  1305. end)
  1306. */
  1307. lua_pushstring(L, "IP_READY");
  1308. luat_ip_addr_t local_ip, net_mask, gate_way, ipv6;
  1309. #ifdef LUAT_USE_LWIP
  1310. ipv6.type = 0xff;
  1311. ret = network_get_full_local_ip_info(NULL, NW_ADAPTER_INDEX_LWIP_GPRS, &local_ip, &net_mask, &gate_way, &ipv6);
  1312. #else
  1313. void* userdata = NULL;
  1314. network_adapter_info* info = network_adapter_fetch(NW_ADAPTER_INDEX_LWIP_GPRS, &userdata);
  1315. if (info == NULL)
  1316. ret = -1;
  1317. else
  1318. ret = info->get_local_ip_info(&local_ip, &net_mask, &gate_way, userdata);
  1319. #endif
  1320. if (ret == 0) {
  1321. #ifdef LUAT_USE_LWIP
  1322. lua_pushfstring(L, "%s", ipaddr_ntoa(&local_ip));
  1323. #else
  1324. 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);
  1325. #endif
  1326. }
  1327. else {
  1328. lua_pushliteral(L, "0.0.0.0");
  1329. }
  1330. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
  1331. lua_call(L, 3, 0);
  1332. break;
  1333. case LUAT_MOBILE_NETIF_LINK_OFF:
  1334. LLOGD("NETIF_LINK_OFF -> IP_LOSE");
  1335. /*
  1336. @sys_pub mobile
  1337. 已断网
  1338. IP_LOSE
  1339. @usage
  1340. -- 断网后会发一次这个消息
  1341. sys.subscribe("IP_LOSE", function(adapter)
  1342. log.info("mobile", "IP_LOSE", (adapter or -1) == socket.LWIP_GP)
  1343. end)
  1344. */
  1345. lua_pushstring(L, "IP_LOSE");
  1346. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
  1347. lua_call(L, 2, 0);
  1348. break;
  1349. default:
  1350. break;
  1351. }
  1352. break;
  1353. case LUAT_MOBILE_EVENT_TIME_SYNC:
  1354. /*
  1355. @sys_pub mobile
  1356. 时间已经同步
  1357. NTP_UPDATE
  1358. @usage
  1359. -- 对于电信/移动的卡, 联网后,基站会下发时间,但联通卡不会,务必留意
  1360. sys.subscribe("NTP_UPDATE", function()
  1361. log.info("mobile", "time", os.date())
  1362. end)
  1363. */
  1364. LLOGD("TIME_SYNC %d", status);
  1365. lua_pushstring(L, "NTP_UPDATE");
  1366. lua_call(L, 1, 0);
  1367. break;
  1368. case LUAT_MOBILE_EVENT_CSCON:
  1369. // LLOGD("CSCON %d", status);
  1370. /*
  1371. @sys_pub mobile
  1372. RRC状态
  1373. CSCON
  1374. @usage
  1375. -- state 1 CONNECT 0 IDLE
  1376. sys.subscribe("CSCON", function(state)
  1377. log.info("mobile", "CSCON", state)
  1378. end)
  1379. */
  1380. lua_pushstring(L, "CSCON");
  1381. lua_pushinteger(L, status);
  1382. lua_call(L, 2, 0);
  1383. break;
  1384. case LUAT_MOBILE_EVENT_BEARER:
  1385. LLOGD("bearer act %d, result %d",status, index);
  1386. break;
  1387. case LUAT_MOBILE_EVENT_SMS:
  1388. switch(status)
  1389. {
  1390. case LUAT_MOBILE_SMS_READY:
  1391. LLOGI("sim%d sms ready", index);
  1392. break;
  1393. case LUAT_MOBILE_NEW_SMS:
  1394. break;
  1395. case LUAT_MOBILE_SMS_SEND_DONE:
  1396. break;
  1397. case LUAT_MOBILE_SMS_ACK:
  1398. break;
  1399. }
  1400. break;
  1401. case LUAT_MOBILE_EVENT_IMS_REGISTER_STATUS:
  1402. LLOGD("ims reg state %d", status);
  1403. break;
  1404. case LUAT_MOBILE_EVENT_CC:
  1405. LLOGD("LUAT_MOBILE_EVENT_CC status %d",status);
  1406. /*
  1407. @sys_pub mobile
  1408. 通话状态变化
  1409. CC_IND
  1410. @usage
  1411. sys.subscribe("CC_IND", function(status, value)
  1412. log.info("cc status", status, value)
  1413. end)
  1414. */
  1415. switch(status){
  1416. case LUAT_MOBILE_CC_READY:
  1417. LLOGD("LUAT_MOBILE_CC_READY");
  1418. lua_pushstring(L, "CC_IND");
  1419. lua_pushstring(L, "READY");
  1420. lua_call(L, 2, 0);
  1421. break;
  1422. case LUAT_MOBILE_CC_INCOMINGCALL:
  1423. lua_pushstring(L, "CC_IND");
  1424. lua_pushstring(L, "INCOMINGCALL");
  1425. lua_call(L, 2, 0);
  1426. break;
  1427. case LUAT_MOBILE_CC_CALL_NUMBER:
  1428. // lua_pushstring(L, "CC_IND");
  1429. // lua_pushstring(L, "CALL_NUMBER");
  1430. // lua_call(L, 2, 0);
  1431. break;
  1432. case LUAT_MOBILE_CC_CONNECTED_NUMBER:
  1433. // lua_pushstring(L, "CC_IND");
  1434. // lua_pushstring(L, "CONNECTED_NUMBER");
  1435. // lua_call(L, 2, 0);
  1436. break;
  1437. case LUAT_MOBILE_CC_CONNECTED:
  1438. lua_pushstring(L, "CC_IND");
  1439. lua_pushstring(L, "CONNECTED");
  1440. lua_call(L, 2, 0);
  1441. break;
  1442. case LUAT_MOBILE_CC_DISCONNECTED:
  1443. lua_pushstring(L, "CC_IND");
  1444. lua_pushstring(L, "DISCONNECTED");
  1445. lua_call(L, 2, 0);
  1446. break;
  1447. case LUAT_MOBILE_CC_SPEECH_START:
  1448. lua_pushstring(L, "CC_IND");
  1449. lua_pushstring(L, "SPEECH_START");
  1450. lua_call(L, 2, 0);
  1451. break;
  1452. case LUAT_MOBILE_CC_MAKE_CALL_OK:
  1453. lua_pushstring(L, "CC_IND");
  1454. lua_pushstring(L, "MAKE_CALL_OK");
  1455. lua_call(L, 2, 0);
  1456. break;
  1457. case LUAT_MOBILE_CC_MAKE_CALL_FAILED:
  1458. lua_pushstring(L, "CC_IND");
  1459. lua_pushstring(L, "MAKE_CALL_FAILED");
  1460. lua_call(L, 2, 0);
  1461. break;
  1462. case LUAT_MOBILE_CC_ANSWER_CALL_DONE:
  1463. lua_pushstring(L, "CC_IND");
  1464. lua_pushstring(L, "ANSWER_CALL_DONE");
  1465. lua_call(L, 2, 0);
  1466. break;
  1467. case LUAT_MOBILE_CC_HANGUP_CALL_DONE:
  1468. lua_pushstring(L, "CC_IND");
  1469. lua_pushstring(L, "HANGUP_CALL_DONE");
  1470. lua_call(L, 2, 0);
  1471. break;
  1472. case LUAT_MOBILE_CC_LIST_CALL_RESULT:
  1473. lua_pushstring(L, "CC_IND");
  1474. lua_pushstring(L, "LIST_CALL_RESULT");
  1475. lua_call(L, 2, 0);
  1476. break;
  1477. case LUAT_MOBILE_CC_PLAY:// 最先
  1478. lua_pushstring(L, "CC_IND");
  1479. lua_pushstring(L, "PLAY");
  1480. lua_call(L, 2, 0);
  1481. break;
  1482. }
  1483. break;
  1484. case LUAT_MOBILE_EVENT_RRC:
  1485. // LLOGD("LUAT_MOBILE_EVENT_RRC status %d",status);
  1486. /*
  1487. @sys_pub mobile
  1488. RRC部分信息上报,2025/9/15启用
  1489. RRC_IND
  1490. @usage
  1491. sys.subscribe("RRC_IND", function(event, value, ...)
  1492. log.info("rrc status", event, value, ...)
  1493. end)
  1494. event目前有
  1495. 1、"DRX",DRX周期值,后续跟1个参数为具体的DRX周期值,单位ms,目前只有320,640,1280,2560
  1496. 2、"IDLE_MEAS_THRESHOLD",RRC IDLE下邻区测量阈值,后续跟4个参数为具体的测量阈值,单位dbm
  1497. 4个参数分别为sIntraSearchP, sNonIntraSearchP, sIntraSearchQ, sNonIntraSearchQ
  1498. 当rsrp <= sIntraSearchP,启动同频邻区测量,低功耗下功耗有所升高
  1499. 当rsrp <= sNonIntraSearchP,启动异频邻区测量,低功耗下功耗显著升高
  1500. 如果sIntraSearchQ不为0,当rsrq <= sIntraSearchQ,启动同频邻区测量,低功耗下功耗有所升高
  1501. 如果sNonIntraSearchQ不为0,当rsrq <= sNonIntraSearchQ,启动异频邻区测量,低功耗下功耗显著升高
  1502. */
  1503. switch(status)
  1504. {
  1505. case LUAT_MOBILE_RRC_DRX_CYCLE_UPDATED:
  1506. lua_pushstring(L, "RRC_IND");
  1507. lua_pushstring(L, "DRX");
  1508. lua_pushinteger(L, index * 320);
  1509. lua_call(L, 3, 0);
  1510. break;
  1511. case LUAT_MOBILE_RRC_IDLE_MEAS_THRESHOLD:
  1512. lua_pushstring(L, "RRC_IND");
  1513. lua_pushstring(L, "IDLE_MEAS_THRESHOLD");
  1514. luat_mobile_rrc_get_idle_meas_threshold(&sIntraSearchP, &sNonIntraSearchP, &sIntraSearchQ, &sNonIntraSearchQ);
  1515. lua_pushinteger(L, sIntraSearchP);
  1516. lua_pushinteger(L, sNonIntraSearchP);
  1517. lua_pushinteger(L, sIntraSearchQ);
  1518. lua_pushinteger(L, sNonIntraSearchQ);
  1519. lua_call(L, 6, 0);
  1520. break;
  1521. }
  1522. break;
  1523. default:
  1524. break;
  1525. }
  1526. return 0;
  1527. }
  1528. void luat_mobile_event_cb(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status, void* ptr) {
  1529. #if defined LUAT_USE_VOLTE
  1530. switch (event){
  1531. case LUAT_MOBILE_EVENT_CC:
  1532. switch(status){
  1533. case LUAT_MOBILE_CC_SPEECH_START:
  1534. luat_cc_start_speech(index+1);
  1535. break;
  1536. case LUAT_MOBILE_CC_PLAY:
  1537. luat_cc_play_tone(index);
  1538. break;
  1539. }
  1540. break;
  1541. default:
  1542. break;
  1543. }
  1544. #endif
  1545. #if defined LUAT_USE_AIRLINK_EXEC_MOBILE
  1546. luat_airlink_mobile_event_callback(event, index, status, ptr);
  1547. #endif
  1548. rtos_msg_t msg = {
  1549. .handler = l_mobile_event_handle,
  1550. .arg1 = event,
  1551. .arg2 = (index << 8) + status ,
  1552. .ptr = ptr
  1553. };
  1554. luat_msgbus_put(&msg, 0);
  1555. }