Use record instead of contact as naming convention

This commit is contained in:
H1ghBre4k3r 2021-05-03 22:07:15 +02:00 committed by Patrick Rathje
parent 36ad239a87
commit 970a42aea8
6 changed files with 41 additions and 42 deletions

View File

@ -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.rolling_proximity_identifier, &rx_adv->rolling_proximity_identifier, sizeof(contact.rolling_proximity_identifier));
memcpy(&contact.timestamp, &timestamp, sizeof(contact.timestamp));
int rc = add_contact(&contact);
int rc = add_record(&contact);
printk("Contact stored (err %d)\n", rc);
}
}

View File

@ -10,7 +10,7 @@ int ens_records_iterator_init_range(record_iterator_t* iterator,
record_sequence_number_t* opt_end) {
iterator->sn_next = opt_start ? *opt_start : get_oldest_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 :)
} else {
iterator->finished = false;
@ -34,11 +34,11 @@ int find_record_via_binary_search(record_t* record,
record_t 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) {
return rc;
}
rc = load_contact(&end_record, end);
rc = load_record(&end_record, end);
if (rc) {
return rc;
}
@ -46,7 +46,7 @@ int find_record_via_binary_search(record_t* record,
do {
// 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;
int rc = load_contact(record, middle);
int rc = load_record(record, middle);
if (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
record_t start_rec;
int rc = load_contact(&start_rec, oldest_sn);
int rc = load_record(&start_rec, oldest_sn);
if (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
record_t end_rec;
rc = load_contact(&end_rec, latest_sn);
rc = load_record(&end_rec, latest_sn);
if (rc) {
return rc;
}
@ -109,7 +109,7 @@ record_t* ens_records_iterator_next(record_iterator_t* iter) {
while (next == NULL && !iter->finished) {
record_t 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) {
next = &iter->current;

View File

@ -43,7 +43,7 @@ enum {
// 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
// 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);
#endif

View File

@ -23,7 +23,7 @@ static struct k_mutex info_fs_lock;
static ens_fs_t ens_fs;
// 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) {
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() {
k_mutex_lock(&info_fs_lock, K_FOREVER);
size_t size = sizeof(contact_information);
int rc = nvs_read(&info_fs, STORED_CONTACTS_INFO_ID, &contact_information, size);
size_t size = sizeof(record_information);
int rc = nvs_read(&info_fs, STORED_CONTACTS_INFO_ID, &record_information, size);
// Check, if read what we wanted
if (rc != size) {
// 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) {
return rc;
}
@ -54,7 +54,7 @@ int load_storage_information() {
*/
int save_storage_information() {
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) {
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() {
k_mutex_lock(&info_fs_lock, K_FOREVER);
if (contact_information.count >= MAX_CONTACTS) {
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
if (record_information.count >= MAX_CONTACTS) {
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
} else {
contact_information.count++;
record_information.count++;
}
save_storage_information();
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;
}
int init_contact_storage(void) {
int init_record_storage(void) {
int rc = 0;
struct flash_pages_info info;
// define the nvs file system
@ -106,7 +106,7 @@ int init_contact_storage(void) {
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));
// TODO lome: change size to sizeof(contact_struct)
@ -117,7 +117,7 @@ int init_contact_storage(void) {
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);
int rc = ens_fs_read(&ens_fs, id, dest);
if (rc < 0) {
@ -126,7 +126,7 @@ int load_contact(record_t* dest, record_sequence_number_t sn) {
return 0;
}
int add_contact(record_t* src) {
int add_record(record_t* src) {
// Check, if next sn would be at start of page
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);
@ -143,16 +143,16 @@ int add_contact(record_t* 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);
int rc = ens_fs_delete(&ens_fs, id);
if (!rc) {
k_mutex_lock(&info_fs_lock, K_FOREVER);
if (sn_equal(sn, get_latest_sequence_number())) {
contact_information.count--;
record_information.count--;
} else if (sn_equal(sn, get_oldest_sequence_number())) {
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
contact_information.count--;
record_information.oldest_contact = sn_increment(record_information.oldest_contact);
record_information.count--;
}
save_storage_information();
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?
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() {
return contact_information.oldest_contact;
return record_information.oldest_contact;
}
uint32_t get_num_contacts() {
return contact_information.count;
uint32_t get_num_records() {
return record_information.count;
}

View File

@ -7,7 +7,6 @@
typedef uint16_t storage_id_t;
// TODO lome: own datatype for storage
typedef struct record {
record_sequence_number_t sn; // TODO: Convert Sequence Number
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;
} record_t;
typedef struct stored_contacts_information {
typedef struct stored_records_information {
record_sequence_number_t oldest_contact;
record_sequence_number_t count;
} stored_contacts_information_t;
} stored_records_information_t;
/**
* Initializes the contact storage component
* @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 sn
* @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 src
* @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
* @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?
@ -67,6 +66,6 @@ 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();
uint32_t get_num_records();
#endif

View File

@ -13,10 +13,10 @@
#include "contacts.h"
#include "covid.h"
#include "covid_types.h"
#include "ens/storage.h"
#include "exposure-notification.h"
#include "gatt_service.h"
#include "io.h"
#include "ens/storage.h"
void main(void) {
int err = 0;
@ -30,7 +30,7 @@ void main(void) {
return;
}
err = init_contact_storage();
err = init_record_storage();
if (err) {
printk("init storage failed (err %d)\n", err);
return;