k-fsw/app/src/parameters/storage_table.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdint.h> | ||
| 2 | |||
| 3 | #include <zephyr/sys/util.h> | ||
| 4 | |||
| 5 | #include <kfsw/platform/storage.h> | ||
| 6 | #include <kfsw/services/parameter.h> | ||
| 7 | |||
| 8 | #include "tables.h" | ||
| 9 | |||
| 10 | #define KFSW_BYTES_PER_KB 1024U | ||
| 11 | #define KFSW_PERCENT 100U | ||
| 12 | |||
| 13 | static uint32_t storage_total_kb; | ||
| 14 | static uint32_t storage_free_kb; | ||
| 15 | static uint8_t storage_used_pct; | ||
| 16 | static uint8_t storage_mounted; | ||
| 17 | |||
| 18 | 8 | static void sample_storage(void) | |
| 19 | { | ||
| 20 | struct kfsw_storage_info info; | ||
| 21 | uint64_t used; | ||
| 22 | |||
| 23 | 8 | if (kfsw_storage_get_info(&info) != 0) { | |
| 24 | ✗ | storage_mounted = 0U; | |
| 25 | ✗ | storage_total_kb = 0U; | |
| 26 | ✗ | storage_free_kb = 0U; | |
| 27 | ✗ | storage_used_pct = 0U; | |
| 28 | ✗ | return; | |
| 29 | } | ||
| 30 | |||
| 31 | 8 | storage_mounted = info.ready ? 1U : 0U; | |
| 32 | 8 | storage_total_kb = (uint32_t)(info.total_bytes / KFSW_BYTES_PER_KB); | |
| 33 | 8 | storage_free_kb = (uint32_t)(info.free_bytes / KFSW_BYTES_PER_KB); | |
| 34 | |||
| 35 | /* Zero while unmounted, not 100% used. */ | ||
| 36 | 8 | if ((info.total_bytes == 0U) || (info.free_bytes > info.total_bytes)) { | |
| 37 | ✗ | storage_used_pct = 0U; | |
| 38 | ✗ | return; | |
| 39 | } | ||
| 40 | 8 | used = info.total_bytes - info.free_bytes; | |
| 41 | 8 | storage_used_pct = (uint8_t)((used * KFSW_PERCENT) / info.total_bytes); | |
| 42 | } | ||
| 43 | |||
| 44 | 2 | static void sample_total_kb(void *value) | |
| 45 | { | ||
| 46 | 2 | sample_storage(); | |
| 47 | 2 | *(uint32_t *)value = storage_total_kb; | |
| 48 | 2 | } | |
| 49 | |||
| 50 | 2 | static void sample_free_kb(void *value) | |
| 51 | { | ||
| 52 | 2 | sample_storage(); | |
| 53 | 2 | *(uint32_t *)value = storage_free_kb; | |
| 54 | 2 | } | |
| 55 | |||
| 56 | 2 | static void sample_used_pct(void *value) | |
| 57 | { | ||
| 58 | 2 | sample_storage(); | |
| 59 | 2 | *(uint8_t *)value = storage_used_pct; | |
| 60 | 2 | } | |
| 61 | |||
| 62 | 2 | static void sample_mounted(void *value) | |
| 63 | { | ||
| 64 | 2 | sample_storage(); | |
| 65 | 2 | *(uint8_t *)value = storage_mounted; | |
| 66 | 2 | } | |
| 67 | |||
| 68 | static const struct kfsw_param_definition storage_param_definitions[] = { | ||
| 69 | { | ||
| 70 | .offset = 0x00U, | ||
| 71 | .type = KFSW_PARAM_U32, | ||
| 72 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 73 | .name = "total_kb", | ||
| 74 | .unit = "kB", | ||
| 75 | .description = "Filesystem capacity, or zero while unmounted", | ||
| 76 | .value = &storage_total_kb, | ||
| 77 | .default_value = {.u32 = 0U}, | ||
| 78 | .sample = sample_total_kb, | ||
| 79 | }, | ||
| 80 | { | ||
| 81 | .offset = 0x04U, | ||
| 82 | .type = KFSW_PARAM_U32, | ||
| 83 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 84 | .name = "free_kb", | ||
| 85 | .unit = "kB", | ||
| 86 | .description = "Filesystem space available, or zero while unmounted", | ||
| 87 | .value = &storage_free_kb, | ||
| 88 | .default_value = {.u32 = 0U}, | ||
| 89 | .sample = sample_free_kb, | ||
| 90 | }, | ||
| 91 | { | ||
| 92 | .offset = 0x08U, | ||
| 93 | .type = KFSW_PARAM_U8, | ||
| 94 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 95 | .name = "used_pct", | ||
| 96 | .unit = "%", | ||
| 97 | .description = "Filesystem space in use, or zero while unmounted", | ||
| 98 | .value = &storage_used_pct, | ||
| 99 | .default_value = {.u8 = 0U}, | ||
| 100 | .sample = sample_used_pct, | ||
| 101 | }, | ||
| 102 | { | ||
| 103 | .offset = 0x09U, | ||
| 104 | .type = KFSW_PARAM_U8, | ||
| 105 | .flags = KFSW_PARAM_FLAG_READ_ONLY, | ||
| 106 | .name = "mounted", | ||
| 107 | .description = "Whether the volume is mounted and usable", | ||
| 108 | .value = &storage_mounted, | ||
| 109 | .default_value = {.u8 = 0U}, | ||
| 110 | .sample = sample_mounted, | ||
| 111 | }, | ||
| 112 | }; | ||
| 113 | |||
| 114 | KFSW_PARAM_TABLE_FITS_THE_LINK(storage_param_definitions); | ||
| 115 | |||
| 116 | const struct kfsw_param_definition_set kfsw_storage_param_definitions = { | ||
| 117 | .table = KFSW_PARAM_TABLE_STORAGE, | ||
| 118 | .name = KFSW_PARAM_TABLE_STORAGE_NAME, | ||
| 119 | .description = "Filesystem size, free space, mount", | ||
| 120 | .definitions = storage_param_definitions, | ||
| 121 | .count = ARRAY_SIZE(storage_param_definitions), | ||
| 122 | }; | ||
| 123 |