Enable logs reporting

This commit is contained in:
Pierre HUBERT 2024-10-05 11:47:41 +02:00
parent cc63906ec1
commit 17f8931f0f
3 changed files with 88 additions and 0 deletions

View File

@ -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)

View File

@ -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();

View File

@ -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
*