Skip to content

Commit f59b857

Browse files
nordicjmcarlescufi
authored andcommitted
mgmt: mcumgr: smp: Convert functions to function pointer array
Converts the list of functions for registering an MCUmgr SMP transport from function arguments to a single list which contains the function pointers. Signed-off-by: Jamie McCrae <[email protected]>
1 parent 7119cc1 commit f59b857

File tree

8 files changed

+93
-103
lines changed

8 files changed

+93
-103
lines changed

include/zephyr/mgmt/mcumgr/transport/smp.h

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright Runtime.io 2018. All rights reserved.
3-
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
@@ -35,8 +35,6 @@ struct net_buf;
3535
* @return 0 on success, #mcumgr_err_t code on failure.
3636
*/
3737
typedef int (*smp_transport_out_fn)(struct net_buf *nb);
38-
/* use smp_transport_out_fn instead */
39-
typedef int zephyr_smp_transport_out_fn(struct net_buf *nb);
4038

4139
/** @typedef smp_transport_get_mtu_fn
4240
* @brief SMP MTU query callback for transport
@@ -52,8 +50,6 @@ typedef int zephyr_smp_transport_out_fn(struct net_buf *nb);
5250
* 0 if transmission is currently not possible.
5351
*/
5452
typedef uint16_t (*smp_transport_get_mtu_fn)(const struct net_buf *nb);
55-
/* use smp_transport_get_mtu_fn instead */
56-
typedef uint16_t zephyr_smp_transport_get_mtu_fn(const struct net_buf *nb);
5753

5854
/** @typedef smp_transport_ud_copy_fn
5955
* @brief SMP copy user_data callback
@@ -70,9 +66,6 @@ typedef uint16_t zephyr_smp_transport_get_mtu_fn(const struct net_buf *nb);
7066
*/
7167
typedef int (*smp_transport_ud_copy_fn)(struct net_buf *dst,
7268
const struct net_buf *src);
73-
/* use smp_transport_ud_copy_fn instead */
74-
typedef int zephyr_smp_transport_ud_copy_fn(struct net_buf *dst,
75-
const struct net_buf *src);
7669

7770
/** @typedef smp_transport_ud_free_fn
7871
* @brief SMP free user_data callback
@@ -84,8 +77,6 @@ typedef int zephyr_smp_transport_ud_copy_fn(struct net_buf *dst,
8477
* @param ud Contains a user_data pointer to be freed.
8578
*/
8679
typedef void (*smp_transport_ud_free_fn)(void *ud);
87-
/* use smp_transport_ud_free_fn instead */
88-
typedef void zephyr_smp_transport_ud_free_fn(void *ud);
8980

9081
/** @typedef smp_transport_query_valid_check_fn
9182
* @brief Function for checking if queued data is still valid.
@@ -101,42 +92,38 @@ typedef void zephyr_smp_transport_ud_free_fn(void *ud);
10192
typedef bool (*smp_transport_query_valid_check_fn)(struct net_buf *nb, void *arg);
10293

10394
/**
104-
* @brief SMP transport object for sending SMP responses.
95+
* @brief Function pointers of SMP transport functions, if a handler is NULL then it is not
96+
* supported/implemented.
10597
*/
106-
struct smp_transport {
107-
/* Must be the first member. */
108-
struct k_work work;
109-
110-
/* FIFO containing incoming requests to be processed. */
111-
struct k_fifo fifo;
112-
98+
struct smp_transport_api_t {
99+
/** Transport's send function. */
113100
smp_transport_out_fn output;
101+
102+
/** Transport's get-MTU function. */
114103
smp_transport_get_mtu_fn get_mtu;
104+
105+
/** Transport buffer user_data copy function. */
115106
smp_transport_ud_copy_fn ud_copy;
107+
108+
/** Transport buffer user_data free function. */
116109
smp_transport_ud_free_fn ud_free;
117-
smp_transport_query_valid_check_fn query_valid_check;
118110

119-
#ifdef CONFIG_MCUMGR_TRANSPORT_REASSEMBLY
120-
/* Packet reassembly internal data, API access only */
121-
struct {
122-
struct net_buf *current; /* net_buf used for reassembly */
123-
uint16_t expected; /* expected bytes to come */
124-
} __reassembly;
125-
#endif
111+
/** Transport's check function for if a query is valid. */
112+
smp_transport_query_valid_check_fn query_valid_check;
126113
};
127114

128-
/* Deprecated, use smp_transport instead */
129-
struct zephyr_smp_transport {
115+
/**
116+
* @brief SMP transport object for sending SMP responses.
117+
*/
118+
struct smp_transport {
130119
/* Must be the first member. */
131-
struct k_work zst_work;
120+
struct k_work work;
132121

133122
/* FIFO containing incoming requests to be processed. */
134-
struct k_fifo zst_fifo;
123+
struct k_fifo fifo;
135124

136-
smp_transport_out_fn zst_output;
137-
smp_transport_get_mtu_fn zst_get_mtu;
138-
smp_transport_ud_copy_fn zst_ud_copy;
139-
smp_transport_ud_free_fn zst_ud_free;
125+
/* Function pointers */
126+
struct smp_transport_api_t functions;
140127

141128
#ifdef CONFIG_MCUMGR_TRANSPORT_REASSEMBLY
142129
/* Packet reassembly internal data, API access only */
@@ -150,33 +137,12 @@ struct zephyr_smp_transport {
150137
/**
151138
* @brief Initializes a Zephyr SMP transport object.
152139
*
153-
* @param smpt The transport to construct.
154-
* @param output_func The transport's send function.
155-
* @param get_mtu_func The transport's get-MTU function.
156-
* @param ud_copy_func The transport buffer user_data copy function.
157-
* @param ud_free_func The transport buffer user_data free function.
158-
* @param query_valid_check_func The transport's check function for if a query is valid.
140+
* @param smpt The transport to construct.
141+
*
142+
* @return 0 If successful
143+
* @return Negative errno code if failure.
159144
*/
160-
void smp_transport_init(struct smp_transport *smpt,
161-
smp_transport_out_fn output_func,
162-
smp_transport_get_mtu_fn get_mtu_func,
163-
smp_transport_ud_copy_fn ud_copy_func,
164-
smp_transport_ud_free_fn ud_free_func,
165-
smp_transport_query_valid_check_fn query_valid_check_func);
166-
167-
__deprecated static inline
168-
void zephyr_smp_transport_init(struct zephyr_smp_transport *smpt,
169-
zephyr_smp_transport_out_fn *output_func,
170-
zephyr_smp_transport_get_mtu_fn *get_mtu_func,
171-
zephyr_smp_transport_ud_copy_fn *ud_copy_func,
172-
zephyr_smp_transport_ud_free_fn *ud_free_func)
173-
{
174-
smp_transport_init((struct smp_transport *)smpt,
175-
(smp_transport_out_fn)output_func,
176-
(smp_transport_get_mtu_fn)get_mtu_func,
177-
(smp_transport_ud_copy_fn)ud_copy_func,
178-
(smp_transport_ud_free_fn)ud_free_func, NULL);
179-
}
145+
int smp_transport_init(struct smp_transport *smpt);
180146

181147
/**
182148
* @brief Used to remove queued requests for an SMP transport that are no longer valid. A

subsys/mgmt/mcumgr/smp/src/smp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static void smp_on_err(struct smp_streamer *streamer, const struct smp_hdr *req_
334334
/* Build and transmit the error response. */
335335
rc = smp_build_err_rsp(streamer, req_hdr, status, rsn);
336336
if (rc == 0) {
337-
streamer->smpt->output(rsp);
337+
streamer->smpt->functions.output(rsp);
338338
rsp = NULL;
339339
}
340340

@@ -415,7 +415,7 @@ int smp_process_request_packet(struct smp_streamer *streamer, void *vreq)
415415
}
416416

417417
/* Send the response. */
418-
rc = streamer->smpt->output(rsp);
418+
rc = streamer->smpt->functions.output(rsp);
419419
rsp = NULL;
420420
if (rc != 0) {
421421
break;

subsys/mgmt/mcumgr/transport/src/smp.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include <assert.h>
89
#include <zephyr/kernel.h>
910
#include <zephyr/device.h>
1011
#include <zephyr/net/buf.h>
@@ -72,8 +73,8 @@ void *smp_alloc_rsp(const void *req, void *arg)
7273
return NULL;
7374
}
7475

75-
if (smpt->ud_copy) {
76-
smpt->ud_copy(rsp_nb, req_nb);
76+
if (smpt->functions.ud_copy) {
77+
smpt->functions.ud_copy(rsp_nb, req_nb);
7778
} else {
7879
memcpy(net_buf_user_data(rsp_nb),
7980
net_buf_user_data((void *)req_nb),
@@ -91,8 +92,8 @@ void smp_free_buf(void *buf, void *arg)
9192
return;
9293
}
9394

94-
if (smpt->ud_free) {
95-
smpt->ud_free(net_buf_user_data((struct net_buf *)buf));
95+
if (smpt->functions.ud_free) {
96+
smpt->functions.ud_free(net_buf_user_data((struct net_buf *)buf));
9697
}
9798

9899
smp_packet_free(buf);
@@ -135,28 +136,23 @@ smp_handle_reqs(struct k_work *work)
135136
}
136137
}
137138

138-
void
139-
smp_transport_init(struct smp_transport *smpt,
140-
smp_transport_out_fn output_func,
141-
smp_transport_get_mtu_fn get_mtu_func,
142-
smp_transport_ud_copy_fn ud_copy_func,
143-
smp_transport_ud_free_fn ud_free_func,
144-
smp_transport_query_valid_check_fn query_valid_check_func)
139+
int smp_transport_init(struct smp_transport *smpt)
145140
{
146-
*smpt = (struct smp_transport) {
147-
.output = output_func,
148-
.get_mtu = get_mtu_func,
149-
.ud_copy = ud_copy_func,
150-
.ud_free = ud_free_func,
151-
.query_valid_check = query_valid_check_func,
152-
};
141+
__ASSERT((smpt->functions.output != NULL),
142+
"Required transport output function pointer cannot be NULL");
143+
144+
if (smpt->functions.output == NULL) {
145+
return -EINVAL;
146+
}
153147

154148
#ifdef CONFIG_MCUMGR_TRANSPORT_REASSEMBLY
155149
smp_reassembly_init(smpt);
156150
#endif
157151

158152
k_work_init(&smpt->work, smp_handle_reqs);
159153
k_fifo_init(&smpt->fifo);
154+
155+
return 0;
160156
}
161157

162158
/**
@@ -180,7 +176,7 @@ void smp_rx_remove_invalid(struct smp_transport *zst, void *arg)
180176
struct net_buf *nb;
181177
struct k_fifo temp_fifo;
182178

183-
if (zst->query_valid_check == NULL) {
179+
if (zst->functions.query_valid_check == NULL) {
184180
/* No check check function registered, abort check */
185181
return;
186182
}
@@ -196,7 +192,7 @@ void smp_rx_remove_invalid(struct smp_transport *zst, void *arg)
196192
k_fifo_init(&temp_fifo);
197193

198194
while ((nb = net_buf_get(&zst->fifo, K_NO_WAIT)) != NULL) {
199-
if (!zst->query_valid_check(nb, arg)) {
195+
if (!zst->functions.query_valid_check(nb, arg)) {
200196
smp_free_buf(nb, zst);
201197
} else {
202198
net_buf_put(&temp_fifo, nb);

subsys/mgmt/mcumgr/transport/src/smp_bt.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,17 @@ static void smp_bt_setup(void)
653653
++i;
654654
}
655655

656-
smp_transport_init(&smp_bt_transport, smp_bt_tx_pkt,
657-
smp_bt_get_mtu, smp_bt_ud_copy,
658-
smp_bt_ud_free, smp_bt_query_valid_check);
656+
smp_bt_transport.functions.output = smp_bt_tx_pkt;
657+
smp_bt_transport.functions.get_mtu = smp_bt_get_mtu;
658+
smp_bt_transport.functions.ud_copy = smp_bt_ud_copy;
659+
smp_bt_transport.functions.ud_free = smp_bt_ud_free;
660+
smp_bt_transport.functions.query_valid_check = smp_bt_query_valid_check;
659661

660-
rc = smp_bt_register();
662+
rc = smp_transport_init(&smp_bt_transport);
663+
664+
if (rc == 0) {
665+
rc = smp_bt_register();
666+
}
661667

662668
if (rc != 0) {
663669
LOG_ERR("Bluetooth SMP transport register failed (err %d)", rc);

subsys/mgmt/mcumgr/transport/src/smp_dummy.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,24 @@ static int smp_dummy_tx_pkt_int(struct net_buf *nb)
187187

188188
static int smp_dummy_init(void)
189189
{
190+
int rc;
190191

191192
k_sem_init(&smp_data_ready_sem, 0, 1);
192193

193-
smp_transport_init(&smp_dummy_transport, smp_dummy_tx_pkt_int,
194-
smp_dummy_get_mtu, NULL, NULL, NULL);
194+
smp_dummy_transport.functions.output = smp_dummy_tx_pkt_int;
195+
smp_dummy_transport.functions.get_mtu = smp_dummy_get_mtu;
196+
197+
rc = smp_transport_init(&smp_dummy_transport);
198+
199+
if (rc != 0) {
200+
return rc;
201+
}
202+
195203
dummy_mgumgr_recv_cb = smp_dummy_rx_frag;
196204

197205
return 0;
198206
}
199207

200-
201208
static struct uart_mcumgr_rx_buf *dummy_mcumgr_alloc_rx_buf(void)
202209
{
203210
struct uart_mcumgr_rx_buf *rx_buf;

subsys/mgmt/mcumgr/transport/src/smp_shell.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,12 @@ static int smp_shell_tx_pkt(struct net_buf *nb)
235235

236236
int smp_shell_init(void)
237237
{
238-
smp_transport_init(&smp_shell_transport, smp_shell_tx_pkt,
239-
smp_shell_get_mtu, NULL, NULL, NULL);
238+
int rc;
240239

241-
return 0;
240+
smp_shell_transport.functions.output = smp_shell_tx_pkt;
241+
smp_shell_transport.functions.get_mtu = smp_shell_get_mtu;
242+
243+
rc = smp_transport_init(&smp_shell_transport);
244+
245+
return rc;
242246
}

subsys/mgmt/mcumgr/transport/src/smp_uart.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,18 @@ static int smp_uart_tx_pkt(struct net_buf *nb)
9292

9393
static int smp_uart_init(void)
9494
{
95+
int rc;
96+
97+
smp_uart_transport.functions.output = smp_uart_tx_pkt;
98+
smp_uart_transport.functions.get_mtu = smp_uart_get_mtu;
9599

96-
smp_transport_init(&smp_uart_transport, smp_uart_tx_pkt,
97-
smp_uart_get_mtu, NULL, NULL, NULL);
98-
uart_mcumgr_register(smp_uart_rx_frag);
100+
rc = smp_transport_init(&smp_uart_transport);
99101

100-
return 0;
102+
if (rc == 0) {
103+
uart_mcumgr_register(smp_uart_rx_frag);
104+
}
105+
106+
return rc;
101107
}
102108

103109
SYS_INIT(smp_uart_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);

subsys/mgmt/mcumgr/transport/src/smp_udp.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,25 @@ static void smp_udp_receive_thread(void *p1, void *p2, void *p3)
174174

175175
static int smp_udp_init(void)
176176
{
177+
int rc;
177178

178179
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
179-
smp_transport_init(&configs.ipv4.smp_transport,
180-
smp_udp4_tx, smp_udp_get_mtu,
181-
smp_udp_ud_copy, NULL, NULL);
180+
configs.ipv4.smp_transport.functions.output = smp_udp4_tx;
181+
configs.ipv4.smp_transport.functions.get_mtu = smp_udp_get_mtu;
182+
configs.ipv4.smp_transport.functions.ud_copy = smp_udp_ud_copy;
183+
184+
rc = smp_transport_init(&configs.ipv4.smp_transport);
182185
#endif
183186

184187
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
185-
smp_transport_init(&configs.ipv6.smp_transport,
186-
smp_udp6_tx, smp_udp_get_mtu,
187-
smp_udp_ud_copy, NULL, NULL);
188+
configs.ipv6.smp_transport.functions.output = smp_udp6_tx;
189+
configs.ipv6.smp_transport.functions.get_mtu = smp_udp_get_mtu;
190+
configs.ipv6.smp_transport.functions.ud_copy = smp_udp_ud_copy;
191+
192+
rc = smp_transport_init(&configs.ipv6.smp_transport);
188193
#endif
189194

190-
return MGMT_ERR_EOK;
195+
return rc;
191196
}
192197

193198
static int create_socket(struct sockaddr *addr, const char *proto)

0 commit comments

Comments
 (0)