Skip to content

Commit 0eb2382

Browse files
nordicjmmbolivar-nordic
authored andcommitted
[nrf fromtree] mgmt: mcumgr: transport: smp_bt: Fix wrongly enabling Bluetooth
Bluetooth does not need to be enabled to register services, therefore the newly introduced automatic bluetooth SMP transport registration system can be simplified by returning enabling of bluetooth back to the application. Signed-off-by: Jamie McCrae <[email protected]> (cherry picked from commit 4516e71) (cherry picked from commit 7074afa)
1 parent 5b0872a commit 0eb2382

File tree

5 files changed

+27
-82
lines changed

5 files changed

+27
-82
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ int smp_bt_register(void);
3333
*/
3434
int smp_bt_unregister(void);
3535

36-
/**
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-
4436
/**
4537
* @brief Transmits an SMP command/response over the specified Bluetooth connection as a
4638
* notification.

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

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

61+
static void bt_ready(int err)
62+
{
63+
if (err != 0) {
64+
LOG_ERR("Bluetooth failed to initialise: %d", err);
65+
} else {
66+
k_work_submit(&advertise_work);
67+
}
68+
}
69+
6170
void start_smp_bluetooth_adverts(void)
6271
{
72+
int rc;
73+
6374
k_work_init(&advertise_work, advertise);
75+
rc = bt_enable(bt_ready);
6476

65-
k_work_submit(&advertise_work);
77+
if (rc != 0) {
78+
LOG_ERR("Bluetooth enable failed: %d", rc);
79+
}
6680
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void main(void)
6666
LOG_ERR("Error mounting littlefs [%d]", rc);
6767
}
6868
#endif
69+
6970
#ifdef CONFIG_MCUMGR_SMP_BT
7071
start_smp_bluetooth_adverts();
7172
#endif

subsys/mgmt/mcumgr/transport/Kconfig.bluetooth

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

8787
endif # MCUMGR_SMP_BT_CONN_PARAM_CONTROL
8888

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

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

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
#include <zephyr/mgmt/mcumgr/smp/smp.h>
1919
#include <zephyr/mgmt/mcumgr/transport/smp.h>
2020
#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
21+
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
2122
#include <errno.h>
2223

2324
#include <mgmt/mcumgr/transport/smp_internal.h>
2425
#include <mgmt/mcumgr/transport/smp_reassembly.h>
2526

26-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
27-
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
28-
#endif
29-
3027
#include <zephyr/logging/log.h>
3128
LOG_MODULE_DECLARE(mcumgr_smp, CONFIG_MCUMGR_SMP_LOG_LEVEL);
3229

@@ -90,10 +87,6 @@ static uint8_t next_id;
9087
static struct smp_transport smp_bt_transport;
9188
static struct conn_param_data conn_data[CONFIG_BT_MAX_CONN];
9289

93-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
94-
static K_SEM_DEFINE(bt_ready_sem, 0, 1);
95-
#endif
96-
9790
/* SMP service.
9891
* {8D53DC1D-1DB7-4CD3-868B-8A527460AA84}
9992
*/
@@ -106,6 +99,15 @@ static struct bt_uuid_128 smp_bt_svc_uuid = BT_UUID_INIT_128(
10699
static struct bt_uuid_128 smp_bt_chr_uuid = BT_UUID_INIT_128(
107100
BT_UUID_128_ENCODE(0xda2e7828, 0xfbce, 0x4e01, 0xae9e, 0x261174997c48));
108101

102+
static void connected(struct bt_conn *conn, uint8_t err);
103+
static void disconnected(struct bt_conn *conn, uint8_t reason);
104+
105+
/* Bluetooth connection callback handlers */
106+
BT_CONN_CB_DEFINE(mcumgr_bt_callbacks) = {
107+
.connected = connected,
108+
.disconnected = disconnected,
109+
};
110+
109111
/* Helper function that allocates conn_param_data for a conn. */
110112
static struct conn_param_data *conn_param_data_alloc(struct bt_conn *conn)
111113
{
@@ -634,35 +636,13 @@ static bool smp_bt_query_valid_check(struct net_buf *nb, void *arg)
634636
return true;
635637
}
636638

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)
639+
static void smp_bt_setup(void)
652640
{
653641
int rc;
654642
uint8_t i = 0;
655643

656644
next_id = 1;
657645

658-
/* Register BT callbacks */
659-
static struct bt_conn_cb conn_callbacks = {
660-
.connected = connected,
661-
.disconnected = disconnected,
662-
};
663-
664-
bt_conn_cb_register(&conn_callbacks);
665-
666646
if (IS_ENABLED(CONFIG_MCUMGR_SMP_BT_CONN_PARAM_CONTROL)) {
667647
conn_param_control_init();
668648
}
@@ -683,26 +663,4 @@ void smp_bt_start(void)
683663
}
684664
}
685665

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();
705-
}
706-
707666
MCUMGR_HANDLER_DEFINE(smp_bt, smp_bt_setup);
708-
#endif

0 commit comments

Comments
 (0)