Skip to content

Commit b1c169e

Browse files
committed
treewide: Various cleanups
- Place shell commands for modules in separate files. This makes it easier to distribute the shell commands to the correct modules. Also removes the need to have a separate shell module. - Remove config and error channels - Update tests and references in modules to reflect the changes - Update tests to use new name and shell commands Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 986f351 commit b1c169e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+286
-380
lines changed

app/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010
project("Asset Tracker Template")
1111

1212
# Include files that are common for all modules
13-
add_subdirectory(src/common)
13+
target_include_directories(app PRIVATE src/common)
1414

1515
# Add main application source
1616
target_sources(app PRIVATE src/main.c)
@@ -26,7 +26,6 @@ add_subdirectory(src/modules/button)
2626
add_subdirectory_ifdef(CONFIG_APP_POWER src/modules/power)
2727
add_subdirectory_ifdef(CONFIG_APP_ENVIRONMENTAL src/modules/environmental)
2828
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
29-
add_subdirectory_ifdef(CONFIG_APP_SHELL src/modules/shell)
3029

3130
if (CONFIG_NRF_CLOUD_COAP_SEC_TAG GREATER_EQUAL 2147483648 AND CONFIG_NRF_CLOUD_COAP_SEC_TAG LESS_EQUAL 2147483667)
3231
message(WARNING "CONFIG_NRF_CLOUD_COAP_SEC_TAG is set to a developer security tag. "

app/Kconfig

-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66

77
menu "Asset Tracker Template"
88

9-
config APP_PAYLOAD_CHANNEL_BUFFER_MAX_SIZE
10-
int "Payload maximum buffer size"
11-
default 128
12-
help
13-
Maximum size of the buffer sent over the payload channel.
14-
Contains encoded CBOR data sampled and encoded in the various modules.
15-
169
rsource "src/Kconfig.main"
1710
rsource "src/modules/power/Kconfig.power"
1811
rsource "src/modules/network/Kconfig.network"
@@ -21,7 +14,6 @@ rsource "src/modules/location/Kconfig.location"
2114
rsource "src/modules/led/Kconfig.led"
2215
rsource "src/modules/fota/Kconfig.fota"
2316
rsource "src/modules/environmental/Kconfig.environmental"
24-
rsource "src/modules/shell/Kconfig.shell"
2517
rsource "src/modules/button/Kconfig.button"
2618

2719
endmenu

app/prj.conf

-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ CONFIG_APP_FOTA_LOG_LEVEL_DBG=y
200200
CONFIG_APP_LOCATION_LOG_LEVEL_DBG=y
201201
CONFIG_APP_CLOUD_LOG_LEVEL_DBG=y
202202
CONFIG_APP_NETWORK_LOG_LEVEL_DBG=y
203-
CONFIG_APP_SHELL_LOG_LEVEL_DBG=y
204203
CONFIG_APP_LOG_LEVEL_DBG=y
205204

206205
# Task Watchdog

app/src/common/CMakeLists.txt

-9
This file was deleted.

app/src/common/message_channel.h app/src/common/app_common.h

+4-48
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7-
#ifndef _MESSAGE_CHANNEL_H_
8-
#define _MESSAGE_CHANNEL_H_
7+
#ifndef _APP_COMMON_H_
8+
#define _APP_COMMON_H_
99

1010
#include <zephyr/kernel.h>
1111
#include <zephyr/sys/reboot.h>
@@ -24,8 +24,6 @@ extern "C" {
2424
* @param is_watchdog_timeout Boolean indicating if the macro was called upon a watchdog timeout.
2525
*/
2626
#define FATAL_ERROR_HANDLE(is_watchdog_timeout) do { \
27-
enum error_type type = ERROR_FATAL; \
28-
(void)zbus_chan_pub(&ERROR_CHAN, &type, K_SECONDS(10)); \
2927
LOG_PANIC(); \
3028
if (is_watchdog_timeout) { \
3129
IF_ENABLED(CONFIG_MEMFAULT, (MEMFAULT_SOFTWARE_WATCHDOG())); \
@@ -36,54 +34,12 @@ extern "C" {
3634

3735
/** @brief Macro used to handle fatal errors. */
3836
#define SEND_FATAL_ERROR() FATAL_ERROR_HANDLE(0)
37+
3938
/** @brief Macro used to handle watchdog timeouts. */
4039
#define SEND_FATAL_ERROR_WATCHDOG_TIMEOUT() FATAL_ERROR_HANDLE(1)
4140

42-
#define SEND_IRRECOVERABLE_ERROR() do { \
43-
enum error_type type = ERROR_IRRECOVERABLE; \
44-
(void)zbus_chan_pub(&ERROR_CHAN, &type, K_SECONDS(10)); \
45-
LOG_PANIC(); \
46-
k_sleep(K_SECONDS(10)); \
47-
} while (0)
48-
49-
enum time_status {
50-
TIME_AVAILABLE = 0x1,
51-
};
52-
53-
#define MSG_TO_TIME_STATUS(_msg) (*(const enum time_status *)_msg)
54-
55-
enum error_type {
56-
ERROR_FATAL = 0x1,
57-
ERROR_IRRECOVERABLE,
58-
};
59-
60-
struct configuration {
61-
/* LED */
62-
int led_red;
63-
int led_green;
64-
int led_blue;
65-
bool led_present;
66-
bool led_red_present;
67-
bool led_green_present;
68-
bool led_blue_present;
69-
70-
/* Configuration */
71-
bool gnss;
72-
uint64_t update_interval;
73-
bool config_present;
74-
bool gnss_present;
75-
bool update_interval_present;
76-
};
77-
78-
#define MSG_TO_CONFIGURATION(_msg) (*(const struct configuration *)_msg)
79-
80-
ZBUS_CHAN_DECLARE(
81-
CONFIG_CHAN,
82-
ERROR_CHAN
83-
);
84-
8541
#ifdef __cplusplus
8642
}
8743
#endif
8844

89-
#endif /* _MESSAGE_CHANNEL_H_ */
45+
#endif /* _APP_COMMON_H_ */

app/src/common/message_channel.c

-27
This file was deleted.

app/src/main.c

+8-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <zephyr/task_wdt/task_wdt.h>
1111
#include <zephyr/smf.h>
1212

13-
#include "message_channel.h"
13+
#include "app_common.h"
1414
#include "button.h"
1515
#include "network.h"
1616
#include "cloud_module.h"
@@ -35,7 +35,7 @@
3535
/* Register log module */
3636
LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
3737

38-
#define MAX_MSG_SIZE (MAX(sizeof(struct configuration), \
38+
#define MAX_MSG_SIZE (MAX(sizeof(struct cloud_shadow_response), \
3939
MAX(sizeof(struct cloud_payload), \
4040
/* Button channel payload size */ \
4141
MAX(sizeof(uint8_t), \
@@ -57,7 +57,6 @@ ZBUS_CHAN_DEFINE(TIMER_CHAN,
5757
);
5858

5959
/* Observe channels */
60-
ZBUS_CHAN_ADD_OBS(CONFIG_CHAN, main_subscriber, 0);
6160
ZBUS_CHAN_ADD_OBS(CLOUD_CHAN, main_subscriber, 0);
6261
ZBUS_CHAN_ADD_OBS(BUTTON_CHAN, main_subscriber, 0);
6362
ZBUS_CHAN_ADD_OBS(FOTA_CHAN, main_subscriber, 0);
@@ -453,26 +452,19 @@ static void triggering_run(void *o)
453452
smf_set_state(SMF_CTX(state_object), &states[STATE_IDLE]);
454453
return;
455454
}
456-
}
457-
458-
if (state_object->chan == &CONFIG_CHAN) {
459-
struct configuration config = MSG_TO_CONFIGURATION(state_object->msg_buf);
460-
461-
if (config.config_present) {
462-
int err;
463455

464-
LOG_DBG("Configuration update, new interval: %lld", config.update_interval);
456+
if (msg.type == CLOUD_SHADOW_RESPONSE) {
457+
/* Missing: Parse the interval received in the shadow response,
458+
* write to state object and schedule new interval
459+
*/
465460

466-
state_object->interval_sec = config.update_interval;
461+
int err = k_work_reschedule(&trigger_work,
462+
K_SECONDS(state_object->interval_sec));
467463

468-
err = k_work_reschedule(&trigger_work,
469-
K_SECONDS(state_object->interval_sec));
470464
if (err < 0) {
471465
LOG_ERR("k_work_reschedule, error: %d", err);
472466
SEND_FATAL_ERROR();
473467
}
474-
475-
return;
476468
}
477469
}
478470
}

app/src/modules/button/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
#
66

77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/button.c)
8+
target_sources_ifdef(CONFIG_APP_BUTTON_SHELL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/button_shell.c)
89
target_include_directories(app PRIVATE .)

app/src/modules/button/Kconfig.button

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
menu "Button"
88

9+
config APP_BUTTON_SHELL
10+
bool "Button module shell"
11+
default y if SHELL
12+
help
13+
Enable shell for the button module.
14+
915
module = APP_BUTTON
1016
module-str = Button
1117
source "subsys/logging/Kconfig.template.log_config"

app/src/modules/button/button.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <dk_buttons_and_leds.h>
1111
#include <date_time.h>
1212

13-
#include "message_channel.h"
13+
#include "app_common.h"
1414
#include "button.h"
1515

1616
/* Register log module */

app/src/modules/button/button_shell.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <stdlib.h>
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/shell/shell.h>
10+
#include <zephyr/logging/log.h>
11+
12+
#include "button.h"
13+
14+
LOG_MODULE_DECLARE(button, CONFIG_APP_BUTTON_LOG_LEVEL);
15+
16+
static int cmd_button_press(const struct shell *sh, size_t argc, char **argv)
17+
{
18+
int err;
19+
uint8_t button_number;
20+
21+
if (argc != 2) {
22+
(void)shell_print(sh, "Invalid number of arguments (%d)", argc);
23+
(void)shell_print(sh, "Usage: att_button_press <button_number>");
24+
return 1;
25+
}
26+
27+
button_number = (uint8_t)strtol(argv[1], NULL, 10);
28+
29+
if ((button_number != 1) && (button_number != 2)) {
30+
(void)shell_print(sh, "Invalid button number: %d", button_number);
31+
return 1;
32+
}
33+
34+
LOG_DBG("Button %d pressed", button_number);
35+
36+
err = zbus_chan_pub(&BUTTON_CHAN, &button_number, K_SECONDS(1));
37+
if (err) {
38+
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
39+
return 1;
40+
}
41+
42+
return 0;
43+
}
44+
45+
SHELL_CMD_REGISTER(att_button_press, NULL, "Asset Tracker Template Button CMDs", cmd_button_press);

app/src/modules/cloud/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
#
66

77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_module.c)
8+
target_sources_ifdef(CONFIG_APP_CLOUD_SHELL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cloud_module_shell.c)
89
target_include_directories(app PRIVATE .)

