1
0
Fork 0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2025-07-15 13:58:13 +02:00

Implement api for storage

This commit is contained in:
H1ghBre4k3r 2021-02-18 13:45:29 +01:00 committed by Patrick Rathje
parent d48245657e
commit 9b55b3c8fd
4 changed files with 141 additions and 58 deletions

View file

@ -23,12 +23,13 @@
#include "contacts.h" #include "contacts.h"
#include "exposure-notification.h" #include "exposure-notification.h"
#include "covid.h" #include "covid.h"
#include "storage.h"
static contact_t contacts[MAX_CONTACTS]; static contact_t contacts[MAX_CONTACTS];
static uint32_t contact_count = 0; static uint32_t contact_count = 0;
static period_contacts_t period_contacts[PERIODS]; static period_contacts_t period_contacts[PERIODS];
static int period_index = 0; static int period_index;
static int32_t next_infected_key_id = 0; static int32_t next_infected_key_id = 0;
void print_key(_ENBaseKey* key){ 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); 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); 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++; 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; contact_count = 0;
} }
@ -159,6 +169,8 @@ struct device *dev;
void add_infected_key(period_t* period){ void add_infected_key(period_t* period){
// TODO lome: Do we need it?
//printk("Interval: %u\n", period->periodInterval); //printk("Interval: %u\n", period->periodInterval);
//printk("RPI: "); print_rpi((rolling_proximity_identifier_t*)&period->periodKey); printk("\n"); //printk("RPI: "); print_rpi((rolling_proximity_identifier_t*)&period->periodKey); printk("\n");
next_infected_key_id++; next_infected_key_id++;
@ -194,6 +206,8 @@ uint32_t get_next_infected_key_id(){
void init_contacts(){ void init_contacts(){
contact_count = 0; contact_count = 0;
period_index = 0; period_index = 0;
// period_index = get_current_period_nr();
printk("Starting with period %d\n", period_index);
dev = device_get_binding(LED1); dev = device_get_binding(LED1);
if (dev == NULL) { if (dev == NULL) {

View file

@ -30,6 +30,12 @@ void main(void) {
return; return;
} }
err = init_contact_storage();
if (err) {
printk("init storage failed (err %d)\n", err);
return;
}
printk("init contacts\n"); printk("init contacts\n");
init_contacts(); init_contacts();
err = init_io(); err = init_io();
@ -59,12 +65,6 @@ void main(void) {
return; return;
} }
err = init_storage();
if (err) {
printk("init storage failed (err %d)\n", err);
return;
}
do { do {
do_covid(); do_covid();
do_gatt(); do_gatt();

View file

@ -9,13 +9,29 @@
#include "storage.h" #include "storage.h"
// Maybe use this as param for init function // Maybe use this as param for init function
#define SEC_COUNT 10 #define SEC_COUNT 10U
#define PERIOD_COUNTER_ID 0
#define PERIOD_OFFSET 1 #define STORED_CONTACTS_INFO_ID 0
#define CONTACTS_OFFSET 1
#define MAX_CONTACTS 65535
static struct nvs_fs fs; 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; int rc = 0;
struct flash_pages_info info; struct flash_pages_info info;
// define the nvs file system // define the nvs file system
@ -25,55 +41,58 @@ int init_storage(void) {
if (rc) { if (rc) {
return rc; return rc;
} }
fs.sector_size = info.size; fs.sector_size = info.size * 4;
fs.sector_count = SEC_COUNT; fs.sector_count = SEC_COUNT;
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); 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 rc;
} }
return 0; return 0;
} }
uint16_t get_current_period_nr() { int add_contact(contact_t* src) {
uint16_t period_nr; record_sequence_number_t curr_sn = get_latest_sequence_number() + 1;
int rc = nvs_read(&fs, PERIOD_COUNTER_ID, &period_nr, sizeof(period_nr)); storage_id_t id = convert_sn_to_storage_id(curr_sn);
// 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 store_period_contacts(period_contacts_t contacts) { contact_t test;
uint16_t period = get_current_period_nr(); nvs_read(&fs, id, &test, sizeof(test));
int rc = nvs_write(&fs, period * 2 + PERIOD_OFFSET, &contacts.cnt, sizeof(contacts.cnt)); int rc = nvs_write(&fs, id, src, sizeof(*src));
if (rc <= 0) { if (rc > 0) {
return rc; 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; return rc;
} }
period_contacts_t get_contacts_for_period(uint16_t period) { int delete_contact(record_sequence_number_t sn) {
period_contacts_t contacts; storage_id_t id = convert_sn_to_storage_id(sn);
// Read the count of contacts in the requested period return nvs_delete(&fs, id);
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 record_sequence_number_t get_latest_sequence_number() {
printk("Period #%d not found. (err %d)", period, rc); return contact_information.latest_contact;
contacts.cnt = -1; }
} else {
// Read the actual contacts for the requested period record_sequence_number_t get_oldest_sequence_number() {
rc = nvs_read(&fs, period * 2 + 1 + PERIOD_OFFSET, &contacts.period_contacts, return contact_information.oldest_contact;
sizeof(period_contact_t) * contacts.cnt); }
if (rc <= 0) {
// Error while reading actual contacts for requested period uint32_t get_num_contacts() {
printk("Error while reading contacts for period #%d. (err %d)", period, rc); return get_latest_sequence_number() - get_oldest_sequence_number();
contacts.cnt = -1;
}
}
return contacts;
} }

View file

@ -1,21 +1,71 @@
#ifndef STORAGE_H #ifndef CONTACT_STORAGE_H
#define 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 #endif