mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
read and parse Key Export, started WiFi and CWA connection
This commit is contained in:
@@ -15,5 +15,4 @@ idf_component_register(
|
||||
nanopb
|
||||
EMBED_FILES
|
||||
"test/export.bin"
|
||||
"test/export.sig"
|
||||
)
|
||||
@@ -87,12 +87,4 @@ menu "Exposure Notification API"
|
||||
Defines the TEK rolling period in 10 minute steps. (Default 144 => 24 hours)
|
||||
endmenu
|
||||
|
||||
menu "Miscellaneous"
|
||||
config ENA_RAM
|
||||
int "ENA RAM"
|
||||
default 100000
|
||||
help
|
||||
RAM required for main task. (Default 100 KB)
|
||||
endmenu
|
||||
|
||||
endmenu
|
||||
+109
-48
@@ -15,12 +15,16 @@
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
#include "ena-beacons.h"
|
||||
|
||||
#include "pb_decode.h"
|
||||
#include "TemporaryExposureKeyExport.pb.h"
|
||||
|
||||
#include "ena-exposure.h"
|
||||
|
||||
static ena_exposure_config_t DEFAULT_ENA_EXPOSURE_CONFIG = {
|
||||
@@ -73,52 +77,8 @@ static ena_exposure_config_t DEFAULT_ENA_EXPOSURE_CONFIG = {
|
||||
static const char kFileHeader[] = "EK Export v1 ";
|
||||
static size_t kFileHeaderSize = sizeof(kFileHeader) - 1;
|
||||
|
||||
extern const uint8_t export_bin_start[] asm("_binary_export_bin_start");
|
||||
extern const uint8_t export_bin_end[] asm("_binary_export_bin_end");
|
||||
|
||||
void ena_exposure_keyfiletest(void)
|
||||
{
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_EXPOSURE_LOG, export_bin_start, (export_bin_end - export_bin_start), ESP_LOG_INFO);
|
||||
}
|
||||
|
||||
void ena_exposure_check(ena_tek_reported_t tek_reported)
|
||||
{
|
||||
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_reported.report_type;
|
||||
uint8_t rpi[ENA_KEY_LENGTH];
|
||||
uint8_t rpik[ENA_KEY_LENGTH];
|
||||
ena_crypto_rpik(rpik, tek_reported.key_data);
|
||||
uint32_t beacons_count = ena_storage_beacons_count();
|
||||
for (int i = 0; i < tek_reported.rolling_period; i++)
|
||||
{
|
||||
ena_crypto_rpi(rpi, rpik, tek_reported.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_reported.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)
|
||||
{
|
||||
exposure_info.min_attenuation = beacon.rssi;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
ena_storage_add_exposure_information(&exposure_info);
|
||||
}
|
||||
}
|
||||
extern uint8_t export_bin_start[] asm("_binary_export_bin_start"); // test data from Google or https://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE/date/2020-07-22
|
||||
extern uint8_t export_bin_end[] asm("_binary_export_bin_end");
|
||||
|
||||
int ena_exposure_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
@@ -238,6 +198,9 @@ int ena_exposure_risk_score(ena_exposure_config_t *config, ena_exposure_paramete
|
||||
|
||||
void ena_exposure_summary(ena_exposure_config_t *config, ena_exposure_summary_t *summary)
|
||||
{
|
||||
// XXX TEST key export (should be called on other location though)
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(ena_exposure_check_export(export_bin_start, (export_bin_end - export_bin_start)));
|
||||
|
||||
uint32_t count = ena_storage_exposure_information_count();
|
||||
uint32_t current_time = (uint32_t)time(NULL);
|
||||
|
||||
@@ -270,11 +233,109 @@ void ena_exposure_summary(ena_exposure_config_t *config, ena_exposure_summary_t
|
||||
}
|
||||
summary->risk_score_sum += score;
|
||||
}
|
||||
|
||||
ena_exposure_keyfiletest();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
uint8_t rpi[ENA_KEY_LENGTH];
|
||||
uint8_t rpik[ENA_KEY_LENGTH];
|
||||
ena_crypto_rpik(rpik, key_data);
|
||||
uint32_t beacons_count = ena_storage_beacons_count();
|
||||
for (int i = 0; i < tek.rolling_period; i++)
|
||||
{
|
||||
ena_crypto_rpi(rpi, rpik, tek.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.duration_minutes += (ENA_BEACON_TRESHOLD / 60);
|
||||
exposure_info.typical_attenuation = (exposure_info.typical_attenuation + beacon.rssi) / 2;
|
||||
if (beacon.rssi < exposure_info.min_attenuation)
|
||||
{
|
||||
exposure_info.min_attenuation = beacon.rssi;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
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;
|
||||
}
|
||||
+36
-36
@@ -14,10 +14,6 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
@@ -47,48 +43,42 @@ void ena_next_rpi_timestamp(uint32_t timestamp)
|
||||
ESP_LOGD(ENA_LOG, "next rpi at %u (%u from %u)", next_rpi_timestamp, (ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval), timestamp);
|
||||
}
|
||||
|
||||
void ena_run(void *pvParameter)
|
||||
void ena_run(void)
|
||||
{
|
||||
static uint32_t unix_timestamp = 0;
|
||||
static uint32_t current_enin = 0;
|
||||
while (1)
|
||||
unix_timestamp = (uint32_t)time(NULL);
|
||||
current_enin = ena_crypto_enin(unix_timestamp);
|
||||
if (current_enin - last_tek.enin >= last_tek.rolling_period)
|
||||
{
|
||||
unix_timestamp = (uint32_t)time(NULL);
|
||||
current_enin = ena_crypto_enin(unix_timestamp);
|
||||
if (current_enin - last_tek.enin >= last_tek.rolling_period)
|
||||
{
|
||||
ena_crypto_tek(last_tek.key_data);
|
||||
last_tek.enin = current_enin;
|
||||
// validity only to next day 00:00
|
||||
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
|
||||
ena_storage_write_tek(&last_tek);
|
||||
}
|
||||
ena_crypto_tek(last_tek.key_data);
|
||||
last_tek.enin = current_enin;
|
||||
// validity only to next day 00:00
|
||||
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
|
||||
ena_storage_write_tek(&last_tek);
|
||||
}
|
||||
|
||||
// change RPI
|
||||
if (unix_timestamp >= next_rpi_timestamp)
|
||||
// change RPI
|
||||
if (unix_timestamp >= next_rpi_timestamp)
|
||||
{
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_SCANNING)
|
||||
{
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_SCANNING)
|
||||
{
|
||||
ena_bluetooth_scan_stop();
|
||||
}
|
||||
ena_bluetooth_advertise_stop();
|
||||
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
|
||||
ena_bluetooth_advertise_start();
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_WAITING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
ena_next_rpi_timestamp(unix_timestamp);
|
||||
ena_bluetooth_scan_stop();
|
||||
}
|
||||
|
||||
// scan
|
||||
if (unix_timestamp % ENA_SCANNING_INTERVAL == 0 && ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_NOT_SCANNING)
|
||||
ena_bluetooth_advertise_stop();
|
||||
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
|
||||
ena_bluetooth_advertise_start();
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_WAITING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
ena_next_rpi_timestamp(unix_timestamp);
|
||||
}
|
||||
|
||||
// one second loop correct?!
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
// scan
|
||||
if (unix_timestamp % ENA_SCANNING_INTERVAL == 0 && ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_NOT_SCANNING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,5 +163,15 @@ void ena_start(void)
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
|
||||
// what is a good stack size here?
|
||||
xTaskCreate(&ena_run, "ena_run", ENA_RAM, NULL, 5, NULL);
|
||||
// xTaskCreate(&ena_run, "ena_run", ENA_RAM, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
void ena_stop(void)
|
||||
{
|
||||
ena_bluetooth_advertise_stop();
|
||||
ena_bluetooth_scan_stop();
|
||||
esp_bluedroid_disable();
|
||||
esp_bluedroid_deinit();
|
||||
esp_bt_controller_disable();
|
||||
esp_bt_controller_deinit();
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
#define _ena_EXPOSURE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_EXPOSURE_LOG "ESP-ENA-exposure" // TAG for Logging
|
||||
@@ -134,25 +135,6 @@ typedef struct __attribute__((__packed__))
|
||||
int risk_score_sum; // sum of all risk_scores
|
||||
} ena_exposure_summary_t;
|
||||
|
||||
/**
|
||||
* @brief structure for a reported TEK
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t key_data[ENA_KEY_LENGTH]; // Key of infected user
|
||||
uint32_t rolling_start_interval_number; // The interval number since epoch for which a key starts
|
||||
uint8_t rolling_period; // Increments of 10 minutes describing how long a key is valid
|
||||
ena_report_type_t report_type; // Type of diagnosis associated with a key.
|
||||
uint32_t days_since_onset_of_symptoms; // Number of days elapsed between symptom onset and the TEK being used. E.g. 2 means TEK is 2 days after onset of symptoms.
|
||||
} ena_tek_reported_t;
|
||||
|
||||
/**
|
||||
* @brief check for exposure for a reported tek and store exposure information on finding
|
||||
*
|
||||
* @param[in] tek_reported the reported tek to check
|
||||
*/
|
||||
void ena_exposure_check(ena_tek_reported_t tek_reported);
|
||||
|
||||
/**
|
||||
* @brief calculate risk score
|
||||
*
|
||||
@@ -174,4 +156,15 @@ void ena_exposure_summary(ena_exposure_config_t *config, ena_exposure_summary_t
|
||||
*/
|
||||
ena_exposure_config_t *ena_exposure_default_config(void);
|
||||
|
||||
/**
|
||||
* @brief reads a Temporary Exposue Key Export binary 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
|
||||
*/
|
||||
esp_err_t ena_exposure_check_export(uint8_t *buf, size_t size);
|
||||
|
||||
#endif
|
||||
@@ -15,10 +15,17 @@
|
||||
#define _ena_H_
|
||||
|
||||
#define ENA_LOG "ESP-ENA" // TAG for Logging
|
||||
#define ENA_RAM (CONFIG_ENA_RAM) // change advertising payload and therefore the BT address
|
||||
#define ENA_BT_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_ROTATION_TIMEOUT_INTERVAL) // change advertising payload and therefore the BT address
|
||||
#define ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL) // random intervall change for BT address change
|
||||
|
||||
/**
|
||||
* @brief Run Exposure Notification API
|
||||
*
|
||||
* This runs the complete BLE logic
|
||||
*
|
||||
*/
|
||||
void ena_run(void);
|
||||
|
||||
/**
|
||||
* @brief Start Exposure Notification API
|
||||
*
|
||||
@@ -28,4 +35,9 @@
|
||||
*/
|
||||
void ena_start(void);
|
||||
|
||||
/**
|
||||
* @brief stop ena
|
||||
*/
|
||||
void ena_stop(void);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user