BluezQt

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