kfsw-services/src/parameter/parameter.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | #include <zephyr/kernel.h> | ||
| 9 | #include <zephyr/sys/atomic.h> | ||
| 10 | |||
| 11 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_PARAM | ||
| 12 | #include <kfsw/services/log.h> | ||
| 13 | #include <kfsw/services/parameter.h> | ||
| 14 | |||
| 15 | #include "parameter_internal.h" | ||
| 16 | #include "param_wire.h" | ||
| 17 | |||
| 18 | static struct kfsw_param_entry parameter_table[KFSW_PARAM_MAX_DEFINITIONS]; | ||
| 19 | static size_t parameter_count; | ||
| 20 | |||
| 21 | static struct kfsw_param_table_info parameter_tables[KFSW_PARAM_MAX_TABLES]; | ||
| 22 | static size_t table_count; | ||
| 23 | |||
| 24 | K_MUTEX_DEFINE(kfsw_param_lock); | ||
| 25 | |||
| 26 | static bool initialized; | ||
| 27 | |||
| 28 | /* strnlen is not visible under the C17 profile these builds use, and a plain | ||
| 29 | * strlen on a name that is not terminated would read past it. | ||
| 30 | */ | ||
| 31 | 1298 | static bool name_is_within_limit(const char *name) | |
| 32 | { | ||
| 33 | 16092 | for (size_t index = 0U; index <= KFSW_PARAM_NAME_MAX; index++) { | |
| 34 | 16088 | if (name[index] == '\0') { | |
| 35 | 1294 | return true; | |
| 36 | } | ||
| 37 | } | ||
| 38 | 4 | return false; | |
| 39 | } | ||
| 40 | |||
| 41 | 8308 | static size_t scalar_size(enum kfsw_param_type type) | |
| 42 | { | ||
| 43 | 8308 | switch (type) { | |
| 44 | 2052 | case KFSW_PARAM_U8: | |
| 45 | case KFSW_PARAM_I8: | ||
| 46 | case KFSW_PARAM_X8: | ||
| 47 | 2052 | return sizeof(uint8_t); | |
| 48 | 764 | case KFSW_PARAM_U16: | |
| 49 | case KFSW_PARAM_I16: | ||
| 50 | case KFSW_PARAM_X16: | ||
| 51 | 764 | return sizeof(uint16_t); | |
| 52 | 4944 | case KFSW_PARAM_U32: | |
| 53 | case KFSW_PARAM_I32: | ||
| 54 | case KFSW_PARAM_X32: | ||
| 55 | 4944 | return sizeof(uint32_t); | |
| 56 | 12 | case KFSW_PARAM_U64: | |
| 57 | case KFSW_PARAM_I64: | ||
| 58 | case KFSW_PARAM_X64: | ||
| 59 | 12 | return sizeof(uint64_t); | |
| 60 | 530 | case KFSW_PARAM_FLOAT: | |
| 61 | 530 | return sizeof(float); | |
| 62 | 6 | case KFSW_PARAM_DOUBLE: | |
| 63 | 6 | return sizeof(double); | |
| 64 | ✗ | case KFSW_PARAM_STRING: | |
| 65 | case KFSW_PARAM_DATA: | ||
| 66 | case KFSW_PARAM_INVALID: | ||
| 67 | default: | ||
| 68 | ✗ | return 0U; | |
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | /* A scalar's width comes from its type; a string's from its declared capacity. */ | ||
| 73 | 6326 | static size_t entry_capacity(const struct kfsw_param_entry *entry) | |
| 74 | { | ||
| 75 | 6326 | if ((entry->info.type == KFSW_PARAM_STRING) || (entry->info.type == KFSW_PARAM_DATA)) { | |
| 76 | 702 | return entry->info.array_size; | |
| 77 | } | ||
| 78 | 5624 | return scalar_size(entry->info.type); | |
| 79 | } | ||
| 80 | |||
| 81 | /* Bounded copy that always terminates. */ | ||
| 82 | 346 | static size_t copy_text(char *destination, size_t destination_size, const char *source, | |
| 83 | size_t source_size) | ||
| 84 | { | ||
| 85 | 346 | size_t length = 0U; | |
| 86 | |||
| 87 | 1084 | while ((length < source_size) && (length + 1U < destination_size) && | |
| 88 | 1076 | (source[length] != '\0')) { | |
| 89 | 738 | destination[length] = source[length]; | |
| 90 | 738 | length++; | |
| 91 | } | ||
| 92 | 346 | destination[length] = '\0'; | |
| 93 | 346 | return length; | |
| 94 | } | ||
| 95 | |||
| 96 | 326 | int kfsw_param_parse_text(const char *text, struct kfsw_param_value *value) | |
| 97 | { | ||
| 98 | 326 | char *end = NULL; | |
| 99 | 326 | unsigned long long unsigned_value = 0U; | |
| 100 | 326 | long long signed_value = 0; | |
| 101 | |||
| 102 | 326 | errno = 0; | |
| 103 | 326 | switch (value->type) { | |
| 104 | 114 | case KFSW_PARAM_U8: | |
| 105 | case KFSW_PARAM_U16: | ||
| 106 | case KFSW_PARAM_U32: | ||
| 107 | case KFSW_PARAM_U64: | ||
| 108 | case KFSW_PARAM_X8: | ||
| 109 | case KFSW_PARAM_X16: | ||
| 110 | case KFSW_PARAM_X32: | ||
| 111 | case KFSW_PARAM_X64: | ||
| 112 | 114 | if (text[0] == '-') { | |
| 113 | 2 | return -ERANGE; | |
| 114 | } | ||
| 115 | 112 | unsigned_value = strtoull(text, &end, 0); | |
| 116 | 112 | if ((errno == ERANGE) || (end == text) || (*end != '\0')) { | |
| 117 | 8 | return -EINVAL; | |
| 118 | } | ||
| 119 | 104 | break; | |
| 120 | 194 | case KFSW_PARAM_I8: | |
| 121 | case KFSW_PARAM_I16: | ||
| 122 | case KFSW_PARAM_I32: | ||
| 123 | case KFSW_PARAM_I64: | ||
| 124 | 194 | signed_value = strtoll(text, &end, 0); | |
| 125 | 194 | if ((errno == ERANGE) || (end == text) || (*end != '\0')) { | |
| 126 | 6 | return -EINVAL; | |
| 127 | } | ||
| 128 | 188 | break; | |
| 129 | #if CONFIG_KFSW_PARAM_FLOAT | ||
| 130 | 6 | case KFSW_PARAM_FLOAT: | |
| 131 | 6 | value->scalar.f32 = strtof(text, &end); | |
| 132 | 6 | return ((errno == 0) && (end != text) && (*end == '\0')) ? 0 : -EINVAL; | |
| 133 | 12 | case KFSW_PARAM_DOUBLE: | |
| 134 | 12 | value->scalar.f64 = strtod(text, &end); | |
| 135 | 12 | return ((errno == 0) && (end != text) && (*end == '\0')) ? 0 : -EINVAL; | |
| 136 | #endif | ||
| 137 | ✗ | case KFSW_PARAM_STRING: { | |
| 138 | ✗ | size_t length = 0U; | |
| 139 | |||
| 140 | ✗ | while ((length + 1U < sizeof(value->text)) && (text[length] != '\0')) { | |
| 141 | ✗ | value->text[length] = text[length]; | |
| 142 | ✗ | length++; | |
| 143 | } | ||
| 144 | ✗ | if (text[length] != '\0') { | |
| 145 | ✗ | return -EMSGSIZE; | |
| 146 | } | ||
| 147 | ✗ | value->text[length] = '\0'; | |
| 148 | ✗ | value->size = length + 1U; | |
| 149 | ✗ | return 0; | |
| 150 | } | ||
| 151 | ✗ | case KFSW_PARAM_DATA: { | |
| 152 | /* Comma-separated, with the declared number of elements. */ | ||
| 153 | ✗ | const char *cursor = text; | |
| 154 | ✗ | size_t count = 0U; | |
| 155 | |||
| 156 | ✗ | while ((*cursor != '\0') && (count < sizeof(value->bytes))) { | |
| 157 | unsigned long element; | ||
| 158 | |||
| 159 | ✗ | errno = 0; | |
| 160 | ✗ | element = strtoul(cursor, &end, 0); | |
| 161 | ✗ | if ((errno == ERANGE) || (end == cursor) || (element > UINT8_MAX)) { | |
| 162 | ✗ | return -EINVAL; | |
| 163 | } | ||
| 164 | ✗ | value->bytes[count] = (uint8_t)element; | |
| 165 | ✗ | count++; | |
| 166 | ✗ | cursor = end; | |
| 167 | ✗ | if (*cursor == ',') { | |
| 168 | ✗ | cursor++; | |
| 169 | ✗ | } else if (*cursor != '\0') { | |
| 170 | ✗ | return -EINVAL; | |
| 171 | } | ||
| 172 | } | ||
| 173 | ✗ | if (count == 0U) { | |
| 174 | ✗ | return -EINVAL; | |
| 175 | } | ||
| 176 | ✗ | value->size = count; | |
| 177 | ✗ | return 0; | |
| 178 | } | ||
| 179 | ✗ | case KFSW_PARAM_INVALID: | |
| 180 | default: | ||
| 181 | ✗ | return -ENOTSUP; | |
| 182 | } | ||
| 183 | |||
| 184 | 292 | switch (value->type) { | |
| 185 | 2 | case KFSW_PARAM_U8: | |
| 186 | case KFSW_PARAM_X8: | ||
| 187 | 2 | if (unsigned_value > UINT8_MAX) { | |
| 188 | 2 | return -ERANGE; | |
| 189 | } | ||
| 190 | ✗ | value->scalar.u8 = (uint8_t)unsigned_value; | |
| 191 | ✗ | break; | |
| 192 | 2 | case KFSW_PARAM_U16: | |
| 193 | case KFSW_PARAM_X16: | ||
| 194 | 2 | if (unsigned_value > UINT16_MAX) { | |
| 195 | ✗ | return -ERANGE; | |
| 196 | } | ||
| 197 | 2 | value->scalar.u16 = (uint16_t)unsigned_value; | |
| 198 | 2 | break; | |
| 199 | 100 | case KFSW_PARAM_U32: | |
| 200 | case KFSW_PARAM_X32: | ||
| 201 | 100 | if (unsigned_value > UINT32_MAX) { | |
| 202 | ✗ | return -ERANGE; | |
| 203 | } | ||
| 204 | 100 | value->scalar.u32 = (uint32_t)unsigned_value; | |
| 205 | 100 | break; | |
| 206 | ✗ | case KFSW_PARAM_U64: | |
| 207 | case KFSW_PARAM_X64: | ||
| 208 | ✗ | value->scalar.u64 = (uint64_t)unsigned_value; | |
| 209 | ✗ | break; | |
| 210 | ✗ | case KFSW_PARAM_I8: | |
| 211 | ✗ | if ((signed_value < INT8_MIN) || (signed_value > INT8_MAX)) { | |
| 212 | ✗ | return -ERANGE; | |
| 213 | } | ||
| 214 | ✗ | value->scalar.i8 = (int8_t)signed_value; | |
| 215 | ✗ | break; | |
| 216 | 4 | case KFSW_PARAM_I16: | |
| 217 | 4 | if ((signed_value < INT16_MIN) || (signed_value > INT16_MAX)) { | |
| 218 | ✗ | return -ERANGE; | |
| 219 | } | ||
| 220 | 4 | value->scalar.i16 = (int16_t)signed_value; | |
| 221 | 4 | break; | |
| 222 | ✗ | case KFSW_PARAM_I32: | |
| 223 | ✗ | if ((signed_value < INT32_MIN) || (signed_value > INT32_MAX)) { | |
| 224 | ✗ | return -ERANGE; | |
| 225 | } | ||
| 226 | ✗ | value->scalar.i32 = (int32_t)signed_value; | |
| 227 | ✗ | break; | |
| 228 | 184 | case KFSW_PARAM_I64: | |
| 229 | 184 | value->scalar.i64 = (int64_t)signed_value; | |
| 230 | 184 | break; | |
| 231 | ✗ | default: | |
| 232 | ✗ | return -EINVAL; | |
| 233 | } | ||
| 234 | |||
| 235 | 290 | return 0; | |
| 236 | } | ||
| 237 | |||
| 238 | 4236 | void kfsw_param_table_lock(void) | |
| 239 | { | ||
| 240 | 4236 | k_mutex_lock(&kfsw_param_lock, K_FOREVER); | |
| 241 | 4236 | } | |
| 242 | |||
| 243 | 192 | int kfsw_param_table_lock_until(int64_t deadline) | |
| 244 | { | ||
| 245 | 192 | int64_t remaining = deadline - k_uptime_get(); | |
| 246 | |||
| 247 | 192 | return remaining <= 0 ? -ETIMEDOUT : k_mutex_lock(&kfsw_param_lock, K_MSEC(remaining)); | |
| 248 | } | ||
| 249 | |||
| 250 | 4428 | void kfsw_param_table_unlock(void) | |
| 251 | { | ||
| 252 | 4428 | k_mutex_unlock(&kfsw_param_lock); | |
| 253 | 4428 | } | |
| 254 | |||
| 255 | 2952 | size_t kfsw_param_entry_count(void) | |
| 256 | { | ||
| 257 | 2952 | return parameter_count; | |
| 258 | } | ||
| 259 | |||
| 260 | 1788 | const struct kfsw_param_entry *kfsw_param_entry_at(size_t index) | |
| 261 | { | ||
| 262 | 1788 | return (index < parameter_count) ? ¶meter_table[index] : NULL; | |
| 263 | } | ||
| 264 | |||
| 265 | /* | ||
| 266 | * Tables are sorted by ID, so this is a binary search. | ||
| 267 | */ | ||
| 268 | 2454 | const struct kfsw_param_entry *kfsw_param_find_id(uint16_t id) | |
| 269 | { | ||
| 270 | 2454 | size_t low = 0U; | |
| 271 | 2454 | size_t high = parameter_count; | |
| 272 | |||
| 273 | 6834 | while (low < high) { | |
| 274 | 6816 | size_t middle = low + ((high - low) / 2U); | |
| 275 | 6816 | uint16_t found = parameter_table[middle].info.id; | |
| 276 | |||
| 277 | 6816 | if (found == id) { | |
| 278 | 2436 | return ¶meter_table[middle]; | |
| 279 | } | ||
| 280 | 4380 | if (found < id) { | |
| 281 | 622 | low = middle + 1U; | |
| 282 | } else { | ||
| 283 | 3758 | high = middle; | |
| 284 | } | ||
| 285 | } | ||
| 286 | 18 | return NULL; | |
| 287 | } | ||
| 288 | |||
| 289 | 1440 | const struct kfsw_param_entry *kfsw_param_find_name(const char *name) | |
| 290 | { | ||
| 291 | 1440 | if (name == NULL) { | |
| 292 | ✗ | return NULL; | |
| 293 | } | ||
| 294 | |||
| 295 | 16962 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 296 | 16938 | if (strcmp(parameter_table[index].info.name, name) == 0) { | |
| 297 | 1416 | return ¶meter_table[index]; | |
| 298 | } | ||
| 299 | } | ||
| 300 | 24 | return NULL; | |
| 301 | } | ||
| 302 | |||
| 303 | 2292 | static int read_entry(const struct kfsw_param_entry *entry, struct kfsw_param_value *value, | |
| 304 | bool sample) | ||
| 305 | { | ||
| 306 | size_t size; | ||
| 307 | |||
| 308 | 2292 | if ((entry == NULL) || (value == NULL)) { | |
| 309 | ✗ | return -EINVAL; | |
| 310 | } | ||
| 311 | 2292 | size = entry_capacity(entry); | |
| 312 | 2292 | if (size == 0U) { | |
| 313 | ✗ | return -ENOTSUP; | |
| 314 | } | ||
| 315 | |||
| 316 | /* Refresh sampled values before reading them. */ | ||
| 317 | 2292 | if (sample && (entry->definition->sample != NULL)) { | |
| 318 | 492 | entry->definition->sample(entry->definition->value); | |
| 319 | } | ||
| 320 | |||
| 321 | 2292 | memset(value, 0, sizeof(*value)); | |
| 322 | 2292 | value->type = entry->info.type; | |
| 323 | |||
| 324 | 2292 | if (entry->info.type == KFSW_PARAM_STRING) { | |
| 325 | 66 | value->size = copy_text(value->text, sizeof(value->text), entry->definition->value, | |
| 326 | 66 | size) + | |
| 327 | 1U; | ||
| 328 | 66 | return 0; | |
| 329 | } | ||
| 330 | |||
| 331 | 2226 | if (entry->info.type == KFSW_PARAM_DATA) { | |
| 332 | /* Always the whole array. */ | ||
| 333 | 110 | memcpy(value->bytes, entry->definition->value, size); | |
| 334 | 110 | value->size = size; | |
| 335 | 110 | return 0; | |
| 336 | } | ||
| 337 | |||
| 338 | 2116 | value->size = size; | |
| 339 | 2116 | memcpy(&value->scalar, entry->definition->value, size); | |
| 340 | 2116 | return 0; | |
| 341 | } | ||
| 342 | |||
| 343 | 1792 | int kfsw_param_read_entry(const struct kfsw_param_entry *entry, struct kfsw_param_value *value) | |
| 344 | { | ||
| 345 | 1792 | return read_entry(entry, value, true); | |
| 346 | } | ||
| 347 | |||
| 348 | /* | ||
| 349 | * Read the stored value without sampling. A remote write stores the new value | ||
| 350 | * first and then applies it from here; sampling would overwrite it. | ||
| 351 | */ | ||
| 352 | 500 | int kfsw_param_read_stored_entry(const struct kfsw_param_entry *entry, | |
| 353 | struct kfsw_param_value *value) | ||
| 354 | { | ||
| 355 | 500 | return read_entry(entry, value, false); | |
| 356 | } | ||
| 357 | |||
| 358 | 2146 | int kfsw_param_validate_entry(const struct kfsw_param_entry *entry, | |
| 359 | const struct kfsw_param_value *value) | ||
| 360 | { | ||
| 361 | size_t size; | ||
| 362 | |||
| 363 | 2146 | if ((entry == NULL) || (value == NULL)) { | |
| 364 | ✗ | return -EINVAL; | |
| 365 | } | ||
| 366 | 2146 | size = entry_capacity(entry); | |
| 367 | 2146 | if (size == 0U) { | |
| 368 | ✗ | return -ENOTSUP; | |
| 369 | } | ||
| 370 | 2146 | if (value->type != entry->info.type) { | |
| 371 | 6 | return -EMSGSIZE; | |
| 372 | } | ||
| 373 | |||
| 374 | 2140 | if (entry->info.type == KFSW_PARAM_STRING) { | |
| 375 | /* size includes the terminator. */ | ||
| 376 | 90 | if ((value->size == 0U) || (value->size > size)) { | |
| 377 | 4 | return -EMSGSIZE; | |
| 378 | } | ||
| 379 | 86 | if (entry->definition->validate_text != NULL) { | |
| 380 | 54 | return entry->definition->validate_text(value->text); | |
| 381 | } | ||
| 382 | 32 | return 0; | |
| 383 | } | ||
| 384 | |||
| 385 | 2050 | if (entry->info.type == KFSW_PARAM_DATA) { | |
| 386 | /* Arrays are written whole. */ | ||
| 387 | 182 | if (value->size != size) { | |
| 388 | 4 | return -EMSGSIZE; | |
| 389 | } | ||
| 390 | 178 | if (entry->definition->validate_data != NULL) { | |
| 391 | 150 | return entry->definition->validate_data(value->bytes, value->size); | |
| 392 | } | ||
| 393 | 28 | return 0; | |
| 394 | } | ||
| 395 | |||
| 396 | 1868 | if (value->size != size) { | |
| 397 | 4 | return -EMSGSIZE; | |
| 398 | } | ||
| 399 | 1864 | if (entry->definition->validate != NULL) { | |
| 400 | 1358 | return entry->definition->validate(&value->scalar); | |
| 401 | } | ||
| 402 | 506 | return 0; | |
| 403 | } | ||
| 404 | |||
| 405 | 1722 | int kfsw_param_write_entry(const struct kfsw_param_entry *entry, | |
| 406 | const union kfsw_param_scalar *value) | ||
| 407 | { | ||
| 408 | 1722 | const size_t size = scalar_size(entry->info.type); | |
| 409 | |||
| 410 | 1722 | if (initialized && (entry->definition->apply != NULL)) { | |
| 411 | 80 | int result = entry->definition->apply(value); | |
| 412 | |||
| 413 | 80 | if (result != 0) { | |
| 414 | 28 | return result; | |
| 415 | } | ||
| 416 | } | ||
| 417 | 1694 | memcpy(entry->definition->value, value, size); | |
| 418 | 1694 | if (entry->definition->changed != NULL) { | |
| 419 | 268 | entry->definition->changed(value); | |
| 420 | } | ||
| 421 | 1694 | return 0; | |
| 422 | } | ||
| 423 | |||
| 424 | 144 | void kfsw_param_write_data_entry(const struct kfsw_param_entry *entry, const uint8_t *data, | |
| 425 | size_t size) | ||
| 426 | { | ||
| 427 | 144 | memcpy(entry->definition->value, data, size); | |
| 428 | 144 | if (entry->definition->changed_data != NULL) { | |
| 429 | 138 | entry->definition->changed_data(entry->definition->value, size); | |
| 430 | } | ||
| 431 | 144 | } | |
| 432 | |||
| 433 | 112 | void kfsw_param_write_text_entry(const struct kfsw_param_entry *entry, const char *text) | |
| 434 | { | ||
| 435 | 112 | (void)copy_text(entry->definition->value, entry->info.array_size, text, | |
| 436 | 112 | entry->info.array_size); | |
| 437 | 112 | if (entry->definition->changed_text != NULL) { | |
| 438 | 48 | entry->definition->changed_text(entry->definition->value); | |
| 439 | } | ||
| 440 | 112 | } | |
| 441 | |||
| 442 | 1888 | static void read_default(const struct kfsw_param_entry *entry, struct kfsw_param_value *value) | |
| 443 | { | ||
| 444 | 1888 | *value = (struct kfsw_param_value){.type = entry->info.type, .size = entry_capacity(entry)}; | |
| 445 | 1888 | if (entry->info.type == KFSW_PARAM_DATA) { | |
| 446 | 164 | if (entry->definition->default_data != NULL) { | |
| 447 | 2 | memcpy(value->bytes, entry->definition->default_data, value->size); | |
| 448 | } | ||
| 449 | 1724 | } else if (entry->info.type == KFSW_PARAM_STRING) { | |
| 450 | 88 | const char *text = entry->definition->default_text; | |
| 451 | |||
| 452 | 88 | value->size = copy_text(value->text, sizeof(value->text), text != NULL ? text : "", | |
| 453 | 176 | entry->info.array_size) + | |
| 454 | 1U; | ||
| 455 | } else { | ||
| 456 | 1636 | value->scalar = entry->definition->default_value; | |
| 457 | } | ||
| 458 | 1888 | } | |
| 459 | |||
| 460 | 1056 | int kfsw_param_write_default(const struct kfsw_param_entry *entry) | |
| 461 | { | ||
| 462 | struct kfsw_param_value value; | ||
| 463 | int result; | ||
| 464 | |||
| 465 | 1056 | read_default(entry, &value); | |
| 466 | 1056 | result = initialized ? kfsw_param_validate_entry(entry, &value) : 0; | |
| 467 | 1056 | return result == 0 ? kfsw_param_wire_apply(entry, &value) : result; | |
| 468 | } | ||
| 469 | |||
| 470 | 406 | static void notify_changed(const struct kfsw_param_entry *entry, | |
| 471 | const struct kfsw_param_value *value) | ||
| 472 | { | ||
| 473 | 406 | if (entry->info.type == KFSW_PARAM_STRING) { | |
| 474 | 24 | if (entry->definition->changed_text != NULL) { | |
| 475 | 24 | entry->definition->changed_text(value->text); | |
| 476 | } | ||
| 477 | 24 | return; | |
| 478 | } | ||
| 479 | 382 | if (entry->info.type == KFSW_PARAM_DATA) { | |
| 480 | 24 | if (entry->definition->changed_data != NULL) { | |
| 481 | 24 | entry->definition->changed_data(value->bytes, value->size); | |
| 482 | } | ||
| 483 | 24 | return; | |
| 484 | } | ||
| 485 | 358 | if (entry->definition->changed != NULL) { | |
| 486 | 344 | entry->definition->changed(&value->scalar); | |
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | 406 | void kfsw_param_value_accepted(uint16_t id) | |
| 491 | { | ||
| 492 | 406 | const struct kfsw_param_entry *entry = kfsw_param_find_id(id); | |
| 493 | struct kfsw_param_value value; | ||
| 494 | |||
| 495 | 406 | if (entry != NULL && kfsw_param_read_stored_entry(entry, &value) == 0) { | |
| 496 | 406 | notify_changed(entry, &value); | |
| 497 | } | ||
| 498 | 406 | } | |
| 499 | |||
| 500 | 1286 | static struct kfsw_param_table_info *find_table(uint8_t id) | |
| 501 | { | ||
| 502 | 3126 | for (size_t index = 0U; index < table_count; index++) { | |
| 503 | 2926 | if (parameter_tables[index].id == id) { | |
| 504 | 1086 | return ¶meter_tables[index]; | |
| 505 | } | ||
| 506 | } | ||
| 507 | 200 | return NULL; | |
| 508 | } | ||
| 509 | |||
| 510 | /* Table IDs must be in an allocated band; zero is reserved. */ | ||
| 511 | 212 | static bool table_is_allocated(uint8_t table) | |
| 512 | { | ||
| 513 | 212 | return (table >= KFSW_PARAM_TABLE_CORE_FIRST) && (table <= KFSW_PARAM_TABLE_MODULE_LAST); | |
| 514 | } | ||
| 515 | |||
| 516 | 216 | static int add_table(const struct kfsw_param_definition_set *set) | |
| 517 | { | ||
| 518 | size_t insert_at; | ||
| 519 | |||
| 520 | 216 | if ((set->name == NULL) || (set->name[0] == '\0') || !name_is_within_limit(set->name)) { | |
| 521 | 4 | kfsw_log_error("PARAM: table %u has no usable name", set->table); | |
| 522 | 4 | return -EINVAL; | |
| 523 | } | ||
| 524 | 212 | if (!table_is_allocated(set->table)) { | |
| 525 | 8 | kfsw_log_error("PARAM: table %u is outside every allocated band", set->table); | |
| 526 | 8 | return -EINVAL; | |
| 527 | } | ||
| 528 | 204 | if (find_table(set->table) != NULL) { | |
| 529 | 4 | kfsw_log_error("PARAM: table %u (%s) is already registered", set->table, set->name); | |
| 530 | 4 | return -EEXIST; | |
| 531 | } | ||
| 532 | 200 | if (table_count >= KFSW_PARAM_MAX_TABLES) { | |
| 533 | ✗ | kfsw_log_error("PARAM: no room for table %u (%s)", set->table, set->name); | |
| 534 | ✗ | return -ENOSPC; | |
| 535 | } | ||
| 536 | |||
| 537 | /* Keep tables sorted by ID. */ | ||
| 538 | 200 | insert_at = table_count; | |
| 539 | 400 | for (size_t index = 0U; index < table_count; index++) { | |
| 540 | 248 | if (parameter_tables[index].id > set->table) { | |
| 541 | 48 | insert_at = index; | |
| 542 | 48 | break; | |
| 543 | } | ||
| 544 | } | ||
| 545 | 200 | if (insert_at < table_count) { | |
| 546 | 48 | memmove(¶meter_tables[insert_at + 1U], ¶meter_tables[insert_at], | |
| 547 | 48 | (table_count - insert_at) * sizeof(parameter_tables[0])); | |
| 548 | } | ||
| 549 | 200 | parameter_tables[insert_at] = (struct kfsw_param_table_info){ | |
| 550 | 200 | .id = set->table, | |
| 551 | 200 | .name = set->name, | |
| 552 | 200 | .description = set->description, | |
| 553 | .count = 0U, | ||
| 554 | }; | ||
| 555 | 200 | table_count++; | |
| 556 | 200 | return 0; | |
| 557 | } | ||
| 558 | |||
| 559 | 1092 | static int add_definition(const struct kfsw_param_definition_set *set, | |
| 560 | const struct kfsw_param_definition *definition) | ||
| 561 | { | ||
| 562 | struct kfsw_param_value default_value; | ||
| 563 | struct kfsw_param_table_info *table; | ||
| 564 | 1092 | size_t insert_at = parameter_count; | |
| 565 | size_t size; | ||
| 566 | uint16_t wire_id; | ||
| 567 | uint32_t flags; | ||
| 568 | |||
| 569 | 1092 | if ((definition == NULL) || (definition->name == NULL) || (definition->name[0] == '\0') || | |
| 570 | 1092 | (definition->value == NULL)) { | |
| 571 | ✗ | return -EINVAL; | |
| 572 | } | ||
| 573 | |||
| 574 | 1092 | if ((definition->apply != NULL) && | |
| 575 | 44 | ((definition->type == KFSW_PARAM_STRING) || (definition->type == KFSW_PARAM_DATA))) { | |
| 576 | ✗ | return -EINVAL; | |
| 577 | } | ||
| 578 | |||
| 579 | /* A snapshot is restored as a whole, so one persistent parameter with an | ||
| 580 | * apply callback would make every other persistent value unrestorable. | ||
| 581 | * The combination is refused here, at registration, rather than left to | ||
| 582 | * fail half way through a restore. Owner-store parameters are exempt: | ||
| 583 | * the restore skips them, because their owner keeps them elsewhere. | ||
| 584 | */ | ||
| 585 | 1092 | if ((definition->apply != NULL) && | |
| 586 | 44 | ((definition->flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U) && | |
| 587 | 8 | ((definition->flags & KFSW_PARAM_FLAG_OWNER_STORE) == 0U)) { | |
| 588 | 4 | kfsw_log_error("PARAM: %s is persistent and has an apply callback", | |
| 589 | definition->name); | ||
| 590 | 4 | return -EINVAL; | |
| 591 | } | ||
| 592 | |||
| 593 | /* Strings and arrays declare their size; a missing or too large size is refused. */ | ||
| 594 | 1088 | if (definition->type == KFSW_PARAM_DATA) { | |
| 595 | 44 | if ((definition->capacity == 0U) || | |
| 596 | 44 | (definition->capacity > KFSW_PARAM_STRING_MAX)) { | |
| 597 | ✗ | kfsw_log_error("PARAM: %s declares %u elements", definition->name, | |
| 598 | definition->capacity); | ||
| 599 | ✗ | return -EINVAL; | |
| 600 | } | ||
| 601 | 44 | size = definition->capacity; | |
| 602 | 1044 | } else if (definition->type == KFSW_PARAM_STRING) { | |
| 603 | 80 | if ((definition->capacity < 2U) || (definition->capacity > KFSW_PARAM_STRING_MAX)) { | |
| 604 | ✗ | kfsw_log_error("PARAM: %s declares a capacity of %u", definition->name, | |
| 605 | definition->capacity); | ||
| 606 | ✗ | return -EINVAL; | |
| 607 | } | ||
| 608 | 80 | size = definition->capacity; | |
| 609 | } else { | ||
| 610 | #if !CONFIG_KFSW_PARAM_FLOAT | ||
| 611 | 2 | if ((definition->type == KFSW_PARAM_FLOAT) || | |
| 612 | ✗ | (definition->type == KFSW_PARAM_DOUBLE)) { | |
| 613 | 2 | kfsw_log_error("PARAM: %s needs KFSW_PARAM_FLOAT", definition->name); | |
| 614 | 2 | return -ENOTSUP; | |
| 615 | } | ||
| 616 | #endif | ||
| 617 | 962 | size = scalar_size(definition->type); | |
| 618 | 962 | if (size == 0U) { | |
| 619 | ✗ | return -EINVAL; | |
| 620 | } | ||
| 621 | } | ||
| 622 | 1086 | if (!name_is_within_limit(definition->name)) { | |
| 623 | 4 | kfsw_log_error("PARAM: name '%s' is longer than %u characters", definition->name, | |
| 624 | KFSW_PARAM_NAME_MAX); | ||
| 625 | 4 | return -ENAMETOOLONG; | |
| 626 | } | ||
| 627 | 1082 | if (parameter_count >= KFSW_PARAM_MAX_DEFINITIONS) { | |
| 628 | ✗ | kfsw_log_error("PARAM: no room for %s; the table holds %u", definition->name, | |
| 629 | (unsigned int)KFSW_PARAM_MAX_DEFINITIONS); | ||
| 630 | ✗ | return -ENOSPC; | |
| 631 | } | ||
| 632 | |||
| 633 | 1082 | table = find_table(set->table); | |
| 634 | 1082 | if (table == NULL) { | |
| 635 | ✗ | return -ENOENT; | |
| 636 | } | ||
| 637 | 1082 | wire_id = KFSW_PARAM_WIRE_ID(set->table, definition->offset); | |
| 638 | |||
| 639 | /* A definition must accept its own default. */ | ||
| 640 | 1082 | if (definition->type == KFSW_PARAM_DATA) { | |
| 641 | 44 | if (definition->validate_data != NULL) { | |
| 642 | static const uint8_t zero[KFSW_PARAM_STRING_MAX]; | ||
| 643 | 68 | const uint8_t *data = (definition->default_data != NULL) | |
| 644 | ? definition->default_data | ||
| 645 | 34 | : zero; | |
| 646 | |||
| 647 | 34 | if (definition->validate_data(data, definition->capacity) != 0) { | |
| 648 | ✗ | kfsw_log_error("PARAM: %s refuses its own default", | |
| 649 | definition->name); | ||
| 650 | ✗ | return -ERANGE; | |
| 651 | } | ||
| 652 | } | ||
| 653 | 1038 | } else if (definition->type == KFSW_PARAM_STRING) { | |
| 654 | 80 | const char *text = | |
| 655 | 80 | (definition->default_text != NULL) ? definition->default_text : ""; | |
| 656 | |||
| 657 | 80 | if (copy_text(default_value.text, sizeof(default_value.text), text, | |
| 658 | 80 | sizeof(default_value.text) - 1U) >= definition->capacity) { | |
| 659 | ✗ | kfsw_log_error("PARAM: the default for %s does not fit its capacity", | |
| 660 | definition->name); | ||
| 661 | ✗ | return -ERANGE; | |
| 662 | } | ||
| 663 | 96 | if ((definition->validate_text != NULL) && | |
| 664 | 16 | (definition->validate_text(default_value.text) != 0)) { | |
| 665 | ✗ | kfsw_log_error("PARAM: %s refuses its own default", definition->name); | |
| 666 | ✗ | return -ERANGE; | |
| 667 | } | ||
| 668 | } else { | ||
| 669 | 958 | default_value.type = definition->type; | |
| 670 | 958 | default_value.size = size; | |
| 671 | 958 | default_value.scalar = definition->default_value; | |
| 672 | 1140 | if ((definition->validate != NULL) && | |
| 673 | 182 | (definition->validate(&default_value.scalar) != 0)) { | |
| 674 | ✗ | kfsw_log_error("PARAM: %s refuses its own default", definition->name); | |
| 675 | ✗ | return -ERANGE; | |
| 676 | } | ||
| 677 | } | ||
| 678 | |||
| 679 | /* Offsets are unique in a table and names on the node. */ | ||
| 680 | 17746 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 681 | 16668 | const struct kfsw_param_entry *entry = ¶meter_table[index]; | |
| 682 | |||
| 683 | 16668 | if (entry->info.id == wire_id) { | |
| 684 | 4 | kfsw_log_error("PARAM: table %u offset 0x%02x is taken by %s", set->table, | |
| 685 | definition->offset, entry->info.name); | ||
| 686 | 4 | return -EEXIST; | |
| 687 | } | ||
| 688 | 16664 | if (strcmp(entry->info.name, definition->name) == 0) { | |
| 689 | ✗ | kfsw_log_error("PARAM: name '%s' is already registered", definition->name); | |
| 690 | ✗ | return -EEXIST; | |
| 691 | } | ||
| 692 | 16664 | if ((insert_at == parameter_count) && (entry->info.id > wire_id)) { | |
| 693 | 424 | insert_at = index; | |
| 694 | } | ||
| 695 | } | ||
| 696 | |||
| 697 | 1078 | flags = definition->flags; | |
| 698 | 1078 | if ((definition->apply != NULL) || (definition->changed != NULL) || | |
| 699 | 924 | (definition->changed_text != NULL) || (definition->changed_data != NULL)) { | |
| 700 | 214 | flags |= KFSW_PARAM_FLAG_LIVE; | |
| 701 | } | ||
| 702 | |||
| 703 | 1078 | if (insert_at < parameter_count) { | |
| 704 | 424 | memmove(¶meter_table[insert_at + 1U], ¶meter_table[insert_at], | |
| 705 | 424 | (parameter_count - insert_at) * sizeof(parameter_table[0])); | |
| 706 | } | ||
| 707 | 1078 | parameter_table[insert_at] = (struct kfsw_param_entry){ | |
| 708 | .info = | ||
| 709 | { | ||
| 710 | .node = 0U, | ||
| 711 | .id = wire_id, | ||
| 712 | 1078 | .table = set->table, | |
| 713 | 1078 | .offset = definition->offset, | |
| 714 | 1078 | .array_size = ((definition->type == KFSW_PARAM_STRING) || | |
| 715 | 998 | (definition->type == KFSW_PARAM_DATA)) | |
| 716 | ? definition->capacity | ||
| 717 | : 1U, | ||
| 718 | 1078 | .type = definition->type, | |
| 719 | .flags = flags, | ||
| 720 | 1078 | .table_name = set->name, | |
| 721 | 1078 | .name = definition->name, | |
| 722 | 1078 | .unit = definition->unit, | |
| 723 | 1078 | .description = definition->description, | |
| 724 | 1078 | .read_only = (flags & KFSW_PARAM_FLAG_READ_ONLY) != 0U, | |
| 725 | }, | ||
| 726 | .definition = definition, | ||
| 727 | }; | ||
| 728 | 1078 | parameter_count++; | |
| 729 | 1078 | table->count++; | |
| 730 | 1078 | return 0; | |
| 731 | } | ||
| 732 | |||
| 733 | 144 | int kfsw_param_init(const struct kfsw_param_definition_set *const *sets, size_t set_count) | |
| 734 | { | ||
| 735 | 144 | int result = 0; | |
| 736 | |||
| 737 | 144 | if ((sets == NULL) || (set_count == 0U)) { | |
| 738 | 4 | return -EINVAL; | |
| 739 | } | ||
| 740 | |||
| 741 | 140 | kfsw_param_table_lock(); | |
| 742 | 140 | if (initialized) { | |
| 743 | 4 | kfsw_param_table_unlock(); | |
| 744 | 4 | return 0; | |
| 745 | } | ||
| 746 | |||
| 747 | 136 | parameter_count = 0U; | |
| 748 | 136 | table_count = 0U; | |
| 749 | 322 | for (size_t set_index = 0U; set_index < set_count; set_index++) { | |
| 750 | 216 | const struct kfsw_param_definition_set *set = sets[set_index]; | |
| 751 | |||
| 752 | 216 | if ((set == NULL) || (set->definitions == NULL) || (set->count == 0U)) { | |
| 753 | ✗ | result = -EINVAL; | |
| 754 | ✗ | break; | |
| 755 | } | ||
| 756 | 216 | result = add_table(set); | |
| 757 | 216 | if (result != 0) { | |
| 758 | 16 | break; | |
| 759 | } | ||
| 760 | 1278 | for (size_t definition_index = 0U; definition_index < set->count; | |
| 761 | 1078 | definition_index++) { | |
| 762 | 1092 | result = add_definition(set, &set->definitions[definition_index]); | |
| 763 | 1092 | if (result != 0) { | |
| 764 | 14 | kfsw_log_error("PARAM: table %u (%s) rejected %s: %d", set->table, | |
| 765 | set->name, set->definitions[definition_index].name, | ||
| 766 | result); | ||
| 767 | 14 | break; | |
| 768 | } | ||
| 769 | } | ||
| 770 | 200 | if (result != 0) { | |
| 771 | 14 | break; | |
| 772 | } | ||
| 773 | } | ||
| 774 | |||
| 775 | 136 | if ((result == 0) && (parameter_count == 0U)) { | |
| 776 | ✗ | result = -ENOENT; | |
| 777 | } | ||
| 778 | 136 | if (result == 0) { | |
| 779 | 1160 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 780 | 1054 | const struct kfsw_param_entry *entry = ¶meter_table[index]; | |
| 781 | |||
| 782 | 1054 | result = kfsw_param_write_default(entry); | |
| 783 | 1054 | if (result != 0) { | |
| 784 | ✗ | break; | |
| 785 | } | ||
| 786 | } | ||
| 787 | 106 | initialized = result == 0; | |
| 788 | 106 | kfsw_log_info("PARAM: %u parameters in %u tables", (unsigned int)parameter_count, | |
| 789 | (unsigned int)table_count); | ||
| 790 | } else { | ||
| 791 | 30 | parameter_count = 0U; | |
| 792 | 30 | table_count = 0U; | |
| 793 | } | ||
| 794 | 136 | kfsw_param_table_unlock(); | |
| 795 | 136 | return result; | |
| 796 | } | ||
| 797 | |||
| 798 | 2074 | bool kfsw_param_is_initialized(void) | |
| 799 | { | ||
| 800 | 2074 | return initialized; | |
| 801 | } | ||
| 802 | |||
| 803 | 526 | int kfsw_param_get(const char *name, struct kfsw_param_value *value) | |
| 804 | { | ||
| 805 | const struct kfsw_param_entry *entry; | ||
| 806 | int result; | ||
| 807 | |||
| 808 | 526 | if ((name == NULL) || (value == NULL)) { | |
| 809 | 4 | return -EINVAL; | |
| 810 | } | ||
| 811 | 522 | if (!initialized) { | |
| 812 | 6 | return -EACCES; | |
| 813 | } | ||
| 814 | |||
| 815 | 516 | kfsw_param_table_lock(); | |
| 816 | 516 | entry = kfsw_param_find_name(name); | |
| 817 | 516 | result = (entry == NULL) ? -ENOENT : kfsw_param_read_entry(entry, value); | |
| 818 | 516 | kfsw_param_table_unlock(); | |
| 819 | 516 | return result; | |
| 820 | } | ||
| 821 | |||
| 822 | 812 | int kfsw_param_get_by_id(uint16_t id, struct kfsw_param_value *value) | |
| 823 | { | ||
| 824 | const struct kfsw_param_entry *entry; | ||
| 825 | int result; | ||
| 826 | |||
| 827 | 812 | if (value == NULL) { | |
| 828 | ✗ | return -EINVAL; | |
| 829 | } | ||
| 830 | 812 | if (!initialized) { | |
| 831 | ✗ | return -EACCES; | |
| 832 | } | ||
| 833 | |||
| 834 | 812 | kfsw_param_table_lock(); | |
| 835 | 812 | entry = kfsw_param_find_id(id); | |
| 836 | 812 | result = (entry == NULL) ? -ENOENT : kfsw_param_read_entry(entry, value); | |
| 837 | 812 | kfsw_param_table_unlock(); | |
| 838 | 812 | return result; | |
| 839 | } | ||
| 840 | |||
| 841 | 358 | int kfsw_param_set(const char *name, const struct kfsw_param_value *value) | |
| 842 | { | ||
| 843 | const struct kfsw_param_entry *entry; | ||
| 844 | int result; | ||
| 845 | |||
| 846 | 358 | if ((name == NULL) || (value == NULL)) { | |
| 847 | 4 | return -EINVAL; | |
| 848 | } | ||
| 849 | 354 | if (!initialized) { | |
| 850 | 4 | return -EACCES; | |
| 851 | } | ||
| 852 | |||
| 853 | 350 | kfsw_param_table_lock(); | |
| 854 | 350 | entry = kfsw_param_find_name(name); | |
| 855 | 350 | if (entry == NULL) { | |
| 856 | 4 | result = -ENOENT; | |
| 857 | 346 | } else if (entry->info.read_only) { | |
| 858 | 48 | result = -EACCES; | |
| 859 | } else { | ||
| 860 | 298 | result = kfsw_param_validate_entry(entry, value); | |
| 861 | 298 | if (result == 0) { | |
| 862 | 208 | result = kfsw_param_wire_apply(entry, value); | |
| 863 | } | ||
| 864 | } | ||
| 865 | 350 | kfsw_param_table_unlock(); | |
| 866 | |||
| 867 | /* Log accepted and refused changes. */ | ||
| 868 | 350 | if (result == 0) { | |
| 869 | 196 | kfsw_log_info("PARAM: %s set (%s)", name, kfsw_param_mode_name(entry->info.flags)); | |
| 870 | #if CONFIG_KFSW_PARAM_PERSISTENCE | ||
| 871 | /* Save after releasing the lock, and only for persistent values. */ | ||
| 872 | 68 | if (kfsw_param_autosave_enabled() && | |
| 873 | 30 | ((entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U)) { | |
| 874 | 30 | (void)kfsw_param_persist_save(); | |
| 875 | } | ||
| 876 | #endif | ||
| 877 | } else { | ||
| 878 | 154 | kfsw_log_warning("PARAM: %s refused: %d", name, result); | |
| 879 | } | ||
| 880 | 350 | return result; | |
| 881 | } | ||
| 882 | |||
| 883 | #if CONFIG_KFSW_PARAM_PERSISTENCE | ||
| 884 | 64 | int kfsw_param_restore_defaults(void) | |
| 885 | { | ||
| 886 | 64 | int result = 0; | |
| 887 | struct kfsw_param_value value; | ||
| 888 | |||
| 889 | 64 | if (!initialized) { | |
| 890 | ✗ | return -EACCES; | |
| 891 | } | ||
| 892 | |||
| 893 | 64 | kfsw_param_table_lock(); | |
| 894 | 1366 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 895 | 1302 | const struct kfsw_param_entry *entry = ¶meter_table[index]; | |
| 896 | |||
| 897 | 1302 | if (((entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) == 0U) || | |
| 898 | 420 | ((entry->info.flags & KFSW_PARAM_FLAG_OWNER_STORE) != 0U)) { | |
| 899 | 886 | continue; | |
| 900 | } | ||
| 901 | 416 | read_default(entry, &value); | |
| 902 | 416 | result = kfsw_param_validate_entry(entry, &value); | |
| 903 | 416 | if (result != 0) { | |
| 904 | ✗ | goto out; | |
| 905 | } | ||
| 906 | } | ||
| 907 | 1366 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 908 | 1302 | const struct kfsw_param_entry *entry = ¶meter_table[index]; | |
| 909 | |||
| 910 | 1302 | if (((entry->info.flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U) && | |
| 911 | 420 | ((entry->info.flags & KFSW_PARAM_FLAG_OWNER_STORE) == 0U)) { | |
| 912 | 416 | read_default(entry, &value); | |
| 913 | 416 | result = kfsw_param_wire_apply(entry, &value); | |
| 914 | 416 | if (result != 0) { | |
| 915 | ✗ | break; | |
| 916 | } | ||
| 917 | } | ||
| 918 | } | ||
| 919 | 64 | out: | |
| 920 | 64 | kfsw_param_table_unlock(); | |
| 921 | 64 | return result; | |
| 922 | } | ||
| 923 | #endif | ||
| 924 | |||
| 925 | 844 | int kfsw_param_visit(kfsw_param_visitor_t visitor, void *context) | |
| 926 | { | ||
| 927 | 844 | if (visitor == NULL) { | |
| 928 | ✗ | return -EINVAL; | |
| 929 | } | ||
| 930 | 844 | if (!initialized) { | |
| 931 | 6 | return -EACCES; | |
| 932 | } | ||
| 933 | |||
| 934 | 838 | kfsw_param_table_lock(); | |
| 935 | 1578 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 936 | 1554 | if (!visitor(¶meter_table[index].info, context)) { | |
| 937 | 814 | break; | |
| 938 | } | ||
| 939 | } | ||
| 940 | 838 | kfsw_param_table_unlock(); | |
| 941 | 838 | return 0; | |
| 942 | } | ||
| 943 | |||
| 944 | static atomic_t param_saves; | ||
| 945 | static atomic_t param_load_failures; | ||
| 946 | static atomic_t param_save_failures; | ||
| 947 | |||
| 948 | 66 | void kfsw_param_count_save(void) | |
| 949 | { | ||
| 950 | 66 | (void)atomic_inc(¶m_saves); | |
| 951 | 66 | } | |
| 952 | |||
| 953 | 10 | void kfsw_param_count_load_failure(void) | |
| 954 | { | ||
| 955 | 10 | (void)atomic_inc(¶m_load_failures); | |
| 956 | 10 | } | |
| 957 | |||
| 958 | 6 | void kfsw_param_count_save_failure(void) | |
| 959 | { | ||
| 960 | 6 | (void)atomic_inc(¶m_save_failures); | |
| 961 | 6 | } | |
| 962 | |||
| 963 | 174 | int kfsw_param_get_stats(struct kfsw_param_stats *stats) | |
| 964 | { | ||
| 965 | 174 | if (stats == NULL) { | |
| 966 | ✗ | return -EINVAL; | |
| 967 | } | ||
| 968 | |||
| 969 | 174 | kfsw_param_table_lock(); | |
| 970 | 174 | stats->count = (uint16_t)parameter_count; | |
| 971 | 174 | stats->tables = (uint16_t)table_count; | |
| 972 | 174 | stats->persistent = 0U; | |
| 973 | 2534 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 974 | 2360 | if ((parameter_table[index].info.flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U) { | |
| 975 | 558 | stats->persistent++; | |
| 976 | } | ||
| 977 | } | ||
| 978 | 174 | kfsw_param_table_unlock(); | |
| 979 | |||
| 980 | 174 | stats->saves = (uint32_t)atomic_get(¶m_saves); | |
| 981 | 174 | stats->load_failures = (uint32_t)atomic_get(¶m_load_failures); | |
| 982 | 174 | stats->save_failures = (uint32_t)atomic_get(¶m_save_failures); | |
| 983 | 174 | stats->requests_dropped = 0U; | |
| 984 | 174 | stats->read_resends = 0U; | |
| 985 | #if CONFIG_KFSW_PARAM_CSP | ||
| 986 | 104 | stats->requests_dropped = kfsw_param_csp_dropped_requests(); | |
| 987 | 104 | stats->read_resends = kfsw_param_csp_read_resends(); | |
| 988 | #endif | ||
| 989 | 174 | return 0; | |
| 990 | } | ||
| 991 | |||
| 992 | 24 | void kfsw_param_sample_all(void) | |
| 993 | { | ||
| 994 | /* The CSP server gives libparam the storage directly, so sampled values | ||
| 995 | * are refreshed first. Caller holds the table lock. | ||
| 996 | */ | ||
| 997 | 228 | for (size_t index = 0U; index < parameter_count; index++) { | |
| 998 | 204 | const struct kfsw_param_entry *entry = ¶meter_table[index]; | |
| 999 | |||
| 1000 | 204 | if (entry->definition->sample != NULL) { | |
| 1001 | 54 | entry->definition->sample(entry->definition->value); | |
| 1002 | } | ||
| 1003 | } | ||
| 1004 | 24 | } | |
| 1005 | |||
| 1006 | 230 | int kfsw_param_get_info(const char *name, struct kfsw_param_info *info) | |
| 1007 | { | ||
| 1008 | const struct kfsw_param_entry *entry; | ||
| 1009 | int result; | ||
| 1010 | |||
| 1011 | 230 | if ((name == NULL) || (info == NULL)) { | |
| 1012 | 4 | return -EINVAL; | |
| 1013 | } | ||
| 1014 | 226 | if (!initialized) { | |
| 1015 | ✗ | return -EACCES; | |
| 1016 | } | ||
| 1017 | |||
| 1018 | 226 | kfsw_param_table_lock(); | |
| 1019 | 226 | entry = kfsw_param_find_name(name); | |
| 1020 | 226 | if (entry == NULL) { | |
| 1021 | 6 | result = -ENOENT; | |
| 1022 | } else { | ||
| 1023 | 220 | *info = entry->info; | |
| 1024 | 220 | result = 0; | |
| 1025 | } | ||
| 1026 | 226 | kfsw_param_table_unlock(); | |
| 1027 | 226 | return result; | |
| 1028 | } | ||
| 1029 | |||
| 1030 | 2 | int kfsw_param_visit_tables(kfsw_param_table_visitor_t visitor, void *context) | |
| 1031 | { | ||
| 1032 | 2 | if (visitor == NULL) { | |
| 1033 | ✗ | return -EINVAL; | |
| 1034 | } | ||
| 1035 | 2 | if (!initialized) { | |
| 1036 | ✗ | return -EACCES; | |
| 1037 | } | ||
| 1038 | |||
| 1039 | 2 | kfsw_param_table_lock(); | |
| 1040 | 30 | for (size_t index = 0U; index < table_count; index++) { | |
| 1041 | 28 | if (!visitor(¶meter_tables[index], context)) { | |
| 1042 | ✗ | break; | |
| 1043 | } | ||
| 1044 | } | ||
| 1045 | 2 | kfsw_param_table_unlock(); | |
| 1046 | 2 | return 0; | |
| 1047 | } | ||
| 1048 | |||
| 1049 | 6 | size_t kfsw_param_table_count(void) | |
| 1050 | { | ||
| 1051 | 6 | return table_count; | |
| 1052 | } | ||
| 1053 | |||
| 1054 | 284 | const char *kfsw_param_band_name(uint8_t table) | |
| 1055 | { | ||
| 1056 | 284 | if ((table >= KFSW_PARAM_TABLE_CORE_FIRST) && (table <= KFSW_PARAM_TABLE_CORE_LAST)) { | |
| 1057 | 114 | return "core"; | |
| 1058 | } | ||
| 1059 | 170 | if ((table >= KFSW_PARAM_TABLE_SERVICE_FIRST) && (table <= KFSW_PARAM_TABLE_SERVICE_LAST)) { | |
| 1060 | 154 | return "service"; | |
| 1061 | } | ||
| 1062 | 16 | if ((table >= KFSW_PARAM_TABLE_MODULE_FIRST) && (table <= KFSW_PARAM_TABLE_MODULE_LAST)) { | |
| 1063 | 10 | return "module"; | |
| 1064 | } | ||
| 1065 | 6 | return "invalid"; | |
| 1066 | } | ||
| 1067 | |||
| 1068 | /* | ||
| 1069 | * One letter per property, like libparam's mask: | ||
| 1070 | * | ||
| 1071 | * r read-only | ||
| 1072 | * w writable | ||
| 1073 | * p persistent, so it survives a reset | ||
| 1074 | * b boot: the write is read when the node next starts | ||
| 1075 | * | ||
| 1076 | * Without "b" a value applies as soon as it is set. | ||
| 1077 | */ | ||
| 1078 | 222 | const char *kfsw_param_mode_name(uint32_t flags) | |
| 1079 | { | ||
| 1080 | 222 | const bool persistent = (flags & KFSW_PARAM_FLAG_PERSISTENT) != 0U; | |
| 1081 | |||
| 1082 | /* String literals, so the shell and the CSP server can both call this. */ | ||
| 1083 | 222 | if ((flags & KFSW_PARAM_FLAG_READ_ONLY) != 0U) { | |
| 1084 | /* A read-only value is never applied, so "b" would say nothing. */ | ||
| 1085 | 10 | return persistent ? "rp" : "r"; | |
| 1086 | } | ||
| 1087 | 212 | if ((flags & KFSW_PARAM_FLAG_LIVE) != 0U) { | |
| 1088 | 116 | return persistent ? "wp" : "w"; | |
| 1089 | } | ||
| 1090 | 96 | return persistent ? "wpb" : "wb"; | |
| 1091 | } | ||
| 1092 | |||
| 1093 | ✗ | const char *kfsw_param_type_name(enum kfsw_param_type type) | |
| 1094 | { | ||
| 1095 | ✗ | switch (type) { | |
| 1096 | ✗ | case KFSW_PARAM_U8: | |
| 1097 | ✗ | return "u8"; | |
| 1098 | ✗ | case KFSW_PARAM_U16: | |
| 1099 | ✗ | return "u16"; | |
| 1100 | ✗ | case KFSW_PARAM_U32: | |
| 1101 | ✗ | return "u32"; | |
| 1102 | ✗ | case KFSW_PARAM_U64: | |
| 1103 | ✗ | return "u64"; | |
| 1104 | ✗ | case KFSW_PARAM_I8: | |
| 1105 | ✗ | return "i8"; | |
| 1106 | ✗ | case KFSW_PARAM_I16: | |
| 1107 | ✗ | return "i16"; | |
| 1108 | ✗ | case KFSW_PARAM_I32: | |
| 1109 | ✗ | return "i32"; | |
| 1110 | ✗ | case KFSW_PARAM_I64: | |
| 1111 | ✗ | return "i64"; | |
| 1112 | ✗ | case KFSW_PARAM_X8: | |
| 1113 | ✗ | return "x8"; | |
| 1114 | ✗ | case KFSW_PARAM_X16: | |
| 1115 | ✗ | return "x16"; | |
| 1116 | ✗ | case KFSW_PARAM_X32: | |
| 1117 | ✗ | return "x32"; | |
| 1118 | ✗ | case KFSW_PARAM_X64: | |
| 1119 | ✗ | return "x64"; | |
| 1120 | ✗ | case KFSW_PARAM_FLOAT: | |
| 1121 | ✗ | return "float"; | |
| 1122 | ✗ | case KFSW_PARAM_DOUBLE: | |
| 1123 | ✗ | return "double"; | |
| 1124 | ✗ | case KFSW_PARAM_STRING: | |
| 1125 | ✗ | return "string"; | |
| 1126 | ✗ | case KFSW_PARAM_DATA: | |
| 1127 | ✗ | return "data"; | |
| 1128 | ✗ | case KFSW_PARAM_INVALID: | |
| 1129 | default: | ||
| 1130 | ✗ | return "invalid"; | |
| 1131 | } | ||
| 1132 | } | ||
| 1133 |