Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firmware/sys/storage: Added supports to save specified data types #360

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion firmware/sys/storage/Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
USEMODULE += mtd_flashpage
USEMODULE += mtd_write_page

ifneq (,$(filter storage,$(USEMODULE)))
FEATURES_REQUIRED += periph_flashpage
Expand Down
130 changes: 127 additions & 3 deletions firmware/sys/storage/include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int8_t mtd_start(void);
* @param [in] offset reference to an position in mtd_storage.
* @return int8_t
*/
int mtd_save(void *value, uint32_t len, uint32_t offset);
int mtd_save(const void *value, uint32_t len, uint32_t offset);

/**
* @brief Loads any value in any position of mtd_storage.
Expand All @@ -95,7 +95,7 @@ int mtd_load(void *value, uint16_t len, uint32_t offset);
* @param [in] len size of @p value that will be saved.
* @return int8_t
*/
int mtd_save_reg(void *value, uint8_t *key, uint16_t len);
int mtd_save_reg(const void *value, const uint8_t *key, uint16_t len);

/**
* @brief Load data from mtd_storage localizating with its knew key and saves in @p value the data.
Expand All @@ -108,7 +108,7 @@ int mtd_save_reg(void *value, uint8_t *key, uint16_t len);
* value parameter.
* @return int8_t
*/
int mtd_load_reg(void *value, uint8_t *key, uint16_t len);
int mtd_load_reg(void *value, const uint8_t *key, uint16_t len);

/**
* @brief Removes all saved data in mtd_storage. This will erase all until the
Expand Down Expand Up @@ -148,6 +148,130 @@ int8_t mtd_dump(void);
*/
int8_t mtd_dump_flashpage(uint16_t page);

/* Unsigned int put/get operations */

/**
* @brief Saves an unsigned int 8 bits data type in the mtd_storage.
*
* @param [in] value uint8_t data.
* @param [in] key An unique identifier string for the uint8_t data @p value.
* @return int
*/
int mtd_put_u8(const uint8_t value, const uint8_t *key);
/**
* @brief Saves an unsigned int 16 bits data type in the mtd_storage.
*
* @param [in] value uint16_t data.
* @param [in] key An unique identifier string for the uint16_t data @p value.
* @return int
*/
int mtd_put_u16(const uint16_t value, const uint8_t *key);
/**
* @brief Saves an unsigned int 32 bits data type in the mtd_storage.
*
* @param [in] value uint32_t data.
* @param [in] key An unique identifier string for the uint32_t data @p value.
* @return int
*/
int mtd_put_u32(const uint32_t value, const uint8_t *key);
/**
* @brief Load an unsigned int 8 bits data type in the mtd_storage
*
* @param [out] value uint8_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the uint8_t data @p value.
* @return int
*/
int mtd_get_u8(uint8_t *value, const uint8_t *key);
/**
* @brief Load an unsigned int 16 bits data type in the mtd_storage
*
* @param [out] value uint16_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the uint16_t data @p value.
* @return int
*/
int mtd_get_u16(uint16_t *value, const uint8_t *key);
/**
* @brief Load an unsigned int 32 bits data type in the mtd_storage
*
* @param [out] value uint32_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the uint32_t data @p value.
* @return int
*/
int mtd_get_u32(uint32_t *value, const uint8_t *key);

/* Int put/set operations */

/**
* @brief Saves a signed int 8 bits data type in the mtd_storage.
*
* @param [in] value int8_t data.
* @param [in] key An unique identifier string for the int8_t data @p value.
* @return int
*/
int mtd_put_i8(const int8_t value, const uint8_t *key);
/**
* @brief Saves an signed int 16 bits data type in the mtd_storage.
*
* @param [in] value uint16_t data.
* @param [in] key An unique identifier string for the uint16_t data @p value.
* @return int
*/
int mtd_put_i16(const int16_t value, const uint8_t *key);
/**
* @brief Saves an signed int 32 bits data type in the mtd_storage.
*
* @param [in] value uint32_t data.
* @param [in] key An unique identifier string for the uint32_t data @p value.
* @return int
*/
int mtd_put_i32(const int32_t value, const uint8_t *key);
/**
* @brief Load an signed int 8 bits data type in the mtd_storage
*
* @param [out] value int8_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the int8_t data @p value.
* @return int
*/
int mtd_get_i8(int8_t *value, const uint8_t *key);
/**
* @brief Load an unsigned int 16 bits data type in the mtd_storage
*
* @param [out] value int16_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the int16_t data @p value.
* @return int
*/
int mtd_get_i16(int16_t *value, const uint8_t *key);
/**
* @brief Load an unsigned int 32 bits data type in the mtd_storage
*
* @param [out] value uint32_t data variable that will load the value from mtd_storage
* @param [in] key An unique identifier string for the uint32_t data @p value.
* @return int
*/
int mtd_get_i32(int32_t *value, const uint8_t *key);

/* String put/set operations */

