add : USB CDC虚拟串口通讯

This commit is contained in:
2026-04-03 12:14:43 +08:00
parent da4e944bca
commit dc518cefed
59 changed files with 13943 additions and 673 deletions

View File

@@ -0,0 +1,38 @@
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)

View File

@@ -0,0 +1,14 @@
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

View File

@@ -0,0 +1,310 @@
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

View File

@@ -0,0 +1,461 @@
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

View File

@@ -0,0 +1,83 @@
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

View File

@@ -0,0 +1,123 @@
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

View File

@@ -0,0 +1,65 @@
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()

View File

@@ -0,0 +1,787 @@
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()

View File

@@ -0,0 +1,183 @@
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)

View File

@@ -0,0 +1,184 @@
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

View File

@@ -0,0 +1,150 @@
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

View File

@@ -0,0 +1,65 @@
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

View File

@@ -0,0 +1,554 @@
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)

View File

@@ -0,0 +1,184 @@
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?")

View File

@@ -0,0 +1,121 @@
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

View File

@@ -0,0 +1,680 @@
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

View File

@@ -0,0 +1,526 @@
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

View File

@@ -0,0 +1,286 @@
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