link_wifi.lua 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. local link = {}
  2. local connected, connecting, stopConfig, recvId
  3. function link.isReady()
  4. return connected
  5. end
  6. function link.shut()
  7. log.info("link_wifi", "shut")
  8. end
  9. link.getRecvId = function() return recvId end
  10. local errorNum = 0
  11. local function wifiUrc(data, prefix)
  12. log.info("urc上报", data, prefix)
  13. if prefix == "STATUS" then
  14. if not connecting or not connected then
  15. local state = data:sub(8, -1)
  16. if state == "0" or state == "1" or state == "5" then
  17. errorNum = errorNum + 1
  18. if errorNum > 5 then
  19. ril_wifi.request("AT+CWMODE=1")
  20. ril_wifi.request("AT+CWSTARTSMART=3")
  21. else
  22. point("later")
  23. sys.timerStart(ril_wifi.request, 5000, "AT+CIPSTATUS")
  24. end
  25. elseif state == "2" then
  26. errorNum = 0
  27. connected = true
  28. sys.publish("IP_READY_IND")
  29. return
  30. end
  31. end
  32. elseif prefix == "WIFI GOT IP" then
  33. connecting = false
  34. connected = true
  35. sys.publish("IP_READY_IND")
  36. return
  37. elseif prefix == "WIFI CONNECTED" then
  38. connecting = true
  39. return
  40. elseif prefix == "Smart get wifi info" then
  41. stopConfig = sys.timerStart(ril_wifi.request, 20000, "AT+CWSTOPSMART")
  42. return
  43. elseif prefix == "smartconfig connected wifi" then
  44. connecting = true
  45. if sys.timerIsActive(stopConfig) then sys.timerStop(stopConfig) end
  46. ril_wifi.request("AT+CWSTOPSMART", nil, nil, 6000)
  47. elseif prefix == "+IPD" then
  48. log.info("rcv data", data)
  49. local lid, dataLen = string.match(data, "%+IPD,(%d),(%d+)")
  50. recvId = lid
  51. ril_wifi.request(string.format("AT+CIPRECVDATA=%d,%d", lid, dataLen))
  52. elseif prefix == "+CIPRECVLEN" then
  53. log.info("rcv test", prefix, data)
  54. local lid = {string.match(data, "%+CIPRECVLEN:(.+),(.+),(.+),(.+),(.+)")}
  55. for k, v in pairs(lid) do
  56. if v ~= "-1" and v ~= "0" then
  57. ril_wifi.request(string.format("AT+CIPRECVDATA=%d,%d", k - 1, 2147483647))
  58. end
  59. end
  60. end
  61. end
  62. local function wifiRsp(cmd, success, response, intermediate)
  63. log.info("wifi", cmd, success, response, intermediate)
  64. if cmd == "AT+CWSTARTSMART=3" then
  65. if success then smartConfig = true end
  66. elseif cmd == "AT+CWSTOPSMART" then
  67. if connecting then
  68. connecting = false
  69. connected = true
  70. sys.publish("IP_READY_IND")
  71. else
  72. sys.timerStart(ril_wifi.request, 2000, "AT+CWSTARTSMART=3")
  73. end
  74. end
  75. end
  76. ril_wifi.regUrc("+IPD", wifiUrc)
  77. ril_wifi.regUrc("STATUS", wifiUrc)
  78. ril_wifi.regUrc("WIFI GOT IP", wifiUrc)
  79. ril_wifi.regUrc("WIFI CONNECTED", wifiUrc)
  80. ril_wifi.regUrc("smartconfig connected wifi", wifiUrc)
  81. ril_wifi.regUrc("Smart get wifi info", wifiUrc)
  82. ril_wifi.regRsp("+CWSTARTSMART", wifiRsp)
  83. ril_wifi.regRsp("+CWSTOPSMART", wifiRsp)
  84. ril_wifi.regUrc("+CIPRECVLEN", wifiUrc)
  85. ril_wifi.request("AT+CIPRECVMODE=1")
  86. ril_wifi.request("AT+CIPMODE=0")
  87. ril_wifi.request("AT+CIPMUX=1")
  88. ril_wifi.request("AT+CIPSTATUS")
  89. return link