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

107 lines
3.7 KiB
C++
Raw Normal View History

/**
2016-11-11 14:55:16 +00:00
* Copyright 2016 Saikrishna Arcot <saiarcot895@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bluetoothdevicelink.h"
#include "../linkprovider.h"
2016-11-11 14:55:16 +00:00
#include "bluetoothlinkprovider.h"
#include "bluetoothuploadjob.h"
#include "bluetoothdownloadjob.h"
#include "core_debug.h"
BluetoothDeviceLink::BluetoothDeviceLink(const QString& deviceId, LinkProvider* parent, QBluetoothSocket* socket)
: DeviceLink(deviceId, parent)
2016-11-11 14:55:16 +00:00
, mSocketReader(new DeviceLineReader(socket, this))
, mBluetoothSocket(socket)
, mPairingHandler(new BluetoothPairingHandler(this))
{
2016-11-11 14:55:16 +00:00
connect(mSocketReader, SIGNAL(readyRead()),
this, SLOT(dataReceived()));
//We take ownership of the socket.
2016-11-11 14:55:16 +00:00
//When the link provider destroys us,
//the socket (and the reader) will be
//destroyed as well
mBluetoothSocket->setParent(this);
2016-11-11 14:55:16 +00:00
connect(mBluetoothSocket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
}
2016-11-11 14:55:16 +00:00
QString BluetoothDeviceLink::name()
{
2016-11-11 14:55:16 +00:00
return "BluetoothLink"; // Should be same in both android and kde version
}
bool BluetoothDeviceLink::sendPackage(NetworkPackage& np)
{
if (np.hasPayload()) {
2016-11-11 14:55:16 +00:00
qCWarning(KDECONNECT_CORE) << "Sending packages with payload over bluetooth not yet supported";
/*
BluetoothUploadJob* uploadJob = new BluetoothUploadJob(np.payload(), mBluetoothSocket->peerAddress(), this);
np.setPayloadTransferInfo(uploadJob->transferInfo());
uploadJob->start();
*/
}
2016-11-11 14:55:16 +00:00
int written = mSocketReader->write(np.serialize());
return (written != -1);
}
2016-11-11 14:55:16 +00:00
void BluetoothDeviceLink::userRequestsPair() {
mPairingHandler->requestPairing();
}
2016-11-11 14:55:16 +00:00
void BluetoothDeviceLink::userRequestsUnpair() {
mPairingHandler->unpair();
}
2016-11-11 14:55:16 +00:00
bool BluetoothDeviceLink::linkShouldBeKeptAlive() {
return pairStatus() == Paired;
}
2016-11-11 14:55:16 +00:00
void BluetoothDeviceLink::dataReceived()
{
if (mSocketReader->bytesAvailable() == 0) return;
2016-11-11 14:55:16 +00:00
const QByteArray serializedPackage = mSocketReader->readLine();
2016-11-11 14:55:16 +00:00
//qCDebug(KDECONNECT_CORE) << "BluetoothDeviceLink dataReceived" << package;
2016-11-11 14:55:16 +00:00
NetworkPackage package(QString::null);
NetworkPackage::unserialize(serializedPackage, &package);
2016-11-11 14:55:16 +00:00
if (package.type() == PACKAGE_TYPE_PAIR) {
//TODO: Handle pair/unpair requests and forward them (to the pairing handler?)
mPairingHandler->packageReceived(package);
return;
}
2016-11-11 14:55:16 +00:00
if (package.hasPayloadTransferInfo()) {
BluetoothDownloadJob* downloadJob = new BluetoothDownloadJob(mBluetoothSocket->peerAddress(),
package.payloadTransferInfo(), this);
downloadJob->start();
package.setPayload(downloadJob->payload(), package.payloadSize());
}
2016-11-11 14:55:16 +00:00
Q_EMIT receivedPackage(package);
if (mSocketReader->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
}
}