Skip to content

Commit 0963a8c

Browse files
MarkWangChinesehenrikbrixandersen
authored andcommitted
bluetooth: a2dp: add bt_a2dp_stream_create_pdu
use bt_a2dp_stream_create_pdu to create the stream pdu net buf, then application can use the buf->len to check whether the buf's size exceeds the l2cap mtu. Signed-off-by: Mark Wang <yichang.wang@nxp.com>
1 parent b240560 commit 0963a8c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

include/zephyr/bluetooth/classic/a2dp.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
extern "C" {
2222
#endif
2323

24-
#define BT_A2DP_STREAM_BUF_RESERVE (12U + BT_L2CAP_BUF_SIZE(0))
25-
2624
/** SBC IE length */
2725
#define BT_A2DP_SBC_IE_LENGTH (4U)
2826
/** MPEG1,2 IE length */
@@ -855,6 +853,21 @@ uint32_t bt_a2dp_get_mtu(struct bt_a2dp_stream *stream);
855853
int bt_a2dp_stream_send(struct bt_a2dp_stream *stream, struct net_buf *buf, uint16_t seq_num,
856854
uint32_t ts);
857855

856+
/**
857+
* @brief Allocate a net_buf for bt_a2dp_stream_send
858+
*
859+
* This function allocates a buffer from the specified pool, reserves
860+
* sufficient headroom for protocol headers required by L2CAP over Bluetooth, fills
861+
* the AVDTP header.
862+
*
863+
* @param pool The buffer pool to allocate from.
864+
* @param timeout Non-negative waiting period to obtain a buffer or one of
865+
* the special values K_NO_WAIT and K_FOREVER.
866+
*
867+
* @return A newly allocated net_buf.
868+
*/
869+
struct net_buf *bt_a2dp_stream_create_pdu(struct net_buf_pool *pool, k_timeout_t timeout);
870+
858871
#ifdef __cplusplus
859872
}
860873
#endif

subsys/bluetooth/host/classic/a2dp.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,26 @@ uint32_t bt_a2dp_get_mtu(struct bt_a2dp_stream *stream)
973973
}
974974

975975
#if defined(CONFIG_BT_A2DP_SOURCE)
976+
struct net_buf *bt_a2dp_stream_create_pdu(struct net_buf_pool *pool, k_timeout_t timeout)
977+
{
978+
struct net_buf *buf;
979+
980+
buf = bt_l2cap_create_pdu_timeout(pool, 0, timeout);
981+
if (buf == NULL) {
982+
return NULL;
983+
}
984+
985+
/* add AVDTP header */
986+
if (net_buf_tailroom(buf) < sizeof(struct bt_avdtp_media_hdr)) {
987+
net_buf_unref(buf);
988+
return NULL;
989+
}
990+
991+
net_buf_add(buf, sizeof(struct bt_avdtp_media_hdr));
992+
993+
return buf;
994+
}
995+
976996
int bt_a2dp_stream_send(struct bt_a2dp_stream *stream, struct net_buf *buf, uint16_t seq_num,
977997
uint32_t ts)
978998
{
@@ -982,7 +1002,7 @@ int bt_a2dp_stream_send(struct bt_a2dp_stream *stream, struct net_buf *buf, uint
9821002
return -EINVAL;
9831003
}
9841004

985-
media_hdr = net_buf_push(buf, sizeof(struct bt_avdtp_media_hdr));
1005+
media_hdr = (struct bt_avdtp_media_hdr *)buf->data;
9861006
memset(media_hdr, 0, sizeof(struct bt_avdtp_media_hdr));
9871007

9881008
if (stream->local_ep->codec_type == BT_A2DP_SBC) {

subsys/bluetooth/host/classic/shell/a2dp.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,11 @@ static int cmd_send_media(const struct shell *sh, int32_t argc, char *argv[])
779779
return -ENOEXEC;
780780
}
781781

782-
buf = net_buf_alloc(&a2dp_tx_pool, K_FOREVER);
783-
net_buf_reserve(buf, BT_A2DP_STREAM_BUF_RESERVE);
782+
buf = bt_a2dp_stream_create_pdu(&a2dp_tx_pool, K_FOREVER);
783+
if (buf == NULL) {
784+
shell_error(sh, "fail to allocate buffer");
785+
return -ENOEXEC;
786+
}
784787

785788
/* num of frames is 1 */
786789
net_buf_add_u8(buf, (uint8_t)BT_A2DP_SBC_MEDIA_HDR_ENCODE(1, 0, 0, 0));

0 commit comments

Comments
 (0)