| 1 | /* |
| 2 | * BluezQt - Asynchronous Bluez wrapper library |
| 3 | * |
| 4 | * SPDX-FileCopyrightText: 2014 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 "adapter_p.h" |
| 10 | #include "adapter.h" |
| 11 | #include "gattmanager.h" |
| 12 | #include "leadvertisingmanager.h" |
| 13 | #include "leadvertisingmanager_p.h" |
| 14 | #include "macros.h" |
| 15 | #include "media.h" |
| 16 | #include "media_p.h" |
| 17 | #include "utils.h" |
| 18 | |
| 19 | namespace BluezQt |
| 20 | { |
| 21 | AdapterPrivate::AdapterPrivate(const QString &path, const QVariantMap &properties) |
| 22 | : QObject() |
| 23 | , m_dbusProperties(nullptr) |
| 24 | , m_adapterClass(0) |
| 25 | , m_powered(0) |
| 26 | , m_discoverable(false) |
| 27 | , m_discoverableTimeout(0) |
| 28 | , m_pairable(false) |
| 29 | , m_pairableTimeout(0) |
| 30 | { |
| 31 | m_bluezAdapter = new BluezAdapter(Strings::orgBluez(), path, DBusConnection::orgBluez(), this); |
| 32 | |
| 33 | init(properties); |
| 34 | } |
| 35 | |
| 36 | void AdapterPrivate::init(const QVariantMap &properties) |
| 37 | { |
| 38 | m_dbusProperties = new DBusProperties(Strings::orgBluez(), m_bluezAdapter->path(), DBusConnection::orgBluez(), this); |
| 39 | |
| 40 | // Init properties |
| 41 | m_address = properties.value(QStringLiteral("Address" )).toString(); |
| 42 | m_name = properties.value(QStringLiteral("Name" )).toString(); |
| 43 | m_alias = properties.value(QStringLiteral("Alias" )).toString(); |
| 44 | m_adapterClass = properties.value(QStringLiteral("Class" )).toUInt(); |
| 45 | m_powered = properties.value(QStringLiteral("Powered" )).toBool(); |
| 46 | m_discoverable = properties.value(QStringLiteral("Discoverable" )).toBool(); |
| 47 | m_discoverableTimeout = properties.value(QStringLiteral("DiscoverableTimeout" )).toUInt(); |
| 48 | m_pairable = properties.value(QStringLiteral("Pairable" )).toBool(); |
| 49 | m_pairableTimeout = properties.value(QStringLiteral("PairableTimeout" )).toUInt(); |
| 50 | m_discovering = properties.value(QStringLiteral("Discovering" )).toBool(); |
| 51 | m_uuids = stringListToUpper(list: properties.value(QStringLiteral("UUIDs" )).toStringList()); |
| 52 | m_modalias = properties.value(QStringLiteral("Modalias" )).toString(); |
| 53 | } |
| 54 | |
| 55 | void AdapterPrivate::interfacesAdded(const QString &path, const QVariantMapMap &interfaces) |
| 56 | { |
| 57 | bool changed = false; |
| 58 | |
| 59 | for (auto it = interfaces.cbegin(); it != interfaces.cend(); ++it) { |
| 60 | if (it.key() == Strings::orgBluezMedia1()) { |
| 61 | m_media = MediaPtr(new Media(path)); |
| 62 | Q_EMIT q.lock()->mediaChanged(media: m_media); |
| 63 | changed = true; |
| 64 | } else if (it.key() == Strings::orgBluezLEAdvertisingManager1()) { |
| 65 | m_leAdvertisingManager = LEAdvertisingManagerPtr(new LEAdvertisingManager(path)); |
| 66 | Q_EMIT q.lock()->leAdvertisingManagerChanged(leAdvertisingManager: m_leAdvertisingManager); |
| 67 | changed = true; |
| 68 | } else if (it.key() == Strings::orgBluezGattManager1()) { |
| 69 | m_gattManager = GattManagerPtr(new GattManager(path)); |
| 70 | Q_EMIT q.lock()->gattManagerChanged(gattManager: m_gattManager); |
| 71 | changed = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (changed) { |
| 76 | Q_EMIT q.lock()->adapterChanged(adapter: q.toStrongRef()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void AdapterPrivate::interfacesRemoved(const QString &path, const QStringList &interfaces) |
| 81 | { |
| 82 | bool changed = false; |
| 83 | |
| 84 | for (const QString &interface : interfaces) { |
| 85 | if (interface == Strings::orgBluezMedia1() && m_media && m_media->d->m_path == path) { |
| 86 | m_media.clear(); |
| 87 | Q_EMIT q.lock()->mediaChanged(media: m_media); |
| 88 | changed = true; |
| 89 | } else if (interface == Strings::orgBluezLEAdvertisingManager1() && m_leAdvertisingManager && m_leAdvertisingManager->d->m_path == path) { |
| 90 | m_leAdvertisingManager.clear(); |
| 91 | Q_EMIT q.lock()->leAdvertisingManagerChanged(leAdvertisingManager: m_leAdvertisingManager); |
| 92 | changed = true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (changed) { |
| 97 | Q_EMIT q.lock()->adapterChanged(adapter: q.toStrongRef()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void AdapterPrivate::addDevice(const DevicePtr &device) |
| 102 | { |
| 103 | m_devices.append(t: device); |
| 104 | Q_EMIT q.lock()->deviceAdded(device); |
| 105 | |
| 106 | connect(sender: device.data(), signal: &Device::deviceChanged, context: q.lock().data(), slot: &Adapter::deviceChanged); |
| 107 | } |
| 108 | |
| 109 | void AdapterPrivate::removeDevice(const DevicePtr &device) |
| 110 | { |
| 111 | m_devices.removeOne(t: device); |
| 112 | Q_EMIT device->deviceRemoved(device); |
| 113 | Q_EMIT q.lock()->deviceRemoved(device); |
| 114 | |
| 115 | disconnect(sender: device.data(), signal: &Device::deviceChanged, receiver: q.lock().data(), slot: &Adapter::deviceChanged); |
| 116 | } |
| 117 | |
| 118 | QDBusPendingReply<> AdapterPrivate::setDBusProperty(const QString &name, const QVariant &value) |
| 119 | { |
| 120 | return m_dbusProperties->Set(interface_name: Strings::orgBluezAdapter1(), property_name: name, value: QDBusVariant(value)); |
| 121 | } |
| 122 | |
| 123 | void AdapterPrivate::propertiesChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated) |
| 124 | { |
| 125 | if (interface != Strings::orgBluezAdapter1()) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | QVariantMap::const_iterator i; |
| 130 | for (i = changed.constBegin(); i != changed.constEnd(); ++i) { |
| 131 | const QVariant &value = i.value(); |
| 132 | const QString &property = i.key(); |
| 133 | |
| 134 | if (property == QLatin1String("Name" )) { |
| 135 | PROPERTY_CHANGED(m_name, toString, systemNameChanged); |
| 136 | } else if (property == QLatin1String("Alias" )) { |
| 137 | PROPERTY_CHANGED(m_alias, toString, nameChanged); |
| 138 | } else if (property == QLatin1String("Class" )) { |
| 139 | PROPERTY_CHANGED(m_adapterClass, toUInt, adapterClassChanged); |
| 140 | } else if (property == QLatin1String("Powered" )) { |
| 141 | PROPERTY_CHANGED(m_powered, toBool, poweredChanged); |
| 142 | } else if (property == QLatin1String("Discoverable" )) { |
| 143 | PROPERTY_CHANGED(m_discoverable, toBool, discoverableChanged); |
| 144 | } else if (property == QLatin1String("DiscoverableTimeout" )) { |
| 145 | PROPERTY_CHANGED(m_discoverableTimeout, toUInt, discoverableTimeoutChanged); |
| 146 | } else if (property == QLatin1String("Pairable" )) { |
| 147 | PROPERTY_CHANGED(m_pairable, toBool, pairableChanged); |
| 148 | } else if (property == QLatin1String("PairableTimeout" )) { |
| 149 | PROPERTY_CHANGED(m_pairableTimeout, toUInt, pairableTimeoutChanged); |
| 150 | } else if (property == QLatin1String("Discovering" )) { |
| 151 | PROPERTY_CHANGED(m_discovering, toBool, discoveringChanged); |
| 152 | } else if (property == QLatin1String("Modalias" )) { |
| 153 | PROPERTY_CHANGED(m_modalias, toString, modaliasChanged); |
| 154 | } else if (property == QLatin1String("UUIDs" )) { |
| 155 | PROPERTY_CHANGED2(m_uuids, stringListToUpper(value.toStringList()), uuidsChanged); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | for (const QString &property : invalidated) { |
| 160 | if (property == QLatin1String("Modalias" )) { |
| 161 | PROPERTY_INVALIDATED(m_modalias, QString(), modaliasChanged); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Q_EMIT q.lock()->adapterChanged(adapter: q.toStrongRef()); |
| 166 | } |
| 167 | |
| 168 | } // namespace BluezQt |
| 169 | |
| 170 | #include "moc_adapter_p.cpp" |
| 171 | |