Skip to content

Commit cee37af

Browse files
committed
treewide: Add shell commands for modules
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. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 6ee3743 commit cee37af

14 files changed

+187
-177
lines changed

app/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ rsource "src/modules/location/Kconfig.location"
2121
rsource "src/modules/led/Kconfig.led"
2222
rsource "src/modules/fota/Kconfig.fota"
2323
rsource "src/modules/environmental/Kconfig.environmental"
24-
rsource "src/modules/shell/Kconfig.shell"
2524
rsource "src/modules/button/Kconfig.button"
2625

2726
endmenu

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.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_shell.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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/shell/shell.h>
9+
#include <zephyr/logging/log.h>
10+
11+
LOG_MODULE_DECLARE(button, CONFIG_APP_BUTTON_LOG_LEVEL);
12+
13+
static int cmd_button_press(const struct shell *sh, size_t argc, char **argv)
14+
{
15+
int err;
16+
uint8_t button_number = strtol(argv[1], NULL, 10);
17+
18+
if (argc != 2) {
19+
(void)shell_print(sh, "Invalid number of arguments (%d)", argc);
20+
(void)shell_print(sh, "Usage: att_button_press <button_number>");
21+
return 1;
22+
}
23+
24+
if ((button_number != 1) && (button_number != 2)) {
25+
(void)shell_print(sh, "Invalid button number: %d", button_number);
26+
return 1;
27+
}
28+
29+
LOG_DBG("Button %d pressed", button_number);
30+
31+
err = zbus_chan_pub(&BUTTON_CHAN, &button_number, K_SECONDS(1));
32+
if (err) {
33+
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
34+
return 1;
35+
}
36+
37+
return 0;
38+
}
39+
40+
SHELL_CMD_ARG_REGISTER(att_button_press, NULL, "Asset Tracker Template Button Press", cmd_button_press, 1, 0);

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

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
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+
1016
config APP_CLOUD_CONFIRMABLE_MESSAGES
1117
bool "Use confirmable messages"
1218
help
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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/shell/shell.h>
9+
#include <zephyr/logging/log.h>
10+
#include <zephyr/zbus/zbus.h>
11+
#include <net/nrf_cloud_defs.h>
12+
#include <date_time.h>
13+
14+
LOG_MODULE_DECLARE(cloud, CONFIG_APP_CLOUD_LOG_LEVEL);
15+
16+
#define PAYLOAD_MSG_TEMPLATE \
17+
"{\"" NRF_CLOUD_JSON_MSG_TYPE_KEY "\":\"" NRF_CLOUD_JSON_MSG_TYPE_VAL_DATA "\"," \
18+
"\"" NRF_CLOUD_JSON_APPID_KEY "\":\"%s\"," \
19+
"\"" NRF_CLOUD_JSON_DATA_KEY "\":\"%s\"," \
20+
"\"" NRF_CLOUD_MSG_TIMESTAMP_KEY "\":%lld}"
21+
22+
static int cmd_publish(const struct shell *sh, size_t argc, char **argv)
23+
{
24+
int err;
25+
int64_t current_time;
26+
struct cloud_payload payload = { 0 };
27+
28+
if (argc != 3) {
29+
(void)shell_print(sh, "Invalid number of arguments (%d)", argc);
30+
(void)shell_print(sh, "Usage: zbus publish payload_chan <appid> <data>");
31+
return 1;
32+
}
33+
34+
err = date_time_now(&current_time);
35+
if (err) {
36+
(void)shell_print(sh, "Failed to get current time, error: %d", err);
37+
return 1;
38+
}
39+
40+
err = snprintk(payload.buffer, sizeof(payload.buffer),
41+
PAYLOAD_MSG_TEMPLATE,
42+
argv[1], argv[2], current_time);
43+
if (err < 0 || err >= sizeof(payload.buffer)) {
44+
(void)shell_print(sh, "Failed to format payload, error: %d", err);
45+
return 1;
46+
}
47+
48+
payload.buffer_len = err;
49+
50+
(void)shell_print(sh, "Sending on payload channel: %s (%d bytes)",
51+
payload.buffer, payload.buffer_len);
52+
53+
err = zbus_chan_pub(&PAYLOAD_CHAN, &payload, K_SECONDS(1));
54+
if (err) {
55+
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
56+
return 1;
57+
}
58+
59+
return 0;
60+
}
61+
62+
SHELL_CMD_ARG_REGISTER(att_cloud_publish, NULL, "Asset Tracker Template Cloud Shell", cmd_publish, 2, 0);

app/src/modules/network/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}/network.c)
8+
target_sources_ifdef(CONFIG_APP_NETWORK_SHELL app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/network_shell.c)
89
target_include_directories(app PRIVATE .)

app/src/modules/network/Kconfig.network

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
menu "Network"
88
depends on NRF_MODEM_LIB_NET_IF || (WIFI_NRF700X && WIFI_MGMT_EXT) || BOARD_NATIVE_POSIX
99

10+
config APP_NETWORK_SHELL
11+
bool "Network module shell commands"
12+
default y if SHELL
13+
help
14+
Enable shell commands for the network module.
15+
1016
config APP_NETWORK_THREAD_STACK_SIZE
1117
int "Thread stack size"
1218
default 2048
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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/shell/shell.h>
9+
#include <zephyr/logging/log.h>
10+
#include <zephyr/zbus/zbus.h>
11+
12+
LOG_MODULE_DECLARE(network, CONFIG_APP_NETWORK_LOG_LEVEL);
13+
14+
static int cmd_connect(const struct shell *sh, size_t argc, char **argv)
15+
{
16+
ARG_UNUSED(argc);
17+
ARG_UNUSED(argv);
18+
19+
int err;
20+
const struct network_msg msg = {
21+
.type = NETWORK_CONNECT,
22+
};
23+
24+
err = zbus_chan_pub(&NETWORK_CHAN, &msg, K_SECONDS(1));
25+
if (err) {
26+
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
27+
return 1;
28+
}
29+
30+
return 0;
31+
}
32+
33+
static int cmd_disconnect(const struct shell *sh, size_t argc, char **argv)
34+
{
35+
ARG_UNUSED(argc);
36+
ARG_UNUSED(argv);
37+
38+
int err;
39+
const struct network_msg msg = {
40+
.type = NETWORK_DISCONNECT,
41+
};
42+
43+
err = zbus_chan_pub(&NETWORK_CHAN, &msg, K_SECONDS(1));
44+
if (err) {
45+
(void)shell_print(sh, "zbus_chan_pub, error: %d", err);
46+
return 1;
47+
}
48+
49+
return 0;
50+
}
51+
52+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_cmds,
53+
SHELL_CMD(connect,
54+
NULL,
55+
"Connect to LTE",
56+
cmd_connect),
57+
SHELL_CMD(disconnect,
58+
NULL,
59+
"Disconnect from LTE",
60+
cmd_disconnect),
61+
SHELL_SUBCMD_SET_END
62+
);
63+
64+
SHELL_CMD_REGISTER(att_network, &sub_cmds, "Asset Tracker Template Network Shell", NULL);

app/src/modules/shell/CMakeLists.txt

-7
This file was deleted.

app/src/modules/shell/Kconfig.shell

-22
This file was deleted.

0 commit comments

Comments
 (0)