mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
move esp-ena components to branch with FetchContent
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"ena.c"
|
||||
"ena-beacons.c"
|
||||
"ena-bluetooth-advertise.c"
|
||||
"ena-bluetooth-scan.c"
|
||||
"ena-crypto.c"
|
||||
"ena-exposure.c"
|
||||
"ena-storage.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
spi_flash
|
||||
mbedtls
|
||||
bt)
|
||||
@@ -1,94 +0,0 @@
|
||||
menu "Exposure Notification API"
|
||||
|
||||
menu "Storage"
|
||||
config ENA_STORAGE_DUMP
|
||||
bool "Dump storage"
|
||||
default false
|
||||
help
|
||||
Dump storage (stored TEKs, temp. beacons and perm. beacons) to serial output after scan.
|
||||
|
||||
config ENA_STORAGE_TEK_MAX
|
||||
int "Max. TEKs"
|
||||
default 14
|
||||
help
|
||||
Defines the maximum number of TEKs to be stored. (Default 14 [14 * 144 => 14 days])
|
||||
|
||||
config ENA_STORAGE_EXPOSURE_INFORMATION_MAX
|
||||
int "Max. exporure information"
|
||||
default 500
|
||||
help
|
||||
Defines the maximum number of exposure information to be stored. (Default 500)
|
||||
|
||||
config ENA_STORAGE_TEMP_BEACONS_MAX
|
||||
int "Max. temporary beacons"
|
||||
default 1000
|
||||
help
|
||||
Defines the maximum number of temporary beacons to be stored. (Default 1000)
|
||||
|
||||
config ENA_STORAGE_START_ADDRESS
|
||||
int "Storage start address"
|
||||
default 0
|
||||
help
|
||||
Defines the start address on partition. (Default 0)
|
||||
|
||||
config ENA_STORAGE_PARTITION_NAME
|
||||
string "Partition name"
|
||||
default "ena"
|
||||
help
|
||||
Name of the partition used for storage. (Default "ena", see partitions.csv)
|
||||
|
||||
config ENA_STORAGE_ERASE
|
||||
bool "Erase storage (!)"
|
||||
default false
|
||||
help
|
||||
Erases the complete(!) partition on startup and reset counters.
|
||||
|
||||
endmenu
|
||||
|
||||
menu "Scanning"
|
||||
config ENA_BEACON_TRESHOLD
|
||||
int "Contact threshold"
|
||||
default 300
|
||||
help
|
||||
Threshold in seconds after a received beacon is stored permanently. (Default 5 minutes)
|
||||
|
||||
config ENA_BEACON_CLEANUP_TRESHOLD
|
||||
int "Clean-Up threshold"
|
||||
default 14
|
||||
help
|
||||
Threshold in days after stored beacons to be removed.
|
||||
|
||||
config ENA_SCANNING_TIME
|
||||
int "Scanning time"
|
||||
default 30
|
||||
help
|
||||
Time in seconds how long a scan should run. (Default 30 seconds)
|
||||
|
||||
config ENA_SCANNING_INTERVAL
|
||||
int "Scanning interval"
|
||||
default 300
|
||||
help
|
||||
Interval in seconds for the next scan to happen. (Default 5 minutes)
|
||||
endmenu
|
||||
|
||||
menu "Advertising"
|
||||
config ENA_BT_ROTATION_TIMEOUT_INTERVAL
|
||||
int "Rotation timeout interval"
|
||||
default 900
|
||||
help
|
||||
Base rotation timeout interval in seconds for BT address change & therefore the advertised beacon.(Default 5 minutes)
|
||||
|
||||
config ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL
|
||||
int "Randomize rotation timeout interval"
|
||||
default 150
|
||||
help
|
||||
Range in seconds for randomize the rotation timeout interval. (Default +/- ~2.5 minutes)
|
||||
|
||||
config ENA_TEK_ROLLING_PERIOD
|
||||
int "TEK rolling period"
|
||||
default 144
|
||||
help
|
||||
Defines the TEK rolling period in 10 minute steps. (Default 144 => 24 hours)
|
||||
endmenu
|
||||
|
||||
endmenu
|
||||
@@ -1,123 +0,0 @@
|
||||
// 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 <time.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
|
||||
#include "ena-beacons.h"
|
||||
|
||||
static uint32_t temp_beacons_count = 0;
|
||||
static ena_beacon_t temp_beacons[ENA_STORAGE_TEMP_BEACONS_MAX];
|
||||
|
||||
int ena_get_temp_beacon_index(uint8_t *rpi, uint8_t *aem)
|
||||
{
|
||||
for (int i = 0; i < temp_beacons_count; i++)
|
||||
{
|
||||
if (memcmp(temp_beacons[i].rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
|
||||
memcmp(temp_beacons[i].aem, aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ena_beacons_temp_refresh(uint32_t unix_timestamp)
|
||||
{
|
||||
for (int i = temp_beacons_count - 1; i >= 0; i--)
|
||||
{
|
||||
// check for treshold and add permanent beacon
|
||||
if (temp_beacons[i].timestamp_last - temp_beacons[i].timestamp_first >= ENA_BEACON_TRESHOLD)
|
||||
{
|
||||
ESP_LOGD(ENA_BEACON_LOG, "create beacon after treshold");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_BEACON_LOG, temp_beacons[i].rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ena_storage_add_beacon(&temp_beacons[i]);
|
||||
ena_storage_remove_temp_beacon(i);
|
||||
}
|
||||
else
|
||||
// delete temp beacons older than two times time window (two times to be safe, one times time window enough?!)
|
||||
if (unix_timestamp - temp_beacons[i].timestamp_last > (ENA_TIME_WINDOW * 2))
|
||||
{
|
||||
ESP_LOGD(ENA_BEACON_LOG, "remove old temporary beacon %u", i);
|
||||
ena_storage_remove_temp_beacon(i);
|
||||
}
|
||||
}
|
||||
|
||||
// update beacons
|
||||
temp_beacons_count = ena_storage_temp_beacons_count();
|
||||
for (int i = 0; i < temp_beacons_count; i++)
|
||||
{
|
||||
ena_storage_get_temp_beacon(i, &temp_beacons[i]);
|
||||
}
|
||||
|
||||
#if (CONFIG_ENA_STORAGE_DUMP)
|
||||
// DEBUG dump
|
||||
ena_storage_dump_teks();
|
||||
ena_storage_dump_exposure_information();
|
||||
ena_storage_dump_temp_beacons();
|
||||
ena_storage_dump_beacons();
|
||||
#endif
|
||||
}
|
||||
|
||||
void ena_beacons_cleanup(uint32_t unix_timestamp)
|
||||
{
|
||||
uint32_t count = ena_storage_beacons_count();
|
||||
ena_beacon_t beacon;
|
||||
for (int i = count - 1; i >= 0; i--)
|
||||
{
|
||||
ena_storage_get_beacon(i, &beacon);
|
||||
if (((unix_timestamp - beacon.timestamp_last) / (60 * 60 * 24)) > ENA_BEACON_CLEANUP_TRESHOLD)
|
||||
{
|
||||
ena_storage_remove_beacon(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
|
||||
{
|
||||
uint32_t beacon_index = ena_get_temp_beacon_index(rpi, aem);
|
||||
if (beacon_index == -1)
|
||||
{
|
||||
temp_beacons[temp_beacons_count].timestamp_first = unix_timestamp;
|
||||
memcpy(temp_beacons[temp_beacons_count].rpi, rpi, ENA_KEY_LENGTH);
|
||||
memcpy(temp_beacons[temp_beacons_count].aem, aem, ENA_AEM_METADATA_LENGTH);
|
||||
temp_beacons[temp_beacons_count].rssi = rssi;
|
||||
temp_beacons[temp_beacons_count].timestamp_last = unix_timestamp;
|
||||
beacon_index = ena_storage_add_temp_beacon(&temp_beacons[temp_beacons_count]);
|
||||
ESP_LOGD(ENA_BEACON_LOG, "new temporary beacon %d at %u", temp_beacons_count, unix_timestamp);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_BEACON_LOG, "RSSI %d", rssi);
|
||||
|
||||
if (beacon_index != temp_beacons_count)
|
||||
{
|
||||
ESP_LOGW(ENA_BEACON_LOG, "last temporary beacon index does not match array index!");
|
||||
}
|
||||
temp_beacons_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_beacons[beacon_index].rssi = (temp_beacons[beacon_index].rssi + rssi) / 2;
|
||||
temp_beacons[beacon_index].timestamp_last = unix_timestamp;
|
||||
ESP_LOGD(ENA_BEACON_LOG, "update temporary beacon %d at %u", beacon_index, unix_timestamp);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, temp_beacons[beacon_index].rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, temp_beacons[beacon_index].aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_BEACON_LOG, "RSSI %d", temp_beacons[beacon_index].rssi);
|
||||
ena_storage_set_temp_beacon(beacon_index, &temp_beacons[beacon_index]);
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
// 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 "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#include "ena-bluetooth-advertise.h"
|
||||
|
||||
static esp_ble_adv_params_t ena_adv_params = {
|
||||
.adv_int_min = 0x140, // 200 ms
|
||||
.adv_int_max = 0x190, // 250 ms
|
||||
.adv_type = ADV_TYPE_NONCONN_IND,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
|
||||
.channel_map = ADV_CHNL_ALL,
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
void ena_bluetooth_advertise_start(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_start_advertising(&ena_adv_params));
|
||||
}
|
||||
|
||||
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek)
|
||||
{
|
||||
uint8_t rpik[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t rpi[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aemk[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
|
||||
|
||||
ena_crypto_rpik(rpik, tek);
|
||||
|
||||
ena_crypto_rpi(rpi, rpik, enin);
|
||||
|
||||
ena_crypto_aemk(aemk, tek);
|
||||
|
||||
ena_crypto_aem(aem, aemk, rpi, esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV));
|
||||
|
||||
uint8_t adv_raw_data[31];
|
||||
// FLAG??? skipped on sniffed android packages!?
|
||||
adv_raw_data[0] = 0x02;
|
||||
adv_raw_data[1] = 0x01;
|
||||
adv_raw_data[2] = ENA_BLUETOOTH_TAG_DATA;
|
||||
|
||||
// SERVICE UUID
|
||||
adv_raw_data[3] = 0x03;
|
||||
adv_raw_data[4] = 0x03;
|
||||
adv_raw_data[5] = 0x6F;
|
||||
adv_raw_data[6] = 0xFD;
|
||||
|
||||
// SERVICE DATA
|
||||
adv_raw_data[7] = 0x17;
|
||||
adv_raw_data[8] = 0x16;
|
||||
|
||||
adv_raw_data[9] = 0x6F;
|
||||
adv_raw_data[10] = 0xFD;
|
||||
|
||||
for (int i = 0; i < ENA_KEY_LENGTH; i++)
|
||||
{
|
||||
adv_raw_data[i + 11] = rpi[i];
|
||||
}
|
||||
for (int i = 0; i < ENA_AEM_METADATA_LENGTH; i++)
|
||||
{
|
||||
adv_raw_data[i + ENA_KEY_LENGTH + 11] = aem[i];
|
||||
}
|
||||
|
||||
esp_ble_gap_config_adv_data_raw(adv_raw_data, sizeof(adv_raw_data));
|
||||
|
||||
ESP_LOGD(ENA_ADVERTISE_LOG, "payload for ENIN %u", enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_ADVERTISE_LOG, adv_raw_data, sizeof(adv_raw_data), ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_bluetooth_advertise_stop(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_stop_advertising());
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
// 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 <time.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-beacons.h"
|
||||
|
||||
#include "ena-bluetooth-scan.h"
|
||||
|
||||
static int scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
|
||||
static const uint16_t ENA_SERVICE_UUID = 0xFD6F;
|
||||
|
||||
static esp_ble_scan_params_t ena_scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
|
||||
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
|
||||
.scan_interval = 0x50, // don't know good parameters, just copied
|
||||
.scan_window = 0x30, // don't know good parameters, just copied
|
||||
.scan_duplicate = BLE_SCAN_DUPLICATE_ENABLE,
|
||||
};
|
||||
|
||||
void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
|
||||
uint32_t unix_timestamp = (uint32_t)time(NULL);
|
||||
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
|
||||
switch (event)
|
||||
{
|
||||
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
|
||||
ESP_LOGD(ENA_SCAN_LOG, "start scanning...");
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
||||
ESP_LOGD(ENA_SCAN_LOG, "stopped scanning...");
|
||||
ena_beacons_temp_refresh(unix_timestamp);
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_RESULT_EVT:
|
||||
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT)
|
||||
{
|
||||
uint8_t service_uuid_length = 0;
|
||||
uint8_t *service_uuid_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x03, &service_uuid_length);
|
||||
// check for ENA Service UUID
|
||||
if (service_uuid_length == sizeof(ENA_SERVICE_UUID) && memcmp(service_uuid_data, &ENA_SERVICE_UUID, service_uuid_length) == 0)
|
||||
{
|
||||
uint8_t service_data_length = 0;
|
||||
uint8_t *service_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x16, &service_data_length);
|
||||
if (service_data_length != (sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH + ENA_AEM_METADATA_LENGTH))
|
||||
{
|
||||
ESP_LOGW(ENA_SCAN_LOG, "received ENA Service with invalid payload");
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t *rpi = malloc(ENA_KEY_LENGTH);
|
||||
memcpy(rpi, &service_data[sizeof(ENA_SERVICE_UUID)], ENA_KEY_LENGTH);
|
||||
uint8_t *aem = malloc(ENA_AEM_METADATA_LENGTH);
|
||||
memcpy(aem, &service_data[sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH], ENA_AEM_METADATA_LENGTH);
|
||||
ena_beacon(unix_timestamp, rpi, aem, p->scan_rst.rssi);
|
||||
free(rpi);
|
||||
free(aem);
|
||||
}
|
||||
}
|
||||
else if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
ena_beacons_temp_refresh(unix_timestamp);
|
||||
ESP_LOGD(ENA_SCAN_LOG, "finished scanning...");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_init(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ena_scan_params));
|
||||
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ena_bluetooth_scan_event_callback));
|
||||
// init temporary beacons
|
||||
ena_beacons_temp_refresh((uint32_t)time(NULL));
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_start(uint32_t duration)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_SCANNING;
|
||||
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(duration));
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_stop(void)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_WAITING;
|
||||
ESP_ERROR_CHECK(esp_ble_gap_stop_scanning());
|
||||
}
|
||||
|
||||
int ena_bluetooth_scan_get_status(void)
|
||||
{
|
||||
return scan_status;
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
// 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 "mbedtls/md.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ESP_CRYPTO_LOG "ESP-CRYPTO"
|
||||
|
||||
static mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
||||
void ena_crypto_init(void)
|
||||
{
|
||||
mbedtls_entropy_context entropy;
|
||||
uint8_t pers[] = "Exposure Notifcation API esp32";
|
||||
int ret;
|
||||
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pers, sizeof(pers))) != 0)
|
||||
{
|
||||
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ena_crypto_enin(uint32_t unix_epoch_time)
|
||||
{
|
||||
return unix_epoch_time / ENA_TIME_WINDOW;
|
||||
}
|
||||
|
||||
void ena_crypto_tek(uint8_t *tek)
|
||||
{
|
||||
int ret;
|
||||
if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, tek, ENA_KEY_LENGTH)) != 0)
|
||||
{
|
||||
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n", -ret);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek)
|
||||
{
|
||||
const uint8_t rpik_info[] = "EN-RPIK";
|
||||
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, rpik_info, sizeof(rpik_info), rpik, ENA_KEY_LENGTH);
|
||||
}
|
||||
|
||||
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin)
|
||||
{
|
||||
uint8_t padded_data[] = "EN-RPI";
|
||||
padded_data[12] = (enin & 0x000000ff);
|
||||
padded_data[13] = (enin & 0x0000ff00) >> 8;
|
||||
padded_data[14] = (enin & 0x00ff0000) >> 16;
|
||||
padded_data[15] = (enin & 0xff000000) >> 24;
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, rpik, ENA_KEY_LENGTH * 8);
|
||||
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, padded_data, rpi);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
|
||||
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek)
|
||||
{
|
||||
uint8_t aemkInfo[] = "EN-AEMK";
|
||||
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, aemkInfo, sizeof(aemkInfo), aemk, ENA_KEY_LENGTH);
|
||||
}
|
||||
|
||||
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level)
|
||||
{
|
||||
uint8_t metadata[ENA_AEM_METADATA_LENGTH];
|
||||
metadata[0] = 0b01000000;
|
||||
metadata[1] = power_level;
|
||||
size_t count = 0;
|
||||
uint8_t sb[16] = {0};
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, aemk, ENA_KEY_LENGTH * 8);
|
||||
mbedtls_aes_crypt_ctr(&aes, ENA_AEM_METADATA_LENGTH, &count, rpi, sb, metadata, aem);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
@@ -1,307 +0,0 @@
|
||||
// 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 <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 "ena-exposure.h"
|
||||
|
||||
static ena_exposure_summary_t *current_summary;
|
||||
|
||||
static ena_exposure_config_t DEFAULT_ENA_EXPOSURE_CONFIG = {
|
||||
// transmission_risk_values
|
||||
{
|
||||
MINIMAL, // UNKNOWN
|
||||
LOW, // CONFIRMED_TEST_LOW
|
||||
MEDIUM, // CONFIRMED_TEST_STANDARD
|
||||
HIGH, // CONFIRMED_TEST_HIGH
|
||||
VERY_HIGH, // CONFIRMED_CLINICAL_DIAGNOSIS
|
||||
VERY_LOW, // SELF_REPORT
|
||||
ZERO, // NEGATIVE
|
||||
MINIMAL // RECURSIVE
|
||||
},
|
||||
// duration_risk_values
|
||||
{
|
||||
MINIMAL, // D = 0 min
|
||||
MINIMAL, // D <= 5 min
|
||||
MEDIUM, // D <= 10 min
|
||||
VERY_HIGH, // D <= 15 min
|
||||
VERY_HIGH, // D <= 20 min
|
||||
MAXIMUM, // D <= 25 min
|
||||
MAXIMUM, // D <= 30 min
|
||||
MAXIMUM // D > 30 min
|
||||
},
|
||||
// days_risk_values
|
||||
{
|
||||
MINIMAL, // >= 14 days
|
||||
VERY_LOW, // 12-13 days
|
||||
VERY_LOW, // 10-11 days
|
||||
MEDIUM, // 8-9 days
|
||||
HIGH, // 6-7 days
|
||||
MAXIMUM, // 4-5 days
|
||||
MAXIMUM, // 2-3 days
|
||||
MAXIMUM // 0-1 days
|
||||
},
|
||||
// attenuation_risk_values
|
||||
{
|
||||
MINIMAL, // A > 73 dB
|
||||
MINIMAL, // 73 >= A > 63
|
||||
MINIMAL, // 63 >= A > 61
|
||||
MAXIMUM, // 51 >= A > 33
|
||||
MAXIMUM, // 33 >= A > 27
|
||||
MAXIMUM, // 27 >= A > 15
|
||||
MAXIMUM, // 15 >= A > 10
|
||||
MAXIMUM // A <= 10
|
||||
},
|
||||
};
|
||||
|
||||
int ena_exposure_transmission_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
return config->transmission_risk_values[params.report_type];
|
||||
}
|
||||
|
||||
int ena_exposure_duration_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
// calc duration level
|
||||
int duration_level = MINUTES_0;
|
||||
if (params.duration > 0)
|
||||
{
|
||||
if (params.duration <= 5)
|
||||
{
|
||||
duration_level = MINUTES_5;
|
||||
}
|
||||
else if (params.duration <= 10)
|
||||
{
|
||||
duration_level = MINUTES_10;
|
||||
}
|
||||
if (params.duration <= 15)
|
||||
{
|
||||
duration_level = MINUTES_15;
|
||||
}
|
||||
if (params.duration <= 20)
|
||||
{
|
||||
duration_level = MINUTES_20;
|
||||
}
|
||||
if (params.duration <= 25)
|
||||
{
|
||||
duration_level = MINUTES_25;
|
||||
}
|
||||
if (params.duration <= 30)
|
||||
{
|
||||
duration_level = MINUTES_30;
|
||||
}
|
||||
else
|
||||
{
|
||||
duration_level = MINUTES_LONGER;
|
||||
}
|
||||
}
|
||||
|
||||
return config->duration_risk_values[duration_level];
|
||||
}
|
||||
|
||||
int ena_exposure_days_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
// calc days level
|
||||
int days_level = DAYS_14;
|
||||
|
||||
if (params.days < 2)
|
||||
{
|
||||
days_level = DAYS_0;
|
||||
}
|
||||
else if (params.days < 4)
|
||||
{
|
||||
days_level = DAYS_3;
|
||||
}
|
||||
else if (params.days < 6)
|
||||
{
|
||||
days_level = DAYS_5;
|
||||
}
|
||||
else if (params.days < 8)
|
||||
{
|
||||
days_level = DAYS_7;
|
||||
}
|
||||
else if (params.days < 10)
|
||||
{
|
||||
days_level = DAYS_9;
|
||||
}
|
||||
else if (params.days < 12)
|
||||
{
|
||||
days_level = DAYS_11;
|
||||
}
|
||||
else if (params.days < 14)
|
||||
{
|
||||
days_level = DAYS_13;
|
||||
}
|
||||
|
||||
return config->days_risk_values[days_level];
|
||||
}
|
||||
|
||||
int ena_exposure_attenuation_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
// calc attenuation level
|
||||
int attenuation_level = ATTENUATION_73;
|
||||
|
||||
if (params.attenuation <= 10)
|
||||
{
|
||||
attenuation_level = ATTENUATION_LOWER;
|
||||
}
|
||||
else if (params.attenuation <= 15)
|
||||
{
|
||||
attenuation_level = ATTENUATION_10;
|
||||
}
|
||||
else if (params.attenuation <= 27)
|
||||
{
|
||||
attenuation_level = ATTENUATION_15;
|
||||
}
|
||||
else if (params.attenuation <= 33)
|
||||
{
|
||||
attenuation_level = ATTENUATION_27;
|
||||
}
|
||||
else if (params.attenuation <= 51)
|
||||
{
|
||||
attenuation_level = ATTENUATION_33;
|
||||
}
|
||||
else if (params.attenuation <= 63)
|
||||
{
|
||||
attenuation_level = ATTENUATION_51;
|
||||
}
|
||||
else if (params.attenuation <= 73)
|
||||
{
|
||||
attenuation_level = ATTENUATION_63;
|
||||
}
|
||||
|
||||
return config->attenuation_risk_values[attenuation_level];
|
||||
}
|
||||
|
||||
int ena_exposure_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params)
|
||||
{
|
||||
int score = 1;
|
||||
score *= ena_exposure_transmission_risk_score(config, params);
|
||||
|
||||
score *= ena_exposure_duration_risk_score(config, params);
|
||||
|
||||
score *= ena_exposure_days_risk_score(config, params);
|
||||
|
||||
score *= ena_exposure_attenuation_risk_score(config, params);
|
||||
|
||||
if (score > 255)
|
||||
{
|
||||
score = 255;
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
void ena_exposure_summary(ena_exposure_config_t *config)
|
||||
{
|
||||
uint32_t count = ena_storage_exposure_information_count();
|
||||
uint32_t current_time = (uint32_t)time(NULL);
|
||||
|
||||
if (current_summary == NULL)
|
||||
{
|
||||
current_summary = malloc(sizeof(ena_exposure_summary_t));
|
||||
}
|
||||
|
||||
current_summary->last_update = ena_storage_read_last_exposure_date();
|
||||
current_summary->days_since_last_exposure = INT_MAX;
|
||||
current_summary->max_risk_score = 0;
|
||||
current_summary->risk_score_sum = 0;
|
||||
current_summary->num_exposures = count;
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
current_summary->days_since_last_exposure = -1;
|
||||
}
|
||||
|
||||
ena_exposure_information_t exposure_info;
|
||||
ena_exposure_parameter_t params;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
ena_storage_get_exposure_information(i, &exposure_info);
|
||||
params.days = (current_time - exposure_info.day) / (60 * 60 * 24); // difference in days
|
||||
if (params.days < current_summary->days_since_last_exposure)
|
||||
{
|
||||
current_summary->days_since_last_exposure = params.days;
|
||||
}
|
||||
params.duration = exposure_info.duration_minutes;
|
||||
params.attenuation = exposure_info.typical_attenuation;
|
||||
int score = ena_exposure_risk_score(config, params);
|
||||
if (score > current_summary->max_risk_score)
|
||||
{
|
||||
current_summary->max_risk_score = score;
|
||||
}
|
||||
current_summary->risk_score_sum += score;
|
||||
}
|
||||
}
|
||||
|
||||
ena_exposure_summary_t *ena_exposure_current_summary(void)
|
||||
{
|
||||
if (current_summary == NULL)
|
||||
{
|
||||
ena_exposure_summary(ena_exposure_default_config());
|
||||
}
|
||||
|
||||
return current_summary;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
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 = 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)
|
||||
{
|
||||
exposure_info.min_attenuation = beacon.rssi;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
ena_storage_add_exposure_information(&exposure_info);
|
||||
}
|
||||
}
|
||||
@@ -1,491 +0,0 @@
|
||||
// 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 "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
#include "ena-storage.h"
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define BLOCK_SIZE (4096)
|
||||
|
||||
const int ENA_STORAGE_LAST_EXPOSURE_DATE_ADDRESS = (ENA_STORAGE_START_ADDRESS);
|
||||
const int ENA_STORAGE_TEK_COUNT_ADDRESS = (ENA_STORAGE_LAST_EXPOSURE_DATE_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_TEK_START_ADDRESS = (ENA_STORAGE_TEK_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS = (ENA_STORAGE_TEK_START_ADDRESS + sizeof(ena_tek_t) * ENA_STORAGE_TEK_MAX);
|
||||
const int ENA_STORAGE_EXPOSURE_INFORMATION_START_ADDRESS = (ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS + sizeof(ena_exposure_information_t) * ENA_STORAGE_EXPOSURE_INFORMATION_MAX);
|
||||
const int ENA_STORAGE_TEMP_BEACONS_START_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + sizeof(ena_beacon_t) * ENA_STORAGE_TEMP_BEACONS_MAX);
|
||||
const int ENA_STORAGE_BEACONS_START_ADDRESS = (ENA_STORAGE_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
|
||||
void ena_storage_read(size_t address, void *data, size_t size)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, address, data, size));
|
||||
vTaskDelay(1);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read data at %u", address);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_storage_write(size_t address, void *data, size_t size)
|
||||
{
|
||||
const int block_num = address / BLOCK_SIZE;
|
||||
// check for overflow
|
||||
if (address + size <= (block_num + 1) * BLOCK_SIZE)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
const int block_start = block_num * BLOCK_SIZE;
|
||||
const int block_address = address - block_start;
|
||||
void *buffer = malloc(BLOCK_SIZE);
|
||||
if (buffer == NULL)
|
||||
{
|
||||
ESP_LOGE(ENA_STORAGE_LOG, "Warning %s malloc low memory", "buffer");
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read block %d buffer: start %d size %u", block_num, block_start, BLOCK_SIZE);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, block_start, buffer, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_start, BLOCK_SIZE));
|
||||
|
||||
memcpy((buffer + block_address), data, size);
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_write(partition, block_start, buffer, BLOCK_SIZE));
|
||||
free(buffer);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write data at %u", address);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num);
|
||||
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;
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "block1_address %d, block1_size %d (block %d)", address, data1_size, block_num);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "block2_address %d, block2_size %d (block %d)", block2_address, data2_size, block_num + 1);
|
||||
void *data1 = malloc(data1_size);
|
||||
memcpy(data1, data, data1_size);
|
||||
ena_storage_write(address, data1, data1_size);
|
||||
free(data1);
|
||||
|
||||
void *data2 = malloc(data2_size);
|
||||
memcpy(data2, (data + data1_size), data2_size);
|
||||
ena_storage_write(block2_address, data2, data2_size);
|
||||
free(data2);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
|
||||
{
|
||||
int block_num_start = address / BLOCK_SIZE;
|
||||
// check for overflow
|
||||
if (address + size <= (block_num_start + 1) * BLOCK_SIZE)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
|
||||
int block_num_end = end_address / BLOCK_SIZE;
|
||||
size_t block_start = address - block_num_start * BLOCK_SIZE;
|
||||
while (block_num_end >= block_num_start)
|
||||
{
|
||||
|
||||
void *buffer = malloc(BLOCK_SIZE);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
// shift inside buffer
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "shift block %d from %u to %u with size %u", block_num_start, (block_start + size), block_start, (BLOCK_SIZE - block_start - size));
|
||||
memcpy((buffer + block_start), (buffer + block_start + size), BLOCK_SIZE - block_start - size);
|
||||
if (block_num_end > block_num_start)
|
||||
{
|
||||
void *buffer_next_block = malloc(BLOCK_SIZE);
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, (block_num_start + 1) * BLOCK_SIZE, buffer_next_block, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
// shift from next block
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "shift next block size %u", size);
|
||||
memcpy((buffer + BLOCK_SIZE - size), buffer_next_block, size);
|
||||
free(buffer_next_block);
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_num_start * BLOCK_SIZE, BLOCK_SIZE));
|
||||
ESP_ERROR_CHECK(esp_partition_write(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
|
||||
free(buffer);
|
||||
|
||||
block_num_start++;
|
||||
block_start = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num_start);
|
||||
const size_t block1_address = address;
|
||||
const size_t block2_address = (block_num_start + 1) * BLOCK_SIZE;
|
||||
const size_t data2_size = address + size - block2_address;
|
||||
const size_t data1_size = size - data2_size;
|
||||
ena_storage_shift_delete(block1_address, block2_address, data1_size);
|
||||
ena_storage_shift_delete(block2_address, end_address - data1_size, data2_size);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ena_storage_read_last_exposure_date(void)
|
||||
{
|
||||
uint32_t timestamp = 0;
|
||||
ena_storage_read(ENA_STORAGE_LAST_EXPOSURE_DATE_ADDRESS, ×tamp, sizeof(uint32_t));
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
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_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));
|
||||
if (tek_count < 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX) - 1;
|
||||
ena_storage_read(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read last tek %u:", tek->enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
return tek_count;
|
||||
}
|
||||
|
||||
void ena_storage_write_tek(ena_tek_t *tek)
|
||||
{
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX);
|
||||
ena_storage_write(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
|
||||
|
||||
tek_count++;
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write tek: ENIN %u", tek->enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
uint32_t ena_storage_exposure_information_count(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read exposure information count: %u", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_get_exposure_information(uint32_t index, ena_exposure_information_t *exposure_info)
|
||||
{
|
||||
ena_storage_read(ENA_STORAGE_EXPOSURE_INFORMATION_START_ADDRESS + index * sizeof(ena_exposure_information_t), exposure_info, sizeof(ena_exposure_information_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read exporuse information: day %u, duration %d", exposure_info->day, exposure_info->duration_minutes);
|
||||
}
|
||||
|
||||
void ena_storage_add_exposure_information(ena_exposure_information_t *exposure_info)
|
||||
{
|
||||
uint32_t count = ena_storage_exposure_information_count();
|
||||
ena_storage_write(ENA_STORAGE_EXPOSURE_INFORMATION_START_ADDRESS + count * sizeof(ena_exposure_information_t), exposure_info, sizeof(ena_exposure_information_t));
|
||||
count++;
|
||||
ena_storage_write(ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write exposure info: day %u, duration %d", exposure_info->day, exposure_info->duration_minutes);
|
||||
}
|
||||
|
||||
uint32_t ena_storage_temp_beacons_count(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp beacons count: %u", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_get_temp_beacon(uint32_t index, ena_beacon_t *beacon)
|
||||
{
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp beacon: first %u, last %u and rssi %d", beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
uint32_t ena_storage_add_temp_beacon(ena_beacon_t *beacon)
|
||||
{
|
||||
uint32_t count = ena_storage_temp_beacons_count();
|
||||
// overwrite older temporary beacons?!
|
||||
uint8_t index = count % ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
ena_storage_set_temp_beacon(index, beacon);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "add temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
count++;
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
void ena_storage_set_temp_beacon(uint32_t index, ena_beacon_t *beacon)
|
||||
{
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "set temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_storage_remove_temp_beacon(uint32_t index)
|
||||
{
|
||||
uint32_t count = ena_storage_temp_beacons_count();
|
||||
size_t address_from = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t);
|
||||
size_t address_to = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + count * sizeof(ena_beacon_t);
|
||||
|
||||
ena_storage_shift_delete(address_from, address_to, sizeof(ena_beacon_t));
|
||||
|
||||
count--;
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "remove temp beacon: %u", index);
|
||||
}
|
||||
|
||||
uint32_t ena_storage_beacons_count(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read contancts count: %u", count);
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon)
|
||||
{
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read beacon: first %u, last %u and rssi %d", beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_storage_add_beacon(ena_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
uint32_t count = ena_storage_beacons_count();
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_START_ADDRESS + count * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
count++;
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write beacon: first %u, last %u and rssi %d", beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_storage_remove_beacon(uint32_t index)
|
||||
{
|
||||
uint32_t count = ena_storage_beacons_count();
|
||||
size_t address_from = ENA_STORAGE_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t);
|
||||
size_t address_to = ENA_STORAGE_BEACONS_START_ADDRESS + count * sizeof(ena_beacon_t);
|
||||
|
||||
ena_storage_shift_delete(address_from, address_to, sizeof(ena_beacon_t));
|
||||
|
||||
count--;
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "remove beacon: %u", index);
|
||||
}
|
||||
|
||||
void ena_storage_erase(void)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased partition %s!", ENA_STORAGE_PARTITION_NAME);
|
||||
|
||||
uint32_t count = 0;
|
||||
ena_storage_write(ENA_STORAGE_LAST_EXPOSURE_DATE_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
void ena_storage_erase_tek(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEK_MAX;
|
||||
|
||||
if (count < ENA_STORAGE_TEK_MAX)
|
||||
{
|
||||
stored = count;
|
||||
}
|
||||
|
||||
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);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d teks (size %u at %u)", stored, size, ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
void ena_storage_erase_exposure_information(void)
|
||||
{
|
||||
uint32_t count = ena_storage_exposure_information_count();
|
||||
uint32_t stored = ENA_STORAGE_EXPOSURE_INFORMATION_MAX;
|
||||
|
||||
if (count < ENA_STORAGE_EXPOSURE_INFORMATION_MAX)
|
||||
{
|
||||
stored = count;
|
||||
}
|
||||
|
||||
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);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d exposure information (size %u at %u)", stored, size, ENA_STORAGE_EXPOSURE_INFORMATION_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
void ena_storage_erase_temporary_beacon(void)
|
||||
{
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
|
||||
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
|
||||
{
|
||||
stored = beacon_count;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d temporary beacons (size %u at %u)", stored, size, ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
void ena_storage_erase_beacon(void)
|
||||
{
|
||||
uint32_t beacon_count = 0;
|
||||
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);
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d beacons (size %u at %u)", beacon_count, size, ENA_STORAGE_BEACONS_COUNT_ADDRESS);
|
||||
}
|
||||
|
||||
void ena_storage_dump_hash_array(uint8_t *data, size_t size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
printf("%02x", data[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" %02x", data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_teks(void)
|
||||
{
|
||||
ena_tek_t tek;
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEK_MAX;
|
||||
|
||||
if (tek_count < ENA_STORAGE_TEK_MAX)
|
||||
{
|
||||
stored = tek_count;
|
||||
}
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs (%u stored)\n", tek_count, stored);
|
||||
printf("#,enin,tek,rolling_period\n");
|
||||
for (int i = 0; i < stored; i++)
|
||||
{
|
||||
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + i * sizeof(ena_tek_t);
|
||||
ena_storage_read(address, &tek, sizeof(ena_tek_t));
|
||||
printf("%d,%u,", i, tek.enin);
|
||||
ena_storage_dump_hash_array(tek.key_data, ENA_KEY_LENGTH);
|
||||
printf(",%u\n", tek.rolling_period);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_exposure_information(void)
|
||||
{
|
||||
ena_exposure_information_t exposure_info;
|
||||
uint32_t exposure_information_count = ena_storage_exposure_information_count();
|
||||
uint32_t stored = ENA_STORAGE_EXPOSURE_INFORMATION_MAX;
|
||||
|
||||
if (exposure_information_count < ENA_STORAGE_EXPOSURE_INFORMATION_MAX)
|
||||
{
|
||||
stored = exposure_information_count;
|
||||
}
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u exposure information (%u stored)\n", exposure_information_count, stored);
|
||||
printf("#,day,typical_attenuation,min_attenuation,duration_minutes,report_type\n");
|
||||
for (int i = 0; i < stored; i++)
|
||||
{
|
||||
|
||||
size_t address = ENA_STORAGE_EXPOSURE_INFORMATION_START_ADDRESS + i * sizeof(ena_exposure_information_t);
|
||||
ena_storage_read(address, &exposure_info, sizeof(ena_exposure_information_t));
|
||||
printf("%d,%u,%d,%d,%d,%d\n", i, exposure_info.day, exposure_info.typical_attenuation, exposure_info.min_attenuation, exposure_info.duration_minutes, exposure_info.report_type);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_temp_beacons(void)
|
||||
{
|
||||
ena_beacon_t beacon;
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
|
||||
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
|
||||
{
|
||||
stored = beacon_count;
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary beacons (%u stored)\n", beacon_count, stored);
|
||||
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < stored; i++)
|
||||
{
|
||||
ena_storage_get_temp_beacon(i, &beacon);
|
||||
printf("%d,%u,%u,", i, beacon.timestamp_first, beacon.timestamp_last);
|
||||
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", beacon.rssi);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_beacons(void)
|
||||
{
|
||||
|
||||
ena_beacon_t beacon;
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u beacons\n", beacon_count);
|
||||
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < beacon_count; i++)
|
||||
{
|
||||
ena_storage_get_beacon(i, &beacon);
|
||||
printf("%d,%u,%u,", i, beacon.timestamp_first, beacon.timestamp_last);
|
||||
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", beacon.rssi);
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
// 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 <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
#include "ena-bluetooth-scan.h"
|
||||
#include "ena-bluetooth-advertise.h"
|
||||
#include "ena-beacons.h"
|
||||
|
||||
#include "ena.h"
|
||||
|
||||
static ena_tek_t last_tek; // last ENIN
|
||||
static uint32_t next_rpi_timestamp; // next rpi
|
||||
|
||||
void ena_next_rpi_timestamp(uint32_t timestamp)
|
||||
{
|
||||
int random_interval = esp_random() % (2 * ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL);
|
||||
if (random_interval > ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL)
|
||||
{
|
||||
random_interval = ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL - random_interval;
|
||||
}
|
||||
next_rpi_timestamp = timestamp + ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval;
|
||||
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)
|
||||
{
|
||||
static uint32_t unix_timestamp = 0;
|
||||
static uint32_t current_enin = 0;
|
||||
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);
|
||||
// clean up old beacons
|
||||
ena_beacons_cleanup(unix_timestamp);
|
||||
}
|
||||
|
||||
// change RPI
|
||||
if (unix_timestamp >= next_rpi_timestamp)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_start(void)
|
||||
{
|
||||
#if (CONFIG_ENA_STORAGE_ERASE)
|
||||
ena_storage_erase();
|
||||
#endif
|
||||
|
||||
if (ena_storage_read_last_exposure_date() == 0xFFFFFFFF)
|
||||
{
|
||||
ena_storage_erase();
|
||||
}
|
||||
|
||||
// init NVS for BLE
|
||||
esp_err_t ret;
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
}
|
||||
|
||||
// init BLE
|
||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
|
||||
{
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
||||
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
||||
}
|
||||
|
||||
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bluedroid_init());
|
||||
}
|
||||
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_INITIALIZED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bluedroid_enable());
|
||||
}
|
||||
|
||||
// new bluetooth address nesseccary?
|
||||
uint8_t bt_address[ESP_BD_ADDR_LEN];
|
||||
esp_fill_random(bt_address, ESP_BD_ADDR_LEN);
|
||||
bt_address[0] |= 0xC0;
|
||||
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(bt_address));
|
||||
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_gap_config_local_privacy(true));
|
||||
|
||||
// init ENA
|
||||
ena_crypto_init();
|
||||
|
||||
uint32_t unix_timestamp = (uint32_t)time(NULL);
|
||||
|
||||
uint32_t current_enin = ena_crypto_enin(unix_timestamp);
|
||||
uint32_t tek_count = ena_storage_read_last_tek(&last_tek);
|
||||
|
||||
ena_next_rpi_timestamp(unix_timestamp);
|
||||
|
||||
// read last TEK or create new
|
||||
if (tek_count == 0 || (current_enin - last_tek.enin) >= last_tek.rolling_period)
|
||||
{
|
||||
ena_crypto_tek(last_tek.key_data);
|
||||
last_tek.enin = ena_crypto_enin(unix_timestamp);
|
||||
// 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);
|
||||
}
|
||||
|
||||
// init scan
|
||||
ena_bluetooth_scan_init();
|
||||
|
||||
// init and start advertising
|
||||
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
|
||||
ena_bluetooth_advertise_start();
|
||||
// initial scan on every start
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
|
||||
// what is a good stack size here?
|
||||
// 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();
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief handles scanned data by storing temporary beacons, check for threshold and store beacons permanently
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_BEACON_H_
|
||||
#define _ena_BEACON_H_
|
||||
|
||||
#define ENA_BEACON_LOG "ESP-ENA-beacon" // TAG for Logging
|
||||
#define ENA_BEACON_TRESHOLD (CONFIG_ENA_BEACON_TRESHOLD) // meet for longer than 5 minutes
|
||||
#define ENA_BEACON_CLEANUP_TRESHOLD (CONFIG_ENA_BEACON_CLEANUP_TRESHOLD) // threshold (in days) for stored beacons to be removed
|
||||
|
||||
/**
|
||||
* @brief check temporary beacon for threshold or expiring
|
||||
*
|
||||
* This function checks all current temporary beacons if the contact threshold is
|
||||
* reached or if the temporary contact can be discarded.
|
||||
*
|
||||
* @param[in] unix_timestamp current time as UNIX timestamp to compare
|
||||
*
|
||||
*/
|
||||
void ena_beacons_temp_refresh(uint32_t unix_timestamp);
|
||||
|
||||
/**
|
||||
* @brief check stored beacons to expire
|
||||
*
|
||||
* This function checks for all stored beacons if the last timestamp is over a threshold to remove the beacon.
|
||||
*
|
||||
* @param[in] unix_timestamp current time as UNIX timestamp to compate
|
||||
*
|
||||
*/
|
||||
void ena_beacons_cleanup(uint32_t unix_timestamp);
|
||||
|
||||
/**
|
||||
* @brief handle new beacon received from a BLE scan
|
||||
*
|
||||
* This function gets called when a running BLE scan received a new ENA payload.
|
||||
* On already detected RPI this will update just the timestamp and RSSI.
|
||||
*
|
||||
* @param[in] unix_timestamp UNIX timestamp when beacon was made
|
||||
* @param[in] rpi received RPI from scanned payload
|
||||
* @param[in] aem received AEM from scanned payload
|
||||
* @param[in] rssi measured RSSI on scan
|
||||
*
|
||||
*/
|
||||
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
|
||||
|
||||
#endif
|
||||
@@ -1,52 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief BLE advertising to send own beacons
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_BLUETOOTH_ADVERTISE_H_
|
||||
#define _ena_BLUETOOTH_ADVERTISE_H_
|
||||
|
||||
#define ENA_ADVERTISE_LOG "ESP-ENA-advertise" // TAG for Logging
|
||||
#define ENA_BLUETOOTH_TAG_DATA (0x1A) // Data for BLE payload TAG
|
||||
|
||||
/**
|
||||
* @brief Start BLE advertising
|
||||
*/
|
||||
void ena_bluetooth_advertise_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set payload for BLE advertising
|
||||
*
|
||||
* This will set the payload for based on given ENIN and TEK.
|
||||
*
|
||||
* Source documents (Section: Advertising Payload)
|
||||
*
|
||||
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
|
||||
*
|
||||
* @param[in] enin ENIN defining the start of the tek vadility. This should be the ENIN for the current timestamp
|
||||
* @param[in] tek pointer to the TEK used to encrypt the payload.
|
||||
*/
|
||||
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief Stop BLE advertising
|
||||
*/
|
||||
void ena_bluetooth_advertise_stop(void);
|
||||
|
||||
#endif
|
||||
@@ -1,69 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief BLE scans for detecting other beacons
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_BLUETOOTH_SCAN_H_
|
||||
#define _ena_BLUETOOTH_SCAN_H_
|
||||
|
||||
#define ENA_SCAN_LOG "ESP-ENA-scan" // TAG for Logging
|
||||
#define ENA_SCANNING_TIME (CONFIG_ENA_SCANNING_TIME) // time how long a scan should run
|
||||
#define ENA_SCANNING_INTERVAL (CONFIG_ENA_SCANNING_INTERVAL) // interval for next scan to happen
|
||||
|
||||
/**
|
||||
* @brief status of BLE scan
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ENA_SCAN_STATUS_SCANNING = 0, // scan is running
|
||||
ENA_SCAN_STATUS_NOT_SCANNING, // scan is not running
|
||||
ENA_SCAN_STATUS_WAITING, // scan is not running but stopped manually
|
||||
} ena_bluetooth_scan_status;
|
||||
|
||||
/**
|
||||
* @brief initialize the BLE scanning
|
||||
*
|
||||
*/
|
||||
void ena_bluetooth_scan_init(void);
|
||||
|
||||
/**
|
||||
* @brief start BLE scanning for a given duration
|
||||
*
|
||||
* Source documents (Section: Scanning Behavior)
|
||||
*
|
||||
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
|
||||
*
|
||||
* @param[in] duration duration of the scan in seconds
|
||||
*/
|
||||
void ena_bluetooth_scan_start(uint32_t duration);
|
||||
|
||||
/**
|
||||
* @brief stop a running BLE scanning
|
||||
*/
|
||||
void ena_bluetooth_scan_stop(void);
|
||||
|
||||
/**
|
||||
* @brief return the current scanning status
|
||||
*
|
||||
* @return
|
||||
* current scan status
|
||||
*/
|
||||
int ena_bluetooth_scan_get_status(void);
|
||||
|
||||
#endif
|
||||
@@ -1,126 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief covers cryptography part (key creation, encryption etc.)
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_CRYPTO_H_
|
||||
#define _ena_CRYPTO_H_
|
||||
|
||||
#define ENA_TIME_WINDOW (600) // time window every 10 minutes
|
||||
#define ENA_KEY_LENGTH (16) // key length
|
||||
#define ENA_AEM_METADATA_LENGTH (4) // size of metadata
|
||||
#define ENA_TEK_ROLLING_PERIOD (CONFIG_ENA_TEK_ROLLING_PERIOD) // TEKRollingPeriod
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* @brief initialize cryptography
|
||||
*
|
||||
* This initialize the cryptography by setting up entropy.
|
||||
*/
|
||||
void ena_crypto_init(void);
|
||||
|
||||
/**
|
||||
* @brief calculate ENIntervalNumber (ENIN) for given UNIX timestamp
|
||||
*
|
||||
* Source documents (Section: ENIntervalNumber)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
*
|
||||
* @param[in] unix_timestamp UNIX Timestamp to calculate ENIN for
|
||||
*
|
||||
* @return
|
||||
* ENIN for given timestamp
|
||||
*/
|
||||
uint32_t ena_crypto_enin(uint32_t unix_timestamp);
|
||||
|
||||
/**
|
||||
* @brief calculate a new random Temporary Exposure Key (TEK)
|
||||
*
|
||||
* Source documents (Section: Temporary Exposure Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] tek pointer to the new TEK
|
||||
*/
|
||||
void ena_crypto_tek(uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Rolling Proximity Identifier Key (RPIK) with given TEK
|
||||
*
|
||||
* Source documents (Section: Rolling Proximity Identifier Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] rpik pointer to the new RPIK
|
||||
* @param[in] tek TEK for calculating RPIK
|
||||
*/
|
||||
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Rolling Proximity Identifier with given RPIK and ENIN
|
||||
*
|
||||
* Source documents (Section: Rolling Proximity Identifier)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] rpi pointer to the new RPI
|
||||
* @param[in] rpik RPIK for encrypting RPI
|
||||
* @param[in] enin ENIN to encrypt in RPI
|
||||
*/
|
||||
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Associated Encrypted Metadata Key (AEMK) with given TEK
|
||||
*
|
||||
* Source documents (Section: Associated Encrypted Metadata Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] aemk pointer to the new AEMK
|
||||
* @param[in] tek TEK for calculating AEMK
|
||||
*/
|
||||
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief create Associated Encrypted Metadata (AEM) with given AEMK along the RPI
|
||||
*
|
||||
* Source documents (Section: Associated Encrypted Metadata)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] aem pointer to the new AEM
|
||||
* @param[in] aemk AEMK for encrypting AEM
|
||||
* @param[in] rpi RPI for encrypting AEM
|
||||
* @param[in] power_level BLE power level to encrypt in AEM
|
||||
*/
|
||||
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level);
|
||||
|
||||
#endif
|
||||
@@ -1,240 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief compare temporary exposure keys with stored beacons, calculate score and risk
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_EXPOSURE_H_
|
||||
#define _ena_EXPOSURE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "esp_err.h"
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_EXPOSURE_LOG "ESP-ENA-exposure" // TAG for Logging
|
||||
|
||||
/**
|
||||
* @brief report type
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
UNKNOWN = 0,
|
||||
CONFIRMED_TEST_LOW = 1,
|
||||
CONFIRMED_TEST_STANDARD = 2,
|
||||
CONFIRMED_TEST_HIGH = 3,
|
||||
CONFIRMED_CLINICAL_DIAGNOSIS = 4,
|
||||
SELF_REPORT = 5,
|
||||
NEGATIVE = 6,
|
||||
RECURSIVE = 7,
|
||||
} ena_report_type_t;
|
||||
|
||||
/**
|
||||
* @brief duration risk
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
MINUTES_0 = 0, // D = 0 min
|
||||
MINUTES_5 = 1, // D <= 5 min
|
||||
MINUTES_10 = 2, // D <= 10 min
|
||||
MINUTES_15 = 3, // D <= 15 min
|
||||
MINUTES_20 = 4, // D <= 20 min
|
||||
MINUTES_25 = 5, // D <= 25 min
|
||||
MINUTES_30 = 6, // D <= 30 min
|
||||
MINUTES_LONGER = 7, // D > 30 min
|
||||
} ena_duration_risk_t;
|
||||
|
||||
/**
|
||||
* @brief day risk
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
DAYS_14 = 0, // >= 14 days
|
||||
DAYS_13 = 1, // 12-13 days
|
||||
DAYS_11 = 2, // 10-11 days
|
||||
DAYS_9 = 3, // 8-9 days
|
||||
DAYS_7 = 4, // 6-7 days
|
||||
DAYS_5 = 5, // 4-5 days
|
||||
DAYS_3 = 6, // 2-3 days
|
||||
DAYS_0 = 7, // 0-1 days
|
||||
} ena_day_risk_t;
|
||||
|
||||
/**
|
||||
* @brief attenuation risk
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ATTENUATION_73 = 0, // A > 73 dB
|
||||
ATTENUATION_63 = 1, // 73 >= A > 63
|
||||
ATTENUATION_51 = 2, // 63 >= A > 61
|
||||
ATTENUATION_33 = 3, // 51 >= A > 33
|
||||
ATTENUATION_27 = 4, // 33 >= A > 27
|
||||
ATTENUATION_15 = 5, // 27 >= A > 15
|
||||
ATTENUATION_10 = 6, // 15 >= A > 10
|
||||
ATTENUATION_LOWER = 7, // A <= 10
|
||||
} ena_attenuation_risk_t;
|
||||
|
||||
/**
|
||||
* @brief risk level from 0-8
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ZERO = 0,
|
||||
MINIMAL = 1,
|
||||
VERY_LOW = 2,
|
||||
LOW = 3,
|
||||
MEDIUM = 4,
|
||||
INCREASED = 5,
|
||||
HIGH = 6,
|
||||
VERY_HIGH = 7,
|
||||
MAXIMUM = 8,
|
||||
} ena_risk_level_t;
|
||||
|
||||
/**
|
||||
* @brief structure for exposure configuration
|
||||
*
|
||||
* The exposure configuration is used to calculate the risk score.
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t transmission_risk_values[8];
|
||||
uint8_t duration_risk_values[8];
|
||||
uint8_t days_risk_values[8];
|
||||
uint8_t attenuation_risk_values[8];
|
||||
} ena_exposure_config_t;
|
||||
|
||||
/**
|
||||
* @brief structure for exposure parameter
|
||||
*
|
||||
* These parameter are obtained from an exposure information to calculate the risk score.
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
ena_report_type_t report_type;
|
||||
int days;
|
||||
int duration;
|
||||
int attenuation;
|
||||
} ena_exposure_parameter_t;
|
||||
|
||||
/**
|
||||
* @brief structure for exposure summary
|
||||
*
|
||||
* This represents the current state of all exposures.
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint32_t last_update; // timestamp of last update of exposure data
|
||||
int days_since_last_exposure; // Number of days since the most recent exposure.
|
||||
int num_exposures; // Number of all exposure information
|
||||
int max_risk_score; // max. risk score of all exposure information
|
||||
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
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating score
|
||||
* @param[in] params the exposure parameter to calculate with
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int ena_exposure_transmission_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params);
|
||||
|
||||
/**
|
||||
* @brief calculate duration risk score
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating score
|
||||
* @param[in] params the exposure parameter to calculate with
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int ena_exposure_duration_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params);
|
||||
|
||||
/**
|
||||
* @brief calculate days risk score
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating score
|
||||
* @param[in] params the exposure parameter to calculate with
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int ena_exposure_days_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params);
|
||||
|
||||
/**
|
||||
* @brief calculate attenuation risk score
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating score
|
||||
* @param[in] params the exposure parameter to calculate with
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int ena_exposure_attenuation_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params);
|
||||
|
||||
/**
|
||||
* @brief calculate overall risk score
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating score
|
||||
* @param[in] params the exposure parameter to calculate with
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int ena_exposure_risk_score(ena_exposure_config_t *config, ena_exposure_parameter_t params);
|
||||
|
||||
/**
|
||||
* @brief returns the current exposure summary
|
||||
*
|
||||
* @param[in] config the exposure configuration used for calculating scores
|
||||
*/
|
||||
void ena_exposure_summary(ena_exposure_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief return the current exposure summary
|
||||
*
|
||||
* @return
|
||||
* ena_exposure_summary_t pointer to the current exposure summary
|
||||
*/
|
||||
ena_exposure_summary_t *ena_exposure_current_summary(void);
|
||||
|
||||
/**
|
||||
* @brief return a default exposure configuration
|
||||
*
|
||||
* @return
|
||||
* ena_exposure_config_t default exposure configuration
|
||||
*/
|
||||
ena_exposure_config_t *ena_exposure_default_config(void);
|
||||
|
||||
/**
|
||||
* @brief reads Temporary Exposue Key and check for exposures
|
||||
*
|
||||
* @param[in] temporary_exposure_key the temporary exposure keys to check
|
||||
*/
|
||||
void ena_exposure_check_temporary_exposure_key(ena_temporary_exposure_key_t temporary_exposure_key);
|
||||
|
||||
#endif
|
||||
@@ -1,289 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief storage part to store own TEKs and beacons
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_STORAGE_H_
|
||||
#define _ena_STORAGE_H_
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_STORAGE_LOG "ESP-ENA-storage" // TAG for Logging
|
||||
#define ENA_STORAGE_PARTITION_NAME (CONFIG_ENA_STORAGE_PARTITION_NAME) // name of partition to use for storing
|
||||
#define ENA_STORAGE_START_ADDRESS (CONFIG_ENA_STORAGE_START_ADDRESS) // start address of storage
|
||||
#define ENA_STORAGE_TEK_MAX (CONFIG_ENA_STORAGE_TEK_MAX) // Period of storing TEKs // length of a stored beacon -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
|
||||
#define ENA_STORAGE_TEMP_BEACONS_MAX (CONFIG_ENA_STORAGE_TEMP_BEACONS_MAX) // Maximum number of temporary stored beacons // length of a stored beacon -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
|
||||
#define ENA_STORAGE_EXPOSURE_INFORMATION_MAX (CONFIG_ENA_STORAGE_EXPOSURE_INFORMATION_MAX) // Maximum number of stored exposure information
|
||||
|
||||
/**
|
||||
* @brief structure for TEK
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t key_data[ENA_KEY_LENGTH]; // key data for encryption
|
||||
uint32_t enin; // ENIN marking start of validity
|
||||
uint8_t rolling_period; // period after validity start to mark key as expired
|
||||
} ena_tek_t;
|
||||
|
||||
/**
|
||||
* @brief sturcture for storing a beacons
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of beacon
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of beacon
|
||||
uint32_t timestamp_first; // timestamp of first recognition
|
||||
uint32_t timestamp_last; // timestamp of last recognition
|
||||
int rssi; // average measured RSSI
|
||||
} ena_beacon_t;
|
||||
|
||||
/**
|
||||
* @brief structure for storing a Exposure Information (combined ExposureInformation, ExposureWindow and ScanInstance from Google API >= 1.5)
|
||||
*/
|
||||
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 report_type; // Type of diagnosis associated with a key.
|
||||
} ena_exposure_information_t;
|
||||
|
||||
/**
|
||||
* @brief read bytes at given address
|
||||
*
|
||||
* @param[in] address the address to read bytes from
|
||||
* @param[out] data pointer to write the read data
|
||||
* @param[in] size how many bytes to read
|
||||
*/
|
||||
void ena_storage_read(size_t address, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief store bytes at given address
|
||||
*
|
||||
* @param[in] address the address to write bytes to
|
||||
* @param[in] data pointer to the data to write
|
||||
* @param[in] size how many bytes to write
|
||||
*/
|
||||
void ena_storage_write(size_t address, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief deletes bytes at given address and shift other data back
|
||||
*
|
||||
* @param[in] address the address to delete from
|
||||
* @param[in] end_address the address to mark end of shift
|
||||
* @param[in] size how many bytes to delete
|
||||
*/
|
||||
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size);
|
||||
|
||||
/**
|
||||
* @brief get timestamp of most recent exposure data
|
||||
*
|
||||
* @return
|
||||
* unix timestamp
|
||||
*/
|
||||
uint32_t ena_storage_read_last_exposure_date(void);
|
||||
|
||||
/**
|
||||
* @brief set timestamp of most recent exposure data
|
||||
*
|
||||
* @param[in] timestamp unix timestamp
|
||||
*/
|
||||
void ena_storage_write_last_exposure_date(uint32_t timestamp);
|
||||
|
||||
/**
|
||||
* @brief get last stored TEK
|
||||
*
|
||||
* @param[out] tek pointer to write last TEK to
|
||||
*
|
||||
* @return
|
||||
* total number of TEKs stored
|
||||
*/
|
||||
uint32_t ena_storage_read_last_tek(ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief store given TEK
|
||||
*
|
||||
* This will store the given TEK as new TEK.
|
||||
*
|
||||
* @param[in] tek the tek to store
|
||||
*/
|
||||
void ena_storage_write_tek(ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief get number of stored exposure information
|
||||
*
|
||||
* @return
|
||||
* total number of exposure information stored
|
||||
*/
|
||||
uint32_t ena_storage_exposure_information_count(void);
|
||||
|
||||
/**
|
||||
* @brief get exposure information at given index
|
||||
*
|
||||
* @param[in] index the index of the exposure information to read
|
||||
* @param[out] exposure_info pointer to exposure information to write to
|
||||
*/
|
||||
void ena_storage_get_exposure_information(uint32_t index, ena_exposure_information_t *exposure_info);
|
||||
|
||||
/**
|
||||
* @brief store exposure information
|
||||
*
|
||||
* @param[in] exposure_info new exposure information to store
|
||||
*/
|
||||
void ena_storage_add_exposure_information(ena_exposure_information_t *exposure_info);
|
||||
|
||||
/**
|
||||
* @brief get number of stored temporary beacons
|
||||
*
|
||||
* @return
|
||||
* total number of temporary beacons stored
|
||||
*/
|
||||
uint32_t ena_storage_temp_beacons_count(void);
|
||||
|
||||
/**
|
||||
* @brief get temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to read
|
||||
* @param[out] beacon pointer to temporary beacon to write to
|
||||
*/
|
||||
void ena_storage_get_temp_beacon(uint32_t index, ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief store temporary beacon
|
||||
*
|
||||
* @param[in] beacon new temporary beacon to store
|
||||
*
|
||||
* @return
|
||||
* index of new stored beacon
|
||||
*/
|
||||
uint32_t ena_storage_add_temp_beacon(ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief store temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to overwrite
|
||||
* @param[in] beacon temporary beacon to store
|
||||
*/
|
||||
void ena_storage_set_temp_beacon(uint32_t index, ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief remove temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to remove
|
||||
*/
|
||||
void ena_storage_remove_temp_beacon(uint32_t index);
|
||||
|
||||
/**
|
||||
* @brief get number of permanently stored beacons
|
||||
*
|
||||
* @return
|
||||
* total number of beacons stored
|
||||
*/
|
||||
uint32_t ena_storage_beacons_count(void);
|
||||
|
||||
/**
|
||||
* @brief get permanently stored beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the beacon to read
|
||||
* @param[out] beacon pointer to to write to
|
||||
*/
|
||||
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief permanently store beacon
|
||||
*
|
||||
* @param[in] beacon new beacon to permanently store
|
||||
*/
|
||||
void ena_storage_add_beacon(ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief remove beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the beacon to remove
|
||||
*/
|
||||
void ena_storage_remove_beacon(uint32_t index);
|
||||
|
||||
/**
|
||||
* @brief erase the storage
|
||||
*
|
||||
* This function completely deletes all stored data and resets the counters
|
||||
* of TEKs, temporary beacon and beacon to zero.
|
||||
*/
|
||||
void ena_storage_erase(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored TEKs
|
||||
*
|
||||
* This function deletes all stored TEKs and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_tek(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored exposure information
|
||||
*
|
||||
* This function deletes all stored exposure information and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_exposure_information(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored temporary beacons
|
||||
*
|
||||
* This function deletes all stored temporary beacons and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_temporary_beacon(void);
|
||||
|
||||
/**
|
||||
* @brief erase all permanently stored beacons
|
||||
*
|
||||
* This function deletes all stored beacons and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_beacon(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored TEKs to serial output
|
||||
*
|
||||
* This function prints all stored TEKs to serial output in
|
||||
* the following CSV format: #,enin,tek
|
||||
*/
|
||||
void ena_storage_dump_teks(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored exposure information to serial output
|
||||
*
|
||||
* This function prints all stored exposure information to serial output in
|
||||
* the following CSV format: #,day,typical_attenuation,min_attenuation,duration_minutes,report_type
|
||||
*/
|
||||
void ena_storage_dump_exposure_information(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored temporary beacons to serial output
|
||||
*
|
||||
* This function prints all stored temporary beacons to serial output in
|
||||
* the following CSV format: #,timestamp_first,timestamp_last,rpi,aem,rssi
|
||||
*/
|
||||
void ena_storage_dump_temp_beacons(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored beacons to serial output
|
||||
*
|
||||
* This function prints all stored beacons to serial output in
|
||||
* the following CSV format: #,timestamp,rpi,aem,rssi
|
||||
*/
|
||||
void ena_storage_dump_beacons(void);
|
||||
|
||||
#endif
|
||||
@@ -1,49 +0,0 @@
|
||||
// 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.
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @brief run all other ena parts together to time scanning, advertising and exposure checks
|
||||
*
|
||||
*/
|
||||
#ifndef _ena_H_
|
||||
#define _ena_H_
|
||||
|
||||
#define ENA_LOG "ESP-ENA" // TAG for Logging
|
||||
#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
|
||||
*
|
||||
* This initializes the complete stack of ESP_ENA. It will initialize BLE module and
|
||||
* starting a task for managing advertising and scanning processes.
|
||||
*
|
||||
*/
|
||||
void ena_start(void);
|
||||
|
||||
/**
|
||||
* @brief stop ena
|
||||
*/
|
||||
void ena_stop(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user