mirror of
https://github.com/Lurkars/esp-ena.git
synced 2024-12-22 21:25:19 +01:00
update storage erase, fix exposure model, optimize exposure loop
This commit is contained in:
parent
af56fc6a12
commit
af3888ec87
@ -267,29 +267,38 @@ ena_exposure_config_t *ena_exposure_default_config(void)
|
||||
return &DEFAULT_ENA_EXPOSURE_CONFIG;
|
||||
}
|
||||
|
||||
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key)
|
||||
void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t temporary_exposure_key)
|
||||
{
|
||||
bool match = false;
|
||||
ena_beacon_t beacon;
|
||||
ena_exposure_information_t exposure_info;
|
||||
exposure_info.duration_minutes = 0;
|
||||
exposure_info.min_attenuation = INT_MAX;
|
||||
exposure_info.typical_attenuation = 0;
|
||||
exposure_info.report_type = temporary_exposure_key.report_type;
|
||||
uint8_t rpi[ENA_KEY_LENGTH];
|
||||
uint8_t rpik[ENA_KEY_LENGTH];
|
||||
ena_crypto_rpik(rpik, temporary_exposure_key.key_data);
|
||||
uint32_t beacons_count = ena_storage_beacons_count();
|
||||
for (int i = 0; i < temporary_exposure_key.rolling_period; i++)
|
||||
uint32_t timestamp_day_start = temporary_exposure_key.rolling_start_interval_number * ENA_TIME_WINDOW;
|
||||
uint32_t timestamp_day_end = temporary_exposure_key.rolling_start_interval_number * ENA_TIME_WINDOW + temporary_exposure_key.rolling_period * ENA_TIME_WINDOW;
|
||||
|
||||
if (beacon.timestamp_first > timestamp_day_start && beacon.timestamp_last < timestamp_day_end)
|
||||
{
|
||||
ena_crypto_rpi(rpi, rpik, temporary_exposure_key.rolling_start_interval_number + i);
|
||||
for (int y = 0; y < beacons_count; y++)
|
||||
ESP_LOGD(ENA_EXPOSURE_LOG, "matched timestamps!");
|
||||
|
||||
ESP_LOGD(ENA_EXPOSURE_LOG, "Beacon: %u,%u,%d", beacon.timestamp_first, beacon.timestamp_last, beacon.rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, beacon.rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, beacon.aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
|
||||
ESP_LOGD(ENA_EXPOSURE_LOG, "Key: %u,%u,%d", timestamp_day_start, timestamp_day_end, temporary_exposure_key.rolling_period);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, temporary_exposure_key.key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
bool match = false;
|
||||
ena_exposure_information_t exposure_info;
|
||||
exposure_info.duration_minutes = 0;
|
||||
exposure_info.min_attenuation = INT_MAX;
|
||||
exposure_info.typical_attenuation = 0;
|
||||
exposure_info.report_type = temporary_exposure_key.report_type;
|
||||
uint8_t rpi[ENA_KEY_LENGTH];
|
||||
uint8_t rpik[ENA_KEY_LENGTH];
|
||||
ena_crypto_rpik(rpik, temporary_exposure_key.key_data);
|
||||
|
||||
for (int i = 0; i < temporary_exposure_key.rolling_period; i++)
|
||||
{
|
||||
ena_storage_get_beacon(y, &beacon);
|
||||
ena_crypto_rpi(rpi, rpik, temporary_exposure_key.rolling_start_interval_number + i);
|
||||
if (memcmp(beacon.rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0)
|
||||
{
|
||||
match = true;
|
||||
exposure_info.day = temporary_exposure_key.rolling_start_interval_number * ENA_TIME_WINDOW;
|
||||
exposure_info.day = timestamp_day_start;
|
||||
exposure_info.duration_minutes += (ENA_BEACON_TRESHOLD / 60);
|
||||
exposure_info.typical_attenuation = (exposure_info.typical_attenuation + beacon.rssi) / 2;
|
||||
if (beacon.rssi < exposure_info.min_attenuation)
|
||||
@ -298,10 +307,21 @@ void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
if (match)
|
||||
{
|
||||
ena_storage_add_exposure_information(&exposure_info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key)
|
||||
{
|
||||
ena_beacon_t beacon;
|
||||
uint32_t beacons_count = ena_storage_beacons_count();
|
||||
for (int y = 0; y < beacons_count; y++)
|
||||
{
|
||||
ena_storage_add_exposure_information(&exposure_info);
|
||||
ena_storage_get_beacon(y, &beacon);
|
||||
ena_exposure_check(beacon, temporary_exposure_key);
|
||||
}
|
||||
}
|
@ -92,6 +92,28 @@ void ena_storage_write(size_t address, void *data, size_t size)
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_erase(size_t address, size_t size)
|
||||
{
|
||||
const int block_num = address / BLOCK_SIZE;
|
||||
// check for overflow
|
||||
if (address + size <= (block_num + 1) * BLOCK_SIZE)
|
||||
{
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(address, zeros, size);
|
||||
free(zeros);
|
||||
}
|
||||
else
|
||||
{
|
||||
const size_t block2_address = (block_num + 1) * BLOCK_SIZE;
|
||||
const size_t data2_size = address + size - block2_address;
|
||||
const size_t data1_size = size - data2_size;
|
||||
uint8_t *zeros = calloc(data1_size, sizeof(uint8_t));
|
||||
ena_storage_write(address, zeros, data1_size);
|
||||
free(zeros);
|
||||
ena_storage_erase(block2_address, data2_size);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
|
||||
{
|
||||
int block_num_start = address / BLOCK_SIZE;
|
||||
@ -302,7 +324,7 @@ void ena_storage_remove_beacon(uint32_t index)
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "remove beacon: %u", index);
|
||||
}
|
||||
|
||||
void ena_storage_erase(void)
|
||||
void ena_storage_erase_all(void)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
@ -330,9 +352,7 @@ void ena_storage_erase_tek(void)
|
||||
}
|
||||
|
||||
size_t size = sizeof(uint32_t) + stored * sizeof(ena_tek_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
ena_storage_erase(ENA_STORAGE_TEK_COUNT_ADDRESS, size);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d teks (size %u at %u)", stored, size, ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
@ -347,9 +367,7 @@ void ena_storage_erase_exposure_information(void)
|
||||
}
|
||||
|
||||
size_t size = sizeof(uint32_t) + stored * sizeof(ena_exposure_information_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
ena_storage_erase(ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS, size);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d exposure information (size %u at %u)", stored, size, ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
@ -365,9 +383,7 @@ void ena_storage_erase_temporary_beacon(void)
|
||||
}
|
||||
|
||||
size_t size = sizeof(uint32_t) + stored * sizeof(ena_beacon_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
ena_storage_erase(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, size);
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d temporary beacons (size %u at %u)", stored, size, ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS);
|
||||
}
|
||||
@ -378,10 +394,7 @@ void ena_storage_erase_beacon(void)
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
|
||||
size_t size = sizeof(uint32_t) + beacon_count * sizeof(ena_beacon_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
|
||||
ena_storage_erase(ENA_STORAGE_BEACONS_COUNT_ADDRESS, size);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d beacons (size %u at %u)", beacon_count, size, ENA_STORAGE_BEACONS_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
|
@ -88,12 +88,12 @@ void ena_run(void)
|
||||
void ena_start(void)
|
||||
{
|
||||
#if (CONFIG_ENA_STORAGE_ERASE)
|
||||
ena_storage_erase();
|
||||
ena_storage_erase_all();
|
||||
#endif
|
||||
|
||||
if (ena_storage_read_last_exposure_date() == 0xFFFFFFFF)
|
||||
{
|
||||
ena_storage_erase();
|
||||
ena_storage_erase_all();
|
||||
}
|
||||
|
||||
// init NVS for BLE
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include "esp_err.h"
|
||||
#include "ena-storage.h"
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_EXPOSURE_LOG "ESP-ENA-exposure" // TAG for Logging
|
||||
@ -151,8 +152,8 @@ typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t key_data[ENA_KEY_LENGTH];
|
||||
uint8_t transmission_risk_level;
|
||||
uint8_t rolling_start_interval_number;
|
||||
uint8_t rolling_period;
|
||||
uint32_t rolling_start_interval_number;
|
||||
uint32_t rolling_period;
|
||||
ena_report_type_t report_type;
|
||||
uint32_t days_since_onset_of_symptoms;
|
||||
} ena_temporary_exposure_key_t;
|
||||
@ -231,7 +232,15 @@ ena_exposure_summary_t *ena_exposure_current_summary(void);
|
||||
ena_exposure_config_t *ena_exposure_default_config(void);
|
||||
|
||||
/**
|
||||
* @brief reads Temporary Exposue Key and check for exposures
|
||||
* @brief reads Temporary Exposue Key check for exposures with certain beacon
|
||||
*
|
||||
* @param[in] ena_beacon_t the beacon to check against
|
||||
* @param[in] temporary_exposure_key the temporary exposure keys to check
|
||||
*/
|
||||
void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t temporary_exposure_key);
|
||||
|
||||
/**
|
||||
* @brief reads Temporary Exposue Key and check for exposures with all beacons
|
||||
*
|
||||
* @param[in] temporary_exposure_key the temporary exposure keys to check
|
||||
*/
|
||||
|
@ -81,6 +81,14 @@ void ena_storage_read(size_t address, void *data, size_t size);
|
||||
*/
|
||||
void ena_storage_write(size_t address, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief erase storage at given address
|
||||
*
|
||||
* @param[in] address the address to erase from
|
||||
* @param[in] size how many bytes to erase
|
||||
*/
|
||||
void ena_storage_erase(size_t address, size_t size);
|
||||
|
||||
/**
|
||||
* @brief deletes bytes at given address and shift other data back
|
||||
*
|
||||
@ -224,7 +232,7 @@ void ena_storage_remove_beacon(uint32_t index);
|
||||
* This function completely deletes all stored data and resets the counters
|
||||
* of TEKs, temporary beacon and beacon to zero.
|
||||
*/
|
||||
void ena_storage_erase(void);
|
||||
void ena_storage_erase_all(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored TEKs
|
||||
@ -254,6 +262,11 @@ void ena_storage_erase_temporary_beacon(void);
|
||||
*/
|
||||
void ena_storage_erase_beacon(void);
|
||||
|
||||
/**
|
||||
* @brief helper to dump a byte array as hash
|
||||
*/
|
||||
void ena_storage_dump_hash_array(uint8_t *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief dump all stored TEKs to serial output
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user