net: lwm2m: adds LwM2M specific shell command

First available subcommand is for doing a send operation. Send operation
is supported by the LwM2M version 1.1.

Signed-off-by: Veijo Pesonen <veijo.pesonen@nordicsemi.no>
This commit is contained in:
Veijo Pesonen 2022-03-30 10:05:21 +03:00 committed by Carles Cufí
commit d1751cafd3
5 changed files with 104 additions and 0 deletions

View file

@ -1218,5 +1218,13 @@ char *lwm2m_path_log_strdup(char *buf, struct lwm2m_obj_path *path);
int lwm2m_engine_send(struct lwm2m_ctx *ctx, char const *path_list[], uint8_t path_list_size, int lwm2m_engine_send(struct lwm2m_ctx *ctx, char const *path_list[], uint8_t path_list_size,
bool confirmation_request); bool confirmation_request);
/** 
* @brief Returns LwM2M client context
*
* @return ctx LwM2M context
*
*/
struct lwm2m_ctx *lwm2m_rd_client_ctx(void);
#endif /* ZEPHYR_INCLUDE_NET_LWM2M_H_ */ #endif /* ZEPHYR_INCLUDE_NET_LWM2M_H_ */
/**@} */ /**@} */

View file

@ -94,4 +94,9 @@ zephyr_library_sources_ifdef(CONFIG_LWM2M_UCIFI_BATTERY
ucifi_battery.c ucifi_battery.c
) )
# Shell commands
zephyr_library_sources_ifdef(CONFIG_LWM2M_SHELL
lwm2m_shell.c
)
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS) zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)

View file

@ -32,6 +32,13 @@ config LWM2M_VERSION_1_1
endchoice endchoice
config LWM2M_SHELL
bool "LwM2M shell utilities"
select SHELL
help
Activate shell module that provides LwM2M commands like
send to the console.
config LWM2M_DTLS_SUPPORT config LWM2M_DTLS_SUPPORT
bool "DTLS support in the LwM2M client" bool "DTLS support in the LwM2M client"
select TLS_CREDENTIALS select TLS_CREDENTIALS

View file

@ -1149,6 +1149,11 @@ void lwm2m_rd_client_update(void)
engine_trigger_update(false); engine_trigger_update(false);
} }
struct lwm2m_ctx *lwm2m_rd_client_ctx(void)
{
return client.ctx;
}
static int lwm2m_rd_client_init(const struct device *dev) static int lwm2m_rd_client_init(const struct device *dev)
{ {
k_mutex_init(&client.mutex); k_mutex_init(&client.mutex);

View file

@ -0,0 +1,79 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_MODULE_NAME net_lwm2m_shell
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include <stddef.h>
#include <stdlib.h>
#include <kernel.h>
#include <net/lwm2m.h>
#include <shell/shell.h>
#define LWM2M_HELP_CMD "LwM2M commands"
#define LWM2M_HELP_SEND "LwM2M SEND operation\nsend [OPTION]... [PATH]...\n" \
"-n\tSend as non-confirmable\n" \
"Paths are inserted without leading '/'\n" \
"Root-level operation is unsupported"
static int cmd_send(const struct shell *sh, size_t argc, char **argv)
{
int ret = 0;
struct lwm2m_ctx *ctx = lwm2m_rd_client_ctx();
int path_cnt = argc - 1;
bool confirmable = true;
int ignore_cnt = 1; /* Subcmd + arguments preceding the path list */
if (!ctx) {
shell_error(sh, "no lwm2m context yet\n");
return -ENOEXEC;
}
if (argc < 2) {
shell_error(sh, "no arguments or path(s)\n");
shell_help(sh);
return -EINVAL;
}
if (strcmp(argv[1], "-n") == 0) {
confirmable = false;
path_cnt--;
ignore_cnt++;
}
if ((argc - ignore_cnt) == 0) {
shell_error(sh, "no path(s)\n");
shell_help(sh);
return -EINVAL;
}
for (int idx = ignore_cnt; idx < argc; idx++) {
if (argv[idx][0] < '0' || argv[idx][0] > '9') {
shell_error(sh, "invalid path: %s\n", argv[idx]);
shell_help(sh);
return -EINVAL;
}
}
ret = lwm2m_engine_send(ctx, (const char **)&(argv[ignore_cnt]),
path_cnt, confirmable);
if (ret < 0) {
shell_error(sh, "can't do send operation, request failed\n");
return -ENOEXEC;
}
return 0;
}
SHELL_STATIC_SUBCMD_SET_CREATE(sub_lwm2m,
SHELL_COND_CMD_ARG(CONFIG_LWM2M_VERSION_1_1, send, NULL,
LWM2M_HELP_SEND, cmd_send, 1, 9),
SHELL_SUBCMD_SET_END);
SHELL_COND_CMD_ARG_REGISTER(CONFIG_LWM2M_SHELL, lwm2m, &sub_lwm2m, LWM2M_HELP_CMD, NULL, 1, 0);