最终提交
This commit is contained in:
@@ -1,38 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local uartReceiveCallbacks = {}
|
|
||||||
local uartSentCallbacks = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
uart.on = function(id, event, callback)
|
|
||||||
if event == "receive" then
|
|
||||||
uartReceiveCallbacks[id] = callback
|
|
||||||
elseif event == "sent" then
|
|
||||||
uartSentCallbacks[id] = callback
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
rtos.on(rtos.MSG_UART_RXDATA, function(id, length)
|
|
||||||
if uartReceiveCallbacks[id] then
|
|
||||||
uartReceiveCallbacks[id](id, length)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
rtos.on(rtos.MSG_UART_TX_DONE, function(id)
|
|
||||||
if uartSentCallbacks[id] then
|
|
||||||
uartSentCallbacks[id](id)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
|
|
||||||
require "linksocket"
|
|
||||||
require "socket"
|
|
||||||
require "sys"
|
|
||||||
|
|
||||||
function handleCmd01(subCmd, payload)
|
|
||||||
|
|
||||||
sendToSocket(subCmd, payload)
|
|
||||||
log.info("CMD 0x01 received, sub:", subCmd, "data len:", #payload)
|
|
||||||
end
|
|
||||||
|
|
||||||
function handleCmd02(subCmd, payload)
|
|
||||||
sys.restart()
|
|
||||||
end
|
|
||||||
@@ -1,310 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require"socket"
|
|
||||||
require"utils"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
local function response(client,cbFnc,result,prompt,head,body)
|
|
||||||
if not result then log.error("http.response",result,prompt) end
|
|
||||||
if cbFnc then cbFnc(result,prompt,head,body) end
|
|
||||||
if client then client:close() end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function receive(client,timeout,cbFnc,result,prompt,head,body)
|
|
||||||
local res,data = client:recv(timeout)
|
|
||||||
if not res then
|
|
||||||
response(client,cbFnc,result,prompt or "receive timeout",head,body)
|
|
||||||
end
|
|
||||||
return res,data
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getFileBase64Len(s)
|
|
||||||
if s then return (io.fileSize(s)+2)/3*4 end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function taskClient(method,protocal,auth,host,port,path,cert,head,body,timeout,cbFnc,rcvFilePath,tCoreExtPara)
|
|
||||||
log.info("http path",path)
|
|
||||||
while not socket.isReady() do
|
|
||||||
if not sys.waitUntil("IP_READY_IND",timeout) then return response(nil,cbFnc,false,"network not ready") end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local bodyLen = 0
|
|
||||||
if body then
|
|
||||||
if type(body)=="string" then
|
|
||||||
bodyLen = body:len()
|
|
||||||
elseif type(body)=="table" then
|
|
||||||
for i=1,#body do
|
|
||||||
bodyLen = bodyLen + (type(body[i])=="string" and string.len(body[i]) or getFileBase64Len(body[i].file_base64) or io.fileSize(body[i].file))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local heads = head or {}
|
|
||||||
if not heads.Host then heads["Host"] = (port ~= 80 and port ~= 443) and (host..":"..port) or host end
|
|
||||||
if not heads.Connection then heads["Connection"] = "short" end
|
|
||||||
if bodyLen>0 and bodyLen~=tonumber(heads["Content-Length"] or "0") then heads["Content-Length"] = bodyLen end
|
|
||||||
if auth~="" and not heads.Authorization then heads["Authorization"] = ("Basic "..crypto.base64_encode(auth,#auth)) end
|
|
||||||
local headStr = ""
|
|
||||||
for k,v in pairs(heads) do
|
|
||||||
headStr = headStr..k..": "..v.."\r\n"
|
|
||||||
end
|
|
||||||
headStr = headStr.."\r\n"
|
|
||||||
|
|
||||||
local client = socket.tcp(protocal=="https",cert,tCoreExtPara)
|
|
||||||
if not client then return response(nil,cbFnc,false,"create socket error") end
|
|
||||||
if not client:connect(host,port,timeout/1000) then
|
|
||||||
return response(client,cbFnc,false,"connect fail")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if not client:send(method.." "..path.." HTTP/1.1".."\r\n"..headStr..(type(body)=="string" and body or "")) then
|
|
||||||
return response(client,cbFnc,false,"send head fail")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if type(body)=="table" then
|
|
||||||
for i=1,#body do
|
|
||||||
if type(body[i])=="string" then
|
|
||||||
if not client:send(body[i]) then
|
|
||||||
return response(client,cbFnc,false,"send body fail")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local file = io.open(body[i].file or body[i].file_base64,"rb")
|
|
||||||
if file then
|
|
||||||
while true do
|
|
||||||
local dat = file:read(body[i].file and 11200 or 8400)
|
|
||||||
if not dat then
|
|
||||||
io.close(file)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
if body[i].file_base64 then dat=crypto.base64_encode(dat,#dat) end
|
|
||||||
if not client:send(dat) then
|
|
||||||
io.close(file)
|
|
||||||
return response(client,cbFnc,false,"send file fail")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
return response(client,cbFnc,false,"send file open fail")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local rcvCache,rspHead,rspBody,d1,d2,result,data,statusCode,rcvChunked,contentLen = "",{},{}
|
|
||||||
|
|
||||||
while true do
|
|
||||||
result,data = receive(client,timeout,cbFnc,false,nil,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
if not result then return end
|
|
||||||
rcvCache = rcvCache..data
|
|
||||||
d1,d2 = rcvCache:find("\r\n\r\n")
|
|
||||||
if d2 then
|
|
||||||
|
|
||||||
_,d1,statusCode = rcvCache:find("%s(%d+)%s.-\r\n")
|
|
||||||
if not statusCode then
|
|
||||||
return response(client,cbFnc,false,"parse received status error",rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
end
|
|
||||||
|
|
||||||
for k,v in string.gmatch(rcvCache:sub(d1+1,d2-2),"(.-):%s*(.-)\r\n") do
|
|
||||||
rspHead[k] = v
|
|
||||||
if (string.upper(k)==string.upper("Transfer-Encoding")) and (string.upper(v)==string.upper("chunked")) then rcvChunked = true end
|
|
||||||
end
|
|
||||||
if not rcvChunked then
|
|
||||||
contentLen = tonumber(rspHead["Content-Length"] or "2147483647")
|
|
||||||
end
|
|
||||||
if method == "HEAD" then
|
|
||||||
contentLen = 0
|
|
||||||
end
|
|
||||||
|
|
||||||
rcvCache = rcvCache:sub(d2+1,-1)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if rcvChunked then
|
|
||||||
local chunkSize
|
|
||||||
|
|
||||||
while true do
|
|
||||||
|
|
||||||
if not chunkSize then
|
|
||||||
d1,d2,chunkSize = rcvCache:find("(%x+)\r\n")
|
|
||||||
if chunkSize then
|
|
||||||
chunkSize = tonumber(chunkSize,16)
|
|
||||||
rcvCache = rcvCache:sub(d2+1,-1)
|
|
||||||
else
|
|
||||||
result,data = receive(client,timeout,cbFnc,false,nil,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
if not result then return end
|
|
||||||
rcvCache = rcvCache..data
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if chunkSize then
|
|
||||||
if rcvCache:len()<chunkSize+2 then
|
|
||||||
result,data = receive(client,timeout,cbFnc,false,nil,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
if not result then return end
|
|
||||||
rcvCache = rcvCache..data
|
|
||||||
else
|
|
||||||
if chunkSize>0 then
|
|
||||||
local chunkData = rcvCache:sub(1,chunkSize)
|
|
||||||
|
|
||||||
if type(rcvFilePath)=="string" then
|
|
||||||
local file = io.open(rcvFilePath,"a+")
|
|
||||||
if not file then return response(client,cbFnc,false,"receive: open file error",rspHead,rcvFilePath or table.concat(rspBody)) end
|
|
||||||
if not file:write(chunkData) then response(client,cbFnc,false,"receive: write file error",rspHead,rcvFilePath or table.concat(rspBody)) end
|
|
||||||
file:close()
|
|
||||||
elseif type(rcvFilePath)=="function" then
|
|
||||||
local userResult = rcvFilePath(data,rspHead["Content-Range"] and tonumber((rspHead["Content-Range"]):match("/(%d+)")) or contentLen,statusCode)
|
|
||||||
if userResult~=nil then
|
|
||||||
return response(client,cbFnc,userResult,userResult and statusCode or "receive: user process error",rspHead)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
table.insert(rspBody,chunkData)
|
|
||||||
end
|
|
||||||
rcvCache = rcvCache:sub(chunkSize+3,-1)
|
|
||||||
chunkSize = nil
|
|
||||||
elseif chunkSize==0 then
|
|
||||||
return response(client,cbFnc,true,statusCode,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local rmnLen = contentLen
|
|
||||||
while true do
|
|
||||||
data = rcvCache:len()<=rmnLen and rcvCache or rcvCache:sub(1,rmnLen)
|
|
||||||
if type(rcvFilePath)=="string" then
|
|
||||||
if data:len()>0 then
|
|
||||||
local file = io.open(rcvFilePath,"a+")
|
|
||||||
if not file then return response(client,cbFnc,false,"receive: open file error",rspHead,rcvFilePath or table.concat(rspBody)) end
|
|
||||||
if not file:write(data) then response(client,cbFnc,false,"receive: write file error",rspHead,rcvFilePath or table.concat(rspBody)) end
|
|
||||||
file:close()
|
|
||||||
end
|
|
||||||
elseif type(rcvFilePath)=="function" then
|
|
||||||
local userResult = rcvFilePath(data,rspHead["Content-Range"] and tonumber((rspHead["Content-Range"]):match("/(%d+)")) or contentLen,statusCode)
|
|
||||||
if userResult~=nil then
|
|
||||||
return response(client,cbFnc,userResult,userResult and statusCode or "receive: user process error",rspHead)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
table.insert(rspBody,data)
|
|
||||||
end
|
|
||||||
rmnLen = rmnLen-data:len()
|
|
||||||
if rmnLen==0 then break end
|
|
||||||
result,rcvCache = receive(client,timeout,cbFnc,contentLen==0x7FFFFFFF,contentLen==0x7FFFFFFF and statusCode or nil,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
if not result then return end
|
|
||||||
end
|
|
||||||
return response(client,cbFnc,true,statusCode,rspHead,rcvFilePath or table.concat(rspBody))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function request(method,url,cert,head,body,timeout,cbFnc,rcvFileName,tCoreExtPara)
|
|
||||||
local protocal,auth,hostName,port,path,d1,d2,offset,rcvFilePath
|
|
||||||
d1,d2,protocal = url:find("^(%a+)://")
|
|
||||||
if not protocal then protocal = "http" end
|
|
||||||
offset = d2 or 0
|
|
||||||
|
|
||||||
d1,d2,auth = url:find("(.-:.-)@",offset+1)
|
|
||||||
offset = d2 or offset
|
|
||||||
|
|
||||||
if url:match("^[^/]+:(%d+)",offset+1) then
|
|
||||||
d1,d2,hostName,port = url:find("^([^/]+):(%d+)",offset+1)
|
|
||||||
else
|
|
||||||
d1,d2,hostName = url:find("(.-)/",offset+1)
|
|
||||||
if hostName then
|
|
||||||
d2 = d2-1
|
|
||||||
else
|
|
||||||
hostName = url:sub(offset+1,-1)
|
|
||||||
offset = url:len()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not hostName then return response(nil,cbFnc,false,"Invalid url, can't get host") end
|
|
||||||
if port=="" or not port then port = (protocal=="https" and 443 or 80) end
|
|
||||||
offset = d2 or offset
|
|
||||||
|
|
||||||
path = url:sub(offset+1,-1)
|
|
||||||
|
|
||||||
sys.taskInit(taskClient,method,protocal,auth or "",hostName,port,path=="" and "/" or path,cert,head,body or "",timeout or 30000,cbFnc,rcvFileName,tCoreExtPara)
|
|
||||||
if type(rcvFileName) == "string" then
|
|
||||||
return rcvFileName
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,461 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "net"
|
|
||||||
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
local publish = sys.publish
|
|
||||||
local request = ril.request
|
|
||||||
local ipAddr = ""
|
|
||||||
local gprsAttached
|
|
||||||
local cid_manual = 5
|
|
||||||
local readyTable = {false, false, false}
|
|
||||||
|
|
||||||
|
|
||||||
CELLULAR = 1
|
|
||||||
|
|
||||||
CH395 = 2
|
|
||||||
|
|
||||||
W5500 = 3
|
|
||||||
|
|
||||||
ESP8266 = 4
|
|
||||||
local network = CELLULAR
|
|
||||||
|
|
||||||
function setReady(mode, state)
|
|
||||||
readyTable[mode] = state
|
|
||||||
end
|
|
||||||
function getIp()
|
|
||||||
return ipAddr
|
|
||||||
end
|
|
||||||
function isReady()
|
|
||||||
return readyTable[network]
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local apnname, username, password
|
|
||||||
local dnsIP
|
|
||||||
local authProt, authApn, authUser, authPassword
|
|
||||||
|
|
||||||
function setAPN(apn, user, pwd)
|
|
||||||
apnname, username, password = apn, user, pwd
|
|
||||||
end
|
|
||||||
|
|
||||||
function setDnsIP(ip1, ip2)
|
|
||||||
dnsIP = "\"" .. (ip1 or "") .. "\",\"" .. (ip2 or "") .. "\""
|
|
||||||
end
|
|
||||||
|
|
||||||
local function setCgdf()
|
|
||||||
request("AT+AUTOAPN=0")
|
|
||||||
request('AT*CGDFLT=1,"IP","' .. authApn .. '",,,,,,,,,,,,,,,,,,1')
|
|
||||||
request('AT*CGDFAUTH=1,' .. authProt .. ',"' .. authUser .. '","' .. authPassword .. '"', nil, function(cmd, result)
|
|
||||||
if result then
|
|
||||||
sys.restart("CGDFAUTH")
|
|
||||||
else
|
|
||||||
sys.timerStart(setCgdf, 5000)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setAuthApn(prot, apn, user, pwd)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
request('AT+CPNETAPN=2,"' .. apn .. '","' .. user .. '","' .. pwd .. '",' .. prot)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
local function Pdp_Act()
|
|
||||||
log.info("link.Pdp_Act", readyTable[CELLULAR], net.getNetMode(), gprsAttached)
|
|
||||||
if readyTable[CELLULAR] then
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if net.getNetMode() == net.NetMode_LTE then
|
|
||||||
if not gprsAttached then
|
|
||||||
gprsAttached = true
|
|
||||||
sys.publish("GPRS_ATTACH", true)
|
|
||||||
end
|
|
||||||
if not apnname then
|
|
||||||
sys.timerStart(pdpCmdCnf, 1000, "SET_PDP_4G_WAITAPN", true)
|
|
||||||
else
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp)
|
|
||||||
|
|
||||||
end
|
|
||||||
else
|
|
||||||
request('AT+CGATT?')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function procshut(curCmd, result, respdata, interdata)
|
|
||||||
if network ~= CELLULAR then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if IsCidActived(cid_manual, interdata) then
|
|
||||||
ril.request(string.format('AT+CGACT=0,%d', cid_manual), nil, function(cmd, result)
|
|
||||||
if result then
|
|
||||||
readyTable[CELLULAR] = false
|
|
||||||
sys.publish('IP_ERROR_IND')
|
|
||||||
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sys.timerStart(Pdp_Act, 2000)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
else
|
|
||||||
readyTable[CELLULAR] = false
|
|
||||||
sys.publish('IP_ERROR_IND')
|
|
||||||
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sys.timerStart(Pdp_Act, 2000)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function shut()
|
|
||||||
if network ~= CELLULAR then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
readyTable[CELLULAR] = false
|
|
||||||
sys.publish('IP_ERROR_IND')
|
|
||||||
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sys.timerStart(Pdp_Act, 2000)
|
|
||||||
end
|
|
||||||
|
|
||||||
function analysis_cgdcont(data)
|
|
||||||
local tmp, loc, result
|
|
||||||
while data do
|
|
||||||
_, loc = string.find(data, "\r\n")
|
|
||||||
if loc then
|
|
||||||
tmp = string.sub(data, 1, loc)
|
|
||||||
data = string.sub(data, loc + 1, -1)
|
|
||||||
log.info("analysis_cgdcont ", tmp, loc, data)
|
|
||||||
else
|
|
||||||
tmp = data
|
|
||||||
data = nil
|
|
||||||
log.info("analysis_cgdcont end", tmp, loc, data)
|
|
||||||
end
|
|
||||||
if tmp then
|
|
||||||
local cid, pdptyp, apn, addr = string.match(tmp, "(%d+),(.+),(.+),[\"\'](.+)[\"\']")
|
|
||||||
if not cid or not pdptyp or not apn or not addr then
|
|
||||||
log.info("analysis_cgdcont CGDCONT is empty")
|
|
||||||
ipAddr = ""
|
|
||||||
result = false
|
|
||||||
else
|
|
||||||
log.info("analysis_cgdcont ", cid, pdptyp, apn, addr)
|
|
||||||
if addr:match("%d+%.%d+%.%d+%.%d") then
|
|
||||||
ipAddr = addr
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
log.info("analysis_cgdcont CGDCONT is empty1")
|
|
||||||
ipAddr = ""
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
ipAddr = ""
|
|
||||||
log.info("analysis_cgdcont tmp is empty")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
function IsCidActived(cid, data)
|
|
||||||
if not data then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
for k, v in string.gfind(data, "(%d+),%s*(%d)") do
|
|
||||||
log.info("iscidactived ", k, v)
|
|
||||||
if cid == tonumber(k) and v == '1' then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
function IsExistActivedCid(data)
|
|
||||||
if not data then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
for k, v in string.gfind(data, "(%d+),%s*(%d)") do
|
|
||||||
if v == '1' then
|
|
||||||
log.info("ExistActivedCid ", k, v)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cgdcontResult
|
|
||||||
|
|
||||||
function cgdcontRsp()
|
|
||||||
if cgdcontResult then
|
|
||||||
pdpCmdCnf("CONNECT_DELAY", true)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function pdpCmdCnf(curCmd, result, respdata, interdata)
|
|
||||||
log.info("link.pdpCmdCnf", curCmd, result, respdata, interdata)
|
|
||||||
if string.find(curCmd, "CGDCONT%?") then
|
|
||||||
if result and interdata then
|
|
||||||
result = analysis_cgdcont(interdata)
|
|
||||||
else
|
|
||||||
result = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if result then
|
|
||||||
cgdcontResult = false
|
|
||||||
if string.find(curCmd, "CGDCONT=") then
|
|
||||||
request(string.format('AT+CGACT=1,%d', cid_manual), nil, pdpCmdCnf)
|
|
||||||
elseif string.find(curCmd, "CGDCONT%?") then
|
|
||||||
|
|
||||||
cgdcontResult = true
|
|
||||||
elseif string.find(curCmd, "CONNECT_DELAY") and network == CELLULAR then
|
|
||||||
log.info("publish IP_READY_IND")
|
|
||||||
readyTable[CELLULAR] = true
|
|
||||||
publish("IP_READY_IND")
|
|
||||||
elseif string.find(curCmd, "CGACT=") then
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp)
|
|
||||||
elseif string.find(curCmd, "CGACT%?") then
|
|
||||||
if IsExistActivedCid(interdata) then
|
|
||||||
sys.timerStart(pdpCmdCnf, 100, "CONNECT_DELAY", true)
|
|
||||||
else
|
|
||||||
request(string.format('AT+CGDCONT=%d,"IP","%s"', cid_manual, authApn or apnname), nil, pdpCmdCnf)
|
|
||||||
end
|
|
||||||
elseif string.find(curCmd, "CGDFLT") then
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp)
|
|
||||||
elseif string.find(curCmd, "SET_PDP_4G_WAITAPN") then
|
|
||||||
if not apnname then
|
|
||||||
sys.timerStart(pdpCmdCnf, 100, "SET_PDP_4G_WAITAPN", true)
|
|
||||||
else
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp, 1000)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if net.getNetMode() == net.NetMode_LTE then
|
|
||||||
request("AT+CGDCONT?", nil, cgdcontRsp, 1000)
|
|
||||||
else
|
|
||||||
request("AT+CGATT?", nil, nil, 1000)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
sys.subscribe("IMSI_READY", function()
|
|
||||||
if not apnname then
|
|
||||||
local mcc, mnc = tonumber(sim.getMcc(), 16), tonumber(sim.getMnc(), 16)
|
|
||||||
apnname, username, password = apn and apn.get_default_apn(mcc, mnc)
|
|
||||||
if not apnname or apnname == '' or apnname == "CMNET" then
|
|
||||||
apnname = (mcc == 0x460 and (mnc == 0x01 or mnc == 0x06)) and 'UNINET' or 'CMIOT'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
username = username or ''
|
|
||||||
password = password or ''
|
|
||||||
end)
|
|
||||||
|
|
||||||
ril.regRsp('+CGATT', function(a, b, c, intermediate)
|
|
||||||
local attached = (intermediate == "+CGATT: 1")
|
|
||||||
if gprsAttached ~= attached then
|
|
||||||
gprsAttached = attached
|
|
||||||
sys.publish("GPRS_ATTACH", attached)
|
|
||||||
end
|
|
||||||
|
|
||||||
if readyTable[CELLULAR] then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if attached then
|
|
||||||
log.info("pdp active", apnname, username, password)
|
|
||||||
request("AT+CGACT?", nil, pdpCmdCnf, 1000)
|
|
||||||
elseif net.getState() == 'REGISTERED' then
|
|
||||||
sys.timerStart(request, 2000, "AT+CGATT=1")
|
|
||||||
sys.timerStart(request, 2000, "AT+CGATT?")
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
rtos.on(rtos.MSG_PDP_DEACT_IND, function()
|
|
||||||
if network ~= CELLULAR then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
readyTable[CELLULAR] = false
|
|
||||||
sys.publish('IP_ERROR_IND')
|
|
||||||
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sys.timerStart(Pdp_Act, 2000)
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sys.subscribe("NET_STATE_REGISTERED", Pdp_Act)
|
|
||||||
|
|
||||||
local function cindCnf(cmd, result)
|
|
||||||
if not result then
|
|
||||||
request("AT+CIND=1", nil, cindCnf, 1000)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function cgevurc(data)
|
|
||||||
if network ~= CELLULAR then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local cid = 0
|
|
||||||
log.info("link.cgevurc", data)
|
|
||||||
|
|
||||||
if string.match(data, "DEACT") then
|
|
||||||
cid = string.match(data, "DEACT,(%d)")
|
|
||||||
cid = tonumber(cid)
|
|
||||||
|
|
||||||
if cid == cid_manual then
|
|
||||||
request("AT+CFUN?")
|
|
||||||
readyTable[CELLULAR] = false
|
|
||||||
sys.publish('IP_ERROR_IND')
|
|
||||||
sys.publish('PDP_DEACT_IND')
|
|
||||||
if net.getState() ~= 'REGISTERED' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
sys.timerStart(Pdp_Act, 2000)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
request("AT+CIND=1", nil, cindCnf)
|
|
||||||
ril.regUrc("*CGEV", cgevurc)
|
|
||||||
ril.regUrc("+CGDCONT", function(data)
|
|
||||||
pdpCmdCnf("AT+CGDCONT?", true, "OK", data)
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function openNetwork(mode, para)
|
|
||||||
local tSocketModule = {
|
|
||||||
[CH395] = socketCh395,
|
|
||||||
[W5500] = socketW5500,
|
|
||||||
[ESP8266] = socketESP8266
|
|
||||||
}
|
|
||||||
local md = mode or CELLULAR
|
|
||||||
closeNetWork()
|
|
||||||
network = md
|
|
||||||
if network == CELLULAR then
|
|
||||||
net.switchFly(false)
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
ipAddr = tSocketModule[network].open(para)
|
|
||||||
if ipAddr ~= "" then
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
log.info('link', 'open CH395 err')
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function closeNetWork()
|
|
||||||
local tSocketModule = {
|
|
||||||
[CH395] = socketCh395,
|
|
||||||
[W5500] = socketW5500,
|
|
||||||
[ESP8266] = socketESP8266
|
|
||||||
}
|
|
||||||
|
|
||||||
if network == CELLULAR then
|
|
||||||
|
|
||||||
net.switchFly(true)
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
return tSocketModule[network].close()
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getNetwork()
|
|
||||||
return network
|
|
||||||
end
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
require "socket"
|
|
||||||
require "log"
|
|
||||||
|
|
||||||
|
|
||||||
local ip, port = "121.43.69.62", "8767"
|
|
||||||
|
|
||||||
local clientCount = 1
|
|
||||||
|
|
||||||
local clients = {}
|
|
||||||
|
|
||||||
|
|
||||||
for i = 1, clientCount do
|
|
||||||
local client = {
|
|
||||||
id = i,
|
|
||||||
socket = nil,
|
|
||||||
connected = false
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
sys.taskInit(function()
|
|
||||||
while true do
|
|
||||||
while not socket.isReady() do sys.wait(1000) end
|
|
||||||
client.socket = socket.tcp()
|
|
||||||
log.debug("Client " .. client.id .. ": Connecting to " .. ip .. ":" .. port)
|
|
||||||
while not client.socket:connect(ip, port) do
|
|
||||||
log.warn("Client " .. client.id .. ": Connection failed, retrying...")
|
|
||||||
sys.wait(2000)
|
|
||||||
end
|
|
||||||
client.connected = true
|
|
||||||
log.debug("Client " .. client.id .. ": Connected successfully")
|
|
||||||
|
|
||||||
uart.write(1, "Socket " .. client.id .. " connected\r\n")
|
|
||||||
|
|
||||||
|
|
||||||
while client.socket:asyncSelect(60, "ping") do end
|
|
||||||
|
|
||||||
client.connected = false
|
|
||||||
client.socket:close()
|
|
||||||
log.error("Client " .. client.id .. ": Disconnected")
|
|
||||||
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
clients[i] = client
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function toHexcode(str)
|
|
||||||
local hexcode = ""
|
|
||||||
for i = 1, #str do
|
|
||||||
hexcode = hexcode .. string.format("%02X", str:byte(i))
|
|
||||||
end
|
|
||||||
return hexcode
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
sys.subscribe("SOCKET_RECV", function(id)
|
|
||||||
|
|
||||||
for i, client in ipairs(clients) do
|
|
||||||
if client.socket and client.socket.id == id then
|
|
||||||
|
|
||||||
local data = client.socket:asyncRecv()
|
|
||||||
log.info("Client " .. client.id .. ": Received data: " .. toHexcode(data))
|
|
||||||
|
|
||||||
local frame = string.char(0x55) .. string.char(0xAA) .. string.char(0x01) .. string.char(i) .. string.char(#data) .. data .. string.char(0xAA) .. string.char(0x55)
|
|
||||||
uart.write(1, frame)
|
|
||||||
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sendToSocket(id_byte, msg)
|
|
||||||
if clients[id_byte] and clients[id_byte].socket and clients[id_byte].connected then
|
|
||||||
clients[id_byte].socket:asyncSend(msg)
|
|
||||||
else
|
|
||||||
log.error("Socket " .. id_byte .. " not connected or does not exist")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
LOG_SILENT = 0x00;
|
|
||||||
LOGLEVEL_TRACE = 0x01;
|
|
||||||
LOGLEVEL_DEBUG = 0x02;
|
|
||||||
LOGLEVEL_INFO = 0x03;
|
|
||||||
LOGLEVEL_WARN = 0x04;
|
|
||||||
LOGLEVEL_ERROR = 0x05;
|
|
||||||
LOGLEVEL_FATAL = 0x06;
|
|
||||||
|
|
||||||
|
|
||||||
local LEVEL_TAG = {'T', 'D', 'I', 'W', 'E', 'F'}
|
|
||||||
local PREFIX_FMT = "[%s]-[%s]"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function _log(level, tag, ...)
|
|
||||||
|
|
||||||
local OPENLEVEL = LOG_LEVEL and LOG_LEVEL or LOGLEVEL_INFO
|
|
||||||
|
|
||||||
if OPENLEVEL == LOG_SILENT or OPENLEVEL > level then return end
|
|
||||||
|
|
||||||
local prefix = string.format(PREFIX_FMT, LEVEL_TAG[level], type(tag)=="string" and tag or "")
|
|
||||||
print(prefix, ...)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function trace(tag, ...)
|
|
||||||
_log(LOGLEVEL_TRACE, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function debug(tag, ...)
|
|
||||||
_log(LOGLEVEL_DEBUG, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function info(tag, ...)
|
|
||||||
_log(LOGLEVEL_INFO, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function warn(tag, ...)
|
|
||||||
_log(LOGLEVEL_WARN, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function error(tag, ...)
|
|
||||||
_log(LOGLEVEL_ERROR, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function fatal(tag, ...)
|
|
||||||
_log(LOGLEVEL_FATAL, tag, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function openTrace(v, uartid, baudrate)
|
|
||||||
rtos.set_trace(v and 1 or 0, uartid,baudrate)
|
|
||||||
end
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PROJECT = "4G_NETWORK"
|
|
||||||
VERSION = "1.0.0"
|
|
||||||
|
|
||||||
require "sys"
|
|
||||||
require "net"
|
|
||||||
require "log"
|
|
||||||
require "cmd"
|
|
||||||
require "uart"
|
|
||||||
require "linksocket"
|
|
||||||
|
|
||||||
|
|
||||||
uart.setup(1, 115200, 8, uart.PAR_NONE, uart.STOP_1)
|
|
||||||
|
|
||||||
|
|
||||||
require "netLed"
|
|
||||||
pmd.ldoset(2,pmd.LDO_VLCD)
|
|
||||||
netLed.setup(true,pio.P0_1,pio.P0_4)
|
|
||||||
|
|
||||||
local uartID = 1
|
|
||||||
|
|
||||||
|
|
||||||
local function toHexcode(str)
|
|
||||||
local hexcode = ""
|
|
||||||
for i = 1, #str do
|
|
||||||
hexcode = hexcode .. string.format("%02X", str:byte(i))
|
|
||||||
end
|
|
||||||
return hexcode
|
|
||||||
end
|
|
||||||
|
|
||||||
uart.on(uartID, "receive", function()
|
|
||||||
local data = uart.read(uartID, 300)
|
|
||||||
if data and type(data) == "string" and #data > 0 then
|
|
||||||
log.info("UART received:", toHexcode(data))
|
|
||||||
if data:byte(1) == 0x55 and data:byte(2) == 0xAA and data:byte(-2) == 0xAA and data:byte(-1) == 0x55 then
|
|
||||||
local Main_Cmd = data:byte(3)
|
|
||||||
local Sub_Cmd = data:byte(4)
|
|
||||||
local payload = data:sub(5, -3)
|
|
||||||
|
|
||||||
if Main_Cmd ==0x01 then
|
|
||||||
handleCmd01(Sub_Cmd, payload)
|
|
||||||
end
|
|
||||||
if Main_Cmd == 0x02 then
|
|
||||||
handleCmd02(Sub_Cmd, payload)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
log.warn("UART receive callback triggered but no valid data")
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
net.startQueryAll(8 * 1000, 60 * 1000)
|
|
||||||
|
|
||||||
ril.request("AT+RNDISCALL=0,1")
|
|
||||||
|
|
||||||
|
|
||||||
sys.init(0, 0)
|
|
||||||
sys.run()
|
|
||||||
@@ -1,787 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "sys"
|
|
||||||
require "ril"
|
|
||||||
require "pio"
|
|
||||||
require "sim"
|
|
||||||
require "log"
|
|
||||||
require "utils"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
local publish = sys.publish
|
|
||||||
|
|
||||||
|
|
||||||
NetMode_noNet= 0
|
|
||||||
NetMode_GSM= 1
|
|
||||||
NetMode_EDGE= 2
|
|
||||||
NetMode_TD= 3
|
|
||||||
NetMode_LTE= 4
|
|
||||||
NetMode_WCDMA= 5
|
|
||||||
local netMode = NetMode_noNet
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local state = "INIT"
|
|
||||||
|
|
||||||
local simerrsta
|
|
||||||
|
|
||||||
flyMode = false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local lac, ci, rssi, rsrp, band = "", "", 0, 0, ""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local cellinfo, multicellcb = {}
|
|
||||||
local curCellSeted
|
|
||||||
|
|
||||||
local function cops(data)
|
|
||||||
|
|
||||||
local fmt,oper = data:match('COPS:%s*%d+%s*,(%d+)%s*,"(%d+)"')
|
|
||||||
log.info("cops",fmt,oper,curCellSeted)
|
|
||||||
if fmt=="2" and not curCellSeted then
|
|
||||||
cellinfo[1].mcc = tonumber(oper:sub(1,3),16)
|
|
||||||
cellinfo[1].mnc = tonumber(oper:sub(4,5),16)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function creg(data)
|
|
||||||
local p1, s,act
|
|
||||||
local prefix = (netMode == NetMode_LTE) and "+CEREG: " or (netMode == NetMode_noNet and "+CREG: " or "+CGREG: ")
|
|
||||||
log.info("net.creg1",netMode,prefix)
|
|
||||||
if not data:match(prefix) then
|
|
||||||
|
|
||||||
if prefix=="+CREG: " then
|
|
||||||
|
|
||||||
prefix = "+CGREG: "
|
|
||||||
if not data:match("+CGREG: ") then
|
|
||||||
log.warn("net.creg1","no match",data)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
elseif prefix=="+CGREG: " then
|
|
||||||
|
|
||||||
prefix = "+CREG: "
|
|
||||||
if not data:match("+CREG: ") then
|
|
||||||
log.warn("net.creg2","no match",data)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
_, _, p1 = data:find(prefix .. "%d,(%d+)")
|
|
||||||
|
|
||||||
if p1 == nil then
|
|
||||||
_, _, p1 = data:find(prefix .. "(%d+)")
|
|
||||||
|
|
||||||
if p1 == nil then return end
|
|
||||||
act = data:match(prefix .. "%d+,.-,.-,(%d+)")
|
|
||||||
else
|
|
||||||
act = data:match(prefix .. "%d,%d+,.-,.-,(%d+)")
|
|
||||||
end
|
|
||||||
|
|
||||||
log.info("net.creg7",p1,act)
|
|
||||||
|
|
||||||
|
|
||||||
s = (p1=="1" or p1=="5") and "REGISTERED" or "UNREGISTER"
|
|
||||||
|
|
||||||
|
|
||||||
if prefix=="+CGREG: " and s=="UNREGISTER" then
|
|
||||||
log.info("net.creg9 ignore!!!")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if s ~= state then
|
|
||||||
|
|
||||||
if s == "REGISTERED" then
|
|
||||||
|
|
||||||
publish("NET_STATE_REGISTERED")
|
|
||||||
cengQueryPoll()
|
|
||||||
end
|
|
||||||
state = s
|
|
||||||
end
|
|
||||||
|
|
||||||
if state == "REGISTERED" then
|
|
||||||
p2, p3 = data:match("\"(%x+)\",\"(%x+)\"")
|
|
||||||
if p2 and p3 and (lac ~= p2 or ci ~= p3) then
|
|
||||||
lac = p2
|
|
||||||
ci = p3
|
|
||||||
|
|
||||||
publish("NET_CELL_CHANGED")
|
|
||||||
|
|
||||||
|
|
||||||
cellinfo[1].lac = tonumber(lac,16)
|
|
||||||
cellinfo[1].ci = tonumber(ci,16)
|
|
||||||
cellinfo[1].rssi = 28
|
|
||||||
end
|
|
||||||
|
|
||||||
if act then
|
|
||||||
if act=="0" then
|
|
||||||
UpdNetMode("^MODE: 3,1")
|
|
||||||
elseif act=="1" then
|
|
||||||
UpdNetMode("^MODE: 3,2")
|
|
||||||
elseif act=="3" then
|
|
||||||
UpdNetMode("^MODE: 3,3")
|
|
||||||
elseif act=="7" then
|
|
||||||
UpdNetMode("^MODE: 17,17")
|
|
||||||
else
|
|
||||||
UpdNetMode("^MODE: 5,7")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function resetCellInfo()
|
|
||||||
local i
|
|
||||||
cellinfo.cnt = 11
|
|
||||||
for i = 1, cellinfo.cnt do
|
|
||||||
cellinfo[i] = {}
|
|
||||||
cellinfo[i].mcc, cellinfo[i].mnc = nil
|
|
||||||
cellinfo[i].lac = 0
|
|
||||||
cellinfo[i].ci = 0
|
|
||||||
cellinfo[i].rssi = 0
|
|
||||||
cellinfo[i].ta = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function eemLteSvc(data)
|
|
||||||
local mcc,mnc,lac,ci,rssi,svcData
|
|
||||||
if data:match("%+EEMLTESVC:%s*%d+,%s*%d+,%s*%d+,%s*.+") then
|
|
||||||
svcData = string.match(data, "%+EEMLTESVC:(.+)")
|
|
||||||
|
|
||||||
if svcData then
|
|
||||||
svcDataT = string.split(svcData, ', ')
|
|
||||||
|
|
||||||
if not(svcDataT[1] and svcDataT[3] and svcDataT[4] and svcDataT[10] and svcDataT[15]) then
|
|
||||||
svcDataT = string.split(svcData, ',')
|
|
||||||
log.info("eemLteSvc2",svcDataT[1],svcDataT[3],svcDataT[4],svcDataT[10],svcDataT[15])
|
|
||||||
end
|
|
||||||
mcc = svcDataT[1]
|
|
||||||
mnc = svcDataT[3]
|
|
||||||
lac = svcDataT[4]
|
|
||||||
ci = svcDataT[10]
|
|
||||||
band = svcDataT[8]
|
|
||||||
rssi = (tonumber(svcDataT[15])-(tonumber(svcDataT[15])%3))/3
|
|
||||||
if rssi>31 then rssi=31 end
|
|
||||||
if rssi<0 then rssi=0 end
|
|
||||||
end
|
|
||||||
log.info("eemLteSvc1",lac,ci,mcc,mnc)
|
|
||||||
if lac and lac~="0" and ci and ci ~= "0" and mcc and mnc then
|
|
||||||
|
|
||||||
resetCellInfo()
|
|
||||||
curCellSeted = true
|
|
||||||
|
|
||||||
cellinfo[1].mcc = mcc
|
|
||||||
cellinfo[1].mnc = mnc
|
|
||||||
cellinfo[1].lac = tonumber(lac)
|
|
||||||
cellinfo[1].ci = tonumber(ci)
|
|
||||||
cellinfo[1].rssi = tonumber(rssi)
|
|
||||||
|
|
||||||
|
|
||||||
if multicellcb then multicellcb(cellinfo) end
|
|
||||||
publish("CELL_INFO_IND", cellinfo)
|
|
||||||
end
|
|
||||||
elseif data:match("%+EEMLTEINTER") or data:match("%+EEMLTEINTRA") or data:match("%+EEMLTEINTERRAT") then
|
|
||||||
|
|
||||||
|
|
||||||
data = data:gsub(" ","")
|
|
||||||
|
|
||||||
if data:match("%+EEMLTEINTERRAT") then
|
|
||||||
mcc,mnc,lac,ci,rssi = data:match("[-]*%d+,[-]*%d+,([-]*%d+),([-]*%d+),([-]*%d+),([-]*%d+),[-]*%d+,[-]*%d+,([-]*%d+)")
|
|
||||||
else
|
|
||||||
rssi,mcc,mnc,lac,ci = data:match("[-]*%d+,[-]*%d+,[-]*%d+,([-]*%d+),[-]*%d+,([-]*%d+),([-]*%d+),([-]*%d+),([-]*%d+)")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if rssi then
|
|
||||||
rssi = (rssi-(rssi%3))/3
|
|
||||||
if rssi>31 then rssi=31 end
|
|
||||||
if rssi<0 then rssi=0 end
|
|
||||||
end
|
|
||||||
if lac~="0" and lac~="-1" and ci~="0" and ci~="-1" then
|
|
||||||
for i = 1, cellinfo.cnt do
|
|
||||||
|
|
||||||
if cellinfo[i].lac==0 then
|
|
||||||
cellinfo[i] =
|
|
||||||
{
|
|
||||||
mcc = mcc,
|
|
||||||
mnc = mnc,
|
|
||||||
lac = tonumber(lac),
|
|
||||||
ci = tonumber(ci),
|
|
||||||
rssi = tonumber(rssi)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function eemGsmInfoSvc(data)
|
|
||||||
|
|
||||||
if string.find(data, "%+EEMGINFOSVC:%s*%d+,%s*%d+,%s*%d+,%s*.+") then
|
|
||||||
local mcc,mnc,lac,ci,ta,rssi
|
|
||||||
local svcData = string.match(data, "%+EEMGINFOSVC:(.+)")
|
|
||||||
if svcData then
|
|
||||||
svcDataT = string.split(svcData, ', ')
|
|
||||||
mcc = svcDataT[1]
|
|
||||||
mnc = svcDataT[2]
|
|
||||||
lac = svcDataT[3]
|
|
||||||
ci = svcDataT[4]
|
|
||||||
ta = svcDataT[10]
|
|
||||||
rssi = svcDataT[12]
|
|
||||||
if tonumber(rssi) >31
|
|
||||||
then rssi = 31
|
|
||||||
end
|
|
||||||
if tonumber(rssi) < 0
|
|
||||||
then rssi = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if lac and lac~="0" and ci and ci ~= "0" and mcc and mnc then
|
|
||||||
|
|
||||||
resetCellInfo()
|
|
||||||
curCellSeted = true
|
|
||||||
|
|
||||||
cellinfo[1].mcc = mcc
|
|
||||||
cellinfo[1].mnc = mnc
|
|
||||||
cellinfo[1].lac = tonumber(lac)
|
|
||||||
cellinfo[1].ci = tonumber(ci)
|
|
||||||
cellinfo[1].rssi = (tonumber(rssi) == 99) and 0 or tonumber(rssi)
|
|
||||||
cellinfo[1].ta = tonumber(ta or "0")
|
|
||||||
|
|
||||||
if multicellcb then multicellcb(cellinfo) end
|
|
||||||
publish("CELL_INFO_IND", cellinfo)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function eemGsmNCInfoSvc(data)
|
|
||||||
if string.find(data, "%+EEMGINFONC: %d+, %d+, %d+, .+") then
|
|
||||||
local mcc,mnc,lac,ci,ta,rssi,id
|
|
||||||
local svcData = string.match(data, "%+EEMGINFONC:(.+)")
|
|
||||||
if svcData then
|
|
||||||
svcDataT = string.split(svcData, ', ')
|
|
||||||
id = svcDataT[1]
|
|
||||||
mcc = svcDataT[2]
|
|
||||||
mnc = svcDataT[3]
|
|
||||||
lac = svcDataT[4]
|
|
||||||
ci = svcDataT[6]
|
|
||||||
rssi = svcDataT[7]
|
|
||||||
if tonumber(rssi) >31
|
|
||||||
then rssi = 31
|
|
||||||
end
|
|
||||||
if tonumber(rssi) < 0
|
|
||||||
then rssi = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if lac and ci and mcc and mnc then
|
|
||||||
|
|
||||||
cellinfo[id + 2].mcc = mcc
|
|
||||||
cellinfo[id + 2].mnc = mnc
|
|
||||||
cellinfo[id + 2].lac = tonumber(lac)
|
|
||||||
cellinfo[id + 2].ci = tonumber(ci)
|
|
||||||
cellinfo[id + 2].rssi = (tonumber(rssi) == 99) and 0 or tonumber(rssi)
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function eemUMTSInfoSvc(data)
|
|
||||||
|
|
||||||
if string.find(data, "%+EEMUMTSSVC: %d+, %d+, %d+, .+") then
|
|
||||||
local mcc,mnc,lac,ci,rssi
|
|
||||||
local svcData = string.match(data, "%+EEMUMTSSVC:(.+)")
|
|
||||||
local cellMeasureFlag, cellParamFlag = string.match(data, "%+EEMUMTSSVC:%d+, (%d+), (%d+), .+")
|
|
||||||
local svcDataT = string.split(svcData, ', ')
|
|
||||||
local offset = 4
|
|
||||||
if svcData and svcDataT then
|
|
||||||
if tonumber(cellMeasureFlag) ~= 0 then
|
|
||||||
offset = offset + 2
|
|
||||||
rssi = svcDataT[offset]
|
|
||||||
offset = offset + 4
|
|
||||||
else
|
|
||||||
offset = offset + 2
|
|
||||||
rssi = svcDataT[offset]
|
|
||||||
offset = offset + 2
|
|
||||||
end
|
|
||||||
|
|
||||||
if tonumber(cellParamFlag) ~= 0 then
|
|
||||||
offset = offset + 3
|
|
||||||
mcc = svcDataT[offset]
|
|
||||||
mnc = svcDataT[offset + 1]
|
|
||||||
lac = svcDataT[offset + 2]
|
|
||||||
ci = svcDataT[offset + 3]
|
|
||||||
offset = offset + 3
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if lac and lac~="0" and ci and ci ~= "0" and mcc and mnc and rssi then
|
|
||||||
|
|
||||||
resetCellInfo()
|
|
||||||
curCellSeted = true
|
|
||||||
|
|
||||||
cellinfo[1].mcc = mcc
|
|
||||||
cellinfo[1].mnc = mnc
|
|
||||||
cellinfo[1].lac = tonumber(lac)
|
|
||||||
cellinfo[1].ci = tonumber(ci)
|
|
||||||
cellinfo[1].rssi = tonumber(rssi)
|
|
||||||
|
|
||||||
if multicellcb then multicellcb(cellinfo) end
|
|
||||||
publish("CELL_INFO_IND", cellinfo)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function UpdNetMode(data)
|
|
||||||
local _, _, SysMainMode,SysMode = string.find(data, "(%d+),(%d+)")
|
|
||||||
local netMode_cur
|
|
||||||
log.info("net.UpdNetMode",netMode_cur,netMode, SysMainMode,SysMode)
|
|
||||||
if SysMainMode and SysMode then
|
|
||||||
if SysMainMode=="3" then
|
|
||||||
netMode_cur = NetMode_GSM
|
|
||||||
elseif SysMainMode=="5" then
|
|
||||||
netMode_cur = NetMode_WCDMA
|
|
||||||
elseif SysMainMode=="15" then
|
|
||||||
netMode_cur = NetMode_TD
|
|
||||||
elseif SysMainMode=="17" then
|
|
||||||
netMode_cur = NetMode_LTE
|
|
||||||
else
|
|
||||||
netMode_cur = NetMode_noNet
|
|
||||||
end
|
|
||||||
|
|
||||||
if SysMode=="3" then
|
|
||||||
netMode_cur = NetMode_EDGE
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if netMode ~= netMode_cur then
|
|
||||||
netMode = netMode_cur
|
|
||||||
publish("NET_UPD_NET_MODE",netMode)
|
|
||||||
log.info("net.NET_UPD_NET_MODE",netMode)
|
|
||||||
ril.request("AT+COPS?")
|
|
||||||
if netMode == NetMode_LTE then
|
|
||||||
ril.request("AT+CEREG?")
|
|
||||||
elseif netMode == NetMode_noNet then
|
|
||||||
ril.request("AT+CREG?")
|
|
||||||
else
|
|
||||||
ril.request("AT+CGREG?")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function neturc(data, prefix)
|
|
||||||
if prefix=="+COPS" then
|
|
||||||
cops(data)
|
|
||||||
elseif prefix == "+CREG" or prefix == "+CGREG" or prefix == "+CEREG" then
|
|
||||||
|
|
||||||
csqQueryPoll()
|
|
||||||
|
|
||||||
creg(data)
|
|
||||||
elseif prefix == "+EEMLTESVC" or prefix == "+EEMLTEINTRA" or prefix == "+EEMLTEINTER" or prefix=="+EEMLTEINTERRAT" then
|
|
||||||
eemLteSvc(data)
|
|
||||||
elseif prefix == "+EEMUMTSSVC" then
|
|
||||||
eemUMTSInfoSvc(data)
|
|
||||||
elseif prefix == "+EEMGINFOSVC" then
|
|
||||||
eemGsmInfoSvc(data)
|
|
||||||
elseif prefix == "+EEMGINFONC" then
|
|
||||||
eemGsmNCInfoSvc(data)
|
|
||||||
elseif prefix == "^MODE" then
|
|
||||||
UpdNetMode(data)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function switchFly(mode)
|
|
||||||
if flyMode == mode then return end
|
|
||||||
flyMode = mode
|
|
||||||
|
|
||||||
if mode then
|
|
||||||
ril.request("AT+CFUN=0")
|
|
||||||
|
|
||||||
else
|
|
||||||
ril.request("AT+CFUN=1")
|
|
||||||
|
|
||||||
csqQueryPoll()
|
|
||||||
cengQueryPoll()
|
|
||||||
|
|
||||||
neturc("2", "+CREG")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getNetMode()
|
|
||||||
return netMode
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getState()
|
|
||||||
return state
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getMcc()
|
|
||||||
return cellinfo[1].mcc and string.format("%x",cellinfo[1].mcc) or sim.getMcc()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getMnc()
|
|
||||||
return cellinfo[1].mnc and string.format("%x",cellinfo[1].mnc) or sim.getMnc()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getLac()
|
|
||||||
return lac
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getBand()
|
|
||||||
return band
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getCi()
|
|
||||||
return ci
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getRssi()
|
|
||||||
return rssi
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getRsrp()
|
|
||||||
return rsrp
|
|
||||||
end
|
|
||||||
|
|
||||||
function getCell()
|
|
||||||
local i,ret = 1,""
|
|
||||||
for i=1,cellinfo.cnt do
|
|
||||||
if cellinfo[i] and cellinfo[i].lac and cellinfo[i].lac ~= 0 and cellinfo[i].ci and cellinfo[i].ci ~= 0 then
|
|
||||||
ret = ret..cellinfo[i].ci.."."..cellinfo[i].rssi.."."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getCellInfo()
|
|
||||||
local i, ret = 1, ""
|
|
||||||
for i = 1, cellinfo.cnt do
|
|
||||||
if cellinfo[i] and cellinfo[i].lac and cellinfo[i].lac ~= 0 and cellinfo[i].ci and cellinfo[i].ci ~= 0 then
|
|
||||||
ret = ret .. cellinfo[i].lac .. "." .. cellinfo[i].ci .. "." .. cellinfo[i].rssi .. ";"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getCellInfoExt(rssi)
|
|
||||||
local i, ret = 1, ""
|
|
||||||
for i = 1, cellinfo.cnt do
|
|
||||||
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
|
|
||||||
ret = ret .. string.format("%x",cellinfo[i].mcc) .. "." .. string.format("%x",cellinfo[i].mnc) .. "." .. cellinfo[i].lac .. "." .. cellinfo[i].ci .. "." .. (rssi and (cellinfo[i].rssi*2-113) or cellinfo[i].rssi) .. ";"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return ret
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getTa()
|
|
||||||
return cellinfo[1].ta
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function rsp(cmd, success, response, intermediate)
|
|
||||||
local prefix = string.match(cmd, "AT(%+%u+)")
|
|
||||||
|
|
||||||
if intermediate ~= nil then
|
|
||||||
if prefix == "+CSQ" then
|
|
||||||
local s = string.match(intermediate, "+CSQ:%s*(%d+)")
|
|
||||||
if s ~= nil then
|
|
||||||
rssi = tonumber(s)
|
|
||||||
rssi = rssi == 99 and 0 or rssi
|
|
||||||
|
|
||||||
publish("GSM_SIGNAL_REPORT_IND", success, rssi)
|
|
||||||
end
|
|
||||||
elseif prefix == "+CESQ" then
|
|
||||||
local s = string.match(intermediate, "+CESQ: %d+,%d+,%d+,%d+,%d+,(%d+)")
|
|
||||||
if s ~= nil then
|
|
||||||
rsrp = tonumber(s)
|
|
||||||
end
|
|
||||||
elseif prefix == "+CENG" then end
|
|
||||||
end
|
|
||||||
if prefix == "+CFUN" then
|
|
||||||
if success then publish("FLYMODE", flyMode) end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getMultiCell(cbFnc)
|
|
||||||
multicellcb = cbFnc
|
|
||||||
|
|
||||||
ril.request("AT+EEMGINFO?")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function cengQueryPoll(period)
|
|
||||||
|
|
||||||
if not flyMode then
|
|
||||||
|
|
||||||
ril.request("AT+EEMGINFO?")
|
|
||||||
else
|
|
||||||
log.warn("net.cengQueryPoll", "flymode:", flyMode)
|
|
||||||
end
|
|
||||||
if nil ~= period then
|
|
||||||
|
|
||||||
sys.timerStopAll(cengQueryPoll)
|
|
||||||
sys.timerStart(cengQueryPoll, period, period)
|
|
||||||
end
|
|
||||||
return not flyMode
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function csqQueryPoll(period)
|
|
||||||
|
|
||||||
if not flyMode then
|
|
||||||
|
|
||||||
ril.request("AT+CSQ")
|
|
||||||
ril.request("AT+CESQ")
|
|
||||||
else
|
|
||||||
log.warn("net.csqQueryPoll", "flymode:", flyMode)
|
|
||||||
end
|
|
||||||
if nil ~= period then
|
|
||||||
|
|
||||||
sys.timerStopAll(csqQueryPoll)
|
|
||||||
sys.timerStart(csqQueryPoll, period, period)
|
|
||||||
end
|
|
||||||
return not flyMode
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function startQueryAll(...)
|
|
||||||
local arg = { ... }
|
|
||||||
csqQueryPoll(arg[1])
|
|
||||||
cengQueryPoll(arg[2])
|
|
||||||
if flyMode then
|
|
||||||
log.info("sim.startQuerAll", "flyMode:", flyMode)
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function stopQueryAll()
|
|
||||||
sys.timerStopAll(csqQueryPoll)
|
|
||||||
sys.timerStopAll(cengQueryPoll)
|
|
||||||
end
|
|
||||||
|
|
||||||
local sEngMode
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setEngMode(mode)
|
|
||||||
sEngMode = mode or 1
|
|
||||||
ril.request("AT+EEMOPT="..sEngMode,nil,function(cmd,success)
|
|
||||||
function retrySetEngMode()
|
|
||||||
setEngMode(sEngMode)
|
|
||||||
end
|
|
||||||
if success then
|
|
||||||
sys.timerStop(retrySetEngMode)
|
|
||||||
else
|
|
||||||
sys.timerStart(retrySetEngMode,3000)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
sys.subscribe("SIM_IND", function(para)
|
|
||||||
log.info("SIM.subscribe", simerrsta, para)
|
|
||||||
if simerrsta ~= (para ~= "RDY") then
|
|
||||||
simerrsta = (para ~= "RDY")
|
|
||||||
end
|
|
||||||
|
|
||||||
if para ~= "RDY" then
|
|
||||||
|
|
||||||
state = "UNREGISTER"
|
|
||||||
|
|
||||||
publish("NET_STATE_UNREGISTER")
|
|
||||||
else
|
|
||||||
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
ril.regUrc("+COPS", neturc)
|
|
||||||
ril.regUrc("+CREG", neturc)
|
|
||||||
ril.regUrc("+CGREG", neturc)
|
|
||||||
ril.regUrc("+CEREG", neturc)
|
|
||||||
|
|
||||||
ril.regUrc("+EEMLTESVC", neturc)
|
|
||||||
ril.regUrc("+EEMLTEINTER", neturc)
|
|
||||||
ril.regUrc("+EEMLTEINTRA", neturc)
|
|
||||||
ril.regUrc("+EEMLTEINTERRAT", neturc)
|
|
||||||
ril.regUrc("+EEMGINFOSVC", neturc)
|
|
||||||
ril.regUrc("+EEMGINFONC", neturc)
|
|
||||||
ril.regUrc("+EEMUMTSSVC", neturc)
|
|
||||||
ril.regUrc("^MODE", neturc)
|
|
||||||
|
|
||||||
|
|
||||||
ril.regRsp("+CSQ", rsp)
|
|
||||||
ril.regRsp("+CESQ",rsp)
|
|
||||||
|
|
||||||
ril.regRsp("+CFUN", rsp)
|
|
||||||
|
|
||||||
ril.request("AT+COPS?")
|
|
||||||
ril.request("AT+CREG=2")
|
|
||||||
ril.request("AT+CGREG=2")
|
|
||||||
ril.request("AT+CEREG=2")
|
|
||||||
ril.request("AT+CREG?")
|
|
||||||
ril.request("AT+CGREG?")
|
|
||||||
ril.request("AT+CEREG?")
|
|
||||||
ril.request("AT+CALIBINFO?")
|
|
||||||
ril.request("AT*BAND?")
|
|
||||||
setEngMode(1)
|
|
||||||
|
|
||||||
resetCellInfo()
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
require "pins"
|
|
||||||
require "sim"
|
|
||||||
|
|
||||||
|
|
||||||
local simError
|
|
||||||
|
|
||||||
local flyMode
|
|
||||||
|
|
||||||
local gsmRegistered
|
|
||||||
|
|
||||||
local gprsAttached
|
|
||||||
|
|
||||||
local socketConnected
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local ledState = "NULL"
|
|
||||||
local ON,OFF = 1,2
|
|
||||||
|
|
||||||
local ledBlinkTime =
|
|
||||||
{
|
|
||||||
NULL = {0,0xFFFF},
|
|
||||||
FLYMODE = {0,0xFFFF},
|
|
||||||
SIMERR = {300,5700},
|
|
||||||
IDLE = {300,3700},
|
|
||||||
GSM = {300,1700},
|
|
||||||
GPRS = {300,700},
|
|
||||||
SCK = {100,100},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
local ledSwitch = false
|
|
||||||
|
|
||||||
local LEDPIN = pio.P2_0
|
|
||||||
|
|
||||||
local lteSwitch = false
|
|
||||||
|
|
||||||
local LTEPIN = pio.P2_1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function updateState()
|
|
||||||
|
|
||||||
if ledSwitch then
|
|
||||||
local newState = "IDLE"
|
|
||||||
if flyMode then
|
|
||||||
newState = "FLYMODE"
|
|
||||||
elseif simError then
|
|
||||||
newState = "SIMERR"
|
|
||||||
elseif socketConnected then
|
|
||||||
newState = "SCK"
|
|
||||||
elseif gprsAttached then
|
|
||||||
newState = "GPRS"
|
|
||||||
elseif gsmRegistered then
|
|
||||||
newState = "GSM"
|
|
||||||
end
|
|
||||||
|
|
||||||
if newState~=ledState then
|
|
||||||
ledState = newState
|
|
||||||
sys.publish("NET_LED_UPDATE")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function taskLed(ledPinSetFunc)
|
|
||||||
while true do
|
|
||||||
|
|
||||||
if ledSwitch then
|
|
||||||
local onTime,offTime = ledBlinkTime[ledState][ON],ledBlinkTime[ledState][OFF]
|
|
||||||
if onTime>0 then
|
|
||||||
ledPinSetFunc(1)
|
|
||||||
if not sys.waitUntil("NET_LED_UPDATE", onTime) then
|
|
||||||
if offTime>0 then
|
|
||||||
ledPinSetFunc(0)
|
|
||||||
sys.waitUntil("NET_LED_UPDATE", offTime)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else if offTime>0 then
|
|
||||||
ledPinSetFunc(0)
|
|
||||||
sys.waitUntil("NET_LED_UPDATE", offTime)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
ledPinSetFunc(0)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function taskLte(ledPinSetFunc)
|
|
||||||
while true do
|
|
||||||
local _,arg = sys.waitUntil("LTE_LED_UPDATE")
|
|
||||||
if lteSwitch then
|
|
||||||
ledPinSetFunc(arg and 1 or 0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setup(flag,ledPin,ltePin)
|
|
||||||
|
|
||||||
local oldSwitch = ledSwitch
|
|
||||||
if flag~=ledSwitch then
|
|
||||||
ledSwitch = flag
|
|
||||||
sys.publish("NET_LED_UPDATE")
|
|
||||||
end
|
|
||||||
if flag and not oldSwitch then
|
|
||||||
sys.taskInit(taskLed, pins.setup(ledPin or LEDPIN, 0))
|
|
||||||
end
|
|
||||||
if flag~=lteSwitch then
|
|
||||||
lteSwitch = flag
|
|
||||||
end
|
|
||||||
if flag and ltePin and not oldSwitch then
|
|
||||||
sys.taskInit(taskLte, pins.setup(ltePin, 0))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function updateBlinkTime(state,on,off)
|
|
||||||
if not ledBlinkTime[state] then log.error("netLed.updateBlinkTime") return end
|
|
||||||
local updated
|
|
||||||
if on and ledBlinkTime[state][ON]~=on then
|
|
||||||
ledBlinkTime[state][ON] = on
|
|
||||||
updated = true
|
|
||||||
end
|
|
||||||
if off and ledBlinkTime[state][OFF]~=off then
|
|
||||||
ledBlinkTime[state][OFF] = off
|
|
||||||
updated = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if updated then sys.publish("NET_LED_UPDATE") end
|
|
||||||
end
|
|
||||||
|
|
||||||
sys.subscribe("FLYMODE", function(mode) if flyMode~=mode then flyMode=mode updateState() end end)
|
|
||||||
sys.subscribe("SIM_IND", function(para) if simError~=(para~="RDY") then simError=(para~="RDY") updateState() end end)
|
|
||||||
sys.subscribe("NET_STATE_UNREGISTER", function() if gsmRegistered then gsmRegistered=false updateState() end end)
|
|
||||||
sys.subscribe("NET_STATE_REGISTERED", function() if not gsmRegistered then gsmRegistered=true updateState() end end)
|
|
||||||
sys.subscribe("GPRS_ATTACH", function(attach) if gprsAttached~=attach then gprsAttached=attach updateState() end end)
|
|
||||||
sys.subscribe("SOCKET_ACTIVE", function(active) if socketConnected~=active then socketConnected=active updateState() end end)
|
|
||||||
sys.subscribe("NET_UPD_NET_MODE", function() if lteSwitch then sys.publish("LTE_LED_UPDATE",net.getNetMode()==net.NetMode_LTE) end end)
|
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require"pm"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local oldostime = os.time
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function safeostime(t)
|
|
||||||
return oldostime(t) or 0
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
os.time = safeostime
|
|
||||||
|
|
||||||
|
|
||||||
local oldosdate = os.date
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function safeosdate(s, t)
|
|
||||||
if s == "*t" then
|
|
||||||
return oldosdate(s, t) or {year = 2012,
|
|
||||||
month = 12,
|
|
||||||
day = 11,
|
|
||||||
hour = 10,
|
|
||||||
min = 9,
|
|
||||||
sec = 0}
|
|
||||||
else
|
|
||||||
return oldosdate(s, t)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
os.date = safeosdate
|
|
||||||
|
|
||||||
|
|
||||||
local rawcoresume = coroutine.resume
|
|
||||||
coroutine.resume = function(...)
|
|
||||||
local arg = { ... }
|
|
||||||
function wrapper(co,...)
|
|
||||||
local arg = { ... }
|
|
||||||
if not arg[1] then
|
|
||||||
local traceBack = debug.traceback(co) or "empty"
|
|
||||||
traceBack = (traceBack and traceBack~="") and ((arg[2] or "").."\r\n"..traceBack) or (arg[2] or "")
|
|
||||||
log.error("coroutine.resume",traceBack)
|
|
||||||
if errDump and type(errDump.appendErr)=="function" then
|
|
||||||
errDump.appendErr(traceBack)
|
|
||||||
end
|
|
||||||
if _G.COROUTINE_ERROR_RESTART then rtos.restart() end
|
|
||||||
end
|
|
||||||
return unpack(arg)
|
|
||||||
end
|
|
||||||
return wrapper(arg[1],rawcoresume(...))
|
|
||||||
end
|
|
||||||
|
|
||||||
os.clockms = function() return rtos.tick()/16 end
|
|
||||||
|
|
||||||
|
|
||||||
if json and json.decode then oldjsondecode = json.decode end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function safeJsonDecode(s)
|
|
||||||
local result, info = pcall(oldjsondecode, s)
|
|
||||||
if result then
|
|
||||||
return info, true
|
|
||||||
else
|
|
||||||
return {}, false, info
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if json and json.decode then json.decode = safeJsonDecode end
|
|
||||||
|
|
||||||
local oldUartWrite = uart.write
|
|
||||||
uart.write = function(...)
|
|
||||||
pm.wake("lib.patch.uart.write")
|
|
||||||
local result = oldUartWrite(...)
|
|
||||||
pm.sleep("lib.patch.uart.write")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
if i2c and i2c.write then
|
|
||||||
local oldI2cWrite = i2c.write
|
|
||||||
i2c.write = function(...)
|
|
||||||
pm.wake("lib.patch.i2c.write")
|
|
||||||
local result = oldI2cWrite(...)
|
|
||||||
pm.sleep("lib.patch.i2c.write")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if i2c and i2c.send then
|
|
||||||
local oldI2cSend = i2c.send
|
|
||||||
i2c.send = function(...)
|
|
||||||
pm.wake("lib.patch.i2c.send")
|
|
||||||
local result = oldI2cSend(...)
|
|
||||||
pm.sleep("lib.patch.i2c.send")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if spi and spi.send then
|
|
||||||
oldSpiSend = spi.send
|
|
||||||
spi.send = function(...)
|
|
||||||
pm.wake("lib.patch.spi.send")
|
|
||||||
local result = oldSpiSend(...)
|
|
||||||
pm.sleep("lib.patch.spi.send")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if spi and spi.send_recv then
|
|
||||||
oldSpiSendRecv = spi.send_recv
|
|
||||||
spi.send_recv = function(...)
|
|
||||||
pm.wake("lib.patch.spi.send_recv")
|
|
||||||
local result = oldSpiSendRecv(...)
|
|
||||||
pm.sleep("lib.patch.spi.send_recv")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if disp and disp.sleep then
|
|
||||||
oldDispSleep = disp.sleep
|
|
||||||
disp.sleep = function(...)
|
|
||||||
pm.wake("lib.patch.disp.sleep")
|
|
||||||
oldDispSleep(...)
|
|
||||||
pm.sleep("lib.patch.disp.sleep")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if io and io.mount then
|
|
||||||
oldIoMount = io.mount
|
|
||||||
io.mount = function (...)
|
|
||||||
pm.wake("lib.patch.io.mount")
|
|
||||||
local result = oldIoMount(...)
|
|
||||||
pm.sleep("lib.patch.io.mount")
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local pmdInited
|
|
||||||
|
|
||||||
if pmd and pmd.init then
|
|
||||||
oldPmdInit = pmd.init
|
|
||||||
pmd.init = function (...)
|
|
||||||
if not pmdInited then pmdInited = true end
|
|
||||||
local result = oldPmdInit(...)
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
pmd.libScriptInit = function()
|
|
||||||
if not pmdInited then pmd.init({}) end
|
|
||||||
end
|
|
||||||
@@ -1,150 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "sys"
|
|
||||||
module(..., package.seeall)
|
|
||||||
local interruptCallbacks = {}
|
|
||||||
local dirs = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setup(pin, val, pull)
|
|
||||||
|
|
||||||
pio.pin.close(pin)
|
|
||||||
|
|
||||||
if type(val) == "function" then
|
|
||||||
pio.pin.setdir(pio.INT, pin)
|
|
||||||
if pull then pio.pin.setpull(pull or pio.PULLUP, pin) end
|
|
||||||
|
|
||||||
interruptCallbacks[pin] = val
|
|
||||||
dirs[pin] = false
|
|
||||||
return function()
|
|
||||||
return pio.pin.getval(pin)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if val ~= nil then
|
|
||||||
dirs[pin] = true
|
|
||||||
pio.pin.setdir(val == 1 and pio.OUTPUT1 or pio.OUTPUT, pin)
|
|
||||||
else
|
|
||||||
|
|
||||||
dirs[pin] = false
|
|
||||||
pio.pin.setdir(pio.INPUT, pin)
|
|
||||||
if pull then pio.pin.setpull(pull or pio.PULLUP, pin) end
|
|
||||||
end
|
|
||||||
|
|
||||||
return function(val)
|
|
||||||
val = tonumber(val)
|
|
||||||
if (not val and dirs[pin]) or (val and not dirs[pin]) then
|
|
||||||
pio.pin.close(pin)
|
|
||||||
pio.pin.setdir(val and (val == 1 and pio.OUTPUT1 or pio.OUTPUT) or pio.INPUT, pin)
|
|
||||||
if not val and pull then pio.pin.setpull(pull or pio.PULLUP, pin) end
|
|
||||||
dirs[pin] = val and true or false
|
|
||||||
return val or pio.pin.getval(pin)
|
|
||||||
end
|
|
||||||
if val then
|
|
||||||
pio.pin.setval(val, pin)
|
|
||||||
return val
|
|
||||||
else
|
|
||||||
return pio.pin.getval(pin)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function close(pin)
|
|
||||||
pio.pin.close(pin)
|
|
||||||
end
|
|
||||||
|
|
||||||
rtos.on(rtos.MSG_INT, function(msg)
|
|
||||||
if interruptCallbacks[msg.int_resnum] == nil then
|
|
||||||
log.warn('pins.rtos.on', 'warning:rtos.MSG_INT callback nil', msg.int_resnum)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
interruptCallbacks[msg.int_resnum](msg.int_id)
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
IOMUX_GPIO0 = 0
|
|
||||||
IOMUX_GPIO1 = 1
|
|
||||||
IOMUX_GPIO2 = 2
|
|
||||||
IOMUX_GPIO3 = 3
|
|
||||||
IOMUX_GPIO4 = 4
|
|
||||||
IOMUX_GPIO5 = 5
|
|
||||||
|
|
||||||
IOMUX_GPIO8 = 8
|
|
||||||
IOMUX_GPIO9 = 9
|
|
||||||
IOMUX_GPIO10 = 10
|
|
||||||
|
|
||||||
IOMUX_GPIO11 = 11
|
|
||||||
IOMUX_GPIO12 = 12
|
|
||||||
IOMUX_GPIO13 = 13
|
|
||||||
IOMUX_GPIO14 = 14
|
|
||||||
IOMUX_GPIO15 = 15
|
|
||||||
IOMUX_GPIO18 = 18
|
|
||||||
IOMUX_GPIO19 = 19
|
|
||||||
IOMUX_GPIO20 = 20
|
|
||||||
|
|
||||||
IOMUX_GPIO21 = 21
|
|
||||||
IOMUX_GPIO22 = 22
|
|
||||||
IOMUX_GPIO23 = 23
|
|
||||||
IOMUX_GPIO29 = 29
|
|
||||||
IOMUX_GPIO30 = 30
|
|
||||||
|
|
||||||
IOMUX_GPIO31 = 31
|
|
||||||
|
|
||||||
IOMUX_USART_1 = 57
|
|
||||||
IOMUX_USART_2 = 58
|
|
||||||
IOMUX_USART_3 = 59
|
|
||||||
|
|
||||||
IOMUX_I2C_2 = 67
|
|
||||||
IOMUX_I2C_3 = 68
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local tags = {}
|
|
||||||
|
|
||||||
local flag = true
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function wake(tag)
|
|
||||||
assert(tag and tag ~= nil, "pm.wake tag invalid")
|
|
||||||
|
|
||||||
tags[tag] = 1
|
|
||||||
|
|
||||||
if flag == true then
|
|
||||||
|
|
||||||
flag = false
|
|
||||||
|
|
||||||
pmd.sleep(0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sleep(tag)
|
|
||||||
assert(tag and tag ~= nil, "pm.sleep tag invalid")
|
|
||||||
|
|
||||||
tags[tag] = 0
|
|
||||||
|
|
||||||
for k, v in pairs(tags) do
|
|
||||||
if v > 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
flag = true
|
|
||||||
|
|
||||||
pmd.sleep(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function isSleep(tag)
|
|
||||||
return tag and tags[tag] ~= 1 or flag
|
|
||||||
end
|
|
||||||
@@ -1,554 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "uart"
|
|
||||||
require "rtos"
|
|
||||||
require "sys"
|
|
||||||
require "log"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
local vwrite = uart.write
|
|
||||||
local vread = uart.read
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local transparentmode
|
|
||||||
|
|
||||||
local rcvfunc
|
|
||||||
|
|
||||||
|
|
||||||
local TIMEOUT = 60000*3
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local NORESULT, NUMBERIC, SLINE, MLINE, STRING, SPECIAL = 0, 1, 2, 3, 4, 10
|
|
||||||
|
|
||||||
|
|
||||||
local RILCMD = {
|
|
||||||
["+CSQ"] = 2,
|
|
||||||
["+CESQ"] = 2,
|
|
||||||
["+CGMM"] = 2,
|
|
||||||
["+RFTEMPERATURE"] =2,
|
|
||||||
["+MUID"] = 2,
|
|
||||||
["+CGSN"] = 1,
|
|
||||||
["+WISN"] = 4,
|
|
||||||
["+CIMI"] = 1,
|
|
||||||
["+ICCID"] = 2,
|
|
||||||
["+SIMCROSS"] = 2,
|
|
||||||
["+CGATT"] = 2,
|
|
||||||
["+CCLK"] = 2,
|
|
||||||
['+CNUM'] = 3,
|
|
||||||
|
|
||||||
["+CMGR"] = 3,
|
|
||||||
["+CMGS"] = 2,
|
|
||||||
["+CPBF"] = 3,
|
|
||||||
["+CPBR"] = 3,
|
|
||||||
['+CLCC'] = 3,
|
|
||||||
["+CTFSGETID"] = 2,
|
|
||||||
["+CTFSDECRYPT"] = 2,
|
|
||||||
["+CTFSAUTH"] = 2,
|
|
||||||
["+CGDATA"] = 10,
|
|
||||||
["+CIND"] = 2,
|
|
||||||
|
|
||||||
["+CGACT"] = 3,
|
|
||||||
["+CALIBINFO"] = 4,
|
|
||||||
["*CALINFO"] = 3,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local radioready, delaying = false
|
|
||||||
|
|
||||||
|
|
||||||
local cmdqueue = {
|
|
||||||
"ATE0",
|
|
||||||
"AT+CMEE=0",
|
|
||||||
}
|
|
||||||
|
|
||||||
local currcmd, currarg, currsp, curdelay, cmdhead, cmdtype, rspformt
|
|
||||||
|
|
||||||
local result, interdata, respdata
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function atimeout()
|
|
||||||
|
|
||||||
sys.restart("ril.atimeout_" .. (currcmd or ""))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function defrsp(cmd, success, response, intermediate)
|
|
||||||
log.info("ril.defrsp", cmd, success, response, intermediate)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local rsptable = {}
|
|
||||||
setmetatable(rsptable, {__index = function() return defrsp end})
|
|
||||||
|
|
||||||
|
|
||||||
local formtab = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function regRsp(head, fnc, typ, formt)
|
|
||||||
|
|
||||||
if typ == nil then
|
|
||||||
rsptable[head] = fnc
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
if typ == 0 or typ == 1 or typ == 2 or typ == 3 or typ == 4 or typ == 10 then
|
|
||||||
|
|
||||||
if RILCMD[head] and RILCMD[head] ~= typ then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
RILCMD[head] = typ
|
|
||||||
rsptable[head] = fnc
|
|
||||||
formtab[head] = formt
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local app_rilcb=nil
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setrilcb(cb)
|
|
||||||
app_rilcb =cb
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function rsp()
|
|
||||||
|
|
||||||
sys.timerStopAll(atimeout)
|
|
||||||
|
|
||||||
if currsp then
|
|
||||||
currsp(currcmd, result, respdata, interdata)
|
|
||||||
|
|
||||||
else
|
|
||||||
rsptable[cmdhead](currcmd, result, respdata, interdata)
|
|
||||||
end
|
|
||||||
|
|
||||||
currcmd, currarg, currsp, curdelay, cmdhead, cmdtype, rspformt = nil
|
|
||||||
result, interdata, respdata = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function defurc(data)
|
|
||||||
log.info("ril.defurc", data)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local urctable = {}
|
|
||||||
setmetatable(urctable, {__index = function() return defurc end})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function regUrc(prefix, handler)
|
|
||||||
urctable[prefix] = handler
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function deRegUrc(prefix)
|
|
||||||
urctable[prefix] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local urcfilter
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function urc(data)
|
|
||||||
|
|
||||||
if data == "RDY" then
|
|
||||||
radioready = true
|
|
||||||
else
|
|
||||||
local prefix = string.match(data, "([%+%^%*]*[%u%d& ]+)")
|
|
||||||
|
|
||||||
urcfilter = urctable[prefix](data, prefix)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function procatc(data)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if interdata and cmdtype == MLINE then
|
|
||||||
|
|
||||||
if data ~= "OK\r\n" then
|
|
||||||
|
|
||||||
if string.find(data, "\r\n", -2) then
|
|
||||||
data = string.sub(data, 1, -3)
|
|
||||||
end
|
|
||||||
|
|
||||||
interdata = interdata .. "\r\n" .. data
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if urcfilter then
|
|
||||||
data, urcfilter = urcfilter(data)
|
|
||||||
end
|
|
||||||
|
|
||||||
if string.find(data, "\r\n", -2) then
|
|
||||||
data = string.sub(data, 1, -3)
|
|
||||||
end
|
|
||||||
|
|
||||||
if data == "" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if data:match("^%+EEMLTEINTER") or data:match("^%+EEMLTEINTRA") or data:match("^%+EEMUMTSINTER") or data:match("^%+EEMUMTSINTRA") then
|
|
||||||
|
|
||||||
else
|
|
||||||
log.info("ril.proatc", data)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if currcmd == nil then
|
|
||||||
urc(data)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local isurc = false
|
|
||||||
|
|
||||||
|
|
||||||
if data:match("^%+CMS ERROR:") or data:match("^%+CME ERROR:") then
|
|
||||||
data = "ERROR"
|
|
||||||
end
|
|
||||||
|
|
||||||
if data == "OK" or data == "SHUT OK" then
|
|
||||||
result = true
|
|
||||||
respdata = data
|
|
||||||
|
|
||||||
elseif data == "ERROR" or data == "NO ANSWER" or data == "NO DIALTONE" then
|
|
||||||
result = false
|
|
||||||
respdata = data
|
|
||||||
|
|
||||||
elseif data == "> " then
|
|
||||||
|
|
||||||
if cmdhead == "+CMGS" then
|
|
||||||
log.info("ril.procatc.send", currarg)
|
|
||||||
vwrite(uart.ATC, currarg, "\026")
|
|
||||||
else
|
|
||||||
log.error("error promot cmd:", currcmd)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
|
|
||||||
if cmdtype == NORESULT then
|
|
||||||
isurc = true
|
|
||||||
|
|
||||||
elseif cmdtype == NUMBERIC then
|
|
||||||
local numstr = data:match("(%x+)")
|
|
||||||
if numstr == data then
|
|
||||||
interdata = data
|
|
||||||
else
|
|
||||||
isurc = true
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif cmdtype == STRING then
|
|
||||||
|
|
||||||
if data:match(rspformt or "^.+$") and not data:match("^%+CPIN:") then
|
|
||||||
interdata = data
|
|
||||||
else
|
|
||||||
isurc = true
|
|
||||||
end
|
|
||||||
elseif cmdtype == SLINE or cmdtype == MLINE then
|
|
||||||
if interdata == nil and string.find(data, cmdhead) == 1 then
|
|
||||||
interdata = data
|
|
||||||
else
|
|
||||||
isurc = true
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif cmdhead == "+CGDATA" then
|
|
||||||
if string.find(data, "CONNECT") == 1 then
|
|
||||||
result = true
|
|
||||||
respdata = data
|
|
||||||
else
|
|
||||||
isurc = true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
isurc = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if isurc then
|
|
||||||
urc(data)
|
|
||||||
|
|
||||||
elseif result ~= nil then
|
|
||||||
rsp()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local readat = false
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function getcmd(item)
|
|
||||||
local cmd, arg, rsp, delay
|
|
||||||
|
|
||||||
if type(item) == "string" then
|
|
||||||
|
|
||||||
cmd = item
|
|
||||||
|
|
||||||
elseif type(item) == "table" then
|
|
||||||
|
|
||||||
cmd = item.cmd
|
|
||||||
|
|
||||||
arg = item.arg
|
|
||||||
|
|
||||||
rsp = item.rsp
|
|
||||||
|
|
||||||
delay = item.delay
|
|
||||||
else
|
|
||||||
log.info("ril.getcmd", "getpack unknown item")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local head = string.match(cmd, "AT([%+%*%^]*%u+)")
|
|
||||||
|
|
||||||
if head == nil then
|
|
||||||
log.error("ril.getcmd", "request error cmd:", cmd)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if head == "+CMGS" or head == "+CIPSEND" then
|
|
||||||
if arg == nil or arg == "" then
|
|
||||||
log.error("ril.getcmd", "request error no arg", head)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
currcmd = cmd
|
|
||||||
currarg = arg
|
|
||||||
currsp = rsp
|
|
||||||
curdelay = delay
|
|
||||||
cmdhead = head
|
|
||||||
cmdtype = RILCMD[head] or NORESULT
|
|
||||||
rspformt = formtab[head]
|
|
||||||
|
|
||||||
return currcmd
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function sendat()
|
|
||||||
|
|
||||||
if not radioready or readat or currcmd ~= nil or delaying then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local item
|
|
||||||
|
|
||||||
while true do
|
|
||||||
|
|
||||||
if #cmdqueue == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
item = table.remove(cmdqueue, 1)
|
|
||||||
|
|
||||||
getcmd(item)
|
|
||||||
|
|
||||||
if curdelay then
|
|
||||||
|
|
||||||
sys.timerStart(delayfunc, curdelay)
|
|
||||||
|
|
||||||
currcmd, currarg, currsp, curdelay, cmdhead, cmdtype, rspformt = nil
|
|
||||||
item.delay = nil
|
|
||||||
|
|
||||||
delaying = true
|
|
||||||
|
|
||||||
table.insert(cmdqueue, 1, item)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if currcmd ~= nil then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
sys.timerStart(atimeout, TIMEOUT)
|
|
||||||
|
|
||||||
log.info("ril.sendat", currcmd)
|
|
||||||
|
|
||||||
if currcmd:match("^AT%+POC=") then
|
|
||||||
vwrite(uart.ATC, currcmd .. "\r\n")
|
|
||||||
else
|
|
||||||
vwrite(uart.ATC, currcmd .. "\r")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function delayfunc()
|
|
||||||
|
|
||||||
delaying = nil
|
|
||||||
|
|
||||||
sendat()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function atcreader()
|
|
||||||
local s
|
|
||||||
|
|
||||||
if not transparentmode then readat = true end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
|
|
||||||
s = vread(uart.ATC, "*l", 0)
|
|
||||||
if string.len(s) ~= 0 then
|
|
||||||
if transparentmode then
|
|
||||||
|
|
||||||
rcvfunc(s)
|
|
||||||
else
|
|
||||||
|
|
||||||
procatc(s)
|
|
||||||
|
|
||||||
if app_rilcb ~=nil then app_rilcb(s) end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not transparentmode then
|
|
||||||
readat = false
|
|
||||||
|
|
||||||
sendat()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function request(cmd, arg, onrsp, delay)
|
|
||||||
if transparentmode then return end
|
|
||||||
|
|
||||||
if arg or onrsp or delay or formt then
|
|
||||||
table.insert(cmdqueue, {cmd = cmd, arg = arg, rsp = onrsp, delay = delay})
|
|
||||||
else
|
|
||||||
table.insert(cmdqueue, cmd)
|
|
||||||
end
|
|
||||||
|
|
||||||
sendat()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setransparentmode(fnc)
|
|
||||||
transparentmode, rcvfunc = true, fnc
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sendtransparentdata(data)
|
|
||||||
if not transparentmode then return end
|
|
||||||
vwrite(uart.ATC, data)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
uart.on(uart.ATC, "receive", atcreader)
|
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "ril"
|
|
||||||
require "sys"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
local req = ril.request
|
|
||||||
|
|
||||||
local imsi, iccid, status
|
|
||||||
local sNumber,bQueryNumber = ""
|
|
||||||
local simCross,setSimCrossCbFnc
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getIccid()
|
|
||||||
return iccid
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getImsi()
|
|
||||||
return imsi
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getMcc()
|
|
||||||
return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 1, 3) or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getMnc()
|
|
||||||
return (imsi ~= nil and imsi ~= "") and string.sub(imsi, 4, 5) or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getStatus()
|
|
||||||
return status
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setQueryNumber(flag)
|
|
||||||
bQueryNumber = flag
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getNumber()
|
|
||||||
return sNumber or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function rsp(cmd, success, response, intermediate)
|
|
||||||
if cmd == "AT+ICCID" then
|
|
||||||
if intermediate then
|
|
||||||
iccid = string.match(intermediate, "%+ICCID: (.+)")
|
|
||||||
end
|
|
||||||
elseif cmd == "AT+SIMCROSS?" then
|
|
||||||
if success then
|
|
||||||
simCross = tonumber(intermediate:match("%+SIMCROSS:%s*(%d)"))
|
|
||||||
end
|
|
||||||
if setSimCrossCbFnc then setSimCrossCbFnc(success) end
|
|
||||||
elseif cmd:match("AT%+SIMCROSS=") then
|
|
||||||
if success then
|
|
||||||
req("AT+SIMCROSS?")
|
|
||||||
else
|
|
||||||
if setSimCrossCbFnc then setSimCrossCbFnc(false) end
|
|
||||||
end
|
|
||||||
elseif cmd == "AT+CIMI" then
|
|
||||||
imsi = intermediate
|
|
||||||
|
|
||||||
sys.publish("IMSI_READY")
|
|
||||||
elseif cmd == "AT+CNUM" then
|
|
||||||
if success then
|
|
||||||
if intermediate then sNumber = intermediate:match("%+CNUM:%s*\".-\",\"[%+]*(%d+)\",") end
|
|
||||||
else
|
|
||||||
sys.timerStart(ril.request,5000,"AT+CNUM")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function urc(data, prefix)
|
|
||||||
|
|
||||||
if prefix == "+CPIN" then
|
|
||||||
status = false
|
|
||||||
|
|
||||||
if data == "+CPIN: READY" then
|
|
||||||
status = true
|
|
||||||
ril.request("AT+ICCID")
|
|
||||||
ril.request("AT+CIMI")
|
|
||||||
if bQueryNumber then ril.request("AT+CNUM") end
|
|
||||||
sys.publish("SIM_IND", "RDY")
|
|
||||||
|
|
||||||
elseif data == "+CPIN: NOT INSERTED" then
|
|
||||||
sys.publish("SIM_IND", "NIST")
|
|
||||||
else
|
|
||||||
|
|
||||||
if data == "+CPIN: SIM PIN" then
|
|
||||||
sys.publish("SIM_IND","SIM_PIN")
|
|
||||||
end
|
|
||||||
sys.publish("SIM_IND", "NORDY")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function set2gSim()
|
|
||||||
ril.request("AT+MEDCR=0,8,1")
|
|
||||||
ril.request("AT+MEDCR=0,17,240")
|
|
||||||
ril.request("AT+MEDCR=0,19,1")
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setId(id,cbFnc)
|
|
||||||
if id ~= simCross then
|
|
||||||
setSimCrossCbFnc = cbFnc
|
|
||||||
ril.request("AT+SIMCROSS="..id)
|
|
||||||
else
|
|
||||||
if cbFnc then cbFnc(true) end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function getId()
|
|
||||||
return simCross
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
ril.regRsp("+ICCID", rsp)
|
|
||||||
|
|
||||||
ril.regRsp("+CIMI", rsp)
|
|
||||||
ril.regRsp("+CNUM", rsp)
|
|
||||||
ril.regRsp("+SIMCROSS", rsp)
|
|
||||||
|
|
||||||
ril.regUrc("+CPIN", urc)
|
|
||||||
ril.request("AT+SIMCROSS?")
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "socket4G"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
socket.isReady = link.isReady
|
|
||||||
local tSocketModule = nil
|
|
||||||
local function init()
|
|
||||||
tSocketModule = tSocketModule or {
|
|
||||||
[link.CELLULAR] = socket4G,
|
|
||||||
[link.CH395] = socketCh395,
|
|
||||||
[link.W5500] = socketW5500,
|
|
||||||
[link.ESP8266] = socketESP8266
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function tcp(ssl, cert, tCoreExtPara, ipv6)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].tcp(ssl, cert, tCoreExtPara, ipv6)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function udp(ipv6)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].udp(ipv6)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setTcpResendPara(retryCnt, retryMaxTimeout)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].setTcpResendPara(retryCnt, retryMaxTimeout)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setDnsParsePara(retryCnt, retryTimeoutMulti)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].setDnsParsePara(retryCnt, retryTimeoutMulti)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function printStatus()
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].printStatus()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setLowPower(tm)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].setLowPower(tm)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setIpStatis(interval)
|
|
||||||
init()
|
|
||||||
return tSocketModule[link.getNetwork()].setIpStatis(interval or 0)
|
|
||||||
end
|
|
||||||
@@ -1,680 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "link"
|
|
||||||
require "utils"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
local sockets = {}
|
|
||||||
|
|
||||||
local SENDSIZE = 11200
|
|
||||||
|
|
||||||
local INDEX_MAX = 256
|
|
||||||
|
|
||||||
local socketsConnected = 0
|
|
||||||
|
|
||||||
|
|
||||||
local ipStatisInterval, ipDataFlow = 0,0
|
|
||||||
|
|
||||||
local function ipDataFlowAdd(flow)
|
|
||||||
if ipStatisInterval~=0 then
|
|
||||||
ipDataFlow = ipDataFlow+flow
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function errorInd(error)
|
|
||||||
local coSuspended = {}
|
|
||||||
|
|
||||||
for _, c in pairs(sockets) do
|
|
||||||
c.error = error
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if c.co and coroutine.status(c.co) == "suspended" then
|
|
||||||
|
|
||||||
table.insert(coSuspended, c.co)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for k, v in pairs(coSuspended) do
|
|
||||||
if v and coroutine.status(v) == "suspended" then
|
|
||||||
coroutine.resume(v, false, error)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
sys.subscribe("IP_ERROR_IND", function()errorInd('IP_ERROR_IND') end)
|
|
||||||
|
|
||||||
|
|
||||||
local mt = {}
|
|
||||||
mt.__index = mt
|
|
||||||
local function socket(protocol, cert, tCoreExtPara,ipv6)
|
|
||||||
local ssl = protocol:match("SSL")
|
|
||||||
local co = coroutine.running()
|
|
||||||
if not co then
|
|
||||||
log.warn("socket.socket: socket must be called in coroutine")
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local o = {
|
|
||||||
id = nil,
|
|
||||||
protocol = protocol,
|
|
||||||
tCoreExtPara = tCoreExtPara,
|
|
||||||
ssl = ssl,
|
|
||||||
cert = cert,
|
|
||||||
co = co,
|
|
||||||
input = {},
|
|
||||||
output = {},
|
|
||||||
wait = "",
|
|
||||||
connected = false,
|
|
||||||
iSubscribe = false,
|
|
||||||
subMessage = nil,
|
|
||||||
isBlock = false,
|
|
||||||
msg = nil,
|
|
||||||
rcvProcFnc = nil,
|
|
||||||
ipv6 = ipv6,
|
|
||||||
}
|
|
||||||
return setmetatable(o, mt)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function tcp(ssl, cert, tCoreExtPara, ipv6)
|
|
||||||
return socket("TCP" .. (ssl == true and "SSL" or ""), (ssl == true) and cert or nil, tCoreExtPara, ipv6)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function udp(ipv6)
|
|
||||||
return socket("UDP", nil, nil, ipv6)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:connect(address, port, timeout)
|
|
||||||
assert(self.co == coroutine.running(), "socket:connect: coroutine mismatch")
|
|
||||||
|
|
||||||
if not link.isReady() then
|
|
||||||
log.info("socket.connect: ip not ready")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.protocol=="TCP" then
|
|
||||||
ipDataFlowAdd(120)
|
|
||||||
elseif self.protocol=="TCPSSL" then
|
|
||||||
ipDataFlowAdd(800)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.address = address
|
|
||||||
self.port = port
|
|
||||||
local tCoreExtPara = self.tCoreExtPara or {}
|
|
||||||
|
|
||||||
local rcvBufferSize = tCoreExtPara.rcvBufferSize or 0
|
|
||||||
local socket_connect_fnc = nil
|
|
||||||
if self.ipv6 == true then
|
|
||||||
socket_connect_fnc = socketcore.ipv6_conn
|
|
||||||
else
|
|
||||||
socket_connect_fnc = (type(socketcore.sock_conn_ext)=="function") and socketcore.sock_conn_ext or socketcore.sock_conn
|
|
||||||
end
|
|
||||||
if self.protocol == 'TCP' then
|
|
||||||
self.id = socket_connect_fnc(0, address, port, rcvBufferSize)
|
|
||||||
elseif self.protocol == 'TCPSSL' then
|
|
||||||
local cert = {hostName = address}
|
|
||||||
local insist = 1
|
|
||||||
local hostNameFlag = 0
|
|
||||||
if self.cert then
|
|
||||||
if self.cert.caCert then
|
|
||||||
if self.cert.caCert:sub(1, 1) ~= "/" then self.cert.caCert = "/lua/" .. self.cert.caCert end
|
|
||||||
cert.caCert = io.readFile(self.cert.caCert)
|
|
||||||
end
|
|
||||||
if self.cert.clientCert then
|
|
||||||
if self.cert.clientCert:sub(1, 1) ~= "/" then self.cert.clientCert = "/lua/" .. self.cert.clientCert end
|
|
||||||
cert.clientCert = io.readFile(self.cert.clientCert)
|
|
||||||
end
|
|
||||||
if self.cert.clientKey then
|
|
||||||
if self.cert.clientKey:sub(1, 1) ~= "/" then self.cert.clientKey = "/lua/" .. self.cert.clientKey end
|
|
||||||
cert.clientKey = io.readFile(self.cert.clientKey)
|
|
||||||
end
|
|
||||||
insist = self.cert.insist == 0 and 0 or 1
|
|
||||||
hostNameFlag = self.cert.hostNameFlag == 1 and 1 or 0
|
|
||||||
end
|
|
||||||
self.id = socket_connect_fnc(2, address, port, cert, rcvBufferSize, insist, nil, hostNameFlag)
|
|
||||||
else
|
|
||||||
self.id = socket_connect_fnc(1, address, port, rcvBufferSize)
|
|
||||||
end
|
|
||||||
if self.ipv6 ~= true and type(socketcore.sock_conn_ext)=="function" then
|
|
||||||
if not self.id or self.id<0 then
|
|
||||||
if self.id==-2 then
|
|
||||||
require "http"
|
|
||||||
|
|
||||||
http.request("GET", "119.29.29.29/d?dn=" .. address, nil, nil, nil, 40000,
|
|
||||||
function(result, statusCode, head, body)
|
|
||||||
log.info("socket.httpDnsCb", result, statusCode, head, body)
|
|
||||||
sys.publish("SOCKET_HTTPDNS_RESULT_"..address.."_"..port, result, statusCode, head, body)
|
|
||||||
end)
|
|
||||||
local _, result, statusCode, head, body = sys.waitUntil("SOCKET_HTTPDNS_RESULT_"..address.."_"..port)
|
|
||||||
|
|
||||||
|
|
||||||
if result and statusCode == "200" and body and body:match("^[%d%.]+") then
|
|
||||||
return self:connect(body:match("^([%d%.]+)"),port,timeout)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.id = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not self.id then
|
|
||||||
log.info("socket:connect: core sock conn error", self.protocol, address, port, self.cert)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
log.info("socket:connect-coreid,prot,addr,port,cert,timeout", self.id, self.protocol, address, port, self.cert, timeout or 120)
|
|
||||||
sockets[self.id] = self
|
|
||||||
self.wait = "SOCKET_CONNECT"
|
|
||||||
self.timerId = sys.timerStart(coroutine.resume, (timeout or 120) * 1000, self.co, false, "TIMEOUT")
|
|
||||||
local result, reason = coroutine.yield()
|
|
||||||
if self.timerId and reason ~= "TIMEOUT" then sys.timerStop(self.timerId) end
|
|
||||||
if not result then
|
|
||||||
log.info("socket:connect: connect fail", reason)
|
|
||||||
if reason == "RESPONSE" then
|
|
||||||
sockets[self.id] = nil
|
|
||||||
self.id = nil
|
|
||||||
end
|
|
||||||
sys.publish("LIB_SOCKET_CONNECT_FAIL_IND", self.ssl, self.protocol, address, port)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
log.info("socket:connect: connect ok")
|
|
||||||
|
|
||||||
if not self.connected then
|
|
||||||
self.connected = true
|
|
||||||
socketsConnected = socketsConnected+1
|
|
||||||
sys.publish("SOCKET_ACTIVE", socketsConnected>0)
|
|
||||||
end
|
|
||||||
|
|
||||||
return true, self.id
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:asyncSelect(keepAlive, pingreq)
|
|
||||||
assert(self.co == coroutine.running(), "socket:asyncSelect: coroutine mismatch")
|
|
||||||
if self.error then
|
|
||||||
log.warn('socket.client:asyncSelect', 'error', self.error)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
self.wait = "SOCKET_SEND"
|
|
||||||
local dataLen = 0
|
|
||||||
|
|
||||||
while #self.output ~= 0 do
|
|
||||||
local data = table.concat(self.output)
|
|
||||||
dataLen = string.len(data)
|
|
||||||
self.output = {}
|
|
||||||
local sendSize = self.protocol == "UDP" and 1472 or SENDSIZE
|
|
||||||
for i = 1, dataLen, sendSize do
|
|
||||||
|
|
||||||
if self.ipv6 == true then
|
|
||||||
socketcore.ipv6_send(self.id, data:sub(i, i + sendSize - 1))
|
|
||||||
else
|
|
||||||
socketcore.sock_send(self.id, data:sub(i, i + sendSize - 1))
|
|
||||||
end
|
|
||||||
ipDataFlowAdd((data:sub(i, i + sendSize - 1)):len()+(self.protocol == "UDP" and 40 or 80))
|
|
||||||
if self.timeout then
|
|
||||||
self.timerId = sys.timerStart(coroutine.resume, self.timeout * 1000, self.co, false, "TIMEOUT")
|
|
||||||
end
|
|
||||||
|
|
||||||
local result, reason = coroutine.yield()
|
|
||||||
if self.timerId and reason ~= "TIMEOUT" then sys.timerStop(self.timerId) end
|
|
||||||
sys.publish("SOCKET_ASYNC_SEND", result)
|
|
||||||
if not result then
|
|
||||||
sys.publish("LIB_SOCKET_SEND_FAIL_IND", self.ssl, self.protocol, self.address, self.port)
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.wait = "SOCKET_WAIT"
|
|
||||||
|
|
||||||
if dataLen>0 then sys.publish("SOCKET_SEND", self.id, true) end
|
|
||||||
if keepAlive and keepAlive ~= 0 then
|
|
||||||
if type(pingreq) == "function" then
|
|
||||||
sys.timerStart(pingreq, keepAlive * 1000)
|
|
||||||
else
|
|
||||||
sys.timerStart(self.asyncSend, keepAlive * 1000, self, pingreq or "\0")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return coroutine.yield()
|
|
||||||
end
|
|
||||||
|
|
||||||
function mt:getAsyncSend()
|
|
||||||
if self.error then return 0 end
|
|
||||||
return #(self.output)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:asyncSend(data, timeout)
|
|
||||||
if self.error then
|
|
||||||
log.warn('socket.client:asyncSend', 'error', self.error)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
self.timeout = timeout
|
|
||||||
table.insert(self.output, data or "")
|
|
||||||
|
|
||||||
if self.wait == "SOCKET_WAIT" then coroutine.resume(self.co, true) end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:asyncRecv()
|
|
||||||
if #self.input == 0 then return "" end
|
|
||||||
if self.protocol == "UDP" then
|
|
||||||
return table.remove(self.input)
|
|
||||||
else
|
|
||||||
local s = table.concat(self.input)
|
|
||||||
self.input = {}
|
|
||||||
if self.ipv6 == true then
|
|
||||||
if self.isBlock then table.insert(self.input, socketcore.ipv6_recv(self.msg.socket_index, self.msg.recv_len)) end
|
|
||||||
else
|
|
||||||
if self.isBlock then table.insert(self.input, socketcore.sock_recv(self.msg.socket_index, self.msg.recv_len)) end
|
|
||||||
end
|
|
||||||
return s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:send(data, timeout)
|
|
||||||
assert(self.co == coroutine.running(), "socket:send: coroutine mismatch")
|
|
||||||
if self.error then
|
|
||||||
log.warn('socket.client:send', 'error', self.error)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
log.debug("socket.send", "total " .. string.len(data or "") .. " bytes", "first 30 bytes", (data or ""):sub(1, 30))
|
|
||||||
local sendSize = self.protocol == "UDP" and 1472 or SENDSIZE
|
|
||||||
for i = 1, string.len(data or ""), sendSize do
|
|
||||||
|
|
||||||
self.wait = "SOCKET_SEND"
|
|
||||||
if self.ipv6 == true then
|
|
||||||
socketcore.ipv6_send(self.id, data:sub(i, i + sendSize - 1))
|
|
||||||
else
|
|
||||||
socketcore.sock_send(self.id, data:sub(i, i + sendSize - 1))
|
|
||||||
end
|
|
||||||
ipDataFlowAdd((data:sub(i, i + sendSize - 1)):len()+(self.protocol == "UDP" and 40 or 80))
|
|
||||||
self.timerId = sys.timerStart(coroutine.resume, (timeout or 120) * 1000, self.co, false, "TIMEOUT")
|
|
||||||
local result, reason = coroutine.yield()
|
|
||||||
if self.timerId and reason ~= "TIMEOUT" then sys.timerStop(self.timerId) end
|
|
||||||
if not result then
|
|
||||||
log.info("socket:send", "send fail", reason)
|
|
||||||
sys.publish("LIB_SOCKET_SEND_FAIL_IND", self.ssl, self.protocol, self.address, self.port)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:recv(timeout, msg, msgNoResume)
|
|
||||||
assert(self.co == coroutine.running(), "socket:recv: coroutine mismatch")
|
|
||||||
if self.error then
|
|
||||||
log.warn('socket.client:recv', 'error', self.error)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
self.msgNoResume = msgNoResume
|
|
||||||
if msg and not self.iSubscribe then
|
|
||||||
self.iSubscribe = msg
|
|
||||||
self.subMessage = function(data)
|
|
||||||
|
|
||||||
if self.wait == "+RECEIVE" and not self.msgNoResume then
|
|
||||||
if data then table.insert(self.output, data) end
|
|
||||||
coroutine.resume(self.co, 0xAA)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sys.subscribe(msg, self.subMessage)
|
|
||||||
end
|
|
||||||
if msg and #self.output > 0 then sys.publish(msg, false) end
|
|
||||||
if #self.input == 0 then
|
|
||||||
self.wait = "+RECEIVE"
|
|
||||||
if timeout and timeout > 0 then
|
|
||||||
local r, s = sys.wait(timeout)
|
|
||||||
if r == nil then
|
|
||||||
return false, "timeout"
|
|
||||||
elseif r == 0xAA then
|
|
||||||
local dat = table.concat(self.output)
|
|
||||||
self.output = {}
|
|
||||||
return false, msg, dat
|
|
||||||
else
|
|
||||||
return r, s
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local r, s = coroutine.yield()
|
|
||||||
if r == 0xAA then
|
|
||||||
local dat = table.concat(self.output)
|
|
||||||
self.output = {}
|
|
||||||
return false, msg, dat
|
|
||||||
else
|
|
||||||
return r, s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.protocol == "UDP" then
|
|
||||||
local s = table.remove(self.input)
|
|
||||||
return true, s
|
|
||||||
else
|
|
||||||
log.warn("-------------------使用缓冲区---------------")
|
|
||||||
local s = table.concat(self.input)
|
|
||||||
self.input = {}
|
|
||||||
if self.ipv6 == true then
|
|
||||||
if self.isBlock then table.insert(self.input, socketcore.ipv6_recv(self.msg.socket_index, self.msg.recv_len)) end
|
|
||||||
else
|
|
||||||
if self.isBlock then table.insert(self.input, socketcore.sock_recv(self.msg.socket_index, self.msg.recv_len)) end
|
|
||||||
end
|
|
||||||
return true, s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:close()
|
|
||||||
assert(self.co == coroutine.running(), "socket:close: coroutine mismatch")
|
|
||||||
if self.iSubscribe then
|
|
||||||
sys.unsubscribe(self.iSubscribe, self.subMessage)
|
|
||||||
self.iSubscribe = false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
log.info("socket:sock_close", self.id)
|
|
||||||
local result, reason
|
|
||||||
|
|
||||||
if self.id then
|
|
||||||
if self.ipv6 == true then
|
|
||||||
socketcore.ipv6_close(self.id)
|
|
||||||
else
|
|
||||||
socketcore.sock_close(self.id)
|
|
||||||
end
|
|
||||||
if self.protocol~="UDP" then ipDataFlowAdd(120) end
|
|
||||||
self.wait = "SOCKET_CLOSE"
|
|
||||||
while true do
|
|
||||||
result, reason = coroutine.yield()
|
|
||||||
if reason == "RESPONSE" then break end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if self.connected then
|
|
||||||
self.connected = false
|
|
||||||
if socketsConnected>0 then
|
|
||||||
socketsConnected = socketsConnected-1
|
|
||||||
end
|
|
||||||
sys.publish("SOCKET_ACTIVE", socketsConnected>0)
|
|
||||||
end
|
|
||||||
if self.input then
|
|
||||||
self.input = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.id ~= nil then
|
|
||||||
sockets[self.id] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function mt:setRcvProc(rcvCbFnc)
|
|
||||||
assert(self.co == coroutine.running(), "socket:setRcvProc: coroutine mismatch")
|
|
||||||
self.rcvProcFnc = rcvCbFnc
|
|
||||||
end
|
|
||||||
|
|
||||||
local function on_response(msg)
|
|
||||||
local t = {
|
|
||||||
[rtos.MSG_SOCK_CLOSE_CNF] = 'SOCKET_CLOSE',
|
|
||||||
[rtos.MSG_SOCK_SEND_CNF] = 'SOCKET_SEND',
|
|
||||||
[rtos.MSG_SOCK_CONN_CNF] = 'SOCKET_CONNECT',
|
|
||||||
}
|
|
||||||
if not sockets[msg.socket_index] then
|
|
||||||
log.warn('response on nil socket', msg.socket_index, t[msg.id], msg.result)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if sockets[msg.socket_index].wait ~= t[msg.id] then
|
|
||||||
log.warn('response on invalid wait', sockets[msg.socket_index].id, sockets[msg.socket_index].wait, t[msg.id], msg.socket_index)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
log.info("socket:on_response:", msg.socket_index, t[msg.id], msg.result)
|
|
||||||
if type(socketcore.sock_destroy) == "function" then
|
|
||||||
if (msg.id == rtos.MSG_SOCK_CONN_CNF and msg.result ~= 0) or msg.id == rtos.MSG_SOCK_CLOSE_CNF then
|
|
||||||
socketcore.sock_destroy(msg.socket_index)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
coroutine.resume(sockets[msg.socket_index].co, msg.result == 0, "RESPONSE")
|
|
||||||
end
|
|
||||||
|
|
||||||
rtos.on(rtos.MSG_SOCK_CLOSE_CNF, on_response)
|
|
||||||
rtos.on(rtos.MSG_SOCK_CONN_CNF, on_response)
|
|
||||||
rtos.on(rtos.MSG_SOCK_SEND_CNF, on_response)
|
|
||||||
rtos.on(rtos.MSG_SOCK_CLOSE_IND, function(msg)
|
|
||||||
log.info("socket.rtos.MSG_SOCK_CLOSE_IND")
|
|
||||||
if not sockets[msg.socket_index] then
|
|
||||||
log.warn('close ind on nil socket', msg.socket_index, msg.id)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if sockets[msg.socket_index].connected then
|
|
||||||
sockets[msg.socket_index].connected = false
|
|
||||||
if socketsConnected>0 then
|
|
||||||
socketsConnected = socketsConnected-1
|
|
||||||
end
|
|
||||||
sys.publish("SOCKET_ACTIVE", socketsConnected>0)
|
|
||||||
end
|
|
||||||
sockets[msg.socket_index].error = 'CLOSED'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sys.publish("LIB_SOCKET_CLOSE_IND", sockets[msg.socket_index].ssl, sockets[msg.socket_index].protocol, sockets[msg.socket_index].address, sockets[msg.socket_index].port)
|
|
||||||
coroutine.resume(sockets[msg.socket_index].co, false, "CLOSED")
|
|
||||||
end)
|
|
||||||
rtos.on(rtos.MSG_SOCK_RECV_IND, function(msg)
|
|
||||||
if not sockets[msg.socket_index] then
|
|
||||||
log.warn('close ind on nil socket', msg.socket_index, msg.id)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
log.debug("socket.recv", msg.recv_len, sockets[msg.socket_index].rcvProcFnc)
|
|
||||||
ipDataFlowAdd(msg.recv_len+(sockets[msg.socket_index].protocol=="UDP" and 40 or 80))
|
|
||||||
if sockets[msg.socket_index].rcvProcFnc then
|
|
||||||
if sockets[msg.socket_index].ipv6 == true then
|
|
||||||
sockets[msg.socket_index].rcvProcFnc(socketcore.ipv6_recv, msg.socket_index, msg.recv_len)
|
|
||||||
else
|
|
||||||
sockets[msg.socket_index].rcvProcFnc(socketcore.sock_recv, msg.socket_index, msg.recv_len)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if sockets[msg.socket_index].wait == "+RECEIVE" then
|
|
||||||
if sockets[msg.socket_index].ipv6 == true then
|
|
||||||
coroutine.resume(sockets[msg.socket_index].co, true, socketcore.ipv6_recv(msg.socket_index, msg.recv_len))
|
|
||||||
else
|
|
||||||
coroutine.resume(sockets[msg.socket_index].co, true, socketcore.sock_recv(msg.socket_index, msg.recv_len))
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if #sockets[msg.socket_index].input > INDEX_MAX then
|
|
||||||
log.error("socket recv", "out of stack", "block")
|
|
||||||
|
|
||||||
sockets[msg.socket_index].isBlock = true
|
|
||||||
sockets[msg.socket_index].msg = msg
|
|
||||||
else
|
|
||||||
sockets[msg.socket_index].isBlock = false
|
|
||||||
if sockets[msg.socket_index].ipv6 == true then
|
|
||||||
table.insert(sockets[msg.socket_index].input, socketcore.ipv6_recv(msg.socket_index, msg.recv_len))
|
|
||||||
else
|
|
||||||
table.insert(sockets[msg.socket_index].input, socketcore.sock_recv(msg.socket_index, msg.recv_len))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sys.publish("SOCKET_RECV", msg.socket_index)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setTcpResendPara(retryCnt, retryMaxTimeout)
|
|
||||||
ril.request("AT+TCPUSERPARAM=6," .. (retryCnt or 4) .. ",7200," .. (retryMaxTimeout or 16))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setDnsParsePara(retryCnt, retryTimeoutMulti)
|
|
||||||
ril.request("AT*DNSTMOUT="..(retryCnt or 4)..","..(retryTimeoutMulti or 4))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function printStatus()
|
|
||||||
for _, client in pairs(sockets) do
|
|
||||||
for k, v in pairs(client) do
|
|
||||||
log.info('socket.printStatus', 'client', client.id, k, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function setLowPower(tm)
|
|
||||||
ril.request("AT*RTIME="..tm)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function ipStatisTimerCb()
|
|
||||||
if ipDataFlow~=0 then
|
|
||||||
sys.publish("LIB_IP_STATIS_RPT",ipDataFlow)
|
|
||||||
ipDataFlow = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function setIpStatis(interval)
|
|
||||||
if ipStatisInterval~=interval then
|
|
||||||
ipStatisInterval = interval
|
|
||||||
ipStatisTimerCb()
|
|
||||||
if interval==0 then
|
|
||||||
sys.timerStop(ipStatisTimerCb)
|
|
||||||
else
|
|
||||||
sys.timerLoopStart(ipStatisTimerCb,interval*1000)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,526 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
require "utils"
|
|
||||||
require "log"
|
|
||||||
require "patch"
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
SCRIPT_LIB_VER = "2.4.5"
|
|
||||||
|
|
||||||
|
|
||||||
local TASK_TIMER_ID_MAX = 0x1FFFFFFF
|
|
||||||
|
|
||||||
local MSG_TIMER_ID_MAX = 0x7FFFFFFF
|
|
||||||
|
|
||||||
|
|
||||||
local taskTimerId = 0
|
|
||||||
|
|
||||||
local msgId = TASK_TIMER_ID_MAX
|
|
||||||
|
|
||||||
local timerPool = {}
|
|
||||||
local taskTimerPool = {}
|
|
||||||
|
|
||||||
local para = {}
|
|
||||||
|
|
||||||
local loop = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function powerOn()
|
|
||||||
rtos.poweron(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function restart(r)
|
|
||||||
assert(r and r ~= "", "sys.restart cause null")
|
|
||||||
if errDump and errDump.appendErr and type(errDump.appendErr) == "function" then errDump.appendErr("restart[" .. r .. "];") end
|
|
||||||
log.warn("sys.restart", r)
|
|
||||||
rtos.restart()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function wait(ms)
|
|
||||||
|
|
||||||
assert(ms > 0, "The wait time cannot be negative!")
|
|
||||||
|
|
||||||
if ms < 5 then ms = 5 end
|
|
||||||
|
|
||||||
if taskTimerId >= TASK_TIMER_ID_MAX then taskTimerId = 0 end
|
|
||||||
taskTimerId = taskTimerId + 1
|
|
||||||
local timerid = taskTimerId
|
|
||||||
taskTimerPool[coroutine.running()] = timerid
|
|
||||||
timerPool[timerid] = coroutine.running()
|
|
||||||
|
|
||||||
if 1 ~= rtos.timer_start(timerid, ms) then log.debug("rtos.timer_start error") return end
|
|
||||||
|
|
||||||
local message = {coroutine.yield()}
|
|
||||||
if #message ~= 0 then
|
|
||||||
rtos.timer_stop(timerid)
|
|
||||||
taskTimerPool[coroutine.running()] = nil
|
|
||||||
timerPool[timerid] = nil
|
|
||||||
return unpack(message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function waitUntil(id, ms)
|
|
||||||
subscribe(id, coroutine.running())
|
|
||||||
local message = ms and {wait(ms)} or {coroutine.yield()}
|
|
||||||
unsubscribe(id, coroutine.running())
|
|
||||||
return message[1] ~= nil, unpack(message, 2, #message)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sys.waitUntilMsg(id)
|
|
||||||
local co = sys.check_task()
|
|
||||||
sys.subscribe(id, co)
|
|
||||||
local message = {coroutine.yield()}
|
|
||||||
sys.unsubscribe(id, co)
|
|
||||||
return unpack(message, 2, #message)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function waitUntilExt(id, ms)
|
|
||||||
subscribe(id, coroutine.running())
|
|
||||||
local message = ms and {wait(ms)} or {coroutine.yield()}
|
|
||||||
unsubscribe(id, coroutine.running())
|
|
||||||
if message[1] ~= nil then return unpack(message) end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function taskInit(fun, ...)
|
|
||||||
local co = coroutine.create(fun)
|
|
||||||
coroutine.resume(co, ...)
|
|
||||||
return co
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function init(mode, lprfnc)
|
|
||||||
|
|
||||||
assert(PROJECT and PROJECT ~= "" and VERSION and VERSION ~= "", "Undefine PROJECT or VERSION")
|
|
||||||
collectgarbage("setpause", 80)
|
|
||||||
|
|
||||||
|
|
||||||
uart.setup(uart.ATC, 0, 0, uart.PAR_NONE, uart.STOP_1)
|
|
||||||
log.info("poweron reason:", rtos.poweron_reason(), PROJECT, VERSION, SCRIPT_LIB_VER, rtos.get_version())
|
|
||||||
pcall(rtos.set_lua_info,"\r\n"..rtos.get_version().."\r\n"..(_G.PROJECT or "NO PROJECT").."\r\n"..(_G.VERSION or "NO VERSION"))
|
|
||||||
if type(rtos.get_build_time)=="function" then log.info("core build time", rtos.get_build_time()) end
|
|
||||||
if mode == 1 then
|
|
||||||
|
|
||||||
if rtos.poweron_reason() == rtos.POWERON_CHARGER then
|
|
||||||
|
|
||||||
rtos.poweron(0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function cmpTable(t1, t2)
|
|
||||||
if not t2 then return #t1 == 0 end
|
|
||||||
if #t1 == #t2 then
|
|
||||||
for i = 1, #t1 do
|
|
||||||
if unpack(t1, i, i) ~= unpack(t2, i, i) then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function timerStop(val, ...)
|
|
||||||
|
|
||||||
local arg={ ... }
|
|
||||||
if type(val) == 'number' then
|
|
||||||
timerPool[val], para[val], loop[val] = nil
|
|
||||||
rtos.timer_stop(val)
|
|
||||||
else
|
|
||||||
for k, v in pairs(timerPool) do
|
|
||||||
|
|
||||||
if type(v) == 'table' and v.cb == val or v == val then
|
|
||||||
|
|
||||||
if cmpTable(arg, para[k]) then
|
|
||||||
rtos.timer_stop(k)
|
|
||||||
timerPool[k], para[k], loop[val] = nil
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function timerStopAll(fnc)
|
|
||||||
for k, v in pairs(timerPool) do
|
|
||||||
if type(v) == "table" and v.cb == fnc or v == fnc then
|
|
||||||
rtos.timer_stop(k)
|
|
||||||
timerPool[k], para[k], loop[k] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function timerStart(fnc, ms, ...)
|
|
||||||
|
|
||||||
local arg={ ... }
|
|
||||||
local argcnt=0
|
|
||||||
for i, v in pairs(arg) do
|
|
||||||
argcnt = argcnt+1
|
|
||||||
end
|
|
||||||
assert(fnc ~= nil, "sys.timerStart(first param) is nil !")
|
|
||||||
assert(ms > 0, "sys.timerStart(Second parameter) is <= zero !")
|
|
||||||
|
|
||||||
if ms < 5 then ms = 5 end
|
|
||||||
|
|
||||||
if argcnt == 0 then
|
|
||||||
timerStop(fnc)
|
|
||||||
else
|
|
||||||
timerStop(fnc, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
if msgId >= MSG_TIMER_ID_MAX then msgId = TASK_TIMER_ID_MAX end
|
|
||||||
msgId = msgId + 1
|
|
||||||
if timerPool[msgId] == nil then
|
|
||||||
timerPool[msgId] = fnc
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if rtos.timer_start(msgId, ms) ~= 1 then log.debug("rtos.timer_start error") return end
|
|
||||||
|
|
||||||
if argcnt ~= 0 then
|
|
||||||
para[msgId] = arg
|
|
||||||
end
|
|
||||||
|
|
||||||
return msgId
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function timerLoopStart(fnc, ms, ...)
|
|
||||||
local tid = timerStart(fnc, ms, ...)
|
|
||||||
if tid then loop[tid] = (ms<5 and 5 or ms) end
|
|
||||||
return tid
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function timerIsActive(val, ...)
|
|
||||||
local arg={ ... }
|
|
||||||
if type(val) == "number" then
|
|
||||||
return timerPool[val]
|
|
||||||
else
|
|
||||||
for k, v in pairs(timerPool) do
|
|
||||||
if v == val then
|
|
||||||
if cmpTable(arg, para[k]) then return true end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local subscribers = {}
|
|
||||||
|
|
||||||
local messageQueue = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function subscribe(id, callback)
|
|
||||||
if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
|
|
||||||
log.warn("warning: sys.subscribe invalid parameter", id, callback)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if not subscribers[id] then subscribers[id] = {count = 0} end
|
|
||||||
if not subscribers[id][callback] then
|
|
||||||
subscribers[id].count = subscribers[id].count + 1
|
|
||||||
subscribers[id][callback] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function unsubscribe(id, callback)
|
|
||||||
if type(id) ~= "string" or (type(callback) ~= "function" and type(callback) ~= "thread") then
|
|
||||||
log.warn("warning: sys.unsubscribe invalid parameter", id, callback)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if subscribers[id] then
|
|
||||||
if subscribers[id][callback] then
|
|
||||||
subscribers[id].count = subscribers[id].count - 1
|
|
||||||
subscribers[id][callback] = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function publish(...)
|
|
||||||
local arg = { ... }
|
|
||||||
table.insert(messageQueue, arg)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function dispatch()
|
|
||||||
while true do
|
|
||||||
if #messageQueue == 0 then
|
|
||||||
|
|
||||||
|
|
||||||
for k, v in pairs(subscribers) do
|
|
||||||
if v.count == 0 then subscribers[k] = nil end
|
|
||||||
end
|
|
||||||
break
|
|
||||||
end
|
|
||||||
local message = table.remove(messageQueue, 1)
|
|
||||||
if subscribers[message[1]] then
|
|
||||||
for callback, flag in pairs(subscribers[message[1]]) do
|
|
||||||
if flag then
|
|
||||||
if type(callback) == "function" then
|
|
||||||
callback(unpack(message, 2, #message))
|
|
||||||
elseif type(callback) == "thread" then
|
|
||||||
coroutine.resume(callback, unpack(message))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if subscribers[message[1]] then
|
|
||||||
for callback, flag in pairs(subscribers[message[1]]) do
|
|
||||||
if not flag then
|
|
||||||
subscribers[message[1]][callback] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if subscribers[message[1]].count == 0 then
|
|
||||||
subscribers[message[1]] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local handlers = {}
|
|
||||||
setmetatable(handlers, {__index = function() return function() end end, })
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rtos.on = function(id, handler)
|
|
||||||
handlers[id] = handler
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function run()
|
|
||||||
while true do
|
|
||||||
|
|
||||||
dispatch()
|
|
||||||
|
|
||||||
local msg, param = rtos.receive(rtos.INF_TIMEOUT)
|
|
||||||
|
|
||||||
if msg == rtos.MSG_TIMER and timerPool[param] then
|
|
||||||
if param <= TASK_TIMER_ID_MAX then
|
|
||||||
local taskId = timerPool[param]
|
|
||||||
timerPool[param] = nil
|
|
||||||
if taskTimerPool[taskId] == param then
|
|
||||||
taskTimerPool[taskId] = nil
|
|
||||||
coroutine.resume(taskId)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local cb = timerPool[param]
|
|
||||||
|
|
||||||
if not loop[param] then timerPool[param] = nil end
|
|
||||||
if para[param] ~= nil then
|
|
||||||
cb(unpack(para[param]))
|
|
||||||
if not loop[param] then para[param] = nil end
|
|
||||||
else
|
|
||||||
cb()
|
|
||||||
end
|
|
||||||
|
|
||||||
if loop[param] then rtos.timer_start(param, loop[param]) end
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif type(msg) == "number" then
|
|
||||||
handlers[msg](param)
|
|
||||||
else
|
|
||||||
handlers[msg.id](msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require "clib"
|
|
||||||
|
|
||||||
if type(rtos.openSoftDog)=="function" then
|
|
||||||
rtos.openSoftDog(60000)
|
|
||||||
sys.timerLoopStart(rtos.eatSoftDog,20000)
|
|
||||||
end
|
|
||||||
@@ -1,286 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module(..., package.seeall)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.toHex(str, separator)
|
|
||||||
return str:gsub('.', function(c)
|
|
||||||
return string.format("%02X" .. (separator or ""), string.byte(c))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.fromHex(hex)
|
|
||||||
|
|
||||||
local hex = hex:gsub("[%s%p]", ""):upper()
|
|
||||||
return hex:gsub("%x%x", function(c)
|
|
||||||
return string.char(tonumber(c, 16))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.toValue(str)
|
|
||||||
return string.fromHex(str:gsub("%x", "0%1"))
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.utf8Len(str)
|
|
||||||
local _, count = string.gsub(str, "[^\128-\193]", "")
|
|
||||||
return count
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.utf8ToTable(str)
|
|
||||||
local tab = {}
|
|
||||||
for uchar in string.gfind(str, "[%z\1-\127\194-\244][\128-\191]*") do
|
|
||||||
tab[#tab + 1] = uchar
|
|
||||||
end
|
|
||||||
return tab
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.rawurlEncode(str)
|
|
||||||
local t = str:utf8ToTable()
|
|
||||||
for i = 1, #t do
|
|
||||||
if #t[i] == 1 then
|
|
||||||
t[i] = string.gsub(string.gsub(t[i], "([^%w_%~%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end), " ", "%%20")
|
|
||||||
else
|
|
||||||
t[i] = string.gsub(t[i], ".", function(c) return string.format("%%%02X", string.byte(c)) end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return table.concat(t)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.urlEncode(str)
|
|
||||||
local t = str:utf8ToTable()
|
|
||||||
for i = 1, #t do
|
|
||||||
if #t[i] == 1 then
|
|
||||||
t[i] = string.gsub(string.gsub(t[i], "([^%w_%*%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end), " ", "+")
|
|
||||||
else
|
|
||||||
t[i] = string.gsub(t[i], ".", function(c) return string.format("%%%02X", string.byte(c)) end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return table.concat(t)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function table.gsort(t, f)
|
|
||||||
local a = {}
|
|
||||||
for n in pairs(t) do a[#a + 1] = n end
|
|
||||||
table.sort(a, f)
|
|
||||||
local i = 0
|
|
||||||
return function()
|
|
||||||
i = i + 1
|
|
||||||
return a[i], t[a[i]]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function table.rconcat(l)
|
|
||||||
if type(l) ~= "table" then return l end
|
|
||||||
local res = {}
|
|
||||||
for i = 1, #l do
|
|
||||||
res[i] =table.rconcat(l[i])
|
|
||||||
end
|
|
||||||
return table.concat(res)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.formatNumberThousands(num)
|
|
||||||
local k, formatted
|
|
||||||
formatted = tostring(tonumber(num))
|
|
||||||
while true do
|
|
||||||
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
|
|
||||||
if k == 0 then break end
|
|
||||||
end
|
|
||||||
return formatted
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.split(str, delimiter)
|
|
||||||
local strlist, tmp = {}, string.byte(delimiter)
|
|
||||||
if delimiter == "" then
|
|
||||||
for i = 1, #str do strlist[i] = str:sub(i, i) end
|
|
||||||
else
|
|
||||||
for substr in string.gmatch(str .. delimiter, "(.-)" .. (((tmp > 96 and tmp < 123) or (tmp > 64 and tmp < 91) or (tmp > 47 and tmp < 58)) and delimiter or "%" .. delimiter)) do
|
|
||||||
table.insert(strlist, substr)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return strlist
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function string.checkSum(str, num)
|
|
||||||
assert(type(str) == "string", "The first argument is not a string!")
|
|
||||||
local sum = 0
|
|
||||||
for i = 1, #str do
|
|
||||||
sum = sum + str:sub(i, i):byte()
|
|
||||||
end
|
|
||||||
if num == 2 then
|
|
||||||
return sum % 0x10000
|
|
||||||
else
|
|
||||||
return sum % 0x100
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.exists(path)
|
|
||||||
local file = io.open(path, "r")
|
|
||||||
if file then
|
|
||||||
io.close(file)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.readFile(path)
|
|
||||||
local file = io.open(path, "rb")
|
|
||||||
if file then
|
|
||||||
local content = file:read("*a")
|
|
||||||
io.close(file)
|
|
||||||
return content
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.writeFile(path, content, mode)
|
|
||||||
local mode = mode or "w+b"
|
|
||||||
local file = io.open(path, mode)
|
|
||||||
if file then
|
|
||||||
if file:write(content) == nil then return false end
|
|
||||||
io.close(file)
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.pathInfo(path)
|
|
||||||
local pos = string.len(path)
|
|
||||||
local extpos = pos + 1
|
|
||||||
while pos > 0 do
|
|
||||||
local b = string.byte(path, pos)
|
|
||||||
if b == 46 then
|
|
||||||
extpos = pos
|
|
||||||
elseif b == 47 then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
pos = pos - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local dirname = string.sub(path, 1, pos)
|
|
||||||
local filename = string.sub(path, pos + 1)
|
|
||||||
extpos = extpos - pos
|
|
||||||
local basename = string.sub(filename, 1, extpos - 1)
|
|
||||||
local extname = string.sub(filename, extpos)
|
|
||||||
return {
|
|
||||||
dirname = dirname,
|
|
||||||
filename = filename,
|
|
||||||
basename = basename,
|
|
||||||
extname = extname
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.fileSize(path)
|
|
||||||
local size = 0
|
|
||||||
local file = io.open(path, "r")
|
|
||||||
if file then
|
|
||||||
local current = file:seek()
|
|
||||||
size = file:seek("end")
|
|
||||||
file:seek("set", current)
|
|
||||||
io.close(file)
|
|
||||||
end
|
|
||||||
return size
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function io.readStream(path, offset, len)
|
|
||||||
local file, str = io.open(path, "r")
|
|
||||||
if file then
|
|
||||||
local current = file:seek()
|
|
||||||
file:seek("set", offset)
|
|
||||||
str = file:read(len)
|
|
||||||
file:seek("set", current)
|
|
||||||
io.close(file)
|
|
||||||
end
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
BIN
4G/烧写工具/Luatools_v3.exe
Normal file
BIN
4G/烧写工具/Luatools_v3.exe
Normal file
Binary file not shown.
313
Doc/4G模块文档.md
Normal file
313
Doc/4G模块文档.md
Normal file
@@ -0,0 +1,313 @@
|
|||||||
|
# 4G模块固件文档(AIR724 + LuatOS-Air)
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
基于 **AIR724(RDA8910)** 4G模块 + **LuatOS-Air** 固件平台,运行 Lua 脚本实现多路 TCP Socket 管理、串口命令解析转发、SIM卡查询等功能。
|
||||||
|
|
||||||
|
| 项目 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 硬件平台 | AIR724 (RDA8910) |
|
||||||
|
| 固件平台 | LuatOS-Air V4035 |
|
||||||
|
| 底层固件 | `LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac` |
|
||||||
|
| 量产文件 | `4G_NETWORK_1.0.0_LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac` |
|
||||||
|
| OTA升级文件 | `4G_NETWORK_1.0.0_LuatOS-Air_V4035_RDA8910_BT_FLOAT.dfota.bin` |
|
||||||
|
| 开发语言 | Lua 5.3 |
|
||||||
|
| 串口参数 | 115200, 8N1 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 目录结构
|
||||||
|
|
||||||
|
```
|
||||||
|
4G/
|
||||||
|
├── 已编译量产文件/ ← 可直接用于烧录和OTA升级
|
||||||
|
│ ├── 4G量产文件/
|
||||||
|
│ │ └── 4G_NETWORK_1.0.0_LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac
|
||||||
|
│ │ └── 量产烧录文件(底层固件 + 脚本合包,一键烧录)
|
||||||
|
│ │
|
||||||
|
│ └── 4G远程升级文件/
|
||||||
|
│ └── 4G_NETWORK_1.0.0_LuatOS-Air_V4035_RDA8910_BT_FLOAT.dfota.bin
|
||||||
|
│ └── OTA差分升级文件(用于远程空中升级)
|
||||||
|
│
|
||||||
|
├── 源代码/ ← 源码 + 开发用底层固件
|
||||||
|
│ ├── bin/
|
||||||
|
│ │ └── LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac ← 底层固件(开发调试用)
|
||||||
|
│ │
|
||||||
|
│ └── core/ ← 核心脚本(需烧录到模块)
|
||||||
|
│ ├── main.lua ← 入口:初始化硬件、网络、串口
|
||||||
|
│ ├── cmd.lua ← 命令解析与分发
|
||||||
|
│ └── linksocket.lua ← 多路TCP Socket管理
|
||||||
|
│
|
||||||
|
├── 烧写工具/
|
||||||
|
│ └── Luatools_v3.exe ← LuatOS官方烧录工具
|
||||||
|
│
|
||||||
|
└── lib/ ← LuatOS-Air 标准库(模块内置,无需烧录)
|
||||||
|
├── sys.lua ← 系统核心(任务调度、定时器)
|
||||||
|
├── log.lua ← 日志
|
||||||
|
├── uart.lua ← 串口驱动
|
||||||
|
├── socket.lua ← TCP/UDP Socket
|
||||||
|
├── socket4G.lua ← 4G Socket适配层
|
||||||
|
├── net.lua ← 网络管理
|
||||||
|
├── sim.lua ← SIM卡管理
|
||||||
|
├── ril.lua ← AT命令接口
|
||||||
|
├── pm.lua ← 电源管理
|
||||||
|
└── ...(共约50个标准库)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 核心脚本说明
|
||||||
|
|
||||||
|
### 1. main.lua — 程序入口
|
||||||
|
|
||||||
|
**文件**:[源代码/core/main.lua](file:///d:/公司项目/BR_YKC/4G/源代码/core/main.lua)
|
||||||
|
|
||||||
|
启动流程:
|
||||||
|
|
||||||
|
```
|
||||||
|
main.lua 加载
|
||||||
|
↓
|
||||||
|
require "sys" / "net" / "log" / "uart" / "netLed" / "pmd"
|
||||||
|
↓
|
||||||
|
uart.setup(1, 115200, 8, uart.PAR_NONE, uart.STOP_1) ← 初始化UART1
|
||||||
|
↓
|
||||||
|
pmd.ldoset(2, pmd.LDO_VLCD) ← 开启网络指示灯电源
|
||||||
|
netLed.setup(true, pio.P0_1, pio.P0_4) ← 配置指示灯GPIO
|
||||||
|
↓
|
||||||
|
net.startQueryAll(8 * 1000, 60 * 1000) ← 启动网络注册(超时8s/间隔60s)
|
||||||
|
ril.request("AT+RNDISCALL=0,1") ← 激活数据链路(RNDIS)
|
||||||
|
↓
|
||||||
|
require "cmd" ← 加载命令解析模块
|
||||||
|
require "linksocket" ← 加载Socket管理模块
|
||||||
|
↓
|
||||||
|
linksocket.setRecvCallback(...) ← 注册TCP数据接收回调
|
||||||
|
uart.on(1, "receive", ...) ← 注册串口接收回调
|
||||||
|
↓
|
||||||
|
sys.init(0, 0)
|
||||||
|
sys.run() ← 启动系统(永不返回)
|
||||||
|
```
|
||||||
|
|
||||||
|
**串口数据流**:
|
||||||
|
|
||||||
|
```
|
||||||
|
STM32 → UART1 → cmd.process(rawData) ← STM32发来的命令帧
|
||||||
|
↓
|
||||||
|
解析55 AA帧头
|
||||||
|
↓
|
||||||
|
调用对应的handleCmdXX
|
||||||
|
↓
|
||||||
|
linksocket.connect/send/disconnect
|
||||||
|
↓
|
||||||
|
4G → TCP → 云快充服务器
|
||||||
|
|
||||||
|
云快充服务器 → TCP → 4G → recvCallback(socketId, data)
|
||||||
|
↓
|
||||||
|
组装 55 AA 01 socketId len data AA 55
|
||||||
|
↓
|
||||||
|
UART1 → STM32 ← 下行数据转发
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. cmd.lua — 命令解析与分发
|
||||||
|
|
||||||
|
**文件**:[源代码/core/cmd.lua](file:///d:/公司项目/BR_YKC/4G/源代码/core/cmd.lua)
|
||||||
|
|
||||||
|
负责解析 STM32 通过 UART1 发来的 `55 AA` 帧格式命令,并调用 linksocket 接口或系统接口。
|
||||||
|
|
||||||
|
#### 帧格式
|
||||||
|
|
||||||
|
```
|
||||||
|
55 AA mainCmd subCmd [payload] AA 55
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 命令表
|
||||||
|
|
||||||
|
| mainCmd | subCmd | 处理函数 | 功能 |
|
||||||
|
|---------|--------|----------|------|
|
||||||
|
| 0x01 | socketId | `handleCmd01` | 数据发送到指定Socket。失败回复 `0x83 03 socketId 01` |
|
||||||
|
| 0x02 | - | `handleCmd02` | 系统重启 (`sys.restart()`) |
|
||||||
|
| 0x03 | 0x01 | `handleCmd03` | 连接Socket。调用 `linksocket.connectSocket(socketId)` |
|
||||||
|
| 0x03 | 0x02 | `handleCmd03` | 断开Socket。调用 `linksocket.disconnectSocket(socketId)` |
|
||||||
|
| 0x04 | 0x00 | `handleCmd04` | 设置服务器地址。回复 `0x87 00 00/01` |
|
||||||
|
| 0x05 | 0x01 | `handleCmd05` | 查询ICCID。回复 `0x84 01 len iccid` |
|
||||||
|
| 0x05 | 0x02 | `handleCmd05` | 查询IMSI。回复 `0x84 02 len imsi` |
|
||||||
|
| 0x05 | 0x03 | `handleCmd05` | 查询IMEI。回复 `0x84 03 len imei` |
|
||||||
|
| 0x06 | 0x00 | `handleCmd06` | 查询信号强度CSQ。回复 `0x85 01 csq ber` |
|
||||||
|
| 0x07 | 0x00 | `handleCmd07` | 查询6个Socket链路状态。回复 `0x86 01 [6字节状态]` |
|
||||||
|
|
||||||
|
#### 上行响应帧
|
||||||
|
|
||||||
|
| mainCmd | subCmd | 含义 | 触发时机 |
|
||||||
|
|---------|--------|------|----------|
|
||||||
|
| 0x83 | 0x01 | **TCP已连接** | Socket连接成功 |
|
||||||
|
| 0x83 | 0x02 | **TCP已断开** | Socket断开/掉线 |
|
||||||
|
| 0x83 | 0x03 | **TCP连接失败** | Socket连接失败/发送失败 |
|
||||||
|
| 0x84 | 0x01~0x03 | SIM信息 | 查询ICCID/IMSI/IMEI结果 |
|
||||||
|
| 0x85 | 0x01 | 信号强度 | 查询CSQ结果 |
|
||||||
|
| 0x86 | 0x01 | 链路状态 | 查询6路Socket状态 |
|
||||||
|
| 0x87 | 0x00 | 服务器配置结果 | 设置服务器地址结果 |
|
||||||
|
|
||||||
|
#### 帧缓冲机制
|
||||||
|
|
||||||
|
`cmd.lua` 内部维护 `frameBuffer` 字符串,处理串口数据粘包/拆包:
|
||||||
|
|
||||||
|
```
|
||||||
|
process(rawData) → frameBuffer.append
|
||||||
|
↓
|
||||||
|
while true:
|
||||||
|
查找 55 AA ... AA 55 完整帧
|
||||||
|
找到 → 提取mainCmd/subCmd/payload → 分发处理
|
||||||
|
未找到 → 继续等待更多数据
|
||||||
|
非55AA开头 → 跳过1字节
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. linksocket.lua — 多路TCP Socket管理
|
||||||
|
|
||||||
|
**文件**:[源代码/core/linksocket.lua](file:///d:/公司项目/BR_YKC/4G/源代码/core/linksocket.lua)
|
||||||
|
|
||||||
|
管理 **6路独立TCP Socket**(对应STM32侧的6个充电桩),每路Socket有独立的任务循环。
|
||||||
|
|
||||||
|
#### 结构
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local clients[6] = {
|
||||||
|
{ id = 1, socket = nil, connected = false },
|
||||||
|
{ id = 2, socket = nil, connected = false },
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 每个Socket的任务循环
|
||||||
|
|
||||||
|
```
|
||||||
|
等待 connectRequested[id] == true
|
||||||
|
↓
|
||||||
|
等待网络就绪(socket.isReady()) + SIM卡正常(sim.getStatus())
|
||||||
|
↓
|
||||||
|
socket.tcp():connect(server_config.ip, server_config.port)
|
||||||
|
↓
|
||||||
|
失败 → 上报 0x83 03 socketId 1
|
||||||
|
↓
|
||||||
|
成功 → 上报 0x83 01 socketId 0 → 进入监控循环
|
||||||
|
↓
|
||||||
|
监控:asyncSelect(60, "recv") 60秒超时检测
|
||||||
|
├── 正常 → 继续
|
||||||
|
├── 超时/掉线 → 上报 0x83 02 socketId 0 → 回到顶部
|
||||||
|
├── SIM卡拔出 → 上报 0x83 02 socketId 3 → 回到顶部
|
||||||
|
└── 收到断开命令 → 上报 0x83 02 socketId 0 → 回到顶部
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 外部接口
|
||||||
|
|
||||||
|
| 函数 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `connectSocket(id)` | 标记id为待连接,触发对应Socket任务 |
|
||||||
|
| `disconnectSocket(id)` | 标记id为待断开,关闭Socket |
|
||||||
|
| `sendToSocket(id, data)` | 在已连接的Socket上发送数据 |
|
||||||
|
| `setServerAddress(ip, port)` | 设置服务器地址(断开所有已连接Socket) |
|
||||||
|
| `getLinkStatus()` | 获取6路Socket连接状态 |
|
||||||
|
| `setRecvCallback(cb)` | 注册数据接收回调 |
|
||||||
|
|
||||||
|
#### 服务器配置
|
||||||
|
|
||||||
|
默认在文件顶部配置:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
local server_config = {
|
||||||
|
ip = "129.211.170.245",
|
||||||
|
port = 9002
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
可通过 `0x04` 命令动态修改(由 `drv_air724_set_server` 触发)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 数据流完整路径
|
||||||
|
|
||||||
|
### 上行(桩 → 云快充)
|
||||||
|
|
||||||
|
```
|
||||||
|
充电桩 → UDP → STM32(task_ykc)
|
||||||
|
↓
|
||||||
|
charger_to_server_0Xxx() 组YKC帧
|
||||||
|
↓
|
||||||
|
drv_air724_send_cmd(0x01, socketId, ykc_frame)
|
||||||
|
↓
|
||||||
|
UART1 → 55 AA 01 socketId len [ykc_frame] AA 55
|
||||||
|
↓
|
||||||
|
AIR724 cmd.lua: handleCmd01(socketId, ykc_frame)
|
||||||
|
↓
|
||||||
|
linksocket.sendToSocket(socketId, ykc_frame)
|
||||||
|
↓
|
||||||
|
TCP → 云快充服务器
|
||||||
|
```
|
||||||
|
|
||||||
|
### 下行(云快充 → 桩)
|
||||||
|
|
||||||
|
```
|
||||||
|
云快充服务器 → TCP → AIR724
|
||||||
|
↓
|
||||||
|
linksocket 接收回调
|
||||||
|
↓
|
||||||
|
55 AA 01 socketId len [ykc_frame] AA 55 → UART1
|
||||||
|
↓
|
||||||
|
STM32 task_air724: 解析 → ykc_route_dispatch
|
||||||
|
↓
|
||||||
|
server_to_charger.c: on_cmd_frame_type_0Xxx()
|
||||||
|
↓
|
||||||
|
填充 g_charger_manager 结构体 / 点对点回复桩
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 烧写说明
|
||||||
|
|
||||||
|
AIR724 模块使用 **Luatools** 工具进行固件和脚本烧录。
|
||||||
|
|
||||||
|
### 准备工作
|
||||||
|
|
||||||
|
1. **打开烧写工具**
|
||||||
|
- 工具位置:`4G/烧写工具/Luatools_v3.exe`
|
||||||
|
- 双击打开即可
|
||||||
|
|
||||||
|
2. **硬件连接**
|
||||||
|
- 使用 **Type-C USB 线** 将 AIR724 模块连接到电脑
|
||||||
|
- 模块上电后,电脑会出现 **3 个新的 COM 口**
|
||||||
|
|
||||||
|
### 量产烧录(编译好的文件直接烧录)
|
||||||
|
|
||||||
|
量产文件已预先编译好,位于 `4G/已编译量产文件/4G量产文件/`,可直接烧录:
|
||||||
|
|
||||||
|
1. 打开 `Luatools_v3.exe`
|
||||||
|
2. 点击 **`下载固件`** 按钮
|
||||||
|
3. 等待官方文件下载完毕
|
||||||
|
4. 点击 **`选择文件`** 按钮, 选择**量产文件**
|
||||||
|
- 文件路径:`4G/已编译量产文件/4G量产文件/4G_NETWORK_1.0.0_LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac`
|
||||||
|
5. 点击 **`下载`**
|
||||||
|
6. 工具会自烧录,完成后会有提示
|
||||||
|
|
||||||
|
> **提示**:量产文件是将底层固件和 Lua 脚本打包为一个 `.pac` 文件,一次性烧录,无需分两步操作。
|
||||||
|
|
||||||
|
### 开发调试烧录(自己修改代码后测试)
|
||||||
|
|
||||||
|
修改 Lua 代码后的开发调试阶段:
|
||||||
|
1. **创建项目**
|
||||||
|
- 点击左下角的 **`创建项目`** 新建项目
|
||||||
|
|
||||||
|
2. **选择底层固件**(仅首次需要):
|
||||||
|
- 点击 **`选择文件`** 按钮
|
||||||
|
- 选择底层Core文件:`4G/源代码/bin/LuatOS-Air_V4035_RDA8910_BT_FLOAT.pac`
|
||||||
|
|
||||||
|
2. **选择脚本**(每次修改代码后):
|
||||||
|
- 点击 **`增加目录(递归)`** 按钮
|
||||||
|
- **选择脚本目录**:`4G/源代码/core/`(工具自动识别 `main.lua`、`cmd.lua`、`linksocket.lua`)
|
||||||
|
- **选择lib目录**:`4G/源代码/lib/`(必选,用于包含外部库文件)
|
||||||
|
- 点击 **`下载脚本`**(固件版本不变时)或 **`下载底层和脚本`**(全刷)
|
||||||
|
|
||||||
|
|
||||||
|
## 两种烧录方式对比
|
||||||
|
|
||||||
|
| 方式 | 适用场景 | 文件 | 操作 |
|
||||||
|
|------|----------|------|------|
|
||||||
|
| **量产烧录** | 批量生产、出厂烧录 | `.pac` 量产文件 | Luatools → 量产管理 → 开始烧录 |
|
||||||
|
| **开发调试** | 修改Lua代码后测试 | 底层固件 `.pac` + `core/`+ `lib/` 脚本目录 | 项目管理测试 → 下载脚本 |
|
||||||
Reference in New Issue
Block a user