bluez5-util: fix profile waiting logic
authorTanu Kaskinen <tanuk@iki.fi>
Thu, 16 Mar 2017 21:48:43 +0000 (23:48 +0200)
committerTanu Kaskinen <tanuk@iki.fi>
Tue, 28 Mar 2017 18:56:05 +0000 (21:56 +0300)
commitcb78d6f57c00f3694dae9110f7a7d6f80e3344ac
tree78a152c4454767aba8c65b0081855414d9cf88f0
parentfa44127ebeb2afde467f5a267809f24f281262c6
bluez5-util: fix profile waiting logic

There were two bugs in the old logic. The first one:

If a device has two profiles, the old code would start the wait timer
when the first profile connects, but when the second profile connects,
the timer would not get stopped and the CONNECTION_CHANGED hook would
not get fired, because the code for that was inside an if block that
only gets executed when the first profile connects. As a result,
module-bluez5-device loading would always be delayed until the wait
timeout expires.

The second bug:

A crash was observed in device_start_waiting_for_profiles(). That
function is called whenever the connected profile count changes from 0
to 1. The function also has an assertion that checks that the timer is
not running when the function is called. That assertion crashed in the
following scenario with a headset that supports HSP and A2DP:

1. First HSP gets connected. The timer is started.

2. Then HSP gets disconnected for some reason. The timer is still
running.

3. Then A2DP gets connected. device_start_waiting_for_profiles() is
called, because the connected profile count changed from 0 to 1 again.
The timer is already running, so the assertion fails.

First I thought I'd remove the assertion from
device_start_waiting_for_profiles() and just restart the timer on the
second call, but then I figured that when the device returns to the
"everything disconnected" state in step 2, it would be better to stop
the timer. The purpose of the timer is to delay the notification of the
device becoming connected, but if the device becomes disconnected during
the waiting period, the notification doesn't make sense any more, and
therefore the timer doesn't make sense either.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=100237
src/modules/bluetooth/bluez5-util.c