bluekitchen / btstack

Dual-mode Bluetooth stack, with small memory footprint.
http://bluekitchen-gmbh.com
Other
1.74k stars 618 forks source link

Pairing request is not answered #584

Closed felixdoerre closed 8 months ago

felixdoerre commented 8 months ago

Describe the bug

In a certain situation, when receiving a pairing request immediately after connecting, the SM (in the role of ble periheral) does not respond with a pairing response, leaving the connection hanging and running into a timeout from ble central.

To Reproduce

Connect to a btstack peripheral (e.g. with android).

Expected behavior

After the pairing request is received, a pairing response is sent.

Debugging Trace On my system, when the pairing request is received, irk_lookup is still running, causing this branch to be taken: https://github.com/bluekitchen/btstack/blob/367b47cf6e9c34814b4b65bd34cd20513415ea78/src/ble/sm.c#L4495 This leaves sm_conn->sm_engine_state in SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED_W4_IRK.

At some point later irk_lookup finishes and fails (as expected,because this is a connection from a new device) This event is handled here: https://github.com/bluekitchen/btstack/blob/367b47cf6e9c34814b4b65bd34cd20513415ea78/src/ble/sm.c#L1396

However, while there is code to update SM_RESPONDER_PH0_RECEIVED_LTK_W4_IRK, the update from SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED_W4_IRK to SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED seems to be missing.

I would expect to see a state update here as well, like for the ADDRESS_RESOLUTION_SUCCEEDED-case: https://github.com/bluekitchen/btstack/blob/367b47cf6e9c34814b4b65bd34cd20513415ea78/src/ble/sm.c#L1331

Is this state update missing here (e.g. before line 1400)?

                        if (sm_connection->sm_engine_state == SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED_W4_IRK){
                            sm_connection->sm_engine_state = SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED;
                        }

Or should this case be handled differently?

mringwal commented 8 months ago

Hi @felixdoerre. Thanks for reporting. You're totally right, the code you've suggested is missing. It's the same code block in the successful lookup a few lines earlier: https://github.com/bluekitchen/btstack/blob/367b47cf6e9c34814b4b65bd34cd20513415ea78/src/ble/sm.c#L1331 I've added the lines in commit 434260a. Please let me know if it doesn't work.