1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef H_SMP_CLIENT_
8 #define H_SMP_CLIENT_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/net_buf.h>
12 #include <mgmt/mcumgr/transport/smp_internal.h>
13 #include <zephyr/mgmt/mcumgr/smp/smp.h>
14 #include <zephyr/mgmt/mcumgr/transport/smp.h>
15 
16 /**
17  * @brief MCUmgr SMP client API
18  * @defgroup mcumgr_smp_client SMP client API
19  * @ingroup mcumgr
20  * @{
21  */
22 
23 /**
24  * @brief SMP client object
25  */
26 struct smp_client_object {
27 	/** Must be the first member. */
28 	struct k_work work;
29 	/** FIFO for client TX queue */
30 	struct k_fifo tx_fifo;
31 	/** SMP transport object */
32 	struct smp_transport *smpt;
33 	/** SMP SEQ */
34 	uint8_t smp_seq;
35 };
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @brief Initialize a SMP client object.
43  *
44  * @param smp_client	The Client to construct.
45  * @param smp_type	SMP transport type for discovering transport object
46  *
47  * @return	0 if successful
48  * @return	mcumgr_err_t code on failure
49  */
50 int smp_client_object_init(struct smp_client_object *smp_client, int smp_type);
51 
52 /**
53  * @brief Response callback for SMP send.
54  *
55  * @param nb net_buf for response
56  * @param user_data same user data that was provided as part of the request
57  *
58  * @return 0 on success.
59  * @return @ref mcumgr_err_t code on failure.
60  */
61 typedef int (*smp_client_res_fn)(struct net_buf *nb, void *user_data);
62 
63 /**
64  * @brief SMP client response handler.
65  *
66  * @param nb response net_buf
67  * @param res_hdr Parsed SMP header
68  *
69  * @return 0 on success.
70  * @return @ref mcumgr_err_t code on failure.
71  */
72 int smp_client_single_response(struct net_buf *nb, const struct smp_hdr *res_hdr);
73 
74 /**
75  * @brief Allocate buffer and initialize with SMP header.
76  *
77  * @param smp_client SMP client object
78  * @param group SMP group id
79  * @param command_id SMP command id
80  * @param op SMP operation type
81  * @param version SMP MCUmgr version
82  *
83  * @return      A newly-allocated buffer net_buf on success
84  * @return	NULL on failure.
85  */
86 struct net_buf *smp_client_buf_allocation(struct smp_client_object *smp_client, uint16_t group,
87 					  uint8_t command_id, uint8_t op,
88 					  enum smp_mcumgr_version_t version);
89 /**
90  * @brief Free a SMP client buffer.
91  *
92  * @param nb    The net_buf to free.
93  */
94 void smp_client_buf_free(struct net_buf *nb);
95 
96 /**
97  * @brief SMP client data send request.
98  *
99  * @param smp_client SMP client object
100  * @param nb net_buf packet for send
101  * @param cb Callback for response handler
102  * @param user_data user defined data pointer which will be returned back to response callback
103  * @param timeout_in_sec	Timeout in seconds for send process. Client will retry transport
104  *				based CONFIG_SMP_CMD_RETRY_TIME
105  *
106  * @return 0 on success.
107  * @return @ref mcumgr_err_t code on failure.
108  */
109 int smp_client_send_cmd(struct smp_client_object *smp_client, struct net_buf *nb,
110 			smp_client_res_fn cb, void *user_data, int timeout_in_sec);
111 
112 /**
113  * @}
114  */
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif /* H_SMP_CLIENT_ */
121