Update.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. --- 模块功能:远程升级.
  2. -- 参考 http://ask.openluat.com/article/916 加深对远程升级功能的理解
  3. -- @module update
  4. -- @author openLuat
  5. -- @license MIT
  6. -- @copyright openLuat
  7. -- @release 2018.03.29
  8. -- 注意:实际升级是在系统启动时完成,update.lua是负责下载升级文件的
  9. require "misc"
  10. require "http"
  11. require "log"
  12. require "common"
  13. module(..., package.seeall)
  14. ---------------------------------------------
  15. local filename = "stm8s.bin"
  16. local update_filesize = 0
  17. ----------------------------------------------
  18. --升级包保存路径
  19. local UPDATE_PATH = "/update.bin"
  20. -- @param
  21. -- result true或者false,true表示成功收到了服务器的应答
  22. -- prompt string类型,result为true时,表示服务器的应答码
  23. -- head table类型,表示服务器的应答头
  24. -- filePath string类型,如果调用request接口时传入了rcvFileName,此参数表示下载文件的完整路径
  25. local function cbFncFile(result,prompt,head,filePath)
  26. log.info("testHttp.cbFncFile",result,prompt,filePath)
  27. if result and head then
  28. for k,v in pairs(head) do -- 泛型for循环打印服务器的应答头
  29. log.info("testHttp.cbFncFile",k..": "..v)
  30. end
  31. end
  32. if result and filePath then
  33. local size = io.fileSize(filePath)
  34. log.info("testHttp.cbFncFile","fileSize="..size) -- 文件大小
  35. end
  36. local fileHandle = io.open("/update.bin","r")
  37. if not fileHandle then
  38. log.error("OPEN FAILED!!")
  39. else
  40. if rtos.fota_start() == 0 then
  41. log.info("打开写FBF分区成功!!!")
  42. local pack = 1
  43. local dataLen = io.fileSize(filePath) -- 升级文件大小(单位:byte)
  44. while true do
  45. if pack == 1 then
  46. print(fileHandle:seek("set")) -- 设置文件位置(从文件头开始)
  47. end
  48. print(fileHandle:seek("cur")) -- 上次读取数据的位置
  49. local data = fileHandle:read(1024)
  50. if data == nil then log.info("File send ok and Flie is *K!!!") break end -- 升级文件是整K时,才会执行到这行
  51. print(fileHandle:seek("cur")) -- 下次读取数据的位置
  52. if rtos.fota_process(data,dataLen) == 0 then
  53. log.info("write FBF OK!!!")
  54. else
  55. log.info("write FBF failed!!!")
  56. end
  57. if data:len() < 1024 then break end -- 写入FBF分区完毕
  58. pack = pack + 1
  59. end
  60. if rtos.fota_end() == 0 then
  61. fileHandle:close()
  62. log.info("关闭写FBF分区成功!!!")
  63. pack = 1
  64. log.info("write FBF end!!!")
  65. os.remove("/update.bin")
  66. sys.restart("Device update successful then restart!!!!!")
  67. end
  68. else
  69. log.info("打开写FBF分区失败!!!")
  70. end
  71. end
  72. end
  73. --- 启动远程升级功能
  74. function run()
  75. log.info("开始升级!!!!")
  76. log.info("start update",rtos.get_fs_free_size())
  77. local fileHandle = io.open("/update.bin","rb")
  78. if fileHandle then
  79. fileHandle:close()
  80. log.info("file ----------close!!!!")
  81. os.remove("/update.bin")
  82. log.info("start update",rtos.get_fs_free_size())
  83. end
  84. local url = "/update/firmware_upgrade?imei=" .. misc.getImei() .. "&device_key=" .. misc.getSn().. "&firmware_name=" .. _G.PROJECT .. "&core_version" .. rtos.get_version() .. "&version=" .. _G.VERSION
  85. http.request("GET","8.136.98.49:8002"..url,nil,nil,nil,30000,cbFncFile,"update.bin")
  86. end
  87. --********************************************************************************--
  88. --取回文件大小
  89. local function getFilesize()
  90. local s = io.fileSize("/stm8s.bin")
  91. return s
  92. end
  93. --字符串转16进制
  94. local function bin2hex(s)
  95. s = string.gsub(s,"(.)",function(x) return string.format("%s",string.byte(x)) end)
  96. return s
  97. end
  98. --校验
  99. local function checksum(data_64)
  100. local i=0
  101. local idx=1
  102. local sum={0}
  103. local c_sum = 0
  104. if #data_64 == nil or #data_64==0 then return end
  105. --log.info("getdata len,value:", string.toHex(data_64, " ")) -- 将返回的数据填充到result表
  106. for i = 1,data_64:len() do
  107. idx,sum[i] = pack.unpack(data_64, "b", idx)
  108. --log.info("++++++++++++sum++++++++++++++:",sum[i])
  109. c_sum = c_sum +sum[i]
  110. end
  111. --c_sum = string.format("%x",c_sum) --转换为16进制
  112. -- c_sum = tonumber(c_sum) --字符串转number
  113. c_sum = pack.pack("b",bit.band(c_sum, 0xff))
  114. --print("Check sum and data is====================================","%x",c_sum,string.toHex(data_64, " "))
  115. return c_sum
  116. end
  117. --START APP
  118. local function start_app()
  119. local start_app = string.char(0xB0)
  120. uart.write(1,start_app)
  121. end
  122. --连接开始
  123. --AA 42 4F 4F 54 34 0D 0A
  124. local function connect()
  125. local result,d
  126. local timeout = 0
  127. local start_data = string.char(0xAA,0x42,0x4F,0x4F,0x54,0x34)
  128. while true do
  129. uart.write(1,start_data)
  130. log.info("----------------------connect:",string.toHex(start_data))
  131. timeout = timeout + 1
  132. if timeout == 50 then break end
  133. result,d =sys.waitUntil("YM_IS_OK",1000)
  134. if d and d:find('C') then
  135. log.info("********************************connect ok!!!*****************************")
  136. break
  137. end
  138. end
  139. end
  140. --设置Flash开始地址
  141. local function Flash_start()
  142. -- 开始0x880000
  143. local start_data = string.char(0xA0,0x00,0x00,0x88,0x00,0x88)
  144. local timeout = 0
  145. while true do
  146. uart.write(1,start_data)
  147. log.info("----------------------flash set:",string.toHex(start_data))
  148. timeout = timeout + 1
  149. if timeout == 20 then break end
  150. result,d =sys.waitUntil("YM_IS_OK",1000)
  151. if d and d:find('S') then
  152. log.info("********************************connect ok!!!*****************************")
  153. break
  154. end
  155. end
  156. end
  157. local function xmodem(data)
  158. log.warn("start trans data!!!")
  159. local data = string.char(0xA1)..data..string.rep(string.char(0),64-data:len())..checksum(data)
  160. return data
  161. end
  162. --发某个数据,每次最多发送30次
  163. local function sendData(s)
  164. local result, d
  165. local cnt = 0
  166. uart.write(1,s)
  167. while true do
  168. log.info("send data value,len:", string.toHex(s, " "))
  169. cnt = cnt + 1
  170. d = ""
  171. result, d =sys.waitUntil("YM_IS_OK",1000)
  172. -- local d = uart.read(1,"*l")
  173. -- if s == string.char(EOT) and d~=nil then log.warn("will end!!!",d) uart.write(2,s) end
  174. if d and d:find("W") then return true end--收到了
  175. if d and not d:find("W") then uart.write(1,s) end
  176. if d and d:len() == 0 and cnt == 30 then break end
  177. sys.wait(300)
  178. end
  179. end
  180. -- 清空串口的接收缓冲区:
  181. local function flush_uart_tmp()
  182. local clear_uart_tmp = uart.read(1,'*l')
  183. log.error("****************************clear_uart_tmp:",clear_uart_tmp)
  184. end
  185. -- sys.taskInit(
  186. -- function()
  187. -- local result,d
  188. -- local downnum = 20
  189. -- while true do
  190. -- log.warn("20S后开始下载....")
  191. -- for i = 0,downnum do
  192. -- log.warn("start connect stm8s ....",i)
  193. -- log.info("size is ",i,rtos.get_fs_free_size())
  194. -- log.info("RAM_USEAGE", collectgarbage("count") .. " KB")
  195. -- sys.wait(1000)
  196. -- end
  197. -- if scd.stm8_update_sw == 1 then
  198. -- -- 判断升级文件是否存在
  199. -- fileHandle = io.open("/lua/stm8s.bin","rb")
  200. -- if not fileHandle then return end
  201. -- connect()
  202. -- update_filesize = io.fileSize("/lua/stm8s.bin")
  203. -- --设置Flash地址
  204. -- Flash_start()
  205. -- result,d =sys.waitUntil("YM_IS_OK",1000)
  206. -- if d and d:find('S') then
  207. -- log.info("********************************Set Flash addr ok!!!*****************************")
  208. -- end
  209. -- log.info("start download!!!")
  210. -- local pack = 1
  211. -- while true do
  212. -- if pack == 1 then
  213. -- print(fileHandle:seek("set")) --0
  214. -- end
  215. -- print(fileHandle:seek("cur"))--从当前位置开始
  216. -- local data = fileHandle:read(64)
  217. -- if data == nil then log.info("file send ok!!!") break end
  218. -- log.warn("data len",data:len())
  219. -- print(fileHandle:seek("cur"))--从当前位置开始
  220. -- local sd = xmodem(data)
  221. -- sendData(sd)
  222. -- if data:len() < 64 then fileHandle:close() break end --读完了
  223. -- pack = pack +1
  224. -- sys.wait(500)
  225. -- end
  226. -- scd.stm8_update_sw = 0
  227. -- log.info("update success!!!")
  228. -- start_app()
  229. -- end
  230. -- sys.wait(100)
  231. -- end
  232. -- end)
  233. --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