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>
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>
2831LOG_MODULE_DECLARE (mcumgr_smp , CONFIG_MCUMGR_SMP_LOG_LEVEL );
2932
@@ -87,6 +90,10 @@ static uint8_t next_id;
8790static struct smp_transport smp_bt_transport ;
8891static 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
0 commit comments