mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2024-11-12 22:48:52 +01:00
Implement api for storage
This commit is contained in:
parent
d48245657e
commit
9b55b3c8fd
@ -23,12 +23,13 @@
|
||||
#include "contacts.h"
|
||||
#include "exposure-notification.h"
|
||||
#include "covid.h"
|
||||
#include "storage.h"
|
||||
|
||||
static contact_t contacts[MAX_CONTACTS];
|
||||
static uint32_t contact_count = 0;
|
||||
|
||||
static period_contacts_t period_contacts[PERIODS];
|
||||
static int period_index = 0;
|
||||
static int period_index;
|
||||
static int32_t next_infected_key_id = 0;
|
||||
|
||||
void print_key(_ENBaseKey* key){
|
||||
@ -130,8 +131,17 @@ void key_change(int current_period_index){
|
||||
print_aem(&period_contacts[period_index].period_contacts[index].associated_encrypted_metadata);
|
||||
printk(" max rssi %i, cnt %u, duration %u\n", period_contacts[period_index].period_contacts[index].max_rssi, period_contacts[period_index].period_contacts[index].cnt, period_contacts[period_index].period_contacts[index].duration);
|
||||
period_contacts[period_index].cnt++;
|
||||
|
||||
printk("Attempting to store contact %i...\n", i);
|
||||
int rc = add_contact(&contacts[i]);
|
||||
if(rc) {
|
||||
printk("Failed to store contact %i! (err %d)\n", i, rc);
|
||||
} else {
|
||||
printk("Successfully stored contact %i!\n", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
contact_count = 0;
|
||||
}
|
||||
|
||||
@ -158,6 +168,8 @@ void key_change(int current_period_index){
|
||||
struct device *dev;
|
||||
|
||||
void add_infected_key(period_t* period){
|
||||
|
||||
// TODO lome: Do we need it?
|
||||
|
||||
//printk("Interval: %u\n", period->periodInterval);
|
||||
//printk("RPI: "); print_rpi((rolling_proximity_identifier_t*)&period->periodKey); printk("\n");
|
||||
@ -194,6 +206,8 @@ uint32_t get_next_infected_key_id(){
|
||||
void init_contacts(){
|
||||
contact_count = 0;
|
||||
period_index = 0;
|
||||
// period_index = get_current_period_nr();
|
||||
printk("Starting with period %d\n", period_index);
|
||||
|
||||
dev = device_get_binding(LED1);
|
||||
if (dev == NULL) {
|
||||
|
12
src/main.c
12
src/main.c
@ -30,6 +30,12 @@ void main(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = init_contact_storage();
|
||||
if (err) {
|
||||
printk("init storage failed (err %d)\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
printk("init contacts\n");
|
||||
init_contacts();
|
||||
err = init_io();
|
||||
@ -59,12 +65,6 @@ void main(void) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = init_storage();
|
||||
if (err) {
|
||||
printk("init storage failed (err %d)\n", err);
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
do_covid();
|
||||
do_gatt();
|
||||
|
103
src/storage.c
103
src/storage.c
@ -9,13 +9,29 @@
|
||||
#include "storage.h"
|
||||
|
||||
// Maybe use this as param for init function
|
||||
#define SEC_COUNT 10
|
||||
#define PERIOD_COUNTER_ID 0
|
||||
#define PERIOD_OFFSET 1
|
||||
#define SEC_COUNT 10U
|
||||
|
||||
#define STORED_CONTACTS_INFO_ID 0
|
||||
#define CONTACTS_OFFSET 1
|
||||
#define MAX_CONTACTS 65535
|
||||
|
||||
static struct nvs_fs fs;
|
||||
|
||||
int init_storage(void) {
|
||||
// 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() {
|
||||
contact_information.latest_contact++;
|
||||
if (contact_information.latest_contact - contact_information.oldest_contact >= MAX_CONTACTS ) {
|
||||
contact_information.oldest_contact++;
|
||||
}
|
||||
}
|
||||
|
||||
int init_contact_storage(void) {
|
||||
int rc = 0;
|
||||
struct flash_pages_info info;
|
||||
// define the nvs file system
|
||||
@ -25,55 +41,58 @@ int init_storage(void) {
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
fs.sector_size = info.size;
|
||||
fs.sector_size = info.size * 4;
|
||||
fs.sector_count = SEC_COUNT;
|
||||
|
||||
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
int has_contact(record_sequence_number_t sn) {
|
||||
// Treat case of wrap-around and non-wrap-around different
|
||||
if (contact_information.oldest_contact <= contact_information.latest_contact) {
|
||||
return sn >= contact_information.oldest_contact && sn <= contact_information.latest_contact;
|
||||
} else {
|
||||
return sn <= contact_information.oldest_contact || sn >= contact_information.latest_contact;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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 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 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 <= 0) {
|
||||
return rc;
|
||||
contact_t test;
|
||||
nvs_read(&fs, id, &test, sizeof(test));
|
||||
int rc = nvs_write(&fs, id, src, sizeof(*src));
|
||||
if (rc > 0) {
|
||||
increase_sn_counter();
|
||||
return 0;
|
||||
}
|
||||
rc = nvs_write(&fs, period * 2 + 1 + PERIOD_OFFSET, &contacts.period_contacts,
|
||||
sizeof(period_contact_t) * contacts.cnt);
|
||||
return rc;
|
||||
}
|
||||
|
||||
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;
|
||||
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() {
|
||||
return contact_information.latest_contact;
|
||||
}
|
||||
|
||||
record_sequence_number_t get_oldest_sequence_number() {
|
||||
return contact_information.oldest_contact;
|
||||
}
|
||||
|
||||
uint32_t get_num_contacts() {
|
||||
return get_latest_sequence_number() - get_oldest_sequence_number();
|
||||
}
|
@ -1,21 +1,71 @@
|
||||
#ifndef STORAGE_H
|
||||
#define STORAGE_H
|
||||
#ifndef CONTACT_STORAGE_H
|
||||
#define CONTACT_STORAGE_H
|
||||
|
||||
#include "contacts.h"
|
||||
#include "contacts.h" // Requires contact_t in contacts.h!
|
||||
|
||||
// TODO: This should be masked to k=24 bits
|
||||
typedef uint32_t record_sequence_number_t;
|
||||
|
||||
typedef uint16_t storage_id_t;
|
||||
|
||||
typedef struct stored_contacts_information {
|
||||
record_sequence_number_t oldest_contact;
|
||||
record_sequence_number_t latest_contact;
|
||||
} stored_contacts_information_t;
|
||||
|
||||
/**
|
||||
* Initialize the storage api.
|
||||
* Initializes the contact storage component
|
||||
* @return 0 for success
|
||||
*/
|
||||
int init_storage(void);
|
||||
int init_contact_storage();
|
||||
|
||||
/**
|
||||
* Store all contacts of a period in flash.
|
||||
* Checks if a contact is available with number sn
|
||||
* @param sn
|
||||
* @return 1 if available, 0 otherwise or in case of an error
|
||||
*/
|
||||
int store_period_contacts(period_contacts_t contacts);
|
||||
int has_contact(record_sequence_number_t sn);
|
||||
|
||||
/**
|
||||
* Get all contacts for a certain period.
|
||||
* Loads the contact with number sn into the destination struct
|
||||
* @param dest
|
||||
* @param sn
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
period_contacts_t get_contacts_for_period(uint16_t period);
|
||||
int load_contact(contact_t* dest, record_sequence_number_t sn);
|
||||
|
||||
/**
|
||||
* Stores the contact from src with number sn, increases latest sequence number
|
||||
* @param sn
|
||||
* @param src
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int add_contact(contact_t* src);
|
||||
|
||||
/**
|
||||
* Deletes the contact from storage with number sn
|
||||
* @param sn the sequence number to delete
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int delete_contact(record_sequence_number_t sn);
|
||||
|
||||
/**
|
||||
* TODO: How to handle if none is available?
|
||||
* @return The latest available sequence number (Caution: can actually be lower than the oldes in case of a
|
||||
* wrap-around!)
|
||||
*/
|
||||
record_sequence_number_t get_latest_sequence_number();
|
||||
|
||||
/**
|
||||
* TODO: How to handle if none is available?
|
||||
* @return The oldest available sequence number
|
||||
*/
|
||||
record_sequence_number_t get_oldest_sequence_number();
|
||||
|
||||
/**
|
||||
* TODO: How to handle if none is available?
|
||||
* @return The amount of contacts, usually get_latest_sequence_number() - get_oldest_sequence_number()
|
||||
*/
|
||||
uint32_t get_num_contacts();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user