1 | /* |
---|---|
2 | * BluezQt - Asynchronous Bluez wrapper library |
3 | * |
4 | * SPDX-FileCopyrightText: 2014-2015 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 "utils.h" |
10 | #include "bluezqt_export.h" |
11 | #include "manager.h" |
12 | #include "obexmanager.h" |
13 | |
14 | #include <QDBusArgument> |
15 | #include <QDBusConnection> |
16 | #include <QPointer> |
17 | |
18 | namespace BluezQt |
19 | { |
20 | class GlobalData |
21 | { |
22 | public: |
23 | explicit GlobalData(); |
24 | |
25 | bool testRun; |
26 | QString orgFreedesktopDBus; |
27 | QString orgFreedesktopDBusProperties; |
28 | QString orgBluez; |
29 | QString orgBluezAdapter1; |
30 | QString orgBluezBattery1; |
31 | QString orgBluezDevice1; |
32 | QString orgBluezGattService1; |
33 | QString orgBluezGattCharateristic1; |
34 | QString orgBluezGattDescriptor1; |
35 | QString orgBluezInput1; |
36 | QString orgBluezGattManager1; |
37 | QString orgBluezLEAdvertisingManager1; |
38 | QString orgBluezMedia1; |
39 | QString orgBluezMediaPlayer1; |
40 | QString orgBluezMediaTransport1; |
41 | QString orgBluezAgentManager1; |
42 | QString orgBluezProfileManager1; |
43 | QString orgBluezObex; |
44 | QString orgBluezObexClient1; |
45 | QString orgBluezObexAgentManager1; |
46 | QString orgBluezObexSession1; |
47 | QString orgBluezObexTransfer1; |
48 | QPointer<Manager> manager; |
49 | QPointer<ObexManager> obexManager; |
50 | }; |
51 | |
52 | GlobalData::GlobalData() |
53 | { |
54 | testRun = false; |
55 | orgFreedesktopDBus = QStringLiteral("org.freedesktop.DBus"); |
56 | orgFreedesktopDBusProperties = QStringLiteral("org.freedesktop.DBus.Properties"); |
57 | orgBluez = QStringLiteral("org.bluez"); |
58 | orgBluezAdapter1 = QStringLiteral("org.bluez.Adapter1"); |
59 | orgBluezBattery1 = QStringLiteral("org.bluez.Battery1"); |
60 | orgBluezDevice1 = QStringLiteral("org.bluez.Device1"); |
61 | orgBluezGattService1 = QStringLiteral("org.bluez.GattService1"); |
62 | orgBluezGattCharateristic1 = QStringLiteral("org.bluez.GattCharacteristic1"); |
63 | orgBluezGattDescriptor1 = QStringLiteral("org.bluez.GattDescriptor1"); |
64 | orgBluezInput1 = QStringLiteral("org.bluez.Input1"); |
65 | orgBluezGattManager1 = QStringLiteral("org.bluez.GattManager1"); |
66 | orgBluezLEAdvertisingManager1 = QStringLiteral("org.bluez.LEAdvertisingManager1"); |
67 | orgBluezMedia1 = QStringLiteral("org.bluez.Media1"); |
68 | orgBluezMediaPlayer1 = QStringLiteral("org.bluez.MediaPlayer1"); |
69 | orgBluezMediaTransport1 = QStringLiteral("org.bluez.MediaTransport1"); |
70 | orgBluezAgentManager1 = QStringLiteral("org.bluez.AgentManager1"); |
71 | orgBluezProfileManager1 = QStringLiteral("org.bluez.ProfileManager1"); |
72 | orgBluezObex = QStringLiteral("org.bluez.obex"); |
73 | orgBluezObexClient1 = QStringLiteral("org.bluez.obex.Client1"); |
74 | orgBluezObexAgentManager1 = QStringLiteral("org.bluez.obex.AgentManager1"); |
75 | orgBluezObexSession1 = QStringLiteral("org.bluez.obex.Session1"); |
76 | orgBluezObexTransfer1 = QStringLiteral("org.bluez.obex.Transfer1"); |
77 | } |
78 | |
79 | Q_GLOBAL_STATIC(GlobalData, globalData) |
80 | |
81 | // For fakebluez tests |
82 | BLUEZQT_EXPORT void bluezqt_initFakeBluezTestRun() |
83 | { |
84 | globalData->testRun = true; |
85 | globalData->orgBluez = QStringLiteral("org.kde.bluezqt.fakebluez"); |
86 | globalData->orgBluezObex = QStringLiteral("org.kde.bluezqt.fakebluez"); |
87 | } |
88 | |
89 | QString Strings::orgFreedesktopDBus() |
90 | { |
91 | return globalData->orgFreedesktopDBus; |
92 | } |
93 | |
94 | QString Strings::orgFreedesktopDBusProperties() |
95 | { |
96 | return globalData->orgFreedesktopDBusProperties; |
97 | } |
98 | |
99 | QString Strings::orgBluez() |
100 | { |
101 | return globalData->orgBluez; |
102 | } |
103 | |
104 | QString Strings::orgBluezAdapter1() |
105 | { |
106 | return globalData->orgBluezAdapter1; |
107 | } |
108 | |
109 | QString Strings::orgBluezBattery1() |
110 | { |
111 | return globalData->orgBluezBattery1; |
112 | } |
113 | |
114 | QString Strings::orgBluezDevice1() |
115 | { |
116 | return globalData->orgBluezDevice1; |
117 | } |
118 | |
119 | QString Strings::orgBluezGattService1() |
120 | { |
121 | return globalData->orgBluezGattService1; |
122 | } |
123 | |
124 | QString Strings::orgBluezGattCharacteristic1() |
125 | { |
126 | return globalData->orgBluezGattCharateristic1; |
127 | } |
128 | |
129 | QString Strings::orgBluezGattDescriptor1() |
130 | { |
131 | return globalData->orgBluezGattDescriptor1; |
132 | } |
133 | |
134 | QString Strings::orgBluezInput1() |
135 | { |
136 | return globalData->orgBluezInput1; |
137 | } |
138 | |
139 | QString Strings::orgBluezGattManager1() |
140 | { |
141 | return globalData->orgBluezGattManager1; |
142 | } |
143 | |
144 | QString Strings::orgBluezLEAdvertisingManager1() |
145 | { |
146 | return globalData->orgBluezLEAdvertisingManager1; |
147 | } |
148 | |
149 | QString Strings::orgBluezMedia1() |
150 | { |
151 | return globalData->orgBluezMedia1; |
152 | } |
153 | |
154 | QString Strings::orgBluezMediaPlayer1() |
155 | { |
156 | return globalData->orgBluezMediaPlayer1; |
157 | } |
158 | |
159 | QString Strings::orgBluezMediaTransport1() |
160 | { |
161 | return globalData->orgBluezMediaTransport1; |
162 | } |
163 | |
164 | QString Strings::orgBluezAgentManager1() |
165 | { |
166 | return globalData->orgBluezAgentManager1; |
167 | } |
168 | |
169 | QString Strings::orgBluezProfileManager1() |
170 | { |
171 | return globalData->orgBluezProfileManager1; |
172 | } |
173 | |
174 | QString Strings::orgBluezObex() |
175 | { |
176 | return globalData->orgBluezObex; |
177 | } |
178 | |
179 | QString Strings::orgBluezObexClient1() |
180 | { |
181 | return globalData->orgBluezObexClient1; |
182 | } |
183 | |
184 | QString Strings::orgBluezObexAgentManager1() |
185 | { |
186 | return globalData->orgBluezObexAgentManager1; |
187 | } |
188 | |
189 | QString Strings::orgBluezObexSession1() |
190 | { |
191 | return globalData->orgBluezObexSession1; |
192 | } |
193 | |
194 | QString Strings::orgBluezObexTransfer1() |
195 | { |
196 | return globalData->orgBluezObexTransfer1; |
197 | } |
198 | |
199 | QDBusConnection DBusConnection::orgBluez() |
200 | { |
201 | if (globalData->testRun) { |
202 | return QDBusConnection::sessionBus(); |
203 | } |
204 | return QDBusConnection::systemBus(); |
205 | } |
206 | |
207 | QDBusConnection DBusConnection::orgBluezObex() |
208 | { |
209 | return QDBusConnection::sessionBus(); |
210 | } |
211 | |
212 | Manager *Instance::manager() |
213 | { |
214 | return globalData->manager; |
215 | } |
216 | |
217 | void Instance::setManager(Manager *manager) |
218 | { |
219 | globalData->manager = manager; |
220 | } |
221 | |
222 | ObexManager *Instance::obexManager() |
223 | { |
224 | return globalData->obexManager; |
225 | } |
226 | |
227 | void Instance::setObexManager(ObexManager *obexManager) |
228 | { |
229 | globalData->obexManager = obexManager; |
230 | } |
231 | |
232 | QStringList stringListToUpper(const QStringList &list) |
233 | { |
234 | QStringList converted; |
235 | converted.reserve(asize: list.size()); |
236 | for (const QString &str : list) { |
237 | converted.append(t: str.toUpper()); |
238 | } |
239 | return converted; |
240 | } |
241 | |
242 | ManData variantToManData(const QVariant &v) { |
243 | // Map to return |
244 | ManData manData; |
245 | |
246 | if (!v.isValid()) { |
247 | return manData; |
248 | } |
249 | |
250 | // Map with QVariant values |
251 | QMap<uint16_t,QVariant> vMap; |
252 | const QDBusArgument &dbusArgs = v.value<QDBusArgument>(); |
253 | dbusArgs >> vMap; |
254 | // Iterate & convert values to byte array |
255 | QMap<uint16_t,QVariant>::const_iterator i; |
256 | for (i = vMap.constBegin(); i != vMap.constEnd(); ++i) { |
257 | const QVariant &value = i.value(); |
258 | const uint16_t &key = i.key(); |
259 | manData.insert(key,value: value.toByteArray()); |
260 | } |
261 | return manData; |
262 | } |
263 | |
264 | Device::Type classToType(quint32 classNum) |
265 | { |
266 | switch ((classNum & 0x1f00) >> 8) { |
267 | case 0x01: |
268 | return Device::Computer; |
269 | case 0x02: |
270 | switch ((classNum & 0xfc) >> 2) { |
271 | case 0x04: |
272 | return Device::Modem; |
273 | default: |
274 | return Device::Phone; |
275 | } |
276 | case 0x03: |
277 | return Device::Network; |
278 | case 0x04: |
279 | switch ((classNum & 0xfc) >> 2) { |
280 | case 0x01: |
281 | case 0x02: |
282 | return Device::Headset; |
283 | case 0x06: |
284 | return Device::Headphones; |
285 | default: |
286 | return Device::AudioVideo; |
287 | } |
288 | case 0x05: |
289 | switch ((classNum & 0xc0) >> 6) { |
290 | case 0x00: |
291 | switch ((classNum & 0x1e) >> 2) { |
292 | case 0x01: |
293 | case 0x02: |
294 | return Device::Joypad; |
295 | } |
296 | break; |
297 | case 0x01: |
298 | return Device::Keyboard; |
299 | case 0x02: |
300 | switch ((classNum & 0x1e) >> 2) { |
301 | case 0x05: |
302 | return Device::Tablet; |
303 | default: |
304 | return Device::Mouse; |
305 | } |
306 | } |
307 | return Device::Peripheral; |
308 | case 0x06: |
309 | if (classNum & 0x80) { |
310 | return Device::Printer; |
311 | } else if (classNum & 0x20) { |
312 | return Device::Camera; |
313 | } |
314 | return Device::Imaging; |
315 | case 0x07: |
316 | return Device::Wearable; |
317 | case 0x08: |
318 | return Device::Toy; |
319 | case 0x09: |
320 | return Device::Health; |
321 | default: |
322 | return Device::Uncategorized; |
323 | } |
324 | } |
325 | |
326 | Device::Type appearanceToType(quint16 appearance) |
327 | { |
328 | switch ((appearance & 0xffc0) >> 6) { |
329 | case 0x00: |
330 | return Device::Uncategorized; |
331 | case 0x01: // Generic Phone |
332 | return Device::Phone; |
333 | case 0x02: // Generic Computer |
334 | return Device::Computer; |
335 | case 0x05: // Generic Display |
336 | return Device::AudioVideo; |
337 | case 0x0a: // Generic Media Player |
338 | return Device::AudioVideo; |
339 | case 0x0b: // Generic Barcode Scanner |
340 | return Device::Peripheral; |
341 | case 0x0f: // Generic HID |
342 | switch (appearance & 0x3f) { |
343 | case 0x01: // Keyboard |
344 | return Device::Keyboard; |
345 | case 0x02: // Mouse |
346 | return Device::Mouse; |
347 | case 0x03: // Joystick |
348 | case 0x04: // Gamepad |
349 | return Device::Joypad; |
350 | case 0x05: // Digitizer Tablet |
351 | return Device::Tablet; |
352 | case 0x08: // Barcode Scanner |
353 | return Device::Peripheral; |
354 | } |
355 | // fall-through |
356 | default: |
357 | return Device::Uncategorized; |
358 | } |
359 | } |
360 | |
361 | } // namespace BluezQt |
362 |