upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/components/wisp_relay/flash_monitor.c
blob: ceb8c3b1dd6d9fa60a0d770a95cb14fa92dfd19e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "flash_monitor.h"
#include "esp_littlefs.h"
#include "esp_log.h"
#include <string.h>

static const char *TAG = "flash_monitor";

void flash_get_health(const char *partition_label, flash_health_t *health)
{
    memset(health, 0, sizeof(flash_health_t));

    esp_err_t ret = esp_littlefs_info(partition_label,
                                       &health->total_bytes,
                                       &health->used_bytes);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get LittleFS info: %s", esp_err_to_name(ret));
        return;
    }

    if (health->total_bytes == 0) {
        health->free_bytes = 0;
        health->usage_percent = 0.0f;
    } else {
        health->free_bytes = health->total_bytes - health->used_bytes;
        health->usage_percent = (float)health->used_bytes / health->total_bytes * 100.0f;
    }

    ESP_LOGD(TAG, "Flash: %.1f%% used (%zu/%zu bytes)",
             health->usage_percent, health->used_bytes, health->total_bytes);
}