From 4992d8e273507461870d84d98d78d909575f84bd Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 29 Jan 2025 17:06:55 +0100 Subject: [PATCH] Added phy_save() and phy_load() to save and load PHY. Signed-off-by: Pol Henarejos --- src/fs/phy.c | 27 +++++++++++++++++++-------- src/fs/phy.h | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/fs/phy.c b/src/fs/phy.c index c07c363..21302c7 100644 --- a/src/fs/phy.c +++ b/src/fs/phy.c @@ -58,7 +58,7 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) { *len = p - data; return PICOKEY_OK; } -#include + int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy) { if (!phy || !data || !len) { return PICOKEY_ERR_NULL_PARAM; @@ -103,14 +103,25 @@ int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy) { int phy_init() { memset(&phy_data, 0, sizeof(phy_data_t)); - if (file_has_data(ef_phy)) { - const uint8_t *data = file_get_data(ef_phy); - int ret = phy_unserialize_data(data, file_get_size(ef_phy), &phy_data); - if (ret != PICOKEY_OK) { - return ret; - } - } + return phy_load(); +} +int phy_save() { + uint8_t tmp[PHY_MAX_SIZE] = {0}; + uint16_t tmp_len = 0; + int ret = phy_serialize_data(&phy_data, tmp, &tmp_len); + if (ret != PICOKEY_OK) { + return ret; + } + file_put_data(ef_phy, tmp, tmp_len); + low_flash_available(); + return PICOKEY_OK; +} + +int phy_load() { + if (file_has_data(ef_phy)) { + return phy_unserialize_data(file_get_data(ef_phy), file_get_size(ef_phy), &phy_data); + } return PICOKEY_OK; } #endif diff --git a/src/fs/phy.h b/src/fs/phy.h index 45b773e..8dac2db 100644 --- a/src/fs/phy.h +++ b/src/fs/phy.h @@ -62,6 +62,8 @@ typedef struct phy_data { extern int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len); extern int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy); extern int phy_init(); +extern int phy_save(); +extern int phy_load(); extern phy_data_t phy_data; #endif