Skip to content

Commit 42cc7ef

Browse files
committed
add: 添加函数fota.file, 从文件读取升级数据
1 parent 00178ae commit 42cc7ef

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

luat/modules/luat_lib_fota.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "luat_fota.h"
1212
#include "luat_zbuff.h"
1313
#include "luat_spi.h"
14+
#include "luat_fs.h"
15+
#include "luat_malloc.h"
16+
1417
#define LUAT_LOG_TAG "fota"
1518
#include "luat_log.h"
1619

@@ -108,6 +111,79 @@ static int l_fota_write(lua_State* L)
108111
return 3;
109112
}
110113

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+
111187
/**
112188
等待底层fota流程完成
113189
@api fota.isDone()
@@ -160,6 +236,7 @@ static const rotable_Reg_t reg_fota[] =
160236
{ "run", ROREG_FUNC(l_fota_write)},
161237
{ "isDone", ROREG_FUNC(l_fota_done)},
162238
{ "finish", ROREG_FUNC(l_fota_end)},
239+
{ "file", ROREG_FUNC(l_fota_file)},
163240
{ NULL, ROREG_INT(0) }
164241
};
165242

0 commit comments

Comments
 (0)