From e03c13e0e9267e5ccb8f6cb75cd130825dff0639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Narajowski?= Date: Thu, 24 Jan 2019 00:40:57 +0100 Subject: [PATCH] l2cap: Fix channel creation Previously the channel was added to the channels list after sending COC Accept event to the application. The application called ble_l2cap_coc_recv_ready which was trying to find channel on a list, but failed. With this patch the channel is added on a list before sending Accept event, and if the application returns an error, the channel is removed from the list. Moreover if we fail to send Connection Success, the application should now not receive a confirmation that we are connected. --- nimble/host/src/ble_l2cap_sig.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nimble/host/src/ble_l2cap_sig.c b/nimble/host/src/ble_l2cap_sig.c index a7507e348..07a338ae6 100644 --- a/nimble/host/src/ble_l2cap_sig.c +++ b/nimble/host/src/ble_l2cap_sig.c @@ -691,6 +691,7 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr, chan->coc_tx.credits = le16toh(req->credits); chan->coc_tx.mtu = le16toh(req->mtu); + ble_hs_conn_chan_insert(conn, chan); ble_hs_unlock(); rc = ble_l2cap_event_coc_accept(chan, le16toh(req->mtu)); @@ -700,7 +701,9 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr, /* Make sure we do not send disconnect event when removing channel */ chan->cb = NULL; - ble_l2cap_chan_free(chan); + ble_hs_lock(); + ble_hs_conn_delete_chan(conn, chan); + ble_hs_unlock(); rsp->result = htole16(coc_err); goto failed; } @@ -712,13 +715,11 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr, rsp->result = htole16(BLE_L2CAP_COC_ERR_CONNECTION_SUCCESS); rc = ble_l2cap_sig_tx(conn_handle, txom); - if (rc == 0) { - /* Response sent out with a success. We are connected now*/ + if (rc != 0) { ble_hs_lock(); - ble_hs_conn_chan_insert(conn, chan); + ble_hs_conn_delete_chan(conn, chan); ble_hs_unlock(); - } else { - ble_l2cap_chan_free(chan); + return 0; } /* Notify user about connection status */