2015-03-01 20:17:46 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2016 Saikrishna Arcot <saiarcot895@gmail.com>
|
2015-03-01 20:17:46 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2015-03-01 20:17:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bluetoothlinkprovider.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "bluetoothdevicelink.h"
|
2019-03-08 18:18:01 +00:00
|
|
|
#include "connectionmultiplexer.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "core_debug.h"
|
2023-06-27 12:10:59 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
2019-03-08 18:18:01 +00:00
|
|
|
#include "multiplexchannel.h"
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
#include <QBluetoothServiceInfo>
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
BluetoothLinkProvider::BluetoothLinkProvider()
|
2022-12-22 16:24:31 +00:00
|
|
|
: mServiceUuid(QBluetoothUuid(QStringLiteral("185f3df4-3268-4e3f-9fca-d4d5059915bd")))
|
|
|
|
, mServiceDiscoveryAgent(new QBluetoothServiceDiscoveryAgent(this))
|
|
|
|
, connectTimer(new QTimer(this))
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
|
|
|
connectTimer->setInterval(30000);
|
|
|
|
connectTimer->setSingleShot(false);
|
2017-05-24 22:28:49 +01:00
|
|
|
|
2015-03-01 20:17:46 +00:00
|
|
|
mServiceDiscoveryAgent->setUuidFilter(mServiceUuid);
|
2019-03-08 18:18:01 +00:00
|
|
|
connect(connectTimer, &QTimer::timeout, this, [this]() {
|
|
|
|
mServiceDiscoveryAgent->start();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(mServiceDiscoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered, this, &BluetoothLinkProvider::serviceDiscovered);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothLinkProvider::onStart()
|
|
|
|
{
|
2016-11-11 14:55:16 +00:00
|
|
|
QBluetoothLocalDevice localDevice;
|
|
|
|
if (!localDevice.isValid()) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "No local bluetooth adapter found";
|
2015-03-01 20:17:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
mBluetoothServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
|
2023-07-21 20:12:18 +01:00
|
|
|
mBluetoothServer->setSecurityFlags(QBluetooth::Security::Encryption | QBluetooth::Security::Secure);
|
2023-08-08 21:51:30 +01:00
|
|
|
connect(mBluetoothServer, &QBluetoothServer::newConnection, this, &BluetoothLinkProvider::serverNewConnection);
|
2016-11-11 14:55:16 +00:00
|
|
|
|
2015-03-01 20:17:46 +00:00
|
|
|
mServiceDiscoveryAgent->start();
|
|
|
|
|
|
|
|
connectTimer->start();
|
2019-06-10 15:40:28 +01:00
|
|
|
mKdeconnectService = mBluetoothServer->listen(mServiceUuid, QStringLiteral("KDE Connect"));
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothLinkProvider::onStop()
|
|
|
|
{
|
|
|
|
if (!mBluetoothServer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connectTimer->stop();
|
|
|
|
|
|
|
|
mKdeconnectService.unregisterService();
|
|
|
|
mBluetoothServer->close();
|
2016-11-11 14:55:16 +00:00
|
|
|
mBluetoothServer->deleteLater();
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// I'm in a new network, let's be polite and introduce myself
|
2016-11-11 14:55:16 +00:00
|
|
|
void BluetoothLinkProvider::onNetworkChange()
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothLinkProvider::connectError()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
|
|
|
|
if (!socket)
|
|
|
|
return;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
qCWarning(KDECONNECT_CORE) << "Couldn't connect to socket:" << socket->errorString();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-08-08 21:51:30 +01:00
|
|
|
disconnect(socket, &QBluetoothSocket::connected, this, nullptr);
|
|
|
|
disconnect(socket, &QBluetoothSocket::readyRead, this, nullptr);
|
|
|
|
disconnect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error), this, nullptr);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
mSockets.remove(socket->peerAddress());
|
|
|
|
socket->deleteLater();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothLinkProvider::addLink(BluetoothDeviceLink *deviceLink, const QString &deviceId)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
QMap<QString, DeviceLink *>::iterator oldLinkIterator = mLinks.find(deviceId);
|
2023-06-27 12:10:59 +01:00
|
|
|
delete oldLinkIterator.value(); // not calling deleteLater because this triggers onLinkDestroyed
|
2015-03-01 20:17:46 +00:00
|
|
|
mLinks[deviceId] = deviceLink;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothLinkProvider::serviceDiscovered(const QBluetoothServiceInfo &old_info)
|
|
|
|
{
|
2019-03-08 18:18:01 +00:00
|
|
|
auto info = old_info;
|
|
|
|
info.setServiceUuid(mServiceUuid);
|
|
|
|
if (mSockets.contains(info.device().address())) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QBluetoothSocket *socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Delay before sending data
|
2019-03-08 18:18:01 +00:00
|
|
|
QPointer<QBluetoothSocket> deleteableSocket = socket;
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(socket, &QBluetoothSocket::connected, this, [this, deleteableSocket]() {
|
|
|
|
QTimer::singleShot(500, this, [this, deleteableSocket]() {
|
2019-03-08 18:18:01 +00:00
|
|
|
clientConnected(deleteableSocket);
|
|
|
|
});
|
|
|
|
});
|
2023-08-08 21:51:30 +01:00
|
|
|
connect(socket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error), this, &BluetoothLinkProvider::connectError);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
socket->connectToService(info);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Connecting to" << info.device().address();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-07-21 20:12:18 +01:00
|
|
|
if (socket->error() != QBluetoothSocket::SocketError::NoSocketError) {
|
2019-03-08 18:18:01 +00:00
|
|
|
qCWarning(KDECONNECT_CORE) << "Socket connection error:" << socket->errorString();
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// I'm the new device and I'm connected to the existing device. Time to get data.
|
2019-03-08 18:18:01 +00:00
|
|
|
void BluetoothLinkProvider::clientConnected(QPointer<QBluetoothSocket> socket)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!socket)
|
|
|
|
return;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
auto peer = socket->peerAddress();
|
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Connected to" << peer;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
if (mSockets.contains(socket->peerAddress())) {
|
2023-08-04 16:04:15 +01:00
|
|
|
qCWarning(KDECONNECT_CORE) << "Duplicate connection to" << peer;
|
2015-03-01 20:17:46 +00:00
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
ConnectionMultiplexer *multiplexer = new ConnectionMultiplexer(socket, this);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
mSockets.insert(peer, multiplexer);
|
|
|
|
disconnect(socket, nullptr, this, nullptr);
|
|
|
|
|
|
|
|
auto channel = QSharedPointer<MultiplexChannel>{multiplexer->getDefaultChannel().release()};
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(channel.data(), &MultiplexChannel::readyRead, this, [this, peer, channel]() {
|
|
|
|
clientIdentityReceived(peer, channel);
|
|
|
|
});
|
|
|
|
connect(channel.data(), &MultiplexChannel::aboutToClose, this, [this, peer, channel]() {
|
|
|
|
socketDisconnected(peer, channel.data());
|
|
|
|
});
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
if (channel->bytesAvailable())
|
|
|
|
clientIdentityReceived(peer, channel);
|
|
|
|
if (!channel->isOpen())
|
|
|
|
socketDisconnected(peer, channel.data());
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// I'm the new device and the existing device sent me data.
|
2019-03-08 18:18:01 +00:00
|
|
|
void BluetoothLinkProvider::clientIdentityReceived(const QBluetoothAddress &peer, QSharedPointer<MultiplexChannel> socket)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2019-03-08 18:18:01 +00:00
|
|
|
socket->startTransaction();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
QByteArray identityArray = socket->readLine();
|
|
|
|
if (identityArray.isEmpty()) {
|
|
|
|
socket->rollbackTransaction();
|
2018-12-31 12:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 18:18:01 +00:00
|
|
|
socket->commitTransaction();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
disconnect(socket.data(), &MultiplexChannel::readyRead, this, nullptr);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket receivedPacket;
|
2018-03-04 19:48:51 +00:00
|
|
|
bool success = NetworkPacket::unserialize(identityArray, &receivedPacket);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
if (!success || receivedPacket.type() != PACKET_TYPE_IDENTITY) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Not an identity packet";
|
2019-03-08 18:18:01 +00:00
|
|
|
mSockets.remove(peer);
|
2015-03-01 20:17:46 +00:00
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Received identity packet from" << peer;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// TODO?
|
2023-08-08 21:51:30 +01:00
|
|
|
// disconnect(socket, &QAbstractSocket::error, this, &BluetoothLinkProvider::connectError);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QSslCertificate receivedCertificate(receivedPacket.get<QString>(QStringLiteral("certificate")).toLatin1());
|
|
|
|
DeviceInfo deviceInfo = deviceInfo.FromIdentityPacketAndCert(receivedPacket, receivedCertificate);
|
|
|
|
BluetoothDeviceLink *deviceLink = new BluetoothDeviceLink(deviceInfo, this, mSockets[peer], socket);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo myDeviceInfo = KdeConnectConfig::instance().deviceInfo();
|
|
|
|
NetworkPacket myIdentity = myDeviceInfo.toIdentityPacket();
|
|
|
|
myIdentity.set(QStringLiteral("certificate"), QString::fromLatin1(myDeviceInfo.certificate.toPem()));
|
|
|
|
success = deviceLink->sendPacket(myIdentity);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Handshaking done (I'm the new device)";
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
Q_EMIT onConnectionReceived(deviceLink);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// We kill any possible link from this same device
|
2023-06-27 12:10:59 +01:00
|
|
|
addLink(deviceLink, deviceInfo.id);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Connection might be lost. Delete it.
|
|
|
|
delete deviceLink;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// We don't delete the socket because now it's owned by the BluetoothDeviceLink
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// I'm the existing device, a new device is kindly introducing itself.
|
2015-03-01 20:17:46 +00:00
|
|
|
void BluetoothLinkProvider::serverNewConnection()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
QBluetoothSocket *socket = mBluetoothServer->nextPendingConnection();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Received connection from" << socket->peerAddress();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
QBluetoothAddress peer = socket->peerAddress();
|
|
|
|
|
|
|
|
if (mSockets.contains(peer)) {
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Duplicate connection from" << peer;
|
2015-03-01 20:17:46 +00:00
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
ConnectionMultiplexer *multiplexer = new ConnectionMultiplexer(socket, this);
|
|
|
|
|
|
|
|
mSockets.insert(peer, multiplexer);
|
|
|
|
disconnect(socket, nullptr, this, nullptr);
|
|
|
|
|
|
|
|
auto channel = QSharedPointer<MultiplexChannel>{multiplexer->getDefaultChannel().release()};
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(channel.data(), &MultiplexChannel::readyRead, this, [this, peer, channel]() {
|
|
|
|
serverDataReceived(peer, channel);
|
|
|
|
});
|
|
|
|
connect(channel.data(), &MultiplexChannel::aboutToClose, this, [this, peer, channel]() {
|
|
|
|
socketDisconnected(peer, channel.data());
|
|
|
|
});
|
2019-03-08 18:18:01 +00:00
|
|
|
|
|
|
|
if (!channel->isOpen()) {
|
|
|
|
socketDisconnected(peer, channel.data());
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
DeviceInfo myDeviceInfo = KdeConnectConfig::instance().deviceInfo();
|
|
|
|
NetworkPacket myIdentity = myDeviceInfo.toIdentityPacket();
|
|
|
|
myIdentity.set(QStringLiteral("certificate"), QString::fromLatin1(myDeviceInfo.certificate.toPem()));
|
|
|
|
socket->write(myIdentity.serialize());
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Sent identity packet to" << socket->peerAddress();
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// I'm the existing device and this is the answer to my identity packet (data received)
|
2019-03-08 18:18:01 +00:00
|
|
|
void BluetoothLinkProvider::serverDataReceived(const QBluetoothAddress &peer, QSharedPointer<MultiplexChannel> socket)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
|
|
|
QByteArray identityArray;
|
2019-03-08 18:18:01 +00:00
|
|
|
socket->startTransaction();
|
|
|
|
identityArray = socket->readLine();
|
2018-12-31 12:25:44 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
if (identityArray.isEmpty()) {
|
|
|
|
socket->rollbackTransaction();
|
2018-12-31 12:25:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-08 18:18:01 +00:00
|
|
|
socket->commitTransaction();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
disconnect(socket.data(), &MultiplexChannel::readyRead, this, nullptr);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket receivedPacket;
|
2018-03-04 19:48:51 +00:00
|
|
|
bool success = NetworkPacket::unserialize(identityArray, &receivedPacket);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
if (!success || receivedPacket.type() != PACKET_TYPE_IDENTITY) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Not an identity packet.";
|
2019-03-08 18:18:01 +00:00
|
|
|
mSockets.remove(peer);
|
2015-03-01 20:17:46 +00:00
|
|
|
socket->close();
|
|
|
|
socket->deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Received identity packet from" << peer;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
QSslCertificate receivedCertificate(receivedPacket.get<QString>(QStringLiteral("certificate")).toLatin1());
|
|
|
|
DeviceInfo deviceInfo = deviceInfo.FromIdentityPacketAndCert(receivedPacket, receivedCertificate);
|
|
|
|
BluetoothDeviceLink *deviceLink = new BluetoothDeviceLink(deviceInfo, this, mSockets[peer], socket);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
Q_EMIT onConnectionReceived(deviceLink);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
addLink(deviceLink, deviceInfo.id);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
void BluetoothLinkProvider::onLinkDestroyed(const QString &deviceId, DeviceLink *oldPtr)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2023-06-27 12:10:59 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "BluetoothLinkProvider deviceLinkDestroyed" << deviceId;
|
|
|
|
DeviceLink *link = mLinks.take(deviceId);
|
|
|
|
Q_ASSERT(link == oldPtr);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
void BluetoothLinkProvider::socketDisconnected(const QBluetoothAddress &peer, MultiplexChannel *socket)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2023-08-04 16:04:15 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Disconnected";
|
2019-03-08 18:18:01 +00:00
|
|
|
disconnect(socket, nullptr, this, nullptr);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-03-08 18:18:01 +00:00
|
|
|
mSockets.remove(peer);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BluetoothLinkProvider::~BluetoothLinkProvider()
|
|
|
|
{
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_bluetoothlinkprovider.cpp"
|