Bluetooth: host: Add macros for calculating buffer sizes

Add macros for calculating buffer sizes, accounting for the various
L2CAP and HCI headers needed as well as the reserved bytes needed.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
This commit is contained in:
Joakim Andersson 2021-04-27 10:27:02 +02:00 committed by Carles Cufí
commit eba6350a12
5 changed files with 33 additions and 13 deletions

View file

@ -21,6 +21,7 @@
#include <zephyr/types.h>
#include <net/buf.h>
#include <bluetooth/hci.h>
#include <sys/util.h>
/** Possible types of buffers passed around the Bluetooth stack */
enum bt_buf_type {
@ -51,8 +52,18 @@ struct bt_buf_data {
#define BT_BUF_RESERVE CONFIG_BT_HCI_RESERVE
#endif
/** Helper to include reserved HCI data in buffer calculations */
#define BT_BUF_SIZE(size) (BT_BUF_RESERVE + (size))
/** Helper to calculate needed buffer size for HCI ACL packets */
#define BT_BUF_ACL_SIZE(size) BT_BUF_SIZE(BT_HCI_ACL_HDR_SIZE + (size))
/** Helper to calculate needed buffer size for HCI Event packets. */
#define BT_BUF_EVT_SIZE(size) BT_BUF_SIZE(BT_HCI_EVT_HDR_SIZE + (size))
/** Helper to calculate needed buffer size for HCI Command packets. */
#define BT_BUF_CMD_SIZE(size) BT_BUF_SIZE(BT_HCI_CMD_HDR_SIZE + (size))
/** Data size neeed for HCI RX buffers */
#define BT_BUF_RX_SIZE (BT_BUF_SIZE(CONFIG_BT_RX_BUF_LEN))

View file

@ -31,16 +31,28 @@ extern "C" {
/** @def BT_L2CAP_BUF_SIZE
*
* @brief Helper to calculate needed outgoing buffer size, useful e.g. for
* creating buffer pools.
* @brief Helper to calculate needed buffer size for L2CAP PDUs.
* Useful for creating buffer pools.
*
* @param mtu Needed L2CAP MTU.
* @param mtu Needed L2CAP PDU MTU.
*
* @return Needed buffer size to match the requested L2CAP PDU MTU.
*/
#define BT_L2CAP_BUF_SIZE(mtu) BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
/** L2CAP SDU header size, used for buffer size calculations */
#define BT_L2CAP_SDU_HDR_SIZE 2
/** @def BT_L2CAP_SDU_BUF_SIZE
*
* @brief Helper to calculate needed buffer size for L2CAP SDUs.
* Useful for creating buffer pools.
*
* @param mtu Needed L2CAP SDU MTU.
*
* @return Needed buffer size to match the requested L2CAP MTU.
*/
#define BT_L2CAP_BUF_SIZE(mtu) (BT_BUF_RESERVE + \
BT_HCI_ACL_HDR_SIZE + BT_L2CAP_HDR_SIZE + \
(mtu))
#define BT_L2CAP_SDU_BUF_SIZE(mtu) BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
struct bt_l2cap_chan;
@ -274,7 +286,7 @@ struct bt_l2cap_chan_ops {
/** @def BT_L2CAP_CHAN_SEND_RESERVE
* @brief Headroom needed for outgoing buffers
*/
#define BT_L2CAP_CHAN_SEND_RESERVE (BT_BUF_RESERVE + 4 + 4)
#define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
/** @brief L2CAP Server structure. */
struct bt_l2cap_server {

View file

@ -20,8 +20,7 @@ NET_BUF_POOL_FIXED_DEFINE(hci_rx_pool, CONFIG_BT_RX_BUF_COUNT,
BT_BUF_RX_SIZE, NULL);
#if defined(CONFIG_BT_CONN)
#define NUM_COMLETE_EVENT_SIZE BT_BUF_SIZE(\
sizeof(struct bt_hci_evt_hdr) + \
#define NUM_COMLETE_EVENT_SIZE BT_BUF_EVT_SIZE( \
sizeof(struct bt_hci_cp_host_num_completed_packets) + \
CONFIG_BT_MAX_CONN * sizeof(struct bt_hci_handle_count))
/* Dedicated pool for HCI_Number_of_Completed_Packets. This event is always

View file

@ -66,7 +66,7 @@ NET_BUF_POOL_FIXED_DEFINE(disc_pool, 1,
#define L2CAP_MAX_LE_MPS CONFIG_BT_L2CAP_RX_MTU
/* For now use MPS - SDU length to disable segmentation */
#define L2CAP_MAX_LE_MTU (L2CAP_MAX_LE_MPS - 2)
#define L2CAP_MAX_LE_MTU (L2CAP_MAX_LE_MPS - BT_L2CAP_SDU_HDR_SIZE)
#define L2CAP_ECRED_CHAN_MAX 5
@ -1820,7 +1820,7 @@ static int l2cap_chan_le_send_sdu(struct bt_l2cap_le_chan *ch,
if (!sent) {
/* Add SDU length for the first segment */
ret = l2cap_chan_le_send(ch, frag, BT_L2CAP_SDU_HDR_LEN);
ret = l2cap_chan_le_send(ch, frag, BT_L2CAP_SDU_HDR_SIZE);
if (ret < 0) {
if (ret == -EAGAIN) {
/* Store sent data into user_data */

View file

@ -225,8 +225,6 @@ struct bt_l2cap_ecred_reconf_rsp {
uint16_t result;
} __packed;
#define BT_L2CAP_SDU_HDR_LEN 2
struct bt_l2cap_fixed_chan {
uint16_t cid;
int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);