zephyr/modules/openthread/shell.c
Arkadiusz Balys 596844a2cb openthread: Move OpenThread implementation from net to modules
Move OpenThread-related code from
zephyr/subsys/net/l2/openthread/openthread.c to
zephyr/modules/openthread/platform/openthread.c.

The primary goal of this refactor is to enable the use
of OpenThread as an independent module, without the necessity
of Zephyr's networking layer.

This change is particularly beneficial for simple applications
that have their own implementation of the IEEE802.15.4 driver
and do not require a networking layer. These applications can
now disable Zephyr's L2 and IEEE802.15.4 shim layers and
directly use the OpenThread module, saving valuable kilobytes
of memory.

In this approach if the CONFIG_NET_L2_OPENTHREAD
Kconfig option is set, Zephyr's L2 and IEEE802.15.4 layers
will be used, and everything will function as before.
The main difference is the Zephyr's L2 layer now uses
the OpenThread module, no longer implementing it.

While most of the functions in include/net/openthread.h
have been deprecated, they are still available for use to
maintain backwards compatibility.

Signed-off-by: Arkadiusz Balys <arkadiusz.balys@nordicsemi.no>
2025-05-05 14:25:13 +02:00

97 lines
1.8 KiB
C

/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <stdio.h>
#include <zephyr/net/openthread.h>
#include <zephyr/sys/printk.h>
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_uart.h>
#include <openthread/cli.h>
#include <openthread/instance.h>
#include "platform-zephyr.h"
#define OT_SHELL_BUFFER_SIZE CONFIG_SHELL_CMD_BUFF_SIZE
static char rx_buffer[OT_SHELL_BUFFER_SIZE];
static const struct shell *shell_p;
static bool is_shell_initialized;
static int ot_console_cb(void *context, const char *format, va_list arg)
{
ARG_UNUSED(context);
if (shell_p == NULL) {
return 0;
}
shell_vfprintf(shell_p, SHELL_NORMAL, format, arg);
return 0;
}
#define SHELL_HELP_OT "OpenThread subcommands\n" \
"Use \"ot help\" to get the list of subcommands"
static int ot_cmd(const struct shell *sh, size_t argc, char *argv[])
{
char *buf_ptr = rx_buffer;
size_t buf_len = OT_SHELL_BUFFER_SIZE;
size_t arg_len = 0;
int i;
if (!is_shell_initialized) {
return -ENOEXEC;
}
for (i = 1; i < argc; i++) {
if (arg_len) {
buf_len -= arg_len + 1;
if (buf_len) {
buf_ptr[arg_len] = ' ';
}
buf_ptr += arg_len + 1;
}
arg_len = snprintk(buf_ptr, buf_len, "%s", argv[i]);
if (arg_len >= buf_len) {
shell_fprintf(sh, SHELL_WARNING,
"OT shell buffer full\n");
return -ENOEXEC;
}
}
if (i == argc) {
buf_len -= arg_len;
}
shell_p = sh;
openthread_mutex_lock();
otCliInputLine(rx_buffer);
openthread_mutex_unlock();
return 0;
}
SHELL_CMD_ARG_REGISTER(ot, NULL, SHELL_HELP_OT, ot_cmd, 2, 255);
void platformShellInit(otInstance *aInstance)
{
if (IS_ENABLED(CONFIG_SHELL_BACKEND_SERIAL)) {
shell_p = shell_backend_uart_get_ptr();
} else {
shell_p = NULL;
}
otCliInit(aInstance, ot_console_cb, NULL);
is_shell_initialized = true;
}