Add support for sharing DiscoveryCallbacks across BluetoothDevices.

This commit is contained in:
Vibhav Pant
2023-09-05 20:00:25 +05:30
parent ad7401616b
commit 4042687b8d
7 changed files with 20 additions and 7 deletions
@@ -163,6 +163,10 @@ void MonitoredBluetoothDevice::onPropertiesChanged(
for (auto &observer : observers_.GetObservers()) {
observer->DeviceConnectedStateChanged(*this, it->second);
}
} else if (it->first == bluez::DEVICE_NAME) {
auto callback = discovery_cb_.lock();
if (callback != nullptr && callback->device_name_changed_cb != nullptr)
callback->device_name_changed_cb(*this);
}
}
}
@@ -96,6 +96,12 @@ class MonitoredBluetoothDevice final
ObserverList<api::BluetoothClassicMedium::Observer> &observers);
~MonitoredBluetoothDevice() override { unregisterProxy(); }
void SetDiscoveryCallback(
std::shared_ptr<api::BluetoothClassicMedium::DiscoveryCallback>
&callback) {
discovery_cb_ = callback;
};
protected:
void onPropertiesChanged(
const std::string &interfaceName,
@@ -104,6 +110,7 @@ class MonitoredBluetoothDevice final
private:
ObserverList<api::BluetoothClassicMedium::Observer> &observers_;
std::weak_ptr<api::BluetoothClassicMedium::DiscoveryCallback> discovery_cb_;
};
} // namespace linux
@@ -64,9 +64,9 @@ void BluetoothClassicMedium::onInterfacesAdded(
NEARBY_LOGS(INFO) << __func__ << ": Encountered new device at " << object;
auto device = devices_->add_new_device(object);
if (discovery_cb_.has_value() &&
if (discovery_cb_ != nullptr &&
discovery_cb_->device_discovered_cb != nullptr) {
device->SetDiscoveryCallback(discovery_cb_);
discovery_cb_->device_discovered_cb(*device);
}
@@ -98,7 +98,7 @@ void BluetoothClassicMedium::onInterfacesRemoved(
NEARBY_LOGS(INFO) << __func__ << ": Device " << object
<< " has been removed";
if (discovery_cb_.has_value() &&
if (discovery_cb_ != nullptr &&
discovery_cb_->device_lost_cb != nullptr) {
discovery_cb_->device_lost_cb(*device);
}
@@ -114,7 +114,8 @@ void BluetoothClassicMedium::onInterfacesRemoved(
bool BluetoothClassicMedium::StartDiscovery(
DiscoveryCallback discovery_callback) {
discovery_cb_ = std::move(discovery_callback);
discovery_cb_ =
std::make_shared<DiscoveryCallback>(std::move(discovery_callback));
try {
NEARBY_LOGS(INFO) << __func__ << ": Starting discovery on "
@@ -122,7 +122,7 @@ class BluetoothClassicMedium final
BluetoothAdapter adapter_;
std::unique_ptr<BluetoothDevices> devices_;
std::optional<BluetoothClassicMedium::DiscoveryCallback> discovery_cb_;
std::shared_ptr<BluetoothClassicMedium::DiscoveryCallback> discovery_cb_;
std::unique_ptr<ProfileManager> profile_manager_;
ObserverList<Observer> observers_;
@@ -49,7 +49,7 @@ void BluetoothDevices::remove_device_by_path(
devices_by_path_.erase(device_object_path);
}
std::shared_ptr<BluetoothDevice> BluetoothDevices::add_new_device(
std::shared_ptr<MonitoredBluetoothDevice> BluetoothDevices::add_new_device(
sdbus::ObjectPath device_object_path) {
absl::MutexLock l(&devices_by_path_lock_);
auto pair = devices_by_path_.emplace(
@@ -43,7 +43,7 @@ class BluetoothDevices final {
std::shared_ptr<BluetoothDevice> get_device_by_address(const std::string &);
void remove_device_by_path(const sdbus::ObjectPath &)
ABSL_LOCKS_EXCLUDED(devices_by_path_lock_);
std::shared_ptr<BluetoothDevice> add_new_device(sdbus::ObjectPath)
std::shared_ptr<MonitoredBluetoothDevice> add_new_device(sdbus::ObjectPath)
ABSL_LOCKS_EXCLUDED(devices_by_path_lock_);
private:
@@ -43,6 +43,7 @@ static constexpr const char *DEVICE_PROP_ADDRESS = "Address";
static constexpr const char *DEVICE_PROP_ALIAS = "Alias";
static constexpr const char *DEVICE_PROP_PAIRED = "Paired";
static constexpr const char *DEVICE_PROP_CONNECTED = "Connected";
static constexpr const char *DEVICE_NAME = "Name";
std::string device_object_path(const sdbus::ObjectPath &adapter_object_path,
absl::string_view mac_address);