2021-01-26 16:14:58 +01:00
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/flash.h>
|
|
|
|
#include <fs/nvs.h>
|
|
|
|
#include <power/reboot.h>
|
|
|
|
#include <storage/flash_map.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zephyr.h>
|
|
|
|
|
|
|
|
#include "storage.h"
|
|
|
|
|
|
|
|
// Maybe use this as param for init function
|
|
|
|
#define SEC_COUNT 10
|
2021-02-01 23:17:48 +01:00
|
|
|
#define PERIOD_COUNTER_ID 0
|
|
|
|
#define PERIOD_OFFSET 1
|
2021-01-26 16:14:58 +01:00
|
|
|
|
|
|
|
static struct nvs_fs fs;
|
|
|
|
|
|
|
|
int init_storage(void) {
|
|
|
|
int rc = 0;
|
|
|
|
struct flash_pages_info info;
|
|
|
|
// define the nvs file system
|
|
|
|
fs.offset = FLASH_AREA_OFFSET(storage);
|
|
|
|
rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), fs.offset, &info);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
fs.sector_size = info.size;
|
|
|
|
fs.sector_count = SEC_COUNT;
|
|
|
|
|
|
|
|
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
2021-01-26 16:16:17 +01:00
|
|
|
return 0;
|
2021-02-01 23:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:39:01 +01:00
|
|
|
int store_period_contacts(period_contacts_t contacts) {
|
2021-02-01 23:17:48 +01:00
|
|
|
uint16_t period = get_current_period_nr();
|
2021-02-03 19:39:01 +01:00
|
|
|
int rc = nvs_write(&fs, period * 2 + PERIOD_OFFSET, &contacts.cnt, sizeof(contacts.cnt));
|
|
|
|
if (rc <= 0) {
|
2021-02-01 23:17:48 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2021-02-03 19:39:01 +01:00
|
|
|
rc = nvs_write(&fs, period * 2 + 1 + PERIOD_OFFSET, &contacts.period_contacts,
|
|
|
|
sizeof(period_contact_t) * contacts.cnt);
|
2021-02-01 23:17:48 +01:00
|
|
|
return rc;
|
2021-02-03 19:39:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
period_contacts_t get_contacts_for_period(uint16_t period) {
|
|
|
|
period_contacts_t contacts;
|
|
|
|
// Read the count of contacts in the requested period
|
|
|
|
int rc = nvs_read(&fs, period * 2 + PERIOD_OFFSET, &contacts.cnt, sizeof(contacts.cnt));
|
|
|
|
if (rc <= 0) {
|
|
|
|
// Error while reading count of contacts for requested period
|
|
|
|
printk("Period #%d not found. (err %d)", period, rc);
|
|
|
|
contacts.cnt = -1;
|
|
|
|
} else {
|
|
|
|
// Read the actual contacts for the requested period
|
|
|
|
rc = nvs_read(&fs, period * 2 + 1 + PERIOD_OFFSET, &contacts.period_contacts,
|
|
|
|
sizeof(period_contact_t) * contacts.cnt);
|
|
|
|
if (rc <= 0) {
|
|
|
|
// Error while reading actual contacts for requested period
|
|
|
|
printk("Error while reading contacts for period #%d. (err %d)", period, rc);
|
|
|
|
contacts.cnt = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contacts;
|
2021-01-26 16:14:58 +01:00
|
|
|
}
|