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