Debian Codegraph @

autotests/fakebluez/adapterinterface.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2014-2015 David Rosca <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7 #include "adapterinterface.h"
8 #include "gattmanagerinterface.h"
9 #include "leadvertisingmanagerinterface.h"
10 #include "mediainterface.h"
11 #include "objectmanager.h"
12
13 #include <QDBusConnection>
14 #include <QTimer>
15
16 // AdapterObject
17 AdapterObject::AdapterObject(const QDBusObjectPath &path, QObject *parent)
18 : QObject(parent)
19 {
20 QDBusConnection::sessionBus().registerObject(path.path(), this);
21 }
22
23 // AdapterInterface
24 AdapterInterface::AdapterInterface(const QDBusObjectPath &path, const QVariantMap &properties, QObject *parent)
25 : QDBusAbstractAdaptor(parent)
26 {
27 setPath(path);
28 setObjectParent(parent);
29 setProperties(properties);
30 setName(QStringLiteral("org.bluez.Adapter1"));
31
32 // Alias needs special handling
33 setAlias(properties.value(QStringLiteral("Alias")).toString());
34
35 // Media interface
36 m_media = new MediaInterface(path, parent);
37 ObjectManager *manager = ObjectManager::self();
38 manager->addObject(m_media);
39
40 // LEAdvertisingManager interface
41 m_leAdvertisingManager = new LEAdvertisingManagerInterface(path, parent);
42 manager->addObject(m_leAdvertisingManager);
43
44 // GattManager interface
45 m_gattManager = new GattManagerInterface(path, parent);
46 manager->addObject(m_gattManager);
47 }
48
49 QString AdapterInterface::address() const
50 {
51 return Object::property(QStringLiteral("Address")).toString();
52 }
53
54 QString AdapterInterface::name() const
55 {
56 return Object::property(QStringLiteral("Name")).toString();
57 }
58
59 QString AdapterInterface::alias() const
60 {
61 return Object::property(QStringLiteral("Alias")).toString();
62 }
63
64 void AdapterInterface::setAlias(const QString &alias)
65 {
66 Object::changeProperty(QStringLiteral("Alias"), alias.isEmpty() ? name() : alias);
67 }
68
69 quint32 AdapterInterface::adapterClass() const
70 {
71 return Object::property(QStringLiteral("Class")).toUInt();
72 }
73
74 bool AdapterInterface::powered() const
75 {
76 return Object::property(QStringLiteral("Powered")).toBool();
77 }
78
79 void AdapterInterface::setPowered(bool powered)
80 {
81 Object::changeProperty(QStringLiteral("Powered"), powered);
82 }
83
84 bool AdapterInterface::discoverable() const
85 {
86 return Object::property(QStringLiteral("Discoverable")).toBool();
87 }
88
89 void AdapterInterface::setDiscoverable(bool discoverable)
90 {
91 Object::changeProperty(QStringLiteral("Discoverable"), discoverable);
92
93 if (discoverable && discoverableTimeout() != 0) {
94 QTimer::singleShot(discoverableTimeout() * 1000, this, SLOT(resetDiscoverable()));
95 }
96 }
97
98 bool AdapterInterface::pairable() const
99 {
100 return Object::property(QStringLiteral("Pairable")).toBool();
101 }
102
103 void AdapterInterface::setPairable(bool pairable)
104 {
105 Object::changeProperty(QStringLiteral("Pairable"), pairable);
106
107 if (pairable && pairableTimeout() != 0) {
108 QTimer::singleShot(pairableTimeout() * 1000, this, SLOT(resetPairable()));
109 }
110 }
111
112 quint32 AdapterInterface::pairableTimeout() const
113 {
114 return Object::property(QStringLiteral("PairableTimeout")).toUInt();
115 }
116
117 void AdapterInterface::setPairableTimeout(quint32 timeout)
118 {
119 Object::changeProperty(QStringLiteral("PairableTimeout"), timeout);
120 }
121
122 quint32 AdapterInterface::discoverableTimeout() const
123 {
124 return Object::property(QStringLiteral("DiscoverableTimeout")).toUInt();
125 }
126
127 void AdapterInterface::setDiscoverableTimeout(quint32 timeout)
128 {
129 Object::changeProperty(QStringLiteral("DiscoverableTimeout"), timeout);
130 }
131
132 bool AdapterInterface::discovering() const
133 {
134 return Object::property(QStringLiteral("Discovering")).toBool();
135 }
136
137 QStringList AdapterInterface::uuids() const
138 {
139 return Object::property(QStringLiteral("UUIDs")).toStringList();
140 }
141
142 QString AdapterInterface::modalias() const
143 {
144 return Object::property(QStringLiteral("Modalias")).toString();
145 }
146
147 MediaInterface *AdapterInterface::media() const
148 {
149 return m_media;
150 }
151
152 LEAdvertisingManagerInterface *AdapterInterface::leAdvertisingManager() const
153 {
154 return m_leAdvertisingManager;
155 }
156
157 GattManagerInterface *AdapterInterface::gattManager() const
158 {
159 return m_gattManager;
160 }
161
162 void AdapterInterface::StartDiscovery()
163 {
164 Object::changeProperty(QStringLiteral("Discovering"), true);
165 }
166
167 void AdapterInterface::StopDiscovery()
168 {
169 Object::changeProperty(QStringLiteral("Discovering"), false);
170 }
171
172 void AdapterInterface::RemoveDevice(const QDBusObjectPath &device)
173 {
174 ObjectManager *manager = ObjectManager::self();
175 manager->removeObject(manager->objectByPath(device));
176 }
177
178 void AdapterInterface::SetDiscoveryFilter(const QVariantMap &filter)
179 {
180 // Bluez makes adapter discoverable while discovering if 'Discoverable' option is set
181 if (filter.contains(QStringLiteral("Discoverable")) && filter.value(QStringLiteral("Discoverable")).toBool())
182 {
183 Object::changeProperty(QStringLiteral("Discoverable"), true);
184 }
185 }
186
187 QStringList AdapterInterface::GetDiscoveryFilters()
188 {
189 return {
190 QStringLiteral("UUIDs"),
191 QStringLiteral("RSSI"),
192 QStringLiteral("Pathloss"),
193 QStringLiteral("Transport"),
194 QStringLiteral("DuplicateData"),
195 QStringLiteral("Discoverable"),
196 QStringLiteral("Pattern")
197 };
198 }
199
200 void AdapterInterface::resetPairable()
201 {
202 setPairable(false);
203 }
204
205 void AdapterInterface::resetDiscoverable()
206 {
207 setDiscoverable(false);
208 }
209
210 #include "moc_adapterinterface.cpp"