kdeconnect-kde/core/backends/bluetooth/bluetoothdevicelink.cpp

104 lines
3.3 KiB
C++
Raw Normal View History

/**
* SPDX-FileCopyrightText: 2016 Saikrishna Arcot <saiarcot895@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "bluetoothdevicelink.h"
#include "../linkprovider.h"
#include "bluetoothdownloadjob.h"
2016-11-11 14:55:16 +00:00
#include "bluetoothlinkprovider.h"
#include "bluetoothuploadjob.h"
#include "connectionmultiplexer.h"
#include "core_debug.h"
#include "multiplexchannel.h"
BluetoothDeviceLink::BluetoothDeviceLink(const QString &deviceId,
LinkProvider *parent,
ConnectionMultiplexer *connection,
QSharedPointer<MultiplexChannel> socket)
: DeviceLink(deviceId, parent)
, mSocketReader(new DeviceLineReader(socket.data(), this))
, mConnection(connection)
, mChannel(socket)
2016-11-11 14:55:16 +00:00
, mPairingHandler(new BluetoothPairingHandler(this))
{
connect(mSocketReader, &DeviceLineReader::readyRead, this, &BluetoothDeviceLink::dataReceived);
// We take ownership of the connection.
// When the link provider destroys us,
// the socket (and the reader) will be
// destroyed as well
mConnection->setParent(this);
connect(socket.data(), &MultiplexChannel::aboutToClose, this, &QObject::deleteLater);
}
2016-11-11 14:55:16 +00:00
QString BluetoothDeviceLink::name()
{
return QStringLiteral("BluetoothLink"); // Should be same in both android and kde version
}
bool BluetoothDeviceLink::sendPacket(NetworkPacket &np)
{
if (np.hasPayload()) {
BluetoothUploadJob *uploadJob = new BluetoothUploadJob(np.payload(), mConnection, this);
2016-11-11 14:55:16 +00:00
np.setPayloadTransferInfo(uploadJob->transferInfo());
uploadJob->start();
}
// TODO: handle too-big packets
2016-11-11 14:55:16 +00:00
int written = mSocketReader->write(np.serialize());
return (written != -1);
}
void BluetoothDeviceLink::userRequestsPair()
{
2016-11-11 14:55:16 +00:00
mPairingHandler->requestPairing();
}
void BluetoothDeviceLink::userRequestsUnpair()
{
2016-11-11 14:55:16 +00:00
mPairingHandler->unpair();
}
bool BluetoothDeviceLink::linkShouldBeKeptAlive()
{
2016-11-11 14:55:16 +00:00
return pairStatus() == Paired;
}
2016-11-11 14:55:16 +00:00
void BluetoothDeviceLink::dataReceived()
{
if (mSocketReader->bytesAvailable() == 0)
return;
const QByteArray serializedPacket = mSocketReader->readLine();
// qCDebug(KDECONNECT_CORE) << "BluetoothDeviceLink dataReceived" << packet;
2019-02-03 01:00:36 +00:00
NetworkPacket packet((QString()));
NetworkPacket::unserialize(serializedPacket, &packet);
if (packet.type() == PACKET_TYPE_PAIR) {
// TODO: Handle pair/unpair requests and forward them (to the pairing handler?)
mPairingHandler->packetReceived(packet);
2016-11-11 14:55:16 +00:00
return;
}
if (packet.hasPayloadTransferInfo()) {
BluetoothDownloadJob *downloadJob = new BluetoothDownloadJob(mConnection, packet.payloadTransferInfo(), this);
2016-11-11 14:55:16 +00:00
downloadJob->start();
packet.setPayload(downloadJob->payload(), packet.payloadSize());
}
Q_EMIT receivedPacket(packet);
2016-11-11 14:55:16 +00:00
if (mSocketReader->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, &BluetoothDeviceLink::dataReceived, Qt::QueuedConnection);
}
}
QSslCertificate BluetoothDeviceLink::certificate() const
{
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
}