Skip to content

Commit 5a11a19

Browse files
Thalleycfriedt
authored andcommitted
Bluetooth: Iso: Add BT_ISO_SDU_BUF_SIZE macro
Add the BT_ISO_SDU_BUF_SIZE which can be used to declare the TX buffers for ISO, similar to the BT_L2CAP_SDU_BUF_SIZE macro for L2CAP. This also updates the ISO samples to use this as well updating the SDU check to use CONFIG_BT_ISO_TX_MTU/CONFIG_BT_ISO_RX_MTU without subtracting the BT_ISO_CHAN_SEND_RESERVE to make the API more clear. Signed-off-by: Emil Gydesen <[email protected]>
1 parent c5634f1 commit 5a11a19

File tree

10 files changed

+47
-28
lines changed

10 files changed

+47
-28
lines changed

include/bluetooth/buf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ struct bt_buf_data {
6868
/** Helper to calculate needed buffer size for HCI Command packets. */
6969
#define BT_BUF_CMD_SIZE(size) BT_BUF_SIZE(BT_HCI_CMD_HDR_SIZE + (size))
7070

71+
/** Helper to calculate needed buffer size for HCI ISO packets. */
72+
#define BT_BUF_ISO_SIZE(size) BT_BUF_SIZE(BT_HCI_ISO_HDR_SIZE + \
73+
BT_HCI_ISO_DATA_HDR_SIZE + \
74+
(size))
75+
7176
/** Data size needed for HCI ACL RX buffers */
7277
#define BT_BUF_ACL_RX_SIZE BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_RX_SIZE)
7378

include/bluetooth/iso.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ extern "C" {
2828
#include <bluetooth/hci.h>
2929

3030
/** @def BT_ISO_CHAN_SEND_RESERVE
31-
* @brief Headroom needed for outgoing buffers
31+
* @brief Headroom needed for outgoing ISO SDUs
3232
*/
33-
#define BT_ISO_CHAN_SEND_RESERVE (CONFIG_BT_HCI_RESERVE + \
34-
BT_HCI_ISO_HDR_SIZE + \
35-
BT_HCI_ISO_DATA_HDR_SIZE)
33+
#define BT_ISO_CHAN_SEND_RESERVE BT_BUF_ISO_SIZE(0)
34+
35+
/** @def BT_ISO_SDU_BUF_SIZE
36+
*
37+
* @brief Helper to calculate needed buffer size for ISO SDUs.
38+
* Useful for creating buffer pools.
39+
*
40+
* @param mtu Required ISO SDU size
41+
*
42+
* @return Needed buffer size to match the requested ISO SDU MTU.
43+
*/
44+
#define BT_ISO_SDU_BUF_SIZE(mtu) BT_BUF_ISO_SIZE(mtu)
3645

3746
/** Value to set the ISO data path over HCi. */
3847
#define BT_ISO_DATA_PATH_HCI 0x00

samples/bluetooth/central_iso/src/main.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
#include <bluetooth/iso.h>
1919
#include <sys/byteorder.h>
2020

21-
#define MAX_ISO_APP_DATA (CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE)
22-
2321
static void start_scan(void);
2422

2523
static struct bt_conn *default_conn;
2624
static struct k_work_delayable iso_send_work;
2725
static struct bt_iso_chan iso_chan;
28-
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
26+
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
27+
NULL);
2928

3029
/**
3130
* @brief Send ISO data on timeout
@@ -43,7 +42,7 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
4342
static void iso_timer_timeout(struct k_work *work)
4443
{
4544
int ret;
46-
static uint8_t buf_data[MAX_ISO_APP_DATA];
45+
static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU];
4746
static bool data_initialized;
4847
struct net_buf *buf;
4948
static size_t len_to_send = 1;

samples/bluetooth/iso_broadcast/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ CONFIG_BT_DEVICE_NAME="Test ISO Broadcaster"
66
# Temporary, enable the following to meet BT_ISO dependencies
77
CONFIG_BT_OBSERVER=y
88
CONFIG_BT_BROADCASTER=y
9+
# Just needs to send a uint32_t value
10+
CONFIG_BT_ISO_TX_MTU=23

samples/bluetooth/iso_broadcast/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#define BIG_TERMINATE_TIMEOUT 60 /* seconds */
1212

1313
#define BIS_ISO_CHAN_COUNT 1
14-
#define ISO_MTU (BT_ISO_CHAN_SEND_RESERVE + sizeof(uint32_t))
15-
NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT, ISO_MTU, NULL);
14+
NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT,
15+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
1616

1717
static K_SEM_DEFINE(sem_big_cmplt, 0, 1);
1818
static K_SEM_DEFINE(sem_big_term, 0, 1);
@@ -36,7 +36,7 @@ static struct bt_iso_chan_ops iso_ops = {
3636
};
3737

3838
static struct bt_iso_chan_io_qos iso_tx_qos = {
39-
.sdu = 502, /* bytes */
39+
.sdu = sizeof(uint32_t), /* bytes */
4040
.rtn = 2,
4141
.phy = BT_GAP_LE_PHY_2M,
4242
};

samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ LOG_MODULE_REGISTER(iso_broadcast_broadcaster, LOG_LEVEL_DBG);
2424
#define DEFAULT_BIS_COUNT CONFIG_BT_ISO_MAX_CHAN
2525

2626
NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
27-
DEFAULT_BIS_SDU, NULL);
27+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
2828

2929
static K_SEM_DEFINE(sem_big_complete, 0, 1);
3030
static K_SEM_DEFINE(sem_big_term, 0, 1);
3131
static struct k_work_delayable iso_send_work;
3232
static uint32_t iso_send_count;
33-
static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE];
33+
static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU];
3434
static uint8_t connected_bis;
3535

3636
static struct bt_iso_chan bis_iso_chans[CONFIG_BT_ISO_MAX_CHAN];

samples/bluetooth/iso_connected_benchmark/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ static size_t total_iso_conn_count;
5858
static uint32_t iso_send_count;
5959
static struct bt_iso_cig *cig;
6060

61-
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
62-
static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE];
61+
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
62+
NULL);
63+
static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU];
6364

6465
static K_SEM_DEFINE(sem_adv, 0, 1);
6566
static K_SEM_DEFINE(sem_iso_accept, 0, 1);

subsys/bluetooth/host/hci_raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ NET_BUF_POOL_FIXED_DEFINE(hci_acl_pool, CONFIG_BT_BUF_ACL_TX_COUNT,
4646
BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_TX_SIZE), NULL);
4747
#if defined(CONFIG_BT_ISO)
4848
NET_BUF_POOL_FIXED_DEFINE(hci_iso_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
49-
CONFIG_BT_ISO_TX_MTU, NULL);
49+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
5050
#endif /* CONFIG_BT_ISO */
5151

5252
struct bt_dev_raw bt_dev;

subsys/bluetooth/host/iso.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "common/log.h"
2626

2727
NET_BUF_POOL_FIXED_DEFINE(iso_tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
28-
CONFIG_BT_ISO_TX_MTU, NULL);
28+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
2929
NET_BUF_POOL_FIXED_DEFINE(iso_rx_pool, CONFIG_BT_ISO_RX_BUF_COUNT,
3030
CONFIG_BT_ISO_RX_MTU, NULL);
3131

@@ -35,7 +35,7 @@ static struct bt_iso_recv_info iso_info_data[CONFIG_BT_ISO_RX_BUF_COUNT];
3535

3636
#if CONFIG_BT_ISO_TX_FRAG_COUNT > 0
3737
NET_BUF_POOL_FIXED_DEFINE(iso_frag_pool, CONFIG_BT_ISO_TX_FRAG_COUNT,
38-
CONFIG_BT_ISO_TX_MTU, NULL);
38+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
3939
#endif /* CONFIG_BT_ISO_TX_FRAG_COUNT > 0 */
4040

4141
struct bt_conn iso_conns[CONFIG_BT_ISO_MAX_CHAN];
@@ -688,8 +688,7 @@ int bt_iso_chan_send(struct bt_iso_chan *chan, struct net_buf *buf)
688688
static bool valid_chan_io_qos(const struct bt_iso_chan_io_qos *io_qos,
689689
bool is_tx)
690690
{
691-
const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU) -
692-
BT_ISO_CHAN_SEND_RESERVE;
691+
const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU);
693692
const size_t max_sdu = MIN(max_mtu, BT_ISO_MAX_SDU);
694693

695694
if (io_qos->sdu > max_sdu) {

subsys/bluetooth/shell/iso.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
#include "bt.h"
2626

27-
#define DATA_MTU CONFIG_BT_ISO_TX_MTU
28-
2927
static void iso_recv(struct bt_iso_chan *chan, const struct bt_iso_recv_info *info,
3028
struct net_buf *buf)
3129
{
@@ -75,7 +73,8 @@ struct bt_iso_chan iso_chan = {
7573

7674
static struct bt_iso_cig *cig;
7775

78-
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, DATA_MTU, NULL);
76+
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
77+
NULL);
7978

8079
static int iso_accept(struct bt_conn *acl, struct bt_iso_chan **chan)
8180
{
@@ -275,7 +274,9 @@ static int cmd_connect(const struct shell *sh, size_t argc, char *argv[])
275274

276275
static int cmd_send(const struct shell *sh, size_t argc, char *argv[])
277276
{
278-
static uint8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
277+
static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU] = {
278+
[0 ... (CONFIG_BT_ISO_TX_MTU - 1)] = 0xff
279+
};
279280
int ret, len, count = 1;
280281
struct net_buf *buf;
281282

@@ -293,7 +294,7 @@ static int cmd_send(const struct shell *sh, size_t argc, char *argv[])
293294
return -ENOEXEC;
294295
}
295296

296-
len = MIN(iso_chan.qos->tx->sdu, DATA_MTU - BT_ISO_CHAN_SEND_RESERVE);
297+
len = MIN(iso_chan.qos->tx->sdu, CONFIG_BT_ISO_TX_MTU);
297298

298299
while (count--) {
299300
buf = net_buf_alloc(&tx_pool, K_FOREVER);
@@ -345,11 +346,14 @@ static struct bt_iso_chan *bis_channels[BIS_ISO_CHAN_COUNT] = { &bis_iso_chan };
345346

346347
static struct bt_iso_big *big;
347348

348-
NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT, DATA_MTU, NULL);
349+
NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT,
350+
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
349351

350352
static int cmd_broadcast(const struct shell *sh, size_t argc, char *argv[])
351353
{
352-
static uint8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
354+
static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU] = {
355+
[0 ... (CONFIG_BT_ISO_TX_MTU - 1)] = 0xff
356+
};
353357
int ret, len, count = 1;
354358
struct net_buf *buf;
355359

@@ -367,7 +371,7 @@ static int cmd_broadcast(const struct shell *sh, size_t argc, char *argv[])
367371
return -ENOEXEC;
368372
}
369373

370-
len = MIN(iso_chan.qos->tx->sdu, DATA_MTU - BT_ISO_CHAN_SEND_RESERVE);
374+
len = MIN(iso_chan.qos->tx->sdu, CONFIG_BT_ISO_TX_MTU);
371375

372376
while (count--) {
373377
for (int i = 0; i < BIS_ISO_CHAN_COUNT; i++) {

0 commit comments

Comments
 (0)