1 /*
2 * Copyright 2023 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/version.h>
8
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/uart.h>
12 #include <zephyr/drivers/serial/uart_emul.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/shell/shell.h>
15 #include <zephyr/shell/shell_backend.h>
16 #include <zephyr/shell/shell_uart.h>
17 #include <zephyr/ztest.h>
18
19 #define EMUL_UART_NODE(i) DT_NODELABEL(euart##i)
20 #define EMUL_UART_TX_FIFO_SIZE(i) DT_PROP(DT_NODELABEL(euart##i), tx_fifo_size)
21 #define SAMPLE_DATA_SIZE EMUL_UART_TX_FIFO_SIZE(0)
22
23 struct shell_backend_uart_fixture {
24 const struct device *dev;
25 };
26
before(void * f)27 static void before(void *f)
28 {
29 struct shell_backend_uart_fixture *fixture = f;
30
31 uart_irq_tx_enable(fixture->dev);
32 uart_irq_rx_enable(fixture->dev);
33
34 uart_err_check(fixture->dev);
35 }
36
after(void * f)37 static void after(void *f)
38 {
39 struct shell_backend_uart_fixture *fixture = f;
40
41 uart_irq_tx_disable(fixture->dev);
42 uart_irq_rx_disable(fixture->dev);
43
44 uart_emul_flush_rx_data(fixture->dev);
45 uart_emul_flush_tx_data(fixture->dev);
46 }
47
ZTEST(shell_backend_uart,test_backends_count)48 ZTEST(shell_backend_uart, test_backends_count)
49 {
50 /* 2 backends: 1 for zephyr,shell-uart, another 1 is created in the test */
51 zassert_equal(shell_backend_count_get(), 2, "Expecting 2, got %d",
52 shell_backend_count_get());
53 }
54
ZTEST_F(shell_backend_uart,test_backend_euart0_version)55 ZTEST_F(shell_backend_uart, test_backend_euart0_version)
56 {
57 uint8_t tx_content[SAMPLE_DATA_SIZE] = {0};
58
59 uart_emul_put_rx_data(fixture->dev, "kernel version\n", sizeof("kernel version\n"));
60
61 /* Let the shell to run */
62 k_usleep(50);
63
64 uart_emul_get_tx_data(fixture->dev, tx_content, SAMPLE_DATA_SIZE);
65 zassert_mem_equal(tx_content, "Zephyr version " KERNEL_VERSION_STRING,
66 strlen("Zephyr version " KERNEL_VERSION_STRING));
67 }
68
ZTEST_F(shell_backend_uart,test_backend_euart0_cycles)69 ZTEST_F(shell_backend_uart, test_backend_euart0_cycles)
70 {
71 uint8_t tx_content[SAMPLE_DATA_SIZE] = {0};
72
73 uart_emul_put_rx_data(fixture->dev, "kernel cycles\n", sizeof("kernel cycles\n"));
74
75 /* Let the shell to run */
76 k_usleep(50);
77
78 uart_emul_get_tx_data(fixture->dev, tx_content, SAMPLE_DATA_SIZE);
79 zassert_mem_equal(tx_content, "cycles: ", strlen("cycles: "));
80 }
81
ZTEST_F(shell_backend_uart,test_backend_euart0_uptime)82 ZTEST_F(shell_backend_uart, test_backend_euart0_uptime)
83 {
84 uint8_t tx_content[SAMPLE_DATA_SIZE] = {0};
85
86 uart_emul_put_rx_data(fixture->dev, "kernel uptime\n", sizeof("kernel uptime\n"));
87
88 /* Let the shell to run */
89 k_usleep(50);
90
91 uart_emul_get_tx_data(fixture->dev, tx_content, SAMPLE_DATA_SIZE);
92 zassert_mem_equal(tx_content, "Uptime: ", strlen("Uptime: "));
93 }
94
enable_shell_euart0(const struct device * euart0,const struct shell * sh)95 static int enable_shell_euart0(const struct device *euart0, const struct shell *sh)
96 {
97 static const struct shell_backend_config_flags cfg_flags = {0};
98
99 if (!device_is_ready(euart0)) {
100 return -ENODEV;
101 }
102
103 return shell_init(sh, euart0, cfg_flags, false, 0);
104 }
105
106 SHELL_UART_DEFINE(shell_transport_euart0);
107 SHELL_DEFINE(shell_euart0, "", &shell_transport_euart0,
108 CONFIG_SHELL_BACKEND_SERIAL_LOG_MESSAGE_QUEUE_SIZE,
109 CONFIG_SHELL_BACKEND_SERIAL_LOG_MESSAGE_QUEUE_TIMEOUT, SHELL_FLAG_OLF_CRLF);
110
setup(void)111 static void *setup(void)
112 {
113 uint8_t tx_content[SAMPLE_DATA_SIZE] = {0};
114 static struct shell_backend_uart_fixture fixture = {
115 .dev = DEVICE_DT_GET(DT_NODELABEL(euart0)),
116 };
117
118 zassert_not_null(fixture.dev);
119 enable_shell_euart0(fixture.dev, &shell_euart0);
120
121 /* Let the shell backend initialize. */
122 k_usleep(10);
123
124 /* get the shell startup newline */
125 uart_emul_get_tx_data(fixture.dev, tx_content, SAMPLE_DATA_SIZE);
126 zassert_mem_equal(tx_content, "\r\n", strlen("\r\n"));
127
128 return &fixture;
129 }
130
131 ZTEST_SUITE(shell_backend_uart, NULL, setup, before, after, NULL);
132