Skip to content

Commit e0d90de

Browse files
committed
[nrf fromtree] mgmt: mcumgr: Make Bluetooth and UDP transport init automatic
This moves the UDP and Bluetooth initialisation for MCUmgr to be performed automatically with the new hander registration feature. Signed-off-by: Jamie McCrae <[email protected]> (cherry picked from commit b4b6346)
1 parent 6041217 commit e0d90de

File tree

8 files changed

+149
-45
lines changed

8 files changed

+149
-45
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,37 @@ extern "C" {
1919
#endif
2020

2121
/**
22-
* @brief Registers the SMP Bluetooth service.
22+
* @brief Registers the SMP Bluetooth service. Should only be called if the Bluetooth
23+
* transport has been unregistered by calling smp_bt_unregister().
2324
*
24-
* @return 0 on success; negative error code on failure.
25+
* @return 0 on success; negative error code on failure.
2526
*/
2627
int smp_bt_register(void);
2728

2829
/**
29-
* @brief Unregisters the SMP Bluetooth service.
30+
* @brief Unregisters the SMP Bluetooth service.
3031
*
31-
* @return 0 on success; negative error code on failure.
32+
* @return 0 on success; negative error code on failure.
3233
*/
3334
int smp_bt_unregister(void);
3435

3536
/**
36-
* Transmits an SMP command/response over the specified Bluetooth connection
37-
* as a notification.
37+
* @brief Sets up the Bluetooth SMP (MCUmgr) transport. This should be called if the Kconfig
38+
* option ``CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT`` is not enabled, this will
39+
* register the transport and add it to the Bluetooth registered services so that
40+
* clients can access registered MCUmgr commands.
41+
*/
42+
void smp_bt_start(void);
43+
44+
/**
45+
* @brief Transmits an SMP command/response over the specified Bluetooth connection as a
46+
* notification.
3847
*
39-
* @param conn Connection object.
40-
* @param data Pointer to SMP message.
41-
* @param len data length.
48+
* @param conn Connection object.
49+
* @param data Pointer to SMP message.
50+
* @param len data length.
4251
*
43-
* @return 0 in case of success or negative value in case of error.
52+
* @return 0 in case of success or negative value in case of error.
4453
*/
4554
int smp_bt_notify(struct bt_conn *conn, const void *data, uint16_t len);
4655

samples/subsys/mgmt/mcumgr/smp_svr/src/bluetooth.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,9 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
5858
.disconnected = disconnected,
5959
};
6060

61-
static void bt_ready(int err)
62-
{
63-
if (err) {
64-
LOG_ERR("Bluetooth init failed (err %d)", err);
65-
return;
66-
}
67-
68-
LOG_INF("Bluetooth initialized");
69-
70-
k_work_submit(&advertise_work);
71-
}
72-
73-
void start_smp_bluetooth(void)
61+
void start_smp_bluetooth_adverts(void)
7462
{
7563
k_work_init(&advertise_work, advertise);
7664

77-
/* Enable Bluetooth. */
78-
int rc = bt_enable(bt_ready);
79-
80-
if (rc != 0) {
81-
LOG_ERR("Bluetooth init failed (err %d)", rc);
82-
return;
83-
}
84-
85-
/* Initialize the Bluetooth mcumgr transport. */
86-
smp_bt_register();
65+
k_work_submit(&advertise_work);
8766
}

samples/subsys/mgmt/mcumgr/smp_svr/src/common.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
void start_smp_bluetooth(void);
8-
void start_smp_udp(void);
7+
void start_smp_bluetooth_adverts(void);

samples/subsys/mgmt/mcumgr/smp_svr/src/main.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ void main(void)
6767
}
6868
#endif
6969
#ifdef CONFIG_MCUMGR_SMP_BT
70-
start_smp_bluetooth();
71-
#endif
72-
#ifdef CONFIG_MCUMGR_SMP_UDP
73-
start_smp_udp();
70+
start_smp_bluetooth_adverts();
7471
#endif
7572

