Files
Calendink/Provider/main/api/ota/status.cpp
2026-03-03 19:41:33 -05:00

69 lines
2.1 KiB
C++

// SDK
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_littlefs.h"
#include "esp_partition.h"
// Project
#include "appstate.hpp"
#include "types.hpp"
internal esp_err_t api_ota_status_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "active_slot", g_Active_WWW_Partition);
const char *partitions[] = {"www_0", "www_1"};
cJSON *parts_arr = cJSON_AddArrayToObject(root, "partitions");
for (int i = 0; i < 2; i++)
{
cJSON *p_obj = cJSON_CreateObject();
cJSON_AddStringToObject(p_obj, "label", partitions[i]);
const esp_partition_t *p = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partitions[i]);
if (p)
{
cJSON_AddNumberToObject(p_obj, "size", p->size);
size_t total = 0, used = 0;
if (esp_littlefs_info(partitions[i], &total, &used) == ESP_OK)
{
cJSON_AddNumberToObject(p_obj, "used", used);
cJSON_AddNumberToObject(p_obj, "free", total - used);
}
else
{
// Not mounted or not LFS
cJSON_AddNumberToObject(p_obj, "used", 0);
cJSON_AddNumberToObject(p_obj, "free", p->size);
}
}
cJSON_AddItemToArray(parts_arr, p_obj);
}
cJSON_AddStringToObject(root, "active_partition",
g_Active_WWW_Partition == 0 ? "www_0" : "www_1");
cJSON_AddStringToObject(root, "target_partition",
g_Active_WWW_Partition == 0 ? "www_1" : "www_0");
const char *status_info = cJSON_Print(root);
httpd_resp_sendstr(req, status_info);
free((void *)status_info);
cJSON_Delete(root);
return ESP_OK;
}
internal const httpd_uri_t api_ota_status_uri = {.uri = "/api/ota/status",
.method = HTTP_GET,
.handler =
api_ota_status_handler,
.user_ctx = NULL};