From 991fea48d19da06019eb89a75a62133401c9e8e3 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 4 Oct 2016 16:32:28 +0200 Subject: [PATCH] l2cap: close l2cap le connection on hci disconnect --- src/l2cap.c | 10 ++++++++++ test/pts/le_data_channel.c | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/l2cap.c b/src/l2cap.c index a3562e840..eb72038a3 100644 --- a/src/l2cap.c +++ b/src/l2cap.c @@ -1058,6 +1058,16 @@ static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t * btstack_linked_list_iterator_remove(&it); btstack_memory_l2cap_channel_free(channel); } +#ifdef ENABLE_BLE + btstack_linked_list_iterator_init(&it, &l2cap_le_channels); + while (btstack_linked_list_iterator_has_next(&it)){ + l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); + if (channel->con_handle != handle) continue; + l2cap_emit_channel_closed(channel); + btstack_linked_list_iterator_remove(&it); + btstack_memory_l2cap_channel_free(channel); + } +#endif break; // Notify channel packet handler if they can send now diff --git a/test/pts/le_data_channel.c b/test/pts/le_data_channel.c index 835e33048..f9a4b68b1 100644 --- a/test/pts/le_data_channel.c +++ b/test/pts/le_data_channel.c @@ -105,7 +105,7 @@ static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t * switch (hci_event_le_meta_get_subevent_code(packet)) { case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: handle = little_endian_read_16(packet, 4); - printf("Connection handle 0x%04x\n", handle); + printf("HCI: LE Connection Complete, connection handle 0x%04x\n", handle); break; default: @@ -115,15 +115,15 @@ static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t * case HCI_EVENT_DISCONNECTION_COMPLETE: break; - + case L2CAP_EVENT_INCOMING_CONNECTION: { - uint16_t l2cap_cid = little_endian_read_16(packet, 12); + local_cid = little_endian_read_16(packet, 12); #if 1 - printf("L2CAP Accepting incoming connection request\n"); - l2cap_le_accept_connection(l2cap_cid, receive_buffer_X, sizeof(receive_buffer_X), 1); + printf("L2CAP: Accepting incoming connection request for 0x%02x\n", local_cid); + l2cap_le_accept_connection(local_cid, receive_buffer_X, sizeof(receive_buffer_X), 1); #else printf("L2CAP Decline incoming connection request\n"); - l2cap_le_decline_connection(l2cap_cid); + l2cap_le_decline_connection(local_cid); #endif break; } @@ -135,12 +135,16 @@ static void app_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t * local_cid = little_endian_read_16(packet, 13); handle = little_endian_read_16(packet, 9); if (packet[2] == 0) { - printf("Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", + printf("L2CAP: Channel successfully opened: %s, handle 0x%02x, psm 0x%02x, local cid 0x%02x, remote cid 0x%02x\n", bd_addr_to_str(event_address), handle, psm, local_cid, little_endian_read_16(packet, 15)); } else { - printf("L2CAP connection to device %s failed. status code %u\n", bd_addr_to_str(event_address), packet[2]); + printf("L2CAP: connection to device %s failed. status code %u\n", bd_addr_to_str(event_address), packet[2]); } break; + case L2CAP_EVENT_CHANNEL_CLOSED: + local_cid = l2cap_event_channel_closed_get_local_cid(packet); + printf("L2CAP: Channel closed 0x%02x\n", local_cid); + break; case SM_EVENT_JUST_WORKS_REQUEST: printf("SM_EVENT_JUST_WORKS_REQUEST\n");