luat_lib_fota.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. @module fota
  3. @summary 底层固件升级
  4. @version core V0007
  5. @date 2022.05.26
  6. */
  7. #include "luat_base.h"
  8. #include "luat_fota.h"
  9. #include "luat_zbuff.h"
  10. #include "luat_spi.h"
  11. #define LUAT_LOG_TAG "fota"
  12. #include "luat_log.h"
  13. /**
  14. 初始化fota流程,目前只有105能使用
  15. @api mcu.fotaInit(storge_location, param1)
  16. @int or string fota数据存储的起始位置,如果是int,则是由芯片平台具体判断,如果是string,则存储在文件系统中,如果为nil,则由底层决定存储位置
  17. @int 数据存储的最大空间
  18. @userdata param1,如果数据存储在spiflash时,为spi_device
  19. @return 成功返回true, 失败返回false
  20. @usage
  21. -- 初始化fota流程
  22. local result = mcu.fotaInit(0, 0x00300000, spi_device) --由于105的flash从0x01000000开始,所以0就是外部spiflash
  23. */
  24. static int l_fota_init(lua_State* L)
  25. {
  26. uint32_t address = 0xffffffff;
  27. size_t len = 0;
  28. uint32_t length;
  29. const char *buf = NULL;
  30. luat_spi_device_t* spi_device = NULL;
  31. if (lua_type(L, 1) == LUA_TSTRING)
  32. {
  33. buf = lua_tolstring(L, 1, &len);//取出字符串数据
  34. }
  35. else
  36. {
  37. address = luaL_optinteger(L, 1, 0xffffffff);
  38. }
  39. length = luaL_optinteger(L, 2, 0);
  40. if (lua_isuserdata(L, 3))
  41. {
  42. spi_device = (luat_spi_device_t*)lua_touserdata(L, 3);
  43. }
  44. lua_pushboolean(L, !luat_fota_init(address, length, spi_device, buf, len));
  45. return 1;
  46. }
  47. /**
  48. 等待底层fota流程准备好,目前只有105能使用
  49. @api mcu.fotaWait()
  50. @boolean 是否完整走完流程,true 表示正确走完流程了
  51. @return
  52. @boolean 准备好返回true
  53. @usage
  54. local isDone = mcu.fotaWait()
  55. */
  56. static int l_fota_wait(lua_State* L)
  57. {
  58. lua_pushboolean(L, luat_fota_wait_ready());
  59. return 1;
  60. }
  61. /**
  62. 写入fota数据,目前只有105能使用
  63. @api mcu.fotaRun(buff)
  64. @zbuff or string fota数据,尽量用zbuff,如果传入的是zbuff,写入成功后,自动清空zbuff内的数据
  65. @return
  66. @boolean 有异常返回true
  67. @boolean 接收到最后一块返回true
  68. @int 还未写入的数据量,超过64K必须做等待
  69. @usage
  70. -- 写入fota流程
  71. local isError, isDone, cache = mcu.fotaRun(buf)
  72. */
  73. static int l_fota_write(lua_State* L)
  74. {
  75. int result;
  76. size_t len;
  77. const char *buf;
  78. if(lua_isuserdata(L, 1))
  79. {
  80. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
  81. result = luat_fota_write(buff->addr, buff->used);
  82. }
  83. else
  84. {
  85. buf = lua_tolstring(L, 1, &len);//取出字符串数据
  86. result = luat_fota_write((uint8_t*)buf, len);
  87. }
  88. if (result > 0)
  89. {
  90. lua_pushboolean(L, 0);
  91. lua_pushboolean(L, 0);
  92. }
  93. else if (result == 0)
  94. {
  95. lua_pushboolean(L, 0);
  96. lua_pushboolean(L, 1);
  97. }
  98. else
  99. {
  100. lua_pushboolean(L, 1);
  101. lua_pushboolean(L, 1);
  102. }
  103. lua_pushinteger(L, result);
  104. return 3;
  105. }
  106. /**
  107. 等待底层fota流程完成,目前只有105能使用
  108. @api mcu.fotaDone()
  109. @boolean 是否完整走完流程,true 表示正确走完流程了
  110. @return
  111. @boolean 有异常返回true
  112. @boolean 写入到最后一块返回true
  113. @usage
  114. local isError, isDone = mcu.fotaDone()
  115. */
  116. static int l_fota_done(lua_State* L)
  117. {
  118. int result = luat_fota_done();
  119. if (result > 0)
  120. {
  121. lua_pushboolean(L, 0);
  122. lua_pushboolean(L, 0);
  123. }
  124. else if (result == 0)
  125. {
  126. lua_pushboolean(L, 0);
  127. lua_pushboolean(L, 1);
  128. }
  129. else
  130. {
  131. lua_pushboolean(L, 1);
  132. lua_pushboolean(L, 1);
  133. }
  134. return 2;
  135. }
  136. /**
  137. 结束fota流程,目前只有105能使用
  138. @api mcu.fotaEnd(is_ok)
  139. @boolean 是否完整走完流程,true 表示正确走完流程了
  140. @return 成功返回true, 失败返回false
  141. @usage
  142. -- 结束fota流程
  143. local result = mcu.fotaEnd(true)
  144. */
  145. static int l_fota_end(lua_State* L)
  146. {
  147. lua_pushboolean(L, !luat_fota_end(lua_toboolean(L, 1)));
  148. return 1;
  149. }
  150. #include "rotable2.h"
  151. static const rotable_Reg_t reg_fota[] =
  152. {
  153. { "init", ROREG_FUNC(l_fota_init)},
  154. { "wait", ROREG_FUNC(l_fota_wait)},
  155. { "run", ROREG_FUNC(l_fota_write)},
  156. { "done", ROREG_FUNC(l_fota_done)},
  157. { "end", ROREG_FUNC(l_fota_end)},
  158. { NULL, ROREG_INT(0) }
  159. };
  160. LUAMOD_API int luaopen_fota( lua_State *L ) {
  161. luat_newlib2(L, reg_fota);
  162. return 1;
  163. }