| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- --- 模块功能:远程升级.
- -- 参考 http://ask.openluat.com/article/916 加深对远程升级功能的理解
- -- @module update
- -- @author openLuat
- -- @license MIT
- -- @copyright openLuat
- -- @release 2018.03.29
- -- 注意:实际升级是在系统启动时完成,update.lua是负责下载升级文件的
- require "misc"
- require "http"
- require "log"
- require "common"
- module(..., package.seeall)
- ---------------------------------------------
- local filename = "stm8s.bin"
- local update_filesize = 0
- ----------------------------------------------
- --升级包保存路径
- local UPDATE_PATH = "/update.bin"
- -- @param
- -- result true或者false,true表示成功收到了服务器的应答
- -- prompt string类型,result为true时,表示服务器的应答码
- -- head table类型,表示服务器的应答头
- -- filePath string类型,如果调用request接口时传入了rcvFileName,此参数表示下载文件的完整路径
- local function cbFncFile(result,prompt,head,filePath)
- log.info("testHttp.cbFncFile",result,prompt,filePath)
- if result and head then
- for k,v in pairs(head) do -- 泛型for循环打印服务器的应答头
- log.info("testHttp.cbFncFile",k..": "..v)
- end
- end
- if result and filePath then
- local size = io.fileSize(filePath)
- log.info("testHttp.cbFncFile","fileSize="..size) -- 文件大小
- end
- local fileHandle = io.open("/update.bin","r")
- if not fileHandle then
- log.error("OPEN FAILED!!")
- else
- if rtos.fota_start() == 0 then
- log.info("打开写FBF分区成功!!!")
- local pack = 1
- local dataLen = io.fileSize(filePath) -- 升级文件大小(单位:byte)
- while true do
- if pack == 1 then
- print(fileHandle:seek("set")) -- 设置文件位置(从文件头开始)
- end
- print(fileHandle:seek("cur")) -- 上次读取数据的位置
- local data = fileHandle:read(1024)
- if data == nil then log.info("File send ok and Flie is *K!!!") break end -- 升级文件是整K时,才会执行到这行
- print(fileHandle:seek("cur")) -- 下次读取数据的位置
- if rtos.fota_process(data,dataLen) == 0 then
- log.info("write FBF OK!!!")
- else
- log.info("write FBF failed!!!")
- end
- if data:len() < 1024 then break end -- 写入FBF分区完毕
- pack = pack + 1
- end
- if rtos.fota_end() == 0 then
- fileHandle:close()
- log.info("关闭写FBF分区成功!!!")
- pack = 1
- log.info("write FBF end!!!")
- os.remove("/update.bin")
- sys.restart("Device update successful then restart!!!!!")
- end
- else
- log.info("打开写FBF分区失败!!!")
- end
- end
- end
- --- 启动远程升级功能
- function run()
- log.info("开始升级!!!!")
- log.info("start update",rtos.get_fs_free_size())
- local fileHandle = io.open("/update.bin","rb")
- if fileHandle then
- fileHandle:close()
- log.info("file ----------close!!!!")
- os.remove("/update.bin")
- log.info("start update",rtos.get_fs_free_size())
- end
- local url = "/update/firmware_upgrade?imei=" .. misc.getImei() .. "&device_key=" .. misc.getSn().. "&firmware_name=" .. _G.PROJECT .. "&core_version" .. rtos.get_version() .. "&version=" .. _G.VERSION
- http.request("GET","8.136.98.49:8002"..url,nil,nil,nil,30000,cbFncFile,"update.bin")
- end
- --********************************************************************************--
- --取回文件大小
- local function getFilesize()
- local s = io.fileSize("/stm8s.bin")
- return s
- end
- --字符串转16进制
- local function bin2hex(s)
- s = string.gsub(s,"(.)",function(x) return string.format("%s",string.byte(x)) end)
- return s
- end
- --校验
- local function checksum(data_64)
- local i=0
- local idx=1
- local sum={0}
- local c_sum = 0
- if #data_64 == nil or #data_64==0 then return end
- --log.info("getdata len,value:", string.toHex(data_64, " ")) -- 将返回的数据填充到result表
- for i = 1,data_64:len() do
- idx,sum[i] = pack.unpack(data_64, "b", idx)
- --log.info("++++++++++++sum++++++++++++++:",sum[i])
-
- c_sum = c_sum +sum[i]
- end
- --c_sum = string.format("%x",c_sum) --转换为16进制
- -- c_sum = tonumber(c_sum) --字符串转number
- c_sum = pack.pack("b",bit.band(c_sum, 0xff))
- --print("Check sum and data is====================================","%x",c_sum,string.toHex(data_64, " "))
- return c_sum
-
- end
- --START APP
- local function start_app()
- local start_app = string.char(0xB0)
- uart.write(1,start_app)
- end
- --连接开始
- --AA 42 4F 4F 54 34 0D 0A
- local function connect()
- local result,d
- local timeout = 0
- local start_data = string.char(0xAA,0x42,0x4F,0x4F,0x54,0x34)
- while true do
- uart.write(1,start_data)
- log.info("----------------------connect:",string.toHex(start_data))
- timeout = timeout + 1
- if timeout == 50 then break end
- result,d =sys.waitUntil("YM_IS_OK",1000)
- if d and d:find('C') then
- log.info("********************************connect ok!!!*****************************")
- break
- end
- end
- end
- --设置Flash开始地址
- local function Flash_start()
- -- 开始0x880000
- local start_data = string.char(0xA0,0x00,0x00,0x88,0x00,0x88)
- local timeout = 0
- while true do
- uart.write(1,start_data)
- log.info("----------------------flash set:",string.toHex(start_data))
- timeout = timeout + 1
- if timeout == 20 then break end
- result,d =sys.waitUntil("YM_IS_OK",1000)
- if d and d:find('S') then
- log.info("********************************connect ok!!!*****************************")
- break
- end
- end
- end
- local function xmodem(data)
- log.warn("start trans data!!!")
- local data = string.char(0xA1)..data..string.rep(string.char(0),64-data:len())..checksum(data)
- return data
- end
- --发某个数据,每次最多发送30次
- local function sendData(s)
- local result, d
- local cnt = 0
- uart.write(1,s)
- while true do
- log.info("send data value,len:", string.toHex(s, " "))
- cnt = cnt + 1
- d = ""
- result, d =sys.waitUntil("YM_IS_OK",1000)
- -- local d = uart.read(1,"*l")
- -- if s == string.char(EOT) and d~=nil then log.warn("will end!!!",d) uart.write(2,s) end
- if d and d:find("W") then return true end--收到了
- if d and not d:find("W") then uart.write(1,s) end
- if d and d:len() == 0 and cnt == 30 then break end
- sys.wait(300)
- end
- end
- -- 清空串口的接收缓冲区:
- local function flush_uart_tmp()
- local clear_uart_tmp = uart.read(1,'*l')
- log.error("****************************clear_uart_tmp:",clear_uart_tmp)
- end
- -- sys.taskInit(
- -- function()
- -- local result,d
- -- local downnum = 20
- -- while true do
- -- log.warn("20S后开始下载....")
- -- for i = 0,downnum do
- -- log.warn("start connect stm8s ....",i)
- -- log.info("size is ",i,rtos.get_fs_free_size())
- -- log.info("RAM_USEAGE", collectgarbage("count") .. " KB")
- -- sys.wait(1000)
- -- end
- -- if scd.stm8_update_sw == 1 then
- -- -- 判断升级文件是否存在
- -- fileHandle = io.open("/lua/stm8s.bin","rb")
- -- if not fileHandle then return end
- -- connect()
- -- update_filesize = io.fileSize("/lua/stm8s.bin")
-
- -- --设置Flash地址
- -- Flash_start()
- -- result,d =sys.waitUntil("YM_IS_OK",1000)
- -- if d and d:find('S') then
- -- log.info("********************************Set Flash addr ok!!!*****************************")
- -- end
- -- log.info("start download!!!")
- -- local pack = 1
- -- while true do
- -- if pack == 1 then
- -- print(fileHandle:seek("set")) --0
- -- end
- -- print(fileHandle:seek("cur"))--从当前位置开始
- -- local data = fileHandle:read(64)
- -- if data == nil then log.info("file send ok!!!") break end
- -- log.warn("data len",data:len())
- -- print(fileHandle:seek("cur"))--从当前位置开始
- -- local sd = xmodem(data)
- -- sendData(sd)
- -- if data:len() < 64 then fileHandle:close() break end --读完了
- -- pack = pack +1
- -- sys.wait(500)
- -- end
- -- scd.stm8_update_sw = 0
- -- log.info("update success!!!")
- -- start_app()
- -- end
- -- sys.wait(100)
- -- end
- -- end)
- --A1 93 90 EE 03 FE F6 90 F7 90 5C 5C B3 9D 26 F6 A6 01 5F 97 81 B6 99 F1 26 1A B6 9A E1 01 26 0C B6 9B E1 02 26 06 B6 9C E1 03 27 08 9C 24 03 A6 FF 81 A6 01 81 E6 03 88 E6 02 88 FE 89 1E 03 B6 99 F3 F1 F2
|