| 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 "devicemanager.h" |
| 8 |
#include "adapterinterface.h" |
| 9 |
#include "deviceinterface.h" |
| 10 |
#include "gattserviceinterface.h" |
| 11 |
#include "gattcharacteristicinterface.h" |
| 12 |
#include "gattdescriptorinterface.h" |
| 13 |
#include "gattmanagerinterface.h" |
| 14 |
#include "leadvertisingmanagerinterface.h" |
| 15 |
#include "mediainterface.h" |
| 16 |
#include "mediatransportinterface.h" |
| 17 |
#include "objectmanager.h" |
| 18 |
|
| 19 |
DeviceManager::DeviceManager(ObjectManager *parent) |
| 20 |
: QObject(parent) |
| 21 |
, m_objectManager(parent) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
void DeviceManager::runAction(const QString &actionName, const QVariantMap &properties) |
| 26 |
{ |
| 27 |
if (actionName == QLatin1String("create-adapter")) { |
| 28 |
runCreateAdapterAction(properties); |
| 29 |
} else if (actionName == QLatin1String("create-device")) { |
| 30 |
runCreateDeviceAction(properties); |
| 31 |
} else if (actionName == QLatin1String("create-gatt-service")) { |
| 32 |
runCreateGattServiceAction(properties); |
| 33 |
} else if (actionName == QLatin1String("create-gatt-characteristic")) { |
| 34 |
runCreateGattCharacteristicAction(properties); |
| 35 |
} else if (actionName == QLatin1String("create-gatt-descriptor")) { |
| 36 |
runCreateGattDescriptorAction(properties); |
| 37 |
} else if (actionName == QLatin1String("remove-adapter")) { |
| 38 |
runRemoveAdapterAction(properties); |
| 39 |
} else if (actionName == QLatin1String("remove-device")) { |
| 40 |
runRemoveDeviceAction(properties); |
| 41 |
} else if (actionName == QLatin1String("remove-gatt-service")) { |
| 42 |
runRemoveGattServiceAction(properties); |
| 43 |
} else if (actionName == QLatin1String("remove-gatt-characteristic")) { |
| 44 |
runRemoveGattCharacteristicAction(properties); |
| 45 |
} else if (actionName == QLatin1String("remove-gatt-descriptor")) { |
| 46 |
runRemoveGattDescriptorAction(properties); |
| 47 |
} else if (actionName == QLatin1String("change-adapter-property")) { |
| 48 |
runChangeAdapterProperty(properties); |
| 49 |
} else if (actionName == QLatin1String("change-device-property")) { |
| 50 |
runChangeDeviceProperty(properties); |
| 51 |
} else if (actionName.startsWith(QLatin1String("adapter-media:"))) { |
| 52 |
runAdapterMediaAction(actionName.mid(14), properties); |
| 53 |
} else if (actionName.startsWith(QLatin1String("adapter-leadvertisingmanager:"))) { |
| 54 |
runAdapterLeAdvertisingManagerAction(actionName.mid(29), properties); |
| 55 |
} else if (actionName.startsWith(QLatin1String("adapter-gattmanager:"))) { |
| 56 |
runAdapterGattManagerAction(actionName.mid(20), properties); |
| 57 |
} else if (actionName == QLatin1String("bug377405")) { |
| 58 |
runBug377405(); |
| 59 |
} else if (actionName == QLatin1String("bug403289")) { |
| 60 |
runBug403289(properties); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void DeviceManager::runCreateAdapterAction(const QVariantMap &properties) |
| 65 |
{ |
| 66 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 67 |
QVariantMap props = properties; |
| 68 |
props.remove(QStringLiteral("Path")); |
| 69 |
|
| 70 |
AdapterObject *adapterObj = new AdapterObject(path); |
| 71 |
AdapterInterface *adapter = new AdapterInterface(path, props, adapterObj); |
| 72 |
m_objectManager->addObject(adapter); |
| 73 |
m_objectManager->addAutoDeleteObject(adapterObj); |
| 74 |
} |
| 75 |
|
| 76 |
void DeviceManager::runCreateDeviceAction(const QVariantMap &properties) |
| 77 |
{ |
| 78 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 79 |
QVariantMap props = properties; |
| 80 |
props.remove(QStringLiteral("Path")); |
| 81 |
|
| 82 |
DeviceObject *deviceObj = new DeviceObject(path); |
| 83 |
DeviceInterface *device = new DeviceInterface(path, props, deviceObj); |
| 84 |
m_objectManager->addObject(device); |
| 85 |
m_objectManager->addAutoDeleteObject(deviceObj); |
| 86 |
} |
| 87 |
|
| 88 |
void DeviceManager::runCreateGattServiceAction(const QVariantMap& properties) |
| 89 |
{ |
| 90 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 91 |
QVariantMap props = properties; |
| 92 |
props.remove(QStringLiteral("Path")); |
| 93 |
|
| 94 |
GattServiceObject *serviceObj = new GattServiceObject(path); |
| 95 |
GattServiceInterface *service = new GattServiceInterface(path, props, serviceObj); |
| 96 |
m_objectManager->addObject(service); |
| 97 |
m_objectManager->addAutoDeleteObject(serviceObj); |
| 98 |
} |
| 99 |
|
| 100 |
void DeviceManager::runCreateGattCharacteristicAction(const QVariantMap& properties) |
| 101 |
{ |
| 102 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 103 |
QVariantMap props = properties; |
| 104 |
props.remove(QStringLiteral("Path")); |
| 105 |
|
| 106 |
GattCharacteristicObject *characteristicObj = new GattCharacteristicObject(path); |
| 107 |
GattCharacteristicInterface *characteristic = new GattCharacteristicInterface(path, props, characteristicObj); |
| 108 |
m_objectManager->addObject(characteristic); |
| 109 |
m_objectManager->addAutoDeleteObject(characteristicObj); |
| 110 |
} |
| 111 |
|
| 112 |
void DeviceManager::runCreateGattDescriptorAction(const QVariantMap& properties) |
| 113 |
{ |
| 114 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 115 |
QVariantMap props = properties; |
| 116 |
props.remove(QStringLiteral("Path")); |
| 117 |
|
| 118 |
GattDescriptorObject *descriptorObj = new GattDescriptorObject(path); |
| 119 |
GattDescriptorInterface *descriptor = new GattDescriptorInterface(path, props, descriptorObj); |
| 120 |
m_objectManager->addObject(descriptor); |
| 121 |
m_objectManager->addAutoDeleteObject(descriptorObj); |
| 122 |
} |
| 123 |
|
| 124 |
void DeviceManager::runRemoveAdapterAction(const QVariantMap &properties) |
| 125 |
{ |
| 126 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 127 |
m_objectManager->removeObject(m_objectManager->objectByPath(path)); |
| 128 |
} |
| 129 |
|
| 130 |
void DeviceManager::runRemoveDeviceAction(const QVariantMap &properties) |
| 131 |
{ |
| 132 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 133 |
m_objectManager->removeObject(m_objectManager->objectByPath(path)); |
| 134 |
} |
| 135 |
|
| 136 |
void DeviceManager::runRemoveGattServiceAction(const QVariantMap& properties) |
| 137 |
{ |
| 138 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 139 |
m_objectManager->removeObject(m_objectManager->objectByPath(path)); |
| 140 |
} |
| 141 |
|
| 142 |
void DeviceManager::runRemoveGattCharacteristicAction(const QVariantMap& properties) |
| 143 |
{ |
| 144 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 145 |
m_objectManager->removeObject(m_objectManager->objectByPath(path)); |
| 146 |
} |
| 147 |
|
| 148 |
void DeviceManager::runRemoveGattDescriptorAction(const QVariantMap& properties) |
| 149 |
{ |
| 150 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 151 |
m_objectManager->removeObject(m_objectManager->objectByPath(path)); |
| 152 |
} |
| 153 |
|
| 154 |
void DeviceManager::runChangeAdapterProperty(const QVariantMap &properties) |
| 155 |
{ |
| 156 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 157 |
Object *adapter = m_objectManager->objectByPath(path); |
| 158 |
if (!adapter || adapter->name() != QLatin1String("org.bluez.Adapter1")) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
adapter->changeProperty(properties.value(QStringLiteral("Name")).toString(), properties.value(QStringLiteral("Value"))); |
| 163 |
} |
| 164 |
|
| 165 |
void DeviceManager::runChangeDeviceProperty(const QVariantMap &properties) |
| 166 |
{ |
| 167 |
const QDBusObjectPath &path = properties.value(QStringLiteral("Path")).value<QDBusObjectPath>(); |
| 168 |
Object *device = m_objectManager->objectByPath(path); |
| 169 |
if (!device || device->name() != QLatin1String("org.bluez.Device1")) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
device->changeProperty(properties.value(QStringLiteral("Name")).toString(), properties.value(QStringLiteral("Value"))); |
| 174 |
} |
| 175 |
|
| 176 |
void DeviceManager::runAdapterMediaAction(const QString action, const QVariantMap &properties) |
| 177 |
{ |
| 178 |
const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); |
| 179 |
AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); |
| 180 |
if (!adapter) { |
| 181 |
return; |
| 182 |
} |
| 183 |
adapter->media()->runAction(action, properties); |
| 184 |
} |
| 185 |
|
| 186 |
void DeviceManager::runAdapterLeAdvertisingManagerAction(const QString action, const QVariantMap &properties) |
| 187 |
{ |
| 188 |
const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); |
| 189 |
AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); |
| 190 |
if (!adapter) { |
| 191 |
return; |
| 192 |
} |
| 193 |
adapter->leAdvertisingManager()->runAction(action, properties); |
| 194 |
} |
| 195 |
|
| 196 |
void DeviceManager::runAdapterGattManagerAction(const QString action, const QVariantMap &properties) |
| 197 |
{ |
| 198 |
const QDBusObjectPath &path = properties.value(QStringLiteral("AdapterPath")).value<QDBusObjectPath>(); |
| 199 |
AdapterInterface *adapter = dynamic_cast<AdapterInterface *>(m_objectManager->objectByPath(path)); |
| 200 |
if (!adapter) { |
| 201 |
return; |
| 202 |
} |
| 203 |
adapter->gattManager()->runAction(action, properties); |
| 204 |
} |
| 205 |
|
| 206 |
void DeviceManager::runBug377405() |
| 207 |
{ |
| 208 |
QDBusObjectPath adapter1path = QDBusObjectPath(QStringLiteral("/org/bluez/hci0")); |
| 209 |
QVariantMap adapterProps; |
| 210 |
adapterProps[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); |
| 211 |
adapterProps[QStringLiteral("Powered")] = false; |
| 212 |
|
| 213 |
runCreateAdapterAction(adapterProps); |
| 214 |
|
| 215 |
QVariantMap properties; |
| 216 |
properties[QStringLiteral("Path")] = QVariant::fromValue(adapter1path); |
| 217 |
properties[QStringLiteral("Name")] = QStringLiteral("Powered"); |
| 218 |
properties[QStringLiteral("Value")] = true; |
| 219 |
|
| 220 |
runChangeAdapterProperty(properties); |
| 221 |
} |
| 222 |
|
| 223 |
void DeviceManager::runBug403289(const QVariantMap &properties) |
| 224 |
{ |
| 225 |
const QDBusObjectPath &path = properties.value(QStringLiteral("DevicePath")).value<QDBusObjectPath>(); |
| 226 |
DeviceInterface *device = dynamic_cast<DeviceInterface *>(m_objectManager->objectByPath(path)); |
| 227 |
if (!device) { |
| 228 |
return; |
| 229 |
} |
| 230 |
device->connectMediaPlayer(); |
| 231 |
Q_EMIT m_objectManager->InterfacesRemoved(path, QStringList(QStringLiteral("org.bluez.MediaPlayer1"))); |
| 232 |
} |
| 233 |
|
| 234 |
#include "moc_devicemanager.cpp" |