Changeset View
Changeset View
Standalone View
Standalone View
core/backends/bluetooth/bluetoothpairinghandler.cpp
Show All 18 Lines | |||||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include <KLocalizedString> | 21 | #include <KLocalizedString> | ||
22 | 22 | | |||
23 | #include "core_debug.h" | 23 | #include "core_debug.h" | ||
24 | #include "daemon.h" | 24 | #include "daemon.h" | ||
25 | #include "kdeconnectconfig.h" | 25 | #include "kdeconnectconfig.h" | ||
26 | #include "bluetoothpairinghandler.h" | 26 | #include "bluetoothpairinghandler.h" | ||
27 | #include "networkpackagetypes.h" | 27 | #include "networkpackettypes.h" | ||
28 | 28 | | |||
29 | BluetoothPairingHandler::BluetoothPairingHandler(DeviceLink* deviceLink) | 29 | BluetoothPairingHandler::BluetoothPairingHandler(DeviceLink* deviceLink) | ||
30 | : PairingHandler(deviceLink) | 30 | : PairingHandler(deviceLink) | ||
31 | , m_status(NotPaired) | 31 | , m_status(NotPaired) | ||
32 | { | 32 | { | ||
33 | m_pairingTimeout.setSingleShot(true); | 33 | m_pairingTimeout.setSingleShot(true); | ||
34 | m_pairingTimeout.setInterval(30 * 1000); //30 seconds of timeout | 34 | m_pairingTimeout.setInterval(30 * 1000); //30 seconds of timeout | ||
35 | connect(&m_pairingTimeout, &QTimer::timeout, this, &BluetoothPairingHandler::pairingTimeout); | 35 | connect(&m_pairingTimeout, &QTimer::timeout, this, &BluetoothPairingHandler::pairingTimeout); | ||
36 | } | 36 | } | ||
37 | 37 | | |||
38 | void BluetoothPairingHandler::packageReceived(const NetworkPackage& np) | 38 | void BluetoothPairingHandler::packetReceived(const NetworkPacket& np) | ||
39 | { | 39 | { | ||
40 | qCDebug(KDECONNECT_CORE) << "Pairing package received!" << np.serialize(); | 40 | qCDebug(KDECONNECT_CORE) << "Pairing packet received!" << np.serialize(); | ||
41 | 41 | | |||
42 | m_pairingTimeout.stop(); | 42 | m_pairingTimeout.stop(); | ||
43 | 43 | | |||
44 | bool wantsPair = np.get<bool>("pair"); | 44 | bool wantsPair = np.get<bool>("pair"); | ||
45 | 45 | | |||
46 | if (wantsPair) { | 46 | if (wantsPair) { | ||
47 | 47 | | |||
48 | if (isPairRequested()) { //We started pairing | 48 | if (isPairRequested()) { //We started pairing | ||
Show All 35 Lines | 77 | switch (m_status) { | |||
84 | case RequestedByPeer: | 84 | case RequestedByPeer: | ||
85 | qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request."; | 85 | qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request."; | ||
86 | acceptPairing(); | 86 | acceptPairing(); | ||
87 | return false; | 87 | return false; | ||
88 | case NotPaired: | 88 | case NotPaired: | ||
89 | ; | 89 | ; | ||
90 | } | 90 | } | ||
91 | 91 | | |||
92 | NetworkPackage np(PACKAGE_TYPE_PAIR); | 92 | NetworkPacket np(PACKET_TYPE_PAIR); | ||
93 | np.set("pair", true); | 93 | np.set("pair", true); | ||
94 | bool success; | 94 | bool success; | ||
95 | success = deviceLink()->sendPackage(np); | 95 | success = deviceLink()->sendPacket(np); | ||
96 | if (success) { | 96 | if (success) { | ||
97 | setInternalPairStatus(Requested); | 97 | setInternalPairStatus(Requested); | ||
98 | m_pairingTimeout.start(); | 98 | m_pairingTimeout.start(); | ||
99 | } | 99 | } | ||
100 | return success; | 100 | return success; | ||
101 | } | 101 | } | ||
102 | 102 | | |||
103 | bool BluetoothPairingHandler::acceptPairing() | 103 | bool BluetoothPairingHandler::acceptPairing() | ||
104 | { | 104 | { | ||
105 | qCDebug(KDECONNECT_CORE) << "User accepts pairing"; | 105 | qCDebug(KDECONNECT_CORE) << "User accepts pairing"; | ||
106 | m_pairingTimeout.stop(); // Just in case it is started | 106 | m_pairingTimeout.stop(); // Just in case it is started | ||
107 | NetworkPackage np(PACKAGE_TYPE_PAIR); | 107 | NetworkPacket np(PACKET_TYPE_PAIR); | ||
108 | np.set("pair", true); | 108 | np.set("pair", true); | ||
109 | bool success = deviceLink()->sendPackage(np); | 109 | bool success = deviceLink()->sendPacket(np); | ||
110 | if (success) { | 110 | if (success) { | ||
111 | setInternalPairStatus(Paired); | 111 | setInternalPairStatus(Paired); | ||
112 | } | 112 | } | ||
113 | return success; | 113 | return success; | ||
114 | } | 114 | } | ||
115 | 115 | | |||
116 | void BluetoothPairingHandler::rejectPairing() | 116 | void BluetoothPairingHandler::rejectPairing() | ||
117 | { | 117 | { | ||
118 | qCDebug(KDECONNECT_CORE) << "User rejects pairing"; | 118 | qCDebug(KDECONNECT_CORE) << "User rejects pairing"; | ||
119 | NetworkPackage np(PACKAGE_TYPE_PAIR); | 119 | NetworkPacket np(PACKET_TYPE_PAIR); | ||
120 | np.set("pair", false); | 120 | np.set("pair", false); | ||
121 | deviceLink()->sendPackage(np); | 121 | deviceLink()->sendPacket(np); | ||
122 | setInternalPairStatus(NotPaired); | 122 | setInternalPairStatus(NotPaired); | ||
123 | } | 123 | } | ||
124 | 124 | | |||
125 | void BluetoothPairingHandler::unpair() { | 125 | void BluetoothPairingHandler::unpair() { | ||
126 | NetworkPackage np(PACKAGE_TYPE_PAIR); | 126 | NetworkPacket np(PACKET_TYPE_PAIR); | ||
127 | np.set("pair", false); | 127 | np.set("pair", false); | ||
128 | deviceLink()->sendPackage(np); | 128 | deviceLink()->sendPacket(np); | ||
129 | setInternalPairStatus(NotPaired); | 129 | setInternalPairStatus(NotPaired); | ||
130 | } | 130 | } | ||
131 | 131 | | |||
132 | void BluetoothPairingHandler::pairingTimeout() | 132 | void BluetoothPairingHandler::pairingTimeout() | ||
133 | { | 133 | { | ||
134 | NetworkPackage np(PACKAGE_TYPE_PAIR); | 134 | NetworkPacket np(PACKET_TYPE_PAIR); | ||
135 | np.set("pair", false); | 135 | np.set("pair", false); | ||
136 | deviceLink()->sendPackage(np); | 136 | deviceLink()->sendPacket(np); | ||
137 | setInternalPairStatus(NotPaired); //Will emit the change as well | 137 | setInternalPairStatus(NotPaired); //Will emit the change as well | ||
138 | Q_EMIT pairingError(i18n("Timed out")); | 138 | Q_EMIT pairingError(i18n("Timed out")); | ||
139 | } | 139 | } | ||
140 | 140 | | |||
141 | void BluetoothPairingHandler::setInternalPairStatus(BluetoothPairingHandler::InternalPairStatus status) | 141 | void BluetoothPairingHandler::setInternalPairStatus(BluetoothPairingHandler::InternalPairStatus status) | ||
142 | { | 142 | { | ||
143 | m_status = status; | 143 | m_status = status; | ||
144 | if (status == Paired) { | 144 | if (status == Paired) { | ||
145 | deviceLink()->setPairStatus(DeviceLink::Paired); | 145 | deviceLink()->setPairStatus(DeviceLink::Paired); | ||
146 | } else if (status == NotPaired){ | 146 | } else if (status == NotPaired){ | ||
147 | deviceLink()->setPairStatus(DeviceLink::NotPaired); | 147 | deviceLink()->setPairStatus(DeviceLink::NotPaired); | ||
148 | } else if (status == RequestedByPeer) { | 148 | } else if (status == RequestedByPeer) { | ||
149 | Q_EMIT deviceLink()->pairingRequest(this); | 149 | Q_EMIT deviceLink()->pairingRequest(this); | ||
150 | } | 150 | } | ||
151 | } | 151 | } |