mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2025-01-09 12:44:24 +01:00
Use record instead of contact as naming convention
This commit is contained in:
parent
36ad239a87
commit
970a42aea8
@ -82,7 +82,7 @@ static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type, str
|
|||||||
memcpy(&contact.associated_encrypted_metadata, &rx_adv->associated_encrypted_metadata, sizeof(contact.associated_encrypted_metadata));
|
memcpy(&contact.associated_encrypted_metadata, &rx_adv->associated_encrypted_metadata, sizeof(contact.associated_encrypted_metadata));
|
||||||
memcpy(&contact.rolling_proximity_identifier, &rx_adv->rolling_proximity_identifier, sizeof(contact.rolling_proximity_identifier));
|
memcpy(&contact.rolling_proximity_identifier, &rx_adv->rolling_proximity_identifier, sizeof(contact.rolling_proximity_identifier));
|
||||||
memcpy(&contact.timestamp, ×tamp, sizeof(contact.timestamp));
|
memcpy(&contact.timestamp, ×tamp, sizeof(contact.timestamp));
|
||||||
int rc = add_contact(&contact);
|
int rc = add_record(&contact);
|
||||||
printk("Contact stored (err %d)\n", rc);
|
printk("Contact stored (err %d)\n", rc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ int ens_records_iterator_init_range(record_iterator_t* iterator,
|
|||||||
record_sequence_number_t* opt_end) {
|
record_sequence_number_t* opt_end) {
|
||||||
iterator->sn_next = opt_start ? *opt_start : get_oldest_sequence_number();
|
iterator->sn_next = opt_start ? *opt_start : get_oldest_sequence_number();
|
||||||
iterator->sn_end = opt_end ? *opt_end : get_latest_sequence_number();
|
iterator->sn_end = opt_end ? *opt_end : get_latest_sequence_number();
|
||||||
if (get_num_contacts() == 0) {
|
if (get_num_records() == 0) {
|
||||||
iterator->finished = true; // no contacts -> no iteration :)
|
iterator->finished = true; // no contacts -> no iteration :)
|
||||||
} else {
|
} else {
|
||||||
iterator->finished = false;
|
iterator->finished = false;
|
||||||
@ -34,11 +34,11 @@ int find_record_via_binary_search(record_t* record,
|
|||||||
record_t end_record;
|
record_t end_record;
|
||||||
|
|
||||||
// load the initial start and end record
|
// load the initial start and end record
|
||||||
int rc = load_contact(&start_record, start);
|
int rc = load_record(&start_record, start);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
rc = load_contact(&end_record, end);
|
rc = load_record(&end_record, end);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ int find_record_via_binary_search(record_t* record,
|
|||||||
do {
|
do {
|
||||||
// calculate the contact in the middle between start and end and load it
|
// calculate the contact in the middle between start and end and load it
|
||||||
record_sequence_number_t middle = (start_record.sn + end_record.sn) / 2;
|
record_sequence_number_t middle = (start_record.sn + end_record.sn) / 2;
|
||||||
int rc = load_contact(record, middle);
|
int rc = load_record(record, middle);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* t
|
|||||||
|
|
||||||
// try to find the oldest contact in our timerange
|
// try to find the oldest contact in our timerange
|
||||||
record_t start_rec;
|
record_t start_rec;
|
||||||
int rc = load_contact(&start_rec, oldest_sn);
|
int rc = load_record(&start_rec, oldest_sn);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -85,7 +85,7 @@ int ens_records_iterator_init_timerange(record_iterator_t* iterator, uint32_t* t
|
|||||||
|
|
||||||
// try to find the newest contact within out timerange
|
// try to find the newest contact within out timerange
|
||||||
record_t end_rec;
|
record_t end_rec;
|
||||||
rc = load_contact(&end_rec, latest_sn);
|
rc = load_record(&end_rec, latest_sn);
|
||||||
if (rc) {
|
if (rc) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ record_t* ens_records_iterator_next(record_iterator_t* iter) {
|
|||||||
while (next == NULL && !iter->finished) {
|
while (next == NULL && !iter->finished) {
|
||||||
record_t contact;
|
record_t contact;
|
||||||
// try to load the next contact
|
// try to load the next contact
|
||||||
int res = load_contact(&contact, iter->sn_next);
|
int res = load_record(&contact, iter->sn_next);
|
||||||
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
next = &iter->current;
|
next = &iter->current;
|
||||||
|
@ -43,7 +43,7 @@ enum {
|
|||||||
|
|
||||||
// TODO: this function could be made asynchronous to handle delays in contact_storage reads?!
|
// TODO: this function could be made asynchronous to handle delays in contact_storage reads?!
|
||||||
// TODO: How can we handle iteration while records are being added or deleted? (should be safe as long as the
|
// TODO: How can we handle iteration while records are being added or deleted? (should be safe as long as the
|
||||||
// load_contact function is thread safe?!)
|
// load_record function is thread safe?!)
|
||||||
uint8_t ens_records_iterate_with_callback(record_iterator_t* iter, ens_record_iterator_cb_t cb, void* userdata);
|
uint8_t ens_records_iterate_with_callback(record_iterator_t* iter, ens_record_iterator_cb_t cb, void* userdata);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -23,7 +23,7 @@ static struct k_mutex info_fs_lock;
|
|||||||
static ens_fs_t ens_fs;
|
static ens_fs_t ens_fs;
|
||||||
|
|
||||||
// Information about currently stored contacts
|
// Information about currently stored contacts
|
||||||
static stored_contacts_information_t contact_information = {.oldest_contact = 0, .count = 0};
|
static stored_records_information_t record_information = {.oldest_contact = 0, .count = 0};
|
||||||
|
|
||||||
inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
|
inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
|
||||||
return (storage_id_t)(sn % MAX_CONTACTS);
|
return (storage_id_t)(sn % MAX_CONTACTS);
|
||||||
@ -34,13 +34,13 @@ inline storage_id_t convert_sn_to_storage_id(record_sequence_number_t sn) {
|
|||||||
*/
|
*/
|
||||||
int load_storage_information() {
|
int load_storage_information() {
|
||||||
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
||||||
size_t size = sizeof(contact_information);
|
size_t size = sizeof(record_information);
|
||||||
int rc = nvs_read(&info_fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
int rc = nvs_read(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, size);
|
||||||
|
|
||||||
// Check, if read what we wanted
|
// Check, if read what we wanted
|
||||||
if (rc != size) {
|
if (rc != size) {
|
||||||
// Write our initial data to storage
|
// Write our initial data to storage
|
||||||
rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
|
rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, size);
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ int load_storage_information() {
|
|||||||
*/
|
*/
|
||||||
int save_storage_information() {
|
int save_storage_information() {
|
||||||
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
||||||
int rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &contact_information, sizeof(contact_information));
|
int rc = nvs_write(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, sizeof(record_information));
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
printk("Something went wrong after saving storage information.\n");
|
printk("Something went wrong after saving storage information.\n");
|
||||||
}
|
}
|
||||||
@ -64,10 +64,10 @@ int save_storage_information() {
|
|||||||
|
|
||||||
record_sequence_number_t get_next_sequence_number() {
|
record_sequence_number_t get_next_sequence_number() {
|
||||||
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
||||||
if (contact_information.count >= MAX_CONTACTS) {
|
if (record_information.count >= MAX_CONTACTS) {
|
||||||
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
|
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
|
||||||
} else {
|
} else {
|
||||||
contact_information.count++;
|
record_information.count++;
|
||||||
}
|
}
|
||||||
save_storage_information();
|
save_storage_information();
|
||||||
record_sequence_number_t next_sn = get_latest_sequence_number();
|
record_sequence_number_t next_sn = get_latest_sequence_number();
|
||||||
@ -75,7 +75,7 @@ record_sequence_number_t get_next_sequence_number() {
|
|||||||
return next_sn;
|
return next_sn;
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_contact_storage(void) {
|
int init_record_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
|
||||||
@ -106,7 +106,7 @@ int init_contact_storage(void) {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
printk("Currently %d contacts stored!\n", contact_information.count);
|
printk("Currently %d contacts stored!\n", record_information.count);
|
||||||
printk("Space available: %d\n", FLASH_AREA_SIZE(storage));
|
printk("Space available: %d\n", FLASH_AREA_SIZE(storage));
|
||||||
|
|
||||||
// TODO lome: change size to sizeof(contact_struct)
|
// TODO lome: change size to sizeof(contact_struct)
|
||||||
@ -117,7 +117,7 @@ int init_contact_storage(void) {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_contact(record_t* dest, record_sequence_number_t sn) {
|
int load_record(record_t* dest, record_sequence_number_t sn) {
|
||||||
storage_id_t id = convert_sn_to_storage_id(sn);
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
||||||
int rc = ens_fs_read(&ens_fs, id, dest);
|
int rc = ens_fs_read(&ens_fs, id, dest);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
@ -126,7 +126,7 @@ int load_contact(record_t* dest, record_sequence_number_t sn) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_contact(record_t* src) {
|
int add_record(record_t* src) {
|
||||||
// Check, if next sn would be at start of page
|
// Check, if next sn would be at start of page
|
||||||
record_sequence_number_t potential_next_sn = sn_increment(get_latest_sequence_number());
|
record_sequence_number_t potential_next_sn = sn_increment(get_latest_sequence_number());
|
||||||
storage_id_t potential_next_id = convert_sn_to_storage_id(potential_next_sn);
|
storage_id_t potential_next_id = convert_sn_to_storage_id(potential_next_sn);
|
||||||
@ -143,16 +143,16 @@ int add_contact(record_t* src) {
|
|||||||
return ens_fs_write(&ens_fs, id, src);
|
return ens_fs_write(&ens_fs, id, src);
|
||||||
}
|
}
|
||||||
|
|
||||||
int delete_contact(record_sequence_number_t sn) {
|
int delete_record(record_sequence_number_t sn) {
|
||||||
storage_id_t id = convert_sn_to_storage_id(sn);
|
storage_id_t id = convert_sn_to_storage_id(sn);
|
||||||
int rc = ens_fs_delete(&ens_fs, id);
|
int rc = ens_fs_delete(&ens_fs, id);
|
||||||
if (!rc) {
|
if (!rc) {
|
||||||
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
||||||
if (sn_equal(sn, get_latest_sequence_number())) {
|
if (sn_equal(sn, get_latest_sequence_number())) {
|
||||||
contact_information.count--;
|
record_information.count--;
|
||||||
} else if (sn_equal(sn, get_oldest_sequence_number())) {
|
} else if (sn_equal(sn, get_oldest_sequence_number())) {
|
||||||
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
|
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
|
||||||
contact_information.count--;
|
record_information.count--;
|
||||||
}
|
}
|
||||||
save_storage_information();
|
save_storage_information();
|
||||||
k_mutex_unlock(&info_fs_lock);
|
k_mutex_unlock(&info_fs_lock);
|
||||||
@ -162,13 +162,13 @@ int delete_contact(record_sequence_number_t sn) {
|
|||||||
|
|
||||||
// TODO lome: do we need lock here aswell?
|
// TODO lome: do we need lock here aswell?
|
||||||
record_sequence_number_t get_latest_sequence_number() {
|
record_sequence_number_t get_latest_sequence_number() {
|
||||||
return GET_MASKED_SN((contact_information.oldest_contact + contact_information.count));
|
return GET_MASKED_SN((record_information.oldest_contact + record_information.count));
|
||||||
}
|
}
|
||||||
|
|
||||||
record_sequence_number_t get_oldest_sequence_number() {
|
record_sequence_number_t get_oldest_sequence_number() {
|
||||||
return contact_information.oldest_contact;
|
return record_information.oldest_contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t get_num_contacts() {
|
uint32_t get_num_records() {
|
||||||
return contact_information.count;
|
return record_information.count;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
typedef uint16_t storage_id_t;
|
typedef uint16_t storage_id_t;
|
||||||
|
|
||||||
// TODO lome: own datatype for storage
|
|
||||||
typedef struct record {
|
typedef struct record {
|
||||||
record_sequence_number_t sn; // TODO: Convert Sequence Number
|
record_sequence_number_t sn; // TODO: Convert Sequence Number
|
||||||
uint32_t timestamp; // TODO: Seconds from january first 2000 (UTC+0)
|
uint32_t timestamp; // TODO: Seconds from january first 2000 (UTC+0)
|
||||||
@ -16,39 +15,39 @@ typedef struct record {
|
|||||||
associated_encrypted_metadata_t associated_encrypted_metadata;
|
associated_encrypted_metadata_t associated_encrypted_metadata;
|
||||||
} record_t;
|
} record_t;
|
||||||
|
|
||||||
typedef struct stored_contacts_information {
|
typedef struct stored_records_information {
|
||||||
record_sequence_number_t oldest_contact;
|
record_sequence_number_t oldest_contact;
|
||||||
record_sequence_number_t count;
|
record_sequence_number_t count;
|
||||||
} stored_contacts_information_t;
|
} stored_records_information_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the contact storage component
|
* Initializes the contact storage component
|
||||||
* @return 0 for success
|
* @return 0 for success
|
||||||
*/
|
*/
|
||||||
int init_contact_storage();
|
int init_record_storage();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the contact with number sn into the destination struct
|
* Loads the record with number sn into the destination struct
|
||||||
* @param dest
|
* @param dest
|
||||||
* @param sn
|
* @param sn
|
||||||
* @return 0 in case of success
|
* @return 0 in case of success
|
||||||
*/
|
*/
|
||||||
int load_contact(record_t* dest, record_sequence_number_t sn);
|
int load_record(record_t* dest, record_sequence_number_t sn);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores the contact from src with number sn, increases latest sequence number
|
* Stores the record from src with number sn, increases latest sequence number
|
||||||
* @param sn
|
* @param sn
|
||||||
* @param src
|
* @param src
|
||||||
* @return 0 in case of success
|
* @return 0 in case of success
|
||||||
*/
|
*/
|
||||||
int add_contact(record_t* src);
|
int add_record(record_t* src);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the contact from storage with number sn
|
* Deletes the record from storage with number sn
|
||||||
* @param sn the sequence number to delete
|
* @param sn the sequence number to delete
|
||||||
* @return 0 in case of success
|
* @return 0 in case of success
|
||||||
*/
|
*/
|
||||||
int delete_contact(record_sequence_number_t sn);
|
int delete_record(record_sequence_number_t sn);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: How to handle if none is available?
|
* TODO: How to handle if none is available?
|
||||||
@ -67,6 +66,6 @@ record_sequence_number_t get_oldest_sequence_number();
|
|||||||
* TODO: How to handle if none is available?
|
* TODO: How to handle if none is available?
|
||||||
* @return The amount of contacts, usually get_latest_sequence_number() - get_oldest_sequence_number()
|
* @return The amount of contacts, usually get_latest_sequence_number() - get_oldest_sequence_number()
|
||||||
*/
|
*/
|
||||||
uint32_t get_num_contacts();
|
uint32_t get_num_records();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -13,10 +13,10 @@
|
|||||||
#include "contacts.h"
|
#include "contacts.h"
|
||||||
#include "covid.h"
|
#include "covid.h"
|
||||||
#include "covid_types.h"
|
#include "covid_types.h"
|
||||||
|
#include "ens/storage.h"
|
||||||
#include "exposure-notification.h"
|
#include "exposure-notification.h"
|
||||||
#include "gatt_service.h"
|
#include "gatt_service.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "ens/storage.h"
|
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
int err = 0;
|
int err = 0;
|
||||||
@ -30,7 +30,7 @@ void main(void) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = init_contact_storage();
|
err = init_record_storage();
|
||||||
if (err) {
|
if (err) {
|
||||||
printk("init storage failed (err %d)\n", err);
|
printk("init storage failed (err %d)\n", err);
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user