From cd1111e16f6f5edb48e23a5d8acd9dd54727bd97 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Wed, 6 Jun 2018 16:28:01 +0200 Subject: [PATCH] mgmt: Reduce net_buf user data requirement Instead of storing a bt_addr_le_t, just store a pointer to the bt_conn object (which is what the code is interested in anyway). This way the user data size requirement drops from 7 to 4, which is the default that all current users are happy with. Signed-off-by: Johan Hedberg --- subsys/mgmt/Kconfig | 4 ++-- subsys/mgmt/smp_bt.c | 37 +++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/subsys/mgmt/Kconfig b/subsys/mgmt/Kconfig index 3c35684385..99e22d1e96 100644 --- a/subsys/mgmt/Kconfig +++ b/subsys/mgmt/Kconfig @@ -87,10 +87,10 @@ config MCUMGR_BUF_SIZE config MCUMGR_BUF_USER_DATA_SIZE int prompt "Size of mcumgr buffer user data" - default 7 + default 4 help The size, in bytes, of user data to allocate for each mcumgr buffer. Different mcumgr transports impose different requirements for this - setting. A value of 7 is sufficient for UART, shell, and bluetooth. + setting. A value of 4 is sufficient for UART, shell, and bluetooth. endmenu diff --git a/subsys/mgmt/smp_bt.c b/subsys/mgmt/smp_bt.c index c667f4e87c..44d5a55c2f 100644 --- a/subsys/mgmt/smp_bt.c +++ b/subsys/mgmt/smp_bt.c @@ -23,6 +23,10 @@ struct device; +struct smp_bt_user_data { + struct bt_conn *conn; +}; + static struct zephyr_smp_transport smp_bt_transport; /* SMP service. @@ -47,14 +51,14 @@ static ssize_t smp_bt_chr_write(struct bt_conn *conn, const void *buf, u16_t len, u16_t offset, u8_t flags) { - const bt_addr_le_t *addr; + struct smp_bt_user_data *ud; struct net_buf *nb; nb = mcumgr_buf_alloc(); net_buf_add_mem(nb, buf, len); - addr = bt_conn_get_dst(conn); - memcpy(net_buf_user_data(nb), addr, sizeof(*addr)); + ud = net_buf_user_data(nb); + ud->conn = bt_conn_ref(conn); zephyr_smp_rx_req(&smp_bt_transport, nb); @@ -89,16 +93,17 @@ static int smp_bt_tx_rsp(struct bt_conn *conn, const void *data, u16_t len) } /** - * Extracts the peer address from a net_buf's user data and looks up the - * corresponding conection. + * Extracts the Bluetooth connection from a net_buf's user data. */ static struct bt_conn *smp_bt_conn_from_pkt(const struct net_buf *nb) { - bt_addr_le_t addr; + struct smp_bt_user_data *ud = net_buf_user_data(nb); - /* Cast away const. */ - memcpy(&addr, net_buf_user_data((void *)nb), sizeof(addr)); - return bt_conn_lookup_addr_le(&addr); + if (!ud->conn) { + return NULL; + } + + return bt_conn_ref(ud->conn); } /** @@ -122,6 +127,18 @@ static u16_t smp_bt_get_mtu(const struct net_buf *nb) return mtu - 3; } +static void smp_bt_buf_free(struct net_buf *nb) +{ + struct smp_bt_user_data *ud = net_buf_user_data(nb); + + if (ud->conn) { + bt_conn_unref(ud->conn); + ud->conn = NULL; + } + + mcumgr_buf_free(nb); +} + /** * Transmits the specified SMP response. */ @@ -138,7 +155,7 @@ static int smp_bt_tx_pkt(struct zephyr_smp_transport *zst, struct net_buf *nb) bt_conn_unref(conn); } - mcumgr_buf_free(nb); + smp_bt_buf_free(nb); return rc; }