link.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. --- 模块功能:数据链路激活(创建、连接、状态维护)
  2. -- @module link
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.9.20
  7. -- 4G网络下不手动激活pdp,注册上网后发cgdcont?等默认承载激活后上报IP_READY_IND,
  8. -- 2G网络下,先cgact?查询有任一一路pdp激活,则直接上报IP_READY_IND,否则cgact激活cid_manual
  9. require "net"
  10. module(..., package.seeall)
  11. local publish = sys.publish
  12. local request = ril.request
  13. local ipAddr = ""
  14. local gprsAttached
  15. local cid_manual = 5
  16. local readyTable = {false, false, false}
  17. -- 链路层网络类型:
  18. -- 蜂窝模组数据网络
  19. CELLULAR = 1
  20. -- ch395以太网
  21. CH395 = 2
  22. -- w5500以太网
  23. W5500 = 3
  24. local network = CELLULAR
  25. function setReady(mode, state)
  26. readyTable[mode] = state
  27. end
  28. function getIp()
  29. return ipAddr
  30. end
  31. function isReady()
  32. return readyTable[network]
  33. end
  34. -- apn,用户名,密码
  35. local apnname, username, password
  36. local dnsIP
  37. local authProt, authApn, authUser, authPassword
  38. function setAPN(apn, user, pwd)
  39. apnname, username, password = apn, user, pwd
  40. end
  41. function setDnsIP(ip1, ip2)
  42. dnsIP = "\"" .. (ip1 or "") .. "\",\"" .. (ip2 or "") .. "\""
  43. end
  44. local function setCgdf()
  45. request("AT+AUTOAPN=0")
  46. request('AT*CGDFLT=1,"IP","' .. authApn .. '",,,,,,,,,,,,,,,,,,1')
  47. request('AT*CGDFAUTH=1,' .. authProt .. ',"' .. authUser .. '","' .. authPassword .. '"', nil, function(cmd, result)
  48. if result then
  49. sys.restart("CGDFAUTH")
  50. else
  51. sys.timerStart(setCgdf, 5000)
  52. end
  53. end)
  54. end
  55. --- 设置专网卡APN(注意:在main.lua中,尽可能靠前的位置调用此接口)
  56. -- 第一次设置成功之后,软件会自动重启,因为重启后才能生效
  57. -- @number[opt=0] prot,加密方式, 0:不加密 1:PAP 2:CHAP
  58. -- @string[opt=""] apn,apn名称
  59. -- @string[opt=""] user,apn用户名
  60. -- @string[opt=""] pwd,apn密码
  61. -- @return nil
  62. -- @usage
  63. -- c = link.setAuthApn(2,"MYAPN","MYNAME","MYPASSWORD")
  64. function setAuthApn(prot, apn, user, pwd)
  65. --[[
  66. local coreVer = rtos.get_version()
  67. local verNo = coreVer:match("Luat_V(%d+)_ASR1802_")
  68. if verNo and tonumber(verNo)>=27 then
  69. request("AT+AUTOAPN=0")]]
  70. -- 0:保存并重启生效
  71. -- 1:不保存立即生效
  72. -- 2:保存并立即生效
  73. -- 3:删除保存的文件
  74. request('AT+CPNETAPN=2,"' .. apn .. '","' .. user .. '","' .. pwd .. '",' .. prot)
  75. --[[else
  76. authProt,authApn,authUser,authPassword = prot or 0,apn or "",user or "",pwd or ""
  77. request("AT*CGDFLT?")
  78. ril.regUrc("*CGDFLT", function(data)
  79. local dftApn = data:match("CGDFLT:%s*\"%w*\",\"(.-)\"")
  80. if dftApn~=authApn then
  81. setCgdf()
  82. end
  83. end)
  84. end]]
  85. end
  86. local function Pdp_Act()
  87. log.info("link.Pdp_Act", readyTable[CELLULAR], net.getNetMode(), gprsAttached)
  88. if readyTable[CELLULAR] then
  89. request("AT+CGDCONT?", nil, cgdcontRsp)
  90. return
  91. end
  92. if net.getNetMode() == net.NetMode_LTE then
  93. if not gprsAttached then
  94. gprsAttached = true
  95. sys.publish("GPRS_ATTACH", true)
  96. end
  97. if not apnname then
  98. sys.timerStart(pdpCmdCnf, 1000, "SET_PDP_4G_WAITAPN", true)
  99. else
  100. request("AT+CGDCONT?", nil, cgdcontRsp)
  101. -- request(string.format('AT*CGDFLT=0,"IP","%s"', apnname), nil, pdpCmdCnf)
  102. end
  103. else
  104. request('AT+CGATT?')
  105. end
  106. end
  107. local function procshut(curCmd, result, respdata, interdata)
  108. if network~=CELLULAR then
  109. return
  110. end
  111. if IsCidActived(cid_manual, interdata) then
  112. ril.request(string.format('AT+CGACT=0,%d', cid_manual), nil, function(cmd, result)
  113. if result then
  114. readyTable[CELLULAR] = false
  115. sys.publish('IP_ERROR_IND')
  116. if net.getState() ~= 'REGISTERED' then
  117. return
  118. end
  119. sys.timerStart(Pdp_Act, 2000)
  120. end
  121. end)
  122. else
  123. readyTable[CELLULAR] = false
  124. sys.publish('IP_ERROR_IND')
  125. if net.getState() ~= 'REGISTERED' then
  126. return
  127. end
  128. sys.timerStart(Pdp_Act, 2000)
  129. end
  130. end
  131. --[[
  132. 如果是默认承载,是去激活不了的,
  133. 如果是手动激活的pdp,去激活cid_manual后也还是有默认承载存在,
  134. 所以如果上层在去激活后要发起socket是能连上的,所以这里直接上报IP_ERROR_IND,由上层自己管理shut之后的逻辑
  135. ]]
  136. function shut()
  137. if network~=CELLULAR then
  138. return
  139. end
  140. -- ril.request("AT+CGACT?",nil,procshut)
  141. readyTable[CELLULAR] = false
  142. sys.publish('IP_ERROR_IND')
  143. if net.getState() ~= 'REGISTERED' then
  144. return
  145. end
  146. sys.timerStart(Pdp_Act, 2000)
  147. end
  148. function analysis_cgdcont(data)
  149. local tmp, loc, result
  150. while data do
  151. _, loc = string.find(data, "\r\n")
  152. if loc then
  153. tmp = string.sub(data, 1, loc)
  154. data = string.sub(data, loc + 1, -1)
  155. log.info("analysis_cgdcont ", tmp, loc, data)
  156. else
  157. tmp = data
  158. data = nil
  159. log.info("analysis_cgdcont end", tmp, loc, data)
  160. end
  161. if tmp then
  162. local cid, pdptyp, apn, addr = string.match(tmp, "(%d+),(.+),(.+),[\"\'](.+)[\"\']")
  163. if not cid or not pdptyp or not apn or not addr then
  164. log.info("analysis_cgdcont CGDCONT is empty")
  165. ipAddr = ""
  166. result = false
  167. else
  168. log.info("analysis_cgdcont ", cid, pdptyp, apn, addr)
  169. if addr:match("%d+%.%d+%.%d+%.%d") then
  170. ipAddr = addr
  171. return true
  172. else
  173. log.info("analysis_cgdcont CGDCONT is empty1")
  174. ipAddr = ""
  175. return false
  176. end
  177. end
  178. else
  179. ipAddr = ""
  180. log.info("analysis_cgdcont tmp is empty")
  181. end
  182. end
  183. return result
  184. end
  185. function IsCidActived(cid, data)
  186. if not data then
  187. return
  188. end
  189. for k, v in string.gfind(data, "(%d+),%s*(%d)") do
  190. log.info("iscidactived ", k, v)
  191. if cid == tonumber(k) and v == '1' then
  192. return true
  193. end
  194. end
  195. return
  196. end
  197. function IsExistActivedCid(data)
  198. if not data then
  199. return
  200. end
  201. for k, v in string.gfind(data, "(%d+),%s*(%d)") do
  202. if v == '1' then
  203. log.info("ExistActivedCid ", k, v)
  204. return true
  205. end
  206. end
  207. return
  208. end
  209. local cgdcontResult
  210. function cgdcontRsp()
  211. if cgdcontResult then
  212. pdpCmdCnf("CONNECT_DELAY", true)
  213. end
  214. end
  215. function pdpCmdCnf(curCmd, result, respdata, interdata)
  216. log.info("link.pdpCmdCnf", curCmd, result, respdata, interdata)
  217. if string.find(curCmd, "CGDCONT%?") then
  218. if result and interdata then
  219. result = analysis_cgdcont(interdata)
  220. else
  221. result = false
  222. end
  223. end
  224. if result then
  225. cgdcontResult = false
  226. if string.find(curCmd, "CGDCONT=") then
  227. request(string.format('AT+CGACT=1,%d', cid_manual), nil, pdpCmdCnf)
  228. elseif string.find(curCmd, "CGDCONT%?") then
  229. -- sys.timerStart(pdpCmdCnf, 100, "CONNECT_DELAY",true)
  230. cgdcontResult = true
  231. elseif string.find(curCmd, "CONNECT_DELAY") and network == CELLULAR then
  232. log.info("publish IP_READY_IND")
  233. readyTable[CELLULAR] = true
  234. publish("IP_READY_IND")
  235. elseif string.find(curCmd, "CGACT=") then
  236. request("AT+CGDCONT?", nil, cgdcontRsp)
  237. elseif string.find(curCmd, "CGACT%?") then
  238. if IsExistActivedCid(interdata) then
  239. sys.timerStart(pdpCmdCnf, 100, "CONNECT_DELAY", true)
  240. else
  241. request(string.format('AT+CGDCONT=%d,"IP","%s"', cid_manual, authApn or apnname), nil, pdpCmdCnf)
  242. end
  243. elseif string.find(curCmd, "CGDFLT") then
  244. request("AT+CGDCONT?", nil, cgdcontRsp)
  245. elseif string.find(curCmd, "SET_PDP_4G_WAITAPN") then
  246. if not apnname then
  247. sys.timerStart(pdpCmdCnf, 100, "SET_PDP_4G_WAITAPN", true)
  248. else
  249. request("AT+CGDCONT?", nil, cgdcontRsp, 1000)
  250. -- request(string.format('AT*CGDFLT=0,"IP","%s"', apnname), nil, pdpCmdCnf)
  251. end
  252. end
  253. else
  254. if net.getState() ~= 'REGISTERED' then
  255. return
  256. end
  257. if net.getNetMode() == net.NetMode_LTE then
  258. request("AT+CGDCONT?", nil, cgdcontRsp, 1000)
  259. else
  260. request("AT+CGATT?", nil, nil, 1000)
  261. end
  262. end
  263. end
  264. -- SIM卡 IMSI READY以后自动设置APN
  265. sys.subscribe("IMSI_READY", function()
  266. if not apnname then -- 如果未设置APN设置默认APN
  267. local mcc, mnc = tonumber(sim.getMcc(), 16), tonumber(sim.getMnc(), 16)
  268. apnname, username, password = apn and apn.get_default_apn(mcc, mnc) -- 如果存在APN库自动获取运营商的APN
  269. if not apnname or apnname == '' or apnname == "CMNET" then -- 默认情况,如果联通卡设置为联通APN 其他都默认为CMIOT
  270. apnname = (mcc == 0x460 and (mnc == 0x01 or mnc == 0x06)) and 'UNINET' or 'CMIOT'
  271. end
  272. end
  273. username = username or ''
  274. password = password or ''
  275. end)
  276. ril.regRsp('+CGATT', function(a, b, c, intermediate)
  277. local attached = (intermediate == "+CGATT: 1")
  278. if gprsAttached ~= attached then
  279. gprsAttached = attached
  280. sys.publish("GPRS_ATTACH", attached)
  281. end
  282. if readyTable[CELLULAR] then
  283. return
  284. end
  285. if attached then
  286. log.info("pdp active", apnname, username, password)
  287. request("AT+CGACT?", nil, pdpCmdCnf, 1000)
  288. elseif net.getState() == 'REGISTERED' then
  289. sys.timerStart(request, 2000, "AT+CGATT=1")
  290. sys.timerStart(request, 2000, "AT+CGATT?")
  291. end
  292. end)
  293. rtos.on(rtos.MSG_PDP_DEACT_IND, function()
  294. if network~=CELLULAR then
  295. return
  296. end
  297. readyTable[CELLULAR] = false
  298. sys.publish('IP_ERROR_IND')
  299. if net.getState() ~= 'REGISTERED' then
  300. return
  301. end
  302. sys.timerStart(Pdp_Act, 2000)
  303. end)
  304. -- 网络注册成功 :AT+CGDCONT?查询默认承载是否激活
  305. -- 2/3G发起GPRS附着状态查询
  306. sys.subscribe("NET_STATE_REGISTERED", Pdp_Act)
  307. local function cindCnf(cmd, result)
  308. if not result then
  309. request("AT+CIND=1", nil, cindCnf, 1000)
  310. end
  311. end
  312. local function cgevurc(data)
  313. if network~=CELLULAR then
  314. return
  315. end
  316. local cid = 0
  317. log.info("link.cgevurc", data)
  318. if string.match(data, "DEACT") then
  319. cid = string.match(data, "DEACT,(%d)")
  320. cid = tonumber(cid)
  321. if cid == cid_manual then
  322. request("AT+CFUN?")
  323. readyTable[CELLULAR] = false
  324. sys.publish('IP_ERROR_IND')
  325. sys.publish('PDP_DEACT_IND')
  326. if net.getState() ~= 'REGISTERED' then
  327. return
  328. end
  329. sys.timerStart(Pdp_Act, 2000)
  330. end
  331. end
  332. end
  333. request("AT+CIND=1", nil, cindCnf)
  334. ril.regUrc("*CGEV", cgevurc)
  335. ril.regUrc("+CGDCONT", function(data)
  336. pdpCmdCnf("AT+CGDCONT?", true, "OK", data)
  337. end)
  338. --- 打开链路层网络类型
  339. -- 注意:设置网络类型后,并不会关机保存,下次开机会自动恢复为默认的link.CELLULAR类型
  340. -- @number[opt=link.CELLULAR] mode,取值如下:
  341. -- link.CELLULAR:蜂窝模组数据网络
  342. -- link.CH395:CH395以太网络
  343. -- link.W5500:W5500以太网络
  344. -- @table[opt=nil] para,取值如下:
  345. -- 当mode为link.CELLULAR时,参数para无意义,可以直接传入nil
  346. -- 当mode为link.CH395,para为table类型,表示以太网的配置参数,参数结构如下:
  347. -- para= {
  348. -- mode = 1, --1表示客户端;2表示服务器;默认为1
  349. -- intPin = pio.P0_22, --以太网芯片中断通知引脚
  350. -- rstPin = pio.P0_23, --复位以太网芯片引脚
  351. -- spiCs = pio.P0_23, --spi片选
  352. -- serverAddr = "192.168.1.112", --做服务器应用时,本机的地址
  353. -- serverPort = 1888, --做服务器应用时,本机的端口
  354. -- serverGateway = "192.168.1.1", --做服务器应用时,本机的网关地址
  355. -- powerFunc=function(state) end --控制以太网模块的供电开关函数,ret为true开启供电,false关闭供电
  356. -- spi = {spi.SPI_1,0,0,8,800000}, --SPI通道参数,id,cpha,cpol,dataBits,clock,默认spi.SPI_1,0,0,8,800000
  357. -- }
  358. -- @return true/false,执行成功返回true,失败返回false。
  359. -- @usage
  360. -- 设置为蜂窝数据网络:
  361. -- c = link.setNetwork(link.CELLULAR, para)
  362. -- 设置为CH395以太网络:
  363. -- link.setNetwork(link.CH395, para)
  364. -- 设置为w5500以太网络:
  365. -- link.setNetwork(link.W5500, para)
  366. function openNetwork(mode, para)
  367. local tSocketModule = {
  368. [CH395] = socketCh395,
  369. [W5500] = socketW5500
  370. }
  371. local md = mode or CELLULAR
  372. closeNetWork()
  373. network = md
  374. if network == CELLULAR then
  375. net.switchFly(false)
  376. return true
  377. else
  378. ipAddr=tSocketModule[network].open(para)
  379. if ipAddr~="" then
  380. return true
  381. else
  382. log.info('link','open CH395 err')
  383. return false
  384. end
  385. end
  386. return false
  387. end
  388. --- 关闭链路层网络类型
  389. -- 注意:关闭链路层网络类型,不会改变链路层网络类型,需要打开链路层网络类型配置才能切换。
  390. -- @return true/false,执行成功返回true,失败返回false。
  391. -- @usage
  392. -- 关闭链路层网络类型:
  393. -- link.closeNetWork()
  394. function closeNetWork()
  395. local tSocketModule = {
  396. [CH395] = socketCh395,
  397. [W5500] = socketW5500
  398. }
  399. if network == CELLULAR then
  400. -- 飞行模式
  401. net.switchFly(true)
  402. return true
  403. else
  404. return tSocketModule[network].close()
  405. end
  406. return false
  407. end
  408. --- 获取链路层网络类型
  409. -- @return network,number类型,取值如下:
  410. -- link.CELLULAR:蜂窝模组数据网络
  411. -- link.CH395:CH395以太网络
  412. -- link.W5500:W5500以太网络
  413. -- @usage
  414. -- 获取数据网络类型:
  415. -- mode = link.getNetwork()
  416. function getNetwork()
  417. return network
  418. end