kfsw-services/src/gndwdt/gndwdt.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/kernel.h> | ||
| 8 | #include <zephyr/sys/byteorder.h> | ||
| 9 | #include <zephyr/sys/reboot.h> | ||
| 10 | |||
| 11 | #if CONFIG_KFSW_COMMAND_CSP | ||
| 12 | #include <csp/csp_iflist.h> | ||
| 13 | #endif | ||
| 14 | |||
| 15 | #include <kfsw/platform/time.h> | ||
| 16 | #include <kfsw/services/gndwdt.h> | ||
| 17 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_GNDWDT | ||
| 18 | #include <kfsw/services/log.h> | ||
| 19 | #if CONFIG_KFSW_EVENT | ||
| 20 | #include <kfsw/services/event.h> | ||
| 21 | #endif | ||
| 22 | |||
| 23 | /* A composition that cannot be fed, or cannot reset, would arm a timer that | ||
| 24 | * runs out once and then does nothing useful for the rest of the mission. | ||
| 25 | * Tests call the handler directly and supply the source themselves. | ||
| 26 | */ | ||
| 27 | BUILD_ASSERT(IS_ENABLED(CONFIG_KFSW_COMMAND_CSP) || IS_ENABLED(CONFIG_ZTEST), | ||
| 28 | "KFSW_GNDWDT needs KFSW_COMMAND_CSP: only a feed over CSP counts"); | ||
| 29 | BUILD_ASSERT(IS_ENABLED(CONFIG_REBOOT), "KFSW_GNDWDT needs REBOOT to act on an expiry"); | ||
| 30 | |||
| 31 | /* The command worker and timeout worker share this state. */ | ||
| 32 | static uint64_t last_contact_ms; | ||
| 33 | static uint32_t contacts; | ||
| 34 | static uint16_t last_node; | ||
| 35 | |||
| 36 | static K_MUTEX_DEFINE(gndwdt_lock); | ||
| 37 | static uint32_t timeout_s = CONFIG_KFSW_GNDWDT_TIMEOUT_S; | ||
| 38 | static uint32_t expiries; | ||
| 39 | static uint32_t reschedule_failures; | ||
| 40 | static bool enabled = IS_ENABLED(CONFIG_KFSW_GNDWDT_ENABLED_AT_START); | ||
| 41 | static bool running; | ||
| 42 | static bool resetting; | ||
| 43 | |||
| 44 | static void gndwdt_work_handler(struct k_work *work); | ||
| 45 | static void gndwdt_reset_handler(struct k_work *work); | ||
| 46 | K_WORK_DELAYABLE_DEFINE(gndwdt_work, gndwdt_work_handler); | ||
| 47 | K_WORK_DELAYABLE_DEFINE(gndwdt_reset_work, gndwdt_reset_handler); | ||
| 48 | |||
| 49 | 268 | static void restart_countdown(void) | |
| 50 | { | ||
| 51 | 268 | last_contact_ms = kfsw_time_monotonic_ms(); | |
| 52 | 268 | } | |
| 53 | |||
| 54 | /* A reschedule that fails leaves the countdown unchecked, which is the one | ||
| 55 | * thing this service exists to do, so it is counted and said out loud instead | ||
| 56 | * of discarded. The lock is taken here as well: it is held at one of the three | ||
| 57 | * call sites, and a Zephyr mutex may be locked again by the thread owning it. | ||
| 58 | */ | ||
| 59 | 152 | static void reschedule(struct k_work_delayable *work, k_timeout_t delay) | |
| 60 | { | ||
| 61 | 152 | if (k_work_reschedule(work, delay) < 0) { | |
| 62 | 12 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 63 | 12 | if (reschedule_failures < UINT32_MAX) { | |
| 64 | 12 | reschedule_failures++; | |
| 65 | } | ||
| 66 | 12 | k_mutex_unlock(&gndwdt_lock); | |
| 67 | 12 | kfsw_log_error("Ground watchdog could not reschedule its check"); | |
| 68 | } | ||
| 69 | 152 | } | |
| 70 | |||
| 71 | 140 | int kfsw_gndwdt_start(void) | |
| 72 | { | ||
| 73 | 140 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 74 | 140 | if (running) { | |
| 75 | 12 | k_mutex_unlock(&gndwdt_lock); | |
| 76 | 12 | return -EALREADY; | |
| 77 | } | ||
| 78 | 128 | running = true; | |
| 79 | 128 | restart_countdown(); | |
| 80 | 128 | k_mutex_unlock(&gndwdt_lock); | |
| 81 | |||
| 82 | 128 | reschedule(&gndwdt_work, K_MSEC(CONFIG_KFSW_GNDWDT_CHECK_MS)); | |
| 83 | 128 | kfsw_log_info("Ground watchdog started, timeout %u s, %s", timeout_s, | |
| 84 | enabled ? "armed" : "disarmed"); | ||
| 85 | 128 | return 0; | |
| 86 | } | ||
| 87 | |||
| 88 | 200 | int kfsw_gndwdt_stop(void) | |
| 89 | { | ||
| 90 | 200 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 91 | 200 | if (!running) { | |
| 92 | 72 | k_mutex_unlock(&gndwdt_lock); | |
| 93 | 72 | return -EALREADY; | |
| 94 | } | ||
| 95 | 128 | running = false; | |
| 96 | /* Taking the service out of service is the one thing that calls off a | ||
| 97 | * reset already decided. A late feed does not. | ||
| 98 | */ | ||
| 99 | 128 | resetting = false; | |
| 100 | 128 | k_mutex_unlock(&gndwdt_lock); | |
| 101 | |||
| 102 | 128 | (void)k_work_cancel_delayable(&gndwdt_reset_work); | |
| 103 | 128 | (void)k_work_cancel_delayable(&gndwdt_work); | |
| 104 | 128 | kfsw_log_info("Ground watchdog stopped"); | |
| 105 | 128 | return 0; | |
| 106 | } | ||
| 107 | |||
| 108 | 80 | static void ground_wtd_reply(struct kfsw_command_result *result) | |
| 109 | { | ||
| 110 | struct kfsw_gndwdt_status status; | ||
| 111 | |||
| 112 | /* Parameter samples use this same snapshot. */ | ||
| 113 | 80 | kfsw_gndwdt_get_status(&status); | |
| 114 | 80 | result->status = KFSW_COMMAND_OK; | |
| 115 | 80 | (void)snprintf(result->detail, sizeof(result->detail), | |
| 116 | "ground_wtd_cnt=%u ground_wtd_timeout=%u", status.remaining_s, | ||
| 117 | status.timeout_s); | ||
| 118 | 80 | } | |
| 119 | |||
| 120 | 188 | static int ground_wtd(const struct kfsw_command_arg *args, size_t arg_count, | |
| 121 | const struct kfsw_command_source *source, struct kfsw_command_result *result) | ||
| 122 | { | ||
| 123 | ARG_UNUSED(arg_count); | ||
| 124 | |||
| 125 | 188 | if (strcmp(args[0].value.text, "get") == 0) { | |
| 126 | 24 | ground_wtd_reply(result); | |
| 127 | 24 | return 0; | |
| 128 | } | ||
| 129 | |||
| 130 | 164 | if (!source->via_csp || strcmp(args[0].value.text, KFSW_GNDWDT_FEED_WORD) != 0) { | |
| 131 | 84 | result->status = KFSW_COMMAND_DENIED; | |
| 132 | 84 | return -EACCES; | |
| 133 | } | ||
| 134 | |||
| 135 | 80 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 136 | 80 | if (!running || resetting) { | |
| 137 | 24 | k_mutex_unlock(&gndwdt_lock); | |
| 138 | 24 | result->status = KFSW_COMMAND_UNAVAILABLE; | |
| 139 | 24 | return -EAGAIN; | |
| 140 | } | ||
| 141 | 56 | restart_countdown(); | |
| 142 | 56 | last_node = source->node; | |
| 143 | 56 | if (contacts < UINT32_MAX) { | |
| 144 | 56 | contacts++; | |
| 145 | } | ||
| 146 | 56 | k_mutex_unlock(&gndwdt_lock); | |
| 147 | 56 | ground_wtd_reply(result); | |
| 148 | 56 | return 0; | |
| 149 | } | ||
| 150 | |||
| 151 | static const enum kfsw_command_type ground_wtd_args[] = {KFSW_COMMAND_TYPE_TEXT}; | ||
| 152 | static const struct kfsw_command_definition ground_wtd_commands[] = { | ||
| 153 | { | ||
| 154 | .id = KFSW_COMMAND_ID_GROUND_WTD, | ||
| 155 | .name = "ground_wtd", | ||
| 156 | .help = "Ground watchdog: get, or KFSWWSFK to feed over CSP.", | ||
| 157 | .flags = KFSW_COMMAND_FLAG_MUTATING, | ||
| 158 | .arg_count = 1U, | ||
| 159 | .arg_types = ground_wtd_args, | ||
| 160 | .handler = ground_wtd, | ||
| 161 | }, | ||
| 162 | }; | ||
| 163 | |||
| 164 | const struct kfsw_command_definition_set kfsw_gndwdt_command_definitions = { | ||
| 165 | .commands = ground_wtd_commands, | ||
| 166 | .count = ARRAY_SIZE(ground_wtd_commands), | ||
| 167 | }; | ||
| 168 | |||
| 169 | #if CONFIG_KFSW_COMMAND_CSP | ||
| 170 | int kfsw_gndwdt_remote(uint16_t node, bool feed, struct kfsw_command_result *result) | ||
| 171 | { | ||
| 172 | struct kfsw_command_arg arg = { | ||
| 173 | .type = KFSW_COMMAND_TYPE_TEXT, | ||
| 174 | .value.text = feed ? KFSW_GNDWDT_FEED_WORD : "get", | ||
| 175 | }; | ||
| 176 | |||
| 177 | if ((node == 0U) || (result == NULL) || (csp_iflist_get_by_addr(node) != NULL)) { | ||
| 178 | return -EINVAL; | ||
| 179 | } | ||
| 180 | return kfsw_command_invoke_remote(node, "ground_wtd", &arg, 1U, result); | ||
| 181 | } | ||
| 182 | #endif | ||
| 183 | |||
| 184 | 168 | int kfsw_gndwdt_evaluate(void) | |
| 185 | { | ||
| 186 | uint64_t elapsed_ms; | ||
| 187 | uint32_t allowed_s; | ||
| 188 | |||
| 189 | 168 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 190 | /* A decided reset is reported once, not again on every later check. */ | ||
| 191 | 168 | if (!running || !enabled || resetting) { | |
| 192 | 36 | k_mutex_unlock(&gndwdt_lock); | |
| 193 | 36 | return 0; | |
| 194 | } | ||
| 195 | 132 | allowed_s = timeout_s; | |
| 196 | 132 | elapsed_ms = kfsw_time_monotonic_ms() - last_contact_ms; | |
| 197 | |||
| 198 | 132 | if ((elapsed_ms / MSEC_PER_SEC) < allowed_s) { | |
| 199 | 48 | k_mutex_unlock(&gndwdt_lock); | |
| 200 | 48 | return 0; | |
| 201 | } | ||
| 202 | |||
| 203 | 84 | if (expiries < UINT32_MAX) { | |
| 204 | 84 | expiries++; | |
| 205 | } | ||
| 206 | /* So a disarm and re-arm starts from now rather than from the expiry. */ | ||
| 207 | 84 | restart_countdown(); | |
| 208 | 84 | k_mutex_unlock(&gndwdt_lock); | |
| 209 | |||
| 210 | 84 | kfsw_log_error("Ground watchdog: no contact for %u s; resetting", allowed_s); | |
| 211 | #if CONFIG_KFSW_EVENT | ||
| 212 | { | ||
| 213 | uint8_t payload[4]; | ||
| 214 | |||
| 215 | 28 | sys_put_be32(allowed_s, payload); | |
| 216 | 28 | kfsw_event_emit(KFSW_EVENT_SOURCE_GNDWDT, KFSW_EVENT_GNDWDT_EXPIRED, | |
| 217 | KFSW_EVENT_CRITICAL, payload, sizeof(payload)); | ||
| 218 | } | ||
| 219 | #endif | ||
| 220 | 84 | return -ETIMEDOUT; | |
| 221 | } | ||
| 222 | |||
| 223 | 368 | void kfsw_gndwdt_get_status(struct kfsw_gndwdt_status *status) | |
| 224 | { | ||
| 225 | uint64_t elapsed_ms; | ||
| 226 | |||
| 227 | 368 | if (status == NULL) { | |
| 228 | 12 | return; | |
| 229 | } | ||
| 230 | |||
| 231 | 356 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 232 | 356 | elapsed_ms = kfsw_time_monotonic_ms() - last_contact_ms; | |
| 233 | 356 | status->timeout_s = timeout_s; | |
| 234 | 356 | status->expiries = expiries; | |
| 235 | 356 | status->reschedule_failures = reschedule_failures; | |
| 236 | 356 | status->enabled = enabled; | |
| 237 | 356 | status->running = running; | |
| 238 | 356 | status->since_contact_s = MIN(elapsed_ms / MSEC_PER_SEC, UINT32_MAX); | |
| 239 | 320 | status->remaining_s = (!running || resetting || status->since_contact_s >= timeout_s) | |
| 240 | ? 0U | ||
| 241 | 676 | : timeout_s - status->since_contact_s; | |
| 242 | 356 | status->contacts = contacts; | |
| 243 | 356 | status->last_node = last_node; | |
| 244 | 356 | k_mutex_unlock(&gndwdt_lock); | |
| 245 | } | ||
| 246 | |||
| 247 | 260 | int kfsw_gndwdt_set_timeout_s(uint32_t value) | |
| 248 | { | ||
| 249 | 260 | if ((value < CONFIG_KFSW_GNDWDT_TIMEOUT_MIN_S) || | |
| 250 | (value > CONFIG_KFSW_GNDWDT_TIMEOUT_MAX_S)) { | ||
| 251 | 48 | return -ERANGE; | |
| 252 | } | ||
| 253 | |||
| 254 | 212 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 255 | 212 | timeout_s = value; | |
| 256 | 212 | k_mutex_unlock(&gndwdt_lock); | |
| 257 | 212 | return 0; | |
| 258 | } | ||
| 259 | |||
| 260 | 172 | void kfsw_gndwdt_set_enabled(bool value) | |
| 261 | { | ||
| 262 | 172 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 263 | 172 | enabled = value; | |
| 264 | 172 | k_mutex_unlock(&gndwdt_lock); | |
| 265 | 172 | } | |
| 266 | |||
| 267 | 12 | static void gndwdt_reset_handler(struct k_work *work) | |
| 268 | { | ||
| 269 | ARG_UNUSED(work); | ||
| 270 | |||
| 271 | 12 | sys_reboot(SYS_REBOOT_COLD); | |
| 272 | } | ||
| 273 | |||
| 274 | 12 | static void gndwdt_work_handler(struct k_work *work) | |
| 275 | { | ||
| 276 | ARG_UNUSED(work); | ||
| 277 | |||
| 278 | 12 | if (kfsw_gndwdt_evaluate() == -ETIMEDOUT) { | |
| 279 | 12 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 280 | 12 | if (!resetting) { | |
| 281 | 12 | resetting = true; | |
| 282 | 12 | k_mutex_unlock(&gndwdt_lock); | |
| 283 | /* Delayed so the log line and the event record leave first. */ | ||
| 284 | 12 | reschedule(&gndwdt_reset_work, K_MSEC(CONFIG_KFSW_GNDWDT_RESET_DELAY_MS)); | |
| 285 | } else { | ||
| 286 | ✗ | k_mutex_unlock(&gndwdt_lock); | |
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | 12 | k_mutex_lock(&gndwdt_lock, K_FOREVER); | |
| 291 | 12 | if (running) { | |
| 292 | 12 | reschedule(&gndwdt_work, K_MSEC(CONFIG_KFSW_GNDWDT_CHECK_MS)); | |
| 293 | } | ||
| 294 | 12 | k_mutex_unlock(&gndwdt_lock); | |
| 295 | 12 | } | |
| 296 |