componeent structure, preparation for component branch

This commit is contained in:
Lurkars
2020-09-29 19:58:01 +02:00
parent b473e88be1
commit 43ecb0a42e
45 changed files with 998 additions and 9747 deletions
+1 -5
View File
@@ -11,8 +11,4 @@ idf_component_register(
PRIV_REQUIRES
spi_flash
mbedtls
bt
nanopb
EMBED_FILES
"test/export.bin"
)
bt)
+6 -73
View File
@@ -22,9 +22,6 @@
#include "ena-storage.h"
#include "ena-beacons.h"
#include "pb_decode.h"
#include "TemporaryExposureKeyExport.pb.h"
#include "ena-exposure.h"
static ena_exposure_summary_t *current_summary;
@@ -76,9 +73,6 @@ static ena_exposure_config_t DEFAULT_ENA_EXPOSURE_CONFIG = {
},
};
static const char kFileHeader[] = "EK Export v1 ";
static size_t kFileHeaderSize = sizeof(kFileHeader) - 1;
int ena_exposure_transmission_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
{
return config->transmission_risk_values[params.report_type];
@@ -273,62 +267,29 @@ ena_exposure_config_t *ena_exposure_default_config(void)
return &DEFAULT_ENA_EXPOSURE_CONFIG;
}
bool ena_exposure_decode_key_data(pb_istream_t *stream, const pb_field_t *field, void **arg)
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key)
{
uint8_t *key_data = (uint8_t *)*arg;
if (!pb_read(stream, key_data, stream->bytes_left))
{
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(stream));
return false;
}
return true;
}
bool ena_exposure_decode_key(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
uint8_t key_data[ENA_KEY_LENGTH] = {0};
TemporaryExposureKey tek = TemporaryExposureKey_init_zero;
tek.key_data = (pb_callback_t){
.funcs.decode = ena_exposure_decode_key_data,
.arg = &key_data,
};
if (!pb_decode(stream, TemporaryExposureKey_fields, &tek))
{
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(stream));
return false;
}
ESP_LOGD(ENA_EXPOSURE_LOG,
"check reported tek: rolling_start_interval_number %d, rolling_period %d, days_since_last_exposure %d, report_type %d, transmission_risk_values %d",
tek.rolling_start_interval_number, tek.rolling_period, tek.days_since_onset_of_symptoms, tek.report_type, tek.transmission_risk_level);
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, &key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
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 = tek.report_type;
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, key_data);
ena_crypto_rpik(rpik, temporary_exposure_key.key_data);
uint32_t beacons_count = ena_storage_beacons_count();
for (int i = 0; i < tek.rolling_period; i++)
for (int i = 0; i < temporary_exposure_key.rolling_period; i++)
{
ena_crypto_rpi(rpi, rpik, tek.rolling_start_interval_number + i);
ena_crypto_rpi(rpi, rpik, temporary_exposure_key.rolling_start_interval_number + i);
for (int y = 0; y < beacons_count; y++)
{
ena_storage_get_beacon(y, &beacon);
if (memcmp(beacon.rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0)
{
match = true;
exposure_info.day = tek.rolling_start_interval_number * ENA_TIME_WINDOW;
exposure_info.day = temporary_exposure_key.rolling_start_interval_number * ENA_TIME_WINDOW;
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)
@@ -343,32 +304,4 @@ bool ena_exposure_decode_key(pb_istream_t *stream, const pb_field_t *field, void
{
ena_storage_add_exposure_information(&exposure_info);
}
return true;
}
esp_err_t ena_exposure_check_export(uint8_t *buf, size_t size)
{
// validate header
if (memcmp(kFileHeader, buf, kFileHeaderSize) != 0)
{
ESP_LOGW(ENA_EXPOSURE_LOG, "Wrong or missing header!");
return ESP_FAIL;
}
TemporaryExposureKeyExport tek_export = TemporaryExposureKeyExport_init_zero;
tek_export.keys = (pb_callback_t){
.funcs.decode = ena_exposure_decode_key,
};
pb_istream_t stream = pb_istream_from_buffer(&buf[kFileHeaderSize], (size - kFileHeaderSize));
if (!pb_decode(&stream, TemporaryExposureKeyExport_fields, &tek_export))
{
ESP_LOGW(ENA_EXPOSURE_LOG, "Decoding failed: %s\n", PB_GET_ERROR(&stream));
return ESP_FAIL;
}
return ESP_OK;
}
+19 -8
View File
@@ -14,7 +14,7 @@
/**
* @file
*
* @brief decode Exposure Key export, compare with stored beacons, calculate score and risk
* @brief compare temporary exposure keys with stored beacons, calculate score and risk
*
*/
#ifndef _ena_EXPOSURE_H_
@@ -142,6 +142,21 @@ typedef struct __attribute__((__packed__))
int risk_score_sum; // sum of all risk_scores
} ena_exposure_summary_t;
/**
* @brief structure for temporary exposure key
*
* The temporary exposure key is used to check for exposure.
*/
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;
ena_report_type_t report_type;
uint32_t days_since_onset_of_symptoms;
} ena_temporary_exposure_key_t;
/**
* @brief calculate transmission risk score
*
@@ -216,14 +231,10 @@ ena_exposure_summary_t *ena_exposure_current_summary(void);
ena_exposure_config_t *ena_exposure_default_config(void);
/**
* @brief reads a Temporary Exposue Key Export binary and check for exposures
* @brief reads Temporary Exposue Key and check for exposures
*
* @param[in] buf the buffer containing the binary data
* @param[in] size the size of the buffer
*
* @return
* esp_err_t status of reading binary
* @param[in] temporary_exposure_key the temporary exposure keys to check
*/
esp_err_t ena_exposure_check_export(uint8_t *buf, size_t size);
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key);
#endif
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.