Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/bluetooth/adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "adapter.hpp"

#include <fcntl.h>
#include <linux/rfkill.h>
#include <qcontainerfwd.h>
#include <qdbusconnection.h>
#include <qdbusextratypes.h>
Expand All @@ -8,8 +10,11 @@
#include <qdebug.h>
#include <qlogging.h>
#include <qloggingcategory.h>
#include <qscopeguard.h>
#include <qstring.h>
#include <qtypes.h>
#include <sys/types.h>
#include <unistd.h>

#include "../core/logcat.hpp"
#include "../dbus/properties.hpp"
Expand Down Expand Up @@ -43,6 +48,8 @@ BluetoothAdapter::BluetoothAdapter(const QString& path, QObject* parent): QObjec
}

this->properties.setInterface(this->mInterface);

QObject::connect(this, &BluetoothAdapter::stateChanged, this, &BluetoothAdapter::onStateChanged);
}

QString BluetoothAdapter::adapterId() const {
Expand All @@ -51,17 +58,58 @@ QString BluetoothAdapter::adapterId() const {
}

void BluetoothAdapter::setEnabled(bool enabled) {
this->mPendingEnable = false;
if (enabled == this->bEnabled) return;

if (enabled && this->bState == BluetoothAdapterState::Blocked) {
qCCritical(logAdapter) << "Cannot enable adapter because it is blocked by rfkill.";
// logind grants the seat user rw on /dev/rfkill, so the soft block can be lifted here.
if (!tryRfkillUnblock()) {
qCCritical(logAdapter) << "Cannot enable adapter" << this
<< "because it is blocked by rfkill.";
return;
}

// bluez learns about the unblock asynchronously via its own /dev/rfkill
// reader and rejects a Powered write that races it. Retry once PowerState
// leaves "off-blocked".
this->mPendingEnable = true;
qCDebug(logAdapter) << "Adapter" << this << "was rfkill-blocked; unblocked, waiting for bluez.";
return;
}

this->bEnabled = enabled;
this->pEnabled.write();
}

void BluetoothAdapter::onStateChanged() {
if (this->mPendingEnable && this->bState != BluetoothAdapterState::Blocked) {
this->setEnabled(true);
}
}

bool BluetoothAdapter::tryRfkillUnblock() {
auto fd = open("/dev/rfkill", O_WRONLY | O_CLOEXEC);
if (fd == -1) {
qCWarning(logAdapter).nospace()
<< "Failed to open /dev/rfkill for writing: " << qt_error_string();
return false;
}
auto fdGuard = qScopeGuard([&] { close(fd); });

struct rfkill_event ev {};
ev.type = RFKILL_TYPE_BLUETOOTH;
ev.op = RFKILL_OP_CHANGE_ALL;
ev.soft = 0;

if (write(fd, &ev, sizeof(ev)) < static_cast<ssize_t>(RFKILL_EVENT_SIZE_V1)) {
qCWarning(logAdapter).nospace()
<< "Failed to write rfkill unblock event: " << qt_error_string();
return false;
}

return true;
}

void BluetoothAdapter::setDiscoverable(bool discoverable) {
if (discoverable == this->bDiscoverable) return;
this->bDiscoverable = discoverable;
Expand Down
6 changes: 6 additions & 0 deletions src/bluetooth/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class BluetoothAdapter: public QObject {
void startDiscovery();
void stopDiscovery();

private slots:
void onStateChanged();

signals:
void nameChanged();
void enabledChanged();
Expand All @@ -143,8 +146,11 @@ class BluetoothAdapter: public QObject {
void pairableTimeoutChanged();

private:
static bool tryRfkillUnblock();

DBusBluezAdapterInterface* mInterface = nullptr;
ObjectModel<BluetoothDevice> mDevices {this};
bool mPendingEnable = false;

// clang-format off
Q_OBJECT_BINDABLE_PROPERTY(BluetoothAdapter, QString, bName, &BluetoothAdapter::nameChanged);
Expand Down
Loading