mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
support for ena-eke-proxy, improved interface
This commit is contained in:
@@ -7,6 +7,12 @@ menu "Exposure Notification API"
|
||||
help
|
||||
Dump storage (stored TEKs, temp. beacons and perm. beacons) to serial output after scan.
|
||||
|
||||
config ENA_RESET_LAST_CHECK
|
||||
bool "Reset last exposure check"
|
||||
default false
|
||||
help
|
||||
Resets the last exposure check date on start.
|
||||
|
||||
config ENA_STORAGE_TEK_MAX
|
||||
int "Max. TEKs"
|
||||
default 14
|
||||
@@ -91,4 +97,5 @@ menu "Exposure Notification API"
|
||||
Defines the TEK rolling period in 10 minute steps. (Default 144 => 24 hours)
|
||||
endmenu
|
||||
|
||||
|
||||
endmenu
|
||||
@@ -243,6 +243,7 @@ void ena_exposure_summary(ena_exposure_config_t *config)
|
||||
}
|
||||
params.duration = exposure_info.duration_minutes;
|
||||
params.attenuation = exposure_info.typical_attenuation;
|
||||
params.report_type = exposure_info.report_type;
|
||||
int score = ena_exposure_risk_score(config, params);
|
||||
if (score > current_summary->max_risk_score)
|
||||
{
|
||||
@@ -270,20 +271,13 @@ ena_exposure_config_t *ena_exposure_default_config(void)
|
||||
void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t temporary_exposure_key)
|
||||
{
|
||||
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;
|
||||
uint32_t timestamp_day_end = (temporary_exposure_key.rolling_start_interval_number + temporary_exposure_key.rolling_period) * ENA_TIME_WINDOW;
|
||||
|
||||
if (beacon.timestamp_first > timestamp_day_start && beacon.timestamp_last < timestamp_day_end)
|
||||
{
|
||||
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.day = timestamp_day_start;
|
||||
exposure_info.duration_minutes = 0;
|
||||
exposure_info.min_attenuation = INT_MAX;
|
||||
exposure_info.typical_attenuation = 0;
|
||||
@@ -298,8 +292,7 @@ void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t tempor
|
||||
if (memcmp(beacon.rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0)
|
||||
{
|
||||
match = true;
|
||||
exposure_info.day = timestamp_day_start;
|
||||
exposure_info.duration_minutes += (ENA_BEACON_TRESHOLD / 60);
|
||||
exposure_info.duration_minutes += ((beacon.timestamp_last - beacon.timestamp_first) / 60);
|
||||
exposure_info.typical_attenuation = (exposure_info.typical_attenuation + beacon.rssi) / 2;
|
||||
if (beacon.rssi < exposure_info.min_attenuation)
|
||||
{
|
||||
@@ -315,13 +308,92 @@ void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t tempor
|
||||
}
|
||||
}
|
||||
|
||||
int ena_expore_check_find_min_rec(int min, int max, uint32_t timestamp)
|
||||
{
|
||||
|
||||
if (min < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (min >= max - 1)
|
||||
{
|
||||
return min;
|
||||
}
|
||||
|
||||
ena_beacon_t beacon;
|
||||
int mid = min + (max - min) / 2;
|
||||
ena_storage_get_beacon(min, &beacon);
|
||||
|
||||
if (beacon.timestamp_first < timestamp)
|
||||
{
|
||||
return ena_expore_check_find_min_rec(mid, max, timestamp);
|
||||
}
|
||||
else if (beacon.timestamp_first > timestamp)
|
||||
{
|
||||
return ena_expore_check_find_min_rec(min - ((max - min) / 2), mid, timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
return min;
|
||||
}
|
||||
}
|
||||
|
||||
int ena_expore_check_find_min(uint32_t timestamp)
|
||||
{
|
||||
return ena_expore_check_find_min_rec(0, (ena_storage_beacons_count() - 1), timestamp);
|
||||
}
|
||||
|
||||
int ena_expore_check_find_max_rec(int min, int max, int max_max, uint32_t timestamp)
|
||||
{
|
||||
if (max > max_max)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (max <= (min + 1))
|
||||
{
|
||||
return max;
|
||||
}
|
||||
|
||||
ena_beacon_t beacon;
|
||||
int mid = min + (max - min) / 2;
|
||||
ena_storage_get_beacon(max, &beacon);
|
||||
if (beacon.timestamp_first > timestamp)
|
||||
{
|
||||
return ena_expore_check_find_max_rec(min, mid, max_max, timestamp);
|
||||
}
|
||||
else if (beacon.timestamp_first < timestamp)
|
||||
{
|
||||
return ena_expore_check_find_max_rec(mid, max + ((max - min) / 2), max_max, timestamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
int ena_expore_check_find_max(uint32_t timestamp)
|
||||
{
|
||||
return ena_expore_check_find_max_rec(0, (ena_storage_beacons_count() - 1), (ena_storage_beacons_count() - 1), timestamp);
|
||||
}
|
||||
|
||||
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key)
|
||||
{
|
||||
uint32_t timestamp_start = temporary_exposure_key.rolling_start_interval_number * ENA_TIME_WINDOW;
|
||||
uint32_t timestamp_end = (temporary_exposure_key.rolling_start_interval_number + temporary_exposure_key.rolling_period) * ENA_TIME_WINDOW;
|
||||
|
||||
int min = ena_expore_check_find_min(timestamp_start);
|
||||
int max = ena_expore_check_find_max(timestamp_end);
|
||||
if (min == -1 || max == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ena_beacon_t beacon;
|
||||
uint32_t beacons_count = ena_storage_beacons_count();
|
||||
for (int y = 0; y < beacons_count; y++)
|
||||
for (int y = min; y <= max; y++)
|
||||
{
|
||||
ena_storage_get_beacon(y, &beacon);
|
||||
ena_exposure_check(beacon, temporary_exposure_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,10 +179,17 @@ void ena_storage_write_last_exposure_date(uint32_t timestamp)
|
||||
ena_storage_write(ENA_STORAGE_LAST_EXPOSURE_DATE_ADDRESS, ×tamp, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
uint32_t ena_storage_tek_count(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read TEK count: %u", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
uint32_t ena_storage_read_last_tek(ena_tek_t *tek)
|
||||
{
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint32_t tek_count = ena_storage_tek_count();
|
||||
if (tek_count < 1)
|
||||
{
|
||||
return 0;
|
||||
@@ -195,6 +202,13 @@ uint32_t ena_storage_read_last_tek(ena_tek_t *tek)
|
||||
return tek_count;
|
||||
}
|
||||
|
||||
void ena_storage_get_tek(uint32_t index, ena_tek_t *tek)
|
||||
{
|
||||
ena_storage_read(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read %d tek %u:", index, tek->enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_storage_write_tek(ena_tek_t *tek)
|
||||
{
|
||||
uint32_t tek_count = 0;
|
||||
|
||||
@@ -91,6 +91,10 @@ void ena_start(void)
|
||||
ena_storage_erase_all();
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ENA_RESET_LAST_CHECK
|
||||
ena_storage_write_last_exposure_date(0);
|
||||
#endif
|
||||
|
||||
if (ena_storage_read_last_exposure_date() == 0xFFFFFFFF)
|
||||
{
|
||||
ena_storage_erase_all();
|
||||
|
||||
@@ -239,6 +239,20 @@ ena_exposure_config_t *ena_exposure_default_config(void);
|
||||
*/
|
||||
void ena_exposure_check(ena_beacon_t beacon, ena_temporary_exposure_key_t temporary_exposure_key);
|
||||
|
||||
/**
|
||||
* @brief find minimal key index of beacons for a certain timestamp
|
||||
*
|
||||
* @param[in] timestamp the timestamp to check against
|
||||
*/
|
||||
int ena_expore_check_find_min(uint32_t timestamp);
|
||||
|
||||
/**
|
||||
* @brief find maximum key index of beacons for a certain timestamp
|
||||
*
|
||||
* @param[in] timestamp the timestamp to check against
|
||||
*/
|
||||
int ena_expore_check_find_max(uint32_t timestamp);
|
||||
|
||||
/**
|
||||
* @brief reads Temporary Exposue Key and check for exposures with all beacons
|
||||
*
|
||||
|
||||
@@ -59,7 +59,7 @@ typedef struct __attribute__((__packed__))
|
||||
uint32_t day; // Day of the exposure, using UTC, encapsulated as the time of the beginning of that day.
|
||||
int typical_attenuation; // Aggregation of the attenuations of all of a given diagnosis key's beacons received during the scan, in dB.
|
||||
int min_attenuation; // Minimum attenuation of all of a given diagnosis key's beacons received during the scan, in dB.
|
||||
int duration_minutes; //The duration of the exposure in minutes.
|
||||
int duration_minutes; // The duration of the exposure in minutes.
|
||||
int report_type; // Type of diagnosis associated with a key.
|
||||
} ena_exposure_information_t;
|
||||
|
||||
@@ -113,6 +113,14 @@ uint32_t ena_storage_read_last_exposure_date(void);
|
||||
*/
|
||||
void ena_storage_write_last_exposure_date(uint32_t timestamp);
|
||||
|
||||
/**
|
||||
* @brief get number of stored TEKs
|
||||
*
|
||||
* @return
|
||||
* total number of TEKs stored
|
||||
*/
|
||||
uint32_t ena_storage_tek_count(void);
|
||||
|
||||
/**
|
||||
* @brief get last stored TEK
|
||||
*
|
||||
@@ -123,6 +131,15 @@ void ena_storage_write_last_exposure_date(uint32_t timestamp);
|
||||
*/
|
||||
uint32_t ena_storage_read_last_tek(ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief get stored TEK at given index
|
||||
*
|
||||
* @param[in] index the index of the TEK to read
|
||||
* @param[out] tek pointer to write TEK to
|
||||
*
|
||||
*/
|
||||
void ena_storage_get_tek(uint32_t index, ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief store given TEK
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user