diff --git a/firmware/storage_init.c b/firmware/storage_init.c deleted file mode 100644 index 8e40e0e36..000000000 --- a/firmware/storage_init.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2022 Mesh4all - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @brief Storage_initialization_setup - * - * @author luisan00 - * - */ -#include -#include -#include - -#include "storage.h" -#include "default_params.h" - -#if MODULE_RPL_PROTOCOL -#include "rpl_protocol.h" -int set_rpl_params(storage_data_t data); -#endif -#if MODULE_RADIO -#include "radio.h" -int set_radio_params(storage_data_t data); -#endif - -void load_settings(void) { - storage_data_t data; - if (mtd_dump() < 0) { - printf("Is not there settings parameters saved"); - return; - } - mtd_load(&data, sizeof(data)); -#if MODULE_RADIO - if (set_radio_params(data) < 0) { - printf("Radio params wrong set\n"); - } -#endif -#if MODULE_RPL_PROTOCOL - if (set_rpl_params(data) < 0) { - printf("RPL params wrong set\n"); - } -#endif - return; -} - -#if MODULE_RADIO -int set_radio_params(storage_data_t data) { - if (set_netopt_tx_power(data.radio_tx_power) < 0) { - return -1; - } - if (set_netopt_channel(data.channel) < 0) { - return -1; - } - return 0; -} -#endif - -#if MODULE_RPL_PROTOCOL -int set_rpl_params(storage_data_t data) { - rpl_setup(data.rpl_mode); - return 0; -} -#endif diff --git a/firmware/sys/storage/include/storage.h b/firmware/sys/storage/include/storage.h index 4fd3a4d68..a7a575e11 100644 --- a/firmware/sys/storage/include/storage.h +++ b/firmware/sys/storage/include/storage.h @@ -29,7 +29,6 @@ #ifndef STORAGE_H #define STORAGE_H -#include #include "mtd_flashpage.h" #ifdef __cplusplus @@ -37,15 +36,25 @@ extern "C" { #endif #define LAST_AVAILABLE_PAGE (FLASHPAGE_NUMOF - 1) /*!< Last position in the block EEPROM*/ -#define MAX_SIZE_STORAGE (FLASHPAGE_SIZE) /*!< max size to save in the page */ - -#define MAX_NUMOF_FLASHPAGES (FLASHPAGE_NUMOF) /*!< max num of pages that can be manipulated */ - -#if CPU_SAMD21A -#undef MAX_NUMOF_FLASHPAGES -#define EEPROM_SIZE (FLASHPAGE_SIZE * FLASHPAGE_PAGES_PER_ROW * 64) -#define MAX_NUMOF_FLASHPAGES (EEPROM_SIZE / FLASHPAGE_SIZE) -#endif +#define MAX_SIZE_STORAGE (4096) /*!< max size of mtd_storage, only writeable 4K */ +#define MAX_NUMOF_FLASHPAGES \ + (MAX_SIZE_STORAGE / FLASHPAGE_SIZE) /*!< max num of pages that can be manipulated */ + +#define MTD_START_ADDR \ + ((uint32_t)(flashpage_addr(LAST_AVAILABLE_PAGE)) - \ + MAX_SIZE_STORAGE) /*!< Reference to the first address writeable*/ +#define MTD_LAST_ADDR \ + (uint32_t)(flashpage_addr(LAST_AVAILABLE_PAGE)) /*!< Reference to the last address writeable*/ +#define MTD_REGISTER_INDEX_LIMIT (512) /*!< Reference to max number of bytes to add an register*/ + +typedef struct { + uint16_t size; + uint8_t key[25]; + uint8_t rwp; + uint32_t ptr_content; +} mtd_register_t; + +#define MTD_REG_IDX_NUMOF (MTD_REGISTER_INDEX_LIMIT / sizeof(mtd_register_t)) /*!< */ /** @note The storage EEPROM section page could be resize with the bootloader block size * @warning Always the block EEPROM and BOOTLOADER are affected between them. @@ -56,126 +65,88 @@ extern "C" { * @return 0 Satisfactory result * -1 Failed result */ -int mtd_start(void); +int8_t mtd_start(void); /** - * @brief This function is executed by mtd write_string and mtd_write_uint8 internally. It is not - * recommended to use it directly, use the functions mentioned + * @brief Saves any value in any position of mtd_storage. * - * @param [in] key this is the address where will be saved the data in the memory - * @param [out] value value to save - * @retval 0 Satisfactory result - * @retval -1 Failed result + * @param [in] value Any type of value. + * @param [in] len size of @p value that will be saved. + * @param [in] offset reference to an position in mtd_storage. + * @return int8_t */ -int mtd_save(uint32_t key, void *value); +int mtd_save(void *value, uint32_t len, uint32_t offset); /** - * @brief This function is executed to save any data type or any strutc, The function saves all the - * information compressed in EEPROM/FLASH_MEMORY. The data is stored in each page of the - * EEPROM/MEMORY_FLASH section. - * @warning: The EEPROM/FLASH_MEMORY section size depends of itself hardware. - * - * @param [in] value The input value given could be any datatype or data struct - * @param [in] len size of the all elements in the container of @p value - * @retval 0 Satisfactory result - * @retval -1 Failed result + * @brief Loads any value in any position of mtd_storage. + * + * @param [out] value Any type of value. + * @param [in] len size of @p value that will be loaded. + * @param [in] offset reference to an position in mtd_storage. + * @return int8_t */ -int mtd_save_compress(void *value, uint16_t len); +int mtd_load(void *value, uint16_t len, uint32_t offset); /** - * @brief load all data in the storage, depending of the number of bytes ( @p len ) required to read - * from the storage block in the mtd device. - * - * @param [out] value pointer to an struct or variable where will be loaded the data of the - * storage. - * @param [in] len size of the struct or variable @p value - * @retval 0 Satisfactory result - * @retval -1 Failed result + * @brief Saves data using a key as identifier to localize a group of bytes. + * + * @param [in] value Any type of value. + * @param [in] key Identifier to set/get a specified value. + * @param [in] len size of @p value that will be saved. + * @return int8_t */ -int mtd_load(void *value, uint16_t len); +int mtd_save_reg(void *value, uint8_t *key, uint16_t len); /** - * @brief Removes all saved data in mtd_storage. This will erase all until the - * last page since to available flash page to write eeprom. + * @brief Load data from mtd_storage localizating with its knew key and saves in @p value the data. * - * @warning The mtd storage needs to define a flashpage limit after base program - * memory allocation + * @param [out] value Any type of value. + * @param [in] key Identifier to set/get a specified value. + * @param [in] len size of data that will be saved in @p value . * - * @retval 0 Erased data success - * @retval -1 Erased Fail. + * @note @p size is used to check with the data saved in mtd_storage and won't be overflowed the @p + * value parameter. + * @return int8_t */ -int mtd_erase_all(void); +int mtd_load_reg(void *value, uint8_t *key, uint16_t len); /** - * @brief Dump all data inside mtd storage + * @brief Removes all saved data in mtd_storage. This will erase all until the + * last page since to available flash page to write eeprom. * * @warning The mtd storage needs to define a flashpage limit after base program * memory allocation * - * @retval 0 Exists data inside of mtd storage. - * @retval -1 All data inside of mtd is erased. - */ -int mtd_dump(void); - -/** - * @brief Function used to get the strings already saved - * - * @param [in] key This is the address where will be saved the data in the memory - * @param [in] len Length of the data - * @param [out] output Output to save - * @return 0 Satisfactory result - * -1 Failed result - */ -int mtd_read_string(uint32_t key, char *output, size_t len); - -/** - * @brief Function used to get the uint8 values already saved - * - * @param [in] key This is the address where will be saved the data in the memory - * @param [out] output Output to save - * @return 0 Satisfactory result - * -1 Failed result + * @retval 0 Erased data success + * @retval -1 Erased Fail. */ -int mtd_read_u8(uint32_t key, uint8_t *output); +int8_t mtd_erase_all(void); /** - * @brief This function deletes the saved data + * @brief Check the available registers, to write/save data. * - * @param key This is the address where will be removed the data in the memory - * @return 0 Satisfactory result - * -1 Failed result + * @return int8_t */ -int mtd_erase_flashpage(uint32_t key); +int8_t mtd_available_idx(void); /** - * @brief This function is used to write only strings. - * If they are greater than 64 bytes your string will be - * cut and will save only 64 bytes + * @brief Dump all data inside mtd storage * - * @param [in] key This is the address where will be saved the data in the memory - * @param [out] value Value to save - * @return 0 Satisfactory result - * -1 Failed result - */ -int mtd_write_string(uint32_t key, char *value); - -/** - * @brief Function used to write only uint8_t + * @warning The mtd storage needs to define a flashpage limit after base program + * memory allocation * - * @param [in] key This is the address where will be saved the data in the memory - * @param [out] value Value to save - * @return 0 Satisfactory result - * -1 Failed result + * @retval 0 Exists data inside of mtd storage. + * @retval -1 All data inside of mtd is erased. */ -int mtd_write_uint8(uint32_t key, uint8_t *value); +int8_t mtd_dump(void); /** - * @brief Function used to get the length of strings - * - * @param [in] key This is the address of memory + * @brief Dump all data inside an flshpage from mtd_storage * + * @param page + * @return int8_t */ -int mtd_get_string_len(uint32_t key); +int8_t mtd_dump_flashpage(uint16_t page); #ifdef __cplusplus } diff --git a/firmware/sys/storage/storage.c b/firmware/sys/storage/storage.c index 1d5ca4cf9..697b45abc 100644 --- a/firmware/sys/storage/storage.c +++ b/firmware/sys/storage/storage.c @@ -23,11 +23,10 @@ #include #include #include -#include "log.h" -#include "board.h" +#include "errno.h" + #include "mtd.h" #include "storage.h" -#include "mtd_flashpage.h" #if (CONFIG_DEBUG_STORAGE) || (DOXYGEN) /** @@ -43,11 +42,11 @@ static mtd_flashpage_t _dev = MTD_FLASHPAGE_INIT_VAL(8); static mtd_dev_t *dev = &_dev.base; -static void locate_addr(uint32_t *addr, const uint32_t idx_page) { - *addr = (uint32_t)flashpage_addr(LAST_AVAILABLE_PAGE - idx_page); -} +#define MTD_WR_BLOCK_POS(offset) ((offset - MTD_START_ADDR) / dev->write_size) +#define MTD_WR_NUMOF_BLOCK(size, offset) ((size) + (offset)) / (dev->write_size) +#define MTD_LAST_BLOCK_RES(size, offset) (offset) + (size) % (dev->write_size) -int mtd_start(void) { +int8_t mtd_start(void) { int ret = mtd_init(dev); if (ret < 0) { DEBUG("Error to the init mtd \n"); @@ -55,212 +54,236 @@ int mtd_start(void) { return ret; } -int mtd_save(uint32_t key, void *value) { - uint8_t buf[MAX_SIZE_STORAGE]; - memset(buf, 0, sizeof(buf)); - memcpy(buf, value, sizeof(buf)); - int ret = mtd_write(dev, buf, key, sizeof(buf)); - +int mtd_write_block(void *val, uint32_t addr, uint8_t size, uint8_t offset) { + uint8_t buf[dev->write_size]; + int8_t ret = 0; + ret = mtd_read(dev, buf, addr, sizeof(buf)); if (ret < 0) { - DEBUG("Error to the write in mtd %d \n", ret); + DEBUG("Err: Reading a Block before overwrite it\n"); + return ret; } - - return ret; + memcpy(buf + offset, val, size - offset); + ret = mtd_write(dev, buf, addr, dev->write_size); + if (ret < 0) { + DEBUG("Err: Writing in a block\n"); + return ret; + } + return 0; } -int mtd_save_compress(void *value, uint16_t len) { - uint16_t num_pages = len / MAX_SIZE_STORAGE; - uint8_t res_bits = len % MAX_SIZE_STORAGE; +int mtd_read_block(void *value, uint32_t addr, uint8_t size, uint8_t offset) { + uint8_t buf[dev->write_size]; int8_t ret = 0; - uint32_t storage_addr = 0; - if (num_pages < 1 || res_bits != 0) { - num_pages++; - } - DEBUG("numpages : %u\n", num_pages); - if (num_pages >= MAX_NUMOF_FLASHPAGES) { - DEBUG("error: Unavailable Memory to save the required data, file: %s, line: %d, " - "function: %s\n", - __FILE__, __LINE__, __func__); - return -1; - } - for (uint16_t i = 0; i < num_pages; i++) { - locate_addr(&storage_addr, i); - ret = mtd_erase_flashpage(storage_addr); - if (ret != 0) { - DEBUG("error: Failed erasing data: file: %s, line: %d, function: %s\n", __FILE__, - __LINE__, __func__); - return ret; - } - } - for (uint16_t i = 0; i < num_pages; i++) { - locate_addr(&storage_addr, i); - ret = mtd_write(dev, value, storage_addr, MAX_SIZE_STORAGE); - if (ret != 0) { - DEBUG("FAILED SAVING DATA\n"); - return ret; - } - value = (uint8_t*)value +MAX_SIZE_STORAGE; + memset(buf, 0xFF, sizeof(buf)); + ret = mtd_read(dev, buf, addr, dev->write_size); + if (ret < 0) { + DEBUG("Err: Reading a Block\n"); + return ret; } - return ret; + memcpy(value, buf + offset, size); + return 0; } -int mtd_load(void *value, uint16_t len) { - uint16_t diff_size = MAX_SIZE_STORAGE; - uint16_t num_pages = len / MAX_SIZE_STORAGE; - uint8_t res_bits = len % MAX_SIZE_STORAGE; - uint32_t storage_addr = 0; - if ((num_pages < 1) || (res_bits != 0)) { - num_pages++; - } - if (num_pages >= MAX_NUMOF_FLASHPAGES) { - DEBUG("error: Overload Memory size, file: %s, line %d, function: %s", __FILE__, __LINE__, +int mtd_save(void *value, uint32_t len, uint32_t addr) { + uint8_t block_offset = addr % dev->write_size; + uint8_t last_size = MTD_LAST_BLOCK_RES(len, block_offset); + uint32_t storage_addr = MTD_START_ADDR + MTD_WR_BLOCK_POS(addr) * dev->write_size; + if (len + storage_addr >= MTD_LAST_ADDR) { + DEBUG("error: Overload Memory size, file: %s, line %d, function: %s\n", __FILE__, __LINE__, __func__); - return -1; + return -EOVERFLOW; } - for (uint8_t i = 0; i < num_pages; i++) { - if ((i == num_pages - 1) && (res_bits != 0)) { - diff_size = res_bits; + for (uint16_t i = 0; i <= MTD_WR_NUMOF_BLOCK(len, block_offset); i++) { + if (mtd_write_block(value, storage_addr, + i == MTD_WR_NUMOF_BLOCK(len, block_offset) ? last_size + : dev->write_size, + block_offset) < 0) { + DEBUG("error: Writing block, file: %s, line %d, function: %s\n", __FILE__, __LINE__, + __func__); + return -EOVERFLOW; } - locate_addr(&storage_addr, i); - mtd_read(dev, value, storage_addr, diff_size); - value = (uint8_t*)value +MAX_SIZE_STORAGE; + value = (uint8_t *)value + dev->write_size - block_offset; + block_offset = 0; + storage_addr += dev->write_size; } return 0; } -int mtd_erase_all(void) { - uint32_t storage_addr = 0; - for (uint16_t i = 0; i < MAX_NUMOF_FLASHPAGES; i++) { - locate_addr(&storage_addr, i); - mtd_erase(dev, storage_addr, MAX_SIZE_STORAGE); - } - return 0; -} +int mtd_load(void *value, uint16_t len, uint32_t addr) { + uint8_t block_offset = addr % dev->write_size; + uint8_t last_size = MTD_LAST_BLOCK_RES(len, block_offset); + uint32_t storage_addr = MTD_START_ADDR + MTD_WR_BLOCK_POS(addr) * dev->write_size; -int mtd_dump(void) { - uint32_t storage_addr = 0; - uint8_t value[MAX_SIZE_STORAGE]; - uint32_t erased_bytes = 0; - uint32_t total_size = MAX_NUMOF_FLASHPAGES * MAX_SIZE_STORAGE; - DEBUG("Total Flashpáges available for storage: %d\n", MAX_NUMOF_FLASHPAGES); - for (uint16_t i = 0; i < MAX_NUMOF_FLASHPAGES; i++) { - int err; - locate_addr(&storage_addr, i); - err = mtd_read(dev, value, storage_addr, MAX_SIZE_STORAGE); - if (err < 0) { - return err; - } - DEBUG("FLASHPAGE #%d:\t", i); - for (uint16_t j = 0; j < MAX_SIZE_STORAGE; j++) { - DEBUG("%02X ", value[j]); - if (value[j] == 255) { - erased_bytes++; - } - } - DEBUG("\n"); + if (len + addr >= MTD_LAST_ADDR) { + DEBUG("error: Overload Memory size, file: %s, line %d, function: %s\n", __FILE__, __LINE__, + __func__); + return -EOVERFLOW; } - if (total_size == erased_bytes) { - return -1; + for (uint16_t i = 0; i <= MTD_WR_NUMOF_BLOCK(len, block_offset); i++) { + if (mtd_read_block(value, storage_addr, + i == MTD_WR_NUMOF_BLOCK(len, block_offset) ? last_size : dev->write_size, + block_offset) < 0) { + DEBUG("Error: Reading in block, file: %s, line %d, function: %s\n", __FILE__, __LINE__, + __func__); + return -EOVERFLOW; + } + value = (uint8_t *)value + dev->write_size - block_offset; + block_offset = 0; + storage_addr += dev->write_size; } return 0; } -int mtd_read_u8(uint32_t key, uint8_t *output) { - size_t len = MAX_SIZE_STORAGE; - uint8_t buf_read[len]; - memset(buf_read, 0, sizeof(buf_read)); - - int ret = mtd_read(dev, buf_read, key, sizeof(buf_read)); - if (ret < 0) { - DEBUG("Error to read in mtd \n"); +int8_t idx_reg_is_empty(mtd_register_t reg) { + uint8_t empty_reg[sizeof(mtd_register_t)]; +#ifdef BOARD_NATIVE + memset(empty_reg, 0x00, sizeof(empty_reg)); +#else + memset(empty_reg, 0xFF, sizeof(empty_reg)); +#endif + if (memcmp(®, empty_reg, sizeof(reg.size) + sizeof(reg.key)) == 0) { + return 0; } - - *output = buf_read[0]; - - return ret; + return -1; } -int mtd_get_string_len(uint32_t key) { - size_t len = MAX_SIZE_STORAGE; - char buf_read[len]; - memset(buf_read, 0, sizeof(buf_read)); - - int ret = mtd_read(dev, buf_read, key, sizeof(buf_read)); - if (ret < 0) { - DEBUG("Error to read in mtd \n"); +int8_t check_idx_reg(mtd_register_t reg, mtd_register_t buffer) { + if (memcmp(®, &buffer, sizeof(reg.size) + sizeof(reg.key)) == 0) { return 0; } - int count = 0; - for (size_t i = 0; i < len; i++) { - DEBUG("%x ", buf_read[i]); - if (buf_read[i] == 0 && count == 0) { - count = i; - } + if (idx_reg_is_empty(reg) == 0) { + return 1; } - DEBUG("\n "); - return count + 1; + return -1; } -int mtd_read_string(uint32_t key, char *output, size_t len) { +int mtd_save_reg(void *value, uint8_t *key, uint16_t len) { + mtd_register_t buff, mtd_reg = {.size = len}; + memcpy(mtd_reg.key, key, sizeof(mtd_reg.key)); + uint8_t reg_count = 0; + mtd_reg.ptr_content = MTD_START_ADDR + MTD_REGISTER_INDEX_LIMIT; + while (reg_count < MTD_REG_IDX_NUMOF) { + int8_t ret = 0; - size_t _len = len; - char buf_read[_len]; - memset(buf_read, 0, sizeof(buf_read)); - - int ret = mtd_read(dev, buf_read, key, sizeof(buf_read)); - if (ret < 0) { - DEBUG("Error to read in mtd \n"); - } - int count = 0; - for (size_t i = 0; i < len; i++) { - if (buf_read[i] == 0 && count == 0) { - count = i; + int8_t reg_state = 0; + ret = mtd_load(&buff, sizeof(mtd_register_t), + MTD_START_ADDR + (reg_count * sizeof(mtd_register_t))); + if (ret < 0) { + DEBUG("Failed to pre-load indexes, file: %s, line %d, function: %s\n", __FILE__, + __LINE__, __func__); + return ret; } + if (buff.size != 0xffff) { + mtd_reg.ptr_content += buff.size; + } + reg_state = check_idx_reg(buff, mtd_reg); + if (reg_state == 0) { + DEBUG("The index already exist, can´t be updated, file: %s, line %d, function: %s\n", + __FILE__, __LINE__, __func__); + return 1; + } + if (reg_state == 1) { + DEBUG("Empty_register\n"); + ret = mtd_save(&mtd_reg, sizeof(mtd_register_t), + MTD_START_ADDR + (reg_count * sizeof(mtd_register_t))); + if (ret < 0) { + DEBUG("Failed Saving mtd register index, file: %s, line %d, function: %s\n", + __FILE__, __LINE__, __func__); + return ret; + } + ret = mtd_save(value, len, mtd_reg.ptr_content); + if (ret < 0) { + DEBUG("Failed Saving mtd register content, file: %s, line %d, function: %s\n", + __FILE__, __LINE__, __func__); + return ret; + } + return 0; + } + reg_count++; } + return -1; +} - memcpy(output, buf_read, count + 1); +int mtd_load_reg(void *value, uint8_t *key, uint16_t len) { + mtd_register_t buff, mtd_reg = {.size = len}; + memcpy(mtd_reg.key, key, sizeof(mtd_reg.key)); + uint8_t reg_count = 0; + while (reg_count < MTD_REG_IDX_NUMOF) { + int8_t ret = 0; + int8_t reg_state = 0; + ret = mtd_load(&buff, sizeof(mtd_register_t), + reg_count * sizeof(mtd_register_t) + MTD_START_ADDR); + if (ret < 0) { + DEBUG("Err: Failed Reading Registers, file: %s, line %d, function: %s\n", __FILE__, + __LINE__, __func__); + return ret; + } - return ret; -} + reg_state = check_idx_reg(buff, mtd_reg); + if (reg_state == 0) { + DEBUG("Reading content of an register\n"); + ret = mtd_load(&mtd_reg, sizeof(mtd_register_t), + reg_count * sizeof(mtd_register_t) + MTD_START_ADDR); + if (ret < 0) { + return ret; + } -int mtd_erase_flashpage(uint32_t key) { - /* Erase last sector */ - int ret = mtd_erase(dev, key, FLASHPAGE_SIZE); - if (ret < 0) { - DEBUG("Error to the erase the address \n"); + uint8_t out[mtd_reg.size]; + mtd_load(out, mtd_reg.size, mtd_reg.ptr_content); + memcpy(value, out, len); + return 0; + } + reg_count++; } - - return ret; + return -1; } -int mtd_write_string(uint32_t key, char *value) { - int err = mtd_erase_flashpage(key); - if (err < 0) { - DEBUG("Error to the erase the address \n"); - return err; - } +int8_t mtd_available_idx(void) { + mtd_register_t buff; + uint8_t available_reg = 0; - err = mtd_save(key, value); - if (err < 0) { - DEBUG("Error to the erase the address \n"); - return err; + for (size_t i = 0; i < MTD_REG_IDX_NUMOF; i++) { + uint32_t idx_storage = MTD_START_ADDR + i * sizeof(mtd_register_t); + mtd_load(&buff, sizeof(buff), idx_storage); + if (idx_reg_is_empty(buff)) { + printf("Available index: 0x%" PRIX32 "\n", idx_storage); + available_reg++; + } + } + if (available_reg == 0) { + printf("It's not available register for write\n"); + return -1; } + printf("Number of available registers: %u\n\n", available_reg); return 0; } -int mtd_write_uint8(uint32_t key, uint8_t *value) { - int err = mtd_erase_flashpage(key); - if (err < 0) { - DEBUG("Error to the erase the address \n"); - return err; +int8_t mtd_erase_all(void) { + uint32_t addr = MTD_START_ADDR; + if (mtd_erase(dev, addr, MAX_SIZE_STORAGE) < 0) { + return -1; } + return 0; +} - err = mtd_save(key, value); - if (err < 0) { - DEBUG("Error to the erase the address \n"); - return err; +int8_t mtd_dump(void) { + uint8_t value[dev->write_size]; + uint32_t addr = MTD_START_ADDR; + uint32_t erased_data = 0; + for (uint32_t i = 0; i < MAX_SIZE_STORAGE / dev->write_size; i++) { + mtd_read_block(value, addr, dev->write_size, 0); + for (uint8_t j = 0; j < dev->write_size; j++) { + DEBUG("%02X ", value[j]); + if ((value[j] == 255) || (value[j] == 0)) { + erased_data++; + } + } + DEBUG("\n"); + addr += dev->write_size; + } + if (erased_data == MAX_SIZE_STORAGE) { + return -1; } - return 0; } diff --git a/tests/system_storage/Kconfig b/tests/system_storage/Kconfig index a1f9e600c..b714b6e52 100644 --- a/tests/system_storage/Kconfig +++ b/tests/system_storage/Kconfig @@ -1 +1,6 @@ rsource "../../firmware/sys/storage/Kconfig" +rsource "../../firmware/Kconfig.debug" + +menu "Test Debug" + +endmenu \ No newline at end of file diff --git a/tests/system_storage/Makefile b/tests/system_storage/Makefile index 3fc8e951e..b6b98e1a1 100644 --- a/tests/system_storage/Makefile +++ b/tests/system_storage/Makefile @@ -2,11 +2,11 @@ include ../Makefile.tests_common USEMODULE += embunit USEMODULE += storage -USEMODULE += radio -USEMODULE += rpl_protocol +# USEMODULE += radio +# USEMODULE += rpl_protocol -ifeq (,$(filter native,$(BOARD))) - USEMODULE += stdio_cdc_acm -endif +# ifneq (,$(filter m4a-24g,$(BOARD))) +# USEMODULE += stdio_cdc_acm +# endif include $(RIOTBASE)/Makefile.include diff --git a/tests/system_storage/main.c b/tests/system_storage/main.c index 218236524..d476d1e68 100644 --- a/tests/system_storage/main.c +++ b/tests/system_storage/main.c @@ -24,20 +24,15 @@ #include #include #include -#include "default_params.h" #include "embUnit.h" #include "storage.h" +#include "mtd.h" #define ENABLE_DEBUG 0 #include "debug.h" -#define ADDRESS (uint32_t) flashpage_addr(LAST_AVAILABLE_PAGE - 5) - -char data[] = "hello world"; -uint8_t u8value = 150; - struct val { - uint8_t var[6]; + uint8_t var[8]; uint32_t var2[6]; char str[28]; int32_t var3[50]; @@ -50,12 +45,17 @@ void test_init_mtd(void) { void test_save_data(void) { int ret = 0; - struct val test_save = {.var = {2, 5, 6, 7, 8, 9}, - .var2 = {1555, 2556, 477, 8975, 987, 414}, - .str = "Welcome to mesh storage!!!", - .var3 = {1550, 5544, -698, -789, -97852, [49] = 2556}}; + mtd_erase_all(); + struct val test_save = { + .var = {2, 5, 150, 7, 33, 9}, + .var2 = {1555, 2556, 477, 8975, 987, 454}, + .str = "Welcome to mesh storage!!!", + .var3 = {1550, 5544, -698, -789, -97852, [19] = -25, [48] = 39, [49] = 2556}}; printf("\nSaving data:\n"); - ret = mtd_save_compress(&test_save, sizeof(test_save)); + ret = mtd_save(&test_save, sizeof(test_save), MTD_START_ADDR); + if (ret < 0) { + DEBUG("Failed Saving data\n"); + } TEST_ASSERT_EQUAL_INT(0, ret); } @@ -63,138 +63,118 @@ void test_load_data(void) { struct val test_load; printf("\n\nLoading data:"); DEBUG("\n\n"); - mtd_load(&test_load, sizeof(test_load)); + mtd_load(&test_load, sizeof(test_load), MTD_START_ADDR); for (uint16_t i = 0; i < ARRAY_SIZE(test_load.var); i++) { - DEBUG("varui8_attr1 [%d]: %d\n", i + 1, test_load.var[i]); + DEBUG("varui8_attr1 [%d]: %d\n", i, test_load.var[i]); } DEBUG("\n"); for (uint16_t i = 0; i < ARRAY_SIZE(test_load.var2); i++) { - DEBUG("varui32_attr2 [%d]: %" PRId32 "\n", i + 1, test_load.var2[i]); + DEBUG("varui32_attr2 [%d]: %" PRId32 "\n", i, test_load.var2[i]); } DEBUG("\nvarstr_attr3: %s\n\n", test_load.str); for (uint16_t i = 0; i < ARRAY_SIZE(test_load.var3); i++) { - DEBUG("vari32_attr4 [%d]: %" PRId32 "\n", i + 1, test_load.var3[i]); + DEBUG("vari32_attr4 [%d]: %" PRId32 "\n", i, test_load.var3[i]); } DEBUG("\n"); printf("\nLoad successfully\n"); } -void test_save_firm_data(void) { +void test_save_pointed(void) { int ret = 0; - storage_data_t storage_test = { - .amount_sensors = 2, - .wifi_subsys = 0, - .uniqueid_mode = 0, - .sensors = - { - { - .class_sensor = 0, - .pin = 5, - .sensor_type = 0, - }, - { - .class_sensor = 1, - .pin = 1, - .sensor_type = 1, - }, - }, -#ifdef MODULE_RADIO - .radio_tx_power = 4, - .subghz = true, - .channel = 11, -#endif -#ifdef MODULE_RPL_PROTOCOL - .rpl_mode = 1, - .pan_id = 0x20, -#endif - }; - ret = mtd_save_compress(&storage_test, sizeof(storage_test)); + mtd_erase_all(); + char str[36] = {"mesh4all, connect with everyone!!!"}; + ret = mtd_save(str, sizeof(str), MTD_START_ADDR + 56); TEST_ASSERT_EQUAL_INT(0, ret); } -void test_load_firm_data(void) { - storage_data_t load_storage; - printf("\n\nLoading Firmware default data:\n\n"); - mtd_load(&load_storage, sizeof(load_storage)); -#ifdef MODULE_RADIO - printf("Tx_power: %d\n", load_storage.radio_tx_power); - printf("Sub_GHz: %s\n", load_storage.subghz ? "YES" : "NO"); - printf("Channel: %u\n", load_storage.channel); -#endif -#ifdef MODULE_RPL_PROTOCOL - printf("\nRouting\n\nRpl_mode: %s\n", load_storage.rpl_mode ? "DAG" : "DODAG"); - printf("RPL instance: %d\n", load_storage.rpl_instance); - printf("pan_id: 0x%X\n", load_storage.pan_id); - -#endif - printf("Wifi-subsys: %s\n", load_storage.wifi_subsys ? "YES" : "NO"); - printf("Amount of sensors %d\n", load_storage.amount_sensors); - for (uint8_t i = 0; i < load_storage.amount_sensors; i++) { - printf("\nSensor Id %d \n", i + 1); - printf("Class of Sensor: %s SENSOR\n", - load_storage.sensors[i].class_sensor ? "TEMPERATURE" : "MOISTURE"); - printf("Application Type of Sensor: %s SENSOR\n", - load_storage.sensors[i].sensor_type ? "AIR" : "SOIL"); - printf("Input pin: %d\n", load_storage.sensors[i].pin); - } -} -void test_write_string(void) { - int ret = mtd_write_string(ADDRESS, data); - TEST_ASSERT_EQUAL_INT(0, ret); -} - -void test_read_string(void) { - size_t len = mtd_get_string_len(ADDRESS); - TEST_ASSERT_EQUAL_INT(len, strlen(data) + 1); - - char output[len]; - int ret = mtd_read_string(ADDRESS, output, len); - +void test_load_pointed(void) { + int ret = 0; + char str[36]; + ret = mtd_load(str, sizeof(str), MTD_START_ADDR + 56); + DEBUG("\nvarstr_attr3: %s\n\n", str); TEST_ASSERT_EQUAL_INT(0, ret); - TEST_ASSERT_EQUAL_INT(0, memcmp(data, output, sizeof(output))); } -void test_write_u8(void) { - int ret = mtd_write_uint8(ADDRESS, &u8value); +void test_saving_reg(void) { + int ret = 0; + mtd_erase_all(); + char str[28] = {"Everyone in the mesh!!!"}; + char str2[20] = {"Contributor: CW"}; + uint8_t age = 24; + uint32_t u32_val = 155556; + + ret = mtd_save_reg(str, (uint8_t *)"KEY0", sizeof(str)); + if (ret < 0) { + DEBUG("Failed Saving data"); + } + printf("\n"); + ret = mtd_save_reg(str2, (uint8_t *)"KEY1", sizeof(str2)); + if (ret < 0) { + DEBUG("Failed Saving data\n"); + } + ret = mtd_save_reg(str, (uint8_t *)"KEY0", sizeof(str)); + if (ret < 0) { + DEBUG("Failed Saving data\n"); + } + ret = mtd_save_reg(&age, (uint8_t *)"KEY2", sizeof(age)); + if (ret < 0) { + DEBUG("Failed Saving data\n"); + } + ret = mtd_save_reg(&u32_val, (uint8_t *)"KEY3", sizeof(u32_val)); + mtd_available_idx(); TEST_ASSERT_EQUAL_INT(0, ret); + mtd_dump(); } -void test_read_u8(void) { - uint8_t output = 0; - int ret = mtd_read_u8(ADDRESS, &output); - TEST_ASSERT_EQUAL_INT(0, ret); +void test_loading_reg(void) { + int ret = 0; + char str[28]; + char str2[20]; + uint32_t u32_val; + uint8_t age; + ret = mtd_load_reg(str, (uint8_t *)"KEY0", sizeof(str)); + if (ret < 0) { + DEBUG("Failed Loading data"); + } + ret = mtd_load_reg(&u32_val, (uint8_t *)"KEY3", sizeof(u32_val)); + if (ret < 0) { + DEBUG("Failed Loading data"); + } + ret = mtd_load_reg(str2, (uint8_t *)"KEY1", sizeof(str2)); + if (ret < 0) { + DEBUG("Failed Loading data"); + } + ret = mtd_load_reg(&age, (uint8_t *)"KEY2", sizeof(age)); - TEST_ASSERT_EQUAL_INT(output, u8value); -} + printf("String#1 loaded: %s\n", str); + printf("String#2 loaded: %s\n", str2); + printf("Uint32_t loaded: %" PRIu32 "\n", u32_val); + printf("Uint8_t loaded: %d\n", age); -void test_erase_address(void) { - int ret = mtd_erase_flashpage(ADDRESS); TEST_ASSERT_EQUAL_INT(0, ret); } -void test_erase_all(void){ +void test_erase_all(void) { int ret = mtd_erase_all(); TEST_ASSERT_EQUAL_INT(0, ret); } -void test_load_post_erased_data(void){ +void test_load_post_erased_data(void) { int ret = mtd_dump(); TEST_ASSERT_EQUAL_INT(-1, ret); } + Test *tests_mtd_flashpage_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures){ new_TestFixture(test_init_mtd), + new_TestFixture(test_saving_reg), + new_TestFixture(test_loading_reg), new_TestFixture(test_save_data), new_TestFixture(test_load_data), - new_TestFixture(test_save_firm_data), - new_TestFixture(test_load_firm_data), + new_TestFixture(test_save_pointed), + new_TestFixture(test_load_pointed), new_TestFixture(test_erase_all), new_TestFixture(test_load_post_erased_data), - new_TestFixture(test_write_string), - new_TestFixture(test_read_string), - new_TestFixture(test_write_u8), - new_TestFixture(test_read_u8), - new_TestFixture(test_erase_address), }; EMB_UNIT_TESTCALLER(mtd_flashpage_tests, NULL, NULL, fixtures);