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 <emil.gydesen@nordicsemi.no>
diff --git a/include/bluetooth/buf.h b/include/bluetooth/buf.h
index f0adfef..9f0910a 100644
--- a/include/bluetooth/buf.h
+++ b/include/bluetooth/buf.h
@@ -68,6 +68,11 @@
 /** 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))
 
+/** Helper to calculate needed buffer size for HCI ISO packets. */
+#define BT_BUF_ISO_SIZE(size) BT_BUF_SIZE(BT_HCI_ISO_HDR_SIZE + \
+					  BT_HCI_ISO_DATA_HDR_SIZE + \
+					  (size))
+
 /** Data size needed for HCI ACL RX buffers */
 #define BT_BUF_ACL_RX_SIZE BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_RX_SIZE)
 
diff --git a/include/bluetooth/iso.h b/include/bluetooth/iso.h
index 088f343..2363638 100644
--- a/include/bluetooth/iso.h
+++ b/include/bluetooth/iso.h
@@ -28,11 +28,20 @@
 #include <bluetooth/hci.h>
 
 /** @def BT_ISO_CHAN_SEND_RESERVE
- *  @brief Headroom needed for outgoing buffers
+ *  @brief Headroom needed for outgoing ISO SDUs
  */
-#define BT_ISO_CHAN_SEND_RESERVE (CONFIG_BT_HCI_RESERVE + \
-				  BT_HCI_ISO_HDR_SIZE + \
-				  BT_HCI_ISO_DATA_HDR_SIZE)
+#define BT_ISO_CHAN_SEND_RESERVE BT_BUF_ISO_SIZE(0)
+
+/** @def BT_ISO_SDU_BUF_SIZE
+ *
+ *  @brief Helper to calculate needed buffer size for ISO SDUs.
+ *         Useful for creating buffer pools.
+ *
+ *  @param mtu Required ISO SDU size
+ *
+ *  @return Needed buffer size to match the requested ISO SDU MTU.
+ */
+#define BT_ISO_SDU_BUF_SIZE(mtu) BT_BUF_ISO_SIZE(mtu)
 
 /** Value to set the ISO data path over HCi. */
 #define BT_ISO_DATA_PATH_HCI        0x00
diff --git a/samples/bluetooth/central_iso/src/main.c b/samples/bluetooth/central_iso/src/main.c
index f490ffb..ad72555 100644
--- a/samples/bluetooth/central_iso/src/main.c
+++ b/samples/bluetooth/central_iso/src/main.c
@@ -18,14 +18,13 @@
 #include <bluetooth/iso.h>
 #include <sys/byteorder.h>
 
-#define MAX_ISO_APP_DATA (CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE)
-
 static void start_scan(void);
 
 static struct bt_conn *default_conn;
 static struct k_work_delayable iso_send_work;
 static struct bt_iso_chan iso_chan;
-NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
+NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
+			  NULL);
 
 /**
  * @brief Send ISO data on timeout
@@ -43,7 +42,7 @@
 static void iso_timer_timeout(struct k_work *work)
 {
 	int ret;
-	static uint8_t buf_data[MAX_ISO_APP_DATA];
+	static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU];
 	static bool data_initialized;
 	struct net_buf *buf;
 	static size_t len_to_send = 1;
diff --git a/samples/bluetooth/iso_broadcast/prj.conf b/samples/bluetooth/iso_broadcast/prj.conf
index d4d7b65..3617e80 100644
--- a/samples/bluetooth/iso_broadcast/prj.conf
+++ b/samples/bluetooth/iso_broadcast/prj.conf
@@ -6,3 +6,5 @@
 # Temporary, enable the following to meet BT_ISO dependencies
 CONFIG_BT_OBSERVER=y
 CONFIG_BT_BROADCASTER=y
+# Just needs to send a uint32_t value
+CONFIG_BT_ISO_TX_MTU=23
diff --git a/samples/bluetooth/iso_broadcast/src/main.c b/samples/bluetooth/iso_broadcast/src/main.c
index 9035f46..7343cc8 100644
--- a/samples/bluetooth/iso_broadcast/src/main.c
+++ b/samples/bluetooth/iso_broadcast/src/main.c
@@ -11,8 +11,8 @@
 #define BIG_TERMINATE_TIMEOUT 60 /* seconds */
 
 #define BIS_ISO_CHAN_COUNT 1
