File indexing completed on 2024-10-06 12:15:31
0001 /* 0002 * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "devicemanager.h" 0008 #include "adapterinterface.h" 0009 #include "deviceinterface.h" 0010 #include "gattserviceinterface.h" 0011 #include "gattcharacteristicinterface.h" 0012 #include "gattdescriptorinterface.h" 0013 #include "gattmanagerinterface.h" 0014 #include "leadvertisingmanagerinterface.h" 0015 #include "mediainterface.h" 0016 #include "mediatransportinterface.h" 0017 #include "objectmanager.h" 0018 0019 DeviceManager::DeviceManager(ObjectManager *parent) 0020 : QObject(parent) 0021 , m_objectManager(parent) 0022 { 0023 } 0024 0025 void DeviceManager::runAction(const QString &actionName, const QVariantMap &properties) 0026 { 0027 if (actionName == QLatin1String("create-adapter")) { 0028 runCreateAdapterAction(properties); 0029 } else if (actionName == QLatin1String("create-device")) { 0030 runCreateDeviceAction(properties); 0031 } else if (actionName == QLatin1String("create-gatt-service")) { 0032 runCreateGattServiceAction(properties); 0033 } else if (actionName == QLatin1String("create-gatt-characteristic")) { 0034 runCreateGattCharacteristicAction(properties); 0035 } else if (actionName == QLatin1String("create-gatt-descriptor")) { 0036 runCreateGattDescriptorAction(properties); 0037 } else if (actionName == QLatin1String("remove-adapter")) { 0038 runRemoveAdapterAction(properties); 0039 } else if (actionName == QLatin1String("remove-device")) { 0040 runRemoveDeviceAction(properties); 0041 } else if (actionName == QLatin1String("remove-gatt-service")) { 0042 runRemoveGattServiceAction(properties); 0043 } else if (actionName == QLatin1String("remove-gatt-characteristic")) { 0044 runRemoveGattCharacteristicAction(properties); 0045 } else if (actionName == QLatin1String("remove-gatt-descriptor")) { 0046 runRemoveGattDescriptorAction(properties); 0047 } else if (actionName == QLatin1String("change-adapter-property")) { 0048 runChangeAdapterProperty(properties); 0049 } else if (actionName == QLatin1String("change-device-property")) { 0050 runChangeDeviceProperty(properties); 0051 } else if (actionName.startsWith(QLatin1String("adapter-media:"))) { 0052 runAdapterMediaAction(actionName.mid(14), properties); 0053 } else if (actionName.startsWith(QLatin1String("adapter-leadvertisingmanager:"))) { 0054 runAdapterLeAdvertisingManagerAction(actionName.mid(29), properties); 0055 } else if (actionName.startsWith(QLatin1String("adapter-gattmanager:"))) { 0056 runAdapterGattManagerAction(actionName.mid(20), properties); 0057 } else if (actionName == QLatin1String("bug377405")) { 0058 runBug377405(); 0059 } else if (actionName == QLatin1String("bug403289")) { 0060 runBug403289(properties); 0061 } 0062 } 0063 0064 void DeviceManager::runCreateAdapterAction(const QVariantMap &properties) 0065 { 0066 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0067 QVariantMap props = properties; 0068 props.remove(QStringLiteral("Path")); 0069 0070 AdapterObject *adapterObj = new AdapterObject(path); 0071 AdapterInterface *adapter = new AdapterInterface(path, props, adapterObj); 0072 m_objectManager->addObject(adapter); 0073 m_objectManager->addAutoDeleteObject(adapterObj); 0074 } 0075 0076 void DeviceManager::runCreateDeviceAction(const QVariantMap &properties) 0077 { 0078 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0079 QVariantMap props = properties; 0080 props.remove(QStringLiteral("Path")); 0081 0082 DeviceObject *deviceObj = new DeviceObject(path); 0083 DeviceInterface *device = new DeviceInterface(path, props, deviceObj); 0084 m_objectManager->addObject(device); 0085 m_objectManager->addAutoDeleteObject(deviceObj); 0086 } 0087 0088 void DeviceManager::runCreateGattServiceAction(const QVariantMap& properties) 0089 { 0090 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0091 QVariantMap props = properties; 0092 props.remove(QStringLiteral("Path")); 0093 0094 GattServiceObject *serviceObj = new GattServiceObject(path); 0095 GattServiceInterface *service = new GattServiceInterface(path, props, serviceObj); 0096 m_objectManager->addObject(service); 0097 m_objectManager->addAutoDeleteObject(serviceObj); 0098 } 0099 0100 void DeviceManager::runCreateGattCharacteristicAction(const QVariantMap& properties) 0101 { 0102 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0103 QVariantMap props = properties; 0104 props.remove(QStringLiteral("Path")); 0105 0106 GattCharacteristicObject *characteristicObj = new GattCharacteristicObject(path); 0107 GattCharacteristicInterface *characteristic = new GattCharacteristicInterface(path, props, characteristicObj); 0108 m_objectManager->addObject(characteristic); 0109 m_objectManager->addAutoDeleteObject(characteristicObj); 0110 } 0111 0112 void DeviceManager::runCreateGattDescriptorAction(const QVariantMap& properties) 0113 { 0114 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0115 QVariantMap props = properties; 0116 props.remove(QStringLiteral("Path")); 0117 0118 GattDescriptorObject *descriptorObj = new GattDescriptorObject(path); 0119 GattDescriptorInterface *descriptor = new GattDescriptorInterface(path, props, descriptorObj); 0120 m_objectManager->addObject(descriptor); 0121 m_objectManager->addAutoDeleteObject(descriptorObj); 0122 } 0123 0124 void DeviceManager::runRemoveAdapterAction(const QVariantMap &properties) 0125 { 0126 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0127 m_objectManager->removeObject(m_objectManager->objectByPath(path)); 0128 } 0129 0130 void DeviceManager::runRemoveDeviceAction(const QVariantMap &properties) 0131 { 0132 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0133 m_objectManager->removeObject(m_objectManager->objectByPath(path)); 0134 } 0135 0136 void DeviceManager::runRemoveGattServiceAction(const QVariantMap& properties) 0137 { 0138 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0139 m_objectManager->removeObject(m_objectManager->objectByPath(path)); 0140 } 0141 0142 void DeviceManager::runRemoveGattCharacteristicAction(const QVariantMap& properties) 0143 { 0144 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0145 m_objectManager->removeObject(m_objectManager->objectByPath(path)); 0146 } 0147 0148 void DeviceManager::runRemoveGattDescriptorAction(const QVariantMap& properties) 0149 { 0150 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0151 m_objectManager->removeObject(m_objectManager->objectByPath(path)); 0152 } 0153 0154 void DeviceManager::runChangeAdapterProperty(const QVariantMap &properties) 0155 { 0156 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0157 Object *adapter = m_objectManager->objectByPath(path); 0158 if (!adapter || adapter->name() != QLatin1String("org.bluez.Adapter1")) { 0159 return; 0160 } 0161 0162 adapter->changeProperty(properties.value(QStringLiteral("Name")).toString(), properties.value(QStringLiteral("Value"))); 0163 } 0164 0165 void DeviceManager::runChangeDeviceProperty(const QVariantMap &properties) 0166 { 0167 const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); 0168 Object *device = m_objectManager->objectByPath(path); 0169 if (!device || device->name() != QLatin1String("org.bluez.Device1")) { 0170 return; 0171 } 0172 0173 device->changeProperty(properties.value(QStringLiteral("Name")).toString(), properties.value(QStringLiteral("Value"))); 0174 } 0175 0176 void DeviceManager::runAdapterMediaAction(const QString action, const QVariantMap &properties) 0177 { 0178 const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); 0179 AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); 0180 if (!adapter) { 0181 return; 0182 } 0183 adapter->media()->runAction(action, properties); 0184 } 0185 0186 void DeviceManager::runAdapterLeAdvertisingManagerAction(const QString action, const QVariantMap &properties) 0187 { 0188 const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); 0189 AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); 0190 if (!adapter) { 0191 return; 0192 } 0193 adapter->leAdvertisingManager()->runAction(action, properties); 0194 } 0195 0196 void DeviceManager::runAdapterGattManagerAction(const QString action, const QVariantMap &properties) 0197 { 0198 const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); 0199 AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); 0200 if (!adapter) { 0201 return; 0202 } 0203 adapter->gattManager()->runAction(action, properties); 0204 } 0205 0206 void DeviceManager::runBug377405() 0207 { 0208 QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); 0209 QVariantMap adapterProps; 0210 adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); 0211 adapterProps[QStringLiteral("Powered")] = false; 0212 0213 runCreateAdapterAction(adapterProps); 0214 0215 QVariantMap properties; 0216 properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); 0217 properties[QStringLiteral("Name")] = QStringLiteral("Powered"); 0218 properties[QStringLiteral("Value")] = true; 0219 0220 runChangeAdapterProperty(properties); 0221 } 0222 0223 void DeviceManager::runBug403289(const QVariantMap &properties) 0224 { 0225 const QDBusObjectPath &path = properties.value(QStringLiteral("DevicePath")).value<QDBusObjectPath>(); 0226 DeviceInterface *device = dynamic_cast<DeviceInterface *>(m_objectManager->objectByPath(path)); 0227 if (!device) { 0228 return; 0229 } 0230 device->connectMediaPlayer(); 0231 Q_EMIT m_objectManager->InterfacesRemoved(path, QStringList(QStringLiteral("org.bluez.MediaPlayer1"))); 0232 } 0233 0234 #include "moc_devicemanager.cpp"