luat_lib_mobile.c 54 KB

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