kfsw-services/src/command/command.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <ctype.h> | ||
| 2 | #include <errno.h> | ||
| 3 | #include <stdbool.h> | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdio.h> | ||
| 7 | #include <stdlib.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | #include <zephyr/kernel.h> | ||
| 11 | #include <zephyr/sys/atomic.h> | ||
| 12 | #include <zephyr/sys/byteorder.h> | ||
| 13 | #include <zephyr/sys/util.h> | ||
| 14 | |||
| 15 | #include <kfsw/services/command.h> | ||
| 16 | #if CONFIG_KFSW_EVENT | ||
| 17 | #include <kfsw/services/event.h> | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #include "command_internal.h" | ||
| 21 | |||
| 22 | /* The registry: definition sets checked once at startup and then fixed. */ | ||
| 23 | |||
| 24 | static const struct kfsw_command_definition *registry[CONFIG_KFSW_COMMAND_MAX_COMMANDS]; | ||
| 25 | static size_t registry_count; | ||
| 26 | static bool registry_ready; | ||
| 27 | |||
| 28 | /* Serializes invocation so a handler never runs concurrently with itself. */ | ||
| 29 | K_MUTEX_DEFINE(command_lock); | ||
| 30 | |||
| 31 | /* | ||
| 32 | * The claim. Held by one thread across a sequence of invocations, and read on | ||
| 33 | * every dispatch, so it has its own mutex: it must never be held while a | ||
| 34 | * handler runs, or an exclusive claim would block the very invocation it is | ||
| 35 | * meant to refuse. | ||
| 36 | */ | ||
| 37 | static K_MUTEX_DEFINE(claim_lock); | ||
| 38 | static struct { | ||
| 39 | k_tid_t owner; | ||
| 40 | enum kfsw_command_claim_mode mode; | ||
| 41 | char name[KFSW_COMMAND_CLAIM_NAME_MAX]; | ||
| 42 | uint32_t intrusions; | ||
| 43 | uint32_t refusals; | ||
| 44 | bool held; | ||
| 45 | } claim; | ||
| 46 | |||
| 47 | /* Lifetime totals. */ | ||
| 48 | static atomic_t command_invoked; | ||
| 49 | static atomic_t command_failed; | ||
| 50 | static atomic_t command_unknown; | ||
| 51 | static atomic_t command_rejected; | ||
| 52 | |||
| 53 | #if CONFIG_KFSW_COMMAND_CSP | ||
| 54 | /* Timeout for the next remote invocation. */ | ||
| 55 | static uint32_t command_timeout_ms = CONFIG_KFSW_COMMAND_TIMEOUT_MS; | ||
| 56 | #endif | ||
| 57 | |||
| 58 | /* Console echo, off by default. The application applies it through a hook. */ | ||
| 59 | static uint8_t command_echo_enabled; | ||
| 60 | static kfsw_command_echo_handler_t command_echo_handler; | ||
| 61 | |||
| 62 | 372 | static const struct kfsw_command_definition *find_by_id(uint16_t id) | |
| 63 | { | ||
| 64 | 500 | for (size_t index = 0U; index < registry_count; index++) { | |
| 65 | 398 | if (registry[index]->id == id) { | |
| 66 | 270 | return registry[index]; | |
| 67 | } | ||
| 68 | } | ||
| 69 | 102 | return NULL; | |
| 70 | } | ||
| 71 | |||
| 72 | 490 | static const struct kfsw_command_definition *find_by_name(const char *name) | |
| 73 | { | ||
| 74 | 664 | for (size_t index = 0U; index < registry_count; index++) { | |
| 75 | 568 | if (strcmp(registry[index]->name, name) == 0) { | |
| 76 | 394 | return registry[index]; | |
| 77 | } | ||
| 78 | } | ||
| 79 | 96 | return NULL; | |
| 80 | } | ||
| 81 | |||
| 82 | 102 | static int validate_definition(const struct kfsw_command_definition *definition) | |
| 83 | { | ||
| 84 | 102 | if ((definition->name == NULL) || (definition->name[0] == '\0') || | |
| 85 | 102 | (definition->handler == NULL)) { | |
| 86 | 4 | return -EINVAL; | |
| 87 | } | ||
| 88 | 98 | if (definition->arg_count > KFSW_COMMAND_MAX_ARGS) { | |
| 89 | 4 | return -E2BIG; | |
| 90 | } | ||
| 91 | 94 | if ((definition->arg_count != 0U) && (definition->arg_types == NULL)) { | |
| 92 | 4 | return -EINVAL; | |
| 93 | } | ||
| 94 | /* Bounded check without strnlen, which the minimal libc does not provide. */ | ||
| 95 | 90 | if (memchr(definition->name, '\0', KFSW_COMMAND_MAX_TEXT_SIZE) == NULL) { | |
| 96 | ✗ | return -ENAMETOOLONG; | |
| 97 | } | ||
| 98 | /* A duplicate id or name would make dispatch ambiguous between front ends. */ | ||
| 99 | 90 | if ((find_by_id(definition->id) != NULL) || (find_by_name(definition->name) != NULL)) { | |
| 100 | 8 | return -EEXIST; | |
| 101 | } | ||
| 102 | 82 | return 0; | |
| 103 | } | ||
| 104 | |||
| 105 | 58 | int kfsw_command_init(const struct kfsw_command_definition_set *const *sets, size_t set_count) | |
| 106 | { | ||
| 107 | 58 | if ((sets == NULL) && (set_count != 0U)) { | |
| 108 | ✗ | return -EINVAL; | |
| 109 | } | ||
| 110 | 58 | registry_count = 0U; | |
| 111 | 58 | registry_ready = false; | |
| 112 | |||
| 113 | 96 | for (size_t set_index = 0U; set_index < set_count; set_index++) { | |
| 114 | 58 | const struct kfsw_command_definition_set *set = sets[set_index]; | |
| 115 | |||
| 116 | 58 | if (set == NULL) { | |
| 117 | ✗ | return -EINVAL; | |
| 118 | } | ||
| 119 | 58 | if ((set->count != 0U) && (set->commands == NULL)) { | |
| 120 | ✗ | return -EINVAL; | |
| 121 | } | ||
| 122 | 140 | for (size_t index = 0U; index < set->count; index++) { | |
| 123 | 102 | const struct kfsw_command_definition *definition = &set->commands[index]; | |
| 124 | 102 | int result = validate_definition(definition); | |
| 125 | |||
| 126 | 102 | if (result != 0) { | |
| 127 | 20 | registry_count = 0U; | |
| 128 | 20 | return result; | |
| 129 | } | ||
| 130 | 82 | if (registry_count == ARRAY_SIZE(registry)) { | |
| 131 | ✗ | registry_count = 0U; | |
| 132 | ✗ | return -ENOSPC; | |
| 133 | } | ||
| 134 | 82 | registry[registry_count] = definition; | |
| 135 | 82 | registry_count++; | |
| 136 | } | ||
| 137 | } | ||
| 138 | 38 | registry_ready = true; | |
| 139 | 38 | return 0; | |
| 140 | } | ||
| 141 | |||
| 142 | 20 | bool kfsw_command_is_initialized(void) | |
| 143 | { | ||
| 144 | 20 | return registry_ready; | |
| 145 | } | ||
| 146 | |||
| 147 | 322 | static void describe(const struct kfsw_command_definition *definition, | |
| 148 | struct kfsw_command_info *info) | ||
| 149 | { | ||
| 150 | 322 | info->id = definition->id; | |
| 151 | 322 | info->name = definition->name; | |
| 152 | 322 | info->help = definition->help; | |
| 153 | 322 | info->flags = definition->flags; | |
| 154 | 322 | info->arg_count = definition->arg_count; | |
| 155 | 322 | info->arg_types = definition->arg_types; | |
| 156 | 322 | } | |
| 157 | |||
| 158 | 4 | void kfsw_command_visit(kfsw_command_visitor_t visitor, void *context) | |
| 159 | { | ||
| 160 | 4 | if ((visitor == NULL) || !registry_ready) { | |
| 161 | ✗ | return; | |
| 162 | } | ||
| 163 | 20 | for (size_t index = 0U; index < registry_count; index++) { | |
| 164 | struct kfsw_command_info info; | ||
| 165 | |||
| 166 | 16 | describe(registry[index], &info); | |
| 167 | 16 | if (!visitor(&info, context)) { | |
| 168 | ✗ | return; | |
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | 146 | int kfsw_command_parse_arg(const char *text, enum kfsw_command_type type, | |
| 174 | struct kfsw_command_arg *arg) | ||
| 175 | { | ||
| 176 | char *end; | ||
| 177 | |||
| 178 | 146 | if ((text == NULL) || (arg == NULL)) { | |
| 179 | ✗ | return -EINVAL; | |
| 180 | } | ||
| 181 | 146 | arg->type = type; | |
| 182 | 146 | switch (type) { | |
| 183 | 122 | case KFSW_COMMAND_TYPE_U32: { | |
| 184 | 122 | const char *number = text; | |
| 185 | unsigned long parsed; | ||
| 186 | |||
| 187 | 130 | while (isspace((unsigned char)*number)) { | |
| 188 | 8 | number++; | |
| 189 | } | ||
| 190 | 122 | if (*number == '-') { | |
| 191 | 20 | return -EINVAL; | |
| 192 | } | ||
| 193 | 102 | errno = 0; | |
| 194 | 102 | parsed = strtoul(number, &end, 0); | |
| 195 | 102 | if ((errno == ERANGE) || (end == number) || (*end != '\0') || | |
| 196 | (parsed > UINT32_MAX)) { | ||
| 197 | 20 | return -EINVAL; | |
| 198 | } | ||
| 199 | 82 | arg->value.u32 = (uint32_t)parsed; | |
| 200 | 82 | break; | |
| 201 | } | ||
| 202 | 24 | case KFSW_COMMAND_TYPE_I32: { | |
| 203 | long parsed; | ||
| 204 | |||
| 205 | 24 | errno = 0; | |
| 206 | 24 | parsed = strtol(text, &end, 0); | |
| 207 | 24 | if ((errno == ERANGE) || (end == text) || (*end != '\0') || (parsed > INT32_MAX) || | |
| 208 | (parsed < INT32_MIN)) { | ||
| 209 | 16 | return -EINVAL; | |
| 210 | } | ||
| 211 | 8 | arg->value.i32 = (int32_t)parsed; | |
| 212 | 8 | break; | |
| 213 | } | ||
| 214 | ✗ | case KFSW_COMMAND_TYPE_TEXT: | |
| 215 | /* Bounded length check; the minimal libc has no strnlen. */ | ||
| 216 | ✗ | if (memchr(text, '\0', KFSW_COMMAND_MAX_TEXT_SIZE + 1U) == NULL) { | |
| 217 | ✗ | return -ENAMETOOLONG; | |
| 218 | } | ||
| 219 | /* Not copied: the caller keeps the text valid during the invocation. */ | ||
| 220 | ✗ | arg->value.text = text; | |
| 221 | ✗ | break; | |
| 222 | ✗ | default: | |
| 223 | ✗ | return -ENOTSUP; | |
| 224 | } | ||
| 225 | 90 | return 0; | |
| 226 | } | ||
| 227 | |||
| 228 | 320 | int kfsw_command_find(const char *name, struct kfsw_command_info *info) | |
| 229 | { | ||
| 230 | const struct kfsw_command_definition *definition; | ||
| 231 | |||
| 232 | 320 | if ((name == NULL) || (info == NULL)) { | |
| 233 | 4 | return -EINVAL; | |
| 234 | } | ||
| 235 | 316 | if (!registry_ready) { | |
| 236 | ✗ | return -EACCES; | |
| 237 | } | ||
| 238 | 316 | definition = find_by_name(name); | |
| 239 | 316 | if (definition == NULL) { | |
| 240 | 10 | return -ENOENT; | |
| 241 | } | ||
| 242 | 306 | describe(definition, info); | |
| 243 | 306 | return 0; | |
| 244 | } | ||
| 245 | |||
| 246 | /* | ||
| 247 | * Validate argument count and types before calling the handler. | ||
| 248 | */ | ||
| 249 | 350 | static enum kfsw_command_status check_arguments(const struct kfsw_command_definition *definition, | |
| 250 | const struct kfsw_command_arg *args, | ||
| 251 | size_t arg_count) | ||
| 252 | { | ||
| 253 | 350 | if (arg_count != definition->arg_count) { | |
| 254 | 30 | return KFSW_COMMAND_INVALID_ARGUMENT; | |
| 255 | } | ||
| 256 | 554 | for (size_t index = 0U; index < arg_count; index++) { | |
| 257 | 254 | if (args[index].type != definition->arg_types[index]) { | |
| 258 | 16 | return KFSW_COMMAND_INVALID_ARGUMENT; | |
| 259 | } | ||
| 260 | 238 | if ((args[index].type == KFSW_COMMAND_TYPE_TEXT) && | |
| 261 | 192 | (args[index].value.text == NULL)) { | |
| 262 | 4 | return KFSW_COMMAND_INVALID_ARGUMENT; | |
| 263 | } | ||
| 264 | } | ||
| 265 | 300 | return KFSW_COMMAND_OK; | |
| 266 | } | ||
| 267 | |||
| 268 | /* Record every dispatch as an event. */ | ||
| 269 | 370 | static void record_outcome(uint16_t event_id, uint16_t command_id, | |
| 270 | const struct kfsw_command_source *source, | ||
| 271 | enum kfsw_command_status status) | ||
| 272 | { | ||
| 273 | /* Counted even without the event record. */ | ||
| 274 | 370 | (void)atomic_inc(&command_invoked); | |
| 275 | 370 | if (status == KFSW_COMMAND_UNKNOWN) { | |
| 276 | 20 | (void)atomic_inc(&command_unknown); | |
| 277 | 350 | } else if (status == KFSW_COMMAND_DENIED) { | |
| 278 | 88 | (void)atomic_inc(&command_rejected); | |
| 279 | 262 | } else if (status != KFSW_COMMAND_OK) { | |
| 280 | 94 | (void)atomic_inc(&command_failed); | |
| 281 | } | ||
| 282 | |||
| 283 | #if CONFIG_KFSW_EVENT | ||
| 284 | uint8_t payload[5]; | ||
| 285 | |||
| 286 | 128 | sys_put_be16(command_id, &payload[0]); | |
| 287 | 128 | sys_put_be16(source->node, &payload[2]); | |
| 288 | 128 | payload[4] = (uint8_t)status; | |
| 289 | 128 | kfsw_event_emit(KFSW_EVENT_SOURCE_COMMAND, event_id, | |
| 290 | (status == KFSW_COMMAND_OK) ? KFSW_EVENT_INFO : KFSW_EVENT_WARNING, payload, | ||
| 291 | sizeof(payload)); | ||
| 292 | #else | ||
| 293 | ARG_UNUSED(event_id); | ||
| 294 | ARG_UNUSED(command_id); | ||
| 295 | ARG_UNUSED(source); | ||
| 296 | ARG_UNUSED(status); | ||
| 297 | #endif | ||
| 298 | 370 | } | |
| 299 | |||
| 300 | /* | ||
| 301 | * An invocation from outside the claim holder takes precedence over the | ||
| 302 | * holder's sequence: it is served as soon as the handler in flight returns, | ||
| 303 | * never after the whole sequence. A holder that cannot survive that claims | ||
| 304 | * exclusively and the invocation is refused instead. | ||
| 305 | */ | ||
| 306 | 300 | static int check_claim(const struct kfsw_command_definition *definition, | |
| 307 | struct kfsw_command_result *result) | ||
| 308 | { | ||
| 309 | 300 | int outcome = 0; | |
| 310 | |||
| 311 | 300 | k_mutex_lock(&claim_lock, K_FOREVER); | |
| 312 | 300 | if (claim.held && (claim.owner != k_current_get())) { | |
| 313 | 4 | if (claim.mode == KFSW_COMMAND_CLAIM_EXCLUSIVE) { | |
| 314 | 2 | if (claim.refusals < UINT32_MAX) { | |
| 315 | 2 | claim.refusals++; | |
| 316 | } | ||
| 317 | 2 | result->status = KFSW_COMMAND_BUSY; | |
| 318 | 2 | (void)snprintf(result->detail, sizeof(result->detail), | |
| 319 | 2 | "%s refused while %s runs", definition->name, claim.name); | |
| 320 | 2 | outcome = -EBUSY; | |
| 321 | 2 | } else if (claim.intrusions < UINT32_MAX) { | |
| 322 | 2 | claim.intrusions++; | |
| 323 | } | ||
| 324 | } | ||
| 325 | 300 | k_mutex_unlock(&claim_lock); | |
| 326 | 300 | return outcome; | |
| 327 | } | ||
| 328 | |||
| 329 | 68 | int kfsw_command_claim_acquire(enum kfsw_command_claim_mode mode, const char *owner) | |
| 330 | { | ||
| 331 | 68 | if ((owner == NULL) || (owner[0] == '\0') || | |
| 332 | 66 | (memchr(owner, '\0', KFSW_COMMAND_CLAIM_NAME_MAX) == NULL)) { | |
| 333 | 2 | return -EINVAL; | |
| 334 | } | ||
| 335 | 66 | k_mutex_lock(&claim_lock, K_FOREVER); | |
| 336 | 66 | if (claim.held && (claim.owner != k_current_get())) { | |
| 337 | ✗ | k_mutex_unlock(&claim_lock); | |
| 338 | ✗ | return -EBUSY; | |
| 339 | } | ||
| 340 | 66 | if (!claim.held) { | |
| 341 | 64 | claim.intrusions = 0U; | |
| 342 | 64 | claim.refusals = 0U; | |
| 343 | } | ||
| 344 | 66 | claim.owner = k_current_get(); | |
| 345 | 66 | claim.mode = mode; | |
| 346 | 66 | strcpy(claim.name, owner); | |
| 347 | 66 | claim.held = true; | |
| 348 | 66 | k_mutex_unlock(&claim_lock); | |
| 349 | 66 | return 0; | |
| 350 | } | ||
| 351 | |||
| 352 | 68 | int kfsw_command_claim_release(void) | |
| 353 | { | ||
| 354 | 68 | int outcome = 0; | |
| 355 | |||
| 356 | 68 | k_mutex_lock(&claim_lock, K_FOREVER); | |
| 357 | 68 | if (claim.held && (claim.owner != k_current_get())) { | |
| 358 | ✗ | outcome = -EPERM; | |
| 359 | } else { | ||
| 360 | 68 | claim.held = false; | |
| 361 | 68 | claim.owner = NULL; | |
| 362 | } | ||
| 363 | 68 | k_mutex_unlock(&claim_lock); | |
| 364 | 68 | return outcome; | |
| 365 | } | ||
| 366 | |||
| 367 | 142 | int kfsw_command_claim_get(struct kfsw_command_claim_state *state) | |
| 368 | { | ||
| 369 | 142 | if (state == NULL) { | |
| 370 | ✗ | return -EINVAL; | |
| 371 | } | ||
| 372 | 142 | k_mutex_lock(&claim_lock, K_FOREVER); | |
| 373 | 142 | state->held = claim.held; | |
| 374 | 142 | state->mode = claim.mode; | |
| 375 | 142 | state->intrusions = claim.intrusions; | |
| 376 | 142 | state->refusals = claim.refusals; | |
| 377 | 142 | strcpy(state->owner, claim.held ? claim.name : ""); | |
| 378 | 142 | k_mutex_unlock(&claim_lock); | |
| 379 | 142 | return 0; | |
| 380 | } | ||
| 381 | |||
| 382 | 370 | static int dispatch(const struct kfsw_command_definition *definition, | |
| 383 | const struct kfsw_command_arg *args, size_t arg_count, | ||
| 384 | const struct kfsw_command_source *source, struct kfsw_command_result *result) | ||
| 385 | { | ||
| 386 | int handler_result; | ||
| 387 | |||
| 388 | 370 | memset(result, 0, sizeof(*result)); | |
| 389 | 370 | if (!registry_ready) { | |
| 390 | ✗ | result->status = KFSW_COMMAND_UNAVAILABLE; | |
| 391 | ✗ | return -EACCES; | |
| 392 | } | ||
| 393 | 370 | if (definition == NULL) { | |
| 394 | 20 | result->status = KFSW_COMMAND_UNKNOWN; | |
| 395 | 20 | record_outcome(KFSW_EVENT_COMMAND_UNKNOWN, 0U, source, result->status); | |
| 396 | 20 | return -ENOENT; | |
| 397 | } | ||
| 398 | 350 | result->status = check_arguments(definition, args, arg_count); | |
| 399 | 350 | if (result->status != KFSW_COMMAND_OK) { | |
| 400 | 50 | record_outcome(KFSW_EVENT_COMMAND_REJECTED, definition->id, source, result->status); | |
| 401 | 50 | return -EINVAL; | |
| 402 | } | ||
| 403 | 300 | if (check_claim(definition, result) != 0) { | |
| 404 | 2 | record_outcome(KFSW_EVENT_COMMAND_REJECTED, definition->id, source, result->status); | |
| 405 | 2 | return -EBUSY; | |
| 406 | } | ||
| 407 | |||
| 408 | 298 | k_mutex_lock(&command_lock, K_FOREVER); | |
| 409 | 298 | handler_result = definition->handler(args, arg_count, source, result); | |
| 410 | 298 | k_mutex_unlock(&command_lock); | |
| 411 | |||
| 412 | /* Use FAILED when a handler returns an error without setting a status. */ | ||
| 413 | 298 | if ((handler_result != 0) && (result->status == KFSW_COMMAND_OK)) { | |
| 414 | 4 | result->status = KFSW_COMMAND_FAILED; | |
| 415 | } | ||
| 416 | 298 | record_outcome(KFSW_EVENT_COMMAND_INVOKED, definition->id, source, result->status); | |
| 417 | 298 | return handler_result; | |
| 418 | } | ||
| 419 | |||
| 420 | 96 | int kfsw_command_invoke(const char *name, const struct kfsw_command_arg *args, size_t arg_count, | |
| 421 | struct kfsw_command_result *result) | ||
| 422 | { | ||
| 423 | 96 | const struct kfsw_command_source source = {.node = 0U, .authenticated = false}; | |
| 424 | |||
| 425 | 96 | if ((name == NULL) || (result == NULL)) { | |
| 426 | 8 | return -EINVAL; | |
| 427 | } | ||
| 428 | 88 | return dispatch(find_by_name(name), args, arg_count, &source, result); | |
| 429 | } | ||
| 430 | |||
| 431 | 282 | int kfsw_command_invoke_id(uint16_t id, const struct kfsw_command_arg *args, size_t arg_count, | |
| 432 | const struct kfsw_command_source *source, | ||
| 433 | struct kfsw_command_result *result) | ||
| 434 | { | ||
| 435 | 282 | const struct kfsw_command_source local = {.node = 0U, .authenticated = false}; | |
| 436 | |||
| 437 | 282 | if (result == NULL) { | |
| 438 | ✗ | return -EINVAL; | |
| 439 | } | ||
| 440 | 282 | return dispatch(find_by_id(id), args, arg_count, (source != NULL) ? source : &local, | |
| 441 | result); | ||
| 442 | } | ||
| 443 | |||
| 444 | ✗ | int kfsw_command_lookup_id(const char *name, uint16_t *id) | |
| 445 | { | ||
| 446 | const struct kfsw_command_definition *definition; | ||
| 447 | |||
| 448 | ✗ | if ((name == NULL) || (id == NULL)) { | |
| 449 | ✗ | return -EINVAL; | |
| 450 | } | ||
| 451 | ✗ | if (!registry_ready) { | |
| 452 | ✗ | return -EACCES; | |
| 453 | } | ||
| 454 | ✗ | definition = find_by_name(name); | |
| 455 | ✗ | if (definition == NULL) { | |
| 456 | ✗ | return -ENOENT; | |
| 457 | } | ||
| 458 | ✗ | *id = definition->id; | |
| 459 | ✗ | return 0; | |
| 460 | } | ||
| 461 | |||
| 462 | 4 | int kfsw_command_get_stats(struct kfsw_command_stats *stats) | |
| 463 | { | ||
| 464 | 4 | if (stats == NULL) { | |
| 465 | ✗ | return -EINVAL; | |
| 466 | } | ||
| 467 | |||
| 468 | 4 | stats->invoked = (uint32_t)atomic_get(&command_invoked); | |
| 469 | 4 | stats->failed = (uint32_t)atomic_get(&command_failed); | |
| 470 | 4 | stats->unknown = (uint32_t)atomic_get(&command_unknown); | |
| 471 | 4 | stats->rejected = (uint32_t)atomic_get(&command_rejected); | |
| 472 | 4 | stats->registered = (uint16_t)registry_count; | |
| 473 | 4 | return 0; | |
| 474 | } | ||
| 475 | |||
| 476 | #if CONFIG_KFSW_COMMAND_CSP | ||
| 477 | 30 | uint32_t kfsw_command_get_timeout_ms(void) | |
| 478 | { | ||
| 479 | 30 | return command_timeout_ms; | |
| 480 | } | ||
| 481 | |||
| 482 | 14 | int kfsw_command_check_timeout_ms(uint32_t timeout_ms) | |
| 483 | { | ||
| 484 | /* Same range as Kconfig. Checked separately because a change callback | ||
| 485 | * can't refuse a value. | ||
| 486 | */ | ||
| 487 | 14 | if ((timeout_ms < KFSW_COMMAND_TIMEOUT_MIN_MS) || | |
| 488 | (timeout_ms > KFSW_COMMAND_TIMEOUT_MAX_MS)) { | ||
| 489 | ✗ | return -ERANGE; | |
| 490 | } | ||
| 491 | 14 | return 0; | |
| 492 | } | ||
| 493 | |||
| 494 | 6 | int kfsw_command_set_timeout_ms(uint32_t timeout_ms) | |
| 495 | { | ||
| 496 | 6 | int result = kfsw_command_check_timeout_ms(timeout_ms); | |
| 497 | |||
| 498 | 6 | if (result != 0) { | |
| 499 | ✗ | return result; | |
| 500 | } | ||
| 501 | 6 | command_timeout_ms = timeout_ms; | |
| 502 | 6 | return 0; | |
| 503 | } | ||
| 504 | #endif | ||
| 505 | |||
| 506 | 8 | bool kfsw_command_echo_enabled(void) | |
| 507 | { | ||
| 508 | 8 | return command_echo_enabled != 0U; | |
| 509 | } | ||
| 510 | |||
| 511 | ✗ | void kfsw_command_set_echo_handler(kfsw_command_echo_handler_t handler) | |
| 512 | { | ||
| 513 | ✗ | command_echo_handler = handler; | |
| 514 | |||
| 515 | /* Apply the default as soon as a console exists. */ | ||
| 516 | ✗ | if (handler != NULL) { | |
| 517 | ✗ | handler(command_echo_enabled != 0U); | |
| 518 | } | ||
| 519 | ✗ | } | |
| 520 | |||
| 521 | 8 | void kfsw_command_set_echo(bool enabled) | |
| 522 | { | ||
| 523 | 8 | command_echo_enabled = enabled ? 1U : 0U; | |
| 524 | 8 | if (command_echo_handler != NULL) { | |
| 525 | ✗ | command_echo_handler(enabled); | |
| 526 | } | ||
| 527 | 8 | } | |
| 528 | |||
| 529 | ✗ | const char *kfsw_command_status_name(enum kfsw_command_status status) | |
| 530 | { | ||
| 531 | ✗ | switch (status) { | |
| 532 | ✗ | case KFSW_COMMAND_OK: | |
| 533 | ✗ | return "ok"; | |
| 534 | ✗ | case KFSW_COMMAND_UNKNOWN: | |
| 535 | ✗ | return "unknown command"; | |
| 536 | ✗ | case KFSW_COMMAND_INVALID_ARGUMENT: | |
| 537 | ✗ | return "invalid argument"; | |
| 538 | ✗ | case KFSW_COMMAND_DENIED: | |
| 539 | ✗ | return "denied"; | |
| 540 | ✗ | case KFSW_COMMAND_BUSY: | |
| 541 | ✗ | return "busy"; | |
| 542 | ✗ | case KFSW_COMMAND_UNAVAILABLE: | |
| 543 | ✗ | return "service unavailable"; | |
| 544 | ✗ | case KFSW_COMMAND_FAILED: | |
| 545 | default: | ||
| 546 | ✗ | return "failed"; | |
| 547 | } | ||
| 548 | } | ||
| 549 |