1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-12-05 00:55:43 +01:00

Use record_t for storing contacts

This commit is contained in:
H1ghBre4k3r 2021-04-21 17:58:16 +02:00 committed by Patrick Rathje
parent 9ae226d50b
commit c8607507b7
5 changed files with 45 additions and 33 deletions

View File

@ -17,6 +17,7 @@
#include "covid_types.h"
#include "contacts.h"
#include "covid.h"
#include "ens/storage.h"
#ifndef COVID_MEASURE_PERFORMANCE
#define COVID_MEASURE_PERFORMANCE 0
@ -74,7 +75,16 @@ static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type, str
covid_adv_svd_t *rx_adv = (covid_adv_svd_t *)buf->data;
if (rx_adv->ens == COVID_ENS)
{
check_add_contact(k_uptime_get() / 1000, &rx_adv->rolling_proximity_identifier, &rx_adv->associated_encrypted_metadata, rssi);
printk("Attempting to store contact...\n");
record_t contact;
uint32_t timestamp = k_uptime_get() / 1000;
memcpy(&contact.rssi, &rssi, sizeof(contact.rssi));
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);
printk("Contact stored (err %d)\n", rc);
// check_add_contact(k_uptime_get() / 1000, &rx_adv->rolling_proximity_identifier, &rx_adv->associated_encrypted_metadata, rssi);
}
}
net_buf_simple_pull(buf, len - 1); //consume the rest, note we already consumed one byte via net_buf_simple_pull_u8(buf)
@ -312,10 +322,11 @@ static void check_keys(struct k_work *work)
memcpy(&covid_adv_svd.rolling_proximity_identifier, &intervalIdentifier, sizeof(rolling_proximity_identifier_t));
memcpy(&covid_adv_svd.associated_encrypted_metadata, &encryptedMetadata, sizeof(associated_encrypted_metadata_t));
if (!init)
{
key_change(current_period_index);
}
// // TODO lome: remove this?
// if (!init)
// {
// key_change(current_period_index);
// }
init = 0;
}
}

View File

@ -24,7 +24,7 @@ record_t* ens_records_iterator_next(record_iterator_t* iter) {
// What is this?
while (next == NULL) {
contact_t contact;
record_t contact;
// try to load the next contact
int res = load_contact(&contact, iter->sn_next);
@ -32,7 +32,7 @@ record_t* ens_records_iterator_next(record_iterator_t* iter) {
next = &iter->current;
memcpy(&next->associated_encrypted_metadata, &contact.associated_encrypted_metadata, sizeof(associated_encrypted_metadata_t));
memcpy(&next->rolling_proximity_identifier, &contact.rolling_proximity_identifier, sizeof(rolling_proximity_identifier_t));
memcpy(&next->rssi, &contact.max_rssi, sizeof(rssi_t));
memcpy(&next->rssi, &contact.rssi, sizeof(rssi_t));
memcpy(&next->sn, &iter->sn_next, sizeof(record_sequence_number_t));
// TODO lome: timestamp?

View File

@ -10,14 +10,6 @@
#include "sequencenumber.h"
#include "storage.h"
typedef struct record {
record_sequence_number_t sn; // TODO: Convert Sequence Number
uint32_t timestamp; // TODO: Seconds from january first 2000 (UTC+0)
rssi_t rssi; // TODO: Check correct
rolling_proximity_identifier_t rolling_proximity_identifier;
associated_encrypted_metadata_t associated_encrypted_metadata;
} record_t;
typedef struct record_iterator {
record_t current;
record_sequence_number_t sn_next;

View File

@ -107,7 +107,7 @@ int init_contact_storage(void) {
return rc;
}
int load_contact(contact_t* dest, record_sequence_number_t sn) {
int load_contact(record_t* dest, record_sequence_number_t sn) {
storage_id_t id = convert_sn_to_storage_id(sn);
// int rc = nvs_read(&info_fs, id, dest, sizeof(*dest));
int rc = ens_fs_read(&ens_fs, id, dest);
@ -117,7 +117,7 @@ int load_contact(contact_t* dest, record_sequence_number_t sn) {
return 0;
}
int add_contact(contact_t* src) {
int add_contact(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());
@ -129,6 +129,7 @@ int add_contact(contact_t* src) {
// Actually increment sn
record_sequence_number_t curr_sn = get_next_sequence_number();
src->sn = curr_sn;
storage_id_t id = convert_sn_to_storage_id(curr_sn);
int rc = ens_fs_write(&ens_fs, id, src);
@ -138,21 +139,20 @@ int add_contact(contact_t* src) {
return rc;
}
// TODO lome: Implement delete flag in ens_fs.c
int delete_contact(record_sequence_number_t sn) {
// storage_id_t id = convert_sn_to_storage_id(sn);
// int rc = nvs_delete(&info_fs, id);
// if (!rc) {
// if (sn_equal(sn, get_latest_sequence_number())) {
// contact_information.count--;
// } else if (sn_equal(sn, get_oldest_sequence_number())) {
// contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
// contact_information.count--;
// }
// save_storage_information();
// }
// return rc;
return 0;
// TODO lome: Use lock
storage_id_t id = convert_sn_to_storage_id(sn);
int rc = ens_fs_delete(&ens_fs, id);
if (!rc) {
if (sn_equal(sn, get_latest_sequence_number())) {
contact_information.count--;
} else if (sn_equal(sn, get_oldest_sequence_number())) {
contact_information.oldest_contact = sn_increment(contact_information.oldest_contact);
contact_information.count--;
}
save_storage_information();
}
return rc;
}
record_sequence_number_t get_latest_sequence_number() {

View File

@ -6,6 +6,15 @@
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)
rssi_t rssi; // TODO: Check correct
rolling_proximity_identifier_t rolling_proximity_identifier;
associated_encrypted_metadata_t associated_encrypted_metadata;
} record_t;
typedef struct stored_contacts_information {
record_sequence_number_t oldest_contact;
record_sequence_number_t count;
@ -23,7 +32,7 @@ int init_contact_storage();
* @param sn
* @return 0 in case of success
*/
int load_contact(contact_t* dest, record_sequence_number_t sn);
int load_contact(record_t* dest, record_sequence_number_t sn);
/**
* Stores the contact from src with number sn, increases latest sequence number
@ -31,7 +40,7 @@ int load_contact(contact_t* dest, record_sequence_number_t sn);
* @param src
* @return 0 in case of success
*/
int add_contact(contact_t* src);
int add_contact(record_t* src);
/**
* Deletes the contact from storage with number sn