|
/usr/sbin/auto_upgrade.lua
#!/usr/bin/lua
--include--
local err = require("luci.phicomm.error")
local libphi_cgi = require("luci.adapter.libphi_cgi")
local cursor = require "luci.model.uci".cursor()
local json = require("luci.json")
local libphi_cgi = require("luci.adapter.libphi_cgi")
local string = require "string"
--functions--
function WIFEXITED(status)
if status <= 0 then
return status
end
local bit = require "nixio".bit
return bit.rshift(status, 8)
end
function auto_system_upgrade()
local fs = require("luci.fs")
local http = require("luci.http")
local json = require("luci.json")
--判断是否升级
local error, result, data
local libphi_cgi = require("luci.adapter.libphi_cgi")
-- define object context
local obj = [[{"Device.X_LANTIQ_COM_Upgrade.Upgrade.4" : { "State" : null }}]]
error, data = libphi_cgi.get_conf(obj)
if error == err.E_NONE then
--判断升级状态
result = json.decode(data)
print(result.Objects[1].Param[1].ParamValue)
if result.Objects[1].Param[1].ParamValue == "UPG_REQ" then
print("upgrade is running")
write_response(err.E_EXPT)
return err.E_EXPT --should be upgrade already error code
end
else
print("get update state failed")
write_response(error)
return error
end
--设置升级标志
obj = [[{"action" : "firmware_req"}]]
error, result = libphi_cgi.do_conf(obj)
if error ~= err.E_NONE then
print("req update state failed")
write_response(err.E_EXPT)
return err.E_EXPT --should be req upgrade state error code
end
-- upack image, check image, write to flash and reboot
local check_state = WIFEXITED(os.execute("sh /usr/sbin/check_image.sh > /dev/null"))
if 0 == check_state then
-- OK
result = err.E_NONE
elseif 1 == check_state then
-- 系统错误
result = err.E_SYSTEM
elseif 2 == check_state then
-- 不合法升级固件
result = err.E_INVUPFILE
elseif 3 == check_state then
-- 不匹配硬件设备
result = err.E_INVUPHWID
elseif 4 == check_state then
-- 不匹配产品型号
result = err.E_INVUPPRODUCTID
elseif 5 == check_state then
-- 当前设备不满足最低版本要求
result = err.E_INVUPREQVER
elseif 6 == check_state then
-- 当前固件不支持版本降级
result = err.E_INVUPDGVER
else
-- 其他情况
result = err.E_INVUPFILE
end
if result ~= err.E_NONE then
local obj = [[{"Device.X_LANTIQ_COM_Upgrade.Upgrade.4" : { "State" : "UPG_FAIL" }}]]
error, data = libphi_cgi.set_conf(obj)
return result
end
--升级镜像
obj = [[{"action" : "firmware_upg"}]]
error, result = libphi_cgi.do_conf(obj)
if error ~= err.E_NONE then
print("do update state failed")
return err.E_EXPT --should be req upgrade state error code
end
--读取升级结果
obj = [[{"Device.X_LANTIQ_COM_Upgrade.Upgrade.4" : { "State" : null }}]]
error, data = libphi_cgi.get_conf(obj)
if error == err.E_NONE then
--判断升级结果
result = json.decode(data)
if result.Objects[1].Param[1].ParamValue == "UPG_SUCC" then
print("upgrade is success")
else
print("do update failed")
return err.E_EXPT --should do upgrade failed error code
end
else
print("get update state failed")
return err.E_EXPT --should get upgrade state error code
end
--重启
obj = [[{"action":"reboot"}]]
error, result = libphi_cgi.do_conf(obj)
return err.E_NONE
end
--main process--
--check new firmware
local check_upgrade = os.execute("ubus call report check_upgrade > /tmp/check_upgrade")
local retstate
local vernum
local url
local size
local f = io.open("/tmp/check_upgrade", "r")
local t = f:read("*all")
f:close()
os.remove("/tmp/check_upgrade")
if nil ~= t and "" ~= t then
local jsonData = json.decode(t)
if jsonData == nil then
print("Json error")
return -1
end
retstate = jsonData["retLogin"]["retState"]
vernum = jsonData["retLogin"]["VerNum"]
url = jsonData["retLogin"]["URL"]
size = jsonData["retLogin"]["Size"]
end
if (retstate == "1" and vernum == "1") then
--down firmware
os.execute(string.format('/lib/upgrade/fw_dl.sh "%s" "%s"', url, size))
--check and upgrade firmware
auto_system_upgrade()
end |
|