app/src/modules/cloud/Kconfig.cloud

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
menu "Cloud module"
88
depends on NRF_CLOUD_COAP
99

10+
config APP_CLOUD_SHELL
11+
bool "Enable cloud shell"
12+
default y if SHELL
13+
help
14+
Enable cloud shell commands.
15+
16+
config APP_CLOUD_PAYLOAD_BUFFER_MAX_SIZE
17+
int "Payload maximum buffer size"
18+
default 128
19+
help
20+
Maximum size of the buffer sent over the payload channel when sending RAW JSON messages
21+
to the cloud.
22+
23+
config APP_CLOUD_SHADOW_RESPONSE_BUFFER_MAX_SIZE
24+
int "Payload maximum buffer size"
25+
default 512
26+
help
27+
Maximum size of the buffer used to receive shadow responses from the cloud.
28+
1029
config APP_CLOUD_CONFIRMABLE_MESSAGES
1130
bool "Use confirmable messages"
1231
help

app/src/modules/cloud/cloud_module.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#endif /* CONFIG_MEMFAULT */
1919

2020
#include "cloud_module.h"
21-
#include "message_channel.h"
21+
#include "app_common.h"
2222
#include "network.h"
2323

2424
#if defined(CONFIG_APP_POWER)

app/src/modules/cloud/cloud_module.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ ZBUS_CHAN_DECLARE(
2121
);
2222

2323
struct cloud_payload {
24-
uint8_t buffer[CONFIG_APP_PAYLOAD_CHANNEL_BUFFER_MAX_SIZE];
24+
uint8_t buffer[CONFIG_APP_CLOUD_PAYLOAD_BUFFER_MAX_SIZE];
25+
size_t buffer_len;
26+
};
27+
28+
struct cloud_shadow_response {
29+
uint8_t buffer[CONFIG_APP_CLOUD_SHADOW_RESPONSE_BUFFER_MAX_SIZE];
2530
size_t buffer_len;
2631
};
2732

@@ -32,6 +37,7 @@ enum cloud_msg_type {
3237
CLOUD_CONNECTION_ATTEMPT_COUNT_REACHED,
3338
CLOUD_PAYLOAD_JSON,
3439
CLOUD_POLL_SHADOW,
40+
CLOUD_SHADOW_RESPONSE,
3541
};
3642

3743
struct cloud_msg {
@@ -41,6 +47,7 @@ struct cloud_msg {
4147

4248
#define MSG_TO_CLOUD_MSG(_msg) (*(const struct cloud_msg *)_msg)
4349
#define MSG_TO_PAYLOAD(_msg) ((struct cloud_payload *)_msg)
50+
#define MSG_TO_SHADOW_RESPONSE(_msg) (*(const struct cloud_shadow_response *)_msg)
4451

4552
#ifdef __cplusplus
4653
}

0 commit comments

Comments
 (0)