File indexing completed on 2024-04-21 03:51:50

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 "adapterinterface.h"
0008 #include "gattmanagerinterface.h"
0009 #include "leadvertisingmanagerinterface.h"
0010 #include "mediainterface.h"
0011 #include "objectmanager.h"
0012 
0013 #include <QDBusConnection>
0014 #include <QTimer>
0015 
0016 // AdapterObject
0017 AdapterObject::AdapterObject(const QDBusObjectPath &path, QObject *parent)
0018     : QObject(parent)
0019 {
0020     QDBusConnection::sessionBus().registerObject(path.path(), this);
0021 }
0022 
0023 // AdapterInterface
0024 AdapterInterface::AdapterInterface(const QDBusObjectPath &path, const QVariantMap &properties, QObject *parent)
0025     : QDBusAbstractAdaptor(parent)
0026 {
0027     setPath(path);
0028     setObjectParent(parent);
0029     setProperties(properties);
0030     setName(QStringLiteral("org.bluez.Adapter1"));
0031 
0032     // Alias needs special handling
0033     setAlias(properties.value(QStringLiteral("Alias")).toString());
0034 
0035     // Media interface
0036     m_media = new MediaInterface(path, parent);
0037     ObjectManager *manager = ObjectManager::self();
0038     manager->addObject(m_media);
0039 
0040     // LEAdvertisingManager interface
0041     m_leAdvertisingManager = new LEAdvertisingManagerInterface(path, parent);
0042     manager->addObject(m_leAdvertisingManager);
0043 
0044     // GattManager interface
0045     m_gattManager = new GattManagerInterface(path, parent);
0046     manager->addObject(m_gattManager);
0047 }
0048 
0049 QString AdapterInterface::address() const
0050 {
0051     return Object::property(QStringLiteral("Address")).toString();
0052 }
0053 
0054 QString AdapterInterface::name() const
0055 {
0056     return Object::property(QStringLiteral("Name")).toString();
0057 }
0058 
0059 QString AdapterInterface::alias() const
0060 {
0061     return Object::property(QStringLiteral("Alias")).toString();
0062 }
0063 
0064 void AdapterInterface::setAlias(const QString &alias)
0065 {
0066     Object::changeProperty(QStringLiteral("Alias"), alias.isEmpty() ? name() : alias);
0067 }
0068 
0069 quint32 AdapterInterface::adapterClass() const
0070 {
0071     return Object::property(QStringLiteral("Class")).toUInt();
0072 }
0073 
0074 bool AdapterInterface::powered() const
0075 {
0076     return Object::property(QStringLiteral("Powered")).toBool();
0077 }
0078 
0079 void AdapterInterface::setPowered(bool powered)
0080 {
0081     Object::changeProperty(QStringLiteral("Powered"), powered);
0082 }
0083 
0084 bool AdapterInterface::discoverable() const
0085 {
0086     return Object::property(QStringLiteral("Discoverable")).toBool();
0087 }
0088 
0089 void AdapterInterface::setDiscoverable(bool discoverable)
0090 {
0091     Object::changeProperty(QStringLiteral("Discoverable"), discoverable);
0092 
0093     if (discoverable && discoverableTimeout() != 0) {
0094         QTimer::singleShot(discoverableTimeout() * 1000, this, SLOT(resetDiscoverable()));
0095     }
0096 }
0097 
0098 bool AdapterInterface::pairable() const
0099 {
0100     return Object::property(QStringLiteral("Pairable")).toBool();
0101 }
0102 
0103 void AdapterInterface::setPairable(bool pairable)
0104 {
0105     Object::changeProperty(QStringLiteral("Pairable"), pairable);
0106 
0107     if (pairable && pairableTimeout() != 0) {
0108         QTimer::singleShot(pairableTimeout() * 1000, this, SLOT(resetPairable()));
0109     }
0110 }
0111 
0112 quint32 AdapterInterface::pairableTimeout() const
0113 {
0114     return Object::property(QStringLiteral("PairableTimeout")).toUInt();
0115 }
0116 
0117 void AdapterInterface::setPairableTimeout(quint32 timeout)
0118 {
0119     Object::changeProperty(QStringLiteral("PairableTimeout"), timeout);
0120 }
0121 
0122 quint32 AdapterInterface::discoverableTimeout() const
0123 {
0124     return Object::property(QStringLiteral("DiscoverableTimeout")).toUInt();
0125 }
0126 
0127 void AdapterInterface::setDiscoverableTimeout(quint32 timeout)
0128 {
0129     Object::changeProperty(QStringLiteral("DiscoverableTimeout"), timeout);
0130 }
0131 
0132 bool AdapterInterface::discovering() const
0133 {
0134     return Object::property(QStringLiteral("Discovering")).toBool();
0135 }
0136 
0137 QStringList AdapterInterface::uuids() const
0138 {
0139     return Object::property(QStringLiteral("UUIDs")).toStringList();
0140 }
0141 
0142 QString AdapterInterface::modalias() const
0143 {
0144     return Object::property(QStringLiteral("Modalias")).toString();
0145 }
0146 
0147 MediaInterface *AdapterInterface::media() const
0148 {
0149     return m_media;
0150 }
0151 
0152 LEAdvertisingManagerInterface *AdapterInterface::leAdvertisingManager() const
0153 {
0154     return m_leAdvertisingManager;
0155 }
0156 
0157 GattManagerInterface *AdapterInterface::gattManager() const
0158 {
0159     return m_gattManager;
0160 }
0161 
0162 void AdapterInterface::StartDiscovery()
0163 {
0164     Object::changeProperty(QStringLiteral("Discovering"), true);
0165 }
0166 
0167 void AdapterInterface::StopDiscovery()
0168 {
0169     Object::changeProperty(QStringLiteral("Discovering"), false);
0170 }
0171 
0172 void AdapterInterface::RemoveDevice(const QDBusObjectPath &device)
0173 {
0174     ObjectManager *manager = ObjectManager::self();
0175     manager->removeObject(manager->objectByPath(device));
0176 }
0177 
0178 void AdapterInterface::SetDiscoveryFilter(const QVariantMap &filter)
0179 {
0180     // Bluez makes adapter discoverable while discovering if 'Discoverable' option is set
0181     if (filter.contains(QStringLiteral("Discoverable")) && filter.value(QStringLiteral("Discoverable")).toBool())
0182     {
0183         Object::changeProperty(QStringLiteral("Discoverable"), true);
0184     }
0185 }
0186 
0187 QStringList AdapterInterface::GetDiscoveryFilters()
0188 {
0189     return {
0190         QStringLiteral("UUIDs"),
0191         QStringLiteral("RSSI"),
0192         QStringLiteral("Pathloss"),
0193         QStringLiteral("Transport"),
0194         QStringLiteral("DuplicateData"),
0195         QStringLiteral("Discoverable"),
0196         QStringLiteral("Pattern")
0197     };
0198 }
0199 
0200 void AdapterInterface::resetPairable()
0201 {
0202     setPairable(false);
0203 }
0204 
0205 void AdapterInterface::resetDiscoverable()
0206 {
0207     setDiscoverable(false);
0208 }
0209 
0210 #include "moc_adapterinterface.cpp"