Unverified Commit d711547a authored by Saikrishna Arcot's avatar Saikrishna Arcot Committed by Àlex Fiestas

Add initial Bluetooth backend implementation.

KDE Connect runs, and the bluetooth service gets published in the SDP
(service discovery protocol), which other devices use to determine what
services are available.

The getPairedDevices() method in the link provider class uses D-Bus to get
the list of paired devices, since Qt doesn't have a method giving that
information. As a result, that part of the code only works on Linux.

REVIEW: 122174
parent 937d919c
......@@ -10,9 +10,11 @@ cmake_minimum_required(VERSION 2.8.12)
find_package(ECM 0.0.9 REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_SOURCE_DIR}/cmake)
find_package(Qt5 5.2 REQUIRED COMPONENTS Quick)
find_package(Qt5 5.2 REQUIRED COMPONENTS Quick Bluetooth)
find_package(KF5 REQUIRED COMPONENTS I18n ConfigWidgets DBusAddons)
find_package(KF5DocTools)
find_package(Qca-qt5 2.1.0 REQUIRED)
include_directories(${CMAKE_SOURCE_DIR})
......
......@@ -8,6 +8,7 @@ include_directories(
add_subdirectory(backends/lan)
add_subdirectory(backends/loopback)
add_subdirectory(backends/bluetooth)
find_package(KF5Notifications 5.9 REQUIRED)
......@@ -17,6 +18,7 @@ set(kdeconnectcore_SRCS
backends/linkprovider.cpp
backends/devicelink.cpp
backends/pairinghandler.cpp
backends/devicelinereader.cpp
kdeconnectplugin.cpp
kdeconnectpluginconfig.cpp
......@@ -37,6 +39,7 @@ PUBLIC
Qt5::Network
KF5::CoreAddons
qca-qt5
Qt5::Bluetooth
PRIVATE
Qt5::DBus
Qt5::Gui
......
set(backends_kdeconnect_SRCS
${backends_kdeconnect_SRCS}
backends/bluetooth/bluetoothlinkprovider.cpp
backends/bluetooth/bluetoothdevicelink.cpp
PARENT_SCOPE
)
/**
* Copyright 2015 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"
BluetoothDeviceLink::BluetoothDeviceLink(const QString& deviceId, LinkProvider* parent, QBluetoothSocket* socket)
: DeviceLink(deviceId, parent)
, mBluetoothSocket(new DeviceLineReader(socket))
{
connect(mBluetoothSocket, SIGNAL(readyRead()),
this, SLOT(dataReceived()));
//We take ownership of the socket.
//When the link provider distroys us,
//the socket (and the reader) will be
//destroyed as well
connect(mBluetoothSocket, SIGNAL(disconnected()),
this, SLOT(deleteLater()));
mBluetoothSocket->setParent(this);
}
/*void BluetoothDeviceLink::sendPayload(const QSharedPointer<QIODevice>& mInput)
{
while ( mInput->bytesAvailable() > 0 )
{
qint64 bytes = qMin(mInput->bytesAvailable(), (qint64)4096);
int w = mBluetoothSocket->write(mInput->read(bytes));
if (w<0) {
qWarning() << "error when writing data to upload" << bytes << mInput->bytesAvailable();
break;
}
else
{
while ( mBluetoothSocket->flush() );
}
}
mInput->close();
}*/
bool BluetoothDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np)
{
np.encrypt(key);
return sendPackage(np);
}
bool BluetoothDeviceLink::sendPackage(NetworkPackage& np)
{
int written = mBluetoothSocket->write(np.serialize());
if (np.hasPayload()) {
qWarning() << "Bluetooth backend does not support payloads.";
}
return (written != -1);
}
void BluetoothDeviceLink::dataReceived()
{
if (mBluetoothSocket->bytesAvailable() == 0) return;
const QByteArray package = mBluetoothSocket->readLine();
//kDebug(debugArea()) << "BluetoothDeviceLink dataReceived" << package;
NetworkPackage unserialized(QString::null);
NetworkPackage::unserialize(package, &unserialized);
if (unserialized.isEncrypted()) {
//mPrivateKey should always be set when device link is added to device, no null-checking done here
NetworkPackage decrypted(QString::null);
unserialized.decrypt(mPrivateKey, &decrypted);
if (decrypted.hasPayloadTransferInfo()) {
qWarning() << "Bluetooth backend does not support payloads.";
}
Q_EMIT receivedPackage(decrypted);
} else {
if (unserialized.hasPayloadTransferInfo()) {
qWarning() << "Ignoring unencrypted payload";
}
Q_EMIT receivedPackage(unserialized);
}
if (mBluetoothSocket->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
}
}
/**
* Copyright 2015 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/>.
*/
#ifndef BLUETOOTHDEVICELINK_H
#define BLUETOOTHDEVICELINK_H
#include <QObject>
#include <QString>
#include <QtBluetooth/QBluetoothSocket>
#include "../devicelink.h"
#include "../devicelinereader.h"
class BluetoothDeviceLink
: public DeviceLink
{
Q_OBJECT
public:
BluetoothDeviceLink(const QString& deviceId, LinkProvider* parent, QBluetoothSocket* socket);
bool sendPackage(NetworkPackage& np);
bool sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np);
private Q_SLOTS:
void dataReceived();
private:
DeviceLineReader* mBluetoothSocket;
//void sendPayload(const QSharedPointer<QIODevice>& mInput);
void sendMessage(const QString mMessage);
};
#endif
This diff is collapsed.
/**
* Copyright 2015 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/>.
*/
#ifndef BLUETOOTHLINKPROVIDER_H
#define BLUETOOTHLINKPROVIDER_H
#include <QObject>
#include <QTimer>
#include <QtBluetooth/QBluetoothAddress>
#include <QtBluetooth/QBluetoothLocalDevice>
#include <QtBluetooth/QBluetoothUuid>
#include <QtBluetooth/QBluetoothServer>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth/QBluetoothServiceDiscoveryAgent>
#include <QtBluetooth/QBluetoothSocket>
#include "../linkprovider.h"
class BluetoothDeviceLink;
class BluetoothLinkProvider
: public LinkProvider
{
Q_OBJECT
public:
BluetoothLinkProvider();
~BluetoothLinkProvider();
QString name() { return "BluetoothLinkProvider"; }
int priority() { return PRIORITY_MEDIUM; }
public Q_SLOTS:
virtual void onNetworkChange(QNetworkSession::State state);
virtual void onStart();
virtual void onStop();
void connectError();
void serviceDiscoveryFinished();
private Q_SLOTS:
void deviceLinkDestroyed(QObject* destroyedDeviceLink);
void socketDisconnected();
void connectToPairedDevices();
void serverNewConnection();
void serverDataReceived();
void clientConnected();
void clientIdentityReceived();
private:
void addLink(BluetoothDeviceLink* deviceLink, const QString& deviceId);
QList<QBluetoothAddress> getPairedDevices();
QBluetoothLocalDevice mDevice;
QBluetoothUuid mServiceUuid;
QBluetoothServer* mBluetoothServer;
QBluetoothServiceInfo mKdeconnectService;
QBluetoothServiceDiscoveryAgent* mServiceDiscoveryAgent;
QTimer* connectTimer;
QMap<QString, DeviceLink*> mLinks;
QMap<QBluetoothAddress, QBluetoothSocket*> mSockets;
};
#endif
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
* Copyright 2014 Alejandro Fiestas Olivares <afiestas@kde.org>
*
* 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 "devicelinereader.h"
DeviceLineReader::DeviceLineReader(QIODevice* device, QObject* parent)
: QObject(parent)
, mDevice(device)
{
connect(mDevice, SIGNAL(readyRead()),
this, SLOT(dataReceived()));
connect(mDevice, SIGNAL(disconnected()),
this, SIGNAL(disconnected()));
}
void DeviceLineReader::dataReceived()
{
while(mDevice->canReadLine()) {
const QByteArray line = mDevice->readLine();
if (line.length() > 1) {
mPackages.enqueue(line);//we don't want single \n
}
}
//If we still have things to read from the device, call dataReceived again
//We do this manually because we do not trust readyRead to be emitted again
//So we call this method again just in case.
if (mDevice->bytesAvailable() > 0) {
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
return;
}
//If we have any packages, tell it to the world.
if (!mPackages.isEmpty()) {
Q_EMIT readyRead();
}
}
/**
* Copyright 2013 Albert Vaca <albertvaka@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/>.
*/
#ifndef DEVICELINEREADER_H
#define DEVICELINEREADER_H
#include <QObject>
#include <QString>
#include <QQueue>
#include <QIODevice>
/*
* Encapsulates a QIODevice and implements the same methods of its API that are
* used by LanDeviceLink, but readyRead is emitted only when a newline is found.
*/
class DeviceLineReader
: public QObject
{
Q_OBJECT
public:
DeviceLineReader(QIODevice* device, QObject* parent = 0);
QByteArray readLine() { return mPackages.dequeue(); }
qint64 write(const QByteArray& data) { return mDevice->write(data); }
qint64 bytesAvailable() { return mPackages.size(); }
Q_SIGNALS:
void readyRead();
void disconnected();
private Q_SLOTS:
void dataReceived();
private:
QByteArray lastChunk;
QIODevice* mDevice;
QQueue<QByteArray> mPackages;
};
#endif
......@@ -28,6 +28,7 @@
#include "core_debug.h"
#include "kdeconnectconfig.h"
#include "networkpackage.h"
#include "backends/bluetooth/bluetoothlinkprovider.h"
#include "backends/lan/lanlinkprovider.h"
#include "backends/loopback/loopbacklinkprovider.h"
#include "device.h"
......@@ -104,6 +105,11 @@ void Daemon::releaseDiscoveryMode(const QString &key)
d->mDiscoveryModeAcquisitions.remove(key);
//Load backends
//d->mLinkProviders.insert(new LoopbackLinkProvider());
d->mLinkProviders.insert(new LanLinkProvider());
d->mLinkProviders.insert(new BluetoothLinkProvider());
if (oldState != d->mDiscoveryModeAcquisitions.isEmpty()) {
cleanDevices();
}
......
/**
* Copyright 2013 Albert Vaca <albertvaka@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 "kdeconnectplugin_config.h"
#include <kcm/kcm.h>
KdeConnectPluginConfig::KdeConnectPluginConfig(QObject* parent, const QVariantList& args)
: KCModule(SftpConfigFactory::componentData(), parent)
, mGlobalConfig(KSharedConfig::openConfig("kdeconnect/plugins/pausemusic"))
{
KdeConnectKcm* kcm = 0;
QObject* kcmCandidate = parent->parentWidget();
while (kcmCandidate) {
//qDebug() << kcmCandidate;
if (kcmCandidate->objectName() == "KdeConnectKcm") {
kcm = qobject_cast<KdeConnectKcm*>(kcmCandidate);
break;
}
kcmCandidate = kcmCandidate->parent();
}
if (kcm) {
kcm->selectedDevice()->id();
}
}
KdeConnectPluginConfig::~KdeConnectPluginConfig()
{
}
/**
* Copyright 2013 Albert Vaca <albertvaka@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/>.
*/
#ifndef KDECONNECTPLUGINCONFIG_H
#define KDECONNECTPLUGINCONFIG_H
#include "kdeconnectcore_export.h"
#include <kcmodule.h>
struct DeviceDbusInterface;
class KDECONNECTCORE_EXPORT KdeConnectPluginConfig
: public KCModule
{
Q_OBJECT
public:
KdeConnectPluginConfig(QObject* parent, const QVariantList& args);
virtual ~KdeConnectPluginConfig();
QString deviceId() { return mDeviceId; }
//TODO: Add these two to the Plugin as well
KSharedConfigPtr deviceConfig() { return mDeviceConfig; }
KSharedConfigPtr globalConfig() { return mGlobalConfig; }
private:
QString mDeviceId;
KSharedConfigPtr mGlobalConfig;
KSharedConfigPtr mDeviceConfig;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment