bluekitchen / btstack

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

hci_connection_timeout_handler never triggered #572

Closed amorniroli closed 8 months ago

amorniroli commented 9 months ago

Describe the bug

Not sure if it's a bug.

I've noticed that when HCI connection is created (function hci_connection_init), timer handler hci_connection_timeout_handler is set, but it's never handled/triggered since timer it's not added to btstack timer (using btstack_run_loop_add_timer).

image

Is this correct?

mringwal commented 8 months ago

Hi Alessandro. The hci_connection_init just initialized the hci connection timer. It is set for the first time on HCI_EVENT_CONNECTION_COMPLETE_EVENT here: https://github.com/bluekitchen/btstack/blob/272986f17af35a67815ab20897f6c91e710322a4/src/hci.c#L3733

Bluetooth itself does not define any kind of timeout on the HCI level (there are timeouts on higher level, e.g. during L2CAP setup). BTstack disconnects an idle HCI Connection == a connection without any L2CAP Channels after 10 seconds.

amorniroli commented 8 months ago

Hi Alessandro. The hci_connection_init just initialized the hci connection timer. It is set for the first time on HCI_EVENT_CONNECTION_COMPLETE_EVENT here:

https://github.com/bluekitchen/btstack/blob/272986f17af35a67815ab20897f6c91e710322a4/src/hci.c#L3733

Bluetooth itself does not define any kind of timeout on the HCI level (there are timeouts on higher level, e.g. during L2CAP setup). BTstack disconnects an idle HCI Connection == a connection without any L2CAP Channels after 10 seconds.

Hi @mringwal ,

thanks for the description.

In my scenario, I try to connect to bonded devices, in the same order they were bonded. E.g.:

  1. connect phone 1
  2. if timeout -> connect phone 2
  3. and so on.

The connection complete timeout event received by controller is too big (in my case, ~15sec). That's a lot of time for my scenario. So I was thinking to handle timeout by my self at application level.

I'm trying to do that for a2dp. Same steps described above: the only difference is that now I have only 1 phone bonded. So:

  1. try to establish a2dp_sink_establish_stream on phone;
  2. if timeout (5s), call a2dp_sink_disconnect
  3. retry to establish a2dp_sink_establish_stream on the same phone

Step 3 is failing because avdtp_connect returns "command disallowed".

image

Connection state remains to 'AVDTP_SIGNALING_CONNECTION_W4_L2CAP_DISCONNECTED'

Am I doing something wrong?

mringwal commented 8 months ago

Am I doing something wrong?

Yes :)

The hci connection timeout is only used to discard the connection if it's not used. It's not related to the connection process.

To get shorter timeout for the connection attempt - called paging - , please call gap_set_page_timeout( timeout_in_625ns). The default is 0x6000 => 15 seconds

amorniroli commented 8 months ago

Am I doing something wrong?

Yes :)

The hci connection timeout is only used to discard the connection if it's not used. It's not related to the connection process.

To get shorter timeout for the connection attempt - called paging - , please call gap_set_page_timeout( timeout_in_625ns). The default is 0x6000 => 15 seconds

Nice! I've missed that function. Thank you very much, now it's working as I was expecting : ) thank you very much