From e74b58aab59c716f4d1e43a7533f12fa822ab65e Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Wed, 24 Mar 2021 15:44:35 +0100 Subject: [PATCH] Correctly increase sequence number and save storage information to flash --- src/storage.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/storage.c b/src/storage.c index 7d06abb..61534ff 100644 --- a/src/storage.c +++ b/src/storage.c @@ -28,14 +28,9 @@ inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) { return (storage_id_t)(sn % MAX_CONTACTS) + CONTACTS_OFFSET; } -void increment_storaed_contact_counter() { - if (contact_information.count >= MAX_CONTACTS) { - contact_information.oldest_contact = sequence_number_increment(contact_information.oldest_contact); - } -} /** - * Load our initial storage information from storage. + * Load our initial storage information from flash. */ int load_storage_information() { size_t size = sizeof(contact_information); @@ -52,6 +47,26 @@ int load_storage_information() { return 0; } +/** + * Save our current storage infromation to flash. + */ +int save_storage_information() { + int rc = nvs_write(&fs, STORED_CONTACTS_INFO_ID, &contact_information, sizeof(contact_information)); + + if(rc <= 0) { + printk("Something went wrong after saving storage information.\n"); + } +} + +void increment_stored_contact_counter() { + if (contact_information.count >= MAX_CONTACTS) { + contact_information.oldest_contact = sequence_number_increment(contact_information.oldest_contact); + } else { + contact_information.count++; + } + save_storage_information(); +} + int init_contact_storage(void) { int rc = 0; struct flash_pages_info info; @@ -90,12 +105,12 @@ int load_contact(contact_t* dest, record_sequence_number_t sn) { } int add_contact(contact_t* src) { - record_sequence_number_t curr_sn = get_latest_sequence_number() + 1; + record_sequence_number_t curr_sn = sequence_number_increment(get_latest_sequence_number()); storage_id_t id = convert_sn_to_storage_id(curr_sn); int rc = nvs_write(&fs, id, src, sizeof(*src)); if (rc > 0) { - increment_storaed_contact_counter(); + increment_stored_contact_counter(); return 0; } return rc;