2021-01-26 16:14:58 +01:00
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/flash.h>
|
|
|
|
#include <fs/nvs.h>
|
2021-04-12 19:31:15 +02:00
|
|
|
#include <logging/log.h>
|
2021-01-26 16:14:58 +01:00
|
|
|
#include <power/reboot.h>
|
|
|
|
#include <storage/flash_map.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zephyr.h>
|
|
|
|
|
2021-05-17 19:16:47 +02:00
|
|
|
#include "ens_error.h"
|
2021-04-12 19:20:15 +02:00
|
|
|
#include "ens_fs.h"
|
2021-04-12 19:07:28 +02:00
|
|
|
#include "sequencenumber.h"
|
2021-03-24 16:18:04 +01:00
|
|
|
#include "storage.h"
|
2021-01-26 16:14:58 +01:00
|
|
|
|
2021-02-18 13:45:29 +01:00
|
|
|
#define STORED_CONTACTS_INFO_ID 0
|
2021-01-26 16:14:58 +01:00
|
|
|
|
2021-04-12 19:20:15 +02:00
|
|
|
static struct nvs_fs info_fs;
|
2021-04-27 00:16:46 +02:00
|
|
|
static struct k_mutex info_fs_lock;
|
2021-03-31 16:31:58 +02:00
|
|
|
|
2021-04-12 19:31:15 +02:00
|
|
|
static ens_fs_t ens_fs;
|
|
|
|
|
2021-03-23 18:11:11 +01:00
|
|
|
// Information about currently stored contacts
|
2021-05-03 22:07:15 +02:00
|
|
|
static stored_records_information_t record_information = {.oldest_contact = 0, .count = 0};
|
2021-02-18 13:45:29 +01:00
|
|
|
|
|
|
|
inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
|
2021-05-18 17:09:07 +02:00
|
|
|
return (storage_id_t)(sn % CONFIG_ENS_MAX_CONTACTS);
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 18:11:11 +01:00
|
|
|
/**
|
2021-03-24 15:44:35 +01:00
|
|
|
* Load our initial storage information from flash.
|
2021-03-23 18:11:11 +01:00
|
|
|
*/
|
|
|
|
int load_storage_information() {
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
2021-05-03 22:07:15 +02:00
|
|
|
size_t size = sizeof(record_information);
|
|
|
|
int rc = nvs_read(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, size);
|
2021-03-23 18:11:11 +01:00
|
|
|
|
2021-03-24 16:18:04 +01:00
|
|
|
// Check, if read what we wanted
|
|
|
|
if (rc != size) {
|
2021-03-23 18:11:11 +01:00
|
|
|
// Write our initial data to storage
|
2021-05-17 19:16:47 +02:00
|
|
|
int rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, size);
|
2021-03-24 16:18:04 +01:00
|
|
|
if (rc <= 0) {
|
2021-03-23 18:11:11 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_unlock(&info_fs_lock);
|
2021-03-23 18:11:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:44:35 +01:00
|
|
|
/**
|
|
|
|
* Save our current storage infromation to flash.
|
|
|
|
*/
|
|
|
|
int save_storage_information() {
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
2021-05-03 22:07:15 +02:00
|
|
|
int rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, sizeof(record_information));
|
2021-03-24 16:18:04 +01:00
|
|
|
if (rc <= 0) {
|
2021-03-24 15:44:35 +01:00
|
|
|
printk("Something went wrong after saving storage information.\n");
|
|
|
|
}
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_unlock(&info_fs_lock);
|
2021-03-31 17:06:38 +02:00
|
|
|
return rc;
|
2021-03-24 15:44:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
int init_record_storage(void) {
|
2021-01-26 16:14:58 +01:00
|
|
|
int rc = 0;
|
|
|
|
struct flash_pages_info info;
|
|
|
|
// define the nvs file system
|
2021-04-12 19:20:15 +02:00
|
|
|
info_fs.offset = FLASH_AREA_OFFSET(storage);
|
2021-04-26 15:56:00 +02:00
|
|
|
rc =
|
|
|
|
flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), info_fs.offset, &info);
|
2021-01-26 16:14:58 +01:00
|
|
|
|
|
|
|
if (rc) {
|
2021-03-23 18:11:11 +01:00
|
|
|
// Error during retrieval of page information
|
2021-04-12 19:31:15 +02:00
|
|
|
printk("Cannot retrieve page information (err %d)\n", rc);
|
2021-01-26 16:14:58 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2021-04-12 19:20:15 +02:00
|
|
|
info_fs.sector_size = info.size;
|
2021-05-14 00:31:14 +02:00
|
|
|
info_fs.sector_count = FLASH_AREA_SIZE(storage) / info.size;
|
2021-01-26 16:14:58 +01:00
|
|
|
|
2021-04-12 19:20:15 +02:00
|
|
|
rc = nvs_init(&info_fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_init(&info_fs_lock);
|
2021-03-24 16:18:04 +01:00
|
|
|
if (rc) {
|
2021-03-23 18:11:11 +01:00
|
|
|
// Error during nvs_init
|
2021-04-12 19:31:15 +02:00
|
|
|
printk("Cannot init NVS (err %d)\n", rc);
|
2021-03-23 18:11:11 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current storage information
|
|
|
|
rc = load_storage_information();
|
2021-04-12 19:31:15 +02:00
|
|
|
if (rc) {
|
|
|
|
printk("Cannot load storage information (err %d)\n", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
2021-03-23 18:11:11 +01:00
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
printk("Currently %d contacts stored!\n", record_information.count);
|
2021-03-31 15:57:12 +02:00
|
|
|
printk("Space available: %d\n", FLASH_AREA_SIZE(storage));
|
2021-03-23 18:11:11 +01:00
|
|
|
|
2021-05-03 23:52:56 +02:00
|
|
|
rc = ens_fs_init(&ens_fs, FLASH_AREA_ID(ens_storage), sizeof(record_t));
|
2021-04-13 14:29:07 +02:00
|
|
|
if (rc) {
|
2021-04-12 19:31:15 +02:00
|
|
|
printk("Cannot init ens_fs (err %d)\n", rc);
|
|
|
|
}
|
2021-02-18 13:45:29 +01:00
|
|
|
return rc;
|
2021-02-01 23:17:48 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
int load_record(record_t* dest, record_sequence_number_t sn) {
|
2021-02-18 13:45:29 +01:00
|
|
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
2021-04-12 19:52:06 +02:00
|
|
|
int rc = ens_fs_read(&ens_fs, id, dest);
|
2021-04-24 16:03:43 +02:00
|
|
|
if (rc < 0) {
|
2021-02-01 23:17:48 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2021-02-18 13:45:29 +01:00
|
|
|
return 0;
|
2021-02-03 19:39:01 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
int add_record(record_t* src) {
|
2021-05-17 19:16:47 +02:00
|
|
|
/**
|
|
|
|
* Some information about the procedure in this function:
|
|
|
|
* 1. we calculate the potential next sn and storage id
|
|
|
|
* 2. we try to write our entry
|
|
|
|
* 2.1 if write was successful, goto 5., otherwise continue at 3.
|
|
|
|
* 3. if our id is already in use, we request the fs to make some space
|
|
|
|
* 4. after making space, we adjust our storage information and try to write again
|
|
|
|
* 5. we actually "increment" our stored contact information
|
|
|
|
*
|
|
|
|
* This order (first erase storage, then increment information) is important, because like this we keep a constant
|
|
|
|
* state of our information about the stored contacts in combination with correct state of our flash.
|
|
|
|
*/
|
2021-05-03 23:31:51 +02:00
|
|
|
|
|
|
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
|
|
|
|
2021-04-13 14:29:07 +02:00
|
|
|
// Check, if next sn would be at start of page
|
2021-05-17 19:16:47 +02:00
|
|
|
record_sequence_number_t potential_next_sn =
|
|
|
|
get_latest_sequence_number() == get_oldest_sequence_number() ? 0 : sn_increment(get_latest_sequence_number());
|
2021-04-13 14:29:07 +02:00
|
|
|
storage_id_t potential_next_id = convert_sn_to_storage_id(potential_next_sn);
|
2021-05-03 23:31:51 +02:00
|
|
|
|
2021-05-17 19:16:47 +02:00
|
|
|
// write our entry to flash and check, if the current entry is already in use
|
|
|
|
int rc = ens_fs_write(&ens_fs, potential_next_id, src);
|
|
|
|
// if our error does NOT indicate, that this address is already in use, we just goto end and do nothing
|
|
|
|
if (rc && rc != -ENS_ADDRINU) {
|
|
|
|
// TODO: maybe also increment, if there is an internal error?
|
|
|
|
goto end;
|
|
|
|
} else if (rc == -ENS_ADDRINU) {
|
|
|
|
// the current id is already in use, so make some space for our new entry
|
|
|
|
int deletedRecordsCount = ens_fs_make_space(&ens_fs, potential_next_id);
|
|
|
|
if (deletedRecordsCount < 0) {
|
|
|
|
// some interal error happened (e.g. we are not at a page start)
|
|
|
|
rc = deletedRecordsCount;
|
|
|
|
// we still need to increment our information, so we are not at the exact same id the entire time
|
|
|
|
goto inc;
|
2021-05-18 17:09:07 +02:00
|
|
|
} else if (deletedRecordsCount > 0 && get_num_records() == CONFIG_ENS_MAX_CONTACTS) {
|
2021-05-03 23:31:51 +02:00
|
|
|
record_information.count -= deletedRecordsCount;
|
2021-05-14 00:31:14 +02:00
|
|
|
record_information.oldest_contact = sn_increment_by(record_information.oldest_contact, deletedRecordsCount);
|
2021-05-17 19:16:47 +02:00
|
|
|
}
|
|
|
|
// after creating some space, try to write again
|
|
|
|
rc = ens_fs_write(&ens_fs, potential_next_id, src);
|
|
|
|
if (rc) {
|
|
|
|
goto inc;
|
2021-05-03 23:31:51 +02:00
|
|
|
}
|
2021-04-12 19:52:06 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 19:16:47 +02:00
|
|
|
inc:
|
|
|
|
// check, how we need to update our storage information
|
2021-05-18 17:09:07 +02:00
|
|
|
if (record_information.count >= CONFIG_ENS_MAX_CONTACTS) {
|
2021-05-17 19:16:47 +02:00
|
|
|
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
|
|
|
|
} else {
|
|
|
|
record_information.count++;
|
|
|
|
}
|
|
|
|
save_storage_information();
|
2021-04-13 14:29:07 +02:00
|
|
|
|
2021-05-17 19:16:47 +02:00
|
|
|
end:
|
2021-05-03 23:31:51 +02:00
|
|
|
k_mutex_unlock(&info_fs_lock);
|
2021-05-17 19:16:47 +02:00
|
|
|
return rc;
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
int delete_record(record_sequence_number_t sn) {
|
2021-04-21 17:58:16 +02:00
|
|
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
|
|
|
int rc = ens_fs_delete(&ens_fs, id);
|
|
|
|
if (!rc) {
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
2021-05-03 23:31:51 +02:00
|
|
|
if (sn_equal(sn, get_oldest_sequence_number())) {
|
2021-05-03 22:07:15 +02:00
|
|
|
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
|
|
|
|
record_information.count--;
|
2021-04-21 17:58:16 +02:00
|
|
|
}
|
|
|
|
save_storage_information();
|
2021-04-27 00:16:46 +02:00
|
|
|
k_mutex_unlock(&info_fs_lock);
|
2021-04-21 17:58:16 +02:00
|
|
|
}
|
|
|
|
return rc;
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
record_sequence_number_t get_latest_sequence_number() {
|
2021-05-14 00:31:14 +02:00
|
|
|
return sn_increment_by(record_information.oldest_contact, record_information.count);
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
record_sequence_number_t get_oldest_sequence_number() {
|
2021-05-03 22:07:15 +02:00
|
|
|
return record_information.oldest_contact;
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:07:15 +02:00
|
|
|
uint32_t get_num_records() {
|
|
|
|
return record_information.count;
|
2021-03-11 00:32:36 +01:00
|
|
|
}
|