|
11 | 11 | #include "luat_fota.h"
|
12 | 12 | #include "luat_zbuff.h"
|
13 | 13 | #include "luat_spi.h"
|
| 14 | +#include "luat_fs.h" |
| 15 | +#include "luat_malloc.h" |
| 16 | + |
14 | 17 | #define LUAT_LOG_TAG "fota"
|
15 | 18 | #include "luat_log.h"
|
16 | 19 |
|
@@ -108,6 +111,79 @@ static int l_fota_write(lua_State* L)
|
108 | 111 | return 3;
|
109 | 112 | }
|
110 | 113 |
|
| 114 | +/** |
| 115 | +从指定文件读取fota数据 |
| 116 | +@api fota.file(path) |
| 117 | +@string 文件路径 |
| 118 | +@return boolean 有异常返回false,无异常返回true |
| 119 | +@return boolean 接收到最后一块返回true |
| 120 | +@return int 还未写入的数据量,超过64K必须做等待 |
| 121 | +@usage |
| 122 | +local result, isDone, cache = fota.file("/xxx.bin") -- 写入fota流程 |
| 123 | +-- 本API于2023.03.23 添加 |
| 124 | +*/ |
| 125 | +static int l_fota_file(lua_State* L) |
| 126 | +{ |
| 127 | + int result = 0; |
| 128 | + const char *path = luaL_checkstring(L, 1); |
| 129 | + FILE* fd = luat_fs_fopen(path, "rb"); |
| 130 | + if (fd == NULL) { |
| 131 | + LLOGE("no such file for FOTA %s", path); |
| 132 | + lua_pushboolean(L, 0); |
| 133 | + lua_pushboolean(L, 0); |
| 134 | + lua_pushinteger(L, 0); |
| 135 | + return 3; |
| 136 | + } |
| 137 | + #define BUFF_SIZE (4096) |
| 138 | + char buff = luat_heap_malloc(BUFF_SIZE); |
| 139 | + if (buff == NULL) { |
| 140 | + luat_fs_fclose(fd); |
| 141 | + LLOGE("out of memory when reading file %s", path); |
| 142 | + lua_pushboolean(L, 0); |
| 143 | + lua_pushboolean(L, 0); |
| 144 | + lua_pushinteger(L, 0); |
| 145 | + return 3; |
| 146 | + } |
| 147 | + size_t len = 0; |
| 148 | + while (1) { |
| 149 | + len = luat_fs_fread(buff + len, BUFF_SIZE - len, 1, fd); |
| 150 | + if (len < 1) { |
| 151 | + // EOF 结束了 |
| 152 | + break; |
| 153 | + } |
| 154 | + result = luat_fota_write((uint8_t*)buff, len); |
| 155 | + if (result < 0) { |
| 156 | + break; |
| 157 | + } |
| 158 | + result = len; |
| 159 | + if (len >= BUFF_SIZE) { |
| 160 | + LLOGD("too many data to write! bug??"); |
| 161 | + result = -7; |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + luat_heap_free(buff); |
| 166 | + luat_fs_fclose(fd); |
| 167 | + |
| 168 | + if (result > 0) |
| 169 | + { |
| 170 | + lua_pushboolean(L, 1); |
| 171 | + lua_pushboolean(L, 0); |
| 172 | + } |
| 173 | + else if (result == 0) |
| 174 | + { |
| 175 | + lua_pushboolean(L, 1); |
| 176 | + lua_pushboolean(L, 1); |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + lua_pushboolean(L, 0); |
| 181 | + lua_pushboolean(L, 1); |
| 182 | + } |
| 183 | + lua_pushinteger(L, result); |
| 184 | + return 3; |
| 185 | +} |
| 186 | + |
111 | 187 | /**
|
112 | 188 | 等待底层fota流程完成
|
113 | 189 | @api fota.isDone()
|
@@ -160,6 +236,7 @@ static const rotable_Reg_t reg_fota[] =
|
160 | 236 | { "run", ROREG_FUNC(l_fota_write)},
|
161 | 237 | { "isDone", ROREG_FUNC(l_fota_done)},
|
162 | 238 | { "finish", ROREG_FUNC(l_fota_end)},
|
| 239 | + { "file", ROREG_FUNC(l_fota_file)}, |
163 | 240 | { NULL, ROREG_INT(0) }
|
164 | 241 | };
|
165 | 242 |
|
|
0 commit comments