From a78bc828fc052e1d7442ffe15f57c92252fc18c1 Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Mon, 1 Feb 2021 23:17:48 +0100 Subject: [PATCH] Add store_period_contacts --- src/storage.c | 23 +++++++++++++++++++++++ src/storage.h | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/src/storage.c b/src/storage.c index 09e5417..52e6d51 100644 --- a/src/storage.c +++ b/src/storage.c @@ -10,6 +10,8 @@ // Maybe use this as param for init function #define SEC_COUNT 10 +#define PERIOD_COUNTER_ID 0 +#define PERIOD_OFFSET 1 static struct nvs_fs fs; @@ -31,4 +33,25 @@ int init_storage(void) { return rc; } return 0; +} + +uint16_t get_current_period_nr() { + uint16_t period_nr; + int rc = nvs_read(&fs, PERIOD_COUNTER_ID, &period_nr, sizeof(period_nr)); + // current period not found, store 0 + if (rc <= 0) { + period_nr = 0; + nvs_write(&fs, PERIOD_COUNTER_ID, &period_nr, sizeof(period_nr)); + } + return period_nr; +} + +int store_period_contacts(period_contacts_t* contacts) { + uint16_t period = get_current_period_nr(); + int rc = nvs_write(&fs, period * 2 + PERIOD_OFFSET, contacts->cnt, sizeof(contacts->cnt)); + if (rc != sizeof(contacts->cnt)) { + return rc; + } + rc = nvs_write(&fs, period * 2 + 1 + PERIOD_OFFSET, contacts->period_contacts, sizeof(period_contact_t) * contacts->cnt); + return rc; } \ No newline at end of file diff --git a/src/storage.h b/src/storage.h index 3a0382e..d9351ff 100644 --- a/src/storage.h +++ b/src/storage.h @@ -1,9 +1,16 @@ #ifndef STORAGE_H #define STORAGE_H +#include "contacts.h" + /** * Initialize the storage api. */ int init_storage(void); +/** + * Store all contacts of a period in flash. + */ +int store_period_contacts(period_contacts_t* contacts); + #endif \ No newline at end of file