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 };
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 Trig SMP client request packet for transmission.
54  *
55  * @param work	The transport to use to send the corresponding response(s).
56  */
57 void smp_tx_req(struct k_work *work);
58 #endif
59 
60 __deprecated static inline
zephyr_smp_rx_req(struct zephyr_smp_transport * smpt,struct net_buf * nb)61 void zephyr_smp_rx_req(struct zephyr_smp_transport *smpt, struct net_buf *nb)
62 {
63 	smp_rx_req((struct smp_transport *)smpt, nb);
64 }
65 
66 /**
67  * @brief Allocates a response buffer.
68  *
69  * If a source buf is provided, its user data is copied into the new buffer.
70  *
71  * @param req		An optional source buffer to copy user data from.
72  * @param arg		The streamer providing the callback.
73  *
74  * @return	Newly-allocated buffer on success
75  *		NULL on failure.
76  */
77 void *smp_alloc_rsp(const void *req, void *arg);
78 
79 __deprecated static inline
zephyr_smp_alloc_rsp(const void * req,void * arg)80 void *zephyr_smp_alloc_rsp(const void *req, void *arg)
81 {
82 	return smp_alloc_rsp(req, arg);
83 }
84 
85 
86 /**
87  * @brief Frees an allocated buffer.
88  *
89  * @param buf		The buffer to free.
90  * @param arg		The streamer providing the callback.
91  */
92 void smp_free_buf(void *buf, void *arg);
93 
94 __deprecated static inline
zephyr_smp_free_buf(void * buf,void * arg)95 void zephyr_smp_free_buf(void *buf, void *arg)
96 {
97 	smp_free_buf(buf, arg);
98 }
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif /* MGMT_MCUMGR_SMP_INTERNAL_H_ */
105