7673
if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {

subsys/mgmt/mcumgr/transport/Kconfig.bluetooth

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,24 @@ config MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RETRY_TIME
8787

8888
endif # MCUMGR_SMP_BT_CONN_PARAM_CONTROL
8989

90+
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
91+
bool "Bluetooth SMP stack enable autostart/setup"
92+
default y
93+
help
94+
If this option is enabled, then the Bluetooth stack will be enabled
95+
when the Bluetooth SMP (MCUmgr) transport initialises. If another
96+
system or the application does this, then this option should be
97+
disabled. Note that Bluetooth will need to be enabled prior to the
98+
Bluetooth SMP (MCUmgr) transport being initialised (by calling
99+
``smp_bt_start()``) if this option is disabled.
100+
101+
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
102+
bool "Bluetooth SMP wait for autostart/setup completion"
103+
default y
104+
depends on MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
105+
help
106+
If this option is enabled, then the setup handler will wait for the
107+
Bluetooth stack to become ready from the ``bt_enable()`` call,
108+
otherwise will return instantly without waiting.
109+
90110
endif # MCUMGR_SMP_BT

subsys/mgmt/mcumgr/transport/Kconfig.udp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ config MCUMGR_SMP_UDP_MTU
5656
MCUMGR_SMP_UDP_MTU <= MCUMGR_BUF_SIZE + SMP msg overhead - address size
5757
where address size is determined by IPv4/IPv6 selection.
5858

59+
config MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
60+
bool "UDP SMP autostart/setup"
61+
default y
62+
help
63+
Enable setting up the UDP SMP transport at boot time without needing
64+
any code in the application to do this. Will automatically start the
65+
UDP SMP service when the network interface is up and disable it when
66+
it goes down (at layer 4).
67+
5968
endif # MCUMGR_SMP_UDP

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

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @brief Bluetooth transport for the mcumgr SMP protocol.
1010
*/
1111

12-
1312
#include <zephyr/kernel.h>
1413
#include <zephyr/init.h>
1514
#include <zephyr/bluetooth/bluetooth.h>
@@ -24,6 +23,10 @@
2423
#include <mgmt/mcumgr/transport/smp_internal.h>
2524
#include <mgmt/mcumgr/transport/smp_reassembly.h>
2625

26+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
27+
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
28+
#endif
29+
2730
#include <zephyr/logging/log.h>
2831
LOG_MODULE_DECLARE(mcumgr_smp, CONFIG_MCUMGR_SMP_LOG_LEVEL);
2932

@@ -87,6 +90,10 @@ static uint8_t next_id;
8790
static struct smp_transport smp_bt_transport;
8891
static struct conn_param_data conn_data[CONFIG_BT_MAX_CONN];
8992

93+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
94+
static K_SEM_DEFINE(bt_ready_sem, 0, 1);
95+
#endif
96+
9097
/* SMP service.
9198
* {8D53DC1D-1DB7-4CD3-868B-8A527460AA84}
9299
*/
@@ -627,10 +634,24 @@ static bool smp_bt_query_valid_check(struct net_buf *nb, void *arg)
627634
return true;
628635
}
629636

630-
static int smp_bt_init(const struct device *dev)
637+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
638+
static void bt_ready(int err)
639+
{
640+
if (err) {
641+
LOG_ERR("Bluetooth init failed (err %d)", err);
642+
return;
643+
}
644+
645+
LOG_INF("Bluetooth initialised");
646+
647+
k_sem_give(&bt_ready_sem);
648+
}
649+
#endif
650+
651+
void smp_bt_start(void)
631652
{
653+
int rc;
632654
uint8_t i = 0;
633-
ARG_UNUSED(dev);
634655

635656
next_id = 1;
636657

@@ -639,6 +660,7 @@ static int smp_bt_init(const struct device *dev)
639660
.connected = connected,
640661
.disconnected = disconnected,
641662
};
663+
642664
bt_conn_cb_register(&conn_callbacks);
643665

644666
if (IS_ENABLED(CONFIG_MCUMGR_SMP_BT_CONN_PARAM_CONTROL)) {
@@ -653,7 +675,34 @@ static int smp_bt_init(const struct device *dev)
653675
smp_transport_init(&smp_bt_transport, smp_bt_tx_pkt,
654676
smp_bt_get_mtu, smp_bt_ud_copy,
655677
smp_bt_ud_free, smp_bt_query_valid_check);
656-
return 0;
678+
679+
rc = smp_bt_register();
680+
681+
if (rc != 0) {
682+
LOG_ERR("Bluetooth SMP transport register failed (err %d)", rc);
683+
}
684+
}
685+
686+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
687+
static void smp_bt_setup(void)
688+
{
689+
/* Enable Bluetooth */
690+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
691+
int rc = bt_enable(bt_ready);
692+
#else
693+
int rc = bt_enable(NULL);
694+
#endif
695+
696+
if (rc != 0) {
697+
LOG_ERR("Bluetooth init failed (err %d)", rc);
698+
} else {
699+
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
700+
k_sem_take(&bt_ready_sem, K_FOREVER);
701+
#endif
702+
}
703+
704+
smp_bt_start();
657705
}
658706

659-
SYS_INIT(smp_bt_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
707+
MCUMGR_HANDLER_DEFINE(smp_bt, smp_bt_setup);
708+
#endif

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
* Copyright (c) 2019, Prevas A/S
2+
* Copyright (c) 2019-2020, Prevas A/S
3+
* Copyright (c) 2022 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -25,6 +26,13 @@
2526

2627
#include <mgmt/mcumgr/transport/smp_internal.h>
2728

29+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
30+
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
31+
#include <zephyr/net/net_mgmt.h>
32+
#include <zephyr/net/net_event.h>
33+
#include <zephyr/net/net_conn_mgr.h>
34+
#endif
35+
2836
#define LOG_LEVEL CONFIG_MCUMGR_LOG_LEVEL
2937
#include <zephyr/logging/log.h>
3038
LOG_MODULE_REGISTER(smp_udp);
@@ -64,6 +72,10 @@ static struct configs configs = {
6472
#endif
6573
};
6674

75+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
76+
static struct net_mgmt_event_callback smp_udp_mgmt_cb;
77+
#endif
78+
6779
#ifdef CONFIG_MCUMGR_SMP_UDP_IPV4
6880
static int smp_udp4_tx(struct net_buf *nb)
6981
{
@@ -280,3 +292,33 @@ int smp_udp_close(void)
280292

281293
return MGMT_ERR_EOK;
282294
}
295+
296+
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
297+
static void smp_udp_net_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
298+
struct net_if *iface)
299+
{
300+
ARG_UNUSED(cb);
301+
ARG_UNUSED(iface);
302+
303+
if (mgmt_event == NET_EVENT_L4_CONNECTED) {
304+
LOG_INF("Network connected");
305+
306+
if (smp_udp_open() < 0) {
307+
LOG_ERR("Could not open SMP UDP");
308+
}
309+
} else if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
310+
LOG_INF("Network disconnected");
311+
smp_udp_close();
312+
}
313+
}
314+
315+
static void smp_udp_start(void)
316+
{
317+
net_mgmt_init_event_callback(&smp_udp_mgmt_cb, smp_udp_net_event_handler,
318+
(NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED));
319+
net_mgmt_add_event_callback(&smp_udp_mgmt_cb);
320+
net_conn_mgr_resend_status();
321+
}
322+
323+
MCUMGR_HANDLER_DEFINE(smp_udp, smp_udp_start);
324+
#endif

0 commit comments

Comments
 (0)