mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
removed observer from bluetooth surface
This commit is contained in:
@@ -35,7 +35,6 @@
|
||||
#include "ble_l2cap_server_socket.h"
|
||||
#include "ble_l2cap_socket.h"
|
||||
#include "ble_gatt_server.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_classic_device.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_devices.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
@@ -72,10 +71,8 @@ BleV2Medium::~BleV2Medium() {
|
||||
BleV2Medium::BleV2Medium(BluetoothAdapter& adapter)
|
||||
: system_bus_(adapter.GetConnection()),
|
||||
adapter_(adapter),
|
||||
observers_(std::make_shared<
|
||||
ObserverList<api::BluetoothClassicMedium::Observer>>()),
|
||||
devices_(std::make_unique<BluetoothDevices>(
|
||||
system_bus_, adapter_.GetObjectPath(), *observers_)),
|
||||
system_bus_, adapter_.GetObjectPath())),
|
||||
gatt_discovery_(std::make_shared<BluezGattDiscovery>(system_bus_)),
|
||||
root_object_manager_(std::make_unique<RootObjectManager>(
|
||||
*system_bus_,
|
||||
|
||||
@@ -145,9 +145,6 @@ class BleV2Medium final : public api::ble::BleMedium {
|
||||
|
||||
std::shared_ptr<sdbus::IConnection> system_bus_;
|
||||
BluetoothAdapter adapter_;
|
||||
// Why do we have observers her
|
||||
std::shared_ptr<ObserverList<api::BluetoothClassicMedium::Observer>>
|
||||
observers_;
|
||||
std::shared_ptr<BluetoothDevices> devices_;
|
||||
std::shared_ptr<BluezGattDiscovery> gatt_discovery_;
|
||||
|
||||
|
||||
@@ -148,14 +148,12 @@ bool BluetoothDevice::ConnectToProfile(absl::string_view service_uuid) {
|
||||
}
|
||||
MonitoredBluetoothDevice::MonitoredBluetoothDevice(
|
||||
std::shared_ptr<sdbus::IConnection> system_bus,
|
||||
std::shared_ptr<bluez::Device> device,
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers)
|
||||
std::shared_ptr<bluez::Device> device)
|
||||
: BluetoothDevice(device),
|
||||
ProxyInterfaces<sdbus::Properties_proxy>(*system_bus,
|
||||
sdbus::ServiceName(bluez::SERVICE_DEST),
|
||||
device->getProxy().getObjectPath()),
|
||||
system_bus_(std::move(system_bus)),
|
||||
observers_(observers) {
|
||||
system_bus_(std::move(system_bus)) {
|
||||
registerProxy();
|
||||
}
|
||||
|
||||
@@ -170,30 +168,9 @@ void MonitoredBluetoothDevice::onPropertiesChanged(
|
||||
for (auto it = changedProperties.begin(); it != changedProperties.end();
|
||||
it++)
|
||||
{
|
||||
if (it->first == bluez::DEVICE_PROP_ADDRESS) {
|
||||
LOG(INFO) << __func__ << ": " << getProxy().getObjectPath()
|
||||
<< ": Notifying observers about address change";
|
||||
std::string address = it->second.get<std::string>();
|
||||
for (const auto &observer : observers_.GetObservers()) {
|
||||
observer->DeviceAddressChanged(*this, address);
|
||||
}
|
||||
|
||||
} else if (it->first == bluez::DEVICE_PROP_PAIRED) {
|
||||
LOG(INFO) << __func__ << ": " << getProxy().getObjectPath()
|
||||
<< "Notifying observers about paired status change.";
|
||||
for (const auto &observer : observers_.GetObservers()) {
|
||||
observer->DevicePairedChanged(*this, it->second.get<bool>());
|
||||
}
|
||||
} else if (it->first == bluez::DEVICE_PROP_CONNECTED) {
|
||||
LOG(INFO)
|
||||
<< __func__ << ": " << getProxy().getObjectPath()
|
||||
<< "Notifying observers about connected status change";
|
||||
for (const auto &observer : observers_.GetObservers()) {
|
||||
observer->DeviceConnectedStateChanged(*this, it->second.get<bool>());
|
||||
}
|
||||
} else if ( it -> first == "ServicesResolved"){
|
||||
if (it->first == "ServicesResolved"){
|
||||
LOG(INFO) << ": ServicesResolved :" << it->second.get<std::string>();
|
||||
}else if (it->first == bluez::DEVICE_NAME) {
|
||||
} else if (it->first == bluez::DEVICE_NAME) {
|
||||
auto callback = GetDiscoveryCallback();
|
||||
if (callback != nullptr && callback->device_name_changed_cb != nullptr)
|
||||
callback->device_name_changed_cb(*this);
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
// #include "internal/platform/implementation/ble.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/linux/bluez_device.h"
|
||||
@@ -150,8 +149,7 @@ class MonitoredBluetoothDevice final
|
||||
MonitoredBluetoothDevice &operator=(MonitoredBluetoothDevice &&) = delete;
|
||||
MonitoredBluetoothDevice(
|
||||
std::shared_ptr<sdbus::IConnection> system_bus,
|
||||
std::shared_ptr<bluez::Device> device,
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers);
|
||||
std::shared_ptr<bluez::Device> device);
|
||||
~MonitoredBluetoothDevice() override { unregisterProxy(); }
|
||||
|
||||
void SetDiscoveryCallback(
|
||||
@@ -178,7 +176,6 @@ class MonitoredBluetoothDevice final
|
||||
return callback;
|
||||
}
|
||||
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers_;
|
||||
absl::Mutex discovery_cb_mutex_;
|
||||
std::weak_ptr<api::BluetoothClassicMedium::DiscoveryCallback> discovery_cb_
|
||||
ABSL_GUARDED_BY(discovery_cb_mutex_);
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
#include <sdbus-c++/Types.h>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluez_agent.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_bluez_profile.h"
|
||||
@@ -43,15 +42,11 @@ constexpr char kBluezAgentPath[] = "/com/google/nearby/bluetooth/agent";
|
||||
BluetoothClassicMedium::BluetoothClassicMedium(BluetoothAdapter &adapter)
|
||||
: system_bus_(adapter.GetConnection()),
|
||||
adapter_(adapter),
|
||||
observers_(nullptr),
|
||||
devices_(nullptr),
|
||||
device_watcher_(nullptr),
|
||||
agent_manager_(std::make_unique<AgentManager>(*system_bus_)),
|
||||
profile_manager_(nullptr) {
|
||||
auto shared =
|
||||
GetSharedBluetoothDevices(system_bus_, adapter_.GetObjectPath());
|
||||
observers_ = shared->observers;
|
||||
devices_ = shared->devices;
|
||||
devices_ = GetSharedBluetoothDevices(system_bus_, adapter_.GetObjectPath());
|
||||
profile_manager_ =
|
||||
std::make_unique<ProfileManager>(*system_bus_, *devices_);
|
||||
|
||||
@@ -67,9 +62,8 @@ BluetoothClassicMedium::BluetoothClassicMedium(BluetoothAdapter &adapter)
|
||||
bool BluetoothClassicMedium::StartDiscovery(
|
||||
DiscoveryCallback discovery_callback) {
|
||||
device_watcher_ = std::make_unique<DeviceWatcher>(
|
||||
*system_bus_, adapter_.GetObjectPath(), adapter_, devices_,
|
||||
std::make_unique<DiscoveryCallback>(std::move(discovery_callback)),
|
||||
observers_);
|
||||
*system_bus_, adapter_.GetObjectPath(), adapter_, devices_,
|
||||
std::make_unique<DiscoveryCallback>(std::move(discovery_callback)));
|
||||
|
||||
std::map<std::string, sdbus::Variant> filter;
|
||||
filter["Transport"] = sdbus::Variant("auto");
|
||||
@@ -159,7 +153,7 @@ std::unique_ptr<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
|
||||
new BluetoothSocket(device, fd.value()));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::BluetoothServerSocket>
|
||||
std::shared_ptr<api::BluetoothServerSocket>
|
||||
BluetoothClassicMedium::ListenForService(const std::string &service_name,
|
||||
const std::string &service_uuid) {
|
||||
if (!profile_manager_->ProfileRegistered(service_uuid)) {
|
||||
@@ -171,9 +165,9 @@ BluetoothClassicMedium::ListenForService(const std::string &service_name,
|
||||
}
|
||||
}
|
||||
|
||||
return std::unique_ptr<api::BluetoothServerSocket>(
|
||||
new BluetoothServerSocket(*profile_manager_, service_uuid));
|
||||
}
|
||||
return std::shared_ptr<api::BluetoothServerSocket>(
|
||||
new BluetoothServerSocket(*profile_manager_, service_uuid));
|
||||
}
|
||||
|
||||
api::BluetoothDevice *BluetoothClassicMedium::GetRemoteDevice(
|
||||
MacAddress mac_address) {
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <sdbus-c++/Types.h>
|
||||
|
||||
#include "internal/platform/implementation/linux/bluez_agent.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_bluez_profile.h"
|
||||
@@ -78,7 +77,7 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
|
||||
// UUID.
|
||||
//
|
||||
// Returns nullptr error.
|
||||
std::unique_ptr<api::BluetoothServerSocket> ListenForService(
|
||||
std::shared_ptr<api::BluetoothServerSocket> ListenForService(
|
||||
const std::string &service_name,
|
||||
const std::string &service_uuid) override;
|
||||
|
||||
@@ -92,18 +91,10 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
|
||||
|
||||
api::BluetoothDevice *GetRemoteDevice(MacAddress mac_address) override;
|
||||
|
||||
void AddObserver(Observer *observer) override {
|
||||
observers_->AddObserver(observer);
|
||||
};
|
||||
void RemoveObserver(Observer *observer) override {
|
||||
observers_->RemoveObserver(observer);
|
||||
};
|
||||
|
||||
private:
|
||||
std::shared_ptr<sdbus::IConnection> system_bus_;
|
||||
|
||||
BluetoothAdapter adapter_;
|
||||
std::shared_ptr<ObserverList<Observer>> observers_;
|
||||
std::shared_ptr<BluetoothDevices> devices_;
|
||||
std::unique_ptr<DeviceWatcher> device_watcher_;
|
||||
|
||||
|
||||
@@ -35,10 +35,10 @@ namespace nearby {
|
||||
namespace linux {
|
||||
static constexpr std::chrono::minutes kLostPeripheralsCleanupMinFreq(5);
|
||||
absl::Mutex g_shared_devices_lock;
|
||||
absl::flat_hash_map<std::string, std::weak_ptr<SharedBluetoothDevices>>
|
||||
absl::flat_hash_map<std::string, std::weak_ptr<BluetoothDevices>>
|
||||
g_shared_devices ABSL_GUARDED_BY(g_shared_devices_lock);
|
||||
|
||||
std::shared_ptr<SharedBluetoothDevices> GetSharedBluetoothDevices(
|
||||
std::shared_ptr<BluetoothDevices> GetSharedBluetoothDevices(
|
||||
std::shared_ptr<sdbus::IConnection> system_bus,
|
||||
const sdbus::ObjectPath& adapter_object_path) {
|
||||
const std::string key = adapter_object_path;
|
||||
@@ -49,13 +49,10 @@ std::shared_ptr<SharedBluetoothDevices> GetSharedBluetoothDevices(
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
auto shared = std::make_shared<SharedBluetoothDevices>();
|
||||
shared->observers =
|
||||
std::make_shared<ObserverList<api::BluetoothClassicMedium::Observer>>();
|
||||
shared->devices = std::make_shared<BluetoothDevices>(
|
||||
std::move(system_bus), adapter_object_path, *shared->observers);
|
||||
g_shared_devices[key] = shared;
|
||||
return shared;
|
||||
auto devices = std::make_shared<BluetoothDevices>(
|
||||
std::move(system_bus), adapter_object_path);
|
||||
g_shared_devices[key] = devices;
|
||||
return devices;
|
||||
}
|
||||
|
||||
std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_path(
|
||||
@@ -136,8 +133,7 @@ std::shared_ptr<MonitoredBluetoothDevice> BluetoothDevices::add_new_device(
|
||||
std::string(device_object_path),
|
||||
std::make_shared<MonitoredBluetoothDevice>(
|
||||
system_bus_,
|
||||
std::make_shared<bluez::Device>(system_bus_, device_object_path),
|
||||
observers_));
|
||||
std::make_shared<bluez::Device>(system_bus_, device_object_path)));
|
||||
if (!inserted) device_it->second->UnmarkLost();
|
||||
return device_it->second;
|
||||
}
|
||||
@@ -163,11 +159,6 @@ void DeviceWatcher::onInterfacesAdded(
|
||||
discovery_cb_->device_discovered_cb(*device);
|
||||
}
|
||||
|
||||
if (observers_ != nullptr) {
|
||||
for (const auto &observer : observers_->GetObservers()) {
|
||||
observer->DeviceAdded(*device);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceWatcher::onInterfacesRemoved(
|
||||
@@ -196,10 +187,7 @@ void DeviceWatcher::onInterfacesRemoved(
|
||||
discovery_cb_->device_lost_cb(*device);
|
||||
}
|
||||
|
||||
if (observers_ != nullptr) {
|
||||
for (const auto &observer : observers_->GetObservers()) {
|
||||
observer->DeviceRemoved(*device);
|
||||
}
|
||||
if (discovery_cb_ != nullptr) {
|
||||
devices_->remove_device_by_path(objectPath);
|
||||
} else {
|
||||
devices_->mark_peripheral_lost(objectPath);
|
||||
|
||||
@@ -28,8 +28,8 @@
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/base/observer_list.h"
|
||||
#include "internal/platform/bluetooth_utils.h"
|
||||
#include "internal/platform/implementation/ble.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_classic_device.h"
|
||||
|
||||
@@ -41,10 +41,8 @@ class BluetoothDevices final {
|
||||
public:
|
||||
BluetoothDevices(
|
||||
std::shared_ptr<sdbus::IConnection> system_bus,
|
||||
sdbus::ObjectPath adapter_object_path,
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers)
|
||||
sdbus::ObjectPath adapter_object_path)
|
||||
: system_bus_(std::move(system_bus)),
|
||||
observers_(observers),
|
||||
adapter_object_path_(std::move(adapter_object_path)) {}
|
||||
|
||||
std::shared_ptr<BluetoothDevice> get_device_by_path(const sdbus::ObjectPath &)
|
||||
@@ -74,7 +72,6 @@ class BluetoothDevices final {
|
||||
|
||||
private:
|
||||
std::shared_ptr<sdbus::IConnection> system_bus_;
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers_;
|
||||
sdbus::ObjectPath adapter_object_path_;
|
||||
|
||||
absl::Mutex devices_by_path_lock_;
|
||||
@@ -84,12 +81,7 @@ class BluetoothDevices final {
|
||||
ABSL_GUARDED_BY(devices_by_path_lock_);
|
||||
};
|
||||
|
||||
struct SharedBluetoothDevices {
|
||||
std::shared_ptr<BluetoothDevices> devices;
|
||||
std::shared_ptr<ObserverList<api::BluetoothClassicMedium::Observer>> observers;
|
||||
};
|
||||
|
||||
std::shared_ptr<SharedBluetoothDevices> GetSharedBluetoothDevices(
|
||||
std::shared_ptr<BluetoothDevices> GetSharedBluetoothDevices(
|
||||
std::shared_ptr<sdbus::IConnection> system_bus,
|
||||
const sdbus::ObjectPath& adapter_object_path);
|
||||
|
||||
@@ -106,16 +98,13 @@ class DeviceWatcher final : sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||
BluetoothAdapter &adapter,
|
||||
std::shared_ptr<BluetoothDevices> devices,
|
||||
std::unique_ptr<api::BluetoothClassicMedium::DiscoveryCallback>
|
||||
discovery_callback,
|
||||
std::shared_ptr<ObserverList<api::BluetoothClassicMedium::Observer>>
|
||||
observers)
|
||||
discovery_callback)
|
||||
: ProxyInterfaces(system_bus, sdbus::ServiceName("org.bluez"),
|
||||
sdbus::ObjectPath("/")),
|
||||
adapter_object_path_(adapter_object_path),
|
||||
adapter_(adapter),
|
||||
devices_(std::move(devices)),
|
||||
discovery_cb_(std::move(discovery_callback)),
|
||||
observers_(std::move(observers)) {
|
||||
discovery_cb_(std::move(discovery_callback)) {
|
||||
notifyExistingDevices();
|
||||
registerProxy();
|
||||
}
|
||||
@@ -124,7 +113,7 @@ class DeviceWatcher final : sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||
BluetoothAdapter &adapter,
|
||||
std::shared_ptr<BluetoothDevices> devices)
|
||||
: DeviceWatcher(system_bus, adapter_object_path, adapter, std::move(devices),
|
||||
nullptr, nullptr) {}
|
||||
nullptr) {}
|
||||
~DeviceWatcher() { unregisterProxy(); }
|
||||
|
||||
void onInterfacesAdded(
|
||||
@@ -143,8 +132,6 @@ class DeviceWatcher final : sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||
BluetoothAdapter &adapter_;
|
||||
std::shared_ptr<BluetoothDevices> devices_;
|
||||
std::shared_ptr<api::BluetoothClassicMedium::DiscoveryCallback> discovery_cb_;
|
||||
std::shared_ptr<ObserverList<api::BluetoothClassicMedium::Observer>>
|
||||
observers_;
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
|
||||
Reference in New Issue
Block a user