diff --git a/client/scripting.c b/client/scripting.c index 45451b241..e531acd97 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -303,7 +303,6 @@ static int l_GetFromFlashMem(lua_State *L) { } } - /** * @brief The following params expected: * uint8_t *destfilename @@ -312,51 +311,51 @@ static int l_GetFromFlashMem(lua_State *L) { */ static int l_GetFromFlashMemSpiffs(lua_State *L) { - if (IfPm3Flash()) { - uint32_t start_index = 0, len = 0x40000; //FLASH_MEM_MAX_SIZE - char destfilename[32] = {0}; - size_t size; - - int n = lua_gettop(L); - if (n == 0) - return returnToLuaWithError(L, "You need to supply the destination filename"); - - if (n >= 1) { - const char *p_filename = luaL_checklstring(L, 1, &size); - if (size != 0) - memcpy(destfilename, p_filename, 31); - } - - if (destfilename[0] == '\0') - return returnToLuaWithError(L, "Filename missing or invalid"); - - // get size from spiffs itself ! - SendCommandMIX(CMD_SPIFFS_STAT, 0, 0, 0, (uint8_t *)destfilename, 32); - PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) - return returnToLuaWithError(L, "No response from the device"); - - len = resp.oldarg[0]; - - if (len <= 0) - return returnToLuaWithError(L, "Filename invalid or empty"); - - uint8_t *data = calloc(len, sizeof(uint8_t)); - if (!data) - return returnToLuaWithError(L, "Allocating memory failed"); - - if (!GetFromDevice(SPIFFS, data, len, start_index, (uint8_t *)destfilename, 32, NULL, -1, true)) { - free(data); - return returnToLuaWithError(L, "ERROR; downloading from spiffs(flashmemory)"); - } - - lua_pushlstring(L, (const char *)data, len); - lua_pushunsigned(L, len); - free(data); - return 2; - } else { + if (IfPm3Flash() == false) { return returnToLuaWithError(L, "No FLASH MEM support"); } + + uint32_t start_index = 0, len = 0x40000; //FLASH_MEM_MAX_SIZE + char destfilename[32] = {0}; + size_t size; + + int n = lua_gettop(L); + if (n == 0) + return returnToLuaWithError(L, "You need to supply the destination filename"); + + if (n >= 1) { + const char *p_filename = luaL_checklstring(L, 1, &size); + if (size != 0) + memcpy(destfilename, p_filename, 31); + } + + if (destfilename[0] == '\0') + return returnToLuaWithError(L, "Filename missing or invalid"); + + // get size from spiffs itself ! + SendCommandMIX(CMD_SPIFFS_STAT, 0, 0, 0, (uint8_t *)destfilename, 32); + PacketResponseNG resp; + if (!WaitForResponseTimeout(CMD_ACK, &resp, 2000)) + return returnToLuaWithError(L, "No response from the device"); + + len = resp.oldarg[0]; + + if (len == 0) + return returnToLuaWithError(L, "Filename invalid or empty"); + + uint8_t *data = calloc(len, sizeof(uint8_t)); + if (!data) + return returnToLuaWithError(L, "Allocating memory failed"); + + if (!GetFromDevice(SPIFFS, data, len, start_index, (uint8_t *)destfilename, 32, NULL, -1, true)) { + free(data); + return returnToLuaWithError(L, "ERROR; downloading from spiffs(flashmemory)"); + } + + lua_pushlstring(L, (const char *)data, len); + lua_pushunsigned(L, len); + free(data); + return 2; } /**