File indexing completed on 2024-04-28 03:52:02

0001 /*
0002  * BluezQt - Asynchronous Bluez wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "gattapplication.h"
0010 
0011 #include "gattapplication_p.h"
0012 #include "gattcharacteristic.h"
0013 #include "gattcharacteristicadaptor.h"
0014 #include "gattdescriptor.h"
0015 #include "gattdescriptoradaptor.h"
0016 #include "gattservice.h"
0017 #include "gattserviceadaptor.h"
0018 
0019 #include <QDBusObjectPath>
0020 #include <QMetaProperty>
0021 
0022 namespace BluezQt
0023 {
0024 GattApplication::GattApplication(QObject *parent)
0025     : GattApplication(QStringLiteral("/org/kde/bluezqt"), parent)
0026 {
0027 }
0028 
0029 GattApplication::GattApplication(const QString &objectPathPrefix, QObject *parent)
0030     : QObject(parent)
0031     , d(new GattApplicationPrivate(objectPathPrefix, this))
0032 {
0033 }
0034 
0035 GattApplication::~GattApplication() = default;
0036 
0037 DBusManagerStruct GattApplicationPrivate::getManagedObjects() const
0038 {
0039     DBusManagerStruct objects;
0040 
0041     const auto serviceAdaptors = q->findChildren<GattServiceAdaptor *>();
0042     const auto charcAdaptors = q->findChildren<GattCharacteristicAdaptor *>();
0043     const auto descriptorAdaptors = q->findChildren<GattDescriptorAdaptor *>();
0044 
0045     for (const GattServiceAdaptor *serviceAdaptor : serviceAdaptors) {
0046         QVariantMap properties;
0047         for (int i = serviceAdaptor->metaObject()->propertyOffset(); i < serviceAdaptor->metaObject()->propertyCount(); ++i) {
0048             auto propertyName = serviceAdaptor->metaObject()->property(i).name();
0049             properties.insert(QString::fromLatin1(propertyName), serviceAdaptor->property(propertyName));
0050         }
0051 
0052         GattService *service = qobject_cast<GattService *>(serviceAdaptor->parent());
0053         if (service) {
0054             objects[service->objectPath()].insert(QStringLiteral("org.bluez.GattService1"), properties);
0055         }
0056     }
0057 
0058     for (const GattCharacteristicAdaptor *charcAdaptor : charcAdaptors) {
0059         QVariantMap properties;
0060         for (int i = charcAdaptor->metaObject()->propertyOffset(); i < charcAdaptor->metaObject()->propertyCount(); ++i) {
0061             auto propertyName = charcAdaptor->metaObject()->property(i).name();
0062             properties.insert(QString::fromLatin1(propertyName), charcAdaptor->property(propertyName));
0063         }
0064 
0065         GattCharacteristic *charc = qobject_cast<GattCharacteristic *>(charcAdaptor->parent());
0066         if (charc) {
0067             objects[charc->objectPath()].insert(QStringLiteral("org.bluez.GattCharacteristic1"), properties);
0068         }
0069     }
0070 
0071     for (const GattDescriptorAdaptor *descAdaptor : descriptorAdaptors) {
0072         QVariantMap properties;
0073         for (int i = descAdaptor->metaObject()->propertyOffset(); i < descAdaptor->metaObject()->propertyCount(); ++i) {
0074             auto propertyName = descAdaptor->metaObject()->property(i).name();
0075             properties.insert(QString::fromLatin1(propertyName), descAdaptor->property(propertyName));
0076         }
0077 
0078         GattDescriptor *desc = qobject_cast<GattDescriptor *>(descAdaptor->parent());
0079         if (desc) {
0080             objects[desc->objectPath()].insert(QStringLiteral("org.bluez.GattDescriptor1"), properties);
0081         }
0082     }
0083 
0084     return objects;
0085 }
0086 
0087 QDBusObjectPath GattApplication::objectPath() const
0088 {
0089     return d->m_objectPath;
0090 }
0091 
0092 } // namespace BluezQt
0093 
0094 #include "moc_gattapplication.cpp"