Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/edgehog_device/include/nvs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <zephyr/fs/nvs.h>

#if defined(CONFIG_SETTINGS_NVS)

#ifdef CONFIG_EDGEHOG_DEVICE_USE_EDGEHOG_PARTITION
/** @brief The devicetree partition name for the NVS. */
#define NVS_PARTITION edgehog_partition
Expand Down Expand Up @@ -68,4 +70,6 @@ edgehog_result_t edgehog_nvs_get_free_space(size_t *free_space);
}
#endif

#endif // defined (CONFIG_SETTINGS_NVS)

#endif // NVS_H
4 changes: 4 additions & 0 deletions lib/edgehog_device/nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <zephyr/fs/nvs.h>
#include <zephyr/storage/flash_map.h>

#if defined(CONFIG_SETTINGS_NVS)

#include "log.h"
EDGEHOG_LOG_MODULE_REGISTER(edgehog_nvs, CONFIG_EDGEHOG_DEVICE_NVS_LOG_LEVEL);

Expand Down Expand Up @@ -69,3 +71,5 @@ edgehog_result_t edgehog_nvs_get_free_space(size_t *const free_space)
*free_space = (size_t) free_space_res;
return EDGEHOG_RESULT_OK;
}

#endif // defined (CONFIG_SETTINGS_NVS)
54 changes: 37 additions & 17 deletions lib/edgehog_device/storage_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/fs/fs.h>

#include "storage_usage.h"

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

void publish_storage_usage(edgehog_device_handle_t edgehog_device)
{

#if defined(CONFIG_SETTINGS_NVS)
const char *path = "/" NVS_PARTITION_LABEL;
size_t total_space = NVS_PARTITION_SIZE;
size_t free_space = 0;
edgehog_result_t res = edgehog_nvs_get_free_space(&free_space);

if (res == EDGEHOG_RESULT_OK) {
astarte_object_entry_t object_entries[]
= { { .path = "totalBytes", .data = astarte_data_from_longinteger(NVS_PARTITION_SIZE) },
{ .path = "freeBytes", .data = astarte_data_from_longinteger(free_space) } };

int64_t timestamp_ms = 0;
system_time_current_ms(&timestamp_ms);

astarte_result_t res = astarte_device_send_object(edgehog_device->astarte_device,
io_edgehog_devicemanager_StorageUsage.name, "/" NVS_PARTITION_LABEL, object_entries,
ARRAY_SIZE(object_entries), &timestamp_ms);
if (res != ASTARTE_RESULT_OK) {
EDGEHOG_LOG_ERR("Unable to send syste_status"); // NOLINT
}
if (edgehog_nvs_get_free_space(&free_space) != EDGEHOG_RESULT_OK) {
return;
}
#elif defined(CONFIG_SETTINGS_FILE)
struct fs_file_t f;
fs_file_t_init(&f);
if (fs_open(&f, CONFIG_SETTINGS_FILE_PATH, 0) != 0) {
return;
}
const char *path = f.mp->mnt_point;
fs_close(&f);
Comment on lines +33 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, this whole open close cycle intended to find the partition mount point should not be required.
Passing CONFIG_SETTINGS_FILE_PATH directly to fs_statvfs should work fine. I have not tested this myself, but looking at the codebase of Zephyr it seems to be used this way in multiple spots.
See, for example, the test test_file_statvfs.


struct fs_statvfs s;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use single-character variables. Minimum is three chars according to our clang-tidy configuration.

if (fs_statvfs(path, &s) != 0) {
return;
}
size_t total_space = (size_t) s.f_frsize * s.f_blocks;
size_t free_space = (size_t) s.f_frsize * s.f_bfree;
#endif

astarte_object_entry_t object_entries[]
= { { .path = "totalBytes", .data = astarte_data_from_longinteger(total_space) },
{ .path = "freeBytes", .data = astarte_data_from_longinteger(free_space) } };

int64_t timestamp_ms = 0;
system_time_current_ms(&timestamp_ms);

astarte_result_t res = astarte_device_send_object(edgehog_device->astarte_device,
io_edgehog_devicemanager_StorageUsage.name, path, object_entries,
ARRAY_SIZE(object_entries), &timestamp_ms);
if (res != ASTARTE_RESULT_OK) {
EDGEHOG_LOG_ERR("Unable to send syste_status"); // NOLINT
}
}
Loading