-#define ISO_MTU (BT_ISO_CHAN_SEND_RESERVE + sizeof(uint32_t))
-NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT, ISO_MTU, NULL);
+NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT,
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 
 static K_SEM_DEFINE(sem_big_cmplt, 0, 1);
 static K_SEM_DEFINE(sem_big_term, 0, 1);
@@ -36,7 +36,7 @@
 };
 
 static struct bt_iso_chan_io_qos iso_tx_qos = {
-	.sdu = 502, /* bytes */
+	.sdu = sizeof(uint32_t), /* bytes */
 	.rtn = 2,
 	.phy = BT_GAP_LE_PHY_2M,
 };
diff --git a/samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c b/samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c
index 4921d00..795c371 100644
--- a/samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c
+++ b/samples/bluetooth/iso_broadcast_benchmark/src/broadcaster.c
@@ -24,13 +24,13 @@
 #define DEFAULT_BIS_COUNT       CONFIG_BT_ISO_MAX_CHAN
 
 NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
-			  DEFAULT_BIS_SDU, NULL);
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 
 static K_SEM_DEFINE(sem_big_complete, 0, 1);
 static K_SEM_DEFINE(sem_big_term, 0, 1);
 static struct k_work_delayable iso_send_work;
 static uint32_t iso_send_count;
-static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE];
+static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU];
 static uint8_t connected_bis;
 
 static struct bt_iso_chan bis_iso_chans[CONFIG_BT_ISO_MAX_CHAN];
diff --git a/samples/bluetooth/iso_connected_benchmark/src/main.c b/samples/bluetooth/iso_connected_benchmark/src/main.c
index 7f2d7c3..4d49060 100644
--- a/samples/bluetooth/iso_connected_benchmark/src/main.c
+++ b/samples/bluetooth/iso_connected_benchmark/src/main.c
@@ -58,8 +58,9 @@
 static uint32_t iso_send_count;
 static struct bt_iso_cig *cig;
 
-NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
-static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE];
+NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
+			  NULL);
+static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU];
 
 static K_SEM_DEFINE(sem_adv, 0, 1);
 static K_SEM_DEFINE(sem_iso_accept, 0, 1);
diff --git a/subsys/bluetooth/host/hci_raw.c b/subsys/bluetooth/host/hci_raw.c
index 503c49e..46aa04d 100644
--- a/subsys/bluetooth/host/hci_raw.c
+++ b/subsys/bluetooth/host/hci_raw.c
@@ -46,7 +46,7 @@
 			  BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_TX_SIZE), NULL);
 #if defined(CONFIG_BT_ISO)
 NET_BUF_POOL_FIXED_DEFINE(hci_iso_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
-			  CONFIG_BT_ISO_TX_MTU, NULL);
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 #endif /* CONFIG_BT_ISO */
 
 struct bt_dev_raw bt_dev;
diff --git a/subsys/bluetooth/host/iso.c b/subsys/bluetooth/host/iso.c
index 9a98102..9d3685b 100644
--- a/subsys/bluetooth/host/iso.c
+++ b/subsys/bluetooth/host/iso.c
@@ -25,7 +25,7 @@
 #include "common/log.h"
 
 NET_BUF_POOL_FIXED_DEFINE(iso_tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
-			  CONFIG_BT_ISO_TX_MTU, NULL);
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 NET_BUF_POOL_FIXED_DEFINE(iso_rx_pool, CONFIG_BT_ISO_RX_BUF_COUNT,
 			  CONFIG_BT_ISO_RX_MTU, NULL);
 
@@ -35,7 +35,7 @@
 
 #if CONFIG_BT_ISO_TX_FRAG_COUNT > 0
 NET_BUF_POOL_FIXED_DEFINE(iso_frag_pool, CONFIG_BT_ISO_TX_FRAG_COUNT,
-			  CONFIG_BT_ISO_TX_MTU, NULL);
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 #endif /* CONFIG_BT_ISO_TX_FRAG_COUNT > 0 */
 
 struct bt_conn iso_conns[CONFIG_BT_ISO_MAX_CHAN];
@@ -688,8 +688,7 @@
 static bool valid_chan_io_qos(const struct bt_iso_chan_io_qos *io_qos,
 			      bool is_tx)
 {
-	const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU) -
-					BT_ISO_CHAN_SEND_RESERVE;
+	const size_t max_mtu = (is_tx ? CONFIG_BT_ISO_TX_MTU : CONFIG_BT_ISO_RX_MTU);
 	const size_t max_sdu = MIN(max_mtu, BT_ISO_MAX_SDU);
 
 	if (io_qos->sdu > max_sdu) {
diff --git a/subsys/bluetooth/shell/iso.c b/subsys/bluetooth/shell/iso.c
index 84457f6..c5a2ae1 100644
--- a/subsys/bluetooth/shell/iso.c
+++ b/subsys/bluetooth/shell/iso.c
@@ -24,8 +24,6 @@
 
 #include "bt.h"
 
-#define DATA_MTU CONFIG_BT_ISO_TX_MTU
-
 static void iso_recv(struct bt_iso_chan *chan, const struct bt_iso_recv_info *info,
 		struct net_buf *buf)
 {
@@ -75,7 +73,8 @@
 
 static struct bt_iso_cig *cig;
 
-NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, DATA_MTU, NULL);
+NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
+			  NULL);
 
 static int iso_accept(struct bt_conn *acl, struct bt_iso_chan **chan)
 {
@@ -275,7 +274,9 @@
 
 static int cmd_send(const struct shell *sh, size_t argc, char *argv[])
 {
-	static uint8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
+	static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU] = {
+		[0 ... (CONFIG_BT_ISO_TX_MTU - 1)] = 0xff
+	};
 	int ret, len, count = 1;
 	struct net_buf *buf;
 
@@ -293,7 +294,7 @@
 		return -ENOEXEC;
 	}
 
-	len = MIN(iso_chan.qos->tx->sdu, DATA_MTU - BT_ISO_CHAN_SEND_RESERVE);
+	len = MIN(iso_chan.qos->tx->sdu, CONFIG_BT_ISO_TX_MTU);
 
 	while (count--) {
 		buf = net_buf_alloc(&tx_pool, K_FOREVER);
@@ -345,11 +346,14 @@
 
 static struct bt_iso_big *big;
 
-NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT, DATA_MTU, NULL);
+NET_BUF_POOL_FIXED_DEFINE(bis_tx_pool, BIS_ISO_CHAN_COUNT,
+			  BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), NULL);
 
 static int cmd_broadcast(const struct shell *sh, size_t argc, char *argv[])
 {
-	static uint8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
+	static uint8_t buf_data[CONFIG_BT_ISO_TX_MTU] = {
+		[0 ... (CONFIG_BT_ISO_TX_MTU - 1)] = 0xff
+	};
 	int ret, len, count = 1;
 	struct net_buf *buf;
 
@@ -367,7 +371,7 @@
 		return -ENOEXEC;
 	}
 
-	len = MIN(iso_chan.qos->tx->sdu, DATA_MTU - BT_ISO_CHAN_SEND_RESERVE);
+	len = MIN(iso_chan.qos->tx->sdu, CONFIG_BT_ISO_TX_MTU);
 
 	while (count--) {
 		for (int i = 0; i < BIS_ISO_CHAN_COUNT; i++) {