moved to configurable components, added SSD1306 support

This commit is contained in:
Lurkars
2020-07-15 22:22:54 +02:00
parent 1eedea1b9a
commit eaf1c74faa
35 changed files with 1577 additions and 621 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
idf_component_register(
SRCS "ena-detection.c" "ena-storage.c" "ena-crypto.c" "main.c" "ena.c" "ena-bluetooth-scan.c" "ena-bluetooth-advertise.c" "ena-detection.c" "ena-interface.c" "ena-interface-menu.c" "ena-interface-datetime.c"
SRCS
"main.c"
INCLUDE_DIRS ""
)
-5
View File
@@ -1,5 +0,0 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
-87
View File
@@ -1,87 +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 "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());
}
-51
View File
@@ -1,51 +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.
#ifndef _ena_BLUETOOTH_ADVERTISE_H_
#define _ena_BLUETOOTH_ADVERTISE_H_
#include "esp_gap_ble_api.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
-109
View File
@@ -1,109 +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-detection.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)
{
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_detections_temp_refresh((uint32_t)time(NULL));
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_detection((uint32_t)time(NULL), 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_detections_temp_refresh((uint32_t)time(NULL));
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 detections
ena_detections_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;
}
-67
View File
@@ -1,67 +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.
#ifndef _ena_BLUETOOTH_SCAN_H_
#define _ena_BLUETOOTH_SCAN_H_
#define ENA_SCAN_LOG "ESP-ENA-scan" // TAG for Logging
#define ENA_SCANNING_TIME (30) // scan for 30 seconds
#define ENA_SCANNING_INTERVAL (300) // scan every 5 minutes
#include "esp_gap_ble_api.h"
/**
* @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
-96
View File
@@ -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);
}
-121
View File
@@ -1,121 +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.
#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 (144) // 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
-91
View File
@@ -1,91 +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.
#ifndef _ena_DATASTRUCTURES_H_
#define _ena_DATASTRUCTURES_H_
#include <stdio.h>
#include "ena-crypto.h"
/**
* @brief different risk levels
*
* not used for now
*/
typedef enum
{
RISK_LEVEL_INVALID = 0,
RISK_LEVEL_LOWEST,
RISK_LEVEL_LOW,
RISK_LEVEL_LOW_MEDIUM,
RISK_LEVEL_MEDIUM,
RISK_LEVEL_MEDIUM_HIGH,
RISK_LEVEL_HIGH,
RISK_LEVEL_VERY_HIGH,
RISK_LEVEL_HIGHEST,
} ena_risklevel_t;
/**
* @brief configuration for risk score calculation
*
* not used for now
*/
typedef struct
{
int minimum_risk_score;
int attenuation_score[8];
int attenuation_weight;
int days_sinse_last_exposure_score[8];
int days_sinse_last_exposure_weight;
int duration_scores[8];
float duration_weight;
int transmission_risk_scores[8];
float transmission_risk_weight;
uint8_t duration_at_attenuation_thresholds[2];
} __packed ena_config_t;
/**
* @brief structure for TEK
*/
typedef struct
{
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
} __packed ena_tek_t;
/**
* @brief sturcture for a temporary detection
*/
typedef struct
{
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of detection
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of detection
uint32_t timestamp_first; // timestamp of first recognition
uint32_t timestamp_last; // timestamp of last recognition
int rssi; // last measured RSSI
} __packed ena_temp_detection_t;
/**
* @brief sturcture for a detection
*/
typedef struct
{
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of detection
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of detection
uint32_t timestamp; // timestamp of last recognition
int rssi; // last measured RSSI
} __packed ena_detection_t;
#endif
-116
View File
@@ -1,116 +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 "esp_log.h"
#include "ena-datastructures.h"
#include "ena-crypto.h"
#include "ena-storage.h"
#include "ena-detection.h"
static uint32_t temp_detections_count = 0;
static ena_temp_detection_t temp_detections[ENA_STORAGE_TEMP_DETECTIONS_MAX];
ena_detection_t ena_detections_convert(ena_temp_detection_t temp_detection)
{
ena_detection_t detection;
memcpy(detection.rpi, temp_detection.rpi, ENA_KEY_LENGTH);
memcpy(detection.aem, temp_detection.aem, ENA_AEM_METADATA_LENGTH);
detection.timestamp = temp_detection.timestamp_last;
detection.rssi = temp_detection.rssi;
return detection;
}
int ena_get_temp_detection_index(uint8_t *rpi, uint8_t *aem)
{
for (int i = 0; i < temp_detections_count; i++)
{
if (memcmp(temp_detections[i].rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
memcmp(temp_detections[i].aem, aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
{
return i;
}
}
return -1;
}
void ena_detections_temp_refresh(uint32_t unix_timestamp)
{
for (int i = temp_detections_count - 1; i >= 0; i--)
{
// check for treshold and add permanent detection
if (temp_detections[i].timestamp_last - temp_detections[i].timestamp_first >= ENA_DETECTION_TRESHOLD)
{
ESP_LOGD(ENA_DETECTION_LOG, "create detection after treshold");
ESP_LOG_BUFFER_HEXDUMP(ENA_DETECTION_LOG, temp_detections[i].rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ena_detection_t detection = ena_detections_convert(temp_detections[i]);
ena_storage_add_detection(&detection);
ena_storage_remove_temp_detection(i);
}
else
// delete temp detections older than two times time window (two times to be safe, one times time window enough?!)
if (unix_timestamp - temp_detections[i].timestamp_last > (ENA_TIME_WINDOW * 2))
{
ESP_LOGD(ENA_DETECTION_LOG, "remove old temporary detection %u", i);
ena_storage_remove_temp_detection(i);
}
}
// update detections
temp_detections_count = ena_storage_temp_detections_count();
for (int i = 0; i < temp_detections_count; i++)
{
ena_storage_get_temp_detection(i, &temp_detections[i]);
}
// DEBUG dump
ena_storage_dump_tek();
ena_storage_dump_temp_detections();
ena_storage_dump_detections();
}
void ena_detection(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
{
uint32_t detection_index = ena_get_temp_detection_index(rpi, aem);
if (detection_index == -1)
{
temp_detections[temp_detections_count].timestamp_first = unix_timestamp;
memcpy(temp_detections[temp_detections_count].rpi, rpi, ENA_KEY_LENGTH);
memcpy(temp_detections[temp_detections_count].aem, aem, ENA_AEM_METADATA_LENGTH);
temp_detections[temp_detections_count].rssi = rssi;
temp_detections[temp_detections_count].timestamp_last = unix_timestamp;
detection_index = ena_storage_add_temp_detection(&temp_detections[temp_detections_count]);
ESP_LOGD(ENA_DETECTION_LOG, "New temporary detection at %d with timestamp %u", temp_detections_count, unix_timestamp);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_DETECTION_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEX_LEVEL(ENA_DETECTION_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_DETECTION_LOG, "RSSI %d", rssi);
if (detection_index != temp_detections_count)
{
ESP_LOGW(ENA_DETECTION_LOG, "last temporary detection index does not match array index!");
}
temp_detections_count++;
}
else
{
temp_detections[detection_index].rssi = rssi;
temp_detections[detection_index].timestamp_last = unix_timestamp;
ESP_LOGD(ENA_DETECTION_LOG, "New Timestamp for temporary detection %d: %u", detection_index, unix_timestamp);
ena_storage_set_temp_detection(temp_detections_count, &temp_detections[temp_detections_count]);
}
}
-47
View File
@@ -1,47 +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.
#ifndef _ena_DETECTION_H_
#define _ena_DETECTION_H_
#define ENA_DETECTION_LOG "ESP-ENA-detection" // TAG for Logging
#define ENA_DETECTION_TRESHOLD (300) // meet for longer than 5 minutes
/**
* @brief check temporary detection for full detection or expiring
*
* This function checks all current temporary detections if the contact threshold is
* reached or if the temporary contact can be discarded.
*
* @param[in] unix_timestamp current time as UNIX timestamp to compate
*
*/
void ena_detections_temp_refresh(uint32_t unix_timestamp);
/**
* @brief handle new detection 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 detection 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_detection(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
#endif
-40
View File
@@ -1,40 +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 "esp_log.h"
#include "ena-interface.h"
#include "driver/touch_pad.h"
#include "ena-interface-menu.h"
#include "ena-interface-datetime.h"
static int interface_datetime_state = ENA_INTERFACE_DATETIME_STATE_YEAR;
void ena_interface_datetime_esc(void)
{
ena_interface_menu_start();
}
void ena_interface_datetime_start(void)
{
ena_interface_set_state(ENA_INTERFACE_STATE_SET_YEAR);
ena_interface_register_touch_callback(TOUCH_PAD_ESC, &ena_interface_datetime_esc);
ESP_LOGD(ENA_INTERFACE_LOG, "start datetime interface");
}
int ena_interface_datetime_state(void)
{
return interface_datetime_state;
}
-32
View File
@@ -1,32 +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.
#ifndef _ena_INTERFACE_DATETIME_H_
#define _ena_INTERFACE_DATETIME_H_
typedef enum
{
ENA_INTERFACE_DATETIME_STATE_YEAR = 0,
ENA_INTERFACE_DATETIME_STATE_MONTH,
ENA_INTERFACE_DATETIME_STATE_DAY,
ENA_INTERFACE_DATETIME_STATE_HOUR,
ENA_INTERFACE_DATETIME_STATE_MINUTE,
ENA_INTERFACE_DATETIME_STATE_SECONDS,
} ena_inerface_datetime_state;
void ena_interface_datetime_start(void);
int ena_interface_datetime_state(void);
#endif
-66
View File
@@ -1,66 +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 "esp_log.h"
#include "driver/touch_pad.h"
#include "ena-interface.h"
#include "ena-interface-datetime.h"
#include "ena-interface-menu.h"
static int interface_menu_state = ENA_INTERFACE_MENU_STATE_IDLE;
void ena_interface_menu_ok(void)
{
if (interface_menu_state == ENA_INTERFACE_MENU_STATE_SELECT_TIME) {
ena_interface_datetime_start();
}
}
void ena_interface_menu_up(void)
{
interface_menu_state--;
if (interface_menu_state < 0)
{
interface_menu_state = sizeof(interface_menu_state) - 1;
}
ESP_LOGD(ENA_INTERFACE_LOG, "menu up to %d", interface_menu_state);
}
void ena_interface_menu_down(void)
{
interface_menu_state++;
if (interface_menu_state == sizeof(interface_menu_state))
{
interface_menu_state = 0;
}
ESP_LOGD(ENA_INTERFACE_LOG, "menu down to %d", interface_menu_state);
}
void ena_interface_menu_start(void)
{
ena_interface_set_state(ENA_INTERFACE_STATE_MENU);
ena_interface_register_touch_callback(TOUCH_PAD_ESC, NULL);
ena_interface_register_touch_callback(TOUCH_PAD_OK, &ena_interface_menu_ok);
ena_interface_register_touch_callback(TOUCH_PAD_UP, &ena_interface_menu_up);
ena_interface_register_touch_callback(TOUCH_PAD_DOWN, &ena_interface_menu_down);
ESP_LOGD(ENA_INTERFACE_LOG, "start menu interface");
}
int ena_interface_menu_get_state(void)
{
return interface_menu_state;
}
-29
View File
@@ -1,29 +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.
#ifndef _ena_INTERFACE_MENU_H_
#define _ena_INTERFACE_MENU_H_
typedef enum
{
ENA_INTERFACE_MENU_STATE_IDLE = 0,
ENA_INTERFACE_MENU_STATE_SELECT_TIME,
ENA_INTERFACE_MENU_STATE_SELECT_INFO,
} ena_interface_menu_state;
void ena_interface_menu_start(void);
int ena_interface_menu_get_state(void);
#endif
-102
View File
@@ -1,102 +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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/touch_pad.h"
#include "esp_log.h"
#include "ena-interface.h"
static int interface_state = ENA_INTERFACE_STATE_IDLE;
static int touch_mapping[TOUCH_PAD_COUNT] = {0};
static bool touch_status[TOUCH_PAD_COUNT] = {0};
static ena_interface_touch_callback touch_callbacks[TOUCH_PAD_MAX];
void ena_interface_register_touch_callback(int touch_pad, ena_interface_touch_callback callback)
{
touch_callbacks[touch_pad] = callback;
}
void ena_interface_run(void *pvParameter)
{
uint16_t touch_value;
uint16_t touch_thresh;
bool touch_status_current[4] = {0};
while (1)
{
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_read_filtered(touch_id, &touch_value));
ESP_ERROR_CHECK_WITHOUT_ABORT(touch_pad_get_thresh(touch_id, &touch_thresh));
touch_status_current[i] = touch_value < touch_thresh;
if (!touch_status[i] & touch_status_current[i])
{
ESP_LOGD(ENA_INTERFACE_LOG, "touch %u at %d (thresh %u)", touch_value, touch_id, touch_thresh);
if (touch_callbacks[touch_id] != NULL)
{
(*touch_callbacks[touch_id])();
}
}
touch_status[i] = touch_status_current[i];
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void ena_interface_start(void)
{
ESP_ERROR_CHECK(touch_pad_init());
ESP_ERROR_CHECK(touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V));
ESP_ERROR_CHECK(touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW));
touch_mapping[0] = TOUCH_PAD_ESC;
touch_mapping[1] = TOUCH_PAD_OK;
touch_mapping[2] = TOUCH_PAD_UP;
touch_mapping[3] = TOUCH_PAD_DOWN;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK(touch_pad_config(touch_id, 0));
}
ESP_ERROR_CHECK(touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD));
uint16_t touch_value;
for (int i = 0; i < TOUCH_PAD_COUNT; i++)
{
int touch_id = touch_mapping[i];
ESP_ERROR_CHECK(touch_pad_read_filtered(touch_id, &touch_value));
ESP_ERROR_CHECK(touch_pad_set_thresh(touch_id, touch_value * 2 / 3));
ESP_LOGD(ENA_INTERFACE_LOG, "calibrate %u at %u (thresh %u)", touch_id, touch_value, (touch_value * 2 / 3));
}
xTaskCreate(&ena_interface_run, "ena_interface_run", configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
}
int ena_interface_get_state(void)
{
return interface_state;
}
void ena_interface_set_state(ena_interface_state state)
{
interface_state = state;
}
-80
View File
@@ -1,80 +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.
#ifndef _ena_INTERFACE_H_
#define _ena_INTERFACE_H_
#define ENA_INTERFACE_LOG "ESP-ENA-interface" // TAG for Logging
#define TOUCHPAD_FILTER_TOUCH_PERIOD (10)
#define TOUCH_PAD_COUNT (4)
#define TOUCH_PAD_ESC (TOUCH_PAD_NUM0)
#define TOUCH_PAD_OK (TOUCH_PAD_NUM6)
#define TOUCH_PAD_UP (TOUCH_PAD_NUM4)
#define TOUCH_PAD_DOWN (TOUCH_PAD_NUM3)
/**
* @brief different interface states
*/
typedef enum
{
ENA_INTERFACE_STATE_IDLE = 0, // ilde state, do nothing
ENA_INTERFACE_STATE_MENU, // main menu
ENA_INTERFACE_STATE_SET_YEAR, // set current year
ENA_INTERFACE_STATE_SET_MONTH, // set current month
ENA_INTERFACE_STATE_SET_DAY, // set current day
ENA_INTERFACE_STATE_SET_HOUR, // set current hour
ENA_INTERFACE_STATE_SET_MINUTE, // set current minute
ENA_INTERFACE_STATE_SET_SECONDS, // set current second
ENA_INTERFACE_STATE_STATUS, // view current status
} ena_interface_state;
/**
* @brief callback function on touch event
*/
typedef void (*ena_interface_touch_callback)(void);
/**
* @brief register a callback function for touch event
*
* @param[in] touch_pad id of the touchpad to listen touch
* @param[in] callback callback function
*/
void ena_interface_register_touch_callback(int touch_pad, ena_interface_touch_callback callback);
/**
* @brief get current interface state
*
* @return
* current state the interface is in
*/
int ena_interface_get_state(void);
/**
* @brief set current interface state
*
* @param[in] state new state to set
*/
void ena_interface_set_state(ena_interface_state state);
/**
* @brief start interface logic
*
* This will initialize the touch controls and start a task to listen to touch
* inputs and calling the callbacks
*/
void ena_interface_start(void);
#endif
-440
View File
@@ -1,440 +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_partition.h"
#include "esp_spi_flash.h"
#include "esp_log.h"
#include "ena-storage.h"
#include "ena-crypto.h"
#define BLOCK_SIZE (4096)
const int ENA_STORAGE_TEK_COUNT_ADDRESS = (0); // starting address for TEK COUNT
const int ENA_STORAGE_TEK_START_ADDRESS = (ENA_STORAGE_TEK_COUNT_ADDRESS + sizeof(uint32_t));
const int ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS = (ENA_STORAGE_TEK_START_ADDRESS + sizeof(ena_tek_t) * ENA_STORAGE_TEK_STORE_PERIOD);
const int ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS = (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + sizeof(uint32_t));
const int ENA_STORAGE_DETECTIONS_COUNT_ADDRESS = (ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + sizeof(ena_temp_detection_t) * ENA_STORAGE_TEMP_DETECTIONS_MAX);
const int ENA_STORAGE_DETECTIONS_START_ADDRESS = (ENA_STORAGE_DETECTIONS_COUNT_ADDRESS + sizeof(uint32_t));
void ena_storage_read(size_t address, void *data, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read");
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, 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);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read");
}
void ena_storage_write(size_t address, void *data, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write");
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, 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);
}
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write");
}
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_shift_delete");
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, 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);
}
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_shift_delete");
}
uint32_t ena_storage_read_last_tek(ena_tek_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_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_STORE_PERIOD) - 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);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_tek");
return tek_count;
}
void ena_storage_write_tek(ena_tek_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_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_STORE_PERIOD);
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);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_tek");
}
uint32_t ena_storage_temp_detections_count(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_temp_detections_count");
uint32_t count = 0;
ena_storage_read(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "read temp contancts count: %u", count);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_temp_detections_count");
return count;
}
void ena_storage_get_temp_detection(uint32_t index, ena_temp_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_temp_detection");
ena_storage_read(ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * sizeof(ena_temp_detection_t), detection, sizeof(ena_temp_detection_t));
ESP_LOGD(ENA_STORAGE_LOG, "read temp detection: first %u, last %u and rssi %d", detection->timestamp_first, detection->timestamp_last, detection->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_temp_detection");
}
uint32_t ena_storage_add_temp_detection(ena_temp_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_add_temp_detection");
uint32_t count = ena_storage_temp_detections_count();
// overwrite older temporary detections?!
uint8_t index = count % ENA_STORAGE_TEMP_DETECTIONS_MAX;
ena_storage_set_temp_detection(index, detection);
ESP_LOGD(ENA_STORAGE_LOG, "add temp detection at %u: first %u, last %u and rssi %d", index, detection->timestamp_first, detection->timestamp_last, detection->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
count++;
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_add_temp_detection");
return count - 1;
}
void ena_storage_set_temp_detection(uint32_t index, ena_temp_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_set_temp_detection");
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * sizeof(ena_temp_detection_t), detection, sizeof(ena_temp_detection_t));
ESP_LOGD(ENA_STORAGE_LOG, "set temp detection at %u: first %u, last %u and rssi %d", index, detection->timestamp_first, detection->timestamp_last, detection->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_set_temp_detection");
}
void ena_storage_remove_temp_detection(uint32_t index)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_remove_temp_detection");
uint32_t count = ena_storage_temp_detections_count();
size_t address_from = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * sizeof(ena_temp_detection_t);
size_t address_to = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + count * sizeof(ena_temp_detection_t);
ena_storage_shift_delete(address_from, address_to, sizeof(ena_temp_detection_t));
count--;
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "remove temp detection: %u", index);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_remove_temp_detection");
}
uint32_t ena_storage_detections_count(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_detections_count");
uint32_t count = 0;
ena_storage_read(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "read contancts count: %u", count);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_detections_count");
return count;
}
void ena_storage_get_detection(uint32_t index, ena_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_detection");
ena_storage_read(ENA_STORAGE_DETECTIONS_START_ADDRESS + index * sizeof(ena_detection_t), detection, sizeof(ena_detection_t));
ESP_LOGD(ENA_STORAGE_LOG, "read detection: timestamp %u and rssi %d", detection->timestamp, detection->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_detection");
}
void ena_storage_add_detection(ena_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_detection");
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
uint32_t count = ena_storage_detections_count();
ena_storage_write(ENA_STORAGE_DETECTIONS_START_ADDRESS + count * sizeof(ena_detection_t), detection, sizeof(ena_detection_t));
count++;
ena_storage_write(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "write detection: timestamp %u and rssi %d", detection->timestamp, detection->rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, detection->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_detection");
}
void ena_storage_erase(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase");
const esp_partition_t *partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARTITION_NAME);
assert(partition);
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
ESP_LOGI(ENA_STORAGE_LOG, "erased partition %s!", PARTITION_NAME);
uint32_t count = 0;
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ena_storage_write(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase");
}
void ena_storage_erase_tek(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_teks");
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
uint8_t stored = ENA_STORAGE_TEK_STORE_PERIOD;
if (tek_count < ENA_STORAGE_TEK_STORE_PERIOD)
{
stored = tek_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);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_teks");
}
void ena_storage_erase_temporary_detection(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_temporary_detections");
uint32_t detection_count = 0;
ena_storage_read(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &detection_count, sizeof(uint32_t));
uint32_t stored = ENA_STORAGE_TEMP_DETECTIONS_MAX;
if (detection_count < ENA_STORAGE_TEMP_DETECTIONS_MAX)
{
stored = detection_count;
}
size_t size = sizeof(uint32_t) + stored * sizeof(ena_temp_detection_t);
uint8_t *zeros = calloc(size, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, zeros, size);
free(zeros);
ESP_LOGI(ENA_STORAGE_LOG, "erased %d temporary detections (size %u at %u)", stored, size, ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_temporary_detections");
}
void ena_storage_erase_detection(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_detection");
uint32_t detection_count = 0;
ena_storage_read(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, &detection_count, sizeof(uint32_t));
size_t size = sizeof(uint32_t) + detection_count * sizeof(ena_detection_t);
uint8_t *zeros = calloc(size, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, zeros, size);
free(zeros);
ESP_LOGI(ENA_STORAGE_LOG, "erased %d detections (size %u at %u)", detection_count, size, ENA_STORAGE_DETECTIONS_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_detection");
}
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_tek(void)
{
ena_tek_t tek;
uint32_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
uint8_t stored = ENA_STORAGE_TEK_STORE_PERIOD;
if (tek_count < ENA_STORAGE_TEK_STORE_PERIOD)
{
stored = tek_count;
}
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs (%u stored)\n", tek_count, stored);
printf("#,enin,tek\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("\n");
}
}
void ena_storage_dump_temp_detections(void)
{
ena_temp_detection_t detection;
uint32_t detection_count = 0;
ena_storage_read(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &detection_count, sizeof(uint32_t));
uint32_t stored = ENA_STORAGE_TEMP_DETECTIONS_MAX;
if (detection_count < ENA_STORAGE_TEMP_DETECTIONS_MAX)
{
stored = detection_count;
}
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary detections (%u stored)\n", detection_count, stored);
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
for (int i = 0; i < stored; i++)
{
ena_storage_get_temp_detection(i, &detection);
printf("%d,%u,%u,", i, detection.timestamp_first, detection.timestamp_last);
ena_storage_dump_hash_array(detection.rpi, ENA_KEY_LENGTH);
printf(",");
ena_storage_dump_hash_array(detection.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", detection.rssi);
}
}
void ena_storage_dump_detections(void)
{
ena_detection_t detection;
uint32_t detection_count = 0;
ena_storage_read(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, &detection_count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "%u detections\n", detection_count);
printf("#,timestamp,rpi,aem,rssi\n");
for (int i = 0; i < detection_count; i++)
{
ena_storage_get_detection(i, &detection);
printf("%d,%u,", i, detection.timestamp);
ena_storage_dump_hash_array(detection.rpi, ENA_KEY_LENGTH);
printf(",");
ena_storage_dump_hash_array(detection.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", detection.rssi);
}
}
-189
View File
@@ -1,189 +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.
#ifndef _ena_STORAGE_H_
#define _ena_STORAGE_H_
#include "ena-crypto.h"
#include "ena-datastructures.h"
#define ENA_STORAGE_LOG "ESP-ENA-storage" // TAG for Logging
#define PARTITION_NAME "ena" // name of partition to use for storing
#define ENA_STORAGE_TEK_STORE_PERIOD (14) // Period of storing TEKs // length of a stored detection -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
#define ENA_STORAGE_TEMP_DETECTIONS_MAX (1000) // Maximum number of temporary stored detections
/**
* @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 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 temporary detections
*
* @return
* total number of temporary detections stored
*/
uint32_t ena_storage_temp_detections_count(void);
/**
* @brief get temporary detection at given index
*
* @param[in] index the index of the temporary detection to read
* @param[out] detection pointer to temporary to write to
*/
void ena_storage_get_temp_detection(uint32_t index, ena_temp_detection_t *detection);
/**
* @brief store temporary detection
*
* @param[in] detection new temporary detection to store
*
* @return
* index of new stored detection
*/
uint32_t ena_storage_add_temp_detection(ena_temp_detection_t *detection);
/**
* @brief store temporary detection at given index
*
* @param[in] index the index of the temporary detection to overwrite
* @param[in] detection temporary detection to store
*/
void ena_storage_set_temp_detection(uint32_t index, ena_temp_detection_t *detection);
/**
* @brief remove temporary detection at given index
*
* @param[in] index the index of the temporary detection to remove
*/
void ena_storage_remove_temp_detection(uint32_t index);
/**
* @brief get number of stored detections
*
* @return
* total number of detections stored
*/
uint32_t ena_storage_detections_count(void);
/**
* @brief get detection at given index
*
* @param[in] index the index of the detection to read
* @param[out] detection pointer to to write to
*/
void ena_storage_get_detection(uint32_t index, ena_detection_t *detection);
/**
* @brief store detection
*
* @param[in] detection new detection to store
*/
void ena_storage_add_detection(ena_detection_t *detection);
/**
* @brief erase the storage
*
* This function completely deletes all stored data and resets the counters
* of TEKs, temporary detection and detection to zero.
*/
void ena_storage_erase(void);
/**
* @brief erase stored TEKs
*
* This function deletes all stored TEKs and resets counter to zero.
*/
void ena_storage_erase_tek(void);
/**
* @brief erase stored temporary detections
*
* This function deletes all stored temporary detections and resets counter to zero.
*/
void ena_storage_erase_temporary_detection(void);
/**
* @brief erase stored detections
*
* This function deletes all stored detections and resets counter to zero.
*/
void ena_storage_erase_detection(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_tek(void);
/**
* @brief dump all stored temporary detections to serial output
*
* This function prints all stored temporary detections to serial output in
* the following CSV format: #,timestamp_first,timestamp_last,rpi,aem,rssi
*/
void ena_storage_dump_temp_detections(void);
/**
* @brief dump all stored detections to serial output
*
* This function prints all stored detections to serial output in
* the following CSV format: #,timestamp,rpi,aem,rssi
*/
void ena_storage_dump_detections(void);
#endif
-169
View File
@@ -1,169 +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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.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-datastructures.h"
#include "ena-crypto.h"
#include "ena-storage.h"
#include "ena-bluetooth-scan.h"
#include "ena-bluetooth-advertise.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_RPI_ROLLING_RANDOM_INTERVAL);
if (random_interval > ENA_RPI_ROLLING_RANDOM_INTERVAL)
{
random_interval = ENA_RPI_ROLLING_RANDOM_INTERVAL - random_interval;
}
next_rpi_timestamp = timestamp + ENA_RPI_ROLLING_PERIOD + random_interval;
ESP_LOGD(ENA_LOG, "next rpi at %u (%u from %u)", next_rpi_timestamp, (ENA_RPI_ROLLING_PERIOD + random_interval), timestamp);
}
void ena_run(void *pvParameter)
{
uint32_t unix_timestamp = 0;
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 >= ENA_TEK_ROLLING_PERIOD)
{
ena_crypto_tek(last_tek.key_data);
last_tek.enin = current_enin;
ena_storage_write_tek(&last_tek);
}
// 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);
}
// one second loop correct?!
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void ena_start(void)
{
// 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 current_enin = ena_crypto_enin((uint32_t)time(NULL));
uint32_t tek_count = ena_storage_read_last_tek(&last_tek);
ena_next_rpi_timestamp((uint32_t)time(NULL));
// read last TEK or create new
if (tek_count == 0 || (current_enin - last_tek.enin) >= ENA_TEK_ROLLING_PERIOD)
{
ena_crypto_tek(last_tek.key_data);
last_tek.enin = ena_crypto_enin((uint32_t)time(NULL));
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", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL);
}
-32
View File
@@ -1,32 +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.
#ifndef _ena_H_
#define _ena_H_
#define ENA_LOG "ESP-ENA" // TAG for Logging
#define ENA_RPI_ROLLING_PERIOD (900) // change RPI every 15 minutes
#define ENA_RPI_ROLLING_RANDOM_INTERVAL (150) // random intervall change of rpi +/- ~2.5 minutes
/**
* @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);
#endif
+15 -3
View File
@@ -21,21 +21,33 @@
#include "esp_log.h"
#include "ena.h"
#include "ena-detection.h"
#include "ena-storage.h"
#include "ena-interface.h"
#include "ena-interface-menu.h"
#include "ssd1306.h"
#include "sdkconfig.h"
void app_main(void)
{
// DEBUG set time
struct timeval tv = {1594459800, 0}; // current hardcoded timestamp (2020-07-11 09:30:00) ¯\_(ツ)_/¯
struct timeval tv = {1594843200, 0}; // current hardcoded timestamp ¯\_(ツ)_/¯
settimeofday(&tv, NULL);
esp_log_level_set(ENA_STORAGE_LOG, ESP_LOG_INFO);
// ena_storage_erase(); // only needed on first start! TODO automatically check (how?)
ssd1306_start(SSD1306_ADDRESS);
ssd1306_clear(SSD1306_ADDRESS);
// TODO
ssd1306_text_line(SSD1306_ADDRESS, " TODO TODO", 0, false);
ssd1306_text_line(SSD1306_ADDRESS, " TODO", 1, true);
ssd1306_text_line(SSD1306_ADDRESS, " TODO TODO", 2, false);
ssd1306_text_line(SSD1306_ADDRESS, " TODO", 3, true);
ssd1306_text_line(SSD1306_ADDRESS, " TODO TODO", 4, false);
ssd1306_text_line(SSD1306_ADDRESS, " TODO", 5, true);
ssd1306_text_line(SSD1306_ADDRESS, " TODO TODO", 6, false);
ssd1306_text_line(SSD1306_ADDRESS, " TODO", 7, true);
ena_interface_start();
ena_interface_menu_start();