|
| 1 | +/* |
| 2 | + * Copyright Runtime.io 2018. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef ZEPHYR_INCLUDE_MGMT_SERIAL_H_ |
| 8 | +#define ZEPHYR_INCLUDE_MGMT_SERIAL_H_ |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief This allows to use the MCUmgr SMP protocol over serial. |
| 12 | + * @defgroup mcumgr_transport_serial Serial transport |
| 13 | + * @ingroup mcumgr_transport |
| 14 | + * @{ |
| 15 | + */ |
| 16 | + |
| 17 | +#include <zephyr/types.h> |
| 18 | + |
| 19 | +#ifdef __cplusplus |
| 20 | +extern "C" { |
| 21 | +#endif |
| 22 | + |
| 23 | +/** Serial packet header */ |
| 24 | +#define MCUMGR_SERIAL_HDR_PKT 0x0609 |
| 25 | +/** Serial fragment header */ |
| 26 | +#define MCUMGR_SERIAL_HDR_FRAG 0x0414 |
| 27 | +/** Maximum frame size */ |
| 28 | +#define MCUMGR_SERIAL_MAX_FRAME 127 |
| 29 | + |
| 30 | +/** First byte of packet header */ |
| 31 | +#define MCUMGR_SERIAL_HDR_PKT_1 (MCUMGR_SERIAL_HDR_PKT >> 8) |
| 32 | +/** Second byte of packet header */ |
| 33 | +#define MCUMGR_SERIAL_HDR_PKT_2 (MCUMGR_SERIAL_HDR_PKT & 0xff) |
| 34 | +/** First byte of fragment header */ |
| 35 | +#define MCUMGR_SERIAL_HDR_FRAG_1 (MCUMGR_SERIAL_HDR_FRAG >> 8) |
| 36 | +/** Second byte of fragment header */ |
| 37 | +#define MCUMGR_SERIAL_HDR_FRAG_2 (MCUMGR_SERIAL_HDR_FRAG & 0xff) |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Maintains state for an incoming mcumgr request packet. |
| 41 | + */ |
| 42 | +struct mcumgr_serial_rx_ctxt { |
| 43 | + /** Contains the partially- or fully-received mcumgr request. Data |
| 44 | + * stored in this buffer has already been base64-decoded. |
| 45 | + */ |
| 46 | + struct net_buf *nb; |
| 47 | + |
| 48 | + /** Length of full packet, as read from header. */ |
| 49 | + uint16_t pkt_len; |
| 50 | + |
| 51 | +#if defined(CONFIG_MCUMGR_TRANSPORT_SERIAL_HAS_SMP_OVER_CONSOLE) && \ |
| 52 | + defined(CONFIG_MCUMGR_TRANSPORT_SERIAL_HAS_RAW_BINARY_NON_SMP_OVER_CONSOLE) |
| 53 | + /** |
| 54 | + * Set to true by transports which use raw MCUmgr over UART (not SMP over console) to know |
| 55 | + * not to deal with the extra base64 etc. processing. |
| 56 | + */ |
| 57 | + const bool raw_transport; |
| 58 | +#endif |
| 59 | +}; |
| 60 | + |
| 61 | +/** @typedef mcumgr_serial_tx_cb |
| 62 | + * @brief Transmits a chunk of raw response data. |
| 63 | + * |
| 64 | + * @param data The data to transmit. |
| 65 | + * @param len The number of bytes to transmit. |
| 66 | + * |
| 67 | + * @return 0 on success; negative error code on failure. |
| 68 | + */ |
| 69 | +typedef int (*mcumgr_serial_tx_cb)(const void *data, int len); |
| 70 | + |
| 71 | +/** |
| 72 | + * @brief Processes an mcumgr request fragment received over a serial |
| 73 | + * transport. |
| 74 | + * |
| 75 | + * Processes an mcumgr request fragment received over a serial transport. If |
| 76 | + * the fragment is the end of a valid mcumgr request, this function returns a |
| 77 | + * net_buf containing the decoded request. It is the caller's responsibility |
| 78 | + * to free the net_buf after it has been processed. |
| 79 | + * |
| 80 | + * @param rx_ctxt The receive context associated with the serial |
| 81 | + * transport being used. |
| 82 | + * @param frag The incoming fragment to process. |
| 83 | + * @param frag_len The length of the fragment, in bytes. |
| 84 | + * |
| 85 | + * @return A net_buf containing the decoded request if a |
| 86 | + * complete and valid request has been |
| 87 | + * received. |
| 88 | + * NULL if the packet is incomplete or invalid. |
| 89 | + */ |
| 90 | +struct net_buf *mcumgr_serial_process_frag( |
| 91 | + struct mcumgr_serial_rx_ctxt *rx_ctxt, |
| 92 | + const uint8_t *frag, int frag_len); |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief Encodes and transmits an mcumgr packet over serial. |
| 96 | + * |
| 97 | + * @param data The mcumgr packet data to send. |
| 98 | + * @param len The length of the unencoded mcumgr packet. |
| 99 | + * @param cb A callback used to transmit raw bytes. |
| 100 | + * |
| 101 | + * @return 0 on success; negative error code on failure. |
| 102 | + */ |
| 103 | +int mcumgr_serial_tx_pkt(const uint8_t *data, int len, mcumgr_serial_tx_cb cb); |
| 104 | + |
| 105 | +#ifdef __cplusplus |
| 106 | +} |
| 107 | +#endif |
| 108 | + |
| 109 | +/** |
| 110 | + * @} |
| 111 | + */ |
| 112 | + |
| 113 | +#endif |
0 commit comments