kfsw-services/src/fbo/fbo_commands.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <inttypes.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/sys/util.h> | ||
| 11 | |||
| 12 | #include <kfsw/services/command.h> | ||
| 13 | #include <kfsw/services/fbo.h> | ||
| 14 | |||
| 15 | #include "fbo_internal.h" | ||
| 16 | |||
| 17 | /* | ||
| 18 | * The ground reaches procedures and the queue through the command registry, | ||
| 19 | * the same path the shell uses, so there is one set of rules for both. | ||
| 20 | */ | ||
| 21 | |||
| 22 | /* `+N` is N seconds from now, `@N` is the UTC second N. A bare number is | ||
| 23 | * refused: the two are not interchangeable, and guessing picks the dangerous | ||
| 24 | * one. | ||
| 25 | */ | ||
| 26 | ✗ | static int parse_when(const char *text, enum kfsw_fbo_time_kind *kind, int64_t *when, | |
| 27 | struct kfsw_command_result *result) | ||
| 28 | { | ||
| 29 | char *end; | ||
| 30 | long long parsed; | ||
| 31 | |||
| 32 | ✗ | if ((text[0] != '+') && (text[0] != '@')) { | |
| 33 | ✗ | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 34 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), | |
| 35 | "write +seconds from now or @utc second"); | ||
| 36 | ✗ | return -EINVAL; | |
| 37 | } | ||
| 38 | ✗ | *kind = (text[0] == '+') ? KFSW_FBO_TIME_RELATIVE : KFSW_FBO_TIME_ABSOLUTE; | |
| 39 | ✗ | parsed = strtoll(&text[1], &end, 10); | |
| 40 | ✗ | if ((end == &text[1]) || (*end != '\0') || (parsed <= 0)) { | |
| 41 | ✗ | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 42 | ✗ | return -EINVAL; | |
| 43 | } | ||
| 44 | ✗ | *when = (int64_t)parsed; | |
| 45 | ✗ | return 0; | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | static void say(struct kfsw_command_result *result, int outcome, const char *reason) | |
| 49 | { | ||
| 50 | ✗ | switch (outcome) { | |
| 51 | ✗ | case -ENODATA: | |
| 52 | ✗ | result->status = KFSW_COMMAND_UNAVAILABLE; | |
| 53 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), | |
| 54 | "the clock is not set, so absolute scheduling is unavailable"); | ||
| 55 | ✗ | break; | |
| 56 | ✗ | case -ENOSPC: | |
| 57 | ✗ | result->status = KFSW_COMMAND_BUSY; | |
| 58 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "the queue is full"); | |
| 59 | ✗ | break; | |
| 60 | ✗ | case -EACCES: | |
| 61 | ✗ | result->status = KFSW_COMMAND_DENIED; | |
| 62 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "%s", reason); | |
| 63 | ✗ | break; | |
| 64 | ✗ | case -EALREADY: | |
| 65 | ✗ | result->status = KFSW_COMMAND_BUSY; | |
| 66 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "the entry has already run"); | |
| 67 | ✗ | break; | |
| 68 | ✗ | case -ENOENT: | |
| 69 | ✗ | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 70 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "%s", reason); | |
| 71 | ✗ | break; | |
| 72 | ✗ | default: | |
| 73 | ✗ | result->status = KFSW_COMMAND_FAILED; | |
| 74 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "%s (%d)", reason, outcome); | |
| 75 | ✗ | break; | |
| 76 | } | ||
| 77 | ✗ | } | |
| 78 | |||
| 79 | ✗ | static int command_fbo_run(const struct kfsw_command_arg *args, size_t count, | |
| 80 | const struct kfsw_command_source *source, | ||
| 81 | struct kfsw_command_result *result) | ||
| 82 | { | ||
| 83 | int outcome; | ||
| 84 | |||
| 85 | ARG_UNUSED(count); | ||
| 86 | ARG_UNUSED(source); | ||
| 87 | |||
| 88 | ✗ | outcome = kfsw_fbo_run(args[0].value.text); | |
| 89 | ✗ | if (outcome == -EBUSY) { | |
| 90 | ✗ | result->status = KFSW_COMMAND_BUSY; | |
| 91 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), | |
| 92 | "a procedure is already running"); | ||
| 93 | ✗ | return outcome; | |
| 94 | } | ||
| 95 | ✗ | if (outcome != 0) { | |
| 96 | ✗ | say(result, outcome, "the procedure could not be started"); | |
| 97 | ✗ | return outcome; | |
| 98 | } | ||
| 99 | ✗ | result->status = KFSW_COMMAND_OK; | |
| 100 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "started=%s", args[0].value.text); | |
| 101 | ✗ | return 0; | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | static int command_sched_add(const struct kfsw_command_arg *args, size_t count, | |
| 105 | const struct kfsw_command_source *source, | ||
| 106 | struct kfsw_command_result *result) | ||
| 107 | { | ||
| 108 | struct kfsw_fbo_schedule_status status; | ||
| 109 | enum kfsw_fbo_time_kind kind; | ||
| 110 | int64_t when; | ||
| 111 | ✗ | uint16_t index = 0U; | |
| 112 | int outcome; | ||
| 113 | |||
| 114 | ARG_UNUSED(count); | ||
| 115 | ARG_UNUSED(source); | ||
| 116 | |||
| 117 | ✗ | outcome = parse_when(args[0].value.text, &kind, &when, result); | |
| 118 | ✗ | if (outcome != 0) { | |
| 119 | ✗ | return outcome; | |
| 120 | } | ||
| 121 | ✗ | if (args[1].value.u32 > UINT16_MAX) { | |
| 122 | ✗ | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 123 | ✗ | return -EINVAL; | |
| 124 | } | ||
| 125 | ✗ | outcome = kfsw_fbo_schedule_add(kind, when, (uint16_t)args[1].value.u32, args[2].value.text, | |
| 126 | &index); | ||
| 127 | ✗ | if (outcome == -EEXIST) { | |
| 128 | /* The same content is already queued, so the upload was a repeat | ||
| 129 | * and the entry it names is the one that will run. | ||
| 130 | */ | ||
| 131 | ✗ | result->status = KFSW_COMMAND_OK; | |
| 132 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "index=%u repeated=yes", | |
| 133 | index); | ||
| 134 | ✗ | return 0; | |
| 135 | } | ||
| 136 | ✗ | if (outcome != 0) { | |
| 137 | ✗ | say(result, outcome, "the entry was refused"); | |
| 138 | ✗ | return outcome; | |
| 139 | } | ||
| 140 | ✗ | (void)kfsw_fbo_schedule_get_status(&status); | |
| 141 | ✗ | result->status = KFSW_COMMAND_OK; | |
| 142 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "index=%u queue=0x%08x", index, | |
| 143 | status.hash); | ||
| 144 | ✗ | return 0; | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | static int command_sched_cancel(const struct kfsw_command_arg *args, size_t count, | |
| 148 | const struct kfsw_command_source *source, | ||
| 149 | struct kfsw_command_result *result) | ||
| 150 | { | ||
| 151 | int outcome; | ||
| 152 | |||
| 153 | ARG_UNUSED(count); | ||
| 154 | ARG_UNUSED(source); | ||
| 155 | |||
| 156 | ✗ | if (args[0].value.u32 > UINT16_MAX) { | |
| 157 | ✗ | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 158 | ✗ | return -EINVAL; | |
| 159 | } | ||
| 160 | ✗ | outcome = kfsw_fbo_schedule_cancel((uint16_t)args[0].value.u32); | |
| 161 | ✗ | if (outcome != 0) { | |
| 162 | ✗ | say(result, outcome, "no such entry"); | |
| 163 | ✗ | return outcome; | |
| 164 | } | ||
| 165 | ✗ | result->status = KFSW_COMMAND_OK; | |
| 166 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "cancelled=%u", args[0].value.u32); | |
| 167 | ✗ | return 0; | |
| 168 | } | ||
| 169 | |||
| 170 | ✗ | static int command_sched_clear(const struct kfsw_command_arg *args, size_t count, | |
| 171 | const struct kfsw_command_source *source, | ||
| 172 | struct kfsw_command_result *result) | ||
| 173 | { | ||
| 174 | ARG_UNUSED(args); | ||
| 175 | ARG_UNUSED(count); | ||
| 176 | ARG_UNUSED(source); | ||
| 177 | |||
| 178 | ✗ | result->status = KFSW_COMMAND_OK; | |
| 179 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "dropped=%d", | |
| 180 | kfsw_fbo_schedule_clear()); | ||
| 181 | ✗ | return 0; | |
| 182 | } | ||
| 183 | |||
| 184 | static const enum kfsw_command_type run_args[] = {KFSW_COMMAND_TYPE_TEXT}; | ||
| 185 | static const enum kfsw_command_type add_args[] = {KFSW_COMMAND_TYPE_TEXT, KFSW_COMMAND_TYPE_U32, | ||
| 186 | KFSW_COMMAND_TYPE_TEXT}; | ||
| 187 | static const enum kfsw_command_type cancel_args[] = {KFSW_COMMAND_TYPE_U32}; | ||
| 188 | |||
| 189 | static const struct kfsw_command_definition fbo_commands[] = { | ||
| 190 | { | ||
| 191 | .id = KFSW_COMMAND_ID_FBO_RUN, | ||
| 192 | .name = "fbo_run", | ||
| 193 | .help = "Carry out a procedure: fbo_run <name>.", | ||
| 194 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 195 | .arg_count = 1U, | ||
| 196 | .arg_types = run_args, | ||
| 197 | .handler = command_fbo_run, | ||
| 198 | }, | ||
| 199 | { | ||
| 200 | .id = KFSW_COMMAND_ID_FBO_SCHED_ADD, | ||
| 201 | .name = "fbo_sched_add", | ||
| 202 | .help = "Release a command later: fbo_sched_add <+s|@utc> <node> \"<command>\".", | ||
| 203 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 204 | .arg_count = 3U, | ||
| 205 | .arg_types = add_args, | ||
| 206 | .handler = command_sched_add, | ||
| 207 | }, | ||
| 208 | { | ||
| 209 | .id = KFSW_COMMAND_ID_FBO_SCHED_CANCEL, | ||
| 210 | .name = "fbo_sched_cancel", | ||
| 211 | .help = "Cancel a scheduled entry: fbo_sched_cancel <index>.", | ||
| 212 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 213 | .arg_count = 1U, | ||
| 214 | .arg_types = cancel_args, | ||
| 215 | .handler = command_sched_cancel, | ||
| 216 | }, | ||
| 217 | { | ||
| 218 | .id = KFSW_COMMAND_ID_FBO_SCHED_CLEAR, | ||
| 219 | .name = "fbo_sched_clear", | ||
| 220 | .help = "Empty the queue; a release in flight finishes.", | ||
| 221 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 222 | .handler = command_sched_clear, | ||
| 223 | }, | ||
| 224 | }; | ||
| 225 | |||
| 226 | const struct kfsw_command_definition_set kfsw_fbo_command_definitions = { | ||
| 227 | .commands = fbo_commands, | ||
| 228 | .count = ARRAY_SIZE(fbo_commands), | ||
| 229 | }; | ||
| 230 |