2016-11-11 14:55:16 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2015 Vineet Garg <grg.vineet@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
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-11-11 14:55:16 +00:00
|
|
|
*/
|
|
|
|
|
2019-02-03 00:44:22 +00:00
|
|
|
#include "bluetoothpairinghandler.h"
|
|
|
|
|
2016-11-11 14:55:16 +00:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
#include "core_debug.h"
|
|
|
|
#include "daemon.h"
|
|
|
|
#include "kdeconnectconfig.h"
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "networkpackettypes.h"
|
2016-11-11 14:55:16 +00:00
|
|
|
|
|
|
|
BluetoothPairingHandler::BluetoothPairingHandler(DeviceLink* deviceLink)
|
|
|
|
: PairingHandler(deviceLink)
|
|
|
|
, m_status(NotPaired)
|
|
|
|
{
|
|
|
|
m_pairingTimeout.setSingleShot(true);
|
2018-12-31 12:25:44 +00:00
|
|
|
m_pairingTimeout.setInterval(pairingTimeoutMsec());
|
2016-11-11 14:55:16 +00:00
|
|
|
connect(&m_pairingTimeout, &QTimer::timeout, this, &BluetoothPairingHandler::pairingTimeout);
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
void BluetoothPairingHandler::packetReceived(const NetworkPacket& np)
|
2016-11-11 14:55:16 +00:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Pairing packet received!" << np.serialize();
|
2016-11-11 14:55:16 +00:00
|
|
|
|
|
|
|
m_pairingTimeout.stop();
|
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
bool wantsPair = np.get<bool>(QStringLiteral("pair"));
|
2016-11-11 14:55:16 +00:00
|
|
|
|
|
|
|
if (wantsPair) {
|
|
|
|
|
|
|
|
if (isPairRequested()) { //We started pairing
|
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Pair answer";
|
|
|
|
setInternalPairStatus(Paired);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Pair request";
|
|
|
|
|
|
|
|
if (isPaired()) { //I'm already paired, but they think I'm not
|
|
|
|
acceptPairing();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setInternalPairStatus(RequestedByPeer);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { //wantsPair == false
|
|
|
|
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Unpair request";
|
|
|
|
|
|
|
|
setInternalPairStatus(NotPaired);
|
|
|
|
if (isPairRequested()) {
|
|
|
|
Q_EMIT pairingError(i18n("Canceled by other peer"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BluetoothPairingHandler::requestPairing()
|
|
|
|
{
|
|
|
|
switch (m_status) {
|
|
|
|
case Paired:
|
|
|
|
Q_EMIT pairingError(i18n("%1: Already paired", deviceLink()->name()));
|
|
|
|
return false;
|
|
|
|
case Requested:
|
|
|
|
Q_EMIT pairingError(i18n("%1: Pairing already requested for this device", deviceLink()->name()));
|
|
|
|
return false;
|
|
|
|
case RequestedByPeer:
|
|
|
|
qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
|
|
|
|
acceptPairing();
|
|
|
|
return false;
|
|
|
|
case NotPaired:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_PAIR);
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("pair"), true);
|
2016-11-11 14:55:16 +00:00
|
|
|
bool success;
|
2018-03-04 19:48:51 +00:00
|
|
|
success = deviceLink()->sendPacket(np);
|
2016-11-11 14:55:16 +00:00
|
|
|
if (success) {
|
|
|
|
setInternalPairStatus(Requested);
|
|
|
|
m_pairingTimeout.start();
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BluetoothPairingHandler::acceptPairing()
|
|
|
|
{
|
|
|
|
qCDebug(KDECONNECT_CORE) << "User accepts pairing";
|
|
|
|
m_pairingTimeout.stop(); // Just in case it is started
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_PAIR);
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("pair"), true);
|
2018-03-04 19:48:51 +00:00
|
|
|
bool success = deviceLink()->sendPacket(np);
|
2016-11-11 14:55:16 +00:00
|
|
|
if (success) {
|
|
|
|
setInternalPairStatus(Paired);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothPairingHandler::rejectPairing()
|
|
|
|
{
|
|
|
|
qCDebug(KDECONNECT_CORE) << "User rejects pairing";
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_PAIR);
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("pair"), false);
|
2018-03-04 19:48:51 +00:00
|
|
|
deviceLink()->sendPacket(np);
|
2016-11-11 14:55:16 +00:00
|
|
|
setInternalPairStatus(NotPaired);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothPairingHandler::unpair() {
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_PAIR);
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("pair"), false);
|
2018-03-04 19:48:51 +00:00
|
|
|
deviceLink()->sendPacket(np);
|
2016-11-11 14:55:16 +00:00
|
|
|
setInternalPairStatus(NotPaired);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothPairingHandler::pairingTimeout()
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_PAIR);
|
2019-06-10 15:40:28 +01:00
|
|
|
np.set(QStringLiteral("pair"), false);
|
2018-03-04 19:48:51 +00:00
|
|
|
deviceLink()->sendPacket(np);
|
2016-11-11 14:55:16 +00:00
|
|
|
setInternalPairStatus(NotPaired); //Will emit the change as well
|
|
|
|
Q_EMIT pairingError(i18n("Timed out"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BluetoothPairingHandler::setInternalPairStatus(BluetoothPairingHandler::InternalPairStatus status)
|
|
|
|
{
|
|
|
|
m_status = status;
|
|
|
|
if (status == Paired) {
|
|
|
|
deviceLink()->setPairStatus(DeviceLink::Paired);
|
2017-05-24 22:54:00 +01:00
|
|
|
} else if (status == NotPaired){
|
2016-11-11 14:55:16 +00:00
|
|
|
deviceLink()->setPairStatus(DeviceLink::NotPaired);
|
2017-05-24 22:54:00 +01:00
|
|
|
} else if (status == RequestedByPeer) {
|
|
|
|
Q_EMIT deviceLink()->pairingRequest(this);
|
2016-11-11 14:55:16 +00:00
|
|
|
}
|
|
|
|
}
|