| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <[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 "gattmanagerinterface.h" |
| 8 |
#include "objectmanager.h" |
| 9 |
|
| 10 |
#include <QDBusConnection> |
| 11 |
#include <QDBusMessage> |
| 12 |
#include <QDBusPendingCall> |
| 13 |
#include <QDBusPendingReply> |
| 14 |
|
| 15 |
GattManagerInterface::GattManagerInterface(const QDBusObjectPath &path, QObject *parent) |
| 16 |
: QDBusAbstractAdaptor(parent) |
| 17 |
{ |
| 18 |
setName(QStringLiteral("org.bluez.GattManager1")); |
| 19 |
setPath(path); |
| 20 |
} |
| 21 |
|
| 22 |
void GattManagerInterface::runAction(const QString &actionName, const QVariantMap &properties) |
| 23 |
{ |
| 24 |
if (actionName == QLatin1String("get-objects")) { |
| 25 |
runGetObjectsAction(); |
| 26 |
} else if (actionName == QLatin1String("read-charc")) { |
| 27 |
runReadCharcAction(properties); |
| 28 |
} else if (actionName == QLatin1String("write-charc")) { |
| 29 |
runWriteCharcAction(properties); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
void GattManagerInterface::RegisterApplication(const QDBusObjectPath &path, const QVariantMap & /*options*/, const QDBusMessage &msg) |
| 34 |
{ |
| 35 |
m_application = path; |
| 36 |
m_service = msg.service(); |
| 37 |
} |
| 38 |
|
| 39 |
void GattManagerInterface::UnregisterApplication(const QDBusObjectPath &path, const QDBusMessage &msg) |
| 40 |
{ |
| 41 |
if (m_application == path && m_service == msg.service()) { |
| 42 |
m_application = QDBusObjectPath(); |
| 43 |
m_service.clear(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
void GattManagerInterface::runGetObjectsAction() |
| 48 |
{ |
| 49 |
QDBusMessage call = QDBusMessage::createMethodCall(m_service, |
| 50 |
m_application.path(), |
| 51 |
QStringLiteral("org.freedesktop.DBus.ObjectManager"), |
| 52 |
QStringLiteral("GetManagedObjects")); |
| 53 |
|
| 54 |
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(call)); |
| 55 |
connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) { |
| 56 |
const QDBusPendingReply<DBusManagerStruct> &reply = *watcher; |
| 57 |
watcher->deleteLater(); |
| 58 |
if (reply.isError()) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
DBusManagerStruct objects = reply.value(); |
| 63 |
for (const auto &object : objects.keys()) { |
| 64 |
if (object.path().contains(QLatin1String("char"))) { |
| 65 |
m_characteristic = object; |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
void GattManagerInterface::runReadCharcAction(const QVariantMap &properties) |
| 73 |
{ |
| 74 |
QDBusMessage call = |
| 75 |
QDBusMessage::createMethodCall(m_service, m_characteristic.path(), QStringLiteral("org.bluez.GattCharacteristic1"), QStringLiteral("ReadValue")); |
| 76 |
call << properties.value(QStringLiteral("Options")); |
| 77 |
QDBusConnection::sessionBus().asyncCall(call); |
| 78 |
} |
| 79 |
|
| 80 |
void GattManagerInterface::runWriteCharcAction(const QVariantMap &properties) |
| 81 |
{ |
| 82 |
QDBusMessage call = |
| 83 |
QDBusMessage::createMethodCall(m_service, m_characteristic.path(), QStringLiteral("org.bluez.GattCharacteristic1"), QStringLiteral("WriteValue")); |
| 84 |
call << properties.value(QStringLiteral("Value")); |
| 85 |
call << properties.value(QStringLiteral("Options")); |
| 86 |
QDBusConnection::sessionBus().asyncCall(call); |
| 87 |
} |
| 88 |
|
| 89 |
#include "moc_gattmanagerinterface.cpp" |