2018-10-02 20:47:20 +08:00
|
|
|
/*
|
2020-05-01 05:25:04 +08:00
|
|
|
* Shell backend used for testing
|
|
|
|
*
|
2018-10-02 20:47:20 +08:00
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/shell/shell_dummy.h>
|
|
|
|
#include <zephyr/init.h>
|
2018-10-02 20:47:20 +08:00
|
|
|
|
|
|
|
SHELL_DUMMY_DEFINE(shell_transport_dummy);
|
2022-11-22 22:56:41 +08:00
|
|
|
SHELL_DEFINE(shell_dummy, CONFIG_SHELL_PROMPT_DUMMY, &shell_transport_dummy, 256,
|
2019-03-22 23:22:24 +08:00
|
|
|
0, SHELL_FLAG_OLF_CRLF);
|
2018-10-02 20:47:20 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-10-17 18:16:06 +08:00
|
|
|
if (!sh_dummy->initialized) {
|
|
|
|
return -ENODEV;
|
2018-10-02 20:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-10-17 18:16:06 +08:00
|
|
|
if (!sh_dummy->initialized) {
|
|
|
|
return -ENODEV;
|
2018-10-02 20:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-05-01 05:25:04 +08:00
|
|
|
size_t store_cnt;
|
2018-10-02 20:47:20 +08:00
|
|
|
|
|
|
|
if (!sh_dummy->initialized) {
|
|
|
|
*cnt = 0;
|
2018-10-17 18:16:06 +08:00
|
|
|
return -ENODEV;
|
2018-10-02 20:47:20 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 05:25:04 +08:00
|
|
|
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;
|
|
|
|
|
2018-10-02 20:47:20 +08:00
|
|
|
*cnt = length;
|
2020-05-01 05:25:04 +08:00
|
|
|
|
2018-10-02 20:47:20 +08:00
|
|
|
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;
|
|
|
|
|
2018-10-17 18:16:06 +08:00
|
|
|
if (!sh_dummy->initialized) {
|
|
|
|
return -ENODEV;
|
2018-10-02 20:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*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)
|
2018-10-02 20:47:20 +08:00
|
|
|
{
|
2023-02-09 16:02:16 +08:00
|
|
|
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;
|
2021-09-20 20:21:57 +08:00
|
|
|
static const struct shell_backend_config_flags cfg_flags =
|
|
|
|
SHELL_DEFAULT_BACKEND_CONFIG_FLAGS;
|
2023-02-09 16:02:16 +08:00
|
|
|
|
|
|
|
shell_init(&shell_dummy, NULL, cfg_flags, log_backend, level);
|
|
|
|
|
2018-10-02 20:47:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SYS_INIT(enable_shell_dummy, POST_KERNEL, 0);
|
|
|
|
|
|
|
|
const struct shell *shell_backend_dummy_get_ptr(void)
|
|
|
|
{
|
2018-11-19 20:24:25 +08:00
|
|
|
return &shell_dummy;
|
2018-10-02 20:47:20 +08:00
|
|
|
}
|
2020-05-01 05:25:04 +08:00
|
|
|
|
2023-04-14 00:59:37 +08:00
|
|
|
const char *shell_backend_dummy_get_output(const struct shell *sh,
|
2020-05-01 05:25:04 +08:00
|
|
|
size_t *sizep)
|
|
|
|
{
|
2023-04-14 00:59:37 +08:00
|
|
|
struct shell_dummy *sh_dummy = (struct shell_dummy *)sh->iface->ctx;
|
2020-05-01 05:25:04 +08:00
|
|
|
|
|
|
|
sh_dummy->buf[sh_dummy->len] = '\0';
|
|
|
|
*sizep = sh_dummy->len;
|
|
|
|
sh_dummy->len = 0;
|
|
|
|
|
|
|
|
return sh_dummy->buf;
|
|
|
|
}
|
2020-08-03 18:47:06 +08:00
|
|
|
|
2023-04-14 00:59:37 +08:00
|
|
|
void shell_backend_dummy_clear_output(const struct shell *sh)
|
2020-08-03 18:47:06 +08:00
|
|
|
{
|
2023-04-14 00:59:37 +08:00
|
|
|
struct shell_dummy *sh_dummy = (struct shell_dummy *)sh->iface->ctx;
|
2020-08-03 18:47:06 +08:00
|
|
|
|
|
|
|
sh_dummy->buf[0] = '\0';
|
|
|
|
sh_dummy->len = 0;
|
|
|
|
}
|