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 <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2018-06-06 16:28:01 +02:00 committed by Johan Hedberg
parent 982385277b
commit cd1111e16f
2 changed files with 29 additions and 12 deletions

View file

@ -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

View file

@ -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;
}