Просмотр исходного кода

add: airlink,支持自定义发送任意命令,支持发送fota指令

Wendal Chen 11 месяцев назад
Родитель
Сommit
ed2f012f8c

+ 93 - 2
components/airlink/binding/luat_lib_airlink.c

@@ -144,6 +144,93 @@ static int l_airlink_ready(lua_State *L) {
     return 1;
 }
 
+static int l_airlink_cmd(lua_State *L) {
+    size_t len = 0;
+    const char* data = NULL;
+    uint32_t cmd_id = luaL_checkinteger(L, 1);
+    if (lua_type(L, 2) == LUA_TSTRING) {
+        data = luaL_checklstring(L, 1, &len);
+    }
+    else if (lua_isuserdata(L, 1)) {
+        // zbuff
+        luat_zbuff_t* buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
+        data = (const char*)buff->addr;
+        len = buff->used;
+    }
+    else {
+        LLOGE("无效的参数,只能是字符串或者zbuff");
+        return 0;
+    }
+    if (len > 1500) {
+        LLOGE("无效的数据长度,最大1500字节");
+        return 0;
+    }
+    luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
+    cmd->cmd = cmd_id;
+    cmd->len = len;
+    memcpy(cmd->data, data, len);
+    luat_airlink_send2slave(cmd);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
+static int sample_cmd_nodata(lua_State *L, uint32_t cmd_id) {
+    luat_airlink_cmd_t* cmd = luat_airlink_cmd_new(cmd_id, 8);
+    uint64_t pkgid = luat_airlink_get_next_cmd_id();
+    memcpy(cmd->data, &pkgid, 8);
+    luat_airlink_send2slave(cmd);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
+static int l_airlink_sfota_init(lua_State *L) {
+    LLOGD("执行sfota_init");
+    return sample_cmd_nodata(L, 0x04);
+}
+
+static int l_airlink_sfota_done(lua_State *L) {
+    LLOGD("执行sfota_done");
+    return sample_cmd_nodata(L, 0x06);
+}
+
+static int l_airlink_sfota_end(lua_State *L) {
+    LLOGD("执行sfota_end");
+    return sample_cmd_nodata(L, 0x07);
+}
+
+static int l_airlink_sfota_write(lua_State *L) {
+    LLOGD("执行sfota_write");
+    size_t len = 0;
+    const char* data = NULL;
+    if (lua_type(L, 1) == LUA_TSTRING) {
+        data = luaL_checklstring(L, 1, &len);
+    }
+    else if (lua_isuserdata(L, 1)) {
+        // zbuff
+        luat_zbuff_t* buff = ((luat_zbuff_t *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE));
+        data = (const char*)buff->addr;
+        len = buff->used;
+    }
+    else {
+        LLOGE("无效的参数,只能是字符串或者zbuff");
+        return 0;
+    }
+    if (len > 1500) {
+        LLOGE("无效的数据长度,最大1500字节");
+        return 0;
+    }
+    luat_airlink_cmd_t* cmd = luat_heap_opt_malloc(AIRLINK_MEM_TYPE, sizeof(luat_airlink_cmd_t) + len);
+    cmd->cmd = 0x05;
+    cmd->len = len;
+    memcpy(cmd->data, data, len);
+    luat_airlink_send2slave(cmd);
+    luat_heap_opt_free(AIRLINK_MEM_TYPE, cmd);
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
 #include "rotable2.h"
 static const rotable_Reg_t reg_airlink[] =
 {
@@ -153,11 +240,15 @@ static const rotable_Reg_t reg_airlink[] =
     { "ready",         ROREG_FUNC(l_airlink_ready)},
     { "test",          ROREG_FUNC(l_airlink_test )},
     { "sdata",         ROREG_FUNC(l_airlink_sdata)},
+    { "cmd",           ROREG_FUNC(l_airlink_cmd)},
     { "statistics",    ROREG_FUNC(l_airlink_statistics )},
-    { "slave_reboot",   ROREG_FUNC(l_airlink_slave_reboot )},
+    { "slave_reboot",  ROREG_FUNC(l_airlink_slave_reboot )},
 
     // 测试用的fota指令
-    // { "fota",          ROREG_FUNC(l_airlink_fota )},
+    { "sfota_init",     ROREG_FUNC(l_airlink_sfota_init )},
+    { "sfota_write",    ROREG_FUNC(l_airlink_sfota_write )},
+    { "sfota_end",      ROREG_FUNC(l_airlink_sfota_end )},
+    { "sfota_done",     ROREG_FUNC(l_airlink_sfota_done )},
 
     { "MODE_SPI_SLAVE",    ROREG_INT(0) },
     { "MODE_SPI_MASTER",   ROREG_INT(1) },

+ 8 - 0
components/airlink/src/exec/luat_airlink_cmd_exec_basic.c

@@ -63,6 +63,14 @@ int luat_airlink_cmd_exec_fota_done(luat_airlink_cmd_t *cmd, void *userdata)
     return 0;
 }
 
+int luat_airlink_cmd_exec_fota_end(luat_airlink_cmd_t *cmd, void *userdata)
+{
+    LLOGD("收到FOTA传输完毕指令!!!");
+    int ret = luat_fota_end(1);
+    LLOGD("fota_write ret %d", ret);
+    return 0;
+}
+
 #ifdef __LUATOS__
 #include "luat_msgbus.h"
 

+ 2 - 0
components/airlink/src/luat_airlink_cmds.c

@@ -24,6 +24,7 @@ CMD_DEFINE(reset);
 CMD_DEFINE(fota_init);
 CMD_DEFINE(fota_write);
 CMD_DEFINE(fota_done);
+CMD_DEFINE(fota_end);
 CMD_DEFINE(dev_info);
 CMD_DEFINE(sdata);
 CMD_DEFINE(nop);
@@ -63,6 +64,7 @@ __USER_FUNC_IN_RAM__ const luat_airlink_cmd_reg_t airlink_cmds[] = {
     CMD_REG(0x04, fota_init),
     CMD_REG(0x05, fota_write),
     CMD_REG(0x06, fota_done),
+    CMD_REG(0x07, fota_end),
 #endif
 
 #ifdef LUAT_USE_AIRLINK_EXEC_WIFI