Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
remotedevicemanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/QLoggingCategory>
5
7#include "bluez5_helper_p.h"
8#include "device1_bluez5_p.h"
9#include "objectmanager_p.h"
10
12
14
15
22 const QBluetoothAddress &address, QObject *parent)
23 : QObject(parent), localAddress(address)
24{
26
27 bool ok = false;
28 adapterPath = findAdapterForAddress(address, &ok);
29 if (!ok || adapterPath.isEmpty()) {
30 qCWarning(QT_BT_BLUEZ) << "Cannot initialize RemoteDeviceManager";
31 }
32}
33
34bool RemoteDeviceManager::scheduleJob(JobType job, const QList<QBluetoothAddress> &remoteDevices)
35{
36 if (adapterPath.isEmpty())
37 return false;
38
39 for (const auto& remote : remoteDevices)
40 jobQueue.push_back(std::make_pair(job, remote));
41
42 QTimer::singleShot(0, this, [this](){ runQueue(); });
43 return true;
44}
45
46void RemoteDeviceManager::runQueue()
47{
48 if (jobInProgress || adapterPath.isEmpty())
49 return;
50
51 if (jobQueue.empty())
52 return;
53
54 jobInProgress = true;
55 switch (jobQueue.front().first) {
57 disconnectDevice(jobQueue.front().second);
58 break;
59 default:
60 break;
61 }
62}
63
64void RemoteDeviceManager::prepareNextJob()
65{
66 Q_ASSERT(!jobQueue.empty());
67
68 jobQueue.pop_front();
69 jobInProgress = false;
70
71 qCDebug(QT_BT_BLUEZ) << "RemoteDeviceManager job queue status:" << jobQueue.empty();
72 if (jobQueue.empty())
73 emit finished();
74 else
75 runQueue();
76}
77
78void RemoteDeviceManager::disconnectDevice(const QBluetoothAddress &remote)
79{
80 // collect initial set of information
82 QStringLiteral("org.bluez"),
83 QStringLiteral("/"),
85 QDBusPendingReply<ManagedObjectList> reply = managerBluez5.GetManagedObjects();
86 reply.waitForFinished();
87 if (reply.isError()) {
88 QTimer::singleShot(0, this, [this](){ prepareNextJob(); });
89 return;
90 }
91
92 bool jobStarted = false;
93 ManagedObjectList managedObjectList = reply.value();
94 for (auto it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) {
95 const QDBusObjectPath &path = it.key();
96 const InterfaceList &ifaceList = it.value();
97
98 for (auto jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) {
99 const QString &iface = jt.key();
100
101 if (path.path().indexOf(adapterPath) != 0)
102 continue; //devices whose path doesn't start with same path we skip
103
104 if (iface != QStringLiteral("org.bluez.Device1"))
105 continue;
106
107 const QBluetoothAddress foundAddress(ifaceList.value(iface).value(QStringLiteral("Address")).toString());
108 if (foundAddress != remote)
109 continue;
110
111 // found the correct Device1 path
113 path.path(),
115 this);
116 QDBusPendingReply<> asyncReply = device1->Disconnect();
118 const auto watcherFinished = [this, device1](QDBusPendingCallWatcher* call) {
119 call->deleteLater();
120 device1->deleteLater();
121 prepareNextJob();
122 };
123 connect(watcher, &QDBusPendingCallWatcher::finished, this, watcherFinished);
124 jobStarted = true;
125 break;
126 }
127 }
128
129 if (!jobStarted) {
130 qCDebug(QT_BT_BLUEZ) << "RemoteDeviceManager JobDisconnectDevice failed";
131 QTimer::singleShot(0, this, [this](){ prepareNextJob(); });
132 }
133}
134
136
137#include "moc_remotedevicemanager_p.cpp"
QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok=nullptr)
QMap< QDBusObjectPath, InterfaceList > ManagedObjectList
QT_BEGIN_NAMESPACE void initializeBluez5()
\inmodule QtBluetooth
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
\inmodule QtDBus
void finished(QDBusPendingCallWatcher *self=nullptr)
This signal is emitted when the pending call has finished and its reply is available.
Definition qmap.h:187
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
const_iterator constBegin() const noexcept
Definition qset.h:139
const_iterator constEnd() const noexcept
Definition qset.h:143
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
bool scheduleJob(JobType job, const QList< QBluetoothAddress > &remoteDevices)
QSet< QString >::iterator it
Combined button and popup list for selecting options.
constexpr QBindableInterface iface
Definition qproperty.h:666
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLuint GLuint64EXT address
GLsizei const GLchar *const * path
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QStringLiteral(str)
#define emit
QFutureWatcher< int > watcher
QNetworkReply * reply
char * toString(const MyType &t)
[31]