Delay CLOSING->IDLE until remote acknowledges L2CAP channel closure. It is not explicitly stated in AVDTP v1.3 Sec. 6.13, but some devices refuse commands sent immediately after L2CAP Disconnect Req, so wait until Rsp. Fails: > ACL Data RX: Handle 6 flags 0x02 dlen 6 Channel: 64 len 2 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Close (0x08) Response Accept (0x02) type 0x00 label 0 nosp 0 < ACL Data TX: Handle 6 flags 0x00 dlen 12 L2CAP: Disconnection Request (0x06) ident 16 len 4 Destination CID: 65 Source CID: 65 < ACL Data TX: Handle 6 flags 0x00 dlen 22 Channel: 64 len 18 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Set Configuration (0x03) Command (0x00) type 0x00 label 1 nosp 0 ACP SEID: 7 INT SEID: 1 Service Category: Media Transport (0x01) Service Category: Media Codec (0x07) Media Type: Audio (0x00) Media Codec: MPEG-2,4 AAC (0x02) Object Type: MPEG-4 AAC LC (0x40) Frequency: 44100 (0x100) Channels: 2 (0x04) Bitrate: 220000bps VBR: No Service Category: Delay Reporting (0x08) > ACL Data RX: Handle 6 flags 0x02 dlen 12 L2CAP: Disconnection Response (0x07) ident 16 len 4 Destination CID: 65 Source CID: 65 > ACL Data RX: Handle 6 flags 0x02 dlen 8 Channel: 64 len 4 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Set Configuration (0x03) Response Reject (0x03) type 0x00 label 1 nosp 0 Service Category: Reserved (0x29) Error code: UNSUPPORTED_CONFIGURATION (0x29) Works: > ACL Data RX: Handle 4 flags 0x02 dlen 6 Channel: 64 len 2 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Close (0x08) Response Accept (0x02) type 0x00 label 12 nosp 0 < ACL Data TX: Handle 4 flags 0x00 dlen 12 L2CAP: Disconnection Request (0x06) ident 16 len 4 Destination CID: 65 Source CID: 65 > ACL Data RX: Handle 4 flags 0x02 dlen 12 L2CAP: Disconnection Response (0x07) ident 16 len 4 Destination CID: 65 Source CID: 65 < ACL Data TX: Handle 4 flags 0x00 dlen 22 Channel: 64 len 18 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Set Configuration (0x03) Command (0x00) type 0x00 label 13 nosp 0 ACP SEID: 9 INT SEID: 2 Service Category: Media Transport (0x01) Service Category: Media Codec (0x07) Media Type: Audio (0x00) Media Codec: MPEG-2,4 AAC (0x02) Object Type: MPEG-4 AAC LC (0x40) Frequency: 44100 (0x100) Channels: 2 (0x04) Bitrate: 220000bps VBR: No Service Category: Delay Reporting (0x08) > ACL Data RX: Handle 4 flags 0x02 dlen 6 Channel: 64 len 2 [PSM 25 mode Basic (0x00)] {chan 0} AVDTP: Set Configuration (0x03) Response Accept (0x02) type 0x00 label 13 nosp 0 Fixes: https://github.com/bluez/bluez/issues/1471 Fixes: aa118e965b ("a2dp: Don't wait to reconfigure") --- profiles/audio/avdtp.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/profiles/audio/avdtp.c b/profiles/audio/avdtp.c index 30648251f..3613dff2d 100644 --- a/profiles/audio/avdtp.c +++ b/profiles/audio/avdtp.c @@ -79,6 +79,7 @@ #define ABORT_TIMEOUT 2 #define DISCONNECT_TIMEOUT 1 #define START_TIMEOUT 1 +#define TRANSPORT_L2CAP_CLOSE_TIMEOUT 2 #if __BYTE_ORDER == __LITTLE_ENDIAN @@ -752,6 +753,8 @@ static void transport_cb(int cond, void *data) struct avdtp_stream *stream = data; struct avdtp_local_sep *sep = stream->lsep; + DBG(""); + if (stream->close_int && sep->cfm && sep->cfm->close) sep->cfm->close(stream->session, sep, stream, NULL, sep->user_data); @@ -765,6 +768,26 @@ static void transport_cb(int cond, void *data) avdtp_stream_set_state(stream, AVDTP_STATE_IDLE); } +static void close_stream_linger_finish(void *data) +{ + DBG(""); + + transport_cb(G_IO_HUP, data); +} + +static void close_stream_linger(struct avdtp_stream *stream) +{ + /* Close and wait for L2CAP Disconnection Rsp via socket linger */ + if (stream->io_id) + g_source_remove(stream->io_id); + + stream->io_id = io_glib_shutdown_linger(stream->io, SHUT_RDWR, + TRANSPORT_L2CAP_CLOSE_TIMEOUT, + close_stream_linger_finish, stream); + if (!stream->io_id) + transport_cb(G_IO_HUP, stream); +} + static int get_send_buffer_size(int sk) { int size; @@ -2922,7 +2945,13 @@ static gboolean avdtp_close_resp(struct avdtp *session, { avdtp_stream_set_state(stream, AVDTP_STATE_CLOSING); - close_stream(stream); + /* Delay CLOSING->IDLE until remote acknowledges L2CAP channel closure. + * + * It is not explicitly stated in AVDTP v1.3 Sec. 6.13, but some devices + * refuse commands sent immediately after L2CAP Disconnect Req, so wait + * until Rsp. + */ + close_stream_linger(stream); return TRUE; } -- 2.51.0