|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <zephyr/zbus/zbus.h> |
| 10 | +#include <zephyr/task_wdt/task_wdt.h> |
| 11 | +#include <zephyr/smf.h> |
| 12 | + |
| 13 | +#include "app_common.h" |
| 14 | +#include "dummy.h" |
| 15 | + |
| 16 | +/* Register log module */ |
| 17 | +LOG_MODULE_REGISTER(dummy_module, CONFIG_APP_DUMMY_LOG_LEVEL); |
| 18 | + |
| 19 | +/* Define module's zbus channel */ |
| 20 | +ZBUS_CHAN_DEFINE(DUMMY_CHAN, |
| 21 | + struct dummy_msg, |
| 22 | + NULL, |
| 23 | + NULL, |
| 24 | + ZBUS_OBSERVERS_EMPTY, |
| 25 | + ZBUS_MSG_INIT(0) |
| 26 | +); |
| 27 | + |
| 28 | +/* Register zbus subscriber */ |
| 29 | +ZBUS_MSG_SUBSCRIBER_DEFINE(dummy); |
| 30 | + |
| 31 | +/* Add subscriber to channel */ |
| 32 | +ZBUS_CHAN_ADD_OBS(DUMMY_CHAN, dummy, 0); |
| 33 | + |
| 34 | +#define MAX_MSG_SIZE sizeof(struct dummy_msg) |
| 35 | + |
| 36 | +BUILD_ASSERT(CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS > |
| 37 | + CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS, |
| 38 | + "Watchdog timeout must be greater than maximum message processing time"); |
| 39 | + |
| 40 | +/* State machine states */ |
| 41 | +enum dummy_module_state { |
| 42 | + STATE_RUNNING, |
| 43 | +}; |
| 44 | + |
| 45 | +/* Module state structure */ |
| 46 | +struct dummy_state { |
| 47 | + /* State machine context (must be first) */ |
| 48 | + struct smf_ctx ctx; |
| 49 | + |
| 50 | + /* Last received zbus channel */ |
| 51 | + const struct zbus_channel *chan; |
| 52 | + |
| 53 | + /* Message buffer */ |
| 54 | + uint8_t msg_buf[MAX_MSG_SIZE]; |
| 55 | + |
| 56 | + /* Current counter value */ |
| 57 | + int32_t current_value; |
| 58 | +}; |
| 59 | + |
| 60 | +/* Forward declarations */ |
| 61 | +static void state_running_run(void *o); |
| 62 | + |
| 63 | +/* State machine definition */ |
| 64 | +static const struct smf_state states[] = { |
| 65 | + [STATE_RUNNING] = SMF_CREATE_STATE(NULL, state_running_run, NULL, NULL, NULL), |
| 66 | +}; |
| 67 | + |
| 68 | +/* Watchdog callback */ |
| 69 | +static void task_wdt_callback(int channel_id, void *user_data) |
| 70 | +{ |
| 71 | + LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", |
| 72 | + channel_id, k_thread_name_get((k_tid_t)user_data)); |
| 73 | + |
| 74 | + SEND_FATAL_ERROR_WATCHDOG_TIMEOUT(); |
| 75 | +} |
| 76 | + |
| 77 | +/* State machine handlers */ |
| 78 | +static void state_running_run(void *o) |
| 79 | +{ |
| 80 | + const struct dummy_state *state_object = (const struct dummy_state *)o; |
| 81 | + |
| 82 | + if (&DUMMY_CHAN == state_object->chan) { |
| 83 | + struct dummy_msg msg = MSG_TO_DUMMY_MSG(state_object->msg_buf); |
| 84 | + |
| 85 | + if (msg.type == DUMMY_SAMPLE_REQUEST) { |
| 86 | + LOG_DBG("Received sample request"); |
| 87 | + state_object->current_value++; |
| 88 | + |
| 89 | + struct dummy_msg response = { |
| 90 | + .type = DUMMY_SAMPLE_RESPONSE, |
| 91 | + .value = state_object->current_value |
| 92 | + }; |
| 93 | + |
| 94 | + int err = zbus_chan_pub(&DUMMY_CHAN, &response, K_NO_WAIT); |
| 95 | + if (err) { |
| 96 | + LOG_ERR("Failed to publish response: %d", err); |
| 97 | + SEND_FATAL_ERROR(); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/* Module task function */ |
| 105 | +static void dummy_task(void) |
| 106 | +{ |
| 107 | + int err; |
| 108 | + int task_wdt_id; |
| 109 | + const uint32_t wdt_timeout_ms = |
| 110 | + (CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC); |
| 111 | + const uint32_t execution_time_ms = |
| 112 | + (CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); |
| 113 | + const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); |
| 114 | + struct dummy_state dummy_state = { |
| 115 | + .current_value = 0 |
| 116 | + }; |
| 117 | + |
| 118 | + LOG_DBG("Starting dummy module task"); |
| 119 | + |
| 120 | + task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); |
| 121 | + if (task_wdt_id < 0) { |
| 122 | + LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); |
| 123 | + SEND_FATAL_ERROR(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + smf_set_initial(SMF_CTX(&dummy_state), &states[STATE_RUNNING]); |
| 128 | + |
| 129 | + while (true) { |
| 130 | + err = task_wdt_feed(task_wdt_id); |
| 131 | + if (err) { |
| 132 | + LOG_ERR("Failed to feed watchdog: %d", err); |
| 133 | + SEND_FATAL_ERROR(); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + err = zbus_sub_wait_msg(&dummy, |
| 138 | + &dummy_state.chan, |
| 139 | + dummy_state.msg_buf, |
| 140 | + zbus_wait_ms); |
| 141 | + if (err == -ENOMSG) { |
| 142 | + continue; |
| 143 | + } else if (err) { |
| 144 | + LOG_ERR("Failed to wait for message: %d", err); |
| 145 | + SEND_FATAL_ERROR(); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + err = smf_run_state(SMF_CTX(&dummy_state)); |
| 150 | + if (err) { |
| 151 | + LOG_ERR("Failed to run state machine: %d", err); |
| 152 | + SEND_FATAL_ERROR(); |
| 153 | + return; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/* Define module thread */ |
| 159 | +K_THREAD_DEFINE(dummy_task_id, |
| 160 | + CONFIG_APP_DUMMY_THREAD_STACK_SIZE, |
| 161 | + dummy_task, NULL, NULL, NULL, |
| 162 | + K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); |
0 commit comments