|
12 | 12 | #include <zephyr/kernel.h> |
13 | 13 | #include <zephyr/net_buf.h> |
14 | 14 | #include <zephyr/mgmt/mcumgr/transport/smp.h> |
| 15 | +#include <zcbor_encode.h> |
15 | 16 |
|
16 | 17 | #ifdef __cplusplus |
17 | 18 | extern "C" { |
@@ -79,6 +80,30 @@ void *smp_alloc_rsp(const void *req, void *arg); |
79 | 80 | */ |
80 | 81 | void smp_free_buf(void *buf, void *arg); |
81 | 82 |
|
| 83 | +/** |
| 84 | + * @brief Reeset a zcbor encoder to allow a new response. |
| 85 | + * |
| 86 | + * If a response has already been (partially) generated than this will allow resetting back to |
| 87 | + * the default state so that new response can be used (e.g. an error). |
| 88 | + * |
| 89 | + * @param streamer The streamer providing the required SMP callbacks. |
| 90 | + * |
| 91 | + * @return true on success, false on failure (memory error). |
| 92 | + */ |
| 93 | +static inline bool smp_mgmt_reset_zse(struct smp_streamer *streamer) |
| 94 | +{ |
| 95 | + zcbor_state_t *zse = streamer->writer->zs; |
| 96 | + |
| 97 | + /* Because there is already data in the buffer, it must be cleared first */ |
| 98 | + net_buf_reset(streamer->writer->nb); |
| 99 | + streamer->writer->nb->len = sizeof(struct smp_hdr); |
| 100 | + zcbor_new_encode_state(zse, ARRAY_SIZE(streamer->writer->zs), |
| 101 | + streamer->writer->nb->data + sizeof(struct smp_hdr), |
| 102 | + net_buf_tailroom(streamer->writer->nb), 0); |
| 103 | + |
| 104 | + return zcbor_map_start_encode(zse, CONFIG_MCUMGR_SMP_CBOR_MAX_MAIN_MAP_ENTRIES); |
| 105 | +} |
| 106 | + |
82 | 107 | #ifdef __cplusplus |
83 | 108 | } |
84 | 109 | #endif |
|
0 commit comments