mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Separate BluetoothAdapter into Adapter and BluezAdapter classes.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
#include "internal/platform/implementation/linux/bluez_adapter_client_glue.h"
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -13,29 +14,19 @@ namespace linux {
|
||||
bool BluetoothAdapter::SetStatus(Status status) {
|
||||
try {
|
||||
bool val = status == api::BluetoothAdapter::Status::kEnabled;
|
||||
Powered(val);
|
||||
bluez_adapter_->Powered(val);
|
||||
return true;
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to set Powered status for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_SET_ERROR(bluez_adapter_, "Powered", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool BluetoothAdapter::IsEnabled() const {
|
||||
auto proxy = sdbus::createProxy(getProxy().getConnection(),
|
||||
bluez::SERVICE_DEST, getObjectPath());
|
||||
proxy->finishRegistration();
|
||||
|
||||
try {
|
||||
return proxy->getProperty("Powered").onInterface(INTERFACE_NAME);
|
||||
return bluez_adapter_->Powered();
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to get Powered status for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Powered", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -47,20 +38,11 @@ BluetoothAdapter::ScanMode BluetoothAdapter::GetScanMode() const {
|
||||
}
|
||||
|
||||
try {
|
||||
auto proxy = sdbus::createProxy(getProxy().getConnection(),
|
||||
bluez::SERVICE_DEST, getObjectPath());
|
||||
proxy->finishRegistration();
|
||||
|
||||
bool discoverable =
|
||||
proxy->getProperty("Discoverable").onInterface(INTERFACE_NAME);
|
||||
bool discoverable = bluez_adapter_->Discoverable();
|
||||
return discoverable ? ScanMode::kConnectableDiscoverable
|
||||
: ScanMode::kConnectable;
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__ << ": Got error '" << e.getName() << "' with message '"
|
||||
<< e.getMessage()
|
||||
<< "' while trying to get Discoverable status for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Discoverable", e);
|
||||
return ScanMode::kUnknown;
|
||||
}
|
||||
}
|
||||
@@ -75,13 +57,9 @@ bool BluetoothAdapter::SetScanMode(ScanMode scan_mode) {
|
||||
}
|
||||
|
||||
try {
|
||||
Discoverable(true);
|
||||
bluez_adapter_->Discoverable(true);
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__ << ": Got error '" << e.getName() << "' with message '"
|
||||
<< e.getMessage()
|
||||
<< "' while trying to set Discoverable status for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_SET_ERROR(bluez_adapter_, "Discoverable", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -95,50 +73,34 @@ bool BluetoothAdapter::SetScanMode(ScanMode scan_mode) {
|
||||
}
|
||||
|
||||
std::string BluetoothAdapter::GetName() const {
|
||||
auto proxy = sdbus::createProxy(getProxy().getConnection(),
|
||||
bluez::SERVICE_DEST, getObjectPath());
|
||||
proxy->finishRegistration();
|
||||
|
||||
try {
|
||||
return proxy->getProperty("Alias").onInterface(INTERFACE_NAME);
|
||||
return bluez_adapter_->Alias();
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to get Alias for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Alias", e);
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
|
||||
bool BluetoothAdapter::SetName(absl::string_view name, bool persist) {
|
||||
persist_name_ = persist;
|
||||
return SetName(name);
|
||||
}
|
||||
|
||||
bool BluetoothAdapter::SetName(absl::string_view name) {
|
||||
try {
|
||||
Alias(std::string(name));
|
||||
bluez_adapter_->Alias(std::string(name));
|
||||
return true;
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to set Alias for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_SET_ERROR(bluez_adapter_, "Alias", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BluetoothAdapter::GetMacAddress() const {
|
||||
auto proxy = sdbus::createProxy(getProxy().getConnection(),
|
||||
bluez::SERVICE_DEST, getObjectPath());
|
||||
proxy->finishRegistration();
|
||||
|
||||
try {
|
||||
return proxy->getProperty("Address").onInterface(INTERFACE_NAME);
|
||||
return bluez_adapter_->Address();
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to get Address for adapter "
|
||||
<< getObjectPath();
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Address", e);
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,37 @@
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
#include "internal/platform/implementation/linux/bluez_adapter_client_glue.h"
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothAdapter
|
||||
: public api::BluetoothAdapter,
|
||||
public sdbus::ProxyInterfaces<org::bluez::Adapter1_proxy> {
|
||||
class BluezAdapter : public sdbus::ProxyInterfaces<org::bluez::Adapter1_proxy> {
|
||||
public:
|
||||
BluetoothAdapter(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path)
|
||||
BluezAdapter(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path)
|
||||
: ProxyInterfaces(system_bus, bluez::SERVICE_DEST, adapter_object_path) {
|
||||
registerProxy();
|
||||
}
|
||||
~BluezAdapter() { unregisterProxy(); }
|
||||
};
|
||||
|
||||
~BluetoothAdapter() override { unregisterProxy(); }
|
||||
class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
public:
|
||||
BluetoothAdapter(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path)
|
||||
: bluez_adapter_(
|
||||
std::make_unique<BluezAdapter>(system_bus, adapter_object_path)) {}
|
||||
|
||||
~BluetoothAdapter() override {
|
||||
if (!persist_name_) {
|
||||
NEARBY_LOGS(INFO) << __func__ << "Resetting adapter Alias";
|
||||
try {
|
||||
bluez_adapter_->Alias("");
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_PROPERTY_SET_ERROR(bluez_adapter_, "Alias", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SetStatus(Status status) override;
|
||||
bool IsEnabled() const override;
|
||||
@@ -33,6 +50,26 @@ public:
|
||||
bool SetName(absl::string_view name) override;
|
||||
bool SetName(absl::string_view name, bool persist) override;
|
||||
std::string GetMacAddress() const override;
|
||||
|
||||
bool RemoveDeviceByObjectPath(const sdbus::ObjectPath &device_object_path) {
|
||||
try {
|
||||
bluez_adapter_->RemoveDevice(device_object_path);
|
||||
return true;
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(bluez_adapter_, "RemoveDevice", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
sdbus::ObjectPath GetObjectPath() const {
|
||||
return bluez_adapter_->getObjectPath();
|
||||
}
|
||||
|
||||
BluezAdapter &GetBluezAdapterObject() { return *bluez_adapter_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<BluezAdapter> bluez_adapter_;
|
||||
bool persist_name_;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -40,7 +40,8 @@ void BluetoothClassicMedium::onInterfacesAdded(
|
||||
&interfacesAndProperties) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << "New intefaces added at " << object;
|
||||
|
||||
auto path_prefix = absl::Substitute("$0/dev_", adapter_->getObjectPath());
|
||||
auto path_prefix = absl::Substitute(
|
||||
"$0/dev_", adapter_->GetBluezAdapterObject().getObjectPath());
|
||||
if (object.find(path_prefix) != 0) {
|
||||
return;
|
||||
}
|
||||
@@ -78,7 +79,7 @@ void BluetoothClassicMedium::onInterfacesRemoved(
|
||||
const std::vector<std::string> &interfaces) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Intefaces removed at " << object;
|
||||
|
||||
auto path_prefix = absl::Substitute("$0/dev_", adapter_->getObjectPath());
|
||||
auto path_prefix = absl::Substitute("$0/dev_", adapter_->GetObjectPath());
|
||||
if (object.find(path_prefix) != 0) {
|
||||
return;
|
||||
}
|
||||
@@ -120,10 +121,11 @@ bool BluetoothClassicMedium::StartDiscovery(
|
||||
|
||||
try {
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Starting discovery on "
|
||||
<< adapter_->getObjectPath();
|
||||
adapter_->StartDiscovery();
|
||||
<< adapter_->GetObjectPath();
|
||||
adapter_->GetBluezAdapterObject().StartDiscovery();
|
||||
} catch (const sdbus::Error &e) {
|
||||
BLUEZ_LOG_METHOD_CALL_ERROR(adapter_, "StartDiscovery", e);
|
||||
DBUS_LOG_METHOD_CALL_ERROR(&adapter_->GetBluezAdapterObject(),
|
||||
"StartDiscovery", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -131,15 +133,17 @@ bool BluetoothClassicMedium::StartDiscovery(
|
||||
}
|
||||
|
||||
bool BluetoothClassicMedium::StopDiscovery() {
|
||||
auto &adapter = adapter_->GetBluezAdapterObject();
|
||||
|
||||
try {
|
||||
NEARBY_LOGS(INFO) << __func__ << "Stopping discovery on "
|
||||
<< adapter_->getObjectPath();
|
||||
<< adapter.getObjectPath();
|
||||
|
||||
absl::MutexLock l(&this->discovery_cb_lock_);
|
||||
adapter_->StopDiscovery();
|
||||
adapter.StopDiscovery();
|
||||
this->discovery_cb_.reset();
|
||||
} catch (const sdbus::Error &e) {
|
||||
BLUEZ_LOG_METHOD_CALL_ERROR(adapter_, "StopDiscovery", e);
|
||||
DBUS_LOG_METHOD_CALL_ERROR(&adapter, "StopDiscovery", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -151,7 +155,7 @@ BluetoothClassicMedium::ConnectToService(api::BluetoothDevice &remote_device,
|
||||
const std::string &service_uuid,
|
||||
CancellationFlag *cancellation_flag) {
|
||||
auto device_object_path = bluez::device_object_path(
|
||||
adapter_->getObjectPath(), remote_device.GetMacAddress());
|
||||
adapter_->GetObjectPath(), remote_device.GetMacAddress());
|
||||
if (!profile_manager_->ProfileRegistered(service_uuid)) {
|
||||
if (!profile_manager_->Register("", service_uuid)) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Could not register profile "
|
||||
|
||||
@@ -100,15 +100,15 @@ bool BluetoothPairing::CancelPairing() {
|
||||
}
|
||||
|
||||
bool BluetoothPairing::Unpair() {
|
||||
try {
|
||||
adapter_.RemoveDevice(device_.getObjectPath());
|
||||
try {
|
||||
adapter_.RemoveDeviceByObjectPath(device_.getObjectPath());
|
||||
return true;
|
||||
} catch (const sdbus::Error &e) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
|
||||
<< "' with message '" << e.getMessage()
|
||||
<< "' while trying to unpair device "
|
||||
<< device_.getObjectPath() << " on adapter "
|
||||
<< adapter_.getObjectPath();
|
||||
<< adapter_.GetObjectPath();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothPairing : public api::BluetoothPairing {
|
||||
public:
|
||||
BluetoothPairing(BluetoothAdapter &adapter,
|
||||
BluetoothDevice &remote_device);
|
||||
BluetoothPairing(BluetoothAdapter &adapter, BluetoothDevice &remote_device);
|
||||
~BluetoothPairing() override = default;
|
||||
|
||||
bool InitiatePairing(api::BluetoothPairingCallback pairing_cb) override;
|
||||
@@ -33,7 +32,7 @@ private:
|
||||
sdbus::PendingAsyncCall pair_async_call_;
|
||||
|
||||
BluetoothDevice &device_;
|
||||
BluetoothAdapter &adapter_;
|
||||
linux::BluetoothAdapter &adapter_;
|
||||
|
||||
api::BluetoothPairingCallback pairing_cb_;
|
||||
};
|
||||
|
||||
@@ -178,7 +178,7 @@ ImplementationPlatform::CreateBluetoothAdapter() {
|
||||
std::unique_ptr<api::BluetoothClassicMedium>
|
||||
ImplementationPlatform::CreateBluetoothClassicMedium(
|
||||
BluetoothAdapter &adapter) {
|
||||
auto path = static_cast<linux::BluetoothAdapter *>(&adapter)->getObjectPath();
|
||||
auto path = static_cast<linux::BluetoothAdapter *>(&adapter)->GetObjectPath();
|
||||
return std::make_unique<linux::BluetoothClassicMedium>(
|
||||
linux::getSystemBusConnection(), path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user