File indexing completed on 2024-12-01 12:29:52

0001 /*
0002  * BluezQt - Asynchronous Bluez wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "utils.h"
0010 #include "bluezqt_export.h"
0011 #include "manager.h"
0012 #include "obexmanager.h"
0013 
0014 #include <QDBusConnection>
0015 #include <QPointer>
0016 
0017 namespace BluezQt
0018 {
0019 class GlobalData
0020 {
0021 public:
0022     explicit GlobalData();
0023 
0024     bool testRun;
0025     QString orgFreedesktopDBus;
0026     QString orgFreedesktopDBusProperties;
0027     QString orgBluez;
0028     QString orgBluezAdapter1;
0029     QString orgBluezBattery1;
0030     QString orgBluezDevice1;
0031     QString orgBluezGattService1;
0032     QString orgBluezGattCharateristic1;
0033     QString orgBluezGattDescriptor1;
0034     QString orgBluezInput1;
0035     QString orgBluezGattManager1;
0036     QString orgBluezLEAdvertisingManager1;
0037     QString orgBluezMedia1;
0038     QString orgBluezMediaPlayer1;
0039     QString orgBluezMediaTransport1;
0040     QString orgBluezAgentManager1;
0041     QString orgBluezProfileManager1;
0042     QString orgBluezObex;
0043     QString orgBluezObexClient1;
0044     QString orgBluezObexAgentManager1;
0045     QString orgBluezObexSession1;
0046     QString orgBluezObexTransfer1;
0047     QPointer<Manager> manager;
0048     QPointer<ObexManager> obexManager;
0049 };
0050 
0051 GlobalData::GlobalData()
0052 {
0053     testRun = false;
0054     orgFreedesktopDBus = QStringLiteral("org.freedesktop.DBus");
0055     orgFreedesktopDBusProperties = QStringLiteral("org.freedesktop.DBus.Properties");
0056     orgBluez = QStringLiteral("org.bluez");
0057     orgBluezAdapter1 = QStringLiteral("org.bluez.Adapter1");
0058     orgBluezBattery1 = QStringLiteral("org.bluez.Battery1");
0059     orgBluezDevice1 = QStringLiteral("org.bluez.Device1");
0060     orgBluezGattService1 = QStringLiteral("org.bluez.GattService1");
0061     orgBluezGattCharateristic1 = QStringLiteral("org.bluez.GattCharacteristic1");
0062     orgBluezGattDescriptor1 = QStringLiteral("org.bluez.GattDescriptor1");
0063     orgBluezInput1 = QStringLiteral("org.bluez.Input1");
0064     orgBluezGattManager1 = QStringLiteral("org.bluez.GattManager1");
0065     orgBluezLEAdvertisingManager1 = QStringLiteral("org.bluez.LEAdvertisingManager1");
0066     orgBluezMedia1 = QStringLiteral("org.bluez.Media1");
0067     orgBluezMediaPlayer1 = QStringLiteral("org.bluez.MediaPlayer1");
0068     orgBluezMediaTransport1 = QStringLiteral("org.bluez.MediaTransport1");
0069     orgBluezAgentManager1 = QStringLiteral("org.bluez.AgentManager1");
0070     orgBluezProfileManager1 = QStringLiteral("org.bluez.ProfileManager1");
0071     orgBluezObex = QStringLiteral("org.bluez.obex");
0072     orgBluezObexClient1 = QStringLiteral("org.bluez.obex.Client1");
0073     orgBluezObexAgentManager1 = QStringLiteral("org.bluez.obex.AgentManager1");
0074     orgBluezObexSession1 = QStringLiteral("org.bluez.obex.Session1");
0075     orgBluezObexTransfer1 = QStringLiteral("org.bluez.obex.Transfer1");
0076 }
0077 
0078 Q_GLOBAL_STATIC(GlobalData, globalData)
0079 
0080 // For fakebluez tests
0081 BLUEZQT_EXPORT void bluezqt_initFakeBluezTestRun()
0082 {
0083     globalData->testRun = true;
0084     globalData->orgBluez = QStringLiteral("org.kde.bluezqt.fakebluez");
0085     globalData->orgBluezObex = QStringLiteral("org.kde.bluezqt.fakebluez");
0086 }
0087 
0088 QString Strings::orgFreedesktopDBus()
0089 {
0090     return globalData->orgFreedesktopDBus;
0091 }
0092 
0093 QString Strings::orgFreedesktopDBusProperties()
0094 {
0095     return globalData->orgFreedesktopDBusProperties;
0096 }
0097 
0098 QString Strings::orgBluez()
0099 {
0100     return globalData->orgBluez;
0101 }
0102 
0103 QString Strings::orgBluezAdapter1()
0104 {
0105     return globalData->orgBluezAdapter1;
0106 }
0107 
0108 QString Strings::orgBluezBattery1()
0109 {
0110     return globalData->orgBluezBattery1;
0111 }
0112 
0113 QString Strings::orgBluezDevice1()
0114 {
0115     return globalData->orgBluezDevice1;
0116 }
0117 
0118 QString Strings::orgBluezGattService1()
0119 {
0120     return globalData->orgBluezGattService1;
0121 }
0122 
0123 QString Strings::orgBluezGattCharacteristic1()
0124 {
0125     return globalData->orgBluezGattCharateristic1;
0126 }
0127 
0128 QString Strings::orgBluezGattDescriptor1()
0129 {
0130     return globalData->orgBluezGattDescriptor1;
0131 }
0132 
0133 QString Strings::orgBluezInput1()
0134 {
0135     return globalData->orgBluezInput1;
0136 }
0137 
0138 QString Strings::orgBluezGattManager1()
0139 {
0140     return globalData->orgBluezGattManager1;
0141 }
0142 
0143 QString Strings::orgBluezLEAdvertisingManager1()
0144 {
0145     return globalData->orgBluezLEAdvertisingManager1;
0146 }
0147 
0148 QString Strings::orgBluezMedia1()
0149 {
0150     return globalData->orgBluezMedia1;
0151 }
0152 
0153 QString Strings::orgBluezMediaPlayer1()
0154 {
0155     return globalData->orgBluezMediaPlayer1;
0156 }
0157 
0158 QString Strings::orgBluezMediaTransport1()
0159 {
0160     return globalData->orgBluezMediaTransport1;
0161 }
0162 
0163 QString Strings::orgBluezAgentManager1()
0164 {
0165     return globalData->orgBluezAgentManager1;
0166 }
0167 
0168 QString Strings::orgBluezProfileManager1()
0169 {
0170     return globalData->orgBluezProfileManager1;
0171 }
0172 
0173 QString Strings::orgBluezObex()
0174 {
0175     return globalData->orgBluezObex;
0176 }
0177 
0178 QString Strings::orgBluezObexClient1()
0179 {
0180     return globalData->orgBluezObexClient1;
0181 }
0182 
0183 QString Strings::orgBluezObexAgentManager1()
0184 {
0185     return globalData->orgBluezObexAgentManager1;
0186 }
0187 
0188 QString Strings::orgBluezObexSession1()
0189 {
0190     return globalData->orgBluezObexSession1;
0191 }
0192 
0193 QString Strings::orgBluezObexTransfer1()
0194 {
0195     return globalData->orgBluezObexTransfer1;
0196 }
0197 
0198 QDBusConnection DBusConnection::orgBluez()
0199 {
0200     if (globalData->testRun) {
0201         return QDBusConnection::sessionBus();
0202     }
0203     return QDBusConnection::systemBus();
0204 }
0205 
0206 QDBusConnection DBusConnection::orgBluezObex()
0207 {
0208     return QDBusConnection::sessionBus();
0209 }
0210 
0211 Manager *Instance::manager()
0212 {
0213     return globalData->manager;
0214 }
0215 
0216 void Instance::setManager(Manager *manager)
0217 {
0218     globalData->manager = manager;
0219 }
0220 
0221 ObexManager *Instance::obexManager()
0222 {
0223     return globalData->obexManager;
0224 }
0225 
0226 void Instance::setObexManager(ObexManager *obexManager)
0227 {
0228     globalData->obexManager = obexManager;
0229 }
0230 
0231 QStringList stringListToUpper(const QStringList &list)
0232 {
0233     QStringList converted;
0234     converted.reserve(list.size());
0235     for (const QString &str : list) {
0236         converted.append(str.toUpper());
0237     }
0238     return converted;
0239 }
0240 
0241 ManData variantToManData(const QVariant &v) {
0242     // Map to return
0243     ManData manData;
0244 
0245     if (!v.isValid()) {
0246         return manData;
0247     }
0248 
0249     // Map with QVariant values
0250     QMap<uint16_t,QVariant> vMap;
0251     const QDBusArgument &dbusArgs = v.value<QDBusArgument>();
0252     dbusArgs >> vMap;
0253     // Iterate & convert values to byte array
0254     QMap<uint16_t,QVariant>::const_iterator i;
0255     for (i = vMap.constBegin(); i != vMap.constEnd(); ++i) {
0256         const QVariant &value = i.value();
0257         const uint16_t &key = i.key();
0258         manData.insert(key,value.toByteArray());
0259     }
0260     return manData;
0261 }
0262 
0263 Device::Type classToType(quint32 classNum)
0264 {
0265     switch ((classNum & 0x1f00) >> 8) {
0266     case 0x01:
0267         return Device::Computer;
0268     case 0x02:
0269         switch ((classNum & 0xfc) >> 2) {
0270         case 0x04:
0271             return Device::Modem;
0272         default:
0273             return Device::Phone;
0274         }
0275     case 0x03:
0276         return Device::Network;
0277     case 0x04:
0278         switch ((classNum & 0xfc) >> 2) {
0279         case 0x01:
0280         case 0x02:
0281             return Device::Headset;
0282         case 0x06:
0283             return Device::Headphones;
0284         default:
0285             return Device::AudioVideo;
0286         }
0287     case 0x05:
0288         switch ((classNum & 0xc0) >> 6) {
0289         case 0x00:
0290             switch ((classNum & 0x1e) >> 2) {
0291             case 0x01:
0292             case 0x02:
0293                 return Device::Joypad;
0294             }
0295             break;
0296         case 0x01:
0297             return Device::Keyboard;
0298         case 0x02:
0299             switch ((classNum & 0x1e) >> 2) {
0300             case 0x05:
0301                 return Device::Tablet;
0302             default:
0303                 return Device::Mouse;
0304             }
0305         }
0306         return Device::Peripheral;
0307     case 0x06:
0308         if (classNum & 0x80) {
0309             return Device::Printer;
0310         } else if (classNum & 0x20) {
0311             return Device::Camera;
0312         }
0313         return Device::Imaging;
0314     case 0x07:
0315         return Device::Wearable;
0316     case 0x08:
0317         return Device::Toy;
0318     case 0x09:
0319         return Device::Health;
0320     default:
0321         return Device::Uncategorized;
0322     }
0323 }
0324 
0325 Device::Type appearanceToType(quint16 appearance)
0326 {
0327     switch ((appearance & 0xffc0) >> 6) {
0328     case 0x00:
0329         return Device::Uncategorized;
0330     case 0x01: // Generic Phone
0331         return Device::Phone;
0332     case 0x02: // Generic Computer
0333         return Device::Computer;
0334     case 0x05: // Generic Display
0335         return Device::AudioVideo;
0336     case 0x0a: // Generic Media Player
0337         return Device::AudioVideo;
0338     case 0x0b: // Generic Barcode Scanner
0339         return Device::Peripheral;
0340     case 0x0f: // Generic HID
0341         switch (appearance & 0x3f) {
0342         case 0x01: // Keyboard
0343             return Device::Keyboard;
0344         case 0x02: // Mouse
0345             return Device::Mouse;
0346         case 0x03: // Joystick
0347         case 0x04: // Gamepad
0348             return Device::Joypad;
0349         case 0x05: // Digitizer Tablet
0350             return Device::Tablet;
0351         case 0x08: // Barcode Scanner
0352             return Device::Peripheral;
0353         }
0354         // fall-through
0355     default:
0356         return Device::Uncategorized;
0357     }
0358 }
0359 
0360 } // namespace BluezQt