Skip to content

Commit 4516e71

Browse files
nordicjmfabiobaltieri
authored andcommitted
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]>
1 parent 9fef0c5 commit 4516e71

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_TRANSPORT_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
@@ -93,24 +93,4 @@ config MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL_RETRY_TIME
9393

9494
endif # MCUMGR_TRASNPORT_BT_CONN_PARAM_CONTROL
9595

96-
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
97-
bool "Bluetooth SMP stack enable autostart/setup"
98-
default y
99-
help
100-
If this option is enabled, then the Bluetooth stack will be enabled
101-
when the Bluetooth SMP (MCUmgr) transport initialises. If another
102-
system or the application does this, then this option should be
103-
disabled. Note that Bluetooth will need to be enabled prior to the
104-
Bluetooth SMP (MCUmgr) transport being initialised (by calling
105-
``smp_bt_start()``) if this option is disabled.
106-
107-
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
108-
bool "Bluetooth SMP wait for autostart/setup completion"
109-
default y
110-
depends on MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
111-
help
112-
If this option is enabled, then the setup handler will wait for the
113-
Bluetooth stack to become ready from the ``bt_enable()`` call,
114-
otherwise will return instantly without waiting.
115-
11696
endif # MCUMGR_TRANSPORT_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_TRANSPORT_LOG_LEVEL);
3229

@@ -91,10 +88,6 @@ static uint8_t next_id;
9188
static struct smp_transport smp_bt_transport;
9289
static struct conn_param_data conn_data[CONFIG_BT_MAX_CONN];
9390

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

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

638-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
639-
static void bt_ready(int err)
640-
{
641-
if (err) {
642-
LOG_ERR("Bluetooth init failed (err %d)", err);
643-
return;
644-
}
645-
646-
LOG_INF("Bluetooth initialised");
647-
648-
k_sem_give(&bt_ready_sem);
649-
}
650-
#endif
651-
652-
void smp_bt_start(void)
640+
static void smp_bt_setup(void)
653641
{
654642
int rc;
655643
uint8_t i = 0;
656644

657645
next_id = 1;
658646

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

687-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
688-
static void smp_bt_setup(void)
689-
{
690-
/* Enable Bluetooth */
691-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
692-
int rc = bt_enable(bt_ready);
693-
#else
694-
int rc = bt_enable(NULL);
695-
#endif
696-
697-
if (rc != 0) {
698-
LOG_ERR("Bluetooth init failed (err %d)", rc);
699-
} else {
700-
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
701-
k_sem_take(&bt_ready_sem, K_FOREVER);
702-
#endif
703-
}
704-
705-
smp_bt_start();
706-
}
707-
708667
MCUMGR_HANDLER_DEFINE(smp_bt, smp_bt_setup);
709-
#endif

0 commit comments

Comments
 (0)