1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtCore/QLoggingCategory>
41
42#include "remotedevicemanager_p.h"
43#include "bluez5_helper_p.h"
44#include "device1_bluez5_p.h"
45#include "objectmanager_p.h"
46
47QT_BEGIN_NAMESPACE
48
49Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ)
50
51/*!
52 * Convenience wrapper around org.bluez.Device1 management
53 *
54 * Very simple and not thread safe.
55 */
56
57RemoteDeviceManager::RemoteDeviceManager(
58 const QBluetoothAddress &address, QObject *parent)
59 : QObject(parent), localAddress(address)
60{
61 if (!isBluez5())
62 return;
63
64 bool ok = false;
65 adapterPath = findAdapterForAddress(address, &ok);
66 if (!ok || adapterPath.isEmpty()) {
67 qCWarning(QT_BT_BLUEZ) << "Cannot initialize RemoteDeviceManager";
68 }
69}
70
71bool RemoteDeviceManager::scheduleJob(
72 JobType job, const QVector<QBluetoothAddress> &remoteDevices)
73{
74 if (adapterPath.isEmpty())
75 return false;
76
77 for (const auto& remote : remoteDevices)
78 jobQueue.push_back(std::make_pair(job, remote));
79
80 QTimer::singleShot(0, this, [this](){ runQueue(); });
81 return true;
82}
83
84void RemoteDeviceManager::runQueue()
85{
86 if (jobInProgress || adapterPath.isEmpty())
87 return;
88
89 if (jobQueue.empty())
90 return;
91
92 jobInProgress = true;
93 switch (jobQueue.front().first) {
94 case JobType::JobDisconnectDevice:
95 disconnectDevice(jobQueue.front().second);
96 break;
97 default:
98 break;
99 }
100}
101
102void RemoteDeviceManager::prepareNextJob()
103{
104 Q_ASSERT(!jobQueue.empty());
105
106 jobQueue.pop_front();
107 jobInProgress = false;
108
109 qDebug(QT_BT_BLUEZ) << "RemoteDeviceManager job queue status:" << jobQueue.empty();
110 if (jobQueue.empty())
111 emit finished();
112 else
113 runQueue();
114}
115
116void RemoteDeviceManager::disconnectDevice(const QBluetoothAddress &remote)
117{
118 // collect initial set of information
119 OrgFreedesktopDBusObjectManagerInterface managerBluez5(
120 QStringLiteral("org.bluez"),
121 QStringLiteral("/"),
122 QDBusConnection::systemBus(), this);
123 QDBusPendingReply<ManagedObjectList> reply = managerBluez5.GetManagedObjects();
124 reply.waitForFinished();
125 if (reply.isError()) {
126 QTimer::singleShot(0, this, [this](){ prepareNextJob(); });
127 return;
128 }
129
130 bool jobStarted = false;
131 ManagedObjectList managedObjectList = reply.value();
132 for (auto it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) {
133 const QDBusObjectPath &path = it.key();
134 const InterfaceList &ifaceList = it.value();
135
136 for (auto jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) {
137 const QString &iface = jt.key();
138
139 if (path.path().indexOf(adapterPath) != 0)
140 continue; //devices whose path doesn't start with same path we skip
141
142 if (iface != QStringLiteral("org.bluez.Device1"))
143 continue;
144
145 const QBluetoothAddress foundAddress(ifaceList.value(iface).value(QStringLiteral("Address")).toString());
146 if (foundAddress != remote)
147 continue;
148
149 // found the correct Device1 path
150 OrgBluezDevice1Interface* device1 = new OrgBluezDevice1Interface(QStringLiteral("org.bluez"),
151 path.path(),
152 QDBusConnection::systemBus(),
153 this);
154 QDBusPendingReply<> asyncReply = device1->Disconnect();
155 QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(asyncReply, this);
156 const auto watcherFinished = [this, device1](QDBusPendingCallWatcher* call) {
157 call->deleteLater();
158 device1->deleteLater();
159 prepareNextJob();
160 };
161 connect(watcher, &QDBusPendingCallWatcher::finished, this, watcherFinished);
162 jobStarted = true;
163 break;
164 }
165 }
166
167 if (!jobStarted) {
168 qDebug(QT_BT_BLUEZ) << "RemoteDeviceManager JobDisconnectDevice failed";
169 QTimer::singleShot(0, this, [this](){ prepareNextJob(); });
170 }
171}
172
173QT_END_NAMESPACE
174