WIP: Add bluetooth

pull/55/head
reionwong 5 years ago
parent 19b24b71b7
commit cff29d68a3

@ -17,6 +17,7 @@ find_package(X11 REQUIRED)
find_package(Freetype REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(Libcrypt REQUIRED)
find_package(KF5BluezQt REQUIRED)
# find_package(QApt REQUIRED)
find_package(KF5NetworkManagerQt REQUIRED)
@ -53,6 +54,8 @@ set(SRCS
src/datetime/timezonemap.cpp
src/datetime/timedated_interface.cpp
src/touchpad.cpp
src/bluetooth/bluetoothmanager.cpp
src/bluetooth/bluetoothagent.cpp
# src/update/updatemodel.cpp
)
@ -81,6 +84,7 @@ target_link_libraries(${PROJECT_NAME}
KF5::NetworkManagerQt
KF5::ModemManagerQt
KF5::BluezQt
PkgConfig::FontConfig
${FREETYPE_LIBRARIES}

@ -25,6 +25,8 @@
#include "datetime/time.h"
#include "datetime/timezonemap.h"
#include "bluetooth/bluetoothmanager.h"
static QObject *passwordSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
@ -77,6 +79,7 @@ Application::Application(int &argc, char **argv)
qmlRegisterType<Time>(uri, 1, 0, "Time");
qmlRegisterType<TimeZoneMap>(uri, 1, 0, "TimeZoneMap");
qmlRegisterType<Touchpad>(uri, 1, 0, "Touchpad");
qmlRegisterType<BluetoothManager>(uri, 1, 0, "BluetoothManager");
qmlRegisterSingletonType<Password>(uri, 1, 0, "Password", passwordSingleton);

@ -0,0 +1,179 @@
/*
* SPDX-FileCopyrightText: 2010 Alex Fiestas <alex@eyeos.org>
* SPDX-FileCopyrightText: 2010 UFO Coders <info@ufocoders.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "bluetoothagent.h"
#include <QDBusObjectPath>
#include <QFile>
#include <QStandardPaths>
#include <QXmlStreamReader>
#include <BluezQt/Device>
#include <QDebug>
#include <QThread>
#include <QThreadStorage>
#include <unistd.h>
static int cRandom()
{
static QThreadStorage<bool> initialized_threads;
if (!initialized_threads.localData()) {
unsigned int seed;
initialized_threads.setLocalData(true);
QFile urandom(QStringLiteral("/dev/urandom"));
bool opened = urandom.open(QIODevice::ReadOnly | QIODevice::Unbuffered);
if (!opened || urandom.read(reinterpret_cast<char *>(&seed), sizeof(seed)) != sizeof(seed)) {
// silence warnings about use of deprecated qsrand()/qrand()
// Porting to QRandomGenerator::global() instead might result in no new seed set for the generator behind qrand()
// which then might affect other places indirectly relying on this.
// So just keeping the old calls here, as this method is also deprecated and will disappear together with qsrand/qrand.
QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
// No /dev/urandom... try something else.
qsrand(getpid());
seed = qrand() ^ time(nullptr) ^ reinterpret_cast<quintptr>(QThread::currentThread());
}
qsrand(seed);
}
return qrand();
QT_WARNING_POP
}
BluetoothAgent::BluetoothAgent(QObject *parent)
: BluezQt::Agent(parent)
, m_fromDatabase(false)
{
}
QString BluetoothAgent::pin()
{
return m_pin;
}
void BluetoothAgent::setPin(const QString &pin)
{
m_pin = pin;
m_fromDatabase = false;
}
bool BluetoothAgent::isFromDatabase()
{
return m_fromDatabase;
}
QString BluetoothAgent::getPin(BluezQt::DevicePtr device)
{
m_fromDatabase = false;
m_pin = QString::number(cRandom());
m_pin = m_pin.left(6);
const QString &xmlPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("pin-code-database.xml"));
QFile file(xmlPath);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Can't open the pin-code-database.xml";
return m_pin;
}
QXmlStreamReader xml(&file);
QString deviceType = BluezQt::Device::typeToString(device->type());
if (deviceType == QLatin1String("audiovideo")) {
deviceType = QStringLiteral("audio");
}
while (!xml.atEnd()) {
xml.readNext();
if (xml.name() != QLatin1String("device")) {
continue;
}
QXmlStreamAttributes attr = xml.attributes();
if (attr.count() == 0) {
continue;
}
if (attr.hasAttribute(QLatin1String("type")) && attr.value(QLatin1String("type")) != QLatin1String("any")) {
if (deviceType != attr.value(QLatin1String("type")).toString()) {
continue;
}
}
if (attr.hasAttribute(QLatin1String("oui"))) {
if (!device->address().startsWith(attr.value(QLatin1String("oui")).toString())) {
continue;
}
}
if (attr.hasAttribute(QLatin1String("name"))) {
if (device->name() != attr.value(QLatin1String("name")).toString()) {
continue;
}
}
m_pin = attr.value(QLatin1String("pin")).toString();
m_fromDatabase = true;
if (m_pin.startsWith(QLatin1String("max:"))) {
m_fromDatabase = false;
int num = m_pin.rightRef(m_pin.length() - 4).toInt();
m_pin = QString::number(cRandom()).left(num);
}
qDebug() << "PIN: " << m_pin;
return m_pin;
}
return m_pin;
}
QDBusObjectPath BluetoothAgent::objectPath() const
{
return QDBusObjectPath(QStringLiteral("/agent"));
}
void BluetoothAgent::requestPinCode(BluezQt::DevicePtr device, const BluezQt::Request<QString> &req)
{
qDebug() << "AGENT-RequestPinCode" << device->ubi();
Q_EMIT pinRequested(m_pin);
req.accept(m_pin);
}
void BluetoothAgent::displayPinCode(BluezQt::DevicePtr device, const QString &pinCode)
{
qDebug() << "AGENT-DisplayPinCode" << device->ubi() << pinCode;
Q_EMIT pinRequested(pinCode);
}
void BluetoothAgent::requestPasskey(BluezQt::DevicePtr device, const BluezQt::Request<quint32> &req)
{
qDebug() << "AGENT-RequestPasskey" << device->ubi();
Q_EMIT pinRequested(m_pin);
req.accept(m_pin.toUInt());
}
void BluetoothAgent::displayPasskey(BluezQt::DevicePtr device, const QString &passkey, const QString &entered)
{
Q_UNUSED(entered);
qDebug() << "AGENT-DisplayPasskey" << device->ubi() << passkey;
Q_EMIT pinRequested(passkey);
}
void BluetoothAgent::requestConfirmation(BluezQt::DevicePtr device, const QString &passkey, const BluezQt::Request<> &req)
{
qDebug() << "AGENT-RequestConfirmation " << device->ubi() << passkey;
Q_EMIT confirmationRequested(passkey, req);
}

@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: 2010 Alex Fiestas <alex@eyeos.org>
* SPDX-FileCopyrightText: 2010 UFO Coders <info@ufocoders.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef BLUETOOTHAGENT_H
#define BLUETOOTHAGENT_H
#include <BluezQt/Agent>
class BluetoothAgent : public BluezQt::Agent
{
Q_OBJECT
public:
explicit BluetoothAgent(QObject *parent = nullptr);
QString pin();
void setPin(const QString &pin);
bool isFromDatabase();
QString getPin(BluezQt::DevicePtr device);
QDBusObjectPath objectPath() const override;
void requestPinCode(BluezQt::DevicePtr device, const BluezQt::Request<QString> &req) override;
void displayPinCode(BluezQt::DevicePtr device, const QString &pinCode) override;
void requestPasskey(BluezQt::DevicePtr device, const BluezQt::Request<quint32> &req) override;
void displayPasskey(BluezQt::DevicePtr device, const QString &passkey, const QString &entered) override;
void requestConfirmation(BluezQt::DevicePtr device, const QString &passkey, const BluezQt::Request<> &req) override;
Q_SIGNALS:
void pinRequested(const QString &pin);
void confirmationRequested(const QString &passkey, const BluezQt::Request<> &req);
private:
bool m_fromDatabase;
QString m_pin;
};
#endif // BluetoothAgent_H

@ -0,0 +1,70 @@
#include "bluetoothmanager.h"
#include <QDebug>
#include <BluezQt/InitManagerJob>
#include <BluezQt/Adapter>
BluetoothManager::BluetoothManager(QObject *parent)
: QObject(parent)
, m_agent(new BluetoothAgent(this))
{
m_manager = new BluezQt::Manager(this);
BluezQt::InitManagerJob *initJob = m_manager->init();
initJob->start();
connect(initJob, &BluezQt::InitManagerJob::result, this, &BluetoothManager::onInitJobResult);
connect(m_manager, &BluezQt::Manager::bluetoothBlockedChanged, this, [=] (bool blocked) {
if (!blocked) {
BluezQt::AdapterPtr adaptor = m_manager->adapters().first();
if (adaptor) {
if (!adaptor->isDiscoverable()) {
adaptor->setDiscoverable(true);
}
}
}
});
}
BluetoothManager::~BluetoothManager()
{
m_manager->unregisterAgent(m_agent);
delete m_agent;
delete m_manager;
}
void BluetoothManager::setName(const QString &name)
{
BluezQt::AdapterPtr adaptor = m_manager->usableAdapter();
adaptor->setName(name);
}
void BluetoothManager::onInitJobResult(BluezQt::InitManagerJob *job)
{
if (job->error()) {
qDebug() << "Init Bluetooth error";
return;
}
// Make sure to register agent when bluetoothd starts
operationalChanged(m_manager->isOperational());
connect(m_manager, &BluezQt::Manager::operationalChanged, this, &BluetoothManager::operationalChanged);
m_adapter = m_manager->usableAdapter();
if (m_adapter) {
setName("CutefishOS");
if (!m_adapter->isDiscoverable())
m_adapter->startDiscovery();
}
}
void BluetoothManager::operationalChanged(bool operational)
{
if (operational) {
m_manager->registerAgent(m_agent);
} else {
// Attempt to start bluetoothd
BluezQt::Manager::startService();
}
}

@ -0,0 +1,29 @@
#ifndef BLUETOOTHMANAGER_H
#define BLUETOOTHMANAGER_H
#include <QObject>
#include <BluezQt/Manager>
#include "bluetoothagent.h"
class BluetoothManager : public QObject
{
Q_OBJECT
public:
explicit BluetoothManager(QObject *parent = nullptr);
~BluetoothManager();
Q_INVOKABLE void setName(const QString &name);
private slots:
void onInitJobResult(BluezQt::InitManagerJob *job);
void operationalChanged(bool operational);
private:
BluezQt::Manager *m_manager;
BluetoothAgent *m_agent;
BluezQt::AdapterPtr m_adapter;
QString m_name;
};
#endif // BLUETOOTHMANAGER_H

@ -22,24 +22,37 @@ import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
import Cutefish.Settings 1.0
import FishUI 1.0 as FishUI
//import org.kde.bluezqt 1.0 as BluezQt
//import org.kde.plasma.private.bluetooth 1.0
import Cutefish.Bluez 1.0 as Bluez
import "../"
ItemPage {
headerTitle: qsTr("Bluetooth")
property bool bluetoothDisConnected: Bluez.Manager.bluetoothBlocked
onBluetoothDisConnectedChanged: {
bluetoothSwitch.checked = !bluetoothDisConnected
}
function setBluetoothEnabled(enabled) {
Bluez.Manager.bluetoothBlocked = !enabled
for (var i = 0; i < Bluez.Manager.adapters.length; ++i) {
var adapter = Bluez.Manager.adapters[i];
adapter.powered = enabled;
var adapter = Bluez.Manager.adapters[i]
console.log(adapter + ", " + enabled)
adapter.powered = enabled
}
}
Bluez.DevicesProxyModel {
id: devicesModel
sourceModel: Bluez.DevicesModel { }
}
BluetoothManager {
id: bluetoothMgr
}
// Label {
// id: noBluetoothMessage
// anchors.centerIn: parent
@ -80,23 +93,24 @@ ItemPage {
id: bluetoothSwitch
Layout.fillHeight: true
rightPadding: 0
checked: !Bluez.Manager.bluetoothBlocked
onCheckedChanged: setBluetoothEnabled(checked)
Component.onCompleted: bluetoothSwitch.checked = Bluez.Manager.operational
}
}
ListView {
visible: count > 0
property var itemHeight: 50
onCountChanged: {
console.log(count)
}
Layout.fillWidth: true
Layout.preferredHeight: itemHeight * count + ((count - 1) * spacing)
Bluez.DevicesProxyModel {
id: devicesModel
sourceModel: Bluez.DevicesModel { }
}
model: Bluez.Manager.bluetoothOperational ? devicesModel : []
model: devicesModel //Bluez.Manager.bluetoothOperational ? devicesModel : []
delegate: Item {
width: ListView.view.itemHeight

@ -52,13 +52,6 @@ Item {
border.width: 3
border.color: control.checked ? FishUI.Theme.highlightColor : "transparent"
Behavior on border.color {
ColorAnimation {
duration: 200
easing.type: Easing.OutSine
}
}
radius: FishUI.Theme.bigRadius + control.iconSpacing
visible: true

@ -78,12 +78,14 @@ Item {
// category: qsTr("Network and connection")
// }
// ListElement {
// title: qsTr("Bluetooth")
// name: "bluetooth"
// page: "qrc:/qml/BluetoothPage.qml"
// iconSource: "bluetooth.svg"
// }
ListElement {
title: qsTr("Bluetooth")
name: "bluetooth"
page: "qrc:/qml/Bluetooth/Main.qml"
iconSource: "bluetooth.svg"
iconColor: "#0067FF"
category: qsTr("Network and connection")
}
ListElement {
title: qsTr("Display")

Loading…
Cancel
Save