Skip to content

Commit 4783f32

Browse files
committed
mgmt: mcumgr: transport: Add zcbor buffer reset function
Adds a common zcbor reset buffer function, this allows for the buffer to be reset after data has been added already to add a different response, e.g. an error response Signed-off-by: Jamie McCrae <[email protected]>
1 parent 8619248 commit 4783f32

File tree

1 file changed

+25
-0
lines changed
  • subsys/mgmt/mcumgr/transport/include/mgmt/mcumgr/transport

1 file changed

+25
-0
lines changed

subsys/mgmt/mcumgr/transport/include/mgmt/mcumgr/transport/smp_internal.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <zephyr/kernel.h>
1313
#include <zephyr/net_buf.h>
1414
#include <zephyr/mgmt/mcumgr/transport/smp.h>
15+
#include <zcbor_encode.h>
1516

1617
#ifdef __cplusplus
1718
extern "C" {
@@ -79,6 +80,30 @@ void *smp_alloc_rsp(const void *req, void *arg);
7980
*/
8081
void smp_free_buf(void *buf, void *arg);
8182

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+
82107
#ifdef __cplusplus
83108
}
84109
#endif

0 commit comments

Comments
 (0)