-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdummy-module.patch
More file actions
360 lines (358 loc) · 9.3 KB
/
dummy-module.patch
File metadata and controls
360 lines (358 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index e1948cc..8d20bcd 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -27,3 +27,4 @@ add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
add_subdirectory_ifdef(CONFIG_APP_POWER src/modules/power)
add_subdirectory_ifdef(CONFIG_APP_ENVIRONMENTAL src/modules/environmental)
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
+add_subdirectory_ifdef(CONFIG_APP_DUMMY src/modules/dummy)
diff --git a/app/Kconfig b/app/Kconfig
index dad4070..684fe73 100644
--- a/app/Kconfig
+++ b/app/Kconfig
@@ -15,6 +15,7 @@ rsource "src/modules/led/Kconfig.led"
rsource "src/modules/fota/Kconfig.fota"
rsource "src/modules/environmental/Kconfig.environmental"
rsource "src/modules/button/Kconfig.button"
+rsource "src/modules/dummy/Kconfig.dummy"
endmenu
diff --git a/app/src/modules/dummy/CMakeLists.txt b/app/src/modules/dummy/CMakeLists.txt
new file mode 100644
index 0000000..8b69448
--- /dev/null
+++ b/app/src/modules/dummy/CMakeLists.txt
@@ -0,0 +1,9 @@
+#
+# Copyright (c) 2025 Nordic Semiconductor ASA
+#
+# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
+#
+
+target_sources_ifdef(CONFIG_APP_DUMMY app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/dummy.c)
+target_sources_ifdef(CONFIG_APP_DUMMY app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/dummy_shell.c)
+target_include_directories(app PRIVATE .)
diff --git a/app/src/modules/dummy/Kconfig.dummy b/app/src/modules/dummy/Kconfig.dummy
new file mode 100644
index 0000000..2319814
--- /dev/null
+++ b/app/src/modules/dummy/Kconfig.dummy
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2024 Nordic Semiconductor
+#
+# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
+#
+
+menuconfig APP_DUMMY
+ bool "Dummy module"
+ default y
+ help
+ Enable the dummy module.
+
+if APP_DUMMY
+
+config APP_DUMMY_THREAD_STACK_SIZE
+ int "Dummy module thread stack size"
+ default 2048
+ help
+ Stack size for the dummy module thread.
+
+config APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS
+ int "Dummy module watchdog timeout in seconds"
+ default 30
+ help
+ Watchdog timeout for the dummy module.
+
+config APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS
+ int "Dummy module message processing timeout in seconds"
+ default 5
+ help
+ Maximum time allowed for processing a single message in the dummy module.
+
+module = APP_DUMMY
+module-str = DUMMY
+source "subsys/logging/Kconfig.template.log_config"
+
+endif # APP_DUMMY
diff --git a/app/src/modules/dummy/dummy.c b/app/src/modules/dummy/dummy.c
new file mode 100644
index 0000000..c2733b5
--- /dev/null
+++ b/app/src/modules/dummy/dummy.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2025 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
+ */
+
+#include <zephyr/kernel.h>
+#include <zephyr/logging/log.h>
+#include <zephyr/zbus/zbus.h>
+#include <zephyr/task_wdt/task_wdt.h>
+#include <zephyr/smf.h>
+
+#include "app_common.h"
+#include "dummy.h"
+
+/* Register log module */
+LOG_MODULE_REGISTER(dummy_module, CONFIG_APP_DUMMY_LOG_LEVEL);
+
+/* Define module's zbus channel */
+ZBUS_CHAN_DEFINE(DUMMY_CHAN,
+ struct dummy_msg,
+ NULL,
+ NULL,
+ ZBUS_OBSERVERS_EMPTY,
+ ZBUS_MSG_INIT(0)
+);
+
+/* Register zbus subscriber */
+ZBUS_MSG_SUBSCRIBER_DEFINE(dummy);
+
+/* Add subscriber to channel */
+ZBUS_CHAN_ADD_OBS(DUMMY_CHAN, dummy, 0);
+
+#define MAX_MSG_SIZE sizeof(struct dummy_msg)
+
+BUILD_ASSERT(CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS >
+ CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS,
+ "Watchdog timeout must be greater than maximum message processing time");
+
+/* State machine states */
+enum dummy_module_state {
+ STATE_RUNNING,
+};
+
+/* Module state structure */
+struct dummy_state {
+ /* State machine context (must be first) */
+ struct smf_ctx ctx;
+
+ /* Last received zbus channel */
+ const struct zbus_channel *chan;
+
+ /* Message buffer */
+ uint8_t msg_buf[MAX_MSG_SIZE];
+
+ /* Current counter value */
+ int32_t current_value;
+};
+
+/* Forward declarations */
+static void state_running_run(void *o);
+
+/* State machine definition */
+static const struct smf_state states[] = {
+ [STATE_RUNNING] = SMF_CREATE_STATE(NULL, state_running_run, NULL, NULL, NULL),
+};
+
+/* Watchdog callback */
+static void task_wdt_callback(int channel_id, void *user_data)
+{
+ LOG_ERR("Watchdog expired, Channel: %d, Thread: %s",
+ channel_id, k_thread_name_get((k_tid_t)user_data));
+
+ SEND_FATAL_ERROR_WATCHDOG_TIMEOUT();
+}
+
+/* State machine handlers */
+static void state_running_run(void *o)
+{
+ struct dummy_state *state_object = (struct dummy_state *)o;
+
+ if (&DUMMY_CHAN == state_object->chan) {
+ struct dummy_msg msg = MSG_TO_DUMMY_MSG(state_object->msg_buf);
+
+ if (msg.type == DUMMY_SAMPLE_REQUEST) {
+ LOG_DBG("Received sample request");
+ state_object->current_value++;
+
+ struct dummy_msg response = {
+ .type = DUMMY_SAMPLE_RESPONSE,
+ .value = state_object->current_value
+ };
+
+ int err = zbus_chan_pub(&DUMMY_CHAN, &response, K_NO_WAIT);
+ if (err) {
+ LOG_ERR("Failed to publish response: %d", err);
+ SEND_FATAL_ERROR();
+ return;
+ }
+ }
+ }
+}
+
+/* Module task function */
+static void dummy_task(void)
+{
+ int err;
+ int task_wdt_id;
+ const uint32_t wdt_timeout_ms =
+ (CONFIG_APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS * MSEC_PER_SEC);
+ const uint32_t execution_time_ms =
+ (CONFIG_APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC);
+ const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms);
+ struct dummy_state dummy_state = {
+ .current_value = 0
+ };
+
+ LOG_DBG("Starting dummy module task");
+
+ task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get());
+ if (task_wdt_id < 0) {
+ LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id);
+ SEND_FATAL_ERROR();
+ return;
+ }
+
+ smf_set_initial(SMF_CTX(&dummy_state), &states[STATE_RUNNING]);
+
+ while (true) {
+ err = task_wdt_feed(task_wdt_id);
+ if (err) {
+ LOG_ERR("Failed to feed watchdog: %d", err);
+ SEND_FATAL_ERROR();
+ return;
+ }
+
+ err = zbus_sub_wait_msg(&dummy,
+ &dummy_state.chan,
+ dummy_state.msg_buf,
+ zbus_wait_ms);
+ if (err == -ENOMSG) {
+ continue;
+ } else if (err) {
+ LOG_ERR("Failed to wait for message: %d", err);
+ SEND_FATAL_ERROR();
+ return;
+ }
+
+ err = smf_run_state(SMF_CTX(&dummy_state));
+ if (err) {
+ LOG_ERR("Failed to run state machine: %d", err);
+ SEND_FATAL_ERROR();
+ return;
+ }
+ }
+}
+
+/* Define module thread */
+K_THREAD_DEFINE(dummy_task_id,
+ CONFIG_APP_DUMMY_THREAD_STACK_SIZE,
+ dummy_task, NULL, NULL, NULL,
+ K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);
diff --git a/app/src/modules/dummy/dummy.h b/app/src/modules/dummy/dummy.h
new file mode 100644
index 0000000..4d3816e
--- /dev/null
+++ b/app/src/modules/dummy/dummy.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2025 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
+ */
+
+#ifndef _DUMMY_H_
+#define _DUMMY_H_
+
+#include <zephyr/kernel.h>
+#include <zephyr/zbus/zbus.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Module's zbus channel */
+ZBUS_CHAN_DECLARE(DUMMY_CHAN);
+
+/* Module message types */
+enum dummy_msg_type {
+ /* Output message types */
+ DUMMY_SAMPLE_RESPONSE = 0x1,
+
+ /* Input message types */
+ DUMMY_SAMPLE_REQUEST,
+};
+
+/* Module message structure */
+struct dummy_msg {
+ enum dummy_msg_type type;
+ int32_t value;
+};
+
+#define MSG_TO_DUMMY_MSG(_msg) (*(const struct dummy_msg *)_msg)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _DUMMY_H_ */
diff --git a/app/src/modules/dummy/dummy_shell.c b/app/src/modules/dummy/dummy_shell.c
new file mode 100644
index 0000000..9a82441
--- /dev/null
+++ b/app/src/modules/dummy/dummy_shell.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2025 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
+ */
+
+#include <zephyr/kernel.h>
+#include <zephyr/shell/shell.h>
+#include <zephyr/zbus/zbus.h>
+#include <zephyr/logging/log.h>
+
+#include "dummy.h"
+
+/* Register log module */
+LOG_MODULE_REGISTER(dummy_shell_module, CONFIG_APP_DUMMY_LOG_LEVEL);
+
+/* Forward declarations */
+static void dummy_shell_callback(const struct zbus_channel *chan);
+
+/* Register zbus callback */
+ZBUS_LISTENER_DEFINE(dummy_shell, dummy_shell_callback);
+
+/* Add subscriber to channel */
+ZBUS_CHAN_ADD_OBS(DUMMY_CHAN, dummy_shell, 0);
+
+/* Static variable to store shell reference */
+static const struct shell *dummy_shell_ref;
+
+/* Shell command handler for sending dummy request */
+static int cmd_dummy_request(const struct shell *shell, size_t argc, char **argv)
+{
+ struct dummy_msg request = {
+ .type = DUMMY_SAMPLE_REQUEST,
+ };
+
+ /* Store the shell reference */
+ dummy_shell_ref = shell;
+
+ int err = zbus_chan_pub(&DUMMY_CHAN, &request, K_SECONDS(1));
+ if (err) {
+ shell_error(shell, "Failed to send request: %d", err);
+ return err;
+ }
+
+ shell_print(shell, "Dummy request sent");
+ return 0;
+}
+
+/* Shell command handler for receiving dummy response */
+static void dummy_shell_callback(const struct zbus_channel *chan)
+{
+ const struct dummy_msg *response = zbus_chan_const_msg(chan);
+
+ if (response->type == DUMMY_SAMPLE_RESPONSE) {
+ shell_print(dummy_shell_ref, "Response received: %d", response->value);
+ }
+}
+
+/* Register shell command */
+SHELL_CMD_REGISTER(att_dummy_request, NULL, "Asset Tracker Template Dummy CMDs", cmd_dummy_request);