From 42cc7efca7804280c1b6fdb93e0907a3cb9b9efc Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Thu, 23 Mar 2023 22:30:59 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=87=BD=E6=95=B0fota?= =?UTF-8?q?.file,=20=E4=BB=8E=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luat/modules/luat_lib_fota.c | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/luat/modules/luat_lib_fota.c b/luat/modules/luat_lib_fota.c index 6672cd0df..fa07b58a9 100644 --- a/luat/modules/luat_lib_fota.c +++ b/luat/modules/luat_lib_fota.c @@ -11,6 +11,9 @@ #include "luat_fota.h" #include "luat_zbuff.h" #include "luat_spi.h" +#include "luat_fs.h" +#include "luat_malloc.h" + #define LUAT_LOG_TAG "fota" #include "luat_log.h" @@ -108,6 +111,79 @@ static int l_fota_write(lua_State* L) return 3; } +/** +从指定文件读取fota数据 +@api fota.file(path) +@string 文件路径 +@return boolean 有异常返回false,无异常返回true +@return boolean 接收到最后一块返回true +@return int 还未写入的数据量,超过64K必须做等待 +@usage +local result, isDone, cache = fota.file("/xxx.bin") -- 写入fota流程 +-- 本API于2023.03.23 添加 +*/ +static int l_fota_file(lua_State* L) +{ + int result = 0; + const char *path = luaL_checkstring(L, 1); + FILE* fd = luat_fs_fopen(path, "rb"); + if (fd == NULL) { + LLOGE("no such file for FOTA %s", path); + lua_pushboolean(L, 0); + lua_pushboolean(L, 0); + lua_pushinteger(L, 0); + return 3; + } + #define BUFF_SIZE (4096) + char buff = luat_heap_malloc(BUFF_SIZE); + if (buff == NULL) { + luat_fs_fclose(fd); + LLOGE("out of memory when reading file %s", path); + lua_pushboolean(L, 0); + lua_pushboolean(L, 0); + lua_pushinteger(L, 0); + return 3; + } + size_t len = 0; + while (1) { + len = luat_fs_fread(buff + len, BUFF_SIZE - len, 1, fd); + if (len < 1) { + // EOF 结束了 + break; + } + result = luat_fota_write((uint8_t*)buff, len); + if (result < 0) { + break; + } + result = len; + if (len >= BUFF_SIZE) { + LLOGD("too many data to write! bug??"); + result = -7; + break; + } + } + luat_heap_free(buff); + luat_fs_fclose(fd); + + if (result > 0) + { + lua_pushboolean(L, 1); + lua_pushboolean(L, 0); + } + else if (result == 0) + { + lua_pushboolean(L, 1); + lua_pushboolean(L, 1); + } + else + { + lua_pushboolean(L, 0); + lua_pushboolean(L, 1); + } + lua_pushinteger(L, result); + return 3; +} + /** 等待底层fota流程完成 @api fota.isDone() @@ -160,6 +236,7 @@ static const rotable_Reg_t reg_fota[] = { "run", ROREG_FUNC(l_fota_write)}, { "isDone", ROREG_FUNC(l_fota_done)}, { "finish", ROREG_FUNC(l_fota_end)}, + { "file", ROREG_FUNC(l_fota_file)}, { NULL, ROREG_INT(0) } };