/**
* @brief Saves a string with its identifier.
* @note it needs to specify the data string length.
*
* @param [in] value pointer to a string, points to a data string
* @param [in] key An unique identifier string for the string data @p value.
* @param [in] len size of @p value string.
* @return int
*/
int mtd_put_str(const char *value, const uint8_t *key, uint8_t len);
/**
* @brief Loads a string using its identifier.
*
* @param [out] value pointer to a string, points to a data string
* @param [in] key An unique identifier string for the string data @p value.
* @param [in] len size of @p value string.
* @return int
*/
int mtd_get_str(char *value, const uint8_t *key, uint8_t len);

#ifdef __cplusplus
}
#endif
Expand Down
84 changes: 69 additions & 15 deletions firmware/sys/storage/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int8_t mtd_start(void) {
return ret;
}

int mtd_write_block(void *val, uint32_t addr, uint8_t size, uint8_t offset) {
int mtd_write_block(const 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));
Expand All @@ -80,11 +80,11 @@ int mtd_read_block(void *value, uint32_t addr, uint8_t size, uint8_t offset) {
DEBUG("Err: Reading a Block\n");
return ret;
}
memcpy(value, buf + offset, size);
memcpy(value, buf + offset, size - offset);
return 0;
}

int mtd_save(void *value, uint32_t len, uint32_t addr) {
int mtd_save(const 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;
Expand Down Expand Up @@ -136,11 +136,7 @@ int mtd_load(void *value, uint16_t len, uint32_t addr) {

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
memset(empty_reg, FLASHPAGE_ERASE_STATE, sizeof(empty_reg));
if (memcmp(&reg, empty_reg, sizeof(reg.size) + sizeof(reg.key)) == 0) {
return 0;
}
Expand All @@ -157,7 +153,7 @@ int8_t check_idx_reg(mtd_register_t reg, mtd_register_t buffer) {
return -1;
}

int mtd_save_reg(void *value, uint8_t *key, uint16_t len) {
int mtd_save_reg(const void *value, const 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;
Expand Down Expand Up @@ -204,7 +200,7 @@ int mtd_save_reg(void *value, uint8_t *key, uint16_t len) {
return -1;
}

int mtd_load_reg(void *value, uint8_t *key, uint16_t len) {
int mtd_load_reg(void *value, const 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;
Expand All @@ -229,7 +225,9 @@ int mtd_load_reg(void *value, uint8_t *key, uint16_t len) {
}

uint8_t out[mtd_reg.size];
mtd_load(out, mtd_reg.size, mtd_reg.ptr_content);
if (mtd_load(out, mtd_reg.size, mtd_reg.ptr_content) < 0) {
return -1;
}
memcpy(value, out, len);
return 0;
}
Expand All @@ -238,24 +236,80 @@ int mtd_load_reg(void *value, uint8_t *key, uint16_t len) {
return -1;
}

int mtd_put_u8(const uint8_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(uint8_t));
}

int mtd_put_u16(const uint16_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(uint16_t));
}

int mtd_put_u32(const uint32_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(uint32_t));
}

int mtd_get_u8(uint8_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(uint8_t));
}

int mtd_get_u16(uint16_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(uint16_t));
}

int mtd_get_u32(uint32_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(uint32_t));
}

int mtd_put_i8(const int8_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(int8_t));
}

int mtd_put_i16(const int16_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(int16_t));
}

int mtd_put_i32(const int32_t value, const uint8_t *key) {
return mtd_save_reg(&value, key, sizeof(int32_t));
}

int mtd_get_i8(int8_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(int8_t));
}

int mtd_get_i16(int16_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(int16_t));
}

int mtd_get_i32(int32_t *value, const uint8_t *key) {
return mtd_load_reg(value, key, sizeof(int32_t));
}

int mtd_put_str(const char *value, const uint8_t *key, uint8_t len) {
return mtd_save_reg(value, key, len);
}

int mtd_get_str(char *value, const uint8_t *key, uint8_t len) {
return mtd_load_reg(value, key, len);
}

int8_t mtd_available_idx(void) {
mtd_register_t buff;
uint8_t available_reg = 0;

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);
if (idx_reg_is_empty(buff) == 0) {
DEBUG("Available index: 0x%" PRIX32 "\n", idx_storage);
available_reg++;
}
}
if (available_reg == 0) {
printf("It's not available register for write\n");
DEBUG("It's not available register for write\n");
return -1;
}

printf("Number of available registers: %u\n\n", available_reg);
DEBUG("Number of available registers: %u\n\n", available_reg);
return 0;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/system_storage/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ rsource "../../firmware/sys/storage/Kconfig"
rsource "../../firmware/Kconfig.debug"

menu "Test Debug"

endmenu
config DEBUG_TEST_STORAGE
bool "Debugging storage Test"
default n
endmenu
1 change: 1 addition & 0 deletions tests/system_storage/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include ../Makefile.tests_common

USEMODULE += embunit
USEMODULE += storage

# USEMODULE += radio
# USEMODULE += rpl_protocol

Expand Down
Loading