|
| 1 | +diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt |
| 2 | +index e1948cc..8d20bcd 100644 |
| 3 | +--- a/app/CMakeLists.txt |
| 4 | ++++ b/app/CMakeLists.txt |
| 5 | +@@ -27,3 +27,4 @@ add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led) |
| 6 | + add_subdirectory_ifdef(CONFIG_APP_LOCATION src/modules/location) |
| 7 | + add_subdirectory_ifdef(CONFIG_APP_CLOUD src/modules/cloud) |
| 8 | + add_subdirectory_ifdef(CONFIG_APP_FOTA src/modules/fota) |
| 9 | ++add_subdirectory_ifdef(CONFIG_APP_DUMMY src/modules/dummy) |
| 10 | +diff --git a/app/src/modules/dummy/CMakeLists.txt b/app/src/modules/dummy/CMakeLists.txt |
| 11 | +new file mode 100644 |
| 12 | +index 0000000..a299b3b |
| 13 | +--- /dev/null |
| 14 | ++++ b/app/src/modules/dummy/CMakeLists.txt |
| 15 | +@@ -0,0 +1,8 @@ |
| 16 | ++# |
| 17 | ++# Copyright (c) 2025 Nordic Semiconductor ASA |
| 18 | ++# |
| 19 | ++# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 20 | ++# |
| 21 | ++ |
| 22 | ++target_sources_ifdef(CONFIG_APP_DUMMY app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/dummy.c) |
| 23 | ++target_include_directories(app PRIVATE .) |
| 24 | +diff --git a/app/src/modules/dummy/Kconfig.dummy b/app/src/modules/dummy/Kconfig.dummy |
| 25 | +new file mode 100644 |
| 26 | +index 0000000..2319814 |
| 27 | +--- /dev/null |
| 28 | ++++ b/app/src/modules/dummy/Kconfig.dummy |
| 29 | +@@ -0,0 +1,37 @@ |
| 30 | ++# |
| 31 | ++# Copyright (c) 2024 Nordic Semiconductor |
| 32 | ++# |
| 33 | ++# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 34 | ++# |
| 35 | ++ |
| 36 | ++menuconfig APP_DUMMY |
| 37 | ++ bool "Dummy module" |
| 38 | ++ default y |
| 39 | ++ help |
| 40 | ++ Enable the dummy module. |
| 41 | ++ |
| 42 | ++if APP_DUMMY |
| 43 | ++ |
| 44 | ++config APP_DUMMY_THREAD_STACK_SIZE |
| 45 | ++ int "Dummy module thread stack size" |
| 46 | ++ default 2048 |
| 47 | ++ help |
| 48 | ++ Stack size for the dummy module thread. |
| 49 | ++ |
| 50 | ++config APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS |
| 51 | ++ int "Dummy module watchdog timeout in seconds" |
| 52 | ++ default 30 |
| 53 | ++ help |
| 54 | ++ Watchdog timeout for the dummy module. |
| 55 | ++ |
| 56 | ++config APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS |
| 57 | ++ int "Dummy module message processing timeout in seconds" |
| 58 | ++ default 5 |
| 59 | ++ help |
| 60 | ++ Maximum time allowed for processing a single message in the dummy module. |
| 61 | ++ |
| 62 | ++module = APP_DUMMY |
| 63 | ++module-str = DUMMY |
| 64 | ++source "subsys/logging/Kconfig.template.log_config" |
| 65 | ++ |
| 66 | ++endif # APP_DUMMY |
| 67 | +diff --git a/app/src/modules/dummy/dummy.c b/app/src/modules/dummy/dummy.c |
| 68 | +new file mode 100644 |
| 69 | +index 0000000..8c2450a |
| 70 | +--- /dev/null |
| 71 | ++++ b/app/src/modules/dummy/dummy.c |
| 72 | +@@ -0,0 +1,162 @@ |
| 73 | ++/* |
| 74 | ++ * Copyright (c) 2025 Nordic Semiconductor ASA |
| 75 | ++ * |
| 76 | ++ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 77 | ++ */ |
| 78 | ++ |
| 79 | ++#include <zephyr/kernel.h> |
| 80 | ++#include <zephyr/logging/log.h> |
| 81 | ++#include <zephyr/zbus/zbus.h> |
| 82 | ++#include <zephyr/task_wdt/task_wdt.h> |
| 83 | ++#include <zephyr/smf.h> |
| 84 | ++ |
| 85 | ++#include "app_common.h" |
| 86 | ++#include "dummy.h" |
| 87 | ++ |
| 88 | ++/* Register log module */ |
| 89 | ++LOG_MODULE_REGISTER(dummy_module, CONFIG_APP_DUMMY_LOG_LEVEL); |
| 90 | ++ |
| 91 | ++/* Define module's zbus channel */ |
| 92 | ++ZBUS_CHAN_DEFINE(DUMMY_CHAN, |
| 93 | ++ struct dummy_msg, |
| 94 | ++ NULL, |
| 95 | ++ NULL, |
| 96 | ++ ZBUS_OBSERVERS_EMPTY, |
| 97 | ++ ZBUS_MSG_INIT(0) |
| 98 | ++); |
| 99 | ++ |
| 100 | ++/* Register zbus subscriber */ |
| 101 | ++ZBUS_MSG_SUBSCRIBER_DEFINE(dummy); |
| 102 | ++ |
| 103 | ++/* Add subscriber to channel */ |
| 104 | ++ZBUS_CHAN_ADD_OBS(DUMMY_CHAN, dummy, 0); |
| 105 | ++ |
| 106 | ++#define MAX_MSG_SIZE sizeof(struct dummy_msg) |
| 107 | ++ |
| 108 | ++BUILD_ASSERT(CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS > |
| 109 | ++ CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS, |
| 110 | ++ "Watchdog timeout must be greater than maximum message processing time"); |
| 111 | ++ |
| 112 | ++/* State machine states */ |
| 113 | ++enum dummy_module_state { |
| 114 | ++ STATE_RUNNING, |
| 115 | ++}; |
| 116 | ++ |
| 117 | ++/* Module state structure */ |
| 118 | ++struct dummy_state { |
| 119 | ++ /* State machine context (must be first) */ |
| 120 | ++ struct smf_ctx ctx; |
| 121 | ++ |
| 122 | ++ /* Last received zbus channel */ |
| 123 | ++ const struct zbus_channel *chan; |
| 124 | ++ |
| 125 | ++ /* Message buffer */ |
| 126 | ++ uint8_t msg_buf[MAX_MSG_SIZE]; |
| 127 | ++ |
| 128 | ++ /* Current counter value */ |
| 129 | ++ int32_t current_value; |
| 130 | ++}; |
| 131 | ++ |
| 132 | ++/* Forward declarations */ |
| 133 | ++static void state_running_run(void *o); |
| 134 | ++ |
| 135 | ++/* State machine definition */ |
| 136 | ++static const struct smf_state states[] = { |
| 137 | ++ [STATE_RUNNING] = SMF_CREATE_STATE(NULL, state_running_run, NULL, NULL, NULL), |
| 138 | ++}; |
| 139 | ++ |
| 140 | ++/* Watchdog callback */ |
| 141 | ++static void task_wdt_callback(int channel_id, void *user_data) |
| 142 | ++{ |
| 143 | ++ LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", |
| 144 | ++ channel_id, k_thread_name_get((k_tid_t)user_data)); |
| 145 | ++ |
| 146 | ++ SEND_FATAL_ERROR_WATCHDOG_TIMEOUT(); |
| 147 | ++} |
| 148 | ++ |
| 149 | ++/* State machine handlers */ |
| 150 | ++static void state_running_run(void *o) |
| 151 | ++{ |
| 152 | ++ const struct dummy_state *state_object = (const struct dummy_state *)o; |
| 153 | ++ |
| 154 | ++ if (&DUMMY_CHAN == state_object->chan) { |
| 155 | ++ struct dummy_msg msg = MSG_TO_DUMMY_MSG(state_object->msg_buf); |
| 156 | ++ |
| 157 | ++ if (msg.type == DUMMY_SAMPLE_REQUEST) { |
| 158 | ++ LOG_DBG("Received sample request"); |
| 159 | ++ state_object->current_value++; |
| 160 | ++ |
| 161 | ++ struct dummy_msg response = { |
| 162 | ++ .type = DUMMY_SAMPLE_RESPONSE, |
| 163 | ++ .value = state_object->current_value |
| 164 | ++ }; |
| 165 | ++ |
| 166 | ++ int err = zbus_chan_pub(&DUMMY_CHAN, &response, K_NO_WAIT); |
| 167 | ++ if (err) { |
| 168 | ++ LOG_ERR("Failed to publish response: %d", err); |
| 169 | ++ SEND_FATAL_ERROR(); |
| 170 | ++ return; |
| 171 | ++ } |
| 172 | ++ } |
| 173 | ++ } |
| 174 | ++} |
| 175 | ++ |
| 176 | ++/* Module task function */ |
| 177 | ++static void dummy_task(void) |
| 178 | ++{ |
| 179 | ++ int err; |
| 180 | ++ int task_wdt_id; |
| 181 | ++ const uint32_t wdt_timeout_ms = |
| 182 | ++ (CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC); |
| 183 | ++ const uint32_t execution_time_ms = |
| 184 | ++ (CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); |
| 185 | ++ const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); |
| 186 | ++ struct dummy_state dummy_state = { |
| 187 | ++ .current_value = 0 |
| 188 | ++ }; |
| 189 | ++ |
| 190 | ++ LOG_DBG("Starting dummy module task"); |
| 191 | ++ |
| 192 | ++ task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); |
| 193 | ++ if (task_wdt_id < 0) { |
| 194 | ++ LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); |
| 195 | ++ SEND_FATAL_ERROR(); |
| 196 | ++ return; |
| 197 | ++ } |
| 198 | ++ |
| 199 | ++ smf_set_initial(SMF_CTX(&dummy_state), &states[STATE_RUNNING]); |
| 200 | ++ |
| 201 | ++ while (true) { |
| 202 | ++ err = task_wdt_feed(task_wdt_id); |
| 203 | ++ if (err) { |
| 204 | ++ LOG_ERR("Failed to feed watchdog: %d", err); |
| 205 | ++ SEND_FATAL_ERROR(); |
| 206 | ++ return; |
| 207 | ++ } |
| 208 | ++ |
| 209 | ++ err = zbus_sub_wait_msg(&dummy, |
| 210 | ++ &dummy_state.chan, |
| 211 | ++ dummy_state.msg_buf, |
| 212 | ++ zbus_wait_ms); |
| 213 | ++ if (err == -ENOMSG) { |
| 214 | ++ continue; |
| 215 | ++ } else if (err) { |
| 216 | ++ LOG_ERR("Failed to wait for message: %d", err); |
| 217 | ++ SEND_FATAL_ERROR(); |
| 218 | ++ return; |
| 219 | ++ } |
| 220 | ++ |
| 221 | ++ err = smf_run_state(SMF_CTX(&dummy_state)); |
| 222 | ++ if (err) { |
| 223 | ++ LOG_ERR("Failed to run state machine: %d", err); |
| 224 | ++ SEND_FATAL_ERROR(); |
| 225 | ++ return; |
| 226 | ++ } |
| 227 | ++ } |
| 228 | ++} |
| 229 | ++ |
| 230 | ++/* Define module thread */ |
| 231 | ++K_THREAD_DEFINE(dummy_task_id, |
| 232 | ++ CONFIG_APP_DUMMY_THREAD_STACK_SIZE, |
| 233 | ++ dummy_task, NULL, NULL, NULL, |
| 234 | ++ K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); |
| 235 | +diff --git a/app/src/modules/dummy/dummy.h b/app/src/modules/dummy/dummy.h |
| 236 | +new file mode 100644 |
| 237 | +index 0000000..4d3816e |
| 238 | +--- /dev/null |
| 239 | ++++ b/app/src/modules/dummy/dummy.h |
| 240 | +@@ -0,0 +1,41 @@ |
| 241 | ++/* |
| 242 | ++ * Copyright (c) 2025 Nordic Semiconductor ASA |
| 243 | ++ * |
| 244 | ++ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 245 | ++ */ |
| 246 | ++ |
| 247 | ++#ifndef _DUMMY_H_ |
| 248 | ++#define _DUMMY_H_ |
| 249 | ++ |
| 250 | ++#include <zephyr/kernel.h> |
| 251 | ++#include <zephyr/zbus/zbus.h> |
| 252 | ++ |
| 253 | ++#ifdef __cplusplus |
| 254 | ++extern "C" { |
| 255 | ++#endif |
| 256 | ++ |
| 257 | ++/* Module's zbus channel */ |
| 258 | ++ZBUS_CHAN_DECLARE(DUMMY_CHAN); |
| 259 | ++ |
| 260 | ++/* Module message types */ |
| 261 | ++enum dummy_msg_type { |
| 262 | ++ /* Output message types */ |
| 263 | ++ DUMMY_SAMPLE_RESPONSE = 0x1, |
| 264 | ++ |
| 265 | ++ /* Input message types */ |
| 266 | ++ DUMMY_SAMPLE_REQUEST, |
| 267 | ++}; |
| 268 | ++ |
| 269 | ++/* Module message structure */ |
| 270 | ++struct dummy_msg { |
| 271 | ++ enum dummy_msg_type type; |
| 272 | ++ int32_t value; |
| 273 | ++}; |
| 274 | ++ |
| 275 | ++#define MSG_TO_DUMMY_MSG(_msg) (*(const struct dummy_msg *)_msg) |
| 276 | ++ |
| 277 | ++#ifdef __cplusplus |
| 278 | ++} |
| 279 | ++#endif |
| 280 | ++ |
| 281 | ++#endif /* _DUMMY_H_ */ |
0 commit comments