diff --git a/src/ble/att_dispatch.c b/src/ble/att_dispatch.c index a5091b23c..e412a9b57 100644 --- a/src/ble/att_dispatch.c +++ b/src/ble/att_dispatch.c @@ -143,15 +143,16 @@ static void att_dispatch_handle_att_pdu(uint8_t packet_type, uint16_t channel, u } static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ - uint8_t index; #ifdef ENABLE_GATT_OVER_CLASSIC hci_connection_t * hci_connection; hci_con_handle_t con_handle; bool outgoing_active; + uint8_t index; #endif -#ifdef ENABLE_GATT_OVER_EATT +#if defined(ENABLE_GATT_OVER_CLASSIC) || defined(ENABLE_GATT_OVER_EATT) bd_addr_t address; uint16_t l2cap_cid; + uint8_t status; #endif switch (packet_type){ case ATT_DATA_PACKET: @@ -174,10 +175,8 @@ static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p // reject if outgoing l2cap connection active, L2CAP/TIM/BV-01-C con_handle = l2cap_event_incoming_connection_get_handle(packet); hci_connection = hci_connection_for_handle(con_handle); - outgoing_active = false; - if (hci_connection != NULL){ - outgoing_active = hci_connection->att_server.l2cap_cid != 0; - } + btstack_assert(hci_connection != NULL); + outgoing_active = hci_connection->att_server.l2cap_cid != 0; if (outgoing_active) { hci_connection->att_server.incoming_connection_request = true; l2cap_decline_connection(l2cap_cid); @@ -188,6 +187,18 @@ static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p } break; case L2CAP_EVENT_CHANNEL_OPENED: + // store l2cap_cid in att_server + status = l2cap_event_channel_opened_get_status(packet); + con_handle = l2cap_event_channel_opened_get_handle(packet); + hci_connection = hci_connection_for_handle(con_handle); + if (status == ERROR_CODE_SUCCESS){ + btstack_assert(hci_connection != NULL); + hci_connection->att_server.l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); + } else { + if (hci_connection != NULL){ + hci_connection->att_server.l2cap_cid = 0; + } + } // dispatch to all roles for (index = 0; index < ATT_MAX; index++){ if (subscriptions[index].packet_handler != NULL){ @@ -196,6 +207,10 @@ static void att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *p } break; case L2CAP_EVENT_CHANNEL_CLOSED: + // clear l2cap cid in hci_connection->att_server + con_handle = l2cap_event_incoming_connection_get_handle(packet); + hci_connection = hci_connection_for_handle(con_handle); + hci_connection->att_server.l2cap_cid = 0; // dispatch to all roles for (index = 0; index < ATT_MAX; index++){ if (subscriptions[index].packet_handler != NULL){ diff --git a/src/ble/att_server.c b/src/ble/att_server.c index 28b7a93f9..e45eeccb6 100644 --- a/src/ble/att_server.c +++ b/src/ble/att_server.c @@ -924,7 +924,6 @@ static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t cha l2cap_event_channel_opened_get_address(packet, att_server->peer_address); att_connection = &hci_connection->att_connection; att_connection->con_handle = con_handle; - att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); // reset connection properties att_server->state = ATT_SERVER_IDLE; att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); diff --git a/src/ble/gatt_client.c b/src/ble/gatt_client.c index 75608299a..dedf20a36 100644 --- a/src/ble/gatt_client.c +++ b/src/ble/gatt_client.c @@ -2142,9 +2142,6 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, break; case L2CAP_EVENT_CHANNEL_CLOSED: gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet)); - // clear l2cap cid in hci_connection->att_server - hci_connection = hci_connection_for_handle(gatt_client->con_handle); - hci_connection->att_server.l2cap_cid = 0; // discard gatt client object gatt_client_classic_handle_disconnected(gatt_client); break; @@ -2901,11 +2898,7 @@ static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, ui memcpy(addr, gatt_client->addr, 6); hci_con_handle_t con_handle = gatt_client->con_handle; btstack_packet_handler_t callback = gatt_client->callback; - if (status == ERROR_CODE_SUCCESS){ - // store l2cap cid in hci_connection->att_server - hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); - hci_connection->att_server.l2cap_cid = gatt_client->l2cap_cid; - } else { + if (status != ERROR_CODE_SUCCESS){ btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client); btstack_memory_gatt_client_free(gatt_client); }