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 <jamie.mccrae@nordicsemi.no>
This commit is contained in:
parent
541fcb884d
commit
b4b6346cdc
8 changed files with 149 additions and 45 deletions
|
@ -19,28 +19,37 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Registers the SMP Bluetooth service.
|
* @brief Registers the SMP Bluetooth service. Should only be called if the Bluetooth
|
||||||
|
* transport has been unregistered by calling smp_bt_unregister().
|
||||||
*
|
*
|
||||||
* @return 0 on success; negative error code on failure.
|
* @return 0 on success; negative error code on failure.
|
||||||
*/
|
*/
|
||||||
int smp_bt_register(void);
|
int smp_bt_register(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Unregisters the SMP Bluetooth service.
|
* @brief Unregisters the SMP Bluetooth service.
|
||||||
*
|
*
|
||||||
* @return 0 on success; negative error code on failure.
|
* @return 0 on success; negative error code on failure.
|
||||||
*/
|
*/
|
||||||
int smp_bt_unregister(void);
|
int smp_bt_unregister(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transmits an SMP command/response over the specified Bluetooth connection
|
* @brief Sets up the Bluetooth SMP (MCUmgr) transport. This should be called if the Kconfig
|
||||||
* as a notification.
|
* option ``CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT`` is not enabled, this will
|
||||||
|
* register the transport and add it to the Bluetooth registered services so that
|
||||||
|
* clients can access registered MCUmgr commands.
|
||||||
|
*/
|
||||||
|
void smp_bt_start(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Transmits an SMP command/response over the specified Bluetooth connection as a
|
||||||
|
* notification.
|
||||||
*
|
*
|
||||||
* @param conn Connection object.
|
* @param conn Connection object.
|
||||||
* @param data Pointer to SMP message.
|
* @param data Pointer to SMP message.
|
||||||
* @param len data length.
|
* @param len data length.
|
||||||
*
|
*
|
||||||
* @return 0 in case of success or negative value in case of error.
|
* @return 0 in case of success or negative value in case of error.
|
||||||
*/
|
*/
|
||||||
int smp_bt_notify(struct bt_conn *conn, const void *data, uint16_t len);
|
int smp_bt_notify(struct bt_conn *conn, const void *data, uint16_t len);
|
||||||
|
|
||||||
|
|
|
@ -58,30 +58,9 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
|
||||||
.disconnected = disconnected,
|
.disconnected = disconnected,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bt_ready(int err)
|
void start_smp_bluetooth_adverts(void)
|
||||||
{
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("Bluetooth init failed (err %d)", err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("Bluetooth initialized");
|
|
||||||
|
|
||||||
k_work_submit(&advertise_work);
|
|
||||||
}
|
|
||||||
|
|
||||||
void start_smp_bluetooth(void)
|
|
||||||
{
|
{
|
||||||
k_work_init(&advertise_work, advertise);
|
k_work_init(&advertise_work, advertise);
|
||||||
|
|
||||||
/* Enable Bluetooth. */
|
k_work_submit(&advertise_work);
|
||||||
int rc = bt_enable(bt_ready);
|
|
||||||
|
|
||||||
if (rc != 0) {
|
|
||||||
LOG_ERR("Bluetooth init failed (err %d)", rc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Initialize the Bluetooth mcumgr transport. */
|
|
||||||
smp_bt_register();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,4 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void start_smp_bluetooth(void);
|
void start_smp_bluetooth_adverts(void);
|
||||||
void start_smp_udp(void);
|
|
||||||
|
|
|
@ -67,10 +67,7 @@ void main(void)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef CONFIG_MCUMGR_SMP_BT
|
#ifdef CONFIG_MCUMGR_SMP_BT
|
||||||
start_smp_bluetooth();
|
start_smp_bluetooth_adverts();
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_MCUMGR_SMP_UDP
|
|
||||||
start_smp_udp();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
|
if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
|
||||||
|
|
|
@ -87,4 +87,24 @@ config MCUMGR_SMP_BT_CONN_PARAM_CONTROL_RETRY_TIME
|
||||||
|
|
||||||
endif # MCUMGR_SMP_BT_CONN_PARAM_CONTROL
|
endif # MCUMGR_SMP_BT_CONN_PARAM_CONTROL
|
||||||
|
|
||||||
|
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
|
||||||
|
bool "Bluetooth SMP stack enable autostart/setup"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
If this option is enabled, then the Bluetooth stack will be enabled
|
||||||
|
when the Bluetooth SMP (MCUmgr) transport initialises. If another
|
||||||
|
system or the application does this, then this option should be
|
||||||
|
disabled. Note that Bluetooth will need to be enabled prior to the
|
||||||
|
Bluetooth SMP (MCUmgr) transport being initialised (by calling
|
||||||
|
``smp_bt_start()``) if this option is disabled.
|
||||||
|
|
||||||
|
config MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
|
||||||
|
bool "Bluetooth SMP wait for autostart/setup completion"
|
||||||
|
default y
|
||||||
|
depends on MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
|
||||||
|
help
|
||||||
|
If this option is enabled, then the setup handler will wait for the
|
||||||
|
Bluetooth stack to become ready from the ``bt_enable()`` call,
|
||||||
|
otherwise will return instantly without waiting.
|
||||||
|
|
||||||
endif # MCUMGR_SMP_BT
|
endif # MCUMGR_SMP_BT
|
||||||
|
|
|
@ -56,4 +56,13 @@ config MCUMGR_SMP_UDP_MTU
|
||||||
MCUMGR_SMP_UDP_MTU <= MCUMGR_BUF_SIZE + SMP msg overhead - address size
|
MCUMGR_SMP_UDP_MTU <= MCUMGR_BUF_SIZE + SMP msg overhead - address size
|
||||||
where address size is determined by IPv4/IPv6 selection.
|
where address size is determined by IPv4/IPv6 selection.
|
||||||
|
|
||||||
|
config MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
|
||||||
|
bool "UDP SMP autostart/setup"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
Enable setting up the UDP SMP transport at boot time without needing
|
||||||
|
any code in the application to do this. Will automatically start the
|
||||||
|
UDP SMP service when the network interface is up and disable it when
|
||||||
|
it goes down (at layer 4).
|
||||||
|
|
||||||
endif # MCUMGR_SMP_UDP
|
endif # MCUMGR_SMP_UDP
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
* @brief Bluetooth transport for the mcumgr SMP protocol.
|
* @brief Bluetooth transport for the mcumgr SMP protocol.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/init.h>
|
#include <zephyr/init.h>
|
||||||
#include <zephyr/bluetooth/bluetooth.h>
|
#include <zephyr/bluetooth/bluetooth.h>
|
||||||
|
@ -24,6 +23,10 @@
|
||||||
#include <mgmt/mcumgr/transport/smp_internal.h>
|
#include <mgmt/mcumgr/transport/smp_internal.h>
|
||||||
#include <mgmt/mcumgr/transport/smp_reassembly.h>
|
#include <mgmt/mcumgr/transport/smp_reassembly.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
|
||||||
|
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
LOG_MODULE_DECLARE(mcumgr_smp, CONFIG_MCUMGR_SMP_LOG_LEVEL);
|
LOG_MODULE_DECLARE(mcumgr_smp, CONFIG_MCUMGR_SMP_LOG_LEVEL);
|
||||||
|
|
||||||
|
@ -87,6 +90,10 @@ static uint8_t next_id;
|
||||||
static struct smp_transport smp_bt_transport;
|
static struct smp_transport smp_bt_transport;
|
||||||
static struct conn_param_data conn_data[CONFIG_BT_MAX_CONN];
|
static struct conn_param_data conn_data[CONFIG_BT_MAX_CONN];
|
||||||
|
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
|
||||||
|
static K_SEM_DEFINE(bt_ready_sem, 0, 1);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* SMP service.
|
/* SMP service.
|
||||||
* {8D53DC1D-1DB7-4CD3-868B-8A527460AA84}
|
* {8D53DC1D-1DB7-4CD3-868B-8A527460AA84}
|
||||||
*/
|
*/
|
||||||
|
@ -627,10 +634,24 @@ static bool smp_bt_query_valid_check(struct net_buf *nb, void *arg)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int smp_bt_init(const struct device *dev)
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
|
||||||
|
static void bt_ready(int err)
|
||||||
{
|
{
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Bluetooth init failed (err %d)", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_INF("Bluetooth initialised");
|
||||||
|
|
||||||
|
k_sem_give(&bt_ready_sem);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void smp_bt_start(void)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
ARG_UNUSED(dev);
|
|
||||||
|
|
||||||
next_id = 1;
|
next_id = 1;
|
||||||
|
|
||||||
|
@ -639,6 +660,7 @@ static int smp_bt_init(const struct device *dev)
|
||||||
.connected = connected,
|
.connected = connected,
|
||||||
.disconnected = disconnected,
|
.disconnected = disconnected,
|
||||||
};
|
};
|
||||||
|
|
||||||
bt_conn_cb_register(&conn_callbacks);
|
bt_conn_cb_register(&conn_callbacks);
|
||||||
|
|
||||||
if (IS_ENABLED(CONFIG_MCUMGR_SMP_BT_CONN_PARAM_CONTROL)) {
|
if (IS_ENABLED(CONFIG_MCUMGR_SMP_BT_CONN_PARAM_CONTROL)) {
|
||||||
|
@ -653,7 +675,34 @@ static int smp_bt_init(const struct device *dev)
|
||||||
smp_transport_init(&smp_bt_transport, smp_bt_tx_pkt,
|
smp_transport_init(&smp_bt_transport, smp_bt_tx_pkt,
|
||||||
smp_bt_get_mtu, smp_bt_ud_copy,
|
smp_bt_get_mtu, smp_bt_ud_copy,
|
||||||
smp_bt_ud_free, smp_bt_query_valid_check);
|
smp_bt_ud_free, smp_bt_query_valid_check);
|
||||||
return 0;
|
|
||||||
|
rc = smp_bt_register();
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Bluetooth SMP transport register failed (err %d)", rc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SYS_INIT(smp_bt_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT
|
||||||
|
static void smp_bt_setup(void)
|
||||||
|
{
|
||||||
|
/* Enable Bluetooth */
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
|
||||||
|
int rc = bt_enable(bt_ready);
|
||||||
|
#else
|
||||||
|
int rc = bt_enable(NULL);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
LOG_ERR("Bluetooth init failed (err %d)", rc);
|
||||||
|
} else {
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_BT_AUTOMATIC_INIT_WAIT
|
||||||
|
k_sem_take(&bt_ready_sem, K_FOREVER);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
smp_bt_start();
|
||||||
|
}
|
||||||
|
|
||||||
|
MCUMGR_HANDLER_DEFINE(smp_bt, smp_bt_setup);
|
||||||
|
#endif
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019, Prevas A/S
|
* Copyright (c) 2019-2020, Prevas A/S
|
||||||
|
* Copyright (c) 2022 Nordic Semiconductor ASA
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
@ -25,6 +26,13 @@
|
||||||
|
|
||||||
#include <mgmt/mcumgr/transport/smp_internal.h>
|
#include <mgmt/mcumgr/transport/smp_internal.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
|
||||||
|
#include <zephyr/mgmt/mcumgr/mgmt/handlers.h>
|
||||||
|
#include <zephyr/net/net_mgmt.h>
|
||||||
|
#include <zephyr/net/net_event.h>
|
||||||
|
#include <zephyr/net/net_conn_mgr.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define LOG_LEVEL CONFIG_MCUMGR_LOG_LEVEL
|
#define LOG_LEVEL CONFIG_MCUMGR_LOG_LEVEL
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
LOG_MODULE_REGISTER(smp_udp);
|
LOG_MODULE_REGISTER(smp_udp);
|
||||||
|
@ -64,6 +72,10 @@ static struct configs configs = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
|
||||||
|
static struct net_mgmt_event_callback smp_udp_mgmt_cb;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MCUMGR_SMP_UDP_IPV4
|
#ifdef CONFIG_MCUMGR_SMP_UDP_IPV4
|
||||||
static int smp_udp4_tx(struct net_buf *nb)
|
static int smp_udp4_tx(struct net_buf *nb)
|
||||||
{
|
{
|
||||||
|
@ -280,3 +292,33 @@ int smp_udp_close(void)
|
||||||
|
|
||||||
return MGMT_ERR_EOK;
|
return MGMT_ERR_EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_MCUMGR_TRANSPORT_UDP_AUTOMATIC_INIT
|
||||||
|
static void smp_udp_net_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
|
||||||
|
struct net_if *iface)
|
||||||
|
{
|
||||||
|
ARG_UNUSED(cb);
|
||||||
|
ARG_UNUSED(iface);
|
||||||
|
|
||||||
|
if (mgmt_event == NET_EVENT_L4_CONNECTED) {
|
||||||
|
LOG_INF("Network connected");
|
||||||
|
|
||||||
|
if (smp_udp_open() < 0) {
|
||||||
|
LOG_ERR("Could not open SMP UDP");
|
||||||
|
}
|
||||||
|
} else if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
|
||||||
|
LOG_INF("Network disconnected");
|
||||||
|
smp_udp_close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void smp_udp_start(void)
|
||||||
|
{
|
||||||
|
net_mgmt_init_event_callback(&smp_udp_mgmt_cb, smp_udp_net_event_handler,
|
||||||
|
(NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED));
|
||||||
|
net_mgmt_add_event_callback(&smp_udp_mgmt_cb);
|
||||||
|
net_conn_mgr_resend_status();
|
||||||
|
}
|
||||||
|
|
||||||
|
MCUMGR_HANDLER_DEFINE(smp_udp, smp_udp_start);
|
||||||
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue