ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
storage.c
Go to the documentation of this file.
1#define LOG_TAG "STORE"
2
3#include "storage.h"
4#include "esp_littlefs.h"
5#include "esp_vfs.h"
6#include "logger.h"
7
8static const char *LITTLEFS_MOUNT_POINT =
9 "/data";
11static const char *LITTLEFS_PARTITION_LABEL = "storage";
13static bool is_mounted = false;
14
15esp_err_t storage_init(void) {
16 esp_vfs_littlefs_conf_t conf = {
17 .base_path = LITTLEFS_MOUNT_POINT,
18 .partition_label = LITTLEFS_PARTITION_LABEL,
19 .format_if_mount_failed = false,
20 .read_only = false,
21 };
22
23 esp_err_t ret = esp_vfs_littlefs_register(&conf);
24
25 if (ret != ESP_OK) {
26 if (ret == ESP_FAIL) {
27 LOGE("Failed to mount LittleFS or format filesystem");
28 } else if (ret == ESP_ERR_INVALID_STATE) {
29 LOGE("ESP_ERR_INVALID_STATE");
30 } else {
31 LOGE("Failed to initialize LittleFS: %s", esp_err_to_name(ret));
32 }
33 return ret;
34 }
35
36 is_mounted = true;
37
38 size_t total = 0, used = 0;
39 ret = esp_littlefs_info(LITTLEFS_PARTITION_LABEL, &total, &used);
40 if (ret == ESP_OK) {
41 LOGI("LittleFS mounted at %s. Total: %d bytes, Used: %d bytes",
42 LITTLEFS_MOUNT_POINT, total, used);
43 } else {
44 LOGE("Failed to get LittleFS information");
45 }
46
47 return ESP_OK;
48}
49
50bool storage_is_mounted(void) { return is_mounted; }
51
52const char *storage_get_mount_point(void) { return LITTLEFS_MOUNT_POINT; }
53
54const char *storage_get_partition_label(void) {
56}
57
59 if (!info) {
60 return ESP_ERR_INVALID_ARG;
61 }
62
63 if (!is_mounted) {
64 return ESP_ERR_INVALID_STATE;
65 }
66
67 size_t total = 0, used = 0;
68 esp_err_t ret = esp_littlefs_info(LITTLEFS_PARTITION_LABEL, &total, &used);
69 if (ret != ESP_OK) {
70 return ret;
71 }
72
73 info->total_bytes = total;
74 info->used_bytes = used;
75 info->free_bytes = total - used;
76
77 return ESP_OK;
78}
79
81 storage_info_t info;
82 LOGI("==================================================");
83 LOGI("STORAGE INFORMATION");
84 LOGI("==================================================");
85 LOGI("Mount Point : %s", LITTLEFS_MOUNT_POINT);
86 LOGI("Partition Label : %s", LITTLEFS_PARTITION_LABEL);
87 LOGI("Status : %s", is_mounted ? "Mounted" : "Not Mounted");
88
89 if (storage_get_info(&info) == ESP_OK) {
90 float usage = (float)info.used_bytes / (float)info.total_bytes * 100.0f;
91 LOGI("--------------------------------------------------");
92 LOGI("Total Space : %zu Bytes (%zu KB)", info.total_bytes,
93 info.total_bytes / 1024);
94 LOGI("Used Space : %zu Bytes (%zu KB)", info.used_bytes,
95 info.used_bytes / 1024);
96 LOGI("Free Space : %zu Bytes (%zu KB)", info.free_bytes,
97 info.free_bytes / 1024);
98 LOGI("Usage : %.2f %%", usage);
99 } else {
100 LOGE("Failed to retrieve storage statistics");
101 }
102 LOGI("==================================================");
103}
104
106 storage_info_t info;
107 if (storage_get_info(&info) != ESP_OK) {
108 return 0;
109 }
110 return info.total_bytes;
111}
112
114 storage_info_t info;
115 if (storage_get_info(&info) != ESP_OK) {
116 return 0;
117 }
118 return info.used_bytes;
119}
120
122 storage_info_t info;
123 if (storage_get_info(&info) != ESP_OK) {
124 return 0;
125 }
126 return info.free_bytes;
127}
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGI(...)
Log a message at Info level.
Definition logger.h:42
#define LOGE(...)
Log a message at Error level.
Definition logger.h:38
size_t storage_get_total_bytes(void)
Get the total size of the storage in bytes.
Definition storage.c:105
static const char * LITTLEFS_PARTITION_LABEL
Partition label for the LittleFS filesystem.
Definition storage.c:11
const char * storage_get_partition_label(void)
Get the partition label for the LittleFS filesystem.
Definition storage.c:54
esp_err_t storage_get_info(storage_info_t *info)
Get comprehensive storage information.
Definition storage.c:58
size_t storage_get_used_bytes(void)
Get the used size of the storage in bytes.
Definition storage.c:113
bool storage_is_mounted(void)
Check if the storage is mounted and ready.
Definition storage.c:50
static const char * LITTLEFS_MOUNT_POINT
Mount point for LittleFS filesystem.
Definition storage.c:8
esp_err_t storage_init(void)
Initialize and mount LittleFS filesystem.
Definition storage.c:15
const char * storage_get_mount_point(void)
Get the mount point for the LittleFS filesystem.
Definition storage.c:52
size_t storage_get_free_bytes(void)
Get the free size of the storage in bytes.
Definition storage.c:121
void storage_print_info(void)
Print storage information to the log.
Definition storage.c:80
static bool is_mounted
Flag indicating if the filesystem is currently mounted.
Definition storage.c:13
Storage information structure.
Definition storage.h:14
size_t total_bytes
Total size of the storage in bytes.
Definition storage.h:15
size_t free_bytes
Free size of the storage in bytes.
Definition storage.h:17
size_t used_bytes
Used size of the storage in bytes.
Definition storage.h:16