Merge lp:~kzapalowicz/ubuntu-system-settings/fix-1539158 into lp:ubuntu-system-settings

Proposed by Konrad Zapałowicz
Status: Merged
Approved by: Ken VanDine
Approved revision: 1636
Merged at revision: 1665
Proposed branch: lp:~kzapalowicz/ubuntu-system-settings/fix-1539158
Merge into: lp:ubuntu-system-settings
Diff against target: 42 lines (+26/-0)
1 file modified
plugins/bluetooth/devicemodel.cpp (+26/-0)
To merge this branch: bzr merge lp:~kzapalowicz/ubuntu-system-settings/fix-1539158
Reviewer Review Type Date Requested Status
Simon Fels (community) Approve
Jim Hodapp (community) code Approve
Ken VanDine Approve
Review via email: mp+293854@code.launchpad.net

Commit message

Fix pin code request is rejected because device is not valid

When the new device is created either almost or at the same time as the
pin code request is requested there is a race between pin code request
handler and the device initialization code. The pin code request handler
works only with devices which are valid. On the other hand the device is
valid only when it's type is updated. This happens when the properties
are gathered which is done in an asynchronous way. Because of this
asynchronousity it happens too late and by that time the pin code request
is rejected which denies the bonding.

This commit works around this issue by implementing a wait for device to
become valid. In order not to stall the wait is not infinite and the
nullptr is returned when the device cannot be set to valid. This has an
effect only when the addDevice is called from the FindOrCreateDevice
context as in any other case the properties are known by that point.

Description of the change

Fix pin code request is rejected because device is not valid

When the new device is created either almost or at the same time as the
pin code request is requested there is a race between pin code request
handler and the device initialization code. The pin code request handler
works only with devices which are valid. On the other hand the device is
valid only when it's type is updated. This happens when the properties
are gathered which is done in an asynchronous way. Because of this
asynchronousity it happens too late and by that time the pin code request
is rejected which denies the bonding.

This commit works around this issue by implementing a wait for device to
become valid. In order not to stall the wait is not infinite and the
nullptr is returned when the device cannot be set to valid. This has an
effect only when the addDevice is called from the FindOrCreateDevice
context as in any other case the properties are known by that point.

To post a comment you must log in.
Revision history for this message
Ken VanDine (ken-vandine) wrote :

The code looks fine, besides a typo in your comment. Could you please fix "oly" in the comment?

I'm not sure how I can test it though, my car doesn't require a pin.

review: Needs Fixing
1634. By Konrad Zapałowicz

Fix typo in the comment

Revision history for this message
Ken VanDine (ken-vandine) wrote :

Thanks, looks good

review: Approve
Revision history for this message
Jim Hodapp (jhodapp) wrote :

A few comments inline.

review: Needs Fixing (code)
1635. By Konrad Zapałowicz

Improve the documentation and add const correctness

Revision history for this message
Jim Hodapp (jhodapp) wrote :

One minor grammar change in a comment.

review: Needs Fixing
1636. By Konrad Zapałowicz

Fix a typo in the documentation

Revision history for this message
Jim Hodapp (jhodapp) wrote :

LGTM

review: Approve (code)
Revision history for this message
Simon Fels (morphis) wrote :

LGTM

review: Approve
Revision history for this message
Michał Sawicz (saviq) wrote :

FWIW this didn't help in my car, will come up with logs again in a bit.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/bluetooth/devicemodel.cpp'
2--- plugins/bluetooth/devicemodel.cpp 2016-03-15 14:44:34 +0000
3+++ plugins/bluetooth/devicemodel.cpp 2016-05-10 13:05:23 +0000
4@@ -22,6 +22,8 @@
5
6 #include <QDBusReply>
7 #include <QDebug>
8+#include <QTime>
9+#include <QCoreApplication>
10
11 #include "dbus-shared.h"
12
13@@ -499,6 +501,30 @@
14 QSharedPointer<Device> device(new Device(path, m_dbus));
15 device->setProperties(properties);
16
17+ // The device is valid when the right properties are set for it. The
18+ // code below makes sure that the right properties are set as in certain
19+ // situations it happens with a delay.
20+ //
21+ // In case this function is called from FindOrCreateDevice() context
22+ // the properties are not yet known. Of course the device will become
23+ // valid once the device properties are gathered however this is done
24+ // asynchronously, in the Device class constructor, and it takes a
25+ // while. This is a problem in certain situations and as a consequence
26+ // the pin code request is rejected and the pairing denied. To
27+ // workaround this unwanted behavior we assume that at this point it is
28+ // just a matter of time for the device to become valid and we wait a
29+ // bit here. This is safe because in any other case the properties are
30+ // already updated.
31+ uint8_t tries = 0;
32+ while (!device->isValid() && tries < 10) {
33+ const QTime timeout = QTime::currentTime().addMSecs(100);
34+ while (QTime::currentTime() < timeout)
35+ QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
36+ tries++;
37+ }
38+
39+ // However if the above fails there is nothing else that can be done
40+ // than returning the nullptr.
41 if (!device->isValid())
42 return QSharedPointer<Device>(nullptr);
43

Subscribers

People subscribed via source and target branches