mgmt: smp: shell: use net_buf API for storing UART SMP fragments

So far there was a simple char array used as buffer, with additional
variable representing number of bytes already written into it. After
full frame was written, a boolean flag was simply set to notify thread
about being ready to be processed. There was however no mechanism
implemented to prevent new incoming bytes from overwriting such buffer
before (or during) being processed.

Use net_buf to store temporary frame. Define dedicated net_buf_pool,
from which such buffer will be allocated and freed after being
processed. This will prevent from reusing the same buffer before having
it fully processed (and returning once again to available buffer pool)
in shell thread.

Define also fifo that will store buffers that are ready to be
processed. This will be the mechanism for notifying thread about new
UART SMP fragments.

net_buf pool and k_fifo are used on purpose, keeping in mind their
additional overhead (mostly in RAM/ROM usage). This makes the code ready
for increasing number of buffers if needed. In this commit however we
stick with only 1 buffer, to keep minimal changes in processing flow.

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
This commit is contained in:
Marcin Niestroj 2020-07-20 13:51:59 +02:00 committed by Carles Cufí
commit 79fa92229a
3 changed files with 39 additions and 22 deletions

View file

@ -8,6 +8,7 @@
#include <drivers/uart.h>
#include <init.h>
#include <logging/log.h>
#include <net/buf.h>
#define LOG_MODULE_NAME shell_uart
LOG_MODULE_REGISTER(shell_uart);
@ -18,6 +19,11 @@ LOG_MODULE_REGISTER(shell_uart);
#define RX_POLL_PERIOD K_NO_WAIT
#endif
#ifdef CONFIG_MCUMGR_SMP_SHELL
NET_BUF_POOL_DEFINE(smp_shell_rx_pool, 1, SMP_SHELL_RX_BUF_SIZE,
0, NULL);
#endif /* CONFIG_MCUMGR_SMP_SHELL */
SHELL_UART_DEFINE(shell_transport_uart,
CONFIG_SHELL_BACKEND_SERIAL_TX_RING_BUFFER_SIZE,
CONFIG_SHELL_BACKEND_SERIAL_RX_RING_BUFFER_SIZE);
@ -169,6 +175,11 @@ static int init(const struct shell_transport *transport,
sh_uart->ctrl_blk->handler = evt_handler;
sh_uart->ctrl_blk->context = context;
#ifdef CONFIG_MCUMGR_SMP_SHELL
sh_uart->ctrl_blk->smp.buf_pool = &smp_shell_rx_pool;
k_fifo_init(&sh_uart->ctrl_blk->smp.buf_ready);
#endif
if (IS_ENABLED(CONFIG_SHELL_BACKEND_SERIAL_INTERRUPT_DRIVEN)) {
uart_irq_init(sh_uart);
} else {