Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | /*
* Copyright Runtime.io 2018. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief A driver for sending and receiving mcumgr packets over UART.
*/
#include <string.h>
#include <kernel.h>
#include <drivers/uart.h>
#include <mgmt/mcumgr/serial.h>
#include <drivers/console/uart_mcumgr.h>
static const struct device *uart_mcumgr_dev;
/** Callback to execute when a valid fragment has been received. */
static uart_mcumgr_recv_fn *uart_mgumgr_recv_cb;
/** Contains the fragment currently being received. */
static struct uart_mcumgr_rx_buf *uart_mcumgr_cur_buf;
/**
* Whether the line currently being read should be ignored. This is true if
* the line is too long or if there is no buffer available to hold it.
*/
static bool uart_mcumgr_ignoring;
/** Contains buffers to hold incoming request fragments. */
K_MEM_SLAB_DEFINE(uart_mcumgr_slab, sizeof(struct uart_mcumgr_rx_buf),
CONFIG_UART_MCUMGR_RX_BUF_COUNT, 1);
static struct uart_mcumgr_rx_buf *uart_mcumgr_alloc_rx_buf(void)
{
struct uart_mcumgr_rx_buf *rx_buf;
void *block;
int rc;
rc = k_mem_slab_alloc(&uart_mcumgr_slab, &block, K_NO_WAIT);
if (rc != 0) {
return NULL;
}
rx_buf = block;
rx_buf->length = 0;
return rx_buf;
}
void uart_mcumgr_free_rx_buf(struct uart_mcumgr_rx_buf *rx_buf)
{
void *block;
block = rx_buf;
k_mem_slab_free(&uart_mcumgr_slab, &block);
}
/**
* Reads a chunk of received data from the UART.
*/
static int uart_mcumgr_read_chunk(void *buf, int capacity)
{
if (!uart_irq_rx_ready(uart_mcumgr_dev)) {
return 0;
}
return uart_fifo_read(uart_mcumgr_dev, buf, capacity);
}
/**
* Processes a single incoming byte.
*/
static struct uart_mcumgr_rx_buf *uart_mcumgr_rx_byte(uint8_t byte)
{
struct uart_mcumgr_rx_buf *rx_buf;
if (!uart_mcumgr_ignoring) {
if (uart_mcumgr_cur_buf == NULL) {
uart_mcumgr_cur_buf = uart_mcumgr_alloc_rx_buf();
if (uart_mcumgr_cur_buf == NULL) {
/* Insufficient buffers; drop this fragment. */
uart_mcumgr_ignoring = true;
}
}
}
rx_buf = uart_mcumgr_cur_buf;
if (!uart_mcumgr_ignoring) {
if (rx_buf->length >= sizeof(rx_buf->data)) {
/* Line too long; drop this fragment. */
uart_mcumgr_free_rx_buf(uart_mcumgr_cur_buf);
uart_mcumgr_cur_buf = NULL;
uart_mcumgr_ignoring = true;
} else {
rx_buf->data[rx_buf->length++] = byte;
}
}
if (byte == '\n') {
/* Fragment complete. */
if (uart_mcumgr_ignoring) {
uart_mcumgr_ignoring = false;
} else {
uart_mcumgr_cur_buf = NULL;
return rx_buf;
}
}
return NULL;
}
/**
* ISR that is called when UART bytes are received.
*/
static void uart_mcumgr_isr(const struct device *unused, void *user_data)
{
struct uart_mcumgr_rx_buf *rx_buf;
uint8_t buf[32];
int chunk_len;
int i;
ARG_UNUSED(unused);
ARG_UNUSED(user_data);
while (uart_irq_update(uart_mcumgr_dev) &&
uart_irq_is_pending(uart_mcumgr_dev)) {
chunk_len = uart_mcumgr_read_chunk(buf, sizeof(buf));
if (chunk_len == 0) {
continue;
}
for (i = 0; i < chunk_len; i++) {
rx_buf = uart_mcumgr_rx_byte(buf[i]);
if (rx_buf != NULL) {
uart_mgumgr_recv_cb(rx_buf);
}
}
}
}
/**
* Sends raw data over the UART.
*/
static int uart_mcumgr_send_raw(const void *data, int len, void *arg)
{
const uint8_t *u8p;
u8p = data;
while (len--) {
uart_poll_out(uart_mcumgr_dev, *u8p++);
}
return 0;
}
int uart_mcumgr_send(const uint8_t *data, int len)
{
return mcumgr_serial_tx_pkt(data, len, uart_mcumgr_send_raw, NULL);
}
static void uart_mcumgr_setup(const struct device *uart)
{
uint8_t c;
uart_irq_rx_disable(uart);
uart_irq_tx_disable(uart);
/* Drain the fifo */
while (uart_fifo_read(uart, &c, 1)) {
continue;
}
uart_irq_callback_set(uart, uart_mcumgr_isr);
uart_irq_rx_enable(uart);
}
void uart_mcumgr_register(uart_mcumgr_recv_fn *cb)
{
uart_mgumgr_recv_cb = cb;
uart_mcumgr_dev = device_get_binding(CONFIG_UART_MCUMGR_ON_DEV_NAME);
if (uart_mcumgr_dev != NULL) {
uart_mcumgr_setup(uart_mcumgr_dev);
}
}
|