From de21af38f217480ad24c856ee051613941dd5504 Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Tue, 23 Mar 2021 18:11:11 +0100 Subject: [PATCH] Add sn_mask to mask a sequence number --- src/main.c | 1 - src/records.c | 1 - src/sequencenumber.c | 7 +++++-- src/sequencenumber.h | 9 +++++++++ src/storage.c | 42 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 24c56d0..67481c1 100644 --- a/src/main.c +++ b/src/main.c @@ -36,7 +36,6 @@ void main(void) { return; } - printk("init contacts\n"); init_contacts(); err = init_io(); if (err) { diff --git a/src/records.c b/src/records.c index 7feeb63..d1cfbed 100644 --- a/src/records.c +++ b/src/records.c @@ -6,7 +6,6 @@ int ens_records_iterator_init_range(record_iterator_t* iterator, record_sequence_number_t* opt_start, record_sequence_number_t* opt_end) { - // TODO: if start or end is null, use currently available numbers iterator->sn_next = opt_start ? *opt_start : get_oldest_sequence_number(); iterator->sn_end = opt_end ? *opt_end : get_latest_sequence_number(); if (get_num_contacts() == 0) { diff --git a/src/sequencenumber.c b/src/sequencenumber.c index c688e7f..b2ddcab 100644 --- a/src/sequencenumber.c +++ b/src/sequencenumber.c @@ -1,10 +1,13 @@ #include "sequencenumber.h" +record_sequence_number_t sn_mask(record_sequence_number_t sn) { + return (sn & SN_MASK); +} int sequence_number_eq(record_sequence_number_t a, record_sequence_number_t b) { - return (a & SN_MASK) == (b & SN_MASK); + return sn_mask(a) == sn_mask(b); } record_sequence_number_t sequence_number_increment(record_sequence_number_t sn) { - return (++sn & SN_MASK); + return sn_mask(++sn); } \ No newline at end of file diff --git a/src/sequencenumber.h b/src/sequencenumber.h index 428f593..7865803 100644 --- a/src/sequencenumber.h +++ b/src/sequencenumber.h @@ -8,6 +8,15 @@ typedef uint32_t record_sequence_number_t; +/** + * Mask a given sequence number to get rid of MSB. + * TODO: maybe as #define? + * + * @param sn sequence number to mask + * @return masked sequence number + */ +record_sequence_number_t sn_mask(record_sequence_number_t sn); + /** * Compare to sequence numbers for equality. * diff --git a/src/storage.c b/src/storage.c index 99df8ff..7d06abb 100644 --- a/src/storage.c +++ b/src/storage.c @@ -7,6 +7,7 @@ #include #include "storage.h" +#include "sequencenumber.h" // Maybe use this as param for init function #define SEC_COUNT 8U @@ -14,12 +15,14 @@ #define STORED_CONTACTS_INFO_ID 0 #define CONTACTS_OFFSET 1 #define MAX_CONTACTS 65535 -#define ADDRESS_ID 1 static struct nvs_fs fs; -// TODO lome: load this from flash -static stored_contacts_information_t contact_information; +// Information about currently stored contacts +static stored_contacts_information_t contact_information = { + .oldest_contact = 0, + .count = 0 +}; inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) { return (storage_id_t)(sn % MAX_CONTACTS) + CONTACTS_OFFSET; @@ -31,6 +34,24 @@ void increment_storaed_contact_counter() { } } +/** + * Load our initial storage information from storage. + */ +int load_storage_information() { + size_t size = sizeof(contact_information); + int rc = nvs_read(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size); + + // Check, if read what we wanted + if(rc != size) { + // Write our initial data to storage + rc = nvs_write(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size); + if(rc <= 0) { + return rc; + } + } + return 0; +} + int init_contact_storage(void) { int rc = 0; struct flash_pages_info info; @@ -39,13 +60,23 @@ int init_contact_storage(void) { rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), fs.offset, &info); if (rc) { + // Error during retrieval of page information return rc; } fs.sector_size = info.size; fs.sector_count = SEC_COUNT; rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); - + if(rc) { + // Error during nvs_init + return rc; + } + + // Load the current storage information + rc = load_storage_information(); + + printk("Currently %d contacts stored!\n", contact_information.count); + return rc; } @@ -71,13 +102,14 @@ int add_contact(contact_t* src) { } // TODO handle start and end +// TODO lome: do we need this? int delete_contact(record_sequence_number_t sn) { storage_id_t id = convert_sn_to_storage_id(sn); return nvs_delete(&fs, id); } record_sequence_number_t get_latest_sequence_number() { - return contact_information.oldest_contact + contact_information.count; + return sn_mask(contact_information.oldest_contact + contact_information.count); } record_sequence_number_t get_oldest_sequence_number() {