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 "bluetoothdevicelink.h"
|
|
|
|
|
|
|
|
#include "../linkprovider.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "bluetoothdownloadjob.h"
|
2016-11-11 14:55:16 +00:00
|
|
|
#include "bluetoothlinkprovider.h"
|
|
|
|
#include "bluetoothuploadjob.h"
|
2019-03-08 18:18:01 +00:00
|
|
|
#include "connectionmultiplexer.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "core_debug.h"
|
2019-03-08 18:18:01 +00:00
|
|
|
#include "multiplexchannel.h"
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
BluetoothDeviceLink::BluetoothDeviceLink(const QString &deviceId,
|
|
|
|
LinkProvider *parent,
|
|
|
|
ConnectionMultiplexer *connection,
|
|
|
|
QSharedPointer<MultiplexChannel> socket)
|
2015-03-01 20:17:46 +00:00
|
|
|
: DeviceLink(deviceId, parent)
|
2019-03-08 18:18:01 +00:00
|
|
|
, mSocketReader(new DeviceLineReader(socket.data(), this))
|
|
|
|
, mConnection(connection)
|
|
|
|
, mChannel(socket)
|
2016-11-11 14:55:16 +00:00
|
|
|
, mPairingHandler(new BluetoothPairingHandler(this))
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2019-03-08 18:18:01 +00:00
|
|
|
connect(mSocketReader, &DeviceLineReader::readyRead, this, &BluetoothDeviceLink::dataReceived);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// We take ownership of the connection.
|
|
|
|
// When the link provider destroys us,
|
|
|
|
// the socket (and the reader) will be
|
|
|
|
// destroyed as well
|
2019-03-08 18:18:01 +00:00
|
|
|
mConnection->setParent(this);
|
|
|
|
connect(socket.data(), &MultiplexChannel::aboutToClose, this, &QObject::deleteLater);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
QString BluetoothDeviceLink::name()
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
return QStringLiteral("BluetoothLink"); // Should be same in both android and kde version
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool BluetoothDeviceLink::sendPacket(NetworkPacket &np)
|
2015-03-01 20:17:46 +00:00
|
|
|
{
|
|
|
|
if (np.hasPayload()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
BluetoothUploadJob *uploadJob = new BluetoothUploadJob(np.payload(), mConnection, this);
|
2016-11-11 14:55:16 +00:00
|
|
|
np.setPayloadTransferInfo(uploadJob->transferInfo());
|
|
|
|
uploadJob->start();
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
// TODO: handle too-big packets
|
2016-11-11 14:55:16 +00:00
|
|
|
int written = mSocketReader->write(np.serialize());
|
2015-03-01 20:17:46 +00:00
|
|
|
return (written != -1);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothDeviceLink::userRequestsPair()
|
|
|
|
{
|
2016-11-11 14:55:16 +00:00
|
|
|
mPairingHandler->requestPairing();
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void BluetoothDeviceLink::userRequestsUnpair()
|
|
|
|
{
|
2016-11-11 14:55:16 +00:00
|
|
|
mPairingHandler->unpair();
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool BluetoothDeviceLink::linkShouldBeKeptAlive()
|
|
|
|
{
|
2016-11-11 14:55:16 +00:00
|
|
|
return pairStatus() == Paired;
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
void BluetoothDeviceLink::dataReceived()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (mSocketReader->bytesAvailable() == 0)
|
|
|
|
return;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
const QByteArray serializedPacket = mSocketReader->readLine();
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// qCDebug(KDECONNECT_CORE) << "BluetoothDeviceLink dataReceived" << packet;
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2019-02-03 01:00:36 +00:00
|
|
|
NetworkPacket packet((QString()));
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket::unserialize(serializedPacket, &packet);
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
if (packet.type() == PACKET_TYPE_PAIR) {
|
2022-09-10 22:23:52 +01:00
|
|
|
// TODO: Handle pair/unpair requests and forward them (to the pairing handler?)
|
2018-03-04 19:48:51 +00:00
|
|
|
mPairingHandler->packetReceived(packet);
|
2016-11-11 14:55:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-01 20:17:46 +00:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
if (packet.hasPayloadTransferInfo()) {
|
2022-09-10 22:23:52 +01:00
|
|
|
BluetoothDownloadJob *downloadJob = new BluetoothDownloadJob(mConnection, packet.payloadTransferInfo(), this);
|
2016-11-11 14:55:16 +00:00
|
|
|
downloadJob->start();
|
2018-03-04 19:48:51 +00:00
|
|
|
packet.setPayload(downloadJob->payload(), packet.payloadSize());
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
Q_EMIT receivedPacket(packet);
|
2016-11-11 14:55:16 +00:00
|
|
|
|
|
|
|
if (mSocketReader->bytesAvailable() > 0) {
|
2019-03-08 18:18:01 +00:00
|
|
|
QMetaObject::invokeMethod(this, &BluetoothDeviceLink::dataReceived, Qt::QueuedConnection);
|
2015-03-01 20:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 19:11:47 +00:00
|
|
|
|
|
|
|
QSslCertificate BluetoothDeviceLink::certificate() const
|
|
|
|
{
|
2023-05-06 20:51:53 +01:00
|
|
|
return QSslCertificate({}); // TODO Not sure what to do here. For LanDeviceLink we use the SSL connection's certificate, but we don't have that here
|
2020-11-30 19:11:47 +00:00
|
|
|
}
|