zephyr/include/mgmt/mcumgr/smp_shell.h
Marcin Niestroj 79fa92229a 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>
2020-10-07 14:51:06 +02:00

69 lines
1.4 KiB
C

/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief Shell transport for the mcumgr SMP protocol.
*/
#ifndef ZEPHYR_INCLUDE_MGMT_SMP_SHELL_H_
#define ZEPHYR_INCLUDE_MGMT_SMP_SHELL_H_
#include <zephyr/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SMP_SHELL_RX_BUF_SIZE 127
/** @brief Data used by SMP shell */
struct smp_shell_data {
struct net_buf_pool *buf_pool;
struct k_fifo buf_ready;
struct net_buf *buf;
atomic_t esc_state;
};
/**
* @brief Attempt to process received bytes as part of an SMP frame.
*
* Called to scan buffer from the beginning and consume all bytes that are
* part of SMP frame until frame or buffer ends.
*
* @param data SMP shell transfer data.
* @param bytes Buffer with bytes to process
* @param size Number of bytes to process
*
* @return number of bytes consumed by the SMP
*/
size_t smp_shell_rx_bytes(struct smp_shell_data *data, const uint8_t *bytes,
size_t size);
/**
* @brief Processes SMP data and executes command if full frame was received.
*
* This function should be called from thread context.
*
* @param data SMP shell transfer data.
*/
void smp_shell_process(struct smp_shell_data *data);
/**
* @brief Initializes SMP transport over shell.
*
* This function should be called before feeding SMP transport with received
* data.
*
* @return 0 on success
*/
int smp_shell_init(void);
#ifdef __cplusplus
}
#endif
#endif