luat_lib_mobile.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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. ret = luat_mobile_get_band_from_earfcn(info.earfcn);
  569. lua_pushinteger(L, ret);
  570. lua_setfield(L, -2, "band");
  571. // 基站相关
  572. uint32_t eci = 0;
  573. uint16_t tac = 0;
  574. luat_mobile_get_service_cell_identifier(&eci);
  575. lua_pushinteger(L, eci);
  576. lua_setfield(L, -2, "eci");
  577. lua_pushinteger(L, eci);
  578. lua_setfield(L, -2, "cid");
  579. luat_mobile_get_service_tac_or_lac(&tac);
  580. lua_pushinteger(L, tac);
  581. lua_setfield(L, -2, "tac");
  582. // 信号强度相关的值
  583. luat_mobile_signal_strength_info_t sinfo = {0};
  584. luat_mobile_get_signal_strength_info(&sinfo);
  585. lua_pushinteger(L, sinfo.lte_signal_strength.snr);
  586. lua_setfield(L, -2, "snr");
  587. lua_pushinteger(L, sinfo.lte_signal_strength.rsrp);
  588. lua_setfield(L, -2, "rsrp");
  589. lua_pushinteger(L, sinfo.lte_signal_strength.rsrq);
  590. lua_setfield(L, -2, "rsrq");
  591. lua_pushinteger(L, sinfo.lte_signal_strength.rssi);
  592. lua_setfield(L, -2, "rssi");
  593. return 1;
  594. }
  595. /**
  596. 进出飞行模式
  597. @api mobile.flymode(index, enable)
  598. @int 编号,默认0. 在支持双卡的模块上才会出现0或1的情况
  599. @bool 是否设置为飞行模式,true为设置, false为退出,可选
  600. @return bool 原飞行模式的状态
  601. */
  602. static int l_mobile_flymode(lua_State* L) {
  603. int index = luaL_optinteger(L, 1, 0);
  604. int flymode = luat_mobile_get_flymode(index);
  605. if (lua_isboolean(L, 2)) {
  606. luat_mobile_set_flymode(index, lua_toboolean(L, 2));
  607. }
  608. lua_pushboolean(L, flymode == 0 ? 0 : 1);
  609. return 1;
  610. }
  611. /**
  612. 配置基站同步时间开关,默认开启
  613. @api mobile.syncTime(enable)
  614. @bool 开启,true开启, false关闭, nil不设置
  615. @return bool 当前开关状态
  616. @usage
  617. mobile.syncTime() --获取当前开关状态
  618. mobile.syncTime(false) --关闭基站同步时间
  619. */
  620. static int l_mobile_sync_time(lua_State* L) {
  621. if (lua_isboolean(L, 1)) {
  622. luat_mobile_set_sync_time(lua_toboolean(L, 1));
  623. }
  624. lua_pushboolean(L, luat_mobile_get_sync_time());
  625. return 1;
  626. }
  627. /**
  628. 获取网络状态
  629. @api mobile.status()
  630. @return int 当前网络状态
  631. @usage
  632. -- 状态描述
  633. -- 0:网络未注册
  634. -- 1:网络已注册
  635. -- 2:正在搜网中
  636. -- 3:网络注册被拒绝
  637. -- 4:网络状态未知
  638. -- 5:漫游,且已注册
  639. -- 6:仅SMS可用
  640. -- 7:仅SMS可用,且漫游状态
  641. -- 8:仅紧急呼叫. 注意, 国内不支持此状态,模块也不支持紧急呼叫
  642. -- 当不是1或者5的时候,无法通过蜂窝移动网络上网
  643. -- 不能使用本API判断联网状态, 可联网状态应该以连上目标服务器为准
  644. */
  645. static int l_mobile_status(lua_State* L) {
  646. int LUAT_MOBILE_REGISTER_STATUS_E = luat_mobile_get_register_status();
  647. lua_pushinteger(L, LUAT_MOBILE_REGISTER_STATUS_E);
  648. return 1;
  649. }
  650. /**
  651. 获取基站信息
  652. @api mobile.getCellInfo()
  653. @return table 包含基站数据的数组
  654. @usage
  655. -- 注意: 从2023.06.20开始, 需要主动请求一次reqCellInfo才会有基站数据.
  656. --示例输出(原始数据是table, 下面是json格式化后的内容)
  657. --[[
  658. [
  659. {"rsrq":-10,"rssi":-55,"cid":124045360,"mnc":17,"pci":115,"earfcn":1850,"snr":15,"rsrp":-85,"mcc":1120,"tdd":0},
  660. {"pci":388,"rsrq":-11,"mnc":17,"earfcn":2452,"snr":5,"rsrp":-67,"mcc":1120,"cid":124045331},
  661. {"pci":100,"rsrq":-9,"mnc":17,"earfcn":75,"snr":17,"rsrp":-109,"mcc":1120,"cid":227096712}
  662. ]
  663. ]]
  664. mobile.reqCellInfo(60)
  665. -- 订阅
  666. sys.subscribe("CELL_INFO_UPDATE", function()
  667. log.info("cell", json.encode(mobile.getCellInfo()))
  668. end)
  669. -- 定期轮训式
  670. sys.taskInit(function()
  671. sys.wait(3000)
  672. while 1 do
  673. mobile.reqCellInfo(15)
  674. sys.waitUntil("CELL_INFO_UPDATE", 15000)
  675. log.info("cell", json.encode(mobile.getCellInfo()))
  676. end
  677. end)
  678. */
  679. static int l_mobile_get_cell_info(lua_State* L) {
  680. lua_newtable(L);
  681. luat_mobile_cell_info_t* info = luat_heap_malloc(sizeof(luat_mobile_cell_info_t));
  682. if (info == NULL) {
  683. LLOGE("out of memory when malloc cell_info");
  684. return 1;
  685. }
  686. int ret = luat_mobile_get_last_notify_cell_info(info);
  687. if (ret != 0) {
  688. LLOGI("none cell info found %d", ret);
  689. goto exit;
  690. }
  691. //LLOGD("cid %d neighbor %d", info->lte_service_info.cid, info->lte_neighbor_info_num);
  692. // 当前仅返回lte信息
  693. if (info->lte_info_valid == 0 || info->lte_service_info.cid == 0) {
  694. if (0 == luat_mobile_get_service_cell_identifier(&info->lte_service_info.cid) && info->lte_service_info.cid) {
  695. LLOGW("请先调用 mobile.reqCellInfo()!!");
  696. }
  697. else {
  698. LLOGI("lte cell info not found");
  699. }
  700. goto exit;
  701. }
  702. lua_newtable(L);
  703. lua_pushinteger(L, info->lte_service_info.pci);
  704. lua_setfield(L, -2, "pci");
  705. lua_pushinteger(L, info->lte_service_info.cid);
  706. lua_setfield(L, -2, "cid");
  707. lua_pushinteger(L, info->lte_service_info.earfcn);
  708. lua_setfield(L, -2, "earfcn");
  709. lua_pushinteger(L, info->lte_service_info.rsrp);
  710. lua_setfield(L, -2, "rsrp");
  711. lua_pushinteger(L, info->lte_service_info.rsrq);
  712. lua_setfield(L, -2, "rsrq");
  713. lua_pushinteger(L, info->lte_service_info.rssi);
  714. lua_setfield(L, -2, "rssi");
  715. lua_pushinteger(L, info->lte_service_info.is_tdd);
  716. lua_setfield(L, -2, "tdd");
  717. lua_pushinteger(L, info->lte_service_info.snr);
  718. lua_setfield(L, -2, "snr");
  719. lua_pushinteger(L, u162bcd(info->lte_service_info.mcc));
  720. lua_setfield(L, -2, "mcc");
  721. lua_pushinteger(L, u162bcd(info->lte_service_info.mnc));
  722. lua_setfield(L, -2, "mnc");
  723. lua_pushinteger(L, info->lte_service_info.tac);
  724. lua_setfield(L, -2, "tac");
  725. lua_pushinteger(L, info->lte_service_info.band);
  726. lua_setfield(L, -2, "band");
  727. lua_pushinteger(L, info->lte_service_info.ulbandwidth);
  728. lua_setfield(L, -2, "ulbandwidth");
  729. lua_pushinteger(L, info->lte_service_info.dlbandwidth);
  730. lua_setfield(L, -2, "dlbandwidth");
  731. lua_seti(L, -2, 1);
  732. if (info->lte_neighbor_info_num > 0) {
  733. for (size_t i = 0; i < info->lte_neighbor_info_num; i++)
  734. {
  735. lua_settop(L, 1);
  736. //LLOGD("add neighbor %d", i);
  737. lua_newtable(L);
  738. lua_pushinteger(L, info->lte_info[i].pci);
  739. lua_setfield(L, -2, "pci");
  740. lua_pushinteger(L, info->lte_info[i].cid);
  741. lua_setfield(L, -2, "cid");
  742. if (0x8850 == info->version)
  743. {
  744. lua_pushinteger(L, info->lte_info[i].rssi);
  745. lua_setfield(L, -2, "rssi");
  746. lua_pushinteger(L, info->lte_info[i].celltype);
  747. lua_setfield(L, -2, "celltype");
  748. lua_pushinteger(L, info->lte_info[i].bandwidth);
  749. lua_setfield(L, -2, "bandwidth");
  750. }
  751. lua_pushinteger(L, info->lte_info[i].earfcn);
  752. lua_setfield(L, -2, "earfcn");
  753. lua_pushinteger(L, info->lte_info[i].rsrp);
  754. lua_setfield(L, -2, "rsrp");
  755. lua_pushinteger(L, info->lte_info[i].rsrq);
  756. lua_setfield(L, -2, "rsrq");
  757. lua_pushinteger(L, u162bcd(info->lte_info[i].mcc));
  758. lua_setfield(L, -2, "mcc");
  759. lua_pushinteger(L, u162bcd(info->lte_info[i].mnc));
  760. lua_setfield(L, -2, "mnc");
  761. lua_pushinteger(L, info->lte_info[i].snr);
  762. lua_setfield(L, -2, "snr");
  763. lua_pushinteger(L, info->lte_info[i].tac);
  764. lua_setfield(L, -2, "tac");
  765. lua_seti(L, -2, i + 2);
  766. }
  767. }
  768. lua_settop(L, 1);
  769. exit:
  770. luat_heap_free(info);
  771. return 1;
  772. }
  773. /**
  774. 发起基站信息查询,含临近小区
  775. @api mobile.reqCellInfo(timeout)
  776. @int 超时时长,单位秒,默认15. 最少5, 最高60
  777. @return nil 无返回值
  778. @usage
  779. -- 参考 mobile.getCellInfo 函数
  780. */
  781. static int l_mobile_request_cell_info(lua_State* L) {
  782. int timeout = luaL_optinteger(L, 1, 15);
  783. if (timeout > 60)
  784. timeout = 60;
  785. else if (timeout < 5)
  786. timeout = 5;
  787. luat_mobile_get_cell_info_async(timeout);
  788. return 0;
  789. }
  790. /**
  791. 锁定/解锁小区,仅用于外场测试,没接触过的,或者生产环境中请勿使用
  792. @api mobile.lockCell(mode, earfcn, pci)
  793. @int 操作码 0删除优先的频点,1设置优先频点,2锁定小区,3解锁小区
  794. @int 下行频点
  795. @int phycellid
  796. @return bool 成功true 失败false
  797. @usage
  798. mobile.lockCell(2,1860,32) --锁定小区
  799. mobile.lockCell(3) --解锁小区
  800. */
  801. static int l_mobile_lock_cell(lua_State* L) {
  802. lua_pushboolean(L, !luat_mobile_lock_cell(luaL_optinteger(L, 1, 4), luaL_optinteger(L, 2, 0), luaL_optinteger(L, 3, 0)));
  803. return 1;
  804. }
  805. /**
  806. 重启协议栈
  807. @api mobile.reset()
  808. @return nil 无返回值
  809. @usage
  810. -- 重启LTE协议栈
  811. mobile.reset()
  812. */
  813. static int l_mobile_reset(lua_State* L) {
  814. luat_mobile_reset_stack();
  815. return 0;
  816. }
  817. /**
  818. 数据量流量处理
  819. @api mobile.dataTraffic(clearUplink, clearDownlink)
  820. @boolean 清空上行流量累计值,true清空,其他忽略
  821. @boolean 清空下行流量累计值,true清空,其他忽略
  822. @return int 上行流量GB
  823. @return int 上行流量B
  824. @return int 下行流量GB
  825. @return int 下行流量B
  826. @usage
  827. -- 获取上下行流量累计值
  828. -- 上行流量值Byte = uplinkGB * 1024 * 1024 * 1024 + uplinkB
  829. -- 下行流量值Byte = downlinkGB * 1024 * 1024 * 1024 + downlinkB
  830. local uplinkGB, uplinkB, downlinkGB, downlinkB = mobile.dataTraffic()
  831. -- 清空上下行流量累计值
  832. mobile.dataTraffic(true, true)
  833. -- 仅记录开机后的流量,复位/重启会归零
  834. */
  835. static int l_mobile_data_traffic(lua_State* L) {
  836. uint64_t uplink;
  837. uint64_t downlink;
  838. uint8_t clear_uplink = 0;
  839. uint8_t clear_downlink = 0;
  840. volatile uint32_t temp;
  841. if (LUA_TBOOLEAN == lua_type(L, 1)) {
  842. clear_uplink = lua_toboolean(L, 1);
  843. }
  844. if (LUA_TBOOLEAN == lua_type(L, 2)) {
  845. clear_downlink = lua_toboolean(L, 2);
  846. }
  847. luat_mobile_get_ip_data_traffic(&uplink, &downlink);
  848. if (clear_uplink || clear_downlink) {
  849. luat_mobile_clear_ip_data_traffic(clear_uplink, clear_downlink);
  850. }
  851. temp = (uint32_t)(uplink >> 30);
  852. lua_pushinteger(L, temp);
  853. temp = (((uint32_t)uplink) & 0x3FFFFFFF);
  854. lua_pushinteger(L, temp);
  855. temp = (uint32_t)(downlink >> 30);
  856. lua_pushinteger(L, temp);
  857. temp = (((uint32_t)downlink) & 0x3FFFFFFF);
  858. lua_pushinteger(L, temp);
  859. return 4;
  860. }
  861. /**
  862. 网络特殊配置
  863. @api mobile.config(item, value)
  864. @int 配置项目,看mobile.CONF_XXX
  865. @int 配置值,根据具体配置的item决定
  866. @return boolean 是否成功
  867. @usage
  868. --针对不同平台有不同的配置,谨慎使用,目前只有Air780EXXX系列支持
  869. -- Air780EXXX配置小区重选信号差值门限,不能大于15dbm,必须在飞行模式下才能用
  870. mobile.flymode(0,true)
  871. mobile.config(mobile.CONF_RESELTOWEAKNCELL, 15)
  872. mobile.config(mobile.CONF_STATICCONFIG, 1) --开启网络静态优化
  873. mobile.flymode(0,false)
  874. -- Air780EXXX设置SIM写入次数的统计
  875. -- 关闭统计
  876. mobile.config(mobile.CONF_SIM_WC_MODE, 0)
  877. -- 开启统计, 默认也是开启的.
  878. mobile.config(mobile.CONF_SIM_WC_MODE, 1)
  879. -- 读取统计值,异步, 需要通过系统消息SIM_IND获取
  880. sys.subscribe("SIM_IND", function(stats, value)
  881. log.info("SIM_IND", stats)
  882. if stats == "SIM_WC" then
  883. log.info("sim", "write counter", value)
  884. end
  885. end)
  886. mobile.config(mobile.CONF_SIM_WC_MODE, 2)
  887. -- 清空统计值
  888. mobile.config(mobile.CONF_SIM_WC_MODE, 3)
  889. */
  890. static int l_mobile_config(lua_State* L) {
  891. uint8_t item = luaL_optinteger(L, 1, 0);
  892. uint32_t value = luaL_optinteger(L, 2, 0);
  893. if (!item)
  894. {
  895. lua_pushboolean(L, 0);
  896. }
  897. else
  898. {
  899. lua_pushboolean(L, !luat_mobile_config(item, value));
  900. }
  901. return 1;
  902. }
  903. #include "luat_uart.h"
  904. #include "luat_zbuff.h"
  905. /**
  906. 获取当前使用/支持的band
  907. @api mobile.getBand(band, is_default)
  908. @zbuff 输出band
  909. @boolean true默认支持,false当前支持的,默认是false,当前是预留功能,不要写true
  910. @return boolean 成功返回true,失败放回false
  911. @usage
  912. local buff = zbuff.create(40)
  913. mobile.getBand(buff) --输出当前使用的band,band号放在buff内,buff[0],buff[1],buff[2] .. buff[buff:used() - 1]
  914. log.info("当前使用的band:")
  915. --轮训方式打印所用band
  916. for i=0,band:used()-1 do
  917. log.info("band", band[i])
  918. end
  919. */
  920. static int l_mobile_get_band(lua_State* L) {
  921. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  922. uint8_t total_num = 0;
  923. int re;
  924. if (buff->len < 40)
  925. {
  926. __zbuff_resize(buff, 40);
  927. }
  928. if (lua_isboolean(L, 2) && lua_toboolean(L, 2))
  929. {
  930. re = luat_mobile_get_support_band(buff->addr, &total_num);
  931. }
  932. else
  933. {
  934. re = luat_mobile_get_band(buff->addr, &total_num);
  935. }
  936. buff->used = total_num;
  937. lua_pushboolean(L, !re);
  938. return 1;
  939. }
  940. /**
  941. 设置使用的band
  942. @api mobile.setBand(band, num)
  943. @zbuff 输入使用的band
  944. @int band数量
  945. @return boolean 成功返回true,失败放回false
  946. @usage
  947. local buff = zbuff.create(40)
  948. buff[0] = 3
  949. buff[1] = 5
  950. buff[2] = 8
  951. buff[3] = 40
  952. mobile.setBand(buff, 4) --设置使用的band一共4个,为3,5,8,40
  953. */
  954. static int l_mobile_set_band(lua_State* L) {
  955. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  956. int num = luaL_optinteger(L, 2, 1);
  957. lua_pushboolean(L, !luat_mobile_set_band(buff->addr, num));
  958. return 1;
  959. }
  960. /**
  961. RF测试开关和配置
  962. @api mobile.nstOnOff(onoff, uart_id)
  963. @boolean true开启测试模式,false关闭
  964. @int 串口号
  965. @return nil 无返回值
  966. @usage
  967. mobile.nstOnOff(true, uart.VUART_0) --打开测试模式,并且用虚拟串口发送结果
  968. mobile.nstOnOff(false) --关闭测试模式
  969. */
  970. static int l_mobile_nst_test_onoff(lua_State* L) {
  971. luat_mobile_rf_test_mode(luaL_optinteger(L, 2, LUAT_VUART_ID_0), lua_toboolean(L, 1));
  972. return 0;
  973. }
  974. /**
  975. RF测试数据输入
  976. @api mobile.nstInput(data)
  977. @string or zbuff 用户从串口获取的数据,注意,当获取完所有数据后,需要再传一个nil来作为传输结束
  978. @return nil 无返回值
  979. @usage
  980. mobile.nstInput(uart_data)
  981. mobile.nstInput(nil)
  982. */
  983. static int l_mobile_nst_data_input(lua_State* L) {
  984. size_t len = 0;
  985. const char *buf = NULL;
  986. if(lua_isuserdata(L, 1))
  987. {
  988. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  989. len = buff->used;
  990. buf = buff->addr;
  991. }
  992. else if (lua_isstring(L, 1))
  993. {
  994. buf = lua_tolstring(L, 1, &len);//取出字符串数据
  995. }
  996. luat_mobile_rf_test_input(buf, len);
  997. return 0;
  998. }
  999. /**
  1000. 初始化内置默认虚拟卡功能(不可用)
  1001. @api mobile.vsimInit()
  1002. @return nil 无返回值
  1003. @usage
  1004. mobile.vsimInit()
  1005. */
  1006. static int l_mobile_init_vsim(lua_State* L) {
  1007. luat_mobile_softsim_init_default();
  1008. return 0;
  1009. }
  1010. /**
  1011. 切换内置虚拟卡和外置实体卡,2024年8月13日启用,虚拟卡需要固件支持,否则切换后无网络,需要在飞行模式下切换,或者切换后重启协议栈
  1012. @api mobile.vsimOnOff(enable)
  1013. @bool 开启,true开启, false关闭
  1014. @return nil 无返回值
  1015. @usage
  1016. mobile.vsimOnOff(true) --使用内置虚拟卡
  1017. mobile.vsimOnOff(false) --使用外置实体卡
  1018. */
  1019. static int l_mobile_vsim_onoff(lua_State* L) {
  1020. if (lua_isboolean(L, 1)) {
  1021. luat_mobile_softsim_onoff(lua_toboolean(L, 1));
  1022. }
  1023. return 0;
  1024. }
  1025. /**
  1026. 初始化自定义APN列表,主要用于海外SIM卡
  1027. @api mobile.apnTableInit()
  1028. @return nil 无返回值
  1029. @usage
  1030. mobile.apnTableInit()
  1031. */
  1032. static int l_mobile_init_apn_table(lua_State* L) {
  1033. luat_mobile_init_auto_apn_by_plmn();
  1034. return 0;
  1035. }
  1036. /**
  1037. 往自定义APN列表添加一条APN信息,主要用于海外SIM卡
  1038. @api mobile.apnTableAdd(mcc, mnc, ip_type, protocol, apn_name, user_name, password)
  1039. @int MCC码,16进制BCD码
  1040. @int MNC码,16进制BCD码
  1041. @int 激活APN时的IP TYPE,1=IPV4 2=IPV6 3=IPV4V6,默认是1
  1042. @int 激活APN时,如果需要username和password,就要写鉴权协议类型,1~3,默认3,代表1和2都尝试一下。不需要鉴权的写0
  1043. @string APN name,不能为空
  1044. @string APN的username
  1045. @string APN的password
  1046. @return nil 无返回值
  1047. @usage
  1048. mobile.apnTableInit() -- 先初始化,必须放在SIM卡识别完成前加入,最好就是写在开头
  1049. mobile.apnTableAdd(0x460,0x00,3,0,"cmiot","","") -- 单独添加一条APN信息,必须放在SIM卡识别完成前加入,最好就是写在开头,移动公网卡设置APN为cmiot(一般不用设置,这里只是举个例子)
  1050. */
  1051. static int l_mobile_add_apn_table(lua_State* L) {
  1052. size_t name_len = 0;
  1053. size_t user_len = 0;
  1054. size_t password_len = 0;
  1055. uint16_t mcc = luaL_optinteger(L, 1, 0x460);
  1056. uint16_t mnc = luaL_optinteger(L, 2, 0);
  1057. uint8_t ip_type = luaL_optinteger(L, 3, 3);
  1058. uint8_t protocol = luaL_optinteger(L, 4, 3);
  1059. const char* name = luaL_checklstring(L, 5, &name_len);
  1060. const char* user = luaL_optlstring(L, 6, "", &user_len);
  1061. const char* password = luaL_optlstring(L, 7, "", &password_len);
  1062. luat_mobile_add_auto_apn_item(mcc, mnc, ip_type, protocol, name, name_len, user, user_len, password, password_len, 1);
  1063. return 0;
  1064. }
  1065. /**
  1066. 打印自定义APN列表里的一条信息,在没有拿到卡的情况下,测试一下对应的APN信息是否和运营商提供的匹配
  1067. @api mobile.apnTablePrint(mcc, mnc)
  1068. @int MCC码,16进制BCD码
  1069. @int MNC码,16进制BCD码
  1070. @return nil 无返回值
  1071. @usage
  1072. mobile.apnTableInit()
  1073. mobile.apnTablePrint(0x202, 0x01)
  1074. */
  1075. static int l_mobile_print_apn_table(lua_State* L) {
  1076. uint16_t mcc = luaL_optinteger(L, 1, 0x460);
  1077. uint16_t mnc = luaL_optinteger(L, 2, 0);
  1078. luat_mobile_print_apn_by_mcc_mnc(mcc, mnc);
  1079. return 0;
  1080. }
  1081. #include "rotable2.h"
  1082. static const rotable_Reg_t reg_mobile[] = {
  1083. {"status", ROREG_FUNC(l_mobile_status)},
  1084. {"imei", ROREG_FUNC(l_mobile_imei)},
  1085. {"imsi", ROREG_FUNC(l_mobile_imsi)},
  1086. {"sn", ROREG_FUNC(l_mobile_sn)},
  1087. {"iccid", ROREG_FUNC(l_mobile_iccid)},
  1088. {"number", ROREG_FUNC(l_mobile_number)},
  1089. {"muid", ROREG_FUNC(l_mobile_muid)},
  1090. {"apn", ROREG_FUNC(l_mobile_apn)},
  1091. {"ipv6", ROREG_FUNC(l_mobile_ipv6)},
  1092. {"csq", ROREG_FUNC(l_mobile_csq)},
  1093. {"rssi", ROREG_FUNC(l_mobile_rssi)},
  1094. {"rsrq", ROREG_FUNC(l_mobile_rsrq)},
  1095. {"rsrp", ROREG_FUNC(l_mobile_rsrp)},
  1096. {"snr", ROREG_FUNC(l_mobile_snr)},
  1097. {"eci", ROREG_FUNC(l_mobile_eci)},
  1098. {"tac", ROREG_FUNC(l_mobile_tac)},
  1099. {"scell", ROREG_FUNC(l_mobile_scell_extern_info)},
  1100. {"enbid", ROREG_FUNC(l_mobile_enbid)},
  1101. {"flymode", ROREG_FUNC(l_mobile_flymode)},
  1102. {"simid", ROREG_FUNC(l_mobile_simid)},
  1103. {"simPin", ROREG_FUNC(l_mobile_sim_pin)},
  1104. {"rtime", ROREG_FUNC(l_mobile_set_rrc_auto_release_time)},
  1105. {"setAuto", ROREG_FUNC(l_mobile_set_auto_work)},
  1106. {"getCellInfo", ROREG_FUNC(l_mobile_get_cell_info)},
  1107. {"reqCellInfo", ROREG_FUNC(l_mobile_request_cell_info)},
  1108. {"lockCell", ROREG_FUNC(l_mobile_lock_cell)},
  1109. {"reset", ROREG_FUNC(l_mobile_reset)},
  1110. {"dataTraffic", ROREG_FUNC(l_mobile_data_traffic)},
  1111. {"config", ROREG_FUNC(l_mobile_config)},
  1112. {"getBand", ROREG_FUNC(l_mobile_get_band)},
  1113. {"setBand", ROREG_FUNC(l_mobile_set_band)},
  1114. {"nstOnOff", ROREG_FUNC(l_mobile_nst_test_onoff)},
  1115. {"nstInput", ROREG_FUNC(l_mobile_nst_data_input)},
  1116. {"syncTime", ROREG_FUNC(l_mobile_sync_time)},
  1117. {"vsimInit", ROREG_FUNC(l_mobile_init_vsim)},
  1118. {"vsimOnOff", ROREG_FUNC(l_mobile_vsim_onoff)},
  1119. {"apnTableInit", ROREG_FUNC(l_mobile_init_apn_table)},
  1120. {"apnTableAdd", ROREG_FUNC(l_mobile_add_apn_table)},
  1121. {"apnTablePrint", ROREG_FUNC(l_mobile_print_apn_table)},
  1122. //@const UNREGISTER number 未注册
  1123. {"UNREGISTER", ROREG_INT(LUAT_MOBILE_STATUS_UNREGISTER)},
  1124. //@const REGISTERED number 已注册
  1125. {"REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_REGISTERED)},
  1126. //@const SEARCH number 正在搜索中
  1127. {"SEARCH", ROREG_INT(LUAT_MOBILE_STATUS_SEARCHING)},
  1128. //@const DENIED number 注册被拒绝
  1129. {"DENIED", ROREG_INT(LUAT_MOBILE_STATUS_DENIED)},
  1130. //@const UNKNOW number 未知
  1131. {"UNKNOW", ROREG_INT(LUAT_MOBILE_STATUS_UNKNOW)},
  1132. //@const REGISTERED_ROAMING number 已注册,漫游
  1133. {"REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_REGISTERED_ROAMING)},
  1134. //@const SMS_ONLY_REGISTERED number 已注册,仅SMS
  1135. {"SMS_ONLY_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED)},
  1136. //@const SMS_ONLY_REGISTERED_ROAMING number 已注册,漫游,仅SMS
  1137. {"SMS_ONLY_REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_SMS_ONLY_REGISTERED_ROAMING)},
  1138. //@const EMERGENCY_REGISTERED number 已注册,紧急服务
  1139. {"EMERGENCY_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_EMERGENCY_REGISTERED)},
  1140. //@const CSFB_NOT_PREFERRED_REGISTERED number 已注册,非主要服务
  1141. {"CSFB_NOT_PREFERRED_REGISTERED", ROREG_INT(LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED)},
  1142. //@const CSFB_NOT_PREFERRED_REGISTERED_ROAMING number 已注册,非主要服务,漫游
  1143. {"CSFB_NOT_PREFERRED_REGISTERED_ROAMING", ROREG_INT(LUAT_MOBILE_STATUS_CSFB_NOT_PREFERRED_REGISTERED_ROAMING)},
  1144. //@const CONF_RESELTOWEAKNCELL number 小区重选信号差值门限,需要飞行模式设置
  1145. {"CONF_RESELTOWEAKNCELL", ROREG_INT(MOBILE_CONF_RESELTOWEAKNCELL)},
  1146. //@const CONF_STATICCONFIG number 网络静态模式优化,需要飞行模式设置
  1147. {"CONF_STATICCONFIG", ROREG_INT(MOBILE_CONF_STATICCONFIG)},
  1148. //@const CONF_QUALITYFIRST number 网络切换以信号质量优先,需要飞行模式设置,0不开,1开启,2开启并加速切换,功耗会增加
  1149. {"CONF_QUALITYFIRST", ROREG_INT(MOBILE_CONF_QUALITYFIRST)},
  1150. //@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
  1151. {"CONF_USERDRXCYCLE", ROREG_INT(MOBILE_CONF_USERDRXCYCLE)},
  1152. //@const CONF_T3324MAXVALUE number PSM模式中的T3324时间,单位S
  1153. {"CONF_T3324MAXVALUE", ROREG_INT(MOBILE_CONF_T3324MAXVALUE)},
  1154. //@const CONF_PSM_MODE number PSM模式开关,0关,1开
  1155. {"CONF_PSM_MODE", ROREG_INT(MOBILE_CONF_PSM_MODE)},
  1156. //@const CONF_CE_MODE number attach模式,0为EPS ONLY 2为混合,遇到IMSI detach脱网问题,设置为0,注意设置为EPS ONLY时会取消短信功能
  1157. {"CONF_CE_MODE", ROREG_INT(MOBILE_CONF_CE_MODE)},
  1158. //@const CONF_SIM_WC_MODE number SIM写入次数的配置和读取
  1159. {"CONF_SIM_WC_MODE", ROREG_INT(MOBILE_CONF_SIM_WC_MODE)},
  1160. //@const CONF_FAKE_CELL_BARTIME number 伪基站禁止接入的时间,取值为0时取消,0xffff永久
  1161. {"CONF_FAKE_CELL_BARTIME", ROREG_INT(MOBILE_CONF_FAKE_CELL_BARTIME)},
  1162. //@const CONF_RESET_TO_FACTORY number 删除已保存的协议栈参数,重启后会使用默认配置
  1163. {"CONF_RESET_TO_FACTORY", ROREG_INT(MOBILE_CONF_RESET_TO_FACTORY)},
  1164. //@const CONF_USB_ETHERNET number 蜂窝网络模块的usb以太网卡控制,bit0开关,1开0关,bit1模式,1NAT0独立IP(在usb以太网卡开启前可以修改,开启过就不行),bit2协议1ECM,0RNDIS,飞行模式里设置
  1165. {"CONF_USB_ETHERNET", ROREG_INT(MOBILE_CONF_USB_ETHERNET)},
  1166. //@const CONF_DISABLE_NCELL_MEAS number 关闭邻区测量 1关,0开,除了功耗测试外不建议使用
  1167. {"CONF_DISABLE_NCELL_MEAS", ROREG_INT(MOBILE_CONF_DISABLE_NCELL_MEAS)},
  1168. //@const CONF_MAX_TX_POWER number 设置最大发射功率,0~23,必须在每次RRC=1时调用,RRC=0后会自动清除配置
  1169. {"CONF_MAX_TX_POWER", ROREG_INT(MOBILE_CONF_MAX_TX_POWER)},
  1170. //@const PIN_VERIFY number 验证PIN码操作
  1171. {"PIN_VERIFY", ROREG_INT(LUAT_SIM_PIN_VERIFY)},
  1172. //@const PIN_CHANGE number 更换PIN码操作
  1173. {"PIN_CHANGE", ROREG_INT(LUAT_SIM_PIN_CHANGE)},
  1174. //@const PIN_ENABLE number 使能PIN码验证
  1175. {"PIN_ENABLE", ROREG_INT(LUAT_SIM_PIN_ENABLE)},
  1176. //@const PIN_DISABLE number 关闭PIN码验证
  1177. {"PIN_DISABLE", ROREG_INT(LUAT_SIM_PIN_DISABLE)},
  1178. //@const PIN_UNBLOCK number 解锁PIN码
  1179. {"PIN_UNBLOCK", ROREG_INT(LUAT_SIM_PIN_UNBLOCK)},
  1180. {NULL, ROREG_INT(0)}
  1181. };
  1182. LUAMOD_API int luaopen_mobile( lua_State *L ) {
  1183. luat_newlib2(L, reg_mobile);
  1184. return 1;
  1185. }
  1186. static int l_mobile_event_handle(lua_State* L, void* ptr) {
  1187. LUAT_MOBILE_EVENT_E event = {0};
  1188. uint8_t index = 0;
  1189. uint8_t status = 0;
  1190. int ret = 0;
  1191. int16_t sIntraSearchP, sNonIntraSearchP, sIntraSearchQ, sNonIntraSearchQ;
  1192. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  1193. event = msg->arg1;
  1194. index = msg->arg2 >> 8;
  1195. status = msg->arg2 & 0xFF;
  1196. // luat_mobile_cell_info_t cell_info;
  1197. // luat_mobile_signal_strength_info_t signal_info;
  1198. // uint8_t csq, i;
  1199. // char imsi[20];
  1200. // char iccid[24] = {0};
  1201. if (lua_getglobal(L, "sys_pub") != LUA_TFUNCTION) {
  1202. return 0;
  1203. };
  1204. switch(event)
  1205. {
  1206. case LUAT_MOBILE_EVENT_CFUN:
  1207. break;
  1208. case LUAT_MOBILE_EVENT_SIM:
  1209. /*
  1210. @sys_pub mobile
  1211. sim卡状态变化
  1212. SIM_IND
  1213. @usage
  1214. sys.subscribe("SIM_IND", function(status, value)
  1215. -- status的取值有:
  1216. -- RDY SIM卡就绪, value为nil
  1217. -- NORDY 无SIM卡, value为nil
  1218. -- SIM_PIN 需要输入PIN, value为nil
  1219. -- GET_NUMBER 获取到电话号码(不一定有值), value为nil
  1220. -- SIM_WC SIM卡的写入次数统计,掉电归0, value为统计值
  1221. log.info("sim status", status, value)
  1222. end)
  1223. */
  1224. switch (status)
  1225. {
  1226. case LUAT_MOBILE_SIM_READY:
  1227. lua_pushstring(L, "SIM_IND");
  1228. lua_pushstring(L, "RDY");
  1229. lua_call(L, 2, 0);
  1230. break;
  1231. case LUAT_MOBILE_NO_SIM:
  1232. lua_pushstring(L, "SIM_IND");
  1233. lua_pushstring(L, "NORDY");
  1234. lua_call(L, 2, 0);
  1235. break;
  1236. case LUAT_MOBILE_SIM_NEED_PIN:
  1237. lua_pushstring(L, "SIM_IND");
  1238. lua_pushstring(L, "SIM_PIN");
  1239. lua_call(L, 2, 0);
  1240. break;
  1241. case LUAT_MOBILE_SIM_NUMBER:
  1242. lua_pushstring(L, "SIM_IND");
  1243. lua_pushstring(L, "GET_NUMBER");
  1244. lua_call(L, 2, 0);
  1245. break;
  1246. case LUAT_MOBILE_SIM_WC:
  1247. lua_pushstring(L, "SIM_IND");
  1248. lua_pushstring(L, "SIM_WC");
  1249. uint32_t tmp = (uint32_t)ptr;
  1250. lua_pushinteger(L, tmp);
  1251. lua_call(L, 3, 0);
  1252. break;
  1253. default:
  1254. break;
  1255. }
  1256. break;
  1257. case LUAT_MOBILE_EVENT_REGISTER_STATUS:
  1258. break;
  1259. case LUAT_MOBILE_EVENT_CELL_INFO:
  1260. switch (status)
  1261. {
  1262. case LUAT_MOBILE_CELL_INFO_UPDATE:
  1263. /*
  1264. @sys_pub mobile
  1265. 基站数据已更新
  1266. CELL_INFO_UPDATE
  1267. @usage
  1268. -- 订阅式
  1269. sys.subscribe("CELL_INFO_UPDATE", function()
  1270. log.info("cell", json.encode(mobile.getCellInfo()))
  1271. end)
  1272. */
  1273. lua_pushstring(L, "CELL_INFO_UPDATE");
  1274. lua_call(L, 1, 0);
  1275. break;
  1276. case LUAT_MOBILE_SERVICE_CELL_UPDATE:
  1277. /*
  1278. @sys_pub mobile
  1279. 服务小区额外信息更新
  1280. SCELL_INFO
  1281. @usage
  1282. -- 订阅式
  1283. sys.subscribe("SCELL_INFO", function()
  1284. log.info("service cell", mobile.scell()))
  1285. end)
  1286. */
  1287. lua_pushstring(L, "SCELL_INFO");
  1288. lua_call(L, 1, 0);
  1289. default:
  1290. break;
  1291. }
  1292. break;
  1293. case LUAT_MOBILE_EVENT_PDP:
  1294. LLOGD("cid%d, state%d", index, status);
  1295. break;
  1296. case LUAT_MOBILE_EVENT_NETIF:
  1297. switch (status)
  1298. {
  1299. case LUAT_MOBILE_NETIF_LINK_ON:
  1300. LLOGD("NETIF_LINK_ON -> IP_READY");
  1301. /*
  1302. @sys_pub mobile
  1303. 已联网
  1304. IP_READY
  1305. @usage
  1306. -- 联网后会发一次这个消息
  1307. sys.subscribe("IP_READY", function(ip, adapter)
  1308. log.info("mobile", "IP_READY", ip, (adapter or -1) == socket.LWIP_GP)
  1309. end)
  1310. */
  1311. lua_pushstring(L, "IP_READY");
  1312. luat_ip_addr_t local_ip, net_mask, gate_way, ipv6;
  1313. #ifdef LUAT_USE_LWIP
  1314. ipv6.type = 0xff;
  1315. ret = network_get_full_local_ip_info(NULL, NW_ADAPTER_INDEX_LWIP_GPRS, &local_ip, &net_mask, &gate_way, &ipv6);
  1316. #else
  1317. void* userdata = NULL;
  1318. network_adapter_info* info = network_adapter_fetch(NW_ADAPTER_INDEX_LWIP_GPRS, &userdata);
  1319. if (info == NULL)
  1320. ret = -1;
  1321. else
  1322. ret = info->get_local_ip_info(&local_ip, &net_mask, &gate_way, userdata);
  1323. #endif
  1324. if (ret == 0) {
  1325. #ifdef LUAT_USE_LWIP
  1326. lua_pushfstring(L, "%s", ipaddr_ntoa(&local_ip));
  1327. #else
  1328. 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);
  1329. #endif
  1330. }
  1331. else {
  1332. lua_pushliteral(L, "0.0.0.0");
  1333. }
  1334. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
  1335. lua_call(L, 3, 0);
  1336. break;
  1337. case LUAT_MOBILE_NETIF_LINK_OFF:
  1338. LLOGD("NETIF_LINK_OFF -> IP_LOSE");
  1339. /*
  1340. @sys_pub mobile
  1341. 已断网
  1342. IP_LOSE
  1343. @usage
  1344. -- 断网后会发一次这个消息
  1345. sys.subscribe("IP_LOSE", function(adapter)
  1346. log.info("mobile", "IP_LOSE", (adapter or -1) == socket.LWIP_GP)
  1347. end)
  1348. */
  1349. lua_pushstring(L, "IP_LOSE");
  1350. lua_pushinteger(L, NW_ADAPTER_INDEX_LWIP_GPRS);
  1351. lua_call(L, 2, 0);
  1352. break;
  1353. default:
  1354. break;
  1355. }
  1356. break;
  1357. case LUAT_MOBILE_EVENT_TIME_SYNC:
  1358. /*
  1359. @sys_pub mobile
  1360. 时间已经同步
  1361. NTP_UPDATE
  1362. @usage
  1363. -- 对于电信/移动的卡, 联网后,基站会下发时间,但联通卡不会,务必留意
  1364. sys.subscribe("NTP_UPDATE", function()
  1365. log.info("mobile", "time", os.date())
  1366. end)
  1367. */
  1368. LLOGD("TIME_SYNC %d", status);
  1369. lua_pushstring(L, "NTP_UPDATE");
  1370. lua_call(L, 1, 0);
  1371. break;
  1372. case LUAT_MOBILE_EVENT_CSCON:
  1373. // LLOGD("CSCON %d", status);
  1374. /*
  1375. @sys_pub mobile
  1376. RRC状态
  1377. CSCON
  1378. @usage
  1379. -- state 1 CONNECT 0 IDLE
  1380. sys.subscribe("CSCON", function(state)
  1381. log.info("mobile", "CSCON", state)
  1382. end)
  1383. */
  1384. lua_pushstring(L, "CSCON");
  1385. lua_pushinteger(L, status);
  1386. lua_call(L, 2, 0);
  1387. break;
  1388. case LUAT_MOBILE_EVENT_BEARER:
  1389. LLOGD("bearer act %d, result %d",status, index);
  1390. break;
  1391. case LUAT_MOBILE_EVENT_SMS:
  1392. switch(status)
  1393. {
  1394. case LUAT_MOBILE_SMS_READY:
  1395. LLOGI("sim%d sms ready", index);
  1396. lua_pushstring(L, "SMS_READY");
  1397. lua_pushinteger(L, index);
  1398. lua_call(L, 2, 0);
  1399. break;
  1400. case LUAT_MOBILE_NEW_SMS:
  1401. break;
  1402. case LUAT_MOBILE_SMS_SEND_DONE:
  1403. break;
  1404. case LUAT_MOBILE_SMS_ACK:
  1405. break;
  1406. }
  1407. break;
  1408. case LUAT_MOBILE_EVENT_IMS_REGISTER_STATUS:
  1409. LLOGD("ims reg state %d", status);
  1410. lua_pushstring(L, "IMS_REG");
  1411. lua_pushinteger(L, status);
  1412. lua_call(L, 2, 0);
  1413. break;
  1414. case LUAT_MOBILE_EVENT_CC:
  1415. LLOGD("LUAT_MOBILE_EVENT_CC status %d",status);
  1416. /*
  1417. @sys_pub mobile
  1418. 通话状态变化
  1419. CC_IND
  1420. @usage
  1421. sys.subscribe("CC_IND", function(status, value)
  1422. log.info("cc status", status, value)
  1423. end)
  1424. */
  1425. switch(status){
  1426. case LUAT_MOBILE_CC_READY:
  1427. LLOGD("LUAT_MOBILE_CC_READY");
  1428. lua_pushstring(L, "CC_IND");
  1429. lua_pushstring(L, "READY");
  1430. lua_call(L, 2, 0);
  1431. break;
  1432. case LUAT_MOBILE_CC_INCOMINGCALL:
  1433. lua_pushstring(L, "CC_IND");
  1434. lua_pushstring(L, "INCOMINGCALL");
  1435. lua_call(L, 2, 0);
  1436. break;
  1437. case LUAT_MOBILE_CC_CALL_NUMBER:
  1438. // lua_pushstring(L, "CC_IND");
  1439. // lua_pushstring(L, "CALL_NUMBER");
  1440. // lua_call(L, 2, 0);
  1441. break;
  1442. case LUAT_MOBILE_CC_CONNECTED_NUMBER:
  1443. // lua_pushstring(L, "CC_IND");
  1444. // lua_pushstring(L, "CONNECTED_NUMBER");
  1445. // lua_call(L, 2, 0);
  1446. break;
  1447. case LUAT_MOBILE_CC_CONNECTED:
  1448. lua_pushstring(L, "CC_IND");
  1449. lua_pushstring(L, "CONNECTED");
  1450. lua_call(L, 2, 0);
  1451. break;
  1452. case LUAT_MOBILE_CC_DISCONNECTED:
  1453. lua_pushstring(L, "CC_IND");
  1454. lua_pushstring(L, "DISCONNECTED");
  1455. lua_call(L, 2, 0);
  1456. break;
  1457. case LUAT_MOBILE_CC_SPEECH_START:
  1458. lua_pushstring(L, "CC_IND");
  1459. lua_pushstring(L, "SPEECH_START");
  1460. lua_call(L, 2, 0);
  1461. break;
  1462. case LUAT_MOBILE_CC_MAKE_CALL_OK:
  1463. lua_pushstring(L, "CC_IND");
  1464. lua_pushstring(L, "MAKE_CALL_OK");
  1465. lua_call(L, 2, 0);
  1466. break;
  1467. case LUAT_MOBILE_CC_MAKE_CALL_FAILED:
  1468. lua_pushstring(L, "CC_IND");
  1469. lua_pushstring(L, "MAKE_CALL_FAILED");
  1470. lua_call(L, 2, 0);
  1471. break;
  1472. case LUAT_MOBILE_CC_ANSWER_CALL_DONE:
  1473. lua_pushstring(L, "CC_IND");
  1474. lua_pushstring(L, "ANSWER_CALL_DONE");
  1475. lua_call(L, 2, 0);
  1476. break;
  1477. case LUAT_MOBILE_CC_HANGUP_CALL_DONE:
  1478. lua_pushstring(L, "CC_IND");
  1479. lua_pushstring(L, "HANGUP_CALL_DONE");
  1480. lua_call(L, 2, 0);
  1481. break;
  1482. case LUAT_MOBILE_CC_LIST_CALL_RESULT:
  1483. lua_pushstring(L, "CC_IND");
  1484. lua_pushstring(L, "LIST_CALL_RESULT");
  1485. lua_call(L, 2, 0);
  1486. break;
  1487. case LUAT_MOBILE_CC_PLAY:// 最先
  1488. lua_pushstring(L, "CC_IND");
  1489. lua_pushstring(L, "PLAY");
  1490. lua_call(L, 2, 0);
  1491. break;
  1492. }
  1493. break;
  1494. case LUAT_MOBILE_EVENT_RRC:
  1495. // LLOGD("LUAT_MOBILE_EVENT_RRC status %d",status);
  1496. /*
  1497. @sys_pub mobile
  1498. RRC部分信息上报,2025/9/15启用
  1499. RRC_IND
  1500. @usage
  1501. sys.subscribe("RRC_IND", function(event, value, ...)
  1502. log.info("rrc status", event, value, ...)
  1503. end)
  1504. event目前有
  1505. 1、"DRX",DRX周期值,后续跟1个参数为具体的DRX周期值,单位ms,目前只有320,640,1280,2560
  1506. 2、"IDLE_MEAS_THRESHOLD",RRC IDLE下邻区测量阈值,后续跟4个参数为具体的测量阈值,单位dbm
  1507. 4个参数分别为sIntraSearchP, sNonIntraSearchP, sIntraSearchQ, sNonIntraSearchQ
  1508. 当rsrp <= sIntraSearchP,启动同频邻区测量,低功耗下功耗有所升高
  1509. 当rsrp <= sNonIntraSearchP,启动异频邻区测量,低功耗下功耗显著升高
  1510. 如果sIntraSearchQ不为0,当rsrq <= sIntraSearchQ,启动同频邻区测量,低功耗下功耗有所升高
  1511. 如果sNonIntraSearchQ不为0,当rsrq <= sNonIntraSearchQ,启动异频邻区测量,低功耗下功耗显著升高
  1512. */
  1513. switch(status)
  1514. {
  1515. case LUAT_MOBILE_RRC_DRX_CYCLE_UPDATED:
  1516. lua_pushstring(L, "RRC_IND");
  1517. lua_pushstring(L, "DRX");
  1518. lua_pushinteger(L, index * 320);
  1519. lua_call(L, 3, 0);
  1520. break;
  1521. case LUAT_MOBILE_RRC_IDLE_MEAS_THRESHOLD:
  1522. lua_pushstring(L, "RRC_IND");
  1523. lua_pushstring(L, "IDLE_MEAS_THRESHOLD");
  1524. luat_mobile_rrc_get_idle_meas_threshold(&sIntraSearchP, &sNonIntraSearchP, &sIntraSearchQ, &sNonIntraSearchQ);
  1525. lua_pushinteger(L, sIntraSearchP);
  1526. lua_pushinteger(L, sNonIntraSearchP);
  1527. lua_pushinteger(L, sIntraSearchQ);
  1528. lua_pushinteger(L, sNonIntraSearchQ);
  1529. lua_call(L, 6, 0);
  1530. break;
  1531. }
  1532. break;
  1533. default:
  1534. break;
  1535. }
  1536. return 0;
  1537. }
  1538. void luat_mobile_event_cb(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status, void* ptr) {
  1539. #if defined LUAT_USE_VOLTE
  1540. switch (event){
  1541. case LUAT_MOBILE_EVENT_CC:
  1542. switch(status){
  1543. case LUAT_MOBILE_CC_SPEECH_START:
  1544. luat_cc_start_speech(index+1);
  1545. break;
  1546. case LUAT_MOBILE_CC_PLAY:
  1547. luat_cc_play_tone(index);
  1548. break;
  1549. }
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. #endif
  1555. #if defined LUAT_USE_AIRLINK_EXEC_MOBILE
  1556. luat_airlink_mobile_event_callback(event, index, status, ptr);
  1557. #endif
  1558. rtos_msg_t msg = {
  1559. .handler = l_mobile_event_handle,
  1560. .arg1 = event,
  1561. .arg2 = (index << 8) + status ,
  1562. .ptr = ptr
  1563. };
  1564. luat_msgbus_put(&msg, 0);
  1565. }