|
|
@@ -10,8 +10,16 @@
|
|
|
local libfota = require("libfota")
|
|
|
|
|
|
-- 功能:获取fota的回调函数
|
|
|
--- 参数:-- result:number类型,0表示成功,1表示连接失败,2表示url错误,3表示服务器断开,4表示接收报文错误,5表示使用iot平台VERSION需要使用 xxx.yyy形式
|
|
|
+-- 参数:
|
|
|
+-- result:number类型
|
|
|
+-- 0表示成功
|
|
|
+-- 1表示连接失败
|
|
|
+-- 2表示url错误
|
|
|
+-- 3表示服务器断开
|
|
|
+-- 4表示接收报文错误
|
|
|
+-- 5表示使用iot平台VERSION需要使用 xxx.yyy形式
|
|
|
function libfota_cb(result)
|
|
|
+ log.info("fota", "result", result)
|
|
|
-- fota成功
|
|
|
if result == 0 then
|
|
|
rtos.reboot() --如果还有其他事情要做,就不要立刻reboot
|
|
|
@@ -23,11 +31,19 @@ end
|
|
|
--注意!!!:使用合宙iot平台,必须用luatools量产生成的.bin文件!!! 自建服务器可使用.ota文件!!!
|
|
|
|
|
|
--下方示例为合宙iot平台,地址:http://iot.openluat.com
|
|
|
-libfota.request(libfota_cb,0,0,0,"http://iot.openluat.com/api/site/firmware_upgrade?project_key=" .. _G.PRODUCT_KEY .. "&imei=".. mobile.imei() .. "&device_key=&firmware_name=" .. _G.PROJECT.. "_LuatOS-SoC_" .. rtos.bsp() .. "&version=" .. rtos.version():sub(2) .. "." .. _G.VERSION)
|
|
|
+libfota.request(libfota_cb)
|
|
|
|
|
|
--如使用自建服务器,自行更换url
|
|
|
-libfota.request(libfota_cb,0,0,0,"http://xxxxxx.com")
|
|
|
+-- 对自定义服务器的要求是:
|
|
|
+-- 若需要升级, 响应http 200, body为升级文件的内容
|
|
|
+-- 若不需要升级, 响应300或以上的代码,务必注意
|
|
|
+libfota.request(libfota_cb,"http://xxxxxx.com/xxx/upgrade?version=" .. _G.VERSION)
|
|
|
|
|
|
+-- 若需要定时升级
|
|
|
+-- 合宙iot平台
|
|
|
+sys.timerLoopStart(libfota.request, 4*3600*1000, libfota_cb)
|
|
|
+-- 自建平台
|
|
|
+sys.timerLoopStart(libfota.request, 4*3600*1000, libfota_cb, "http://xxxxxx.com/xxx/upgrade?version=" .. _G.VERSION)
|
|
|
]]
|
|
|
|
|
|
local sys = require "sys"
|
|
|
@@ -37,21 +53,35 @@ local libnet = require "libnet"
|
|
|
local libfota = {}
|
|
|
|
|
|
local taskName = "OTA_TASK"
|
|
|
+local tag = "fota"
|
|
|
|
|
|
local function netCB(msg)
|
|
|
if msg[1] == socket.EVENT then
|
|
|
- log.info("socket网络状态变更")
|
|
|
+ log.info(tag, "socket网络状态变更")
|
|
|
elseif msg[1] == socket.TX_OK then
|
|
|
- log.info("socket发送完成")
|
|
|
+ log.info(tag, "socket发送完成")
|
|
|
elseif msg[1] == socket.EV_NW_RESULT_CLOSE then
|
|
|
- log.info("socket关闭")
|
|
|
+ log.info(tag, "socket关闭")
|
|
|
else
|
|
|
- log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
|
|
|
+ log.info(tag, "未处理消息", msg[1], msg[2], msg[3], msg[4])
|
|
|
end
|
|
|
end
|
|
|
|
|
|
local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,timeout)
|
|
|
fota.init(storge_location, len, param1)
|
|
|
+ -- 若ota_url没有传,那就是用合宙iot平台
|
|
|
+ if ota_url == nil then
|
|
|
+ if _G.PRODUCT_KEY == nil then
|
|
|
+ -- 必须在main.lua定义 PRODUCT_KEY = "xxx"
|
|
|
+ -- iot平台新建项目后, 项目详情中可以查到
|
|
|
+ log.error("fota", "iot.openluat.com need PRODUCT_KEY!!!")
|
|
|
+ return
|
|
|
+ end
|
|
|
+ ota_url = "http://iot.openluat.com/api/site/firmware_upgrade?project_key=" .. _G.PRODUCT_KEY .. "&imei=".. mobile.imei() .. "&device_key=&firmware_name=" .. _G.PROJECT.. "_LuatOS-SoC_" .. rtos.bsp() .. "&version=" .. rtos.version():sub(2) .. "." .. _G.VERSION
|
|
|
+ end
|
|
|
+
|
|
|
+ -- TODO ota_port 应该从url自动得出
|
|
|
+
|
|
|
local succ, param, ip, port, total, findhead, filelen, rcvCache,d1,d2,statusCode,retry,rspHead,rcvChunked,done,fotaDone,nCache
|
|
|
local tbuff = zbuff.create(512)
|
|
|
local rbuff = zbuff.create(4096)
|
|
|
@@ -64,6 +94,8 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
rspHead = {}
|
|
|
local ret = 1
|
|
|
local result = libnet.waitLink(taskName, 0, netc)
|
|
|
+ -- TODO 支持带端口号的url
|
|
|
+ -- TODO 支持https
|
|
|
local type,host,uri = string.match(ota_url,"(%a-)://(%S-)/(%S+)")
|
|
|
while retry < 3 and not done do
|
|
|
if type == nil or host == nil then
|
|
|
@@ -91,7 +123,7 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
while result do
|
|
|
succ, param, ip, port = socket.rx(netc, rbuff)
|
|
|
if not succ then
|
|
|
- log.info("服务器断开了", succ, param, ip, port)
|
|
|
+ log.info(tag, "服务器断开了", succ, param, ip, port)
|
|
|
ret = 3
|
|
|
break
|
|
|
end
|
|
|
@@ -101,20 +133,20 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
if succ then
|
|
|
total = total + rbuff:used()
|
|
|
else
|
|
|
- log.error("fota写入异常,请至少在1秒后重试")
|
|
|
+ log.error(tag, "写入异常,请至少在1秒后重试")
|
|
|
fota.finish(false)
|
|
|
done = true
|
|
|
break
|
|
|
end
|
|
|
- log.info("收到服务器数据,长度", rbuff:used(), "fota结果", succ, done, "总共", filelen)
|
|
|
+ log.info(tag, "收到服务器数据,长度", rbuff:used(), "fota结果", succ, done, "总共", filelen)
|
|
|
rbuff:del()
|
|
|
if fotaDone then
|
|
|
- log.info("下载完成")
|
|
|
+ log.info(tag, "下载完成")
|
|
|
while true do
|
|
|
succ,fotaDone = fota.isDone()
|
|
|
if fotaDone then
|
|
|
fota.finish(true)
|
|
|
- log.info("FOTA完成")
|
|
|
+ log.info(tag, "FOTA完成")
|
|
|
done = true
|
|
|
ret = 0
|
|
|
break
|
|
|
@@ -132,13 +164,13 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
--状态行
|
|
|
_,d1,statusCode = rcvCache:find("%s(%d+)%s.-\r\n")
|
|
|
if not statusCode then
|
|
|
- log.info("http没有状态返回")
|
|
|
+ log.info(tag, "http没有状态返回")
|
|
|
ret = 4
|
|
|
break
|
|
|
end
|
|
|
statusCode = tonumber(statusCode)
|
|
|
if statusCode ~= 200 and statusCode ~= 206 then
|
|
|
- log.info("http应答不OK", statusCode)
|
|
|
+ log.info(tag, "http应答不OK", statusCode)
|
|
|
done = true
|
|
|
ret = 4
|
|
|
break
|
|
|
@@ -159,21 +191,21 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
if succ then
|
|
|
total = total + rbuff:used()
|
|
|
else
|
|
|
- log.error("fota写入异常,请至少在1秒后重试")
|
|
|
+ log.error(tag, "写入异常,请至少在1秒后重试")
|
|
|
fota.finish(false)
|
|
|
done = true
|
|
|
break
|
|
|
end
|
|
|
- log.info("收到服务器数据,长度", rbuff:used(), "fota结果", succ, done, "总共", filelen)
|
|
|
+ log.info(tag, "收到服务器数据,长度", rbuff:used(), "fota结果", succ, done, "总共", filelen)
|
|
|
rbuff:del()
|
|
|
|
|
|
if fotaDone then
|
|
|
- log.info("下载完成")
|
|
|
+ log.info(tag, "下载完成")
|
|
|
while true do
|
|
|
succ,fotaDone = fota.isDone()
|
|
|
if fotaDone then
|
|
|
fota.finish(true)
|
|
|
- log.info("FOTA完成")
|
|
|
+ log.info(tag, "FOTA完成")
|
|
|
done = true
|
|
|
ret = 0
|
|
|
break
|
|
|
@@ -189,14 +221,14 @@ local function fota_task(cbFnc,storge_location, len, param1,ota_url,ota_port,tim
|
|
|
findhead = true
|
|
|
end
|
|
|
end
|
|
|
- log.info("等待新数据到来")
|
|
|
+ log.info(tag, "等待新数据到来")
|
|
|
result, param = libnet.wait(taskName, 5000, netc)
|
|
|
log.info(result, param)
|
|
|
if not result then
|
|
|
- log.info("服务器断开了", result, param)
|
|
|
+ log.info(tag, "服务器断开了", result, param)
|
|
|
break
|
|
|
elseif not param then
|
|
|
- log.info("服务器没有数据", result, param)
|
|
|
+ log.info(tag, "服务器没有数据", result, param)
|
|
|
break
|
|
|
end
|
|
|
end
|
|
|
@@ -211,18 +243,18 @@ end
|
|
|
|
|
|
--[[
|
|
|
fota升级
|
|
|
-@api libfota.request(cbFnc,storge_location, len, param1,ota_url,ota_port,timeout)
|
|
|
-@function cbFnc 用户回调函数,回调函数的调用形式为:cbFnc(result)
|
|
|
-@number/string storge_location fota数据存储的起始位置<br>如果是int,则是由芯片平台具体判断<br>如果是string,则存储在文件系统中<br>如果为nil,则由底层决定存储位置
|
|
|
-@number len 数据存储的最大空间
|
|
|
-@userdata param1,如果数据存储在spiflash时,为spi_device
|
|
|
-@string ota_url url 合宙iot平台url拼接参考示例
|
|
|
-@number ota_port 请求端口,默认80
|
|
|
-@number timeout 请求超时时间,单位毫秒,默认20000毫秒
|
|
|
-@return nil
|
|
|
+@api libfota.request(cbFnc,ota_url,storge_location, len, param1,ota_port,timeout)
|
|
|
+@function cbFnc 用户回调函数,回调函数的调用形式为:cbFnc(result) , 必须传
|
|
|
+@string ota_url 升级URL, 若不填则自动使用合宙iot平台
|
|
|
+@number/string storge_location 可选,fota数据存储的起始位置<br>如果是int,则是由芯片平台具体判断<br>如果是string,则存储在文件系统中<br>如果为nil,则由底层决定存储位置
|
|
|
+@number len 可选,数据存储的最大空间
|
|
|
+@userdata param1,可选,如果数据存储在spiflash时,为spi_device
|
|
|
+@number ota_port 可选,请求端口,默认80
|
|
|
+@number timeout 可选,请求超时时间,单位毫秒,默认20000毫秒
|
|
|
+@return nil 无返回值
|
|
|
]]
|
|
|
-function libfota.request(cbFnc,storge_location, len, param1,ota_url,ota_port,timeout)
|
|
|
- sysplus.taskInitEx(fota_task, taskName, netCB, cbFnc,storge_location, len, param1,ota_url, ota_port or 80,timeout or 20000)
|
|
|
+function libfota.request(cbFnc,ota_url,storge_location, len, param1,ota_port,timeout)
|
|
|
+ sysplus.taskInitEx(fota_task, taskName, netCB, cbFnc,storge_location, len, param1,ota_url, ota_port or 80,timeout or 30000)
|
|
|
end
|
|
|
|
|
|
return libfota
|