1 /*
2  * Copyright Runtime.io 2018. All rights reserved.
3  * Copyright (c) 2022 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef MGMT_MCUMGR_SMP_INTERNAL_H_
9 #define MGMT_MCUMGR_SMP_INTERNAL_H_
10 
11 #include <stdint.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/net_buf.h>
14 #include <zephyr/mgmt/mcumgr/transport/smp.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 struct smp_hdr {
21 #ifdef CONFIG_LITTLE_ENDIAN
22 	uint8_t  nh_op:3;		/* MGMT_OP_[...] */
23 	uint8_t  nh_version:2;
24 	uint8_t  _res1:3;
25 #else
26 	uint8_t  _res1:3;
27 	uint8_t  nh_version:2;
28 	uint8_t  nh_op:3;		/* MGMT_OP_[...] */
29 #endif
30 	uint8_t  nh_flags;		/* Reserved for future flags */
31 	uint16_t nh_len;		/* Length of the payload */
32 	uint16_t nh_group;		/* MGMT_GROUP_ID_[...] */
33 	uint8_t  nh_seq;		/* Sequence number */
34 	uint8_t  nh_id;			/* Message ID within group */
35 } __packed;
36 
37 struct smp_transport;
38 struct zephyr_smp_transport;
39 
40 /**
41  * @brief Enqueues an incoming SMP request packet for processing.
42  *
43  * This function always consumes the supplied net_buf.
44  *
45  * @param smtp                  The transport to use to send the corresponding
46  *                                  response(s).
47  * @param nb                    The request packet to process.
48  */
49 void smp_rx_req(struct smp_transport *smtp, struct net_buf *nb);
50 
51 #ifdef CONFIG_SMP_CLIENT
52 /**
53  * @brief Get work queue for SMP client.
54  *
55  * @return SMP work queue object.
56  */
57 struct k_work_q *smp_get_wq(void);
58 #endif
59 
60 /**
61  * @brief Allocates a response buffer.
62  *
63  * If a source buf is provided, its user data is copied into the new buffer.
64  *
65  * @param req		An optional source buffer to copy user data from.
66  * @param arg		The streamer providing the callback.
67  *
68  * @return	Newly-allocated buffer on success
69  *		NULL on failure.
70  */
71 void *smp_alloc_rsp(const void *req, void *arg);
72 
73 
74 /**
75  * @brief Frees an allocated buffer.
76  *
77  * @param buf		The buffer to free.
78  * @param arg		The streamer providing the callback.
79  */
80 void smp_free_buf(void *buf, void *arg);
81 
82 #ifdef __cplusplus
83 }
84 #endif
85 
86 #endif /* MGMT_MCUMGR_SMP_INTERNAL_H_ */
87