sim.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. --- 模块功能:查询sim卡状态、iccid、imsi、mcc、mnc
  2. -- @module sim
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.02.13
  7. require "ril"
  8. require "sys"
  9. module(..., package.seeall)
  10. local req = ril.request
  11. --sim卡的imsi、sim卡的iccid
  12. local imsi, iccid, status
  13. local sNumber,bQueryNumber = ""
  14. local simCross,setSimCrossCbFnc
  15. --- 获取sim卡的iccid
  16. -- @return string ,返回iccid,如果还没有读取出来,则返回nil
  17. -- @usage 注意:开机lua脚本运行之后,会发送at命令去查询iccid,所以需要一定时间才能获取到iccid。开机后立即调用此接口,基本上返回nil
  18. -- @usage sim.getIccid()
  19. function getIccid()
  20. return iccid
  21. end
  22. --- 获取sim卡的imsi
  23. -- @return string ,返回imsi,如果还没有读取出来,则返回nil
  24. -- @usage 开机lua脚本运行之后,会发送at命令去查询imsi,所以需要一定时间才能获取到imsi。开机后立即调用此接口,基本上返回nil
  25. -- @usage sim.getImsi()
  26. function getImsi()
  27. return imsi
  28. end
  29. --- 获取sim卡的mcc
  30. -- @return string ,返回值:mcc,如果还没有读取出来,则返回""
  31. -- @usage 注意:开机lua脚本运行之后,会发送at命令去查询imsi,所以需要一定时间才能获取到imsi。开机后立即调用此接口,基本上返回""
  32. -- @usage sim.getMcc()
  33. function getMcc()
  34. return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 1, 3) or ""
  35. end
  36. --- 获取sim卡的getmnc
  37. -- @return string ,返回mnc,如果还没有读取出来,则返回""
  38. -- @usage 注意:开机lua脚本运行之后,会发送at命令去查询imsi,所以需要一定时间才能获取到imsi。开机后立即调用此接口,基本上返回""
  39. -- @usage sim.getMnc()
  40. function getMnc()
  41. return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 4, 5) or ""
  42. end
  43. --- 获取sim卡的状态
  44. -- @return bool ,true表示sim卡正常,false或者nil表示未检测到卡或者卡异常
  45. -- @usage 开机lua脚本运行之后,会发送at命令去查询状态,所以需要一定时间才能获取到状态。开机后立即调用此接口,基本上返回nil
  46. -- @usage sim.getStatus()
  47. function getStatus()
  48. return status
  49. end
  50. --- 设置“是否打开查询本机号码”的功能
  51. -- @bool flag 开启或者关闭查询功能的标志,false或者nil为关闭,其余为开启
  52. -- @return nil
  53. -- @usage sim.setQueryNumber(true)
  54. function setQueryNumber(flag)
  55. bQueryNumber = flag
  56. end
  57. --- 获取sim卡的本机号码
  58. -- @return string ,返回值:sNumber,如果还没有读取出来或者读取失败,则返回""
  59. -- @usage 注意:开机lua脚本运行之后,会发送at命令去查询本机号码,所以需要一定时间才能获取到本机号码。开机后立即调用此接口,基本上返回""
  60. -- @usage 注意:此功能需要卡商支持,卡商必须把卡写到sim卡中,模块才能从卡中读出号码;目前市场上的很多卡,没有写入号码,是无法读取得
  61. -- @usage sim.getNumber()
  62. function getNumber()
  63. return sNumber or ""
  64. end
  65. --[[
  66. 函数名:rsp
  67. 功能 :本功能模块内“通过虚拟串口发送到底层core软件的AT命令”的应答处理
  68. 参数 :
  69. cmd:此应答对应的AT命令
  70. success:AT命令执行结果,true或者false
  71. response:AT命令的应答中的执行结果字符串
  72. intermediate:AT命令的应答中的中间信息
  73. 返回值:无
  74. ]]
  75. local function rsp(cmd, success, response, intermediate)
  76. if cmd == "AT+ICCID" then
  77. if intermediate then
  78. iccid = string.match(intermediate, "%+ICCID: (.+)")
  79. end
  80. elseif cmd == "AT+SIMCROSS?" then
  81. if success then
  82. simCross = tonumber(intermediate:match("%+SIMCROSS:%s*(%d)"))
  83. end
  84. if setSimCrossCbFnc then setSimCrossCbFnc(success) end
  85. elseif cmd:match("AT%+SIMCROSS=") then
  86. if success then
  87. req("AT+SIMCROSS?")
  88. else
  89. if setSimCrossCbFnc then setSimCrossCbFnc(false) end
  90. end
  91. elseif cmd == "AT+CIMI" then
  92. imsi = intermediate
  93. --产生一个内部消息IMSI_READY,通知已经读取imsi
  94. sys.publish("IMSI_READY")
  95. elseif cmd == "AT+CNUM" then
  96. if success then
  97. if intermediate then sNumber = intermediate:match("%+CNUM:%s*\".-\",\"[%+]*(%d+)\",") end
  98. else
  99. sys.timerStart(ril.request,5000,"AT+CNUM")
  100. end
  101. end
  102. end
  103. --[[
  104. 函数名:urc
  105. -- 功能 :本功能模块内“注册的底层core通过虚拟串口主动上报的通知”的处理
  106. 参数 :
  107. data:通知的完整字符串信息
  108. prefix:通知的前缀
  109. 返回值:无
  110. ]]
  111. local function urc(data, prefix)
  112. --sim卡状态通知
  113. if prefix == "+CPIN" then
  114. status = false
  115. --sim卡正常
  116. if data == "+CPIN: READY" then
  117. status = true
  118. ril.request("AT+ICCID")
  119. ril.request("AT+CIMI")
  120. if bQueryNumber then ril.request("AT+CNUM") end
  121. sys.publish("SIM_IND", "RDY")
  122. --未检测到sim卡
  123. elseif data == "+CPIN: NOT INSERTED" then
  124. sys.publish("SIM_IND", "NIST")
  125. else
  126. --sim卡pin开启
  127. if data == "+CPIN: SIM PIN" then
  128. sys.publish("SIM_IND","SIM_PIN")
  129. end
  130. sys.publish("SIM_IND", "NORDY")
  131. end
  132. end
  133. end
  134. function set2gSim()
  135. ril.request("AT+MEDCR=0,8,1")
  136. ril.request("AT+MEDCR=0,17,240")
  137. ril.request("AT+MEDCR=0,19,1")
  138. end
  139. --- 设置双卡单待sim id
  140. -- @number id 双卡单待的simid,仅支持0和1
  141. -- @function[opt=nil] cbFnc 设置结果回调函数,回调函数的调用形式为:
  142. -- cnFnc(result),result为true表示成功,false或者nil为失败
  143. -- @return nil
  144. -- @usage
  145. -- sim.setId(0)
  146. -- sim.setId(1,cbFnc)
  147. function setId(id,cbFnc)
  148. if id ~= simCross then
  149. setSimCrossCbFnc = cbFnc
  150. ril.request("AT+SIMCROSS="..id)
  151. else
  152. if cbFnc then cbFnc(true) end
  153. end
  154. end
  155. --- 获取目前设置的双卡单待id
  156. -- @return number ,返回id(0或者1),如果还没有读取出来,则返回nil
  157. -- @usage 注意:开机lua脚本运行之后,会发送at命令去查询id,所以需要一定时间才能获取到id。开机后立即调用此接口,基本上返回nil
  158. -- @usage sim.getId()
  159. function getId()
  160. return simCross
  161. end
  162. --注册AT+CCID命令的应答处理函数
  163. ril.regRsp("+ICCID", rsp)
  164. --注册AT+CIMI命令的应答处理函数
  165. ril.regRsp("+CIMI", rsp)
  166. ril.regRsp("+CNUM", rsp)
  167. ril.regRsp("+SIMCROSS", rsp)
  168. --注册+CPIN通知的处理函数
  169. ril.regUrc("+CPIN", urc)
  170. ril.request("AT+SIMCROSS?")