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 {