Skip to content

Commit 4f98eed

Browse files
Ayush1325nashif
authored andcommitted
mcumgr: client: Allow specifying server address
- Currently, it is not possible to use mcumgr client with smp server since there is no way to specify smp server address for requests. - This patch series adds support for creating smp_client_object containing information regarding the host server. This design allows multiple smp_client_object to exist for the same underlying smp_udp transport, making it much easier to use the same udp socket for multiple OTAs. Signed-off-by: Ayush Singh <ayush@beagleboard.org>
1 parent aa7efb4 commit 4f98eed

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct smp_client_object {
3232
struct smp_transport *smpt;
3333
/** SMP SEQ */
3434
uint8_t smp_seq;
35+
/** SMP transport data. Allows multiple client_object for single smp_transport. */
36+
void *priv;
3537
};
3638

3739
#ifdef __cplusplus
@@ -49,6 +51,27 @@ extern "C" {
4951
*/
5052
int smp_client_object_init(struct smp_client_object *smp_client, int smp_type);
5153

54+
/**
55+
* @brief Set private data for SMP transport.
56+
*
57+
* @param smp_client The Client to set private data for.
58+
* @param data SMP transport private data.
59+
*/
60+
static inline void smp_client_object_set_data(struct smp_client_object *smp_client, void *data)
61+
{
62+
smp_client->priv = data;
63+
}
64+
65+
/**
66+
* @brief Get private data for SMP transport.
67+
*
68+
* @param smp_client The Client to set private data for.
69+
*/
70+
static inline void *smp_client_object_get_data(struct smp_client_object *smp_client)
71+
{
72+
return smp_client->priv;
73+
}
74+
5275
/**
5376
* @brief Response callback for SMP send.
5477
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ typedef void (*smp_transport_ud_free_fn)(void *ud);
9191
*/
9292
typedef bool (*smp_transport_query_valid_check_fn)(struct net_buf *nb, void *arg);
9393

94+
/** @typedef smp_transport_ud_req_init_fn
95+
* @brief SMP init request buffer
96+
*
97+
* The supplied net_buf should be for a SMP request
98+
*
99+
* @param nb net buf for SMP request
100+
* @param priv SMP transport private data
101+
*/
102+
typedef void (*smp_transport_ud_req_init_fn)(struct net_buf *nb, void *priv);
103+
94104
/**
95105
* @brief Function pointers of SMP transport functions, if a handler is NULL then it is not
96106
* supported/implemented.
@@ -110,6 +120,9 @@ struct smp_transport_api_t {
110120

111121
/** Transport's check function for if a query is valid. */
112122
smp_transport_query_valid_check_fn query_valid_check;
123+
124+
/** Transport's request buffer init function */
125+
smp_transport_ud_req_init_fn ud_init;
113126
};
114127

115128
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#ifndef ZEPHYR_INCLUDE_MGMT_SMP_UDP_H_
1515
#define ZEPHYR_INCLUDE_MGMT_SMP_UDP_H_
1616

17+
#include <zephyr/mgmt/mcumgr/smp/smp_client.h>
18+
#include <zephyr/net/net_ip.h>
19+
1720
/**
1821
* @brief This allows to use the MCUmgr SMP protocol over UDP.
1922
* @defgroup mcumgr_transport_udp UDP transport
@@ -46,6 +49,18 @@ int smp_udp_open(void);
4649
*/
4750
int smp_udp_close(void);
4851

52+
#if defined(CONFIG_SMP_CLIENT) || defined(__DOXYGEN__)
53+
/**
54+
* @brief Set host address for smp_client_object
55+
*
56+
* @note addr should be valid as long as obj is valid.
57+
*
58+
* @return 0 on success
59+
* @return -errno code on failure.
60+
*/
61+
int smp_client_udp_set_host_addr(struct smp_client_object *obj, struct sockaddr *addr);
62+
#endif
63+
4964
#ifdef __cplusplus
5065
}
5166
#endif

subsys/mgmt/mcumgr/smp_client/src/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ struct net_buf *smp_client_buf_allocation(struct smp_client_object *smp_client,
256256
struct net_buf *nb;
257257
struct smp_hdr smp_header;
258258

259-
nb = smp_packet_alloc();
259+
nb = smp_alloc_req(smp_client->smpt, smp_client_object_get_data(smp_client));
260260

261261
if (nb) {
262262
/* Write SMP header with payload length 0 */

subsys/mgmt/mcumgr/transport/include/mgmt/mcumgr/transport/smp_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ void smp_rx_req(struct smp_transport *smtp, struct net_buf *nb);
5959
struct k_work_q *smp_get_wq(void);
6060
#endif
6161

62+
/**
63+
* @brief Allocates a request buffer.
64+
*
65+
* @param arg The streamer providing the callback.
66+
*
67+
* @return Newly-allocated buffer on success
68+
* NULL on failure.
69+
*/
70+
struct net_buf *smp_alloc_req(void *arg, void *priv);
71+
6272
/**
6373
* @brief Allocates a response buffer.
6474
*

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ void smp_packet_free(struct net_buf *nb)
5353
net_buf_unref(nb);
5454
}
5555

56+
/**
57+
* @brief Allocates a request buffer.
58+
*
59+
* @param arg The streamer providing the callback.
60+
* @param priv The streamer private data.
61+
*
62+
* @return Newly-allocated buffer on success
63+
* NULL on failure.
64+
*/
65+
struct net_buf *smp_alloc_req(void *arg, void *priv)
66+
{
67+
struct net_buf *req_nb;
68+
struct smp_transport *smpt = arg;
69+
70+
req_nb = smp_packet_alloc();
71+
if (req_nb == NULL) {
72+
return NULL;
73+
}
74+
75+
if (smpt->functions.ud_init) {
76+
smpt->functions.ud_init(req_nb, priv);
77+
}
78+
79+
return req_nb;
80+
}
81+
5682
/**
5783
* @brief Allocates a response buffer.
5884
*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ static int smp_udp_ud_copy(struct net_buf *dst, const struct net_buf *src)
163163
return MGMT_ERR_EOK;
164164
}
165165

166+
static void smp_udp_ud_init(struct net_buf *nb, void *priv)
167+
{
168+
struct sockaddr *ud = net_buf_user_data(nb);
169+
const struct sockaddr *addr = priv;
170+
171+
if (addr) {
172+
memcpy(ud, addr, sizeof(*addr));
173+
}
174+
}
175+
166176
static int create_socket(enum proto_type proto, int *sock)
167177
{
168178
int tmp_sock;
@@ -438,6 +448,7 @@ static void smp_udp_start(void)
438448
smp_udp_configs.ipv4.smp_transport.functions.output = smp_udp4_tx;
439449
smp_udp_configs.ipv4.smp_transport.functions.get_mtu = smp_udp_get_mtu;
440450
smp_udp_configs.ipv4.smp_transport.functions.ud_copy = smp_udp_ud_copy;
451+
smp_udp_configs.ipv4.smp_transport.functions.ud_init = smp_udp_ud_init;
441452

442453
rc = smp_transport_init(&smp_udp_configs.ipv4.smp_transport);
443454
#ifdef CONFIG_SMP_CLIENT
@@ -460,6 +471,7 @@ static void smp_udp_start(void)
460471
smp_udp_configs.ipv6.smp_transport.functions.output = smp_udp6_tx;
461472
smp_udp_configs.ipv6.smp_transport.functions.get_mtu = smp_udp_get_mtu;
462473
smp_udp_configs.ipv6.smp_transport.functions.ud_copy = smp_udp_ud_copy;
474+
smp_udp_configs.ipv6.smp_transport.functions.ud_init = smp_udp_ud_init;
463475

464476
rc = smp_transport_init(&smp_udp_configs.ipv6.smp_transport);
465477
#ifdef CONFIG_SMP_CLIENT
@@ -483,4 +495,24 @@ static void smp_udp_start(void)
483495
#endif
484496
}
485497

498+
#ifdef CONFIG_SMP_CLIENT
499+
int smp_client_udp_set_host_addr(struct smp_client_object *obj, struct sockaddr *addr)
500+
{
501+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV4
502+
if (obj->smpt == smp_udp_configs.ipv4_transport.smpt && addr->sa_family == AF_INET) {
503+
smp_client_object_set_data(obj, addr);
504+
return 0;
505+
}
506+
#endif
507+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_IPV6
508+
if (obj->smpt == smp_udp_configs.ipv6_transport.smpt && addr->sa_family == AF_INET6) {
509+
smp_client_object_set_data(obj, addr);
510+
return 0;
511+
}
512+
#endif
513+
514+
return -EINVAL;
515+
}
516+
#endif
517+
486518
MCUMGR_HANDLER_DEFINE(smp_udp, smp_udp_start);

0 commit comments

Comments
 (0)