Skip to content

Commit 27d90f6

Browse files
committed
Support using the filesystem as backend for settings subsystem
Signed-off-by: Martin Oemus <martin.oemus@seco.com>
1 parent 58c8a85 commit 27d90f6

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

lib/edgehog_device/include/nvs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include <zephyr/fs/nvs.h>
1919

20+
21+
#if defined (CONFIG_SETTINGS_NVS)
22+
2023
#ifdef CONFIG_EDGEHOG_DEVICE_USE_EDGEHOG_PARTITION
2124
/** @brief The devicetree partition name for the NVS. */
2225
#define NVS_PARTITION edgehog_partition
@@ -68,4 +71,6 @@ edgehog_result_t edgehog_nvs_get_free_space(size_t *free_space);
6871
}
6972
#endif
7073

74+
#endif // defined (CONFIG_SETTINGS_NVS)
75+
7176
#endif // NVS_H

lib/edgehog_device/nvs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <zephyr/fs/nvs.h>
1111
#include <zephyr/storage/flash_map.h>
1212

13+
#if defined (CONFIG_SETTINGS_NVS)
14+
1315
#include "log.h"
1416
EDGEHOG_LOG_MODULE_REGISTER(edgehog_nvs, CONFIG_EDGEHOG_DEVICE_NVS_LOG_LEVEL);
1517

@@ -69,3 +71,5 @@ edgehog_result_t edgehog_nvs_get_free_space(size_t *const free_space)
6971
*free_space = (size_t) free_space_res;
7072
return EDGEHOG_RESULT_OK;
7173
}
74+
75+
#endif // defined (CONFIG_SETTINGS_NVS)

lib/edgehog_device/storage_usage.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <zephyr/fs/fs.h>
8+
79
#include "storage_usage.h"
810

911
#include "edgehog_private.h"
@@ -19,23 +21,41 @@ EDGEHOG_LOG_MODULE_REGISTER(storage_usage, CONFIG_EDGEHOG_DEVICE_STORAGE_USAGE_L
1921

2022
void publish_storage_usage(edgehog_device_handle_t edgehog_device)
2123
{
22-
24+
#if defined (CONFIG_SETTINGS_NVS)
25+
const char* path = "/" NVS_PARTITION_LABEL;
26+
size_t total_space = NVS_PARTITION_SIZE;
2327
size_t free_space = 0;
24-
edgehog_result_t res = edgehog_nvs_get_free_space(&free_space);
25-
26-
if (res == EDGEHOG_RESULT_OK) {
27-
astarte_object_entry_t object_entries[]
28-
= { { .path = "totalBytes", .data = astarte_data_from_longinteger(NVS_PARTITION_SIZE) },
29-
{ .path = "freeBytes", .data = astarte_data_from_longinteger(free_space) } };
30-
31-
int64_t timestamp_ms = 0;
32-
system_time_current_ms(&timestamp_ms);
33-
34-
astarte_result_t res = astarte_device_send_object(edgehog_device->astarte_device,
35-
io_edgehog_devicemanager_StorageUsage.name, "/" NVS_PARTITION_LABEL, object_entries,
36-
ARRAY_SIZE(object_entries), &timestamp_ms);
37-
if (res != ASTARTE_RESULT_OK) {
38-
EDGEHOG_LOG_ERR("Unable to send syste_status"); // NOLINT
39-
}
28+
if (edgehog_nvs_get_free_space(&free_space) != EDGEHOG_RESULT_OK) {
29+
return;
30+
}
31+
#elif defined (CONFIG_SETTINGS_FILE)
32+
struct fs_file_t f;
33+
fs_file_t_init(&f);
34+
if (fs_open(&f, CONFIG_SETTINGS_FILE_PATH, 0) != 0) {
35+
return;
36+
}
37+
const char* path = f.mp->mnt_point;
38+
fs_close(&f);
39+
40+
struct fs_statvfs s;
41+
if (fs_statvfs(path, &s) != 0) {
42+
return;
43+
}
44+
size_t total_space = (size_t)s.f_frsize * s.f_blocks;
45+
size_t free_space = (size_t)s.f_frsize * s.f_bfree;
46+
#endif
47+
48+
astarte_object_entry_t object_entries[]
49+
= { { .path = "totalBytes", .data = astarte_data_from_longinteger(total_space) },
50+
{ .path = "freeBytes", .data = astarte_data_from_longinteger(free_space) } };
51+
52+
int64_t timestamp_ms = 0;
53+
system_time_current_ms(&timestamp_ms);
54+
55+
astarte_result_t res = astarte_device_send_object(edgehog_device->astarte_device,
56+
io_edgehog_devicemanager_StorageUsage.name, path, object_entries,
57+
ARRAY_SIZE(object_entries), &timestamp_ms);
58+
if (res != ASTARTE_RESULT_OK) {
59+
EDGEHOG_LOG_ERR("Unable to send syste_status"); // NOLINT
4060
}
4161
}

0 commit comments

Comments
 (0)