File indexing completed on 2024-05-05 16:04:26

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 "leserver.h"
0008 
0009 #include <QCoreApplication>
0010 #include <QDebug>
0011 
0012 #include "adapter.h"
0013 #include "device.h"
0014 #include "gattapplication.h"
0015 #include "gattcharacteristic.h"
0016 #include "gattmanager.h"
0017 #include "gattservice.h"
0018 #include "initmanagerjob.h"
0019 #include "leadvertisement.h"
0020 #include "leadvertisingmanager.h"
0021 #include "manager.h"
0022 #include "pendingcall.h"
0023 
0024 using namespace BluezQt;
0025 
0026 LeServer::LeServer(Manager *manager, QObject *parent)
0027     : QObject(parent)
0028     , m_manager(manager)
0029 {
0030     auto advertisement = new LEAdvertisement({QStringLiteral("ad100000-d901-11e8-9f8b-f2801f1b9fd1")}, this);
0031     auto call = m_manager->usableAdapter()->leAdvertisingManager()->registerAdvertisement(advertisement);
0032     connect(call, &PendingCall::finished, this, &LeServer::onCallFinished);
0033 
0034     auto application = new GattApplication(QStringLiteral("/org/kde/bluezqt"), this);
0035     auto service = new GattService(QStringLiteral("ad100000-d901-11e8-9f8b-f2801f1b9fd1"), true, application);
0036     new GattCharacteristic(QStringLiteral("ad10e100-d901-11e8-9f8b-f2801f1b9fd1"), service);
0037     auto call2 = m_manager->usableAdapter()->gattManager()->registerApplication(application);
0038     connect(call2, &PendingCall::finished, this, &LeServer::onCallFinished);
0039 }
0040 
0041 void LeServer::onCallFinished(BluezQt::PendingCall *call)
0042 {
0043     if (call->error()) {
0044         qWarning() << "Error: " << call->errorText();
0045         return;
0046     }
0047 }
0048 
0049 int main(int argc, char **argv)
0050 {
0051     QCoreApplication app(argc, argv);
0052 
0053     qDebug() << "Advertising LE services. Ctrl + C to cancel...";
0054 
0055     Manager *manager = new Manager();
0056     InitManagerJob *initJob = manager->init();
0057     initJob->exec();
0058     if (initJob->error()) {
0059         qWarning() << "Error initializing manager:" << initJob->errorText();
0060         return 1;
0061     }
0062 
0063     if (!manager->usableAdapter()) {
0064         qWarning() << "No usable adapter";
0065         return 2;
0066     }
0067     if (!manager->usableAdapter()->leAdvertisingManager()) {
0068         qWarning() << "No LE advertising manager";
0069         return 2;
0070     }
0071     if (!manager->usableAdapter()->gattManager()) {
0072         qWarning() << "No GATT manager";
0073         return 2;
0074     }
0075 
0076     new LeServer(manager);
0077 
0078     return app.exec();
0079 }
0080 
0081 #include "moc_leserver.cpp"