BluezQt

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

KDE's Doxygen guidelines are available online.