mirror of
https://github.com/CovidBraceletPrj/CovidBracelet.git
synced 2024-12-04 16:45:43 +01:00
Finalize refactorings of contacts and bloom module
This commit is contained in:
parent
cd780b122d
commit
22697403f7
@ -4,7 +4,7 @@
|
||||
#include "ens/storage.h"
|
||||
#include "exposure-notification.h"
|
||||
|
||||
typedef struct bloom_filter {
|
||||
typedef struct {
|
||||
uint8_t* data;
|
||||
size_t size;
|
||||
} bloom_filter_t;
|
||||
|
@ -37,7 +37,7 @@ void print_key(_ENBaseKey* key) {
|
||||
|
||||
void print_rpi(rolling_proximity_identifier_t* rpi) {
|
||||
for (int i = 0; i < sizeof(rolling_proximity_identifier_t); i++) {
|
||||
printk("%02x", rpi->data[i]);
|
||||
printk("%02x", rpi->b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,12 +48,7 @@ void print_aem(associated_encrypted_metadata_t* aem) {
|
||||
}
|
||||
|
||||
int register_record(record_t* record) {
|
||||
int rc = add_record(record);
|
||||
if (rc) {
|
||||
return rc;
|
||||
}
|
||||
// rc = bloom_add_record(record);
|
||||
return rc;
|
||||
return add_record(record);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,7 +63,7 @@ int get_number_of_infected_for_multiple_intervals_simple(infected_for_interval_i
|
||||
continue;
|
||||
}
|
||||
record_t* current;
|
||||
while (current = ens_records_iterator_next(&iterator)) {
|
||||
while ((current = ens_records_iterator_next(&iterator))) {
|
||||
if (memcmp(&(current->rolling_proximity_identifier), &ctx[i].interval_identifier,
|
||||
sizeof(rolling_proximity_identifier_t)) == 0) {
|
||||
ctx[i].infected++;
|
||||
@ -92,7 +87,7 @@ void fill_bloom_with_stored_records(bloom_filter_t* bloom) {
|
||||
|
||||
// fill bloom filter with records
|
||||
record_t* current;
|
||||
while (current = ens_records_iterator_next(&iterator)) {
|
||||
while ((current = ens_records_iterator_next(&iterator))) {
|
||||
bloom_add_record(bloom, &(current->rolling_proximity_identifier));
|
||||
}
|
||||
}
|
||||
@ -100,7 +95,7 @@ void fill_bloom_with_stored_records(bloom_filter_t* bloom) {
|
||||
/**
|
||||
* Fill the bloom filter with flash records and test passed RPIs against it.
|
||||
*/
|
||||
int64_t test_bloom_performance(infected_for_interval_ident_ctx_t* ctx, int count) {
|
||||
int64_t bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) {
|
||||
bloom_filter_t* bloom = bloom_init(get_num_records() * 2);
|
||||
if (!bloom) {
|
||||
printk("bloom init failed\n");
|
||||
@ -211,7 +206,7 @@ int reverse_bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) {
|
||||
}
|
||||
|
||||
record_t* current;
|
||||
while (current = ens_records_iterator_next(&iterator)) {
|
||||
while ((current = ens_records_iterator_next(&iterator))) {
|
||||
if (bloom_probably_has_record(bloom, &(current->rolling_proximity_identifier))) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (memcmp(&(current->rolling_proximity_identifier), &ctx[i].interval_identifier,
|
||||
@ -220,10 +215,6 @@ int reverse_bloom_filter(infected_for_interval_ident_ctx_t* ctx, int count) {
|
||||
amount++;
|
||||
break;
|
||||
}
|
||||
// TODO lome: maybe dont require orderred array, so this is not useful
|
||||
if (current->timestamp > ctx[i].search_end) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -266,52 +257,46 @@ void setup_test_data() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define INFECTED_INTERVALS_COUNT 2000
|
||||
// setup our ordered array with infected RPIs
|
||||
static infected_for_interval_ident_ctx_t infectedIntervals[INFECTED_INTERVALS_COUNT];
|
||||
|
||||
printk("Starting measurements with %d RPIs to seach and an infection rate of %d\n", INFECTED_INTERVALS_COUNT,
|
||||
CONFIG_TEST_INFECTED_RATE);
|
||||
|
||||
// measure_perf(get_number_of_infected_for_multiple_intervals_dumb, "dumb", infectedIntervals,
|
||||
// INFECTED_INTERVALS_COUNT);
|
||||
// measure_perf(get_number_of_infected_for_multiple_intervals_simple, "simple", infectedIntervals,
|
||||
// INFECTED_INTERVALS_COUNT);
|
||||
// measure_perf(get_number_of_infected_for_multiple_intervals_optimized, "optimized", infectedIntervals,
|
||||
// INFECTED_INTERVALS_COUNT);
|
||||
|
||||
// measure_perf(test_bloom_performance, "bloom", infectedIntervals, INFECTED_INTERVALS_COUNT);
|
||||
|
||||
measure_perf(reverse_bloom_filter, "bloom reverse", infectedIntervals, INFECTED_INTERVALS_COUNT);
|
||||
}
|
||||
|
||||
int init_contacts() {
|
||||
#if CONFIG_CONTACTS_PERFORM_RISC_CHECK_TEST
|
||||
reset_record_storage();
|
||||
setup_test_data();
|
||||
|
||||
// setup our ordered array with infected RPIs
|
||||
static infected_for_interval_ident_ctx_t infectedIntervals[CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT];
|
||||
|
||||
printk("Starting measurements with %d RPIs to seach and an infection rate of %d\n",
|
||||
CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT, CONFIG_TEST_INFECTED_RATE);
|
||||
|
||||
measure_perf(check_possible_contacts_for_intervals, "bloom reverse", infectedIntervals,
|
||||
CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a list of specified interval identifiers, whether they were probably met or not.
|
||||
* @param ctx list of interval identifiers to check
|
||||
* @param count amount of identifiers to check
|
||||
* @return the amount of met intervals, -ERRNO on error
|
||||
*/
|
||||
int check_possible_contacts_for_intervals(infected_for_interval_ident_ctx_t* ctx, int count) {
|
||||
#if CONFIG_CONTACTS_BLOOM_REVERSE
|
||||
return reverse_bloom_filter(ctx, count);
|
||||
#else
|
||||
return bloom_filter(ctx, count);
|
||||
#endif
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
ENPeriodKey periodKey;
|
||||
time_t start;
|
||||
time_t end;
|
||||
int met;
|
||||
} period_key_information_t;
|
||||
|
||||
// TODO lome: needed?
|
||||
int check_possible_contacts_for_periods(period_key_information_t* periodKey, int count) {
|
||||
infected_for_interval_ident_ctx_t intervalIdents[count * 144];
|
||||
return check_possible_contacts_for_intervals(intervalIdents, 0);
|
||||
int check_possible_contacts_for_periods(period_key_information_t periodKeyInformation[], int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
static infected_for_interval_ident_ctx_t intervalIdents[EN_TEK_ROLLING_PERIOD];
|
||||
int periodStart = en_get_interval_number(periodKeyInformation[i].start);
|
||||
for (int interval = 0; interval < EN_TEK_ROLLING_PERIOD; interval++) {
|
||||
en_derive_interval_identifier(&intervalIdents[interval].interval_identifier,
|
||||
&periodKeyInformation[i].periodKey, periodStart + interval);
|
||||
}
|
||||
int rc = check_possible_contacts_for_intervals(intervalIdents, 0);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
periodKeyInformation[i].met = rc;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -22,6 +22,12 @@ typedef struct {
|
||||
time_t search_end;
|
||||
} __packed infected_for_interval_ident_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
ENPeriodKey periodKey;
|
||||
time_t start;
|
||||
int met;
|
||||
} __packed period_key_information_t;
|
||||
|
||||
typedef int (*test_func_t)(infected_for_interval_ident_ctx_t* infectedIntervals, int count);
|
||||
|
||||
void print_key(_ENBaseKey* key);
|
||||
@ -37,25 +43,24 @@ void print_aem(associated_encrypted_metadata_t* aem);
|
||||
int register_record(record_t* record);
|
||||
|
||||
/**
|
||||
* Get the number of infected records for a given PeriodKey.
|
||||
*
|
||||
* @param key the PeriodKey
|
||||
* @param timestamp the timestamp of the period
|
||||
*
|
||||
* @returns the number of infected records or -ERRNO in case of an error
|
||||
* Initialize the contacts module.
|
||||
*/
|
||||
int get_number_of_infected_for_period(ENPeriodKey* key, time_t timestamp);
|
||||
|
||||
/**
|
||||
* Get the number of infected records for multiple IntervalIdentifier.
|
||||
*
|
||||
* @param ctx array of period keys and timestamps with field "infected" for the number of infected records
|
||||
* @param count the number of periods in the array
|
||||
*
|
||||
* @returns 0 in case of success, -ERRNO in case of an error
|
||||
*/
|
||||
// int get_number_of_infected_for_multiple_intervals(infected_for_period_key_ctx_t* ctx, int count);
|
||||
|
||||
int init_contacts();
|
||||
|
||||
/**
|
||||
* Check for a list of specified interval identifiers, whether they were probably met or not.
|
||||
* @param ctx list of interval identifiers to check
|
||||
* @param count amount of identifiers to check
|
||||
* @return the amount of met intervals, -ERRNO on error
|
||||
*/
|
||||
int check_possible_contacts_for_intervals(infected_for_interval_ident_ctx_t* ctx, int count);
|
||||
|
||||
/**
|
||||
* Check for a list of specified period keys, whether they were probably met or not.
|
||||
* @param ctx list of period keys and their meta information to check
|
||||
* @param count amount of period keys to check
|
||||
* @return -ERRNO on error, 0 otherwise
|
||||
*/
|
||||
int check_possible_contacts_for_periods(period_key_information_t periodKeyInformation[], int count);
|
||||
|
||||
#endif
|
||||
|
@ -7,26 +7,25 @@
|
||||
#ifndef COVID_TYPES_H
|
||||
#define COVID_TYPES_H
|
||||
|
||||
#include <exposure-notification.h>
|
||||
#include <zephyr.h>
|
||||
#include <zephyr/types.h>
|
||||
|
||||
#define COVID_ROLLING_PROXIMITY_IDENTIFIER_LEN 16
|
||||
|
||||
typedef struct rolling_proximity_identifier {
|
||||
uint8_t data[COVID_ROLLING_PROXIMITY_IDENTIFIER_LEN];
|
||||
} __packed rolling_proximity_identifier_t;
|
||||
typedef ENIntervalIdentifier rolling_proximity_identifier_t;
|
||||
|
||||
typedef struct bt_metadata {
|
||||
uint8_t version;
|
||||
uint8_t tx_power;
|
||||
uint8_t rsv1;
|
||||
uint8_t rsv2;
|
||||
} __packed bt_metadata_t;
|
||||
uint8_t version;
|
||||
uint8_t tx_power;
|
||||
uint8_t rsv1;
|
||||
uint8_t rsv2;
|
||||
} __packed bt_metadata_t;
|
||||
|
||||
//typedef struct bt_metadata bt_metadata_t;
|
||||
// typedef struct bt_metadata bt_metadata_t;
|
||||
|
||||
typedef struct associated_encrypted_metadata {
|
||||
uint8_t data[sizeof(bt_metadata_t)];
|
||||
typedef struct associated_encrypted_metadata {
|
||||
uint8_t data[sizeof(bt_metadata_t)];
|
||||
} __packed associated_encrypted_metadata_t;
|
||||
|
||||
typedef uint8_t rssi_t;
|
||||
|
@ -108,7 +108,7 @@ int init_record_storage(bool clean) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
int reset_record_storage() {
|
||||
void reset_record_storage() {
|
||||
k_mutex_lock(&info_fs_lock, K_FOREVER);
|
||||
record_information.count = 0;
|
||||
record_information.oldest_contact = 0;
|
||||
|
@ -32,7 +32,7 @@ int init_record_storage(bool clean);
|
||||
/**
|
||||
* Reset state of record storage.
|
||||
*/
|
||||
int reset_record_storage();
|
||||
void reset_record_storage();
|
||||
|
||||
/**
|
||||
* Loads the record with number sn into the destination struct
|
||||
|
@ -41,6 +41,17 @@ config CONTACTS_PERFORM_RISC_CHECK_TEST
|
||||
bool ""
|
||||
default n
|
||||
help
|
||||
Flag for performing tests.
|
||||
|
||||
Flag for performing tests. WARNING: This deletes all currently stored records.
|
||||
|
||||
config CONTACTS_RISC_CHECK_TEST_PUBLIC_INTERVAL_COUNT
|
||||
int ""
|
||||
default 1024
|
||||
help
|
||||
Amount of interval identifiers to test against stored test data.
|
||||
|
||||
config CONFIG_CONTACTS_BLOOM_REVERSE
|
||||
bool ""
|
||||
default n
|
||||
help
|
||||
Flag for toggling between bloom variants. Yes means, that the reverse bloom filter is used.
|
||||
endmenu
|
||||
|
Loading…
Reference in New Issue
Block a user