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:
@@ -0,0 +1,11 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"ena-cwa.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
esp_http_client
|
||||
miniz
|
||||
ena
|
||||
EMBED_FILES
|
||||
"certs/telekom.pem"
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIFwDCCBKigAwIBAgIIfjnHrR3Z8EMwDQYJKoZIhvcNAQELBQAwgYIxCzAJBgNV
|
||||
BAYTAkRFMSswKQYDVQQKDCJULVN5c3RlbXMgRW50ZXJwcmlzZSBTZXJ2aWNlcyBH
|
||||
bWJIMR8wHQYDVQQLDBZULVN5c3RlbXMgVHJ1c3QgQ2VudGVyMSUwIwYDVQQDDBxU
|
||||
LVRlbGVTZWMgR2xvYmFsUm9vdCBDbGFzcyAyMB4XDTE0MDIxMTE0MzkxMFoXDTI0
|
||||
MDIxMTIzNTk1OVowgd8xCzAJBgNVBAYTAkRFMSUwIwYDVQQKDBxULVN5c3RlbXMg
|
||||
SW50ZXJuYXRpb25hbCBHbWJIMR8wHQYDVQQLDBZULVN5c3RlbXMgVHJ1c3QgQ2Vu
|
||||
dGVyMRwwGgYDVQQIDBNOb3JkcmhlaW4gV2VzdGZhbGVuMQ4wDAYDVQQRDAU1NzI1
|
||||
MDEQMA4GA1UEBwwHTmV0cGhlbjEgMB4GA1UECQwXVW50ZXJlIEluZHVzdHJpZXN0
|
||||
ci4gMjAxJjAkBgNVBAMMHVRlbGVTZWMgU2VydmVyUGFzcyBDbGFzcyAyIENBMIIB
|
||||
IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3oxwJVY3bSb6ejJ42f9VEt1N
|
||||
vW2swwllcs5ifPsHAulpSoFc2Y9gMOKQqkuyjN1foCegDDeEr6FBLD5YuROldcX8
|
||||
2aDNBKDh9GpSJYZMLrYwlfR4EJUGwLidHDn93H95j1M67sNlCyCfcbso0zFBQzXK
|
||||
KO06sbC1QH9M1Xdrltz8bQS+LbGRTM5JcPYhhxXcnsFstQVaGmfqFQitPXhT3g9+
|
||||
8Fbob6taSylFVk1E89G2N0NrrtIVTnaD0PcWF8AdMyX34zIoQAXMezyGV2kqst/Q
|
||||
Ghvzd09jjMT6f8Q8pAlyGFTGuxsEjeU/rrS/yKU8bFEEvuR5WT/I4Kme+8OlzQID
|
||||
AQABo4IB2TCCAdUwEgYDVR0TAQH/BAgwBgEB/wIBADBDBgNVHSAEPDA6MDgGBFUd
|
||||
IAAwMDAuBggrBgEFBQcCARYiaHR0cDovL3BraS50ZWxlc2VjLmRlL2Nwcy9jcHMu
|
||||
aHRtbDAOBgNVHQ8BAf8EBAMCAQYwge8GA1UdHwSB5zCB5DA1oDOgMYYvaHR0cDov
|
||||
L3BraS50ZWxlc2VjLmRlL3JsL0dsb2JhbFJvb3RfQ2xhc3NfMi5jcmwwgaqggaeg
|
||||
gaSGgaFsZGFwOi8vcGtpLnRlbGVzZWMuZGUvQ049VC1UZWxlU2VjJTIwR2xvYmFs
|
||||
Um9vdCUyMENsYXNzJTIwMixPVT1ULVN5c3RlbXMlMjBUcnVzdCUyMENlbnRlcixP
|
||||
PVQtU3lzdGVtcyUyMEVudGVycHJpc2UlMjBTZXJ2aWNlcyUyMEdtYkgsQz1ERT9B
|
||||
dXRob3JpdHlSZXZvY2F0aW9uTGlzdDA4BggrBgEFBQcBAQQsMCowKAYIKwYBBQUH
|
||||
MAGGHGh0dHA6Ly9vY3NwLnRlbGVzZWMuZGUvb2NzcHIwHQYDVR0OBBYEFJTIdEb1
|
||||
OrRGSCb4K8o0HlYmBBIAMB8GA1UdIwQYMBaAFL9ZIDYAeaCgImuM1fJh0rgsy4JK
|
||||
MA0GCSqGSIb3DQEBCwUAA4IBAQB55S9CfCkclWVtUIxl2c4aM5wqlLZRZ7zVhynK
|
||||
KOhWKyTw+D2BOjc+TXIPkgRMqF3Sn8ZD4UTOARboJxswYnLZDkvBuvTbYa+N52Jy
|
||||
oBP2TXIpEWEyJl7Oq8NFbERwg4X6MabLgjGvJETicPpKGfAINKDwPScQCsWHiCaX
|
||||
X50cZzmWw17S0rWECOvPEt/4tXJ4Me9aAxx6WRm708n/K8O4mB3AzvA/M7VUDaP9
|
||||
8LtreoTnWInjyg/8+Ahtce3foMXiIP4+9IX7fbm6yqh4u33tqMESDcRP6eGdzq4D
|
||||
qnHyIvj9XNpuGgMvDgq357kZQS9e5XVH5icSvW1kr2kX2t1f
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "miniz.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "ena-cwa.h"
|
||||
|
||||
extern const uint8_t telekom_pem_start[] asm("_binary_telekom_pem_start");
|
||||
extern const uint8_t telekom__pem_end[] asm("_binary_telekom_pem_end");
|
||||
|
||||
esp_err_t ena_cwa_http_event_handler(esp_http_client_event_t *evt)
|
||||
{
|
||||
static char *output_buffer;
|
||||
static int output_len;
|
||||
switch (evt->event_id)
|
||||
{
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
if (!esp_http_client_is_chunked_response(evt->client))
|
||||
{
|
||||
if (output_buffer == NULL)
|
||||
{
|
||||
output_buffer = (char *)malloc(esp_http_client_get_content_length(evt->client));
|
||||
output_len = 0;
|
||||
if (output_buffer == NULL)
|
||||
{
|
||||
ESP_LOGE(ENA_CWA_LOG, "Failed to allocate memory for output buffer");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
memcpy(output_buffer + output_len, evt->data, evt->data_len);
|
||||
output_len += evt->data_len;
|
||||
}
|
||||
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
if (output_buffer != NULL)
|
||||
{
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
|
||||
size_t zip_image_size = esp_http_client_get_content_length(evt->client);
|
||||
// const char *file_name = "export.sig";
|
||||
|
||||
/*
|
||||
mz_zip_archive zip_archive;
|
||||
mz_zip_archive_file_stat file_stat;
|
||||
mz_uint32 file_index;
|
||||
|
||||
memset(&zip_archive, 0, sizeof(zip_archive));
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
mz_zip_reader_init_mem(&zip_archive, output_buffer, sizeof(output_buffer), 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_locate_file_v2(&zip_archive, file_name, NULL, 0, &file_index);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_file_stat(&zip_archive, file_index, &file_stat);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
size_t extracted_size = file_stat.m_uncomp_size;
|
||||
|
||||
ESP_LOGD(ENA_CWA_LOG, "size of export.sig: %d", extracted_size);
|
||||
|
||||
char *file_buffer = malloc(extracted_size);
|
||||
|
||||
mz_zip_reader_extract_to_mem(&zip_archive, file_index, file_buffer, extracted_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_CWA_LOG, file_buffer, extracted_size, ESP_LOG_DEBUG);
|
||||
|
||||
free(file_buffer);
|
||||
*/
|
||||
|
||||
mz_zip_archive zip_archive;
|
||||
ESP_LOGD(ENA_CWA_LOG, "1 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
memset(&zip_archive, 0, sizeof(zip_archive));
|
||||
ESP_LOGD(ENA_CWA_LOG, "2 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
mz_zip_reader_init_mem(&zip_archive, output_buffer, zip_image_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "3 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
|
||||
ESP_LOGD(ENA_CWA_LOG, "4 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
/*
|
||||
p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_name, &extracted_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "4 memory: %d kB", (esp_get_free_heap_size() / 1024));
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_CWA_LOG, p, extracted_size, ESP_LOG_DEBUG);
|
||||
free(p);
|
||||
*/
|
||||
|
||||
free(output_buffer);
|
||||
output_buffer = NULL;
|
||||
output_len = 0;
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory freed: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void ena_cwa_receive_keys(char *date_string)
|
||||
{
|
||||
char *url = malloc(strlen(ENA_CWA_KEYFILES_URL) + strlen(date_string));
|
||||
sprintf(url, ENA_CWA_KEYFILES_URL, date_string);
|
||||
esp_http_client_config_t config = {
|
||||
.url = url,
|
||||
.cert_pem = (char *)telekom_pem_start,
|
||||
.event_handler = ena_cwa_http_event_handler,
|
||||
};
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
int content_length = esp_http_client_get_content_length(client);
|
||||
ESP_LOGD(ENA_CWA_LOG, "Url = %s, Status = %d, content_length = %d", url,
|
||||
esp_http_client_get_status_code(client),
|
||||
content_length);
|
||||
}
|
||||
// free(url);
|
||||
esp_http_client_close(client);
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_CWA_H_
|
||||
#define _ena_CWA_H_
|
||||
|
||||
#define ENA_CWA_LOG "ESP-ENA-corona-warn-app" // TAG for Logging
|
||||
|
||||
#define ENA_CWA_KEYFILES_URL "http://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE/date/%s"
|
||||
|
||||
/**
|
||||
* @brief fetch key export for given date
|
||||
*
|
||||
* @param[in] date_string the date to fetch the data for
|
||||
*/
|
||||
void ena_cwa_receive_keys(char *date_string);
|
||||
|
||||
#endif
|
||||
@@ -33,6 +33,9 @@ void ena_interface_menu_ok(void)
|
||||
{
|
||||
ena_interface_status_start();
|
||||
}
|
||||
else if (interface_menu_state == ENA_INTERFACE_MENU_STATE_SELECT_DEBUG)
|
||||
{
|
||||
}
|
||||
else if (interface_menu_state == ENA_INTERFACE_MENU_STATE_IDLE)
|
||||
{
|
||||
if (ena_interface_get_state() == ENA_INTERFACE_STATE_MENU)
|
||||
|
||||
@@ -18,6 +18,7 @@ typedef enum
|
||||
{
|
||||
ENA_INTERFACE_MENU_STATE_IDLE = 0,
|
||||
ENA_INTERFACE_MENU_STATE_SELECT_TIME,
|
||||
ENA_INTERFACE_MENU_STATE_SELECT_DEBUG,
|
||||
ENA_INTERFACE_MENU_STATE_SELECT_STATUS,
|
||||
} ena_interface_menu_state;
|
||||
|
||||
|
||||
@@ -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.
@@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"miniz.c"
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
Copyright 2013-2014 RAD Game Tools and Valve Software
|
||||
Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
|
||||
|
||||
All Rights Reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2011 Petteri Aimonen <jpa at nanopb.mail.kapsi.fi>
|
||||
|
||||
This software is provided 'as-is', without any express or
|
||||
implied warranty. In no event will the authors be held liable
|
||||
for any damages arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any
|
||||
purpose, including commercial applications, and to alter it and
|
||||
redistribute it freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you
|
||||
must not claim that you wrote the original software. If you use
|
||||
this software in a product, an acknowledgment in the product
|
||||
documentation would be appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and
|
||||
must not be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
@@ -91,7 +91,6 @@ void ssd1306_clear_line(uint8_t i2address, uint8_t line, bool invert);
|
||||
*/
|
||||
void ssd1306_clear(uint8_t i2address);
|
||||
|
||||
|
||||
/**
|
||||
* @brief set display on or offf
|
||||
*
|
||||
@@ -100,6 +99,17 @@ void ssd1306_clear(uint8_t i2address);
|
||||
*/
|
||||
void ssd1306_on(uint8_t i2address, bool on);
|
||||
|
||||
/**
|
||||
* @brief write raw bytes to display line at starting column
|
||||
*
|
||||
* @param[in] i2address I2C address of SSD1306
|
||||
* @param[in] data bytes to display
|
||||
* @param[in] length length of data
|
||||
* @param[in] line the line to write to
|
||||
* @param[in] offset number of offset chars to start
|
||||
*/
|
||||
void ssd1306_data(uint8_t i2address, uint8_t *data, size_t length, uint8_t line, uint8_t offset, bool invert);
|
||||
|
||||
/**
|
||||
* @brief write text to display line at starting column
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user