| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2015 David Rosca <[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 "chatprofile.h" |
| 8 |
#include "initmanagerjob.h" |
| 9 |
#include "manager.h" |
| 10 |
#include "pendingcall.h" |
| 11 |
|
| 12 |
#include <QCoreApplication> |
| 13 |
#include <QDBusObjectPath> |
| 14 |
#include <QDBusUnixFileDescriptor> |
| 15 |
#include <QDebug> |
| 16 |
#include <QLocalSocket> |
| 17 |
#include <QTimer> |
| 18 |
|
| 19 |
// This is a server for BluetoothChat application from Android samples. |
| 20 |
// |
| 21 |
// On successful connection, it sends "test-data" message to the client |
| 22 |
// and then writes all messages from client to the standard output. |
| 23 |
// |
| 24 |
// https://developer.android.com/samples/BluetoothChat/index.html |
| 25 |
|
| 26 |
ChatProfile::ChatProfile(QObject *parent) |
| 27 |
: BluezQt::Profile(parent) |
| 28 |
{ |
| 29 |
setName(QStringLiteral("BluetoothChatSecure")); |
| 30 |
setChannel(0); |
| 31 |
} |
| 32 |
|
| 33 |
QDBusObjectPath ChatProfile::objectPath() const |
| 34 |
{ |
| 35 |
return QDBusObjectPath(QStringLiteral("/ChatProfile")); |
| 36 |
} |
| 37 |
|
| 38 |
QString ChatProfile::uuid() const |
| 39 |
{ |
| 40 |
return QStringLiteral("fa87c0d0-afac-11de-8a39-0800200c9a66"); |
| 41 |
} |
| 42 |
|
| 43 |
void ChatProfile::newConnection(BluezQt::DevicePtr device, const QDBusUnixFileDescriptor &fd, const QVariantMap &properties, const BluezQt::Request<> &request) |
| 44 |
{ |
| 45 |
qDebug() << "Connect" << device->name() << properties; |
| 46 |
|
| 47 |
m_socket = createSocket(fd); |
| 48 |
if (!m_socket->isValid()) { |
| 49 |
request.cancel(); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
connect(m_socket.data(), &QLocalSocket::readyRead, this, &ChatProfile::socketReadyRead); |
| 54 |
connect(m_socket.data(), &QLocalSocket::disconnected, this, &ChatProfile::socketDisconnected); |
| 55 |
QTimer::singleShot(1000, this, SLOT(writeToSocket())); |
| 56 |
|
| 57 |
request.accept(); |
| 58 |
} |
| 59 |
|
| 60 |
void ChatProfile::requestDisconnection(BluezQt::DevicePtr device, const BluezQt::Request<> &request) |
| 61 |
{ |
| 62 |
qDebug() << "Disconnect" << device->name(); |
| 63 |
|
| 64 |
request.accept(); |
| 65 |
} |
| 66 |
|
| 67 |
void ChatProfile::release() |
| 68 |
{ |
| 69 |
qDebug() << "Release"; |
| 70 |
} |
| 71 |
|
| 72 |
void ChatProfile::socketReadyRead() |
| 73 |
{ |
| 74 |
qDebug() << "Read:" << m_socket->socketDescriptor() << m_socket->readAll(); |
| 75 |
} |
| 76 |
|
| 77 |
void ChatProfile::socketDisconnected() |
| 78 |
{ |
| 79 |
qDebug() << "Socket disconnected"; |
| 80 |
m_socket.clear(); |
| 81 |
} |
| 82 |
|
| 83 |
void ChatProfile::writeToSocket() |
| 84 |
{ |
| 85 |
qDebug() << "Writing" << m_socket->socketDescriptor(); |
| 86 |
m_socket->write(QByteArrayLiteral("test-data")); |
| 87 |
} |
| 88 |
|
| 89 |
int main(int argc, char **argv) |
| 90 |
{ |
| 91 |
QCoreApplication app(argc, argv); |
| 92 |
|
| 93 |
BluezQt::Manager *manager = new BluezQt::Manager(); |
| 94 |
BluezQt::InitManagerJob *initJob = manager->init(); |
| 95 |
initJob->exec(); |
| 96 |
|
| 97 |
if (initJob->error()) { |
| 98 |
qWarning() << "Error initializing manager:" << initJob->errorText(); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
BluezQt::PendingCall *call = manager->registerProfile(new ChatProfile(&app)); |
| 103 |
call->waitForFinished(); |
| 104 |
|
| 105 |
if (call->error()) { |
| 106 |
qWarning() << "Error registering profile" << call->errorText(); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
qDebug() << "Profile registered"; |
| 111 |
|
| 112 |
return app.exec(); |
| 113 |
} |
| 114 |
|
| 115 |
#include "moc_chatprofile.cpp" |