mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
add/update interface
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"ena-cwa.c"
|
||||
INCLUDE_DIRS "include"
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES
|
||||
esp_http_client
|
||||
miniz
|
||||
ena
|
||||
wifi-controller
|
||||
EMBED_FILES
|
||||
"certs/telekom.pem"
|
||||
)
|
||||
@@ -12,15 +12,22 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <string.h>
|
||||
|
||||
#include "time.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "miniz.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "ena-storage.h"
|
||||
#include "ena-exposure.h"
|
||||
#include "ena-cwa.h"
|
||||
#include "wifi-controller.h"
|
||||
|
||||
extern uint8_t export_bin_start[] asm("_binary_export_bin_start"); // test data from Google or https://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE/date/2020-07-22
|
||||
extern uint8_t export_bin_end[] asm("_binary_export_bin_end");
|
||||
|
||||
extern const uint8_t telekom_pem_start[] asm("_binary_telekom_pem_start");
|
||||
extern const uint8_t telekom__pem_end[] asm("_binary_telekom_pem_end");
|
||||
@@ -40,7 +47,7 @@ esp_err_t ena_cwa_http_event_handler(esp_http_client_event_t *evt)
|
||||
output_len = 0;
|
||||
if (output_buffer == NULL)
|
||||
{
|
||||
ESP_LOGE(ENA_CWA_LOG, "Failed to allocate memory for output buffer");
|
||||
ESP_LOGE(ENA_CWA_LOG, "Failed to allocate memory for output buffer, memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
@@ -53,62 +60,14 @@ esp_err_t ena_cwa_http_event_handler(esp_http_client_event_t *evt)
|
||||
if (output_buffer != NULL)
|
||||
{
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
|
||||
size_t zip_image_size = esp_http_client_get_content_length(evt->client);
|
||||
// const char *file_name = "export.sig";
|
||||
|
||||
/*
|
||||
mz_zip_archive zip_archive;
|
||||
mz_zip_archive_file_stat file_stat;
|
||||
mz_uint32 file_index;
|
||||
|
||||
memset(&zip_archive, 0, sizeof(zip_archive));
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
mz_zip_reader_init_mem(&zip_archive, output_buffer, sizeof(output_buffer), 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_locate_file_v2(&zip_archive, file_name, NULL, 0, &file_index);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_file_stat(&zip_archive, file_index, &file_stat);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
size_t extracted_size = file_stat.m_uncomp_size;
|
||||
|
||||
ESP_LOGD(ENA_CWA_LOG, "size of export.sig: %d", extracted_size);
|
||||
|
||||
char *file_buffer = malloc(extracted_size);
|
||||
|
||||
mz_zip_reader_extract_to_mem(&zip_archive, file_index, file_buffer, extracted_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory: %d kB", (xPortGetFreeHeapSize() / 1024));
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_CWA_LOG, file_buffer, extracted_size, ESP_LOG_DEBUG);
|
||||
|
||||
free(file_buffer);
|
||||
*/
|
||||
|
||||
mz_zip_archive zip_archive;
|
||||
ESP_LOGD(ENA_CWA_LOG, "1 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
memset(&zip_archive, 0, sizeof(zip_archive));
|
||||
ESP_LOGD(ENA_CWA_LOG, "2 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
mz_zip_reader_init_mem(&zip_archive, output_buffer, zip_image_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "3 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
|
||||
ESP_LOGD(ENA_CWA_LOG, "4 memory: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
/*
|
||||
p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_name, &extracted_size, 0);
|
||||
ESP_LOGD(ENA_CWA_LOG, "4 memory: %d kB", (esp_get_free_heap_size() / 1024));
|
||||
mz_zip_reader_end(&zip_archive);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_CWA_LOG, p, extracted_size, ESP_LOG_DEBUG);
|
||||
free(p);
|
||||
*/
|
||||
|
||||
free(output_buffer);
|
||||
output_buffer = NULL;
|
||||
output_len = 0;
|
||||
ESP_LOGD(ENA_CWA_LOG, "memory freed: %d kB (min %d kB)", (esp_get_free_heap_size() / 1024), (esp_get_minimum_free_heap_size() / 1024));
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(ena_exposure_check_export(export_bin_start, (export_bin_end - export_bin_start)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return ESP_FAIL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -117,7 +76,7 @@ esp_err_t ena_cwa_http_event_handler(esp_http_client_event_t *evt)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void ena_cwa_receive_keys(char *date_string)
|
||||
esp_err_t ena_cwa_receive_keys(char *date_string)
|
||||
{
|
||||
char *url = malloc(strlen(ENA_CWA_KEYFILES_URL) + strlen(date_string));
|
||||
sprintf(url, ENA_CWA_KEYFILES_URL, date_string);
|
||||
@@ -126,6 +85,8 @@ void ena_cwa_receive_keys(char *date_string)
|
||||
.cert_pem = (char *)telekom_pem_start,
|
||||
.event_handler = ena_cwa_http_event_handler,
|
||||
};
|
||||
|
||||
ESP_LOGD(ENA_CWA_LOG, "start memory: %d kB, %s", (xPortGetFreeHeapSize() / 1024), date_string);
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
@@ -136,7 +97,31 @@ void ena_cwa_receive_keys(char *date_string)
|
||||
esp_http_client_get_status_code(client),
|
||||
content_length);
|
||||
}
|
||||
// free(url);
|
||||
free(url);
|
||||
esp_http_client_close(client);
|
||||
esp_http_client_cleanup(client);
|
||||
return err;
|
||||
}
|
||||
|
||||
void ena_cwa_run(void)
|
||||
{
|
||||
|
||||
static time_t current_time = 0;
|
||||
static uint32_t last_check = 0;
|
||||
current_time = time(NULL);
|
||||
last_check = ena_storage_read_last_exposure_date();
|
||||
if ((((uint32_t)current_time) - last_check) / (60 * 60 * 24) > 0 && wifi_controller_connection() != NULL)
|
||||
{
|
||||
char date_string[11];
|
||||
struct tm *time_info;
|
||||
time_info = localtime(¤t_time);
|
||||
time_info->tm_mday--;
|
||||
strftime(date_string, 11, "%Y-%m-%d", time_info);
|
||||
esp_err_t err = ena_cwa_receive_keys(date_string);
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
ena_storage_write_last_exposure_date((uint32_t)current_time);
|
||||
ena_exposure_summary(ena_exposure_default_config());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,15 +22,22 @@
|
||||
#ifndef _ena_CWA_H_
|
||||
#define _ena_CWA_H_
|
||||
|
||||
#define ENA_CWA_LOG "ESP-ENA-corona-warn-app" // TAG for Logging
|
||||
#include "esp_err.h"
|
||||
|
||||
#define ENA_CWA_KEYFILES_URL "http://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE/date/%s"
|
||||
#define ENA_CWA_LOG "ESP-ENA-cwa" // TAG for Logging
|
||||
|
||||
#define ENA_CWA_KEYFILES_URL "https://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE/date/%s"
|
||||
|
||||
/**
|
||||
* @brief fetch key export for given date
|
||||
*
|
||||
* @param[in] date_string the date to fetch the data for
|
||||
*/
|
||||
void ena_cwa_receive_keys(char *date_string);
|
||||
esp_err_t ena_cwa_receive_keys(char *date_string);
|
||||
|
||||
/**
|
||||
* @brief start ena CWA
|
||||
*/
|
||||
void ena_cwa_run(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user