BluezQt

manager.cpp
1/*
2 * BluezQt - Asynchronous Bluez wrapper library
3 *
4 * SPDX-FileCopyrightText: 2014 David Rosca <nowrep@gmail.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "manager.h"
10
11#include "adapter.h"
12#include "agent.h"
13#include "agentadaptor.h"
14#include "debug.h"
15#include "initmanagerjob.h"
16#include "manager_p.h"
17#include "pendingcall.h"
18#include "profile.h"
19#include "profile_p.h"
20#include "profileadaptor.h"
21#include "utils.h"
22
23namespace BluezQt
24{
26 : QObject(parent)
27 , d(new ManagerPrivate(this))
28{
29 Instance::setManager(this);
30}
31
32Manager::~Manager() = default;
33
35{
36 return new InitManagerJob(this);
37}
38
40{
41 return d->m_initialized;
42}
43
45{
46 return d->m_initialized && d->m_bluezRunning && d->m_loaded;
47}
48
50{
51 return !d->m_bluetoothBlocked && d->m_bluezRunning && d->m_loaded && d->m_usableAdapter;
52}
53
55{
56 return d->m_bluetoothBlocked;
57}
58
60{
61 if (blocked) {
62 d->m_rfkill->block();
63 } else {
64 d->m_rfkill->unblock();
65 }
66}
67
68AdapterPtr Manager::usableAdapter() const
69{
70 return d->m_usableAdapter;
71}
72
73QList<AdapterPtr> Manager::adapters() const
74{
75 return d->m_adapters.values();
76}
77
78QList<DevicePtr> Manager::devices() const
79{
80 return d->m_devices.values();
81}
82
84{
85 QDBusMessage msg = QDBusMessage::createMethodCall(Strings::orgFreedesktopDBus(),
86 QStringLiteral("/org/freedesktop/DBus"),
87 Strings::orgFreedesktopDBus(),
88 QStringLiteral("StartServiceByName"));
89 msg << Strings::orgBluez();
90 msg << quint32(0);
91
92 return new PendingCall(DBusConnection::orgBluez().asyncCall(msg), PendingCall::ReturnUint32);
93}
94
96{
97 for (AdapterPtr adapter : std::as_const(d->m_adapters)) {
98 if (adapter->address() == address) {
99 return adapter;
100 }
101 }
102 return AdapterPtr();
103}
104
106{
107 return d->m_adapters.value(ubi);
108}
109
111{
112 DevicePtr device;
113
114 for (AdapterPtr adapter : std::as_const(d->m_adapters)) {
115 DevicePtr oneDevice = adapter->deviceForAddress(address);
116 if (!oneDevice) {
117 continue;
118 }
119
120 // Prefer powered adapter
121 if (!device) {
122 device = oneDevice;
123 } else if (adapter->isPowered()) {
124 device = oneDevice;
125 }
126 }
127
128 return device;
129}
130
132{
133 return d->m_devices.value(ubi);
134}
135
137{
138 Q_ASSERT(agent);
139
140 if (!d->m_bluezAgentManager) {
141 return new PendingCall(PendingCall::InternalError, QStringLiteral("Manager not operational!"));
142 }
143
144 QString capability;
145
146 switch (agent->capability()) {
147 case Agent::DisplayOnly:
148 capability = QStringLiteral("DisplayOnly");
149 break;
150 case Agent::DisplayYesNo:
151 capability = QStringLiteral("DisplayYesNo");
152 break;
153 case Agent::KeyboardOnly:
154 capability = QStringLiteral("KeyboardOnly");
155 break;
156 case Agent::NoInputNoOutput:
157 capability = QStringLiteral("NoInputNoOutput");
158 break;
159 default:
160 capability = QStringLiteral("DisplayYesNo");
161 break;
162 }
163
164 new AgentAdaptor(agent, this);
165
166 if (!DBusConnection::orgBluez().registerObject(agent->objectPath().path(), agent)) {
167 qCDebug(BLUEZQT) << "Cannot register object" << agent->objectPath().path();
168 }
169
170 return new PendingCall(d->m_bluezAgentManager->RegisterAgent(agent->objectPath(), capability), PendingCall::ReturnVoid, this);
171}
172
174{
175 Q_ASSERT(agent);
176
177 if (!d->m_bluezAgentManager) {
178 return new PendingCall(PendingCall::InternalError, QStringLiteral("Manager not operational!"));
179 }
180
181 DBusConnection::orgBluez().unregisterObject(agent->objectPath().path());
182
183 return new PendingCall(d->m_bluezAgentManager->UnregisterAgent(agent->objectPath()), PendingCall::ReturnVoid, this);
184}
185
187{
188 Q_ASSERT(agent);
189
190 if (!d->m_bluezAgentManager) {
191 return new PendingCall(PendingCall::InternalError, QStringLiteral("Manager not operational!"));
192 }
193
194 return new PendingCall(d->m_bluezAgentManager->RequestDefaultAgent(agent->objectPath()), PendingCall::ReturnVoid, this);
195}
196
198{
199 Q_ASSERT(profile);
200
201 if (!d->m_bluezProfileManager) {
202 return new PendingCall(PendingCall::InternalError, QStringLiteral("Manager not operational!"));
203 }
204
205 new ProfileAdaptor(profile, this);
206
207 if (!DBusConnection::orgBluez().registerObject(profile->objectPath().path(), profile)) {
208 qCDebug(BLUEZQT) << "Cannot register object" << profile->objectPath().path();
209 }
210
211 return new PendingCall(d->m_bluezProfileManager->RegisterProfile(profile->objectPath(), profile->uuid(), profile->d->options),
212 PendingCall::ReturnVoid,
213 this);
214}
215
217{
218 Q_ASSERT(profile);
219
220 if (!d->m_bluezProfileManager) {
221 return new PendingCall(PendingCall::InternalError, QStringLiteral("Manager not operational!"));
222 }
223
224 DBusConnection::orgBluez().unregisterObject(profile->objectPath().path());
225
226 return new PendingCall(d->m_bluezProfileManager->UnregisterProfile(profile->objectPath()), PendingCall::ReturnVoid, this);
227}
228
229Rfkill *Manager::rfkill() const
230{
231 return d->m_rfkill;
232}
233
234} // namespace BluezQt
235
236#include "moc_manager.cpp"
virtual QDBusObjectPath objectPath() const =0
D-Bus object path of the agent.
Init manager job.
PendingCall * unregisterProfile(Profile *profile)
Unregisters profile.
Definition manager.cpp:216
bool isBluetoothOperational() const
Returns whether Bluetooth is operational.
Definition manager.cpp:49
DevicePtr deviceForAddress(const QString &address) const
Returns a device for specified address.
Definition manager.cpp:110
static PendingCall * startService()
Attempts to start org.bluez service by D-Bus activation.
Definition manager.cpp:83
DevicePtr deviceForUbi(const QString &ubi) const
Returns a device for specified UBI.
Definition manager.cpp:131
bool isOperational() const
Returns whether the manager is operational.
Definition manager.cpp:44
PendingCall * unregisterAgent(Agent *agent)
Unregisters agent.
Definition manager.cpp:173
bool isInitialized() const
Returns whether the manager is initialized.
Definition manager.cpp:39
Manager(QObject *parent=nullptr)
Creates a new Manager object.
Definition manager.cpp:25
PendingCall * registerAgent(Agent *agent)
Registers agent.
Definition manager.cpp:136
PendingCall * requestDefaultAgent(Agent *agent)
Requests default agent.
Definition manager.cpp:186
AdapterPtr adapterForUbi(const QString &ubi) const
Returns an adapter for specified UBI.
Definition manager.cpp:105
AdapterPtr adapterForAddress(const QString &address) const
Returns an adapter for specified address.
Definition manager.cpp:95
void setBluetoothBlocked(bool blocked)
Sets a Bluetooth blocked state.
Definition manager.cpp:59
PendingCall * registerProfile(Profile *profile)
Registers profile.
Definition manager.cpp:197
bool isBluetoothBlocked() const
Returns whether Bluetooth is blocked.
Definition manager.cpp:54
~Manager() override
Destroys a Manager object.
InitManagerJob * init()
Creates a new init job.
Definition manager.cpp:34
Pending method call.
@ InternalError
Indicates an internal error.
Definition pendingcall.h:97
Bluetooth profile.
virtual QDBusObjectPath objectPath() const =0
D-Bus object path of the profile.
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
QString path() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.