File indexing completed on 2024-12-08 12:15:33

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 "gattmanager.h"
0010 
0011 #include "debug.h"
0012 #include "gattapplication.h"
0013 #include "gattcharacteristic.h"
0014 #include "gattcharacteristicadaptor.h"
0015 #include "gattmanager_p.h"
0016 #include "gattservice.h"
0017 #include "gattserviceadaptor.h"
0018 #include "objectmanageradaptor.h"
0019 #include "pendingcall.h"
0020 #include "utils.h"
0021 
0022 #include <QDBusPendingCall>
0023 
0024 namespace BluezQt
0025 {
0026 GattManager::GattManager(const QString &path, QObject *parent)
0027     : QObject(parent)
0028     , d(new GattManagerPrivate(path))
0029 {
0030 }
0031 
0032 GattManager::~GattManager()
0033 {
0034     delete d;
0035 }
0036 
0037 PendingCall *GattManager::registerApplication(GattApplication *application)
0038 {
0039     Q_ASSERT(application);
0040 
0041     const auto services = application->findChildren<GattService *>();
0042     for (auto service : services) {
0043         new GattServiceAdaptor(service);
0044 
0045         const auto charcs = service->findChildren<GattCharacteristic *>();
0046         for (auto charc : charcs) {
0047             new GattCharacteristicAdaptor(charc);
0048             if (!DBusConnection::orgBluez().registerObject(charc->objectPath().path(), charc, QDBusConnection::ExportAdaptors)) {
0049                 qCDebug(BLUEZQT) << "Cannot register object" << charc->objectPath().path();
0050             }
0051         }
0052 
0053         if (!DBusConnection::orgBluez().registerObject(service->objectPath().path(), service, QDBusConnection::ExportAdaptors)) {
0054             qCDebug(BLUEZQT) << "Cannot register object" << service->objectPath().path();
0055         }
0056     }
0057 
0058     new ObjectManagerAdaptor(application);
0059 
0060     if (!DBusConnection::orgBluez().registerObject(application->objectPath().path(), application, QDBusConnection::ExportAdaptors)) {
0061         qCDebug(BLUEZQT) << "Cannot register object" << application->objectPath().path();
0062     }
0063 
0064     return new PendingCall(d->m_dbusInterface.RegisterApplication(application->objectPath(), QVariantMap()), PendingCall::ReturnVoid, this);
0065 }
0066 
0067 PendingCall *GattManager::unregisterApplication(GattApplication *application)
0068 {
0069     Q_ASSERT(application);
0070 
0071     DBusConnection::orgBluez().unregisterObject(application->objectPath().path());
0072 
0073     return new PendingCall(d->m_dbusInterface.UnregisterApplication(application->objectPath()), PendingCall::ReturnVoid, this);
0074 }
0075 
0076 } // namespace BluezQt
0077 
0078 #include "moc_gattmanager.cpp"