From 17f8931f0f94bfbdcf6c39460b52781fd5dc4d4c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 5 Oct 2024 11:47:41 +0200 Subject: [PATCH] Enable logs reporting --- esp32_device/main/main.c | 1 + esp32_device/main/secure_api.c | 71 ++++++++++++++++++++++++++++++++++ esp32_device/main/secure_api.h | 16 ++++++++ 3 files changed, 88 insertions(+) diff --git a/esp32_device/main/main.c b/esp32_device/main/main.c index 5cc3cf3..d0416fb 100755 --- a/esp32_device/main/main.c +++ b/esp32_device/main/main.c @@ -171,6 +171,7 @@ void app_main(void) // Main loop ESP_LOGI(TAG, "Starting main loop"); + secure_api_report_log_message(Info, "Starting program main loop"); size_t fails = 0; while (true) diff --git a/esp32_device/main/secure_api.c b/esp32_device/main/secure_api.c index 63e6308..e5c8e17 100644 --- a/esp32_device/main/secure_api.c +++ b/esp32_device/main/secure_api.c @@ -196,6 +196,77 @@ char *secure_api_get_dev_certificate() return res; } +void secure_api_report_log_message(enum LogMessageSeverity severity, const char *msg) +{ + // Prepare signed payload + cJSON *obj = cJSON_CreateObject(); + if (!obj) + { + ESP_LOGE(TAG, "Failed allocate memory to store JSON object!"); + return; + } + + char *severity_s; + switch (severity) + { + case Info: + severity_s = "Info"; + break; + case Warn: + severity_s = "Warn"; + break; + case Error: + severity_s = "Error"; + break; + default: + severity_s = "Debug"; + break; + } + + cJSON_AddStringToObject(obj, "severity", severity_s); + cJSON_AddStringToObject(obj, "message", msg); + char *payload = jwt_gen(obj); + cJSON_Delete(obj); + + if (!payload) + { + ESP_LOGE(TAG, "Failed to build log report request!"); + return; + } + + // Prepare request body + cJSON *json_body = cJSON_CreateObject(); + if (!json_body) + { + ESP_LOGE(TAG, "Failed to allocated memory to store log report request body!"); + free(payload); + return; + } + cJSON_AddStringToObject(json_body, "payload", payload); + free(payload); + + char *body = cJSON_PrintUnformatted(json_body); + cJSON_Delete(json_body); + + if (!body) + { + ESP_LOGE(TAG, "Failed to allocated memory to store encoded log report request body!"); + return; + } + + // Send request + char *res = process_secure_request("/devices_api/logging/record", body); + + free(body); + + if (!res) + { + ESP_LOGE(TAG, "Log reporting failed!"); + } + + free(res); +} + sync_response *secure_api_sync_device() { cJSON *obj = cJSON_CreateObject(); diff --git a/esp32_device/main/secure_api.h b/esp32_device/main/secure_api.h index 1a5f71f..58f90a2 100644 --- a/esp32_device/main/secure_api.h +++ b/esp32_device/main/secure_api.h @@ -29,6 +29,17 @@ extern "C" DevEnrollValidated = 3, }; + /** + * Log message severity + */ + enum LogMessageSeverity + { + Debug = 0, + Info, + Warn, + Error + }; + /** * Get current device enrollment status */ @@ -45,6 +56,11 @@ extern "C" */ char *secure_api_get_dev_certificate(); + /** + * Report log message to backend + */ + void secure_api_report_log_message(enum LogMessageSeverity severity, const char *msg); + /** * Synchronise device with central backend *