zephyr/subsys/shell/backends/shell_dummy.c

139 lines
3.0 KiB
C
Raw Normal View History

/*
* Shell backend used for testing
*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/init.h>
SHELL_DUMMY_DEFINE(shell_transport_dummy);
SHELL_DEFINE(shell_dummy, CONFIG_SHELL_PROMPT_DUMMY, &shell_transport_dummy, 256,
0, SHELL_FLAG_OLF_CRLF);
static int init(const struct shell_transport *transport,
const void *config,
shell_transport_handler_t evt_handler,
void *context)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
if (sh_dummy->initialized) {
return -EINVAL;
}
sh_dummy->initialized = true;
return 0;
}
static int uninit(const struct shell_transport *transport)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
if (!sh_dummy->initialized) {
return -ENODEV;
}
sh_dummy->initialized = false;
return 0;
}
static int enable(const struct shell_transport *transport, bool blocking)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
if (!sh_dummy->initialized) {
return -ENODEV;
}
return 0;
}
static int write(const struct shell_transport *transport,
const void *data, size_t length, size_t *cnt)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
size_t store_cnt;
if (!sh_dummy->initialized) {
*cnt = 0;
return -ENODEV;
}
store_cnt = length;
if (sh_dummy->len + store_cnt >= sizeof(sh_dummy->buf)) {
store_cnt = sizeof(sh_dummy->buf) - sh_dummy->len - 1;
}
memcpy(sh_dummy->buf + sh_dummy->len, data, store_cnt);
sh_dummy->len += store_cnt;
*cnt = length;
return 0;
}
static int read(const struct shell_transport *transport,
void *data, size_t length, size_t *cnt)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)transport->ctx;
if (!sh_dummy->initialized) {
return -ENODEV;
}
*cnt = 0;
return 0;
}
const struct shell_transport_api shell_dummy_transport_api = {
.init = init,
.uninit = uninit,
.enable = enable,
.write = write,
.read = read
};
init: remove the need for a dummy device pointer in SYS_INIT functions The init infrastructure, found in `init.h`, is currently used by: - `SYS_INIT`: to call functions before `main` - `DEVICE_*`: to initialize devices They are all sorted according to an initialization level + a priority. `SYS_INIT` calls are really orthogonal to devices, however, the required function signature requires a `const struct device *dev` as a first argument. The only reason for that is because the same init machinery is used by devices, so we have something like: ```c struct init_entry { int (*init)(const struct device *dev); /* only set by DEVICE_*, otherwise NULL */ const struct device *dev; } ``` As a result, we end up with such weird/ugly pattern: ```c static int my_init(const struct device *dev) { /* always NULL! add ARG_UNUSED to avoid compiler warning */ ARG_UNUSED(dev); ... } ``` This is really a result of poor internals isolation. This patch proposes a to make init entries more flexible so that they can accept sytem initialization calls like this: ```c static int my_init(void) { ... } ``` This is achieved using a union: ```c union init_function { /* for SYS_INIT, used when init_entry.dev == NULL */ int (*sys)(void); /* for DEVICE*, used when init_entry.dev != NULL */ int (*dev)(const struct device *dev); }; struct init_entry { /* stores init function (either for SYS_INIT or DEVICE*) union init_function init_fn; /* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows * to know which union entry to call. */ const struct device *dev; } ``` This solution **does not increase ROM usage**, and allows to offer clean public APIs for both SYS_INIT and DEVICE*. Note that however, init machinery keeps a coupling with devices. **NOTE**: This is a breaking change! All `SYS_INIT` functions will need to be converted to the new signature. See the script offered in the following commit. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> init: convert SYS_INIT functions to the new signature Conversion scripted using scripts/utils/migrate_sys_init.py. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> manifest: update projects for SYS_INIT changes Update modules with updated SYS_INIT calls: - hal_ti - lvgl - sof - TraceRecorderSource Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: devicetree: devices: adjust test Adjust test according to the recently introduced SYS_INIT infrastructure. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no> tests: kernel: threads: adjust SYS_INIT call Adjust to the new signature: int (*init_fn)(void); Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 15:33:44 +08:00
static int enable_shell_dummy(void)
{
bool log_backend = CONFIG_SHELL_DUMMY_INIT_LOG_LEVEL > 0;
uint32_t level = (CONFIG_SHELL_DUMMY_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ?
CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_DUMMY_INIT_LOG_LEVEL;
static const struct shell_backend_config_flags cfg_flags =
SHELL_DEFAULT_BACKEND_CONFIG_FLAGS;
shell_init(&shell_dummy, NULL, cfg_flags, log_backend, level);
return 0;
}
SYS_INIT(enable_shell_dummy, POST_KERNEL, 0);
const struct shell *shell_backend_dummy_get_ptr(void)
{
return &shell_dummy;
}
const char *shell_backend_dummy_get_output(const struct shell *sh,
size_t *sizep)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)sh->iface->ctx;
sh_dummy->buf[sh_dummy->len] = '\0';
*sizep = sh_dummy->len;
sh_dummy->len = 0;
return sh_dummy->buf;
}
void shell_backend_dummy_clear_output(const struct shell *sh)
{
struct shell_dummy *sh_dummy = (struct shell_dummy *)sh->iface->ctx;
sh_dummy->buf[0] = '\0';
sh_dummy->len = 0;
}