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>
|
|
|
|
|
2021-03-23 18:11:11 +01:00
|
|
|
#include "sequencenumber.h"
|
2021-03-24 16:18:04 +01:00
|
|
|
#include "storage.h"
|
2021-01-26 16:14:58 +01:00
|
|
|
|
2021-03-31 15:57:12 +02:00
|
|
|
// Get external flash device
|
2021-03-31 17:06:38 +02:00
|
|
|
#if (CONFIG_SPI_NOR - 0) || DT_NODE_HAS_STATUS(DT_INST(0, jedec_spi_nor), okay)
|
2021-03-31 15:57:12 +02:00
|
|
|
#define FLASH_DEVICE DT_LABEL(DT_INST(0, jedec_spi_nor))
|
|
|
|
#define FLASH_NAME "JEDEC SPI-NOR"
|
2021-03-31 17:06:38 +02:00
|
|
|
#elif (CONFIG_NORDIC_QSPI_NOR - 0) || DT_NODE_HAS_STATUS(DT_INST(0, nordic_qspi_nor), okay)
|
2021-03-31 15:57:12 +02:00
|
|
|
#define FLASH_DEVICE DT_LABEL(DT_INST(0, nordic_qspi_nor))
|
|
|
|
#define FLASH_NAME "JEDEC QSPI-NOR"
|
|
|
|
#else
|
|
|
|
#error Unsupported flash driver
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 16:14:58 +01:00
|
|
|
// Maybe use this as param for init function
|
2021-03-31 16:01:39 +02:00
|
|
|
#define SEC_COUNT 192U
|
2021-02-18 13:45:29 +01:00
|
|
|
|
|
|
|
#define STORED_CONTACTS_INFO_ID 0
|
|
|
|
#define CONTACTS_OFFSET 1
|
|
|
|
#define MAX_CONTACTS 65535
|
2021-01-26 16:14:58 +01:00
|
|
|
|
|
|
|
static struct nvs_fs fs;
|
|
|
|
|
2021-03-31 16:31:58 +02:00
|
|
|
struct k_mutex fs_mutex;
|
|
|
|
|
2021-03-23 18:11:11 +01:00
|
|
|
// Information about currently stored contacts
|
2021-03-24 16:18:04 +01:00
|
|
|
static stored_contacts_information_t contact_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) {
|
|
|
|
return (storage_id_t)(sn % MAX_CONTACTS) + CONTACTS_OFFSET;
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
size_t size = sizeof(contact_information);
|
|
|
|
int rc = nvs_read(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
|
|
|
|
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
|
|
|
|
rc = nvs_write(&fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
2021-03-24 16:18:04 +01:00
|
|
|
if (rc <= 0) {
|
2021-03-23 18:11:11 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:44:35 +01:00
|
|
|
/**
|
|
|
|
* 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));
|
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-03-31 17:06:38 +02:00
|
|
|
return rc;
|
2021-03-24 15:44:35 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 17:06:38 +02:00
|
|
|
record_sequence_number_t get_next_sequence_number() {
|
2021-03-24 15:44:35 +01:00
|
|
|
if (contact_information.count >= MAX_CONTACTS) {
|
2021-03-24 15:52:02 +01:00
|
|
|
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
|
2021-03-24 15:44:35 +01:00
|
|
|
} else {
|
|
|
|
contact_information.count++;
|
|
|
|
}
|
|
|
|
save_storage_information();
|
2021-03-31 17:06:38 +02:00
|
|
|
return get_latest_sequence_number();
|
2021-03-24 15:44:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:45:29 +01:00
|
|
|
int init_contact_storage(void) {
|
2021-03-31 16:31:58 +02:00
|
|
|
k_mutex_init(&fs_mutex);
|
2021-01-26 16:14:58 +01:00
|
|
|
int rc = 0;
|
|
|
|
struct flash_pages_info info;
|
|
|
|
// define the nvs file system
|
|
|
|
fs.offset = FLASH_AREA_OFFSET(storage);
|
2021-03-31 16:31:58 +02:00
|
|
|
fs.nvs_lock = fs_mutex;
|
2021-03-31 15:57:12 +02:00
|
|
|
rc = flash_get_page_info_by_offs(device_get_binding(FLASH_DEVICE), 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-01-26 16:14:58 +01:00
|
|
|
return rc;
|
|
|
|
}
|
2021-03-31 16:01:39 +02:00
|
|
|
fs.sector_size = info.size * 4;
|
2021-01-26 16:14:58 +01:00
|
|
|
fs.sector_count = SEC_COUNT;
|
|
|
|
|
2021-03-31 15:57:12 +02:00
|
|
|
rc = nvs_init(&fs, FLASH_DEVICE);
|
2021-03-24 16:18:04 +01:00
|
|
|
if (rc) {
|
2021-03-23 18:11:11 +01:00
|
|
|
// Error during nvs_init
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current storage information
|
|
|
|
rc = load_storage_information();
|
|
|
|
|
|
|
|
printk("Currently %d contacts stored!\n", contact_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-02-18 13:45:29 +01:00
|
|
|
return rc;
|
2021-02-01 23:17:48 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:45:29 +01:00
|
|
|
int load_contact(contact_t* dest, record_sequence_number_t sn) {
|
|
|
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
|
|
|
int rc = nvs_read(&fs, id, dest, sizeof(*dest));
|
2021-03-17 14:49:07 +01: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-02-18 13:45:29 +01:00
|
|
|
int add_contact(contact_t* src) {
|
2021-03-31 17:06:38 +02:00
|
|
|
record_sequence_number_t curr_sn = get_next_sequence_number();
|
2021-02-18 13:45:29 +01:00
|
|
|
storage_id_t id = convert_sn_to_storage_id(curr_sn);
|
|
|
|
|
|
|
|
int rc = nvs_write(&fs, id, src, sizeof(*src));
|
|
|
|
if (rc > 0) {
|
|
|
|
return 0;
|
2021-02-03 19:39:01 +01:00
|
|
|
}
|
2021-02-18 13:45:29 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-03-11 00:32:36 +01:00
|
|
|
// TODO handle start and end
|
2021-02-18 13:45:29 +01:00
|
|
|
int delete_contact(record_sequence_number_t sn) {
|
|
|
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
2021-03-24 16:18:04 +01:00
|
|
|
int rc = nvs_delete(&fs, id);
|
|
|
|
if (!rc) {
|
2021-03-31 17:06:38 +02:00
|
|
|
if (sn_equal(sn, get_latest_sequence_number())) {
|
|
|
|
contact_information.count--;
|
|
|
|
} else if (sn_equal(sn, get_oldest_sequence_number())) {
|
2021-03-24 16:18:04 +01:00
|
|
|
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
|
2021-03-31 17:06:38 +02:00
|
|
|
contact_information.count--;
|
2021-03-24 16:18:04 +01:00
|
|
|
}
|
|
|
|
save_storage_information();
|
|
|
|
}
|
|
|
|
return rc;
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
record_sequence_number_t get_latest_sequence_number() {
|
2021-03-23 18:11:11 +01:00
|
|
|
return sn_mask(contact_information.oldest_contact + contact_information.count);
|
2021-02-18 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
record_sequence_number_t get_oldest_sequence_number() {
|
|
|
|
return contact_information.oldest_contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t get_num_contacts() {
|
2021-03-11 00:32:36 +01:00
|
|
|
return contact_information.count;
|
|
|
|
}
|