File indexing completed on 2023-09-24 04:02:11

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <mincequi@web.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "gattmanagerinterface.h"
0008 #include "objectmanager.h"
0009 
0010 #include <QDBusConnection>
0011 #include <QDBusMessage>
0012 #include <QDBusPendingCall>
0013 #include <QDBusPendingReply>
0014 
0015 GattManagerInterface::GattManagerInterface(const QDBusObjectPath &path, QObject *parent)
0016     : QDBusAbstractAdaptor(parent)
0017 {
0018     setName(QStringLiteral("org.bluez.GattManager1"));
0019     setPath(path);
0020 }
0021 
0022 void GattManagerInterface::runAction(const QString &actionName, const QVariantMap &properties)
0023 {
0024     if (actionName == QLatin1String("get-objects")) {
0025         runGetObjectsAction();
0026     } else if (actionName == QLatin1String("read-charc")) {
0027         runReadCharcAction(properties);
0028     } else if (actionName == QLatin1String("write-charc")) {
0029         runWriteCharcAction(properties);
0030     }
0031 }
0032 
0033 void GattManagerInterface::RegisterApplication(const QDBusObjectPath &path, const QVariantMap & /*options*/, const QDBusMessage &msg)
0034 {
0035     m_application = path;
0036     m_service = msg.service();
0037 }
0038 
0039 void GattManagerInterface::UnregisterApplication(const QDBusObjectPath &path, const QDBusMessage &msg)
0040 {
0041     if (m_application == path && m_service == msg.service()) {
0042         m_application = QDBusObjectPath();
0043         m_service.clear();
0044     }
0045 }
0046 
0047 void GattManagerInterface::runGetObjectsAction()
0048 {
0049     QDBusMessage call = QDBusMessage::createMethodCall(m_service,
0050                                                        m_application.path(),
0051                                                        QStringLiteral("org.freedesktop.DBus.ObjectManager"),
0052                                                        QStringLiteral("GetManagedObjects"));
0053 
0054     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(call));
0055     connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
0056         const QDBusPendingReply<DBusManagerStruct> &reply = *watcher;
0057         watcher->deleteLater();
0058         if (reply.isError()) {
0059             return;
0060         }
0061 
0062         DBusManagerStruct objects = reply.value();
0063         for (const auto &object : objects.keys()) {
0064             if (object.path().contains(QLatin1String("char"))) {
0065                 m_characteristic = object;
0066                 break;
0067             }
0068         }
0069     });
0070 }
0071 
0072 void GattManagerInterface::runReadCharcAction(const QVariantMap &properties)
0073 {
0074     QDBusMessage call =
0075         QDBusMessage::createMethodCall(m_service, m_characteristic.path(), QStringLiteral("org.bluez.GattCharacteristic1"), QStringLiteral("ReadValue"));
0076     call << properties.value(QStringLiteral("Options"));
0077     QDBusConnection::sessionBus().asyncCall(call);
0078 }
0079 
0080 void GattManagerInterface::runWriteCharcAction(const QVariantMap &properties)
0081 {
0082     QDBusMessage call =
0083         QDBusMessage::createMethodCall(m_service, m_characteristic.path(), QStringLiteral("org.bluez.GattCharacteristic1"), QStringLiteral("WriteValue"));
0084     call << properties.value(QStringLiteral("Value"));
0085     call << properties.value(QStringLiteral("Options"));
0086     QDBusConnection::sessionBus().asyncCall(call);
0087 }
0088 
0089 #include "moc_gattmanagerinterface.cpp"