1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-05-29 08:48:15 +02:00
CovidBracelet/src/storage.c

100 lines
2.6 KiB
C
Raw Normal View History

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
2021-03-11 00:32:36 +01:00
#define SEC_COUNT 8U
2021-02-18 13:45:29 +01:00
#define STORED_CONTACTS_INFO_ID 0
#define CONTACTS_OFFSET 1
#define MAX_CONTACTS 65535
2021-03-11 00:32:36 +01:00
#define ADDRESS_ID 1
2021-01-26 16:14:58 +01:00
static struct nvs_fs fs;
2021-02-18 13:45:29 +01:00
// TODO lome: load this from flash
static stored_contacts_information_t contact_information;
inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
return (storage_id_t)(sn % MAX_CONTACTS) + CONTACTS_OFFSET;
}
void increase_sn_counter() {
2021-03-11 00:32:36 +01:00
if (contact_information.count >= MAX_CONTACTS ) {
2021-02-18 13:45:29 +01:00
contact_information.oldest_contact++;
}
}
int init_contact_storage(void) {
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);
rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), fs.offset, &info);
if (rc) {
return rc;
}
2021-03-11 00:32:36 +01:00
fs.sector_size = info.size;
2021-01-26 16:14:58 +01:00
fs.sector_count = SEC_COUNT;
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
2021-03-11 00:32:36 +01:00
// char buf[16];
// rc = nvs_read(&fs, ADDRESS_ID, &buf, sizeof(buf));
// if (rc > 0) { /* item was found, show it */
// printk("Id: %d, Address: %s\n", ADDRESS_ID, buf);
// } else { /* item was not found, add it */
// strcpy(buf, "192.168.1.1");
// printk("No address found, adding %s at id %d\n", buf, ADDRESS_ID);
// (void)nvs_write(&fs, ADDRESS_ID, &buf, strlen(buf) + 1);
// }
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));
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) {
record_sequence_number_t curr_sn = get_latest_sequence_number() + 1;
storage_id_t id = convert_sn_to_storage_id(curr_sn);
int rc = nvs_write(&fs, id, src, sizeof(*src));
if (rc > 0) {
increase_sn_counter();
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);
return nvs_delete(&fs, id);
}
record_sequence_number_t get_latest_sequence_number() {
2021-03-11 00:32:36 +01:00
return 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;
}