added structrues for stored data, start with first menus

This commit is contained in:
Lurkars 2020-07-13 20:32:53 +02:00
parent 30be89fedb
commit 5b1c6e2c76
19 changed files with 421 additions and 356 deletions

View File

@ -1,4 +1,4 @@
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"
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"
INCLUDE_DIRS ""
)

View File

@ -64,7 +64,7 @@ void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek)
esp_ble_gap_config_adv_data_raw(adv_raw_data, sizeof(adv_raw_data));
ESP_LOGI(ENA_ADVERTISE_LOG, "payload for ENIN %u", enin);
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);
}

View File

@ -17,7 +17,7 @@
#include "esp_gap_ble_api.h"
#define ENA_ADVERTISE_LOG "ESP-ENA-advertise" // TAG for Logging
#define ENA_BLUETOOTH_TAG_DATA 0x1A
#define ENA_BLUETOOTH_TAG_DATA (0x1A)
void ena_bluetooth_advertise_start(void);

View File

@ -26,10 +26,10 @@ void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap
switch (event)
{
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
ESP_LOGI(ENA_SCAN_LOG, "start scanning...");
ESP_LOGD(ENA_SCAN_LOG, "start scanning...");
break;
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
ESP_LOGI(ENA_SCAN_LOG, "stopped scanning...");
ESP_LOGD(ENA_SCAN_LOG, "stopped scanning...");
ena_detections_temp_refresh((uint32_t)time(NULL));
break;
case ESP_GAP_BLE_SCAN_RESULT_EVT:
@ -60,7 +60,7 @@ void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap
{
scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
ena_detections_temp_refresh((uint32_t)time(NULL));
ESP_LOGI(ENA_SCAN_LOG, "finished scanning...");
ESP_LOGD(ENA_SCAN_LOG, "finished scanning...");
}
break;
default:

View File

@ -16,8 +16,8 @@
#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
#define ENA_SCANNING_TIME (30) // scan for 30 seconds
#define ENA_SCANNING_INTERVAL (300) // scan every 5 minutes
#include "esp_gap_ble_api.h"

View File

@ -15,10 +15,10 @@
#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
#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>

65
main/ena-datastructures.h Normal file
View File

@ -0,0 +1,65 @@
/**
* provide data structure models of Exposure Notification API
*
*
*/
#ifndef _ena_DATASTRUCTURES_H_
#define _ena_DATASTRUCTURES_H_
#include <stdio.h>
#include "ena-crypto.h"
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;
// maybe used later
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;
typedef struct
{
uint8_t key_data[ENA_KEY_LENGTH];
uint32_t enin;
uint8_t rolling_period;
} __packed ena_tek_t;
typedef struct
{
uint8_t rpi[ENA_KEY_LENGTH];
uint8_t aem[ENA_AEM_METADATA_LENGTH];
uint32_t timestamp_first;
uint32_t timestamp_last;
int rssi;
} __packed ena_temp_detection_t;
typedef struct
{
uint8_t rpi[ENA_KEY_LENGTH];
uint8_t aem[ENA_AEM_METADATA_LENGTH];
uint32_t timestamp;
int rssi;
} __packed ena_detection_t;
#endif

View File

@ -1,24 +1,32 @@
#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 uint8_t temp_detection_rpi[ENA_STOARGE_TEMP_DETECTIONS_MAX][ENA_KEY_LENGTH] = {{0}};
static uint8_t temp_detection_aem[ENA_STOARGE_TEMP_DETECTIONS_MAX][ENA_AEM_METADATA_LENGTH] = {{0}};
static uint32_t temp_detection_timestamp_first[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
static uint32_t temp_detection_timestamp_last[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
static int temp_detection_rssi_last[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
static ena_temp_detection_t temp_detections[ENA_STOARGE_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_detection_rpi[i], rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
memcmp(temp_detection_aem[i], aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
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;
}
@ -31,17 +39,17 @@ void ena_detections_temp_refresh(uint32_t unix_timestamp)
for (int i = 0; i < temp_detections_count; i++)
{
// check for treshold and add permanent detection
if (temp_detection_timestamp_last[i] - temp_detection_timestamp_first[i] >= ENA_DETECTION_TRESHOLD)
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_STORAGE_LOG, temp_detection_rpi[i], ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ena_storage_write_detection(ena_crypto_enin(temp_detection_timestamp_first[i]), temp_detection_rpi[i],
temp_detection_aem[i], temp_detection_rssi_last[i]);
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_write_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_detection_timestamp_first[i] > (ENA_TIME_WINDOW * 2))
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);
@ -52,8 +60,7 @@ void ena_detections_temp_refresh(uint32_t unix_timestamp)
temp_detections_count = ena_storage_temp_detections_count();
for (int i = 0; i < temp_detections_count; i++)
{
ena_storage_read_temp_detection(i, &temp_detection_timestamp_first[i], temp_detection_rpi[i], temp_detection_aem[i], &temp_detection_rssi_last[i]);
temp_detection_timestamp_last[i] = temp_detection_timestamp_first[i];
ena_storage_read_temp_detection(i, &temp_detections[i]);
}
// DEBUG dump
@ -68,12 +75,12 @@ void ena_detection(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi
if (detection_index == -1)
{
temp_detection_timestamp_first[temp_detections_count] = unix_timestamp;
memcpy(temp_detection_rpi[temp_detections_count], rpi, ENA_KEY_LENGTH);
memcpy(temp_detection_aem[temp_detections_count], aem, ENA_AEM_METADATA_LENGTH);
temp_detection_rssi_last[temp_detections_count] = rssi;
temp_detection_timestamp_last[temp_detections_count] = unix_timestamp;
detection_index = ena_storage_write_temp_detection(unix_timestamp, rpi, aem, rssi);
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_write_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);
@ -88,8 +95,8 @@ void ena_detection(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi
}
else
{
temp_detection_rssi_last[detection_index] = rssi;
temp_detection_timestamp_last[detection_index] = unix_timestamp;
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);
}
}

View File

@ -8,7 +8,7 @@
#define ENA_DETECTION_LOG "ESP-ENA-detection" // TAG for Logging
#define ENA_DETECTION_TRESHOLD 300 // meet for longer than 5 minutes
#define ENA_DETECTION_TRESHOLD (300) // meet for longer than 5 minutes
#include "ena-crypto.h"

View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include "esp_log.h"
#include "ena-interface.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;
}

View File

@ -0,0 +1,18 @@
#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

52
main/ena-interface-menu.c Normal file
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include "esp_log.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;
}

15
main/ena-interface-menu.h Normal file
View File

@ -0,0 +1,15 @@
#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

View File

@ -8,7 +8,7 @@
#include "ena-interface.h"
static int interface_state = ENA_INTERFACE_STATE_MENU;
static int interface_state = ENA_INTERFACE_STATE_IDLE;
static bool touch_status[TOUCH_PAD_MAX] = {0};
static ena_interface_touch_callback touch_callbacks[TOUCH_PAD_MAX];
@ -16,7 +16,7 @@ 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)
{
@ -51,7 +51,7 @@ void ena_interface_start(void)
ESP_ERROR_CHECK(touch_pad_config(TOUCH_PAD_OK, TOUCHPAD_TOUCH_THRESHOLD));
ESP_ERROR_CHECK(touch_pad_config(TOUCH_PAD_UP, TOUCHPAD_TOUCH_THRESHOLD));
ESP_ERROR_CHECK(touch_pad_config(TOUCH_PAD_DOWN, TOUCHPAD_TOUCH_THRESHOLD));
xTaskCreate(&ena_interface_run, "ena_interface_run", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
xTaskCreate(&ena_interface_run, "ena_interface_run", configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
}
int ena_interface_get_state(void)
@ -59,7 +59,7 @@ int ena_interface_get_state(void)
return interface_state;
}
void ena_interface_set_state(ena_inerface_state state)
void ena_interface_set_state(ena_interface_state state)
{
interface_state = state;
}

View File

@ -28,7 +28,7 @@ typedef enum
ENA_INTERFACE_STATE_SET_MINUTE,
ENA_INTERFACE_STATE_SET_SECONDS,
ENA_INTERFACE_STATE_STATUS,
} ena_inerface_state;
} ena_interface_state;
typedef void (*ena_interface_touch_callback)(void);
@ -36,7 +36,7 @@ void ena_interface_register_touch_callback(int touch_pad, ena_interface_touch_ca
int ena_interface_get_state(void);
void ena_interface_set_state(ena_inerface_state state);
void ena_interface_set_state(ena_interface_state state);
void ena_interface_start(void);

View File

@ -8,9 +8,16 @@
#include "ena-storage.h"
#include "ena-crypto.h"
#define BLOCK_SIZE 4096
#define BLOCK_SIZE (4096)
void ena_storage_read(size_t address, uint8_t *data, size_t size)
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(uint8_t));
const int ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS = (ENA_STORAGE_TEK_START_ADDRESS + sizeof(ena_tek_t) * ENA_STOARGE_TEK_STORE_PERIOD); // starting address for temporary detections COUNT (offset from max. stored TEKs)
const int ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS = (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + sizeof(uint32_t)); // starting address for temporary detections
const int ENA_STORAGE_DETECTIONS_COUNT_ADDRESS = (ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + sizeof(ena_temp_detection_t) * ENA_STOARGE_TEMP_DETECTIONS_MAX); // starting address for detections COUNT (offset from max. stored temporary detections)
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(
@ -23,41 +30,19 @@ void ena_storage_read(size_t address, uint8_t *data, size_t size)
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read");
}
void ena_storage_write(size_t address, uint8_t *data, size_t size)
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_num = address / BLOCK_SIZE;
// split if size extends block
if (address + size > (block_num + 1) * BLOCK_SIZE)
{
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num);
const size_t block1_address = address;
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)", block1_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);
uint8_t *data1 = malloc(data1_size);
memcpy(data1, data, data1_size);
ena_storage_write(block1_address, data1, data1_size);
free(data1);
uint8_t *data2 = malloc(data2_size);
memcpy(data2, &data[data1_size], data2_size);
ena_storage_write(block2_address, data2, data2_size);
free(data2);
}
else
{
const int block_start = block_num * BLOCK_SIZE;
const int block_address = address - block_start;
uint8_t *buffer = malloc(BLOCK_SIZE);
void *buffer = malloc(BLOCK_SIZE);
if (buffer == NULL)
{
ESP_LOGE(ENA_STORAGE_LOG, "Warning %s malloc low memory", "buffer");
@ -68,13 +53,32 @@ void ena_storage_write(size_t address, uint8_t *data, size_t size)
vTaskDelay(1);
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_start, BLOCK_SIZE));
memcpy(&buffer[block_address], data, 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 block1_address = address;
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)", block1_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(block1_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");
}
@ -83,18 +87,8 @@ 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;
// split if size extends block
if (address + size > (block_num_start + 1) * BLOCK_SIZE)
{
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, end_address, data1_size);
ena_storage_shift_delete(block2_address, end_address - data1_size, data2_size);
}
else
// 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);
@ -105,28 +99,28 @@ void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
while (block_num_end >= block_num_start)
{
uint8_t *buffer = malloc(BLOCK_SIZE);
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);
memcpy((buffer + block_start), (buffer + block_start + size), BLOCK_SIZE - block_start - size);
if (block_num_end > block_num_start)
{
uint8_t *buffer_next_block = malloc(BLOCK_SIZE);
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[0], size);
memcpy((buffer + BLOCK_SIZE - size), buffer_next_block, size);
free(buffer_next_block);
}
else
{
// fill end with zeros
ESP_LOGD(ENA_STORAGE_LOG, "fill with zeros %u", size);
memset(&buffer[BLOCK_SIZE - size], 0, size);
memset((buffer + BLOCK_SIZE - size), 0, size);
}
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_num_start * BLOCK_SIZE, BLOCK_SIZE));
@ -137,129 +131,112 @@ void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
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, end_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");
}
void ena_storage_write_tek(uint32_t enin, uint8_t *tek)
uint8_t ena_storage_read_last_tek(ena_tek_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_tek");
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count * ENA_STORAGE_TEK_LENGTH);
ena_storage_write_u32(address, enin);
ena_storage_write(address + 4, tek, ENA_KEY_LENGTH);
tek_count++;
if (tek_count > ENA_STOARGE_TEK_STORE_PERIOD)
{
tek_count = 0;
}
ena_storage_write_u8(ENA_STORAGE_TEK_COUNT_ADDRESS, tek_count);
ESP_LOGD(ENA_STORAGE_LOG, "write tek: ENIN %u", enin);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_tek");
}
uint32_t ena_storage_read_enin(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_enin");
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_tek");
uint8_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint8_t));
if (tek_count < 1)
{
return 0;
}
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count - 1) * ENA_STORAGE_TEK_LENGTH;
uint32_t result = ena_storage_read_u32(address);
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count - 1) * sizeof(ena_tek_t);
ESP_LOGD(ENA_STORAGE_LOG, "read last ENIN: %u", result);
ena_storage_read(address, tek, sizeof(ena_tek_t));
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_enin");
return result;
}
void ena_storage_read_tek(uint8_t *tek)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_tek");
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
if (tek_count < 1)
{
return;
}
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count - 1) * ENA_STORAGE_TEK_LENGTH + 4;
ena_storage_read(address, tek, ENA_KEY_LENGTH);
ESP_LOGD(ENA_STORAGE_LOG, "read last tek:");
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
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");
uint8_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEK_START_ADDRESS + tek_count * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
tek_count++;
if (tek_count > ENA_STOARGE_TEK_STORE_PERIOD)
{
tek_count = 1;
}
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint8_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 = ena_storage_read_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS);
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;
}
uint32_t ena_storage_write_temp_detection(uint32_t timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
void ena_storage_read_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_write_temp_detection(ena_temp_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_temp_detection");
uint32_t count = ena_storage_temp_detections_count() + 1;
uint32_t count = ena_storage_temp_detections_count();
// start overwriting temporay detections?!
if (count > ENA_STOARGE_TEMP_DETECTIONS_MAX)
{
count = 1;
count = 0;
}
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + (count - 1) * ENA_STORAGE_DETECTION_LENGTH;
ena_storage_write_u32(address, timestamp);
address += 4;
ena_storage_write(address, rpi, ENA_KEY_LENGTH);
address += ENA_KEY_LENGTH;
ena_storage_write(address, aem, ENA_AEM_METADATA_LENGTH);
address += ENA_AEM_METADATA_LENGTH;
ena_storage_write_int(address, rssi);
ena_storage_write_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, count);
ESP_LOGD(ENA_STORAGE_LOG, "write temp detection: timestamp %u and rssi %d", timestamp, rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + count * sizeof(ena_temp_detection_t), detection, sizeof(ena_temp_detection_t));
count++;
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "write 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_write_temp_detection");
return count - 1;
}
void ena_storage_read_temp_detection(uint32_t index, uint32_t *timestamp, uint8_t *rpi, uint8_t *aem, int *rssi)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_temp_detection");
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
*timestamp = ena_storage_read_u32(address);
address += 4;
ena_storage_read(address, rpi, ENA_KEY_LENGTH);
address += ENA_KEY_LENGTH;
ena_storage_read(address, aem, ENA_AEM_METADATA_LENGTH);
address += 4;
*rssi = ena_storage_read_int(address);
ESP_LOGD(ENA_STORAGE_LOG, "read temp detection: timestamp %u and rssi %d", *timestamp, *rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_temp_detection");
}
void ena_storage_remove_temp_detection(uint32_t index)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_remove_temp_detection");
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
uint32_t count = ena_storage_temp_detections_count();
ena_storage_shift_delete(address, ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + count * ENA_STORAGE_DETECTION_LENGTH, ENA_STORAGE_DETECTION_LENGTH);
size_t address_from = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * sizeof(ena_detection_t);
size_t address_to = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + count * sizeof(ena_detection_t);
ena_storage_shift_delete(address_from, address_to, sizeof(ena_detection_t));
count--;
ena_storage_write_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, 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");
}
@ -267,99 +244,35 @@ void ena_storage_remove_temp_detection(uint32_t index)
uint32_t ena_storage_detections_count(void)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_detections_count");
uint32_t count = ena_storage_read_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS);
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_write_detection(uint32_t timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_detection");
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
uint32_t count = ena_storage_detections_count() + 1;
size_t address = ENA_STORAGE_DETECTIONS_START_ADDRESS + (count - 1) * ENA_STORAGE_DETECTION_LENGTH;
ena_storage_write_u32(address, timestamp);
address += 4;
ena_storage_write(address, rpi, ENA_KEY_LENGTH);
address += ENA_KEY_LENGTH;
ena_storage_write(address, aem, ENA_AEM_METADATA_LENGTH);
address += ENA_AEM_METADATA_LENGTH;
ena_storage_write_int(address, rssi);
ena_storage_write_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, count);
ESP_LOGD(ENA_STORAGE_LOG, "write detection: timestamp %u and rssi %d", timestamp, rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_detection");
}
void ena_storage_read_detection(uint32_t index, uint32_t *timestamp, uint8_t *rpi, uint8_t *aem, int *rssi)
void ena_storage_read_detection(uint32_t index, ena_detection_t *detection)
{
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_detection");
size_t address = ENA_STORAGE_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
*timestamp = ena_storage_read_u32(address);
address += 4;
ena_storage_read(address, rpi, ENA_KEY_LENGTH);
address += ENA_KEY_LENGTH;
ena_storage_read(address, aem, ENA_AEM_METADATA_LENGTH);
address += 4;
*rssi = ena_storage_read_int(address);
ESP_LOGD(ENA_STORAGE_LOG, "read detection: timestamp %u and rssi %d", *timestamp, *rssi);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
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");
}
uint8_t ena_storage_read_u8(size_t address)
void ena_storage_write_detection(ena_detection_t *detection)
{
uint8_t data[1] = {0};
ena_storage_read(address, (uint8_t *)&data, 1);
return data[0];
}
uint32_t ena_storage_read_u32(size_t address)
{
uint8_t data[4] = {0};
ena_storage_read(address, (uint8_t *)&data, 4);
uint32_t result = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
return result;
}
void ena_storage_write_u8(size_t address, uint8_t byte)
{
uint8_t data[1] = {byte};
ena_storage_write(address, data, 1);
}
void ena_storage_write_u32(size_t address, uint32_t value)
{
uint8_t *data = malloc(4);
data[0] = (value & 0x000000ff);
data[1] = (value & 0x0000ff00) >> 8;
data[2] = (value & 0x00ff0000) >> 16;
data[3] = (value & 0xff000000) >> 24;
ena_storage_write(address, data, 4);
free(data);
}
int ena_storage_read_int(size_t address)
{
uint8_t data[sizeof(int)] = {0};
ena_storage_read(address, (uint8_t *)&data, sizeof(int));
int result = 0;
memcpy((int *)&result, (int *)&data, sizeof(int));
return result;
}
void ena_storage_write_int(size_t address, int value)
{
uint8_t data[sizeof(int)] = {0};
memcpy((int *)&data, (int *)&value, sizeof(int));
ena_storage_write(address, data, sizeof(int));
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)
@ -371,14 +284,11 @@ void ena_storage_erase(void)
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
ESP_LOGI(PARTITION_NAME, "erase partition!");
uint8_t *tek_zeros = calloc(ENA_STORAGE_TEK_LENGTH + 1, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, tek_zeros, ENA_STORAGE_TEK_LENGTH + 1);
uint32_t count = 0;
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint8_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));
uint8_t *temp_detection_zeros = calloc(ENA_STORAGE_DETECTION_LENGTH + 4, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, temp_detection_zeros, ENA_STORAGE_DETECTION_LENGTH + 4);
uint8_t *detection_zeros = calloc(ENA_STORAGE_DETECTION_LENGTH + 4, sizeof(uint8_t));
ena_storage_write(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, detection_zeros, ENA_STORAGE_DETECTION_LENGTH + 4);
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase");
}
@ -388,70 +298,66 @@ void ena_storage_dump_hash_array(uint8_t *data, size_t size)
{
if (i == 0)
{
printf("%0x", data[i]);
printf("%02x", data[i]);
}
else
{
printf(" %0x", data[i]);
printf(" %02x", data[i]);
}
}
}
void ena_storage_dump_tek(void)
{
uint32_t timestamp;
uint8_t tek[ENA_KEY_LENGTH] = {0};
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
ena_tek_t tek;
uint8_t tek_count = 0;
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint8_t));
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs\n", tek_count);
printf("#,enin,tek\n");
for (int i = 0; i < tek_count; i++)
{
size_t address = ENA_STORAGE_TEK_START_ADDRESS + i * ENA_STORAGE_TEK_LENGTH;
timestamp = ena_storage_read_u32(address);
ena_storage_read(address + 4, tek, ENA_KEY_LENGTH);
printf("%d,%u,", i, timestamp);
ena_storage_dump_hash_array(tek, ENA_KEY_LENGTH);
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)
{
uint32_t timestamp;
uint8_t rpi[ENA_KEY_LENGTH] = {0};
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
int rssi;
uint32_t detection_count = ena_storage_read_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS);
ena_temp_detection_t detection;
uint32_t detection_count = 0;
ena_storage_read(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, &detection_count, sizeof(uint32_t));
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary detections\n", detection_count);
printf("#,timestamp,rpi,aem,rssi\n");
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
for (int i = 0; i < detection_count; i++)
{
ena_storage_read_temp_detection(i, &timestamp, rpi, aem, &rssi);
printf("%d,%u,", i, timestamp);
ena_storage_dump_hash_array(rpi, ENA_KEY_LENGTH);
ena_storage_read_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(aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", rssi);
ena_storage_dump_hash_array(detection.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", detection.rssi);
}
}
void ena_storage_dump_detections(void)
{
uint32_t enin;
uint8_t rpi[ENA_KEY_LENGTH] = {0};
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
int rssi;
uint32_t detection_count = ena_storage_read_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS);
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("#,enin,rpi,aem,rssi\n");
printf("#,timestamp,rpi,aem,rssi\n");
for (int i = 0; i < detection_count; i++)
{
ena_storage_read_detection(i, &enin, rpi, aem, &rssi);
printf("%d,%u,", i, enin);
ena_storage_dump_hash_array(rpi, ENA_KEY_LENGTH);
ena_storage_read_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(aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", rssi);
ena_storage_dump_hash_array(detection.aem, ENA_AEM_METADATA_LENGTH);
printf(",%d\n", detection.rssi);
}
}

View File

@ -2,66 +2,56 @@
#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"
#define ENA_STOARGE_TEK_STORE_PERIOD (14) // Period of storing TEKs
#define ENA_STORAGE_TEK_COUNT_ADDRESS (0) // starting address for TEK COUNT
#define ENA_STORAGE_TEK_START_ADDRESS (ENA_STORAGE_TEK_COUNT_ADDRESS + 1) // starting address for TEKs
#define ENA_STORAGE_TEK_LENGTH (ENA_KEY_LENGTH + 4) // length of a stored TEK -> TEK keysize + 4 Bytes for ENIN
#define ENA_STORAGE_DETECTION_LENGTH (ENA_KEY_LENGTH + ENA_AEM_METADATA_LENGTH + 4 + sizeof(int)) // length of a stored detection -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
#define ENA_STOARGE_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_STOARGE_TEMP_DETECTIONS_MAX (1000) // Maximum number of temporary stored detections
#define ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS (ENA_STORAGE_TEK_START_ADDRESS + ENA_STORAGE_TEK_LENGTH * ENA_STOARGE_TEK_STORE_PERIOD) // starting address for temporary detections COUNT (offset from max. stored TEKs)
#define ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + 4) // starting address for temporary detections
#define ENA_STORAGE_DETECTIONS_COUNT_ADDRESS (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + ENA_STORAGE_DETECTION_LENGTH * ENA_STOARGE_TEMP_DETECTIONS_MAX) // starting address for detections COUNT (offset from max. stored temporary detections)
#define ENA_STORAGE_DETECTIONS_START_ADDRESS (ENA_STORAGE_DETECTIONS_COUNT_ADDRESS + 4) // starting address of detections
/**
* read bytes at given address
*/
void ena_storage_read(size_t address, uint8_t *data, size_t size);
void ena_storage_read(size_t address, void *data, size_t size);
/**
* store bytes at given address
*/
void ena_storage_write(size_t address, uint8_t *data, size_t size);
void ena_storage_write(size_t address, void *data, size_t size);
/**
* deletes bytes at given address and shift other data back
*/
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size);
/**
* store TEK with ENIN
*/
void ena_storage_write_tek(uint32_t enin, uint8_t *tek);
/**
* get last stored ENIN
*/
uint32_t ena_storage_read_enin(void);
/**
* get last stored TEK
*
* return cound
*/
void ena_storage_read_tek(uint8_t *tek);
uint8_t ena_storage_read_last_tek(ena_tek_t *tek);
/**
* store TEK
*/
void ena_storage_write_tek(ena_tek_t *tek);
/**
* get number of stored temporary detections
*/
uint32_t ena_storage_temp_detections_count(void);
/**
* get temporary detection (RPI + AEM + RSSI with UNIX timestamp) at given index
*/
void ena_storage_read_temp_detection(uint32_t index, ena_temp_detection_t *detection);
/**
* store temporary detection (RPI + AEM + RSSI with UNIX timestamp)
*
* returns index
*/
uint32_t ena_storage_write_temp_detection(uint32_t timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
/**
* get temporary detection (RPI + AEM + RSSI with UNIX timestamp) at given index
*/
void ena_storage_read_temp_detection(uint32_t index, uint32_t *timestamp, uint8_t *rpi, uint8_t *aem, int *rssi);
uint32_t ena_storage_write_temp_detection(ena_temp_detection_t *detection);
/**
* remove temporary detection at given index
@ -73,27 +63,15 @@ void ena_storage_remove_temp_detection(uint32_t index);
*/
uint32_t ena_storage_detections_count(void);
/**
* store detection (RPI + AEM + RSSI with ENIN)
*/
void ena_storage_write_detection(uint32_t timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
/**
* get detection (RPI + AEM + RSSI with ENIN) at given index
*/
void ena_storage_read_detection(uint32_t index, uint32_t *enin, uint8_t *rpi, uint8_t *aem, int *rssi);
void ena_storage_read_detection(uint32_t index, ena_detection_t *detection);
uint8_t ena_storage_read_u8(size_t address);
uint32_t ena_storage_read_u32(size_t address);
void ena_storage_write_u8(size_t address, uint8_t byte);
void ena_storage_write_u32(size_t address, uint32_t value);
int ena_storage_read_int(size_t address);
void ena_storage_write_int(size_t address, int value);
/**
* store detection (RPI + AEM + RSSI with ENIN)
*/
void ena_storage_write_detection(ena_detection_t *detection);
void ena_storage_erase(void);

View File

@ -14,6 +14,7 @@
#include "nvs_flash.h"
#include "ena-datastructures.h"
#include "ena-crypto.h"
#include "ena-storage.h"
#include "ena-bluetooth-scan.h"
@ -21,8 +22,7 @@
#include "ena.h"
static uint32_t last_enin; // last ENIN
static uint8_t tek[ENA_KEY_LENGTH] = {0}; // current TEK
static ena_tek_t last_tek; // last ENIN
void ena_run(void *pvParameter)
{
@ -32,11 +32,11 @@ void ena_run(void *pvParameter)
{
unix_timestamp = (uint32_t)time(NULL);
current_enin = ena_crypto_enin(unix_timestamp);
if (current_enin - last_enin >= ENA_TEK_ROLLING_PERIOD)
if (current_enin - last_tek.enin >= ENA_TEK_ROLLING_PERIOD)
{
ena_crypto_tek(tek);
ena_storage_write_tek(current_enin, tek);
last_enin = current_enin;
ena_crypto_tek(last_tek.key_data);
last_tek.enin = current_enin;
ena_storage_write_tek(&last_tek);
}
// change RPI
@ -47,7 +47,7 @@ void ena_run(void *pvParameter)
ena_bluetooth_scan_stop();
}
ena_bluetooth_advertise_stop();
ena_bluetooth_advertise_set_payload(current_enin, tek);
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)
{
@ -118,29 +118,25 @@ void ena_start(void)
uint32_t current_enin = ena_crypto_enin((uint32_t)time(NULL));
last_enin = ena_storage_read_enin();
uint8_t tek_count = ena_storage_read_last_tek(&last_tek);
// read last TEK or create new
if (ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS) > 0 && (current_enin - last_enin) < ENA_TEK_ROLLING_PERIOD)
if (tek_count == 0 || (current_enin - last_tek.enin) >= ENA_TEK_ROLLING_PERIOD)
{
ena_storage_read_tek(tek);
}
else
{
ena_crypto_tek(tek);
ena_storage_write_tek(ena_crypto_enin((uint32_t)time(NULL)), tek);
last_enin = ena_storage_read_enin();
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, tek);
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 * 4, NULL, 5, NULL);
xTaskCreate(&ena_run, "ena_run", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL);
}

View File

@ -22,6 +22,7 @@
#include "ena.h"
#include "ena-storage.h"
#include "ena-interface.h"
#include "ena-interface-menu.h"
#include "sdkconfig.h"
@ -32,8 +33,9 @@ void app_main(void)
settimeofday(&tv, NULL);
esp_log_level_set(ENA_STORAGE_LOG, ESP_LOG_INFO);
ena_storage_erase(); // only needed on first start! TODO automatically check
// ena_storage_erase(); // only needed on first start! TODO automatically check
ena_interface_start();
ena_interface_menu_start();
ena_start();
}