net.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. ---模块功能:网络管理、信号查询、GSM网络状态查询、网络指示灯控制、临近小区信息查询
  2. -- @module net
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.02.17
  7. require "sys"
  8. require "ril"
  9. require "pio"
  10. require "sim"
  11. require "log"
  12. module(..., package.seeall)
  13. --加载常用的全局函数至本地
  14. local publish = sys.publish
  15. --GSM网络状态:
  16. --INIT:开机初始化中的状态
  17. --REGISTERED:注册上GSM网络
  18. --UNREGISTER:未注册上GSM网络
  19. local state = "INIT"
  20. --SIM卡状态:true为异常,false或者nil为正常
  21. local simerrsta
  22. -- 飞行模式状态
  23. flyMode = false
  24. --lac:位置区ID
  25. --ci:小区ID
  26. --rssi:信号强度
  27. local lac, ci, rssi = "", "", 0
  28. --cellinfo:当前小区和临近小区信息表
  29. --multicellcb:获取多小区的回调函数
  30. local cellinfo, multicellcb = {}
  31. --注册标志参数,creg3:true为没注册,为false为注册成功
  32. --local creg3
  33. --[[
  34. 函数名:checkCRSM
  35. 功能:如果注册被拒绝,运行此函数,先判断是否取得imsi号,再判断是否是中国移动卡
  36. 如果确定是中国移动卡,则进行SIM卡限制访问
  37. 参数:
  38. 返回值:
  39. ]]
  40. --[[
  41. local function checkCRSM()
  42. local imsi = sim.getImsi()
  43. if imsi and imsi ~= "" then
  44. if string.sub(imsi, 1, 3) == "460" then
  45. local mnc = string.sub(imsi, 4, 5)
  46. if (mnc == "00" or mnc == "02" or mnc == "04" or mnc == "07") and creg3 then
  47. ril.request("AT+CRSM=176,28539,0,0,12")
  48. end
  49. end
  50. else
  51. sys.timerStart(checkCRSM, 5000)
  52. end
  53. end
  54. ]]
  55. --[[
  56. 函数名:creg
  57. 功能 :解析CREG信息
  58. 参数 :data:CREG信息字符串,例如+CREG: 2、+CREG: 1,"18be","93e1"、+CREG: 5,"18a7","cb51"
  59. 返回值:无
  60. ]]
  61. local function creg(data)
  62. local p1, s
  63. --获取注册状态
  64. _, _, p1 = string.find(data, "%d,(%d)")
  65. if p1 == nil then
  66. _, _, p1 = string.find(data, "(%d)")
  67. if p1 == nil then
  68. return
  69. end
  70. end
  71. --creg3 = false
  72. --已注册
  73. if p1 == "1" or p1 == "5" then
  74. s = "REGISTERED"
  75. --未注册
  76. else
  77. --[[
  78. if p1 == "3" then
  79. creg3 = true
  80. checkCRSM()
  81. end
  82. ]]
  83. s = "UNREGISTER"
  84. end
  85. --注册状态发生了改变
  86. if s ~= state then
  87. --临近小区查询处理
  88. if s == "REGISTERED" then
  89. --产生一个内部消息NET_STATE_CHANGED,表示GSM网络注册状态发生变化
  90. publish("NET_STATE_REGISTERED")
  91. cengQueryPoll()
  92. end
  93. state = s
  94. end
  95. --已注册并且lac或ci发生了变化
  96. if state == "REGISTERED" then
  97. p2, p3 = string.match(data, "\"(%x+)\",\"(%x+)\"")
  98. if lac ~= p2 or ci ~= p3 then
  99. lac = p2
  100. ci = p3
  101. --产生一个内部消息NET_CELL_CHANGED,表示lac或ci发生了变化
  102. publish("NET_CELL_CHANGED")
  103. end
  104. end
  105. end
  106. --[[
  107. 函数名:resetcellinfo
  108. 功能 :重置当前小区和临近小区信息表
  109. 参数 :无
  110. 返回值:无
  111. ]]
  112. local function resetCellInfo()
  113. local i
  114. cellinfo.cnt = 11 --最大个数
  115. for i = 1, cellinfo.cnt do
  116. cellinfo[i] = {}
  117. cellinfo[i].mcc, cellinfo[i].mnc = nil
  118. cellinfo[i].lac = 0
  119. cellinfo[i].ci = 0
  120. cellinfo[i].rssi = 0
  121. cellinfo[i].ta = 0
  122. end
  123. end
  124. --[[
  125. 函数名:ceng
  126. 功能 :解析当前小区和临近小区信息
  127. 参数 :
  128. data:当前小区和临近小区信息字符串,例如下面中的每一行:
  129. +CENG:1,1
  130. +CENG:0,"573,24,99,460,0,13,49234,10,0,6311,255"
  131. +CENG:1,"579,16,460,0,5,49233,6311"
  132. +CENG:2,"568,14,460,0,26,0,6311"
  133. +CENG:3,"584,13,460,0,10,0,6213"
  134. +CENG:4,"582,13,460,0,51,50146,6213"
  135. +CENG:5,"11,26,460,0,3,52049,6311"
  136. +CENG:6,"29,26,460,0,32,0,6311"
  137. 返回值:无
  138. ]]
  139. local function ceng(data)
  140. --只处理有效的CENG信息
  141. if string.find(data, "%+CENG:%d+,\".+\"") then
  142. local id, rssi, lac, ci, ta, mcc, mnc
  143. id = string.match(data, "%+CENG:(%d)")
  144. id = tonumber(id)
  145. --第一条CENG信息和其余的格式不同
  146. if id == 0 then
  147. rssi, mcc, mnc, ci, lac, ta = string.match(data, "%+CENG: *%d, *\"%d+, *(%d+), *%d+, *(%d+), *(%d+), *%d+, *(%d+), *%d+, *%d+, *(%d+), *(%d+)\"")
  148. else
  149. rssi, mcc, mnc, ci, lac, ta = string.match(data, "%+CENG: *%d, *\"%d+, *(%d+), *(%d+), *(%d+), *%d+, *(%d+), *(%d+)\"")
  150. end
  151. --解析正确
  152. if rssi and ci and lac and mcc and mnc then
  153. --如果是第一条,清除信息表
  154. if id == 0 then
  155. resetCellInfo()
  156. end
  157. --保存mcc、mnc、lac、ci、rssi、ta
  158. cellinfo[id + 1].mcc = mcc
  159. cellinfo[id + 1].mnc = mnc
  160. cellinfo[id + 1].lac = tonumber(lac)
  161. cellinfo[id + 1].ci = tonumber(ci)
  162. cellinfo[id + 1].rssi = (tonumber(rssi) == 99) and 0 or tonumber(rssi)
  163. cellinfo[id + 1].ta = tonumber(ta or "0")
  164. --产生一个内部消息CELL_INFO_IND,表示读取到了新的当前小区和临近小区信息
  165. if id == 0 then
  166. if multicellcb then multicellcb(cellinfo) end
  167. publish("CELL_INFO_IND", cellinfo)
  168. end
  169. end
  170. end
  171. end
  172. -- crsm更新计数
  173. --local crsmUpdCnt = 0
  174. -- 更新FPLMN的应答处理
  175. -- @string cmd ,此应答对应的AT命令
  176. -- @bool success ,AT命令执行结果,true或者false
  177. -- @string response ,AT命令的应答中的执行结果字符串
  178. -- @string intermediate ,AT命令的应答中的中间信息
  179. -- @return 无
  180. --[[
  181. function crsmResponse(cmd, success, response, intermediate)
  182. log.debug("net.crsmResponse", success)
  183. if success then
  184. sys.restart("net.crsmResponse suc")
  185. else
  186. crsmUpdCnt = crsmUpdCnt + 1
  187. if crsmUpdCnt >= 3 then
  188. sys.restart("net.crsmResponse tmout")
  189. else
  190. ril.request("AT+CRSM=214,28539,0,0,12,\"64f01064f03064f002fffff\"", nil, crsmResponse)
  191. end
  192. end
  193. end
  194. ]]
  195. --[[
  196. 函数名:neturc
  197. 功能 :本功能模块内“注册的底层core通过虚拟串口主动上报的通知”的处理
  198. 参数 :
  199. data:通知的完整字符串信息
  200. prefix:通知的前缀
  201. 返回值:无
  202. ]]
  203. local function neturc(data, prefix)
  204. if prefix == "+CREG" then
  205. --收到网络状态变化时,更新一下信号值
  206. csqQueryPoll()
  207. --解析creg信息
  208. creg(data)
  209. elseif prefix == "+CENG" then
  210. --解析ceng信息
  211. ceng(data)
  212. --[[elseif prefix == "+CRSM" then
  213. local str = string.lower(data)
  214. if string.match(str, "64f000") or string.match(str, "64f020") or string.match(str, "64f040") or string.match(str, "64f070") then
  215. ril.request("AT+CRSM=214,28539,0,0,12,\"64f01064f03064f002fffff\"", nil, crsmResponse)
  216. end]]
  217. end
  218. end
  219. --- 设置飞行模式
  220. -- @bool mode,true:飞行模式开,false:飞行模式关
  221. -- @return nil
  222. -- @usage net.switchFly(mode)
  223. function switchFly(mode)
  224. if flyMode == mode then return end
  225. flyMode = mode
  226. -- 处理飞行模式
  227. if mode then
  228. ril.request("AT+CFUN=0")
  229. -- 处理退出飞行模式
  230. else
  231. ril.request("AT+CFUN=1")
  232. --处理查询定时器
  233. csqQueryPoll()
  234. cengQueryPoll()
  235. --复位GSM网络状态
  236. neturc("2", "+CREG")
  237. end
  238. end
  239. --- 获取GSM网络注册状态
  240. -- @return string state,GSM网络注册状态,
  241. -- "INIT"表示正在初始化
  242. -- "REGISTERED"表示已注册
  243. -- "UNREGISTER"表示未注册
  244. -- @usage net.getState()
  245. function getState()
  246. return state
  247. end
  248. --- 获取当前小区的mcc
  249. -- @return string mcc,当前小区的mcc,如果还没有注册GSM网络,则返回sim卡的mcc
  250. -- @usage net.getMcc()
  251. function getMcc()
  252. return cellinfo[1].mcc or sim.getMcc()
  253. end
  254. --- 获取当前小区的mnc
  255. -- @return string mcn,当前小区的mnc,如果还没有注册GSM网络,则返回sim卡的mnc
  256. -- @usage net.getMnc()
  257. function getMnc()
  258. return cellinfo[1].mnc or sim.getMnc()
  259. end
  260. --- 获取当前位置区ID
  261. -- @return string lac,当前位置区ID(16进制字符串,例如"18be"),如果还没有注册GSM网络,则返回""
  262. -- @usage net.getLac()
  263. function getLac()
  264. return lac
  265. end
  266. --- 获取当前小区ID
  267. -- @return string ci,当前小区ID(16进制字符串,例如"93e1"),如果还没有注册GSM网络,则返回""
  268. -- @usage net.getCi()
  269. function getCi()
  270. return ci
  271. end
  272. --- 获取信号强度
  273. -- @return number rssi,当前信号强度(取值范围0-31)
  274. -- @usage net.getRssi()
  275. function getRssi()
  276. return rssi
  277. end
  278. --- 获取当前和临近位置区、小区以及信号强度的拼接字符串
  279. -- @return string cellInfo,当前和临近位置区、小区以及信号强度的拼接字符串,例如:"6311.49234.30;6311.49233.23;6322.49232.18;"
  280. -- @usage net.getCellInfo()
  281. function getCellInfo()
  282. local i, ret = 1, ""
  283. for i = 1, cellinfo.cnt do
  284. if cellinfo[i] and cellinfo[i].lac and cellinfo[i].lac ~= 0 and cellinfo[i].ci and cellinfo[i].ci ~= 0 then
  285. ret = ret .. cellinfo[i].lac .. "." .. cellinfo[i].ci .. "." .. cellinfo[i].rssi .. ";"
  286. end
  287. end
  288. return ret
  289. end
  290. --- 获取当前和临近位置区、小区、mcc、mnc、以及信号强度的拼接字符串
  291. -- @return string cellInfo,当前和临近位置区、小区、mcc、mnc、以及信号强度的拼接字符串,例如:"460.01.6311.49234.30;460.01.6311.49233.23;460.02.6322.49232.18;"
  292. -- @usage net.getCellInfoExt()
  293. function getCellInfoExt(dbm)
  294. local i, ret = 1, ""
  295. for i = 1, cellinfo.cnt do
  296. if cellinfo[i] and cellinfo[i].mcc and cellinfo[i].mnc and cellinfo[i].lac and cellinfo[i].lac ~= 0 and cellinfo[i].ci and cellinfo[i].ci ~= 0 then
  297. ret = ret .. cellinfo[i].mcc .. "." .. cellinfo[i].mnc .. "." .. cellinfo[i].lac .. "." .. cellinfo[i].ci .. "." .. (dbm and (cellinfo[i].rssi*2-113) or cellinfo[i].rssi) .. ";"
  298. end
  299. end
  300. return ret
  301. end
  302. --- 获取TA值
  303. -- @return number ta,TA值
  304. -- @usage net.getTa()
  305. function getTa()
  306. return cellinfo[1].ta
  307. end
  308. --[[
  309. 函数名:rsp
  310. 功能 :本功能模块内“通过虚拟串口发送到底层core软件的AT命令”的应答处理
  311. 参数 :
  312. cmd:此应答对应的AT命令
  313. success:AT命令执行结果,true或者false
  314. response:AT命令的应答中的执行结果字符串
  315. intermediate:AT命令的应答中的中间信息
  316. 返回值:无
  317. ]]
  318. local function rsp(cmd, success, response, intermediate)
  319. local prefix = string.match(cmd, "AT(%+%u+)")
  320. log.info("net.rsp",cmd, success, response, intermediate)
  321. if prefix == "+CSQ" then
  322. if intermediate ~= nil then
  323. local s = string.match(intermediate, "+CSQ:%s*(%d+)")
  324. if s ~= nil then
  325. rssi = tonumber(s)
  326. rssi = rssi == 99 and 0 or rssi
  327. --产生一个内部消息GSM_SIGNAL_REPORT_IND,表示读取到了信号强度
  328. publish("GSM_SIGNAL_REPORT_IND", success, rssi)
  329. end
  330. end
  331. elseif prefix == "+CFUN" then
  332. if success then publish("FLYMODE", flyMode) end
  333. end
  334. end
  335. --- 实时读取“当前和临近小区信息”
  336. -- @function cbFnc,回调函数,当读取到小区信息后,会调用此回调函数,回调函数的调用形式为:
  337. -- cbFnc(cells),其中cells为string类型,格式为:当前和临近位置区、小区、mcc、mnc、以及信号强度的拼接字符串,例如:"460.01.6311.49234.30;460.01.6311.49233.23;460.02.6322.49232.18;"
  338. -- @return nil
  339. function getMultiCell(cbFnc)
  340. multicellcb = cbFnc
  341. --发送AT+CENG?查询
  342. ril.request("AT+CENG?")
  343. end
  344. --- 发起查询基站信息(当前和临近小区信息)的请求
  345. -- @number period 查询间隔,单位毫秒
  346. -- @return bool result, true:查询成功,false:查询失败
  347. -- @usage net.cengQueryPoll() --查询1次
  348. -- @usage net.cengQueryPoll(60000) --每分钟查询1次
  349. function cengQueryPoll(period)
  350. -- 不是飞行模式 并且 工作模式为完整模式
  351. if not flyMode then
  352. --发送AT+CENG?查询
  353. ril.request("AT+CENG?")
  354. else
  355. log.warn("net.cengQueryPoll", "flymode:", flyMode)
  356. end
  357. if nil ~= period then
  358. --启动定时器
  359. sys.timerStopAll(cengQueryPoll)
  360. sys.timerStart(cengQueryPoll, period, period)
  361. end
  362. return not flyMode
  363. end
  364. --- 发起查询信号强度的请求
  365. -- @number period 查询间隔,单位毫秒
  366. -- @return bool , true:查询成功,false:查询停止
  367. -- @usage net.csqQueryPoll() --查询1次
  368. -- @usage net.csqQueryPoll(60000) --每分钟查询1次
  369. function csqQueryPoll(period)
  370. --不是飞行模式 并且 工作模式为完整模式
  371. if not flyMode then
  372. --发送AT+CSQ查询
  373. ril.request("AT+CSQ")
  374. else
  375. log.warn("net.csqQueryPoll", "flymode:", flyMode)
  376. end
  377. if nil ~= period then
  378. --启动定时器
  379. sys.timerStopAll(csqQueryPoll)
  380. sys.timerStart(csqQueryPoll, period, period)
  381. end
  382. return not flyMode
  383. end
  384. --- 设置查询信号强度和基站信息的间隔
  385. -- @number ... 查询周期,参数可变,参数为nil只查询1次,参数1是信号强度查询周期,参数2是基站查询周期
  386. -- @return bool ,true:设置成功,false:设置失败
  387. -- @usage net.startQueryAll()
  388. -- @usage net.startQueryAll(60000) -- 1分钟查询1次信号强度,只立即查询1次基站信息
  389. -- @usage net.startQueryAll(60000,600000) -- 1分钟查询1次信号强度,10分钟查询1次基站信息
  390. function startQueryAll(...)
  391. csqQueryPoll(arg[1])
  392. cengQueryPoll(arg[2])
  393. if flyMode then
  394. log.info("sim.startQuerAll", "flyMode:", flyMode)
  395. end
  396. return true
  397. end
  398. --- 停止查询信号强度和基站信息
  399. -- @return 无
  400. -- @usage net.stopQueryAll()
  401. function stopQueryAll()
  402. sys.timerStopAll(csqQueryPoll)
  403. sys.timerStopAll(cengQueryPoll)
  404. end
  405. -- 处理SIM卡状态消息,SIM卡工作不正常时更新网络状态为未注册
  406. sys.subscribe("SIM_IND", function(para)
  407. log.info("SIM.subscribe", simerrsta, para)
  408. if simerrsta ~= (para ~= "RDY") then
  409. simerrsta = (para ~= "RDY")
  410. end
  411. --sim卡工作不正常
  412. if para ~= "RDY" then
  413. --更新GSM网络状态
  414. state = "UNREGISTER"
  415. --产生内部消息NET_STATE_CHANGED,表示网络状态发生变化
  416. publish("NET_STATE_UNREGISTER")
  417. else
  418. state = "INIT"
  419. end
  420. end)
  421. --注册+CREG和+CENG通知的处理函数
  422. ril.regUrc("+CREG", neturc)
  423. ril.regUrc("+CENG", neturc)
  424. --ril.regUrc("+CRSM", neturc)
  425. --注册AT+CCSQ和AT+CENG?命令的应答处理函数
  426. ril.regRsp("+CSQ", rsp)
  427. ril.regRsp("+CENG", rsp)
  428. ril.regRsp("+CFUN", rsp)-- 飞行模式
  429. --发送AT命令
  430. ril.request("AT+CREG=2")
  431. ril.request("AT+CREG?")
  432. ril.request("AT+CENG=1,1")
  433. --重置当前小区和临近小区信息表
  434. resetCellInfo()