summaryrefslogtreecommitdiff
blob: 84f39a3c2b9b88a23d7a0d0b7a1125a68f72c218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
From c02c4806c9bfcd6dc36fab6ed9873cff86681cdd Mon Sep 17 00:00:00 2001
From: David Rosca <nowrep@gmail.com>
Date: Fri, 26 May 2017 13:55:44 +0200
Subject: Fix property changes being missed immediately after an obejct is
 added

Fix race condition when property changes may be missed if the property
is changed immediately after the object is created.
The issue was that the connection to PropertyChanged signal was
created only after interfacesAdded signal was fired, which may have
already been too late.
This fixes it with connecting to PropertyChanged signal on all paths
in Manager::init().

BUG: 377405

Differential Revision: https://phabricator.kde.org/D5550
---
 autotests/fakebluez/devicemanager.cpp | 19 +++++++++++++++++++
 autotests/fakebluez/devicemanager.h   |  1 +
 autotests/managertest.cpp             | 20 ++++++++++++++++++++
 autotests/managertest.h               |  1 +
 src/adapter_p.cpp                     |  8 --------
 src/device_p.cpp                      | 10 +++++-----
 src/input.cpp                         | 10 +++-------
 src/input_p.h                         |  4 ----
 src/manager_p.cpp                     | 27 +++++++++++++++++++++++++++
 src/manager_p.h                       |  4 +++-
 src/mediaplayer_p.cpp                 |  3 ---
 src/utils.cpp                         |  7 +++++++
 src/utils.h                           |  1 +
 13 files changed, 87 insertions(+), 28 deletions(-)

diff --git a/autotests/fakebluez/devicemanager.cpp b/autotests/fakebluez/devicemanager.cpp
index 7967047..229b92a 100644
--- a/autotests/fakebluez/devicemanager.cpp
+++ b/autotests/fakebluez/devicemanager.cpp
@@ -43,6 +43,8 @@ void DeviceManager::runAction(const QString &actionName, const QVariantMap &prop
         runChangeAdapterProperty(properties);
     } else if (actionName == QLatin1String("change-device-property")) {
         runChangeDeviceProperty(properties);
+    } else if (actionName == QLatin1String("bug377405")) {
+        runBug377405();
     }
 }
 
@@ -103,3 +105,20 @@ void DeviceManager::runChangeDeviceProperty(const QVariantMap &properties)
 
     device->changeProperty(properties.value(QStringLiteral("Name")).toString(), properties.value(QStringLiteral("Value")));
 }
+
+void DeviceManager::runBug377405()
+{
+    QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0"));
+    QVariantMap adapterProps;
+    adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path);
+    adapterProps[QStringLiteral("Powered")] = false;
+
+    runCreateAdapterAction(adapterProps);
+
+    QVariantMap properties;
+    properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path);
+    properties[QStringLiteral("Name")] = QStringLiteral("Powered");
+    properties[QStringLiteral("Value")] = true;
+
+    runChangeAdapterProperty(properties);
+}
diff --git a/autotests/fakebluez/devicemanager.h b/autotests/fakebluez/devicemanager.h
index f830175..163b311 100644
--- a/autotests/fakebluez/devicemanager.h
+++ b/autotests/fakebluez/devicemanager.h
@@ -41,6 +41,7 @@ private:
     void runRemoveDeviceAction(const QVariantMap &properties);
     void runChangeAdapterProperty(const QVariantMap &properties);
     void runChangeDeviceProperty(const QVariantMap &properties);
+    void runBug377405();
 
     ObjectManager *m_objectManager;
 
diff --git a/autotests/managertest.cpp b/autotests/managertest.cpp
index 3217f97..1d7ffcc 100644
--- a/autotests/managertest.cpp
+++ b/autotests/managertest.cpp
@@ -437,4 +437,24 @@ void ManagerTest::bug364416()
     delete manager;
 }
 
+void ManagerTest::bug377405()
+{
+    // Bug 377405: Property changes immediately after adapter is added are lost
+
+    FakeBluez::start();
+    FakeBluez::runTest(QStringLiteral("bluez-standard"));
+
+    Manager *manager = new Manager;
+
+    InitManagerJob *job = manager->init();
+    job->exec();
+
+    QVERIFY(!job->error());
+
+    FakeBluez::runAction(QStringLiteral("devicemanager"), QStringLiteral("bug377405"));
+
+    // Adapter property Powered is changed to true immediately after being added
+    QTRY_COMPARE(manager->isBluetoothOperational(), true);
+}
+
 QTEST_MAIN(ManagerTest)
diff --git a/autotests/managertest.h b/autotests/managertest.h
index 654fe98..9de8ae1 100644
--- a/autotests/managertest.h
+++ b/autotests/managertest.h
@@ -41,6 +41,7 @@ private Q_SLOTS:
     void deviceForAddressTest();
     void adapterWithDevicesRemovedTest();
     void bug364416();
+    void bug377405();
 
 };
 
diff --git a/src/adapter_p.cpp b/src/adapter_p.cpp
index 786fa7d..eb5fa9f 100644
--- a/src/adapter_p.cpp
+++ b/src/adapter_p.cpp
@@ -48,14 +48,6 @@ void AdapterPrivate::init(const QVariantMap &properties)
     m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezAdapter->path(),
                                           DBusConnection::orgBluez(), this);
 
-    // QueuedConnection is important here to be able to perform actions, that depend on
-    // a previously set property, directly from slot connected to propertyChanged signal.
-    // Eg. Powering on adapter and then starting discovery.
-    //  * with DirectConnection the StartDiscovery would fail because the adapter is still
-    //    powered off when the PropertiesChanged signal is emitted ...
-    connect(m_dbusProperties, &DBusProperties::PropertiesChanged,
-            this, &AdapterPrivate::propertiesChanged, Qt::QueuedConnection);
-
     // Init properties
     m_address = properties.value(QStringLiteral("Address")).toString();
     m_name = properties.value(QStringLiteral("Name")).toString();
diff --git a/src/device_p.cpp b/src/device_p.cpp
index 79cbb86..351162d 100644
--- a/src/device_p.cpp
+++ b/src/device_p.cpp
@@ -58,10 +58,6 @@ void DevicePrivate::init(const QVariantMap &properties)
     m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezDevice->path(),
                                           DBusConnection::orgBluez(), this);
 
-    // QueuedConnection is important here - see AdapterPrivate::initProperties
-    connect(m_dbusProperties, &DBusProperties::PropertiesChanged,
-            this, &DevicePrivate::propertiesChanged, Qt::QueuedConnection);
-
     // Init properties
     m_address = properties.value(QStringLiteral("Address")).toString();
     m_name = properties.value(QStringLiteral("Name")).toString();
@@ -136,7 +132,11 @@ QDBusPendingReply<> DevicePrivate::setDBusProperty(const QString &name, const QV
 
 void DevicePrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated)
 {
-    if (interface != Strings::orgBluezDevice1()) {
+    if (interface == Strings::orgBluezInput1() && m_input) {
+        m_input->d->propertiesChanged(interface, changed, invalidated);
+    } else if (interface == Strings::orgBluezMediaPlayer1() && m_mediaPlayer) {
+        m_mediaPlayer->d->propertiesChanged(interface, changed, invalidated);
+    } else if (interface != Strings::orgBluezDevice1()) {
         return;
     }
 
diff --git a/src/input.cpp b/src/input.cpp
index 4cf4f03..aabb996 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -25,11 +25,11 @@
 #include "utils.h"
 #include "macros.h"
 
+#include <QVariantMap>
+
 namespace BluezQt
 {
 
-typedef org::freedesktop::DBus::Properties DBusProperties;
-
 static Input::ReconnectMode stringToReconnectMode(const QString &mode)
 {
     if (mode == QLatin1String("none")) {
@@ -45,11 +45,7 @@ static Input::ReconnectMode stringToReconnectMode(const QString &mode)
 InputPrivate::InputPrivate(const QString &path, const QVariantMap &properties)
     : QObject()
 {
-    m_dbusProperties = new DBusProperties(Strings::orgBluez(), path,
-                                          DBusConnection::orgBluez(), this);
-
-    connect(m_dbusProperties, &DBusProperties::PropertiesChanged,
-            this, &InputPrivate::propertiesChanged, Qt::QueuedConnection);
+    Q_UNUSED(path);
 
     // Init properties
     m_reconnectMode = stringToReconnectMode(properties.value(QStringLiteral("ReconnectMode")).toString());
diff --git a/src/input_p.h b/src/input_p.h
index 424179a..ce3f33d 100644
--- a/src/input_p.h
+++ b/src/input_p.h
@@ -26,13 +26,10 @@
 #include <QObject>
 
 #include "input.h"
-#include "dbusproperties.h"
 
 namespace BluezQt
 {
 
-typedef org::freedesktop::DBus::Properties DBusProperties;
-
 class InputPrivate : public QObject
 {
     Q_OBJECT
@@ -43,7 +40,6 @@ public:
     void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
 
     QWeakPointer<Input> q;
-    DBusProperties *m_dbusProperties;
 
     Input::ReconnectMode m_reconnectMode;
 };
diff --git a/src/manager_p.cpp b/src/manager_p.cpp
index aaec901..105d954 100644
--- a/src/manager_p.cpp
+++ b/src/manager_p.cpp
@@ -82,6 +82,13 @@ void ManagerPrivate::init()
 
     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(DBusConnection::orgBluez().asyncCall(call));
     connect(watcher, &QDBusPendingCallWatcher::finished, this, &ManagerPrivate::nameHasOwnerFinished);
+
+    DBusConnection::orgBluez().connect(Strings::orgBluez(),
+                                       QString(),
+                                       Strings::orgFreedesktopDBusProperties(),
+                                       QStringLiteral("PropertiesChanged"),
+                                       this,
+                                       SLOT(propertiesChanged(QString,QVariantMap,QStringList)));
 }
 
 void ManagerPrivate::nameHasOwnerFinished(QDBusPendingCallWatcher *watcher)
@@ -424,6 +431,26 @@ void ManagerPrivate::setUsableAdapter(const AdapterPtr &adapter)
     }
 }
 
+void ManagerPrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated)
+{
+    // Cut anything after device path to forward it to Device to handle
+    const QString path = message().path().section(QLatin1Char('/'), 0, 4);
+
+    QTimer::singleShot(0, this, [=]() {
+        AdapterPtr adapter = m_adapters.value(path);
+        if (adapter) {
+            adapter->d->propertiesChanged(interface, changed, invalidated);
+            return;
+        }
+        DevicePtr device = m_devices.value(path);
+        if (device) {
+            device->d->propertiesChanged(interface, changed, invalidated);
+            return;
+        }
+        qCDebug(BLUEZQT) << "Unhandled property change" << interface << changed << invalidated;
+    });
+}
+
 void ManagerPrivate::dummy()
 {
 }
diff --git a/src/manager_p.h b/src/manager_p.h
index 0b26d1b..cc3276d 100644
--- a/src/manager_p.h
+++ b/src/manager_p.h
@@ -25,6 +25,7 @@
 
 #include <QObject>
 #include <QHash>
+#include <QDBusContext>
 
 #include "types.h"
 #include "rfkill.h"
@@ -44,7 +45,7 @@ class Adapter;
 class Device;
 class AdapterPrivate;
 
-class ManagerPrivate : public QObject
+class ManagerPrivate : public QObject, protected QDBusContext
 {
     Q_OBJECT
 
@@ -96,6 +97,7 @@ Q_SIGNALS:
     void initFinished();
 
 private Q_SLOTS:
+    void propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
     void dummy();
 
 };
diff --git a/src/mediaplayer_p.cpp b/src/mediaplayer_p.cpp
index 3c4e57e..21a3ebb 100644
--- a/src/mediaplayer_p.cpp
+++ b/src/mediaplayer_p.cpp
@@ -92,9 +92,6 @@ void MediaPlayerPrivate::init(const QVariantMap &properties)
     m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezMediaPlayer->path(),
                                           DBusConnection::orgBluez(), this);
 
-    connect(m_dbusProperties, &DBusProperties::PropertiesChanged,
-            this, &MediaPlayerPrivate::propertiesChanged, Qt::QueuedConnection);
-
     // Init properties
     m_name = properties.value(QStringLiteral("Name")).toString();
     m_equalizer = stringToEqualizer(properties.value(QStringLiteral("Equalizer")).toString());
diff --git a/src/utils.cpp b/src/utils.cpp
index aed8250..9ca3d6d 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -39,6 +39,7 @@ public:
 
     bool testRun;
     QString orgFreedesktopDBus;
+    QString orgFreedesktopDBusProperties;
     QString orgBluez;
     QString orgBluezAdapter1;
     QString orgBluezDevice1;
@@ -59,6 +60,7 @@ GlobalData::GlobalData()
 {
     testRun = false;
     orgFreedesktopDBus = QStringLiteral("org.freedesktop.DBus");
+    orgFreedesktopDBusProperties = QStringLiteral("org.freedesktop.DBus.Properties");
     orgBluez = QStringLiteral("org.bluez");
     orgBluezAdapter1 = QStringLiteral("org.bluez.Adapter1");
     orgBluezDevice1 = QStringLiteral("org.bluez.Device1");
@@ -88,6 +90,11 @@ QString Strings::orgFreedesktopDBus()
     return globalData->orgFreedesktopDBus;
 }
 
+QString Strings::orgFreedesktopDBusProperties()
+{
+    return globalData->orgFreedesktopDBusProperties;
+}
+
 QString Strings::orgBluez()
 {
     return globalData->orgBluez;
diff --git a/src/utils.h b/src/utils.h
index fd49d8e..08cb6b9 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -36,6 +36,7 @@ namespace Strings
 {
 
 QString orgFreedesktopDBus();
+QString orgFreedesktopDBusProperties();
 QString orgBluez();
 QString orgBluezAdapter1();
 QString orgBluezDevice1();
-- 
cgit v0.11.2