| 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 "leserver.h" |
| 8 |
|
| 9 |
#include <QCoreApplication> |
| 10 |
#include <QDebug> |
| 11 |
|
| 12 |
#include "adapter.h" |
| 13 |
#include "device.h" |
| 14 |
#include "gattapplication.h" |
| 15 |
#include "gattdescriptor.h" |
| 16 |
#include "gattmanager.h" |
| 17 |
#include "gattservice.h" |
| 18 |
#include "initmanagerjob.h" |
| 19 |
#include "leadvertisement.h" |
| 20 |
#include "leadvertisingmanager.h" |
| 21 |
#include "manager.h" |
| 22 |
#include "pendingcall.h" |
| 23 |
|
| 24 |
using namespace BluezQt; |
| 25 |
|
| 26 |
LeServer::LeServer(Manager *manager, QObject *parent) |
| 27 |
: QObject(parent) |
| 28 |
, m_manager(manager) |
| 29 |
{ |
| 30 |
auto advertisement = new LEAdvertisement({QStringLiteral("ad100000-d901-11e8-9f8b-f2801f1b9fd1")}, this); |
| 31 |
auto call = m_manager->usableAdapter()->leAdvertisingManager()->registerAdvertisement(advertisement); |
| 32 |
connect(call, &PendingCall::finished, this, [](BluezQt::PendingCall *call) { |
| 33 |
if (call->error()) { |
| 34 |
qWarning() << "Register advertisement error: " << call->errorText(); |
| 35 |
return; |
| 36 |
} |
| 37 |
}); |
| 38 |
|
| 39 |
auto application = new GattApplication(QStringLiteral("/org/kde/bluezqt"), this); |
| 40 |
auto service = new GattService(QStringLiteral("ad100000-d901-11e8-9f8b-f2801f1b9fd1"), true, application); |
| 41 |
|
| 42 |
GattCharacteristic *characteristic = new GattCharacteristic(QStringLiteral("ad10e100-d901-11e8-9f8b-f2801f1b9fd1"), service); |
| 43 |
GattDescriptor::createUserDescription(QLatin1String("MyCharacteristic"), characteristic); |
| 44 |
|
| 45 |
m_notifyingCharacteristic = |
| 46 |
new GattCharacteristic(QStringLiteral("ad10e100-d902-11e8-9f8b-f2801f1b9fd1"), {QStringLiteral("read"), QStringLiteral("notify")}, service); |
| 47 |
|
| 48 |
auto call2 = m_manager->usableAdapter()->gattManager()->registerApplication(application); |
| 49 |
|
| 50 |
connect(call2, &PendingCall::finished, this, [](BluezQt::PendingCall *call) { |
| 51 |
if (call->error()) { |
| 52 |
qWarning() << "Register application error: " << call->errorText(); |
| 53 |
return; |
| 54 |
} |
| 55 |
}); |
| 56 |
|
| 57 |
m_characteristicWriteTimer.setInterval(std::chrono::seconds(1)); |
| 58 |
m_characteristicWriteTimer.setSingleShot(false); |
| 59 |
m_characteristicWriteTimer.start(); |
| 60 |
|
| 61 |
static int charValue = 1; |
| 62 |
QObject::connect(&m_characteristicWriteTimer, &QTimer::timeout, [this]() { |
| 63 |
m_notifyingCharacteristic->writeValue(QByteArray::number(charValue++)); |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
int main(int argc, char **argv) |
| 68 |
{ |
| 69 |
QCoreApplication app(argc, argv); |
| 70 |
|
| 71 |
qDebug() << "Advertising LE services. Ctrl + C to cancel..."; |
| 72 |
|
| 73 |
Manager *manager = new Manager(); |
| 74 |
InitManagerJob *initJob = manager->init(); |
| 75 |
initJob->exec(); |
| 76 |
if (initJob->error()) { |
| 77 |
qWarning() << "Error initializing manager:" << initJob->errorText(); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
if (!manager->usableAdapter()) { |
| 82 |
qWarning() << "No usable adapter"; |
| 83 |
return 2; |
| 84 |
} |
| 85 |
if (!manager->usableAdapter()->leAdvertisingManager()) { |
| 86 |
qWarning() << "No LE advertising manager"; |
| 87 |
return 2; |
| 88 |
} |
| 89 |
if (!manager->usableAdapter()->gattManager()) { |
| 90 |
qWarning() << "No GATT manager"; |
| 91 |
return 2; |
| 92 |
} |
| 93 |
|
| 94 |
new LeServer(manager); |
| 95 |
|
| 96 |
return app.exec(); |
| 97 |
} |
| 98 |
|
| 99 |
#include "moc_leserver.cpp" |