Changeset View
Changeset View
Standalone View
Standalone View
core/device.cpp
Show All 14 Lines | |||||
15 | * GNU General Public License for more details. | 15 | * GNU General Public License for more details. | ||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "device.h" | 21 | #include "device.h" | ||
22 | 22 | | |||
23 | #ifdef interface // MSVC language extension, QDBusConnection uses this as a variable name | | |||
24 | #undef interface | | |||
25 | #endif | | |||
26 | | ||||
27 | #include <QDBusConnection> | 23 | #include <QDBusConnection> | ||
28 | #include <QSslCertificate> | 24 | #include <QSslCertificate> | ||
29 | 25 | | |||
30 | #include <KSharedConfig> | 26 | #include <KSharedConfig> | ||
31 | #include <KConfigGroup> | 27 | #include <KConfigGroup> | ||
32 | #include <KLocalizedString> | 28 | #include <KLocalizedString> | ||
33 | 29 | | |||
34 | #include "core_debug.h" | 30 | #include "core_debug.h" | ||
35 | #include "kdeconnectplugin.h" | 31 | #include "kdeconnectplugin.h" | ||
36 | #include "pluginloader.h" | 32 | #include "pluginloader.h" | ||
37 | #include "backends/devicelink.h" | 33 | #include "backends/devicelink.h" | ||
38 | #include "backends/lan/landevicelink.h" | 34 | #include "backends/lan/landevicelink.h" | ||
39 | #include "backends/linkprovider.h" | 35 | #include "backends/linkprovider.h" | ||
40 | #include "networkpackage.h" | 36 | #include "networkpacket.h" | ||
41 | #include "kdeconnectconfig.h" | 37 | #include "kdeconnectconfig.h" | ||
42 | #include "daemon.h" | 38 | #include "daemon.h" | ||
43 | 39 | | |||
44 | static void warn(const QString& info) | 40 | static void warn(const QString& info) | ||
45 | { | 41 | { | ||
46 | qWarning() << "Device pairing error" << info; | 42 | qWarning() << "Device pairing error" << info; | ||
47 | } | 43 | } | ||
48 | 44 | | |||
49 | Device::Device(QObject* parent, const QString& id) | 45 | Device::Device(QObject* parent, const QString& id) | ||
50 | : QObject(parent) | 46 | : QObject(parent) | ||
51 | , m_deviceId(id) | 47 | , m_deviceId(id) | ||
52 | , m_protocolVersion(NetworkPackage::s_protocolVersion) //We don't know it yet | 48 | , m_protocolVersion(NetworkPacket::s_protocolVersion) //We don't know it yet | ||
53 | { | 49 | { | ||
54 | KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(id); | 50 | KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(id); | ||
55 | 51 | | |||
56 | m_deviceName = info.deviceName; | 52 | m_deviceName = info.deviceName; | ||
57 | m_deviceType = str2type(info.deviceType); | 53 | m_deviceType = str2type(info.deviceType); | ||
58 | 54 | | |||
59 | //Register in bus | 55 | //Register in bus | ||
60 | QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); | 56 | QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); | ||
61 | 57 | | |||
62 | //Assume every plugin is supported until addLink is called and we can get the actual list | 58 | //Assume every plugin is supported until addLink is called and we can get the actual list | ||
63 | m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); | 59 | m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); | ||
64 | 60 | | |||
65 | connect(this, &Device::pairingError, this, &warn); | 61 | connect(this, &Device::pairingError, this, &warn); | ||
66 | } | 62 | } | ||
67 | 63 | | |||
68 | Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLink* dl) | 64 | Device::Device(QObject* parent, const NetworkPacket& identityPacket, DeviceLink* dl) | ||
69 | : QObject(parent) | 65 | : QObject(parent) | ||
70 | , m_deviceId(identityPackage.get<QString>(QStringLiteral("deviceId"))) | 66 | , m_deviceId(identityPacket.get<QString>(QStringLiteral("deviceId"))) | ||
71 | , m_deviceName(identityPackage.get<QString>(QStringLiteral("deviceName"))) | 67 | , m_deviceName(identityPacket.get<QString>(QStringLiteral("deviceName"))) | ||
72 | { | 68 | { | ||
73 | addLink(identityPackage, dl); | 69 | addLink(identityPacket, dl); | ||
74 | 70 | | |||
75 | //Register in bus | 71 | //Register in bus | ||
76 | QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); | 72 | QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors); | ||
77 | 73 | | |||
78 | connect(this, &Device::pairingError, this, &warn); | 74 | connect(this, &Device::pairingError, this, &warn); | ||
79 | } | 75 | } | ||
80 | 76 | | |||
81 | Device::~Device() | 77 | Device::~Device() | ||
Show All 20 Lines | 94 | { | |||
102 | if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices | 98 | if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices | ||
103 | 99 | | |||
104 | PluginLoader* loader = PluginLoader::instance(); | 100 | PluginLoader* loader = PluginLoader::instance(); | ||
105 | 101 | | |||
106 | for (const QString& pluginName : qAsConst(m_supportedPlugins)) { | 102 | for (const QString& pluginName : qAsConst(m_supportedPlugins)) { | ||
107 | const KPluginMetaData service = loader->getPluginInfo(pluginName); | 103 | const KPluginMetaData service = loader->getPluginInfo(pluginName); | ||
108 | 104 | | |||
109 | const bool pluginEnabled = isPluginEnabled(pluginName); | 105 | const bool pluginEnabled = isPluginEnabled(pluginName); | ||
110 | const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPackageType")).toSet(); | 106 | const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPacketType")).toSet(); | ||
111 | 107 | | |||
112 | if (pluginEnabled) { | 108 | if (pluginEnabled) { | ||
113 | KdeConnectPlugin* plugin = m_plugins.take(pluginName); | 109 | KdeConnectPlugin* plugin = m_plugins.take(pluginName); | ||
114 | 110 | | |||
115 | if (!plugin) { | 111 | if (!plugin) { | ||
116 | plugin = loader->instantiatePluginForDevice(pluginName, this); | 112 | plugin = loader->instantiatePluginForDevice(pluginName, this); | ||
117 | } | 113 | } | ||
118 | Q_ASSERT(plugin); | 114 | Q_ASSERT(plugin); | ||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Line(s) | 180 | { | |||
201 | Q_ASSERT(isTrusted == this->isTrusted()); | 197 | Q_ASSERT(isTrusted == this->isTrusted()); | ||
202 | } | 198 | } | ||
203 | 199 | | |||
204 | static bool lessThan(DeviceLink* p1, DeviceLink* p2) | 200 | static bool lessThan(DeviceLink* p1, DeviceLink* p2) | ||
205 | { | 201 | { | ||
206 | return p1->provider()->priority() > p2->provider()->priority(); | 202 | return p1->provider()->priority() > p2->provider()->priority(); | ||
207 | } | 203 | } | ||
208 | 204 | | |||
209 | void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link) | 205 | void Device::addLink(const NetworkPacket& identityPacket, DeviceLink* link) | ||
210 | { | 206 | { | ||
211 | //qCDebug(KDECONNECT_CORE) << "Adding link to" << id() << "via" << link->provider(); | 207 | //qCDebug(KDECONNECT_CORE) << "Adding link to" << id() << "via" << link->provider(); | ||
212 | 208 | | |||
213 | setName(identityPackage.get<QString>(QStringLiteral("deviceName"))); | 209 | setName(identityPacket.get<QString>(QStringLiteral("deviceName"))); | ||
214 | m_deviceType = str2type(identityPackage.get<QString>(QStringLiteral("deviceType"))); | 210 | m_deviceType = str2type(identityPacket.get<QString>(QStringLiteral("deviceType"))); | ||
215 | 211 | | |||
216 | if (m_deviceLinks.contains(link)) | 212 | if (m_deviceLinks.contains(link)) | ||
217 | return; | 213 | return; | ||
218 | 214 | | |||
219 | m_protocolVersion = identityPackage.get<int>(QStringLiteral("protocolVersion"), -1); | 215 | m_protocolVersion = identityPacket.get<int>(QStringLiteral("protocolVersion"), -1); | ||
220 | if (m_protocolVersion != NetworkPackage::s_protocolVersion) { | 216 | if (m_protocolVersion != NetworkPacket::s_protocolVersion) { | ||
221 | qCWarning(KDECONNECT_CORE) << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPackage::s_protocolVersion; | 217 | qCWarning(KDECONNECT_CORE) << m_deviceName << "- warning, device uses a different protocol version" << m_protocolVersion << "expected" << NetworkPacket::s_protocolVersion; | ||
222 | } | 218 | } | ||
223 | 219 | | |||
224 | connect(link, &QObject::destroyed, | 220 | connect(link, &QObject::destroyed, | ||
225 | this, &Device::linkDestroyed); | 221 | this, &Device::linkDestroyed); | ||
226 | 222 | | |||
227 | m_deviceLinks.append(link); | 223 | m_deviceLinks.append(link); | ||
228 | 224 | | |||
229 | //Theoretically we will never add two links from the same provider (the provider should destroy | 225 | //Theoretically we will never add two links from the same provider (the provider should destroy | ||
230 | //the old one before this is called), so we do not have to worry about destroying old links. | 226 | //the old one before this is called), so we do not have to worry about destroying old links. | ||
231 | //-- Actually, we should not destroy them or the provider will store an invalid ref! | 227 | //-- Actually, we should not destroy them or the provider will store an invalid ref! | ||
232 | 228 | | |||
233 | connect(link, &DeviceLink::receivedPackage, | 229 | connect(link, &DeviceLink::receivedPacket, | ||
234 | this, &Device::privateReceivedPackage); | 230 | this, &Device::privateReceivedPacket); | ||
235 | 231 | | |||
236 | std::sort(m_deviceLinks.begin(), m_deviceLinks.end(), lessThan); | 232 | std::sort(m_deviceLinks.begin(), m_deviceLinks.end(), lessThan); | ||
237 | 233 | | |||
238 | const bool capabilitiesSupported = identityPackage.has(QStringLiteral("incomingCapabilities")) || identityPackage.has(QStringLiteral("outgoingCapabilities")); | 234 | const bool capabilitiesSupported = identityPacket.has(QStringLiteral("incomingCapabilities")) || identityPacket.has(QStringLiteral("outgoingCapabilities")); | ||
239 | if (capabilitiesSupported) { | 235 | if (capabilitiesSupported) { | ||
240 | const QSet<QString> outgoingCapabilities = identityPackage.get<QStringList>(QStringLiteral("outgoingCapabilities")).toSet() | 236 | const QSet<QString> outgoingCapabilities = identityPacket.get<QStringList>(QStringLiteral("outgoingCapabilities")).toSet() | ||
241 | , incomingCapabilities = identityPackage.get<QStringList>(QStringLiteral("incomingCapabilities")).toSet(); | 237 | , incomingCapabilities = identityPacket.get<QStringList>(QStringLiteral("incomingCapabilities")).toSet(); | ||
242 | 238 | | |||
243 | m_supportedPlugins = PluginLoader::instance()->pluginsForCapabilities(incomingCapabilities, outgoingCapabilities); | 239 | m_supportedPlugins = PluginLoader::instance()->pluginsForCapabilities(incomingCapabilities, outgoingCapabilities); | ||
244 | //qDebug() << "new plugins for" << m_deviceName << m_supportedPlugins << incomingCapabilities << outgoingCapabilities; | 240 | //qDebug() << "new plugins for" << m_deviceName << m_supportedPlugins << incomingCapabilities << outgoingCapabilities; | ||
245 | } else { | 241 | } else { | ||
246 | m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); | 242 | m_supportedPlugins = PluginLoader::instance()->getPluginList().toSet(); | ||
247 | } | 243 | } | ||
248 | 244 | | |||
249 | reloadPlugins(); | 245 | reloadPlugins(); | ||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Line(s) | 308 | { | |||
315 | //qCDebug(KDECONNECT_CORE) << "RemoveLink" << m_deviceLinks.size() << "links remaining"; | 311 | //qCDebug(KDECONNECT_CORE) << "RemoveLink" << m_deviceLinks.size() << "links remaining"; | ||
316 | 312 | | |||
317 | if (m_deviceLinks.isEmpty()) { | 313 | if (m_deviceLinks.isEmpty()) { | ||
318 | reloadPlugins(); | 314 | reloadPlugins(); | ||
319 | Q_EMIT reachableChanged(false); | 315 | Q_EMIT reachableChanged(false); | ||
320 | } | 316 | } | ||
321 | } | 317 | } | ||
322 | 318 | | |||
323 | bool Device::sendPackage(NetworkPackage& np) | 319 | bool Device::sendPacket(NetworkPacket& np) | ||
324 | { | 320 | { | ||
325 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | 321 | Q_ASSERT(np.type() != PACKET_TYPE_PAIR); | ||
326 | Q_ASSERT(isTrusted()); | 322 | Q_ASSERT(isTrusted()); | ||
327 | 323 | | |||
328 | //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data | 324 | //Maybe we could block here any packet that is not an identity or a pairing packet to prevent sending non encrypted data | ||
329 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | 325 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
330 | if (dl->sendPackage(np)) return true; | 326 | if (dl->sendPacket(np)) return true; | ||
331 | } | 327 | } | ||
332 | 328 | | |||
333 | return false; | 329 | return false; | ||
334 | } | 330 | } | ||
335 | 331 | | |||
336 | void Device::privateReceivedPackage(const NetworkPackage& np) | 332 | void Device::privateReceivedPacket(const NetworkPacket& np) | ||
337 | { | 333 | { | ||
338 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | 334 | Q_ASSERT(np.type() != PACKET_TYPE_PAIR); | ||
339 | if (isTrusted()) { | 335 | if (isTrusted()) { | ||
340 | const QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingCapability.values(np.type()); | 336 | const QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingCapability.values(np.type()); | ||
341 | if (plugins.isEmpty()) { | 337 | if (plugins.isEmpty()) { | ||
342 | qWarning() << "discarding unsupported package" << np.type() << "for" << name(); | 338 | qWarning() << "discarding unsupported packet" << np.type() << "for" << name(); | ||
343 | } | 339 | } | ||
344 | for (KdeConnectPlugin* plugin : plugins) { | 340 | for (KdeConnectPlugin* plugin : plugins) { | ||
345 | plugin->receivePackage(np); | 341 | plugin->receivePacket(np); | ||
346 | } | 342 | } | ||
347 | } else { | 343 | } else { | ||
348 | qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring package" << np.type(); | 344 | qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring packet" << np.type(); | ||
349 | unpair(); | 345 | unpair(); | ||
350 | } | 346 | } | ||
351 | 347 | | |||
352 | } | 348 | } | ||
353 | 349 | | |||
354 | bool Device::isTrusted() const | 350 | bool Device::isTrusted() const | ||
355 | { | 351 | { | ||
356 | return KdeConnectConfig::instance()->trustedDevices().contains(id()); | 352 | return KdeConnectConfig::instance()->trustedDevices().contains(id()); | ||
▲ Show 20 Lines • Show All 133 Lines • Show Last 20 Lines |