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
2022void 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