Skip to content

Commit 24d00ae

Browse files
committed
temp
1 parent d878a03 commit 24d00ae

14 files changed

Lines changed: 1002 additions & 105 deletions

File tree

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
2727
add_subdirectory_ifdef(CONFIG_APP_LOCATION src/modules/location)
2828
add_subdirectory_ifdef(CONFIG_APP_CLOUD src/modules/cloud)
2929
add_subdirectory_ifdef(CONFIG_APP_FOTA src/modules/fota)
30+
add_subdirectory_ifdef(CONFIG_APP_DUMMY src/modules/dummy)

app/boards/thingy91x_nrf9151_ns.overlay

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
status = "okay";
1111
};
1212

13+
&magnetometer {
14+
status = "okay";
15+
};
16+
1317
/ {
1418
chosen {
1519
zephyr,wifi = &nordic_wlan0;

app/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ CONFIG_DK_LIBRARY=y
1414
CONFIG_ZVFS_OPEN_MAX=10
1515
CONFIG_PWM=y
1616

17+
# TFM logging over uart0
18+
CONFIG_TFM_LOG_LEVEL_SILENCE=n
19+
CONFIG_TFM_SECURE_UART0=y
20+
CONFIG_TFM_SECURE_UART_SHARE_INSTANCE=y
21+
CONFIG_TFM_EXCEPTION_INFO_DUMP=y
22+
CONFIG_TFM_SPM_LOG_LEVEL_DEBUG=y
23+
1724
# Heap and stacks
1825
CONFIG_MAIN_STACK_SIZE=1856
1926
# Extended AT host/monitor stack/heap sizes since some nrf_cloud credentials are longer than 1024 bytes.

app/src/modules/cloud/cloud_module.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ LOG_MODULE_REGISTER(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
3838

3939
#define CUSTOM_JSON_APPID_VAL_CONEVAL "CONEVAL"
4040
#define CUSTOM_JSON_APPID_VAL_BATTERY "BATTERY"
41+
#define CUSTOM_JSON_APPID_VAL_MAGNETIC "MAGNETIC_FIELD"
4142

4243
BUILD_ASSERT(CONFIG_APP_CLOUD_WATCHDOG_TIMEOUT_SECONDS >
4344
CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS,
@@ -672,6 +673,33 @@ static void state_connected_ready_run(void *o)
672673
return;
673674
}
674675

676+
char message[100] = { 0 };
677+
678+
err = snprintk(message, sizeof(message),
679+
"%.2f %.2f %.2f",
680+
msg.magnetic_field[0],
681+
msg.magnetic_field[1],
682+
msg.magnetic_field[2]);
683+
if (err < 0 || err >= sizeof(message)) {
684+
LOG_ERR("snprintk, error: %d", err);
685+
SEND_FATAL_ERROR();
686+
return;
687+
}
688+
689+
err = nrf_cloud_coap_message_send(CUSTOM_JSON_APPID_VAL_MAGNETIC,
690+
message,
691+
false,
692+
NRF_CLOUD_NO_TIMESTAMP,
693+
confirmable);
694+
if (err == -ENETUNREACH) {
695+
LOG_WRN("Network is unreachable, error: %d", err);
696+
return;
697+
} else if (err) {
698+
LOG_ERR("nrf_cloud_coap_message_send, error: %d", err);
699+
SEND_FATAL_ERROR();
700+
return;
701+
}
702+
675703
return;
676704
}
677705
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
target_sources_ifdef(CONFIG_APP_DUMMY app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/dummy.c)
8+
target_include_directories(app PRIVATE .)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menuconfig APP_DUMMY
8+
bool "Dummy module"
9+
default y
10+
help
11+
Enable the dummy module.
12+
13+
if APP_DUMMY
14+
15+
config APP_DUMMY_THREAD_STACK_SIZE
16+
int "Dummy module thread stack size"
17+
default 2048
18+
help
19+
Stack size for the dummy module thread.
20+
21+
config APP_DUMMY_WATCHDOG_TIMEOUT_SECONDS
22+
int "Dummy module watchdog timeout in seconds"
23+
default 30
24+
help
25+
Watchdog timeout for the dummy module.
26+
27+
config APP_DUMMY_MSG_PROCESSING_TIMEOUT_SECONDS
28+
int "Dummy module message processing timeout in seconds"
29+
default 5
30+
help
31+
Maximum time allowed for processing a single message in the dummy module.
32+
33+
module = APP_DUMMY
34+
module-str = DUMMY
35+
source "subsys/logging/Kconfig.template.log_config"
36+
37+
endif # APP_DUMMY

app/src/modules/dummy/dummy.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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);

app/src/modules/dummy/dummy.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef _DUMMY_H_
8+
#define _DUMMY_H_
9+
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/zbus/zbus.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/* Module's zbus channel */
18+
ZBUS_CHAN_DECLARE(DUMMY_CHAN);
19+
20+
/* Module message types */
21+
enum dummy_msg_type {
22+
/* Output message types */
23+
DUMMY_SAMPLE_RESPONSE = 0x1,
24+
25+
/* Input message types */
26+
DUMMY_SAMPLE_REQUEST,
27+
};
28+
29+
/* Module message structure */
30+
struct dummy_msg {
31+
enum dummy_msg_type type;
32+
int32_t value;
33+
};
34+
35+
#define MSG_TO_DUMMY_MSG(_msg) (*(const struct dummy_msg *)_msg)
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
#endif /* _DUMMY_H_ */

app/src/modules/environmental/Kconfig.environmental

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if APP_ENVIRONMENTAL
1313

1414
config APP_ENVIRONMENTAL_THREAD_STACK_SIZE
1515
int "Thread stack size"
16-
default 768
16+
default 1024
1717

1818
config APP_ENVIRONMENTAL_WATCHDOG_TIMEOUT_SECONDS
1919
int "Watchdog timeout"

0 commit comments

Comments
 (0)