kfsw-modules/gpredict/src/gpredict_commands.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | |||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | |||
| 9 | #include <kfsw/modules/gpredict.h> | ||
| 10 | #include <kfsw/services/command.h> | ||
| 11 | |||
| 12 | /* | ||
| 13 | * The operator chooses the spacecraft and parks or clears the tracker through | ||
| 14 | * the command registry, the same path the shell and the ground use. | ||
| 15 | */ | ||
| 16 | |||
| 17 | 16 | static void say(struct kfsw_command_result *result, int outcome, const char *reason) | |
| 18 | { | ||
| 19 | 16 | switch (outcome) { | |
| 20 | 8 | case -ENOENT: | |
| 21 | 8 | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 22 | 8 | break; | |
| 23 | 4 | case -EPERM: | |
| 24 | 4 | result->status = KFSW_COMMAND_DENIED; | |
| 25 | 4 | break; | |
| 26 | 4 | case -EBUSY: | |
| 27 | case -EALREADY: | ||
| 28 | 4 | result->status = KFSW_COMMAND_BUSY; | |
| 29 | 4 | break; | |
| 30 | ✗ | default: | |
| 31 | ✗ | result->status = KFSW_COMMAND_FAILED; | |
| 32 | ✗ | (void)snprintf(result->detail, sizeof(result->detail), "%s (%d)", reason, outcome); | |
| 33 | ✗ | return; | |
| 34 | } | ||
| 35 | 16 | (void)snprintf(result->detail, sizeof(result->detail), "%s", reason); | |
| 36 | } | ||
| 37 | |||
| 38 | 18 | static int profile_index(const struct kfsw_command_arg *arg, uint8_t *index, | |
| 39 | struct kfsw_command_result *result) | ||
| 40 | { | ||
| 41 | 18 | if (arg->value.u32 > UINT8_MAX) { | |
| 42 | 4 | say(result, -ENOENT, "no profile is defined there"); | |
| 43 | 4 | return -EINVAL; | |
| 44 | } | ||
| 45 | 14 | *index = (uint8_t)arg->value.u32; | |
| 46 | 14 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | 4 | static void say_state(struct kfsw_command_result *result) | |
| 50 | { | ||
| 51 | struct kfsw_gpredict_status status; | ||
| 52 | |||
| 53 | 4 | (void)kfsw_gpredict_get_status(&status); | |
| 54 | 4 | result->status = KFSW_COMMAND_OK; | |
| 55 | 8 | (void)snprintf(result->detail, sizeof(result->detail), "state=%s", | |
| 56 | 4 | kfsw_gpredict_state_name((enum kfsw_gpredict_state)status.state)); | |
| 57 | 4 | } | |
| 58 | |||
| 59 | 8 | static int command_select(const struct kfsw_command_arg *args, size_t count, | |
| 60 | const struct kfsw_command_source *source, | ||
| 61 | struct kfsw_command_result *result) | ||
| 62 | { | ||
| 63 | struct kfsw_gpredict_profile profile; | ||
| 64 | uint8_t index; | ||
| 65 | int outcome; | ||
| 66 | |||
| 67 | ARG_UNUSED(count); | ||
| 68 | ARG_UNUSED(source); | ||
| 69 | |||
| 70 | 8 | outcome = profile_index(&args[0], &index, result); | |
| 71 | 8 | if (outcome != 0) { | |
| 72 | 2 | return outcome; | |
| 73 | } | ||
| 74 | 6 | outcome = kfsw_gpredict_profile_select(index); | |
| 75 | 6 | if (outcome == 0) { | |
| 76 | 2 | outcome = kfsw_gpredict_profile_get(index, &profile); | |
| 77 | } | ||
| 78 | 6 | if (outcome != 0) { | |
| 79 | 4 | say(result, outcome, | |
| 80 | (outcome == -EPERM) ? "the profile is disabled; enable it first" | ||
| 81 | : "no profile is defined there"); | ||
| 82 | 4 | return outcome; | |
| 83 | } | ||
| 84 | 2 | result->status = KFSW_COMMAND_OK; | |
| 85 | 2 | (void)snprintf(result->detail, sizeof(result->detail), "selected=%u name=%s catalogue=%u", | |
| 86 | index, profile.name, profile.catalogue); | ||
| 87 | 2 | return 0; | |
| 88 | } | ||
| 89 | |||
| 90 | 10 | static int command_enable(const struct kfsw_command_arg *args, size_t count, | |
| 91 | const struct kfsw_command_source *source, | ||
| 92 | struct kfsw_command_result *result) | ||
| 93 | { | ||
| 94 | uint8_t index; | ||
| 95 | int outcome; | ||
| 96 | |||
| 97 | ARG_UNUSED(count); | ||
| 98 | ARG_UNUSED(source); | ||
| 99 | |||
| 100 | 10 | outcome = profile_index(&args[0], &index, result); | |
| 101 | 10 | if (outcome != 0) { | |
| 102 | 2 | return outcome; | |
| 103 | } | ||
| 104 | 8 | if (args[1].value.u32 > 1U) { | |
| 105 | 2 | result->status = KFSW_COMMAND_INVALID_ARGUMENT; | |
| 106 | 2 | (void)snprintf(result->detail, sizeof(result->detail), | |
| 107 | "write 1 to enable or 0 to disable"); | ||
| 108 | 2 | return -EINVAL; | |
| 109 | } | ||
| 110 | 6 | outcome = kfsw_gpredict_profile_enable(index, args[1].value.u32 == 1U); | |
| 111 | 6 | if (outcome != 0) { | |
| 112 | 4 | say(result, outcome, | |
| 113 | (outcome == -EBUSY) ? "the profile is selected; select another first" | ||
| 114 | : "no profile is defined there"); | ||
| 115 | 4 | return outcome; | |
| 116 | } | ||
| 117 | 2 | result->status = KFSW_COMMAND_OK; | |
| 118 | 2 | (void)snprintf(result->detail, sizeof(result->detail), "profile=%u enabled=%u", index, | |
| 119 | 2 | args[1].value.u32); | |
| 120 | 2 | return 0; | |
| 121 | } | ||
| 122 | |||
| 123 | 4 | static int command_park(const struct kfsw_command_arg *args, size_t count, | |
| 124 | const struct kfsw_command_source *source, | ||
| 125 | struct kfsw_command_result *result) | ||
| 126 | { | ||
| 127 | int outcome; | ||
| 128 | |||
| 129 | ARG_UNUSED(args); | ||
| 130 | ARG_UNUSED(count); | ||
| 131 | ARG_UNUSED(source); | ||
| 132 | |||
| 133 | 4 | outcome = kfsw_gpredict_park(); | |
| 134 | 4 | if (outcome != 0) { | |
| 135 | 2 | say(result, outcome, "the tracker is in fault; clear it first"); | |
| 136 | 2 | return outcome; | |
| 137 | } | ||
| 138 | 2 | say_state(result); | |
| 139 | 2 | return 0; | |
| 140 | } | ||
| 141 | |||
| 142 | 4 | static int command_clear(const struct kfsw_command_arg *args, size_t count, | |
| 143 | const struct kfsw_command_source *source, | ||
| 144 | struct kfsw_command_result *result) | ||
| 145 | { | ||
| 146 | int outcome; | ||
| 147 | |||
| 148 | ARG_UNUSED(args); | ||
| 149 | ARG_UNUSED(count); | ||
| 150 | ARG_UNUSED(source); | ||
| 151 | |||
| 152 | 4 | outcome = kfsw_gpredict_clear(); | |
| 153 | 4 | if (outcome != 0) { | |
| 154 | 2 | say(result, outcome, "the tracker is not in fault"); | |
| 155 | 2 | return outcome; | |
| 156 | } | ||
| 157 | 2 | say_state(result); | |
| 158 | 2 | return 0; | |
| 159 | } | ||
| 160 | |||
| 161 | static const enum kfsw_command_type select_args[] = {KFSW_COMMAND_TYPE_U32}; | ||
| 162 | static const enum kfsw_command_type enable_args[] = {KFSW_COMMAND_TYPE_U32, KFSW_COMMAND_TYPE_U32}; | ||
| 163 | |||
| 164 | static const struct kfsw_command_definition gpredict_commands[] = { | ||
| 165 | { | ||
| 166 | .id = KFSW_COMMAND_ID_GPREDICT_SELECT, | ||
| 167 | .name = "gpredict_select", | ||
| 168 | .help = "Judge the next bearing by a profile: gpredict_select <index>.", | ||
| 169 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 170 | .arg_count = 1U, | ||
| 171 | .arg_types = select_args, | ||
| 172 | .handler = command_select, | ||
| 173 | }, | ||
| 174 | { | ||
| 175 | .id = KFSW_COMMAND_ID_GPREDICT_ENABLE, | ||
| 176 | .name = "gpredict_enable", | ||
| 177 | .help = "Allow or forbid selecting a profile: gpredict_enable <index> <1|0>.", | ||
| 178 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 179 | .arg_count = 2U, | ||
| 180 | .arg_types = enable_args, | ||
| 181 | .handler = command_enable, | ||
| 182 | }, | ||
| 183 | { | ||
| 184 | .id = KFSW_COMMAND_ID_GPREDICT_PARK, | ||
| 185 | .name = "gpredict_park", | ||
| 186 | .help = "Send the antenna to its park position now.", | ||
| 187 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 188 | .handler = command_park, | ||
| 189 | }, | ||
| 190 | { | ||
| 191 | .id = KFSW_COMMAND_ID_GPREDICT_CLEAR, | ||
| 192 | .name = "gpredict_clear", | ||
| 193 | .help = "Clear a tracker fault and park.", | ||
| 194 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 195 | .handler = command_clear, | ||
| 196 | }, | ||
| 197 | }; | ||
| 198 | |||
| 199 | const struct kfsw_command_definition_set kfsw_gpredict_command_definitions = { | ||
| 200 | .commands = gpredict_commands, | ||
| 201 | .count = ARRAY_SIZE(gpredict_commands), | ||
| 202 | }; | ||
| 203 |