From c01ac9717aaed795b4cbb0c48cce61a012dfe7d8 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Tue, 26 Sep 2023 18:34:56 +0530 Subject: [PATCH] Enabled BleV2 medium by default. BLE v2 advertisements are now correctly broadcasted --- .../flags/nearby_connections_feature_flags.h | 2 +- internal/platform/implementation/linux/BUILD | 7 +- .../platform/implementation/linux/ble_v2.cc | 156 +++++++++++++++++ .../platform/implementation/linux/ble_v2.h | 165 +++++++++++------- .../linux/ble_v2_advertisement.cc | 38 ++++ .../linux/ble_v2_advertisement.h | 86 +++++++++ .../platform/implementation/linux/bluez.h | 2 +- .../implementation/linux/bluez_profiles.cc | 16 ++ .../implementation/linux/bluez_profiles.h | 53 ++++++ .../platform/implementation/linux/platform.cc | 22 +-- 10 files changed, 465 insertions(+), 82 deletions(-) create mode 100644 internal/platform/implementation/linux/ble_v2.cc create mode 100644 internal/platform/implementation/linux/ble_v2_advertisement.cc create mode 100644 internal/platform/implementation/linux/ble_v2_advertisement.h create mode 100644 internal/platform/implementation/linux/bluez_profiles.cc create mode 100644 internal/platform/implementation/linux/bluez_profiles.h diff --git a/connections/implementation/flags/nearby_connections_feature_flags.h b/connections/implementation/flags/nearby_connections_feature_flags.h index 92db9a59..f021f50a 100644 --- a/connections/implementation/flags/nearby_connections_feature_flags.h +++ b/connections/implementation/flags/nearby_connections_feature_flags.h @@ -39,7 +39,7 @@ constexpr auto kEnableAutoReconnect = flags::Flag(kConfigPackage, "45427690", false); // Disable/Enable BLE v2 in Nearby Connections SDK. constexpr auto kEnableBleV2 = - flags::Flag(kConfigPackage, "45401515", false); + flags::Flag(kConfigPackage, "45401515", true); // Disable/Enable GATT query in thread in BLE V2. // Manual edit: setting this to false for ChromeOS rollout as well. constexpr auto kEnableGattQueryInThread = diff --git a/internal/platform/implementation/linux/BUILD b/internal/platform/implementation/linux/BUILD index bbb67a2f..0582d867 100644 --- a/internal/platform/implementation/linux/BUILD +++ b/internal/platform/implementation/linux/BUILD @@ -57,7 +57,7 @@ cc_library( hdrs = [ "avahi.h", # "ble_medium.h", - # "ble_v2_medium.h", + "ble_v2.h", "bluetooth_adapter.h", # "bluetooth_bluez_profile.h", # "bluetooth_classic_device.h", @@ -66,6 +66,8 @@ cc_library( # "bluetooth_classic_socket.h", # "bluetooth_devices.h", # "bluetooth_pairing.h", + "ble_v2_advertisement.h", + "bluez_profiles.h", "bluez.h", "dbus.h", "network_manager.h", @@ -135,6 +137,9 @@ cc_library( # "bluetooth_classic_socket.cc", # "bluetooth_devices.cc", # "bluetooth_pairing.cc", + "bluez_profiles.cc", + "ble_v2_advertisement.cc", + "ble_v2.cc", "bluez.cc", "dbus.cc", "executor.cc", diff --git a/internal/platform/implementation/linux/ble_v2.cc b/internal/platform/implementation/linux/ble_v2.cc new file mode 100644 index 00000000..aef3d356 --- /dev/null +++ b/internal/platform/implementation/linux/ble_v2.cc @@ -0,0 +1,156 @@ +#include +#include "internal/platform/implementation/linux/ble_v2.h" + +#include "internal/platform/implementation/linux/ble_v2_advertisement.h" +#include "internal/platform/implementation/linux/bluetooth_adapter.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace linux { +BleV2Medium::BleV2Medium(api::BluetoothAdapter &adapter) : adapter_(dynamic_cast(&adapter)) +{ + auto system_bus = linux::getSystemBusConnection(); + bluez_object_manager_ = std::make_unique(*system_bus); +} +bool BleV2Medium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters) +{ + if (!adapter_->IsEnabled()) + { + NEARBY_LOGS(WARNING) << "BLE cannot start advertising because the " + "bluetooth adapter is not enabled."; + return false; + } + + if (advertising_data.service_data.empty()) + { + NEARBY_LOGS(WARNING) << "BLE cannot start to advertise due to invalid service data."; + return false; + } + + auto system_bus = adapter_->GetConnection(); + auto le_advertisement = linux::LEAdvertisement::CreateLEAdvertisement( + *system_bus, advertising_data, advertise_set_parameters); + + if (!le_advertisement) + { + NEARBY_LOGS(WARNING) << "Some error occurred while creating LEAdvertisement object."; + return false; + } + auto le_advertisement_manager = + std::make_unique(*system_bus, *adapter_); + try + { + le_advertisement_manager->RegisterAdvertisement(le_advertisement->getObjectPath(), {}); + } + catch (const sdbus::Error& e) + { + DBUS_LOG_METHOD_CALL_ERROR(le_advertisement_manager, "RegisterAdvertisement", e); + return false; + } + return true; +} + +// dummy overrides +std::unique_ptr +BleV2Medium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters, AdvertisingCallback callback) +{ + return nullptr; +} +bool BleV2Medium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + ScanCallback callback) +{ + return false; +} +bool BleV2Medium::StopAdvertising() { return false; } +bool BleV2Medium::IsExtendedAdvertisementsAvailable() { return true; } +std::unique_ptr +BleV2Medium::StartGattServer(api::ble_v2::ServerGattConnectionCallback callback) +{ + return nullptr; +} +bool BleV2Medium::StopScanning() { return false; } +std::unique_ptr +BleV2Medium::ConnectToGattServer(api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level, + api::ble_v2::ClientGattConnectionCallback callback) +{ + return nullptr; +} +std::unique_ptr BleV2Medium::OpenServerSocket(const std::string &service_id) +{ + + LOG(INFO) << "OpenServerSocket is called"; + + auto server_socket = std::make_unique(*adapter_); + + + return server_socket; +} +bool BleV2Medium::GetRemotePeripheral(const std::string &mac_address, GetRemotePeripheralCallback callback) +{ + return false; +} +bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) +{ + return false; +} +std::unique_ptr BleV2Medium::Connect(const std::string &service_id, + api::ble_v2::TxPowerLevel tx_power_level, + api::ble_v2::BlePeripheral &peripheral, + CancellationFlag *cancellation_flag) +{ + return nullptr; +} +std::unique_ptr +BleV2Medium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + ScanningCallback callback) +{ + return nullptr; +} +// end of dummy overrides + +BleV2ServerSocket::BleV2ServerSocket(api::BluetoothAdapter &adapter) : + adapter_(dynamic_cast(&adapter)) +{ +} + +std::unique_ptr BleV2ServerSocket::Accept() +{ + absl::MutexLock lock(&mutex_); + LOG(INFO) << __func__ << ": Accept is called."; + + while (!closed_ && pending_sockets_.empty()) + { + cond_.Wait(&mutex_); + } + if (closed_) + return nullptr; + + linux::BleV2Socket ble_socket = pending_sockets_.front(); + pending_sockets_.pop_front(); + + LOG(INFO) << __func__ << ": Accepted a remote connection."; + return std::make_unique(ble_socket); +} + +Exception BleV2ServerSocket::Close() +{ + // TODO(b/271031645): implement BLE socket using weave + absl::MutexLock lock(&mutex_); + LOG(INFO) << __func__ << ": Close is called."; + + if (closed_) + { + return {Exception::kSuccess}; + } + + closed_ = true; + cond_.SignalAll(); + + return {Exception::kSuccess}; +} + + +} // namespace linux +} // namespace nearby diff --git a/internal/platform/implementation/linux/ble_v2.h b/internal/platform/implementation/linux/ble_v2.h index 292d06e0..8f3d4af8 100644 --- a/internal/platform/implementation/linux/ble_v2.h +++ b/internal/platform/implementation/linux/ble_v2.h @@ -1,4 +1,6 @@ +#include "bluetooth_adapter.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/linux/stream.h" namespace nearby { namespace linux { @@ -6,110 +8,141 @@ class BleV2Peripheral : public api::ble_v2::BlePeripheral { std::string GetAddress() const override; UniqueId GetUniqueId() const override; - }; -class GattClient: public api::ble_v2::GattClient +class BleV2Socket : public api::ble_v2::BleSocket { + public: + BleV2Socket() = delete; + explicit BleV2Socket(sdbus::UnixFd fd): input_stream_(fd), output_stream_(fd) {}; + ~BleV2Socket() override = default; + + nearby::InputStream& GetInputStream() override { return input_stream_;}; + + nearby::OutputStream& GetOutputStream() override {return output_stream_;}; + + Exception Close() override + { + input_stream_.Close(); + output_stream_.Close(); + return Exception{Exception::kSuccess}; + }; + + api::ble_v2::BlePeripheral* GetRemotePeripheral() override + { + return ble_peripheral_; + }; + + + private: + linux::InputStream input_stream_; + linux::OutputStream output_stream_; + api::ble_v2::BlePeripheral* ble_peripheral_ = nullptr; +}; + +class GattClient : public api::ble_v2::GattClient { - bool DiscoverServiceAndCharacteristics(const Uuid& service_uuid, const std::vector& characteristic_uuids) override; + bool DiscoverServiceAndCharacteristics(const Uuid &service_uuid, + const std::vector &characteristic_uuids) override; - absl::optional - GetCharacteristic(const Uuid& service_uuid, const Uuid& characteristic_uuid) override; + absl::optional GetCharacteristic(const Uuid &service_uuid, + const Uuid &characteristic_uuid) override; - absl::optional - ReadCharacteristic(const api::ble_v2::GattCharacteristic& characteristic) override; + absl::optional ReadCharacteristic(const api::ble_v2::GattCharacteristic &characteristic) override; - bool WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - absl::string_view value, WriteType type) override; + bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, absl::string_view value, + WriteType type) override; bool SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, - bool enable, absl::AnyInvocable on_characteristic_changed_cb) - override; + const api::ble_v2::GattCharacteristic &characteristic, bool enable, + absl::AnyInvocable on_characteristic_changed_cb) override; void Disconnect() override; }; class GattServer : public api::ble_v2::GattServer { - api::ble_v2::BlePeripheral& GetBlePeripheral() override; + api::ble_v2::BlePeripheral &GetBlePeripheral() override; - absl::optional CreateCharacteristic( - const Uuid& service_uuid, const Uuid& characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) - override; + absl::optional + CreateCharacteristic(const Uuid &service_uuid, const Uuid &characteristic_uuid, + api::ble_v2::GattCharacteristic::Permission permission, + api::ble_v2::GattCharacteristic::Property property) override; - bool UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, const nearby::ByteArray& value) - override; + bool UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, + const nearby::ByteArray &value) override; - absl::Status NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, - const ByteArray& new_value) - override; + absl::Status NotifyCharacteristicChanged(const api::ble_v2::GattCharacteristic &characteristic, bool confirm, + const ByteArray &new_value) override; void Stop() override; }; -class BleV2Socket: public api::ble_v2::BleSocket -{ - InputStream& GetInputStream() override; - OutputStream& GetOutputStream() override; - Exception Close() override; -}; - -class BleV2ServerSocket: public api::ble_v2::BleServerSocket +class BleV2ServerSocket : public api::ble_v2::BleServerSocket { +public: + explicit BleV2ServerSocket(api::BluetoothAdapter& adapter); + ~BleV2ServerSocket() override = default; std::unique_ptr Accept() override; Exception Close() override; +private: + // Accept notification. + absl::Mutex mutex_; + absl::CondVar cond_; + bool closed_ = false; + linux::BluetoothAdapter *adapter_; + std::deque pending_sockets_ ABSL_GUARDED_BY(mutex_); }; -class BleV2Medium: public api::ble_v2::BleMedium + +class BleV2Medium : public api::ble_v2::BleMedium { - std::unique_ptr - StartAdvertising(const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - AdvertisingCallback callback) override; +public: + BleV2Medium(const BleV2Medium &) = delete; + BleV2Medium(BleV2Medium &&) = delete; + BleV2Medium &operator=(const BleV2Medium &) = delete; + BleV2Medium &operator=(BleV2Medium &&) = delete; + + explicit BleV2Medium(api::BluetoothAdapter &adapter); + ~BleV2Medium() override = default; + + bool StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters) override; + + std::unique_ptr StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters, + AdvertisingCallback callback) override; + bool StopAdvertising() override; - std::unique_ptr - StartScanning(const Uuid& service_uuid, - api::ble_v2::TxPowerLevel tx_power_level, - ScanningCallback callback) - override; + std::unique_ptr StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + ScanningCallback callback) override; + + bool StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + ScanCallback callback) override; bool StopScanning() override; - std::unique_ptr - StartGattServer(api::ble_v2::ServerGattConnectionCallback callback) - override; + std::unique_ptr StartGattServer(api::ble_v2::ServerGattConnectionCallback callback) override; std::unique_ptr - ConnectToGattServer(api::ble_v2::BlePeripheral& peripheral, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) - override; + ConnectToGattServer(api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level, + api::ble_v2::ClientGattConnectionCallback callback) override; - std::unique_ptr - OpenServerSocket(const std::string& service_id) - override; + std::unique_ptr OpenServerSocket(const std::string &service_id) override; - std::unique_ptr - Connect(const std::string& service_id, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral& peripheral, - CancellationFlag* cancellation_flag) - override; + std::unique_ptr Connect(const std::string &service_id, + api::ble_v2::TxPowerLevel tx_power_level, + api::ble_v2::BlePeripheral &peripheral, + CancellationFlag *cancellation_flag) override; bool IsExtendedAdvertisementsAvailable() override; - bool GetRemotePeripheral(const std::string &mac_address, - GetRemotePeripheralCallback callback) override; - bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, - GetRemotePeripheralCallback callback) override; + bool GetRemotePeripheral(const std::string &mac_address, GetRemotePeripheralCallback callback) override; + bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) override; +private: + linux::BluetoothAdapter *adapter_; + std::unique_ptr bluez_object_manager_; }; -} -} +} // namespace linux +} // namespace nearby diff --git a/internal/platform/implementation/linux/ble_v2_advertisement.cc b/internal/platform/implementation/linux/ble_v2_advertisement.cc new file mode 100644 index 00000000..98913a6d --- /dev/null +++ b/internal/platform/implementation/linux/ble_v2_advertisement.cc @@ -0,0 +1,38 @@ +#include + +#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/linux/bluez.h" +#include "internal/platform/implementation/linux/ble_v2_advertisement.h" +#include "internal/platform/logging.h" +#include "internal/platform/uuid.h" + +namespace nearby { +namespace linux { +LEAdvertisement::LEAdvertisement( + sdbus::IConnection& system_bus, sdbus::ObjectPath path, + const api::ble_v2::BleAdvertisementData& advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters) + : AdaptorInterfaces(system_bus, std::move(path)), + is_extended_advertisement_(advertising_data.is_extended_advertisement), + advertise_set_parameters_(advertise_set_parameters) { + for (const auto& [uuid, data] : advertising_data.service_data) { + std::string uuid_string(uuid); + std::vector data_bytes(data.size()); + const auto* bytes = data.data(); + + service_uuids_.push_back(uuid_string); + for (size_t i = 0; i < data.size(); i++) { + data_bytes[i] = bytes[i]; + } + + service_data_.insert({uuid_string, std::move(data_bytes)}); + } + + registerAdaptor(); + + NEARBY_VLOG(1) << __func__ + << ": Created a org.bluez.LEAdvertisement1 instance at " + << getObjectPath(); +} +} // namespace linux +} // namespace nearby diff --git a/internal/platform/implementation/linux/ble_v2_advertisement.h b/internal/platform/implementation/linux/ble_v2_advertisement.h new file mode 100644 index 00000000..2ca11926 --- /dev/null +++ b/internal/platform/implementation/linux/ble_v2_advertisement.h @@ -0,0 +1,86 @@ +#ifndef PLATFORM_IMPL_LINUX_API_BLUEZ_BLE_ADVERTISEMENT_H_ +#define PLATFORM_IMPL_LINUX_API_BLUEZ_BLE_ADVERTISEMENT_H_ + +#include +#include +#include +#include +#include + +#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/linux/bluetooth_adapter.h" +#include "internal/platform/implementation/linux/bluez.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/le_advertisement_manager_client.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/le_advertisement_server.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace linux { +class LEAdvertisement final + : public sdbus::AdaptorInterfaces +{ +public: + LEAdvertisement(const LEAdvertisement &) = delete; + LEAdvertisement(LEAdvertisement &&) = delete; + LEAdvertisement &operator=(const LEAdvertisement &) = delete; + LEAdvertisement &operator=(LEAdvertisement &&) = delete; + + LEAdvertisement(sdbus::IConnection &system_bus, sdbus::ObjectPath path, + const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertise_set_parameters); + + static std::unique_ptr + CreateLEAdvertisement(sdbus::IConnection &system_bus, const api::ble_v2::BleAdvertisementData &advertising_data, + api::ble_v2::AdvertiseParameters advertising_parameters) + { + static std::atomic adv_count = 0; + auto object_path = bluez::ble_advertisement_path(adv_count++); + return std::make_unique(system_bus, object_path, advertising_data, advertising_parameters); + } + ~LEAdvertisement() { unregisterAdaptor(); } + +private: + // Methods + void Release() override { NEARBY_LOGS(INFO) << __func__ << ": LE Advertisement released: " << getObjectPath(); } + + // Properties + std::string Type() override { return "peripheral"; } + std::vector ServiceUUIDs() override { return service_uuids_; } + std::map ManufacturerData() override { return {}; } + std::vector SolicitUUIDs() override { return {}; } + std::map ServiceData() override { return service_data_; } + std::vector Includes() override { return {}; } + std::string LocalName() override { return {}; } + uint16_t Duration() override { return 0; } + uint16_t Timeout() override { return 0; } + // Windows seems to hardcode the scan interval to 118.125 milliseconds, so + // lets just replicate that. + uint32_t MinInterval() override { return 118; } + uint32_t MaxInterval() override { return 119; } + int16_t TxPower() override { return bluez::TxPowerLevelDbm(advertise_set_parameters_.tx_power_level); }; + + bool is_extended_advertisement_; + std::vector service_uuids_; + std::map service_data_; + api::ble_v2::AdvertiseParameters advertise_set_parameters_; +}; + +class LEAdvertisementManager final : public sdbus::ProxyInterfaces +{ +public: + LEAdvertisementManager(sdbus::IConnection &system_bus, BluetoothAdapter &adapter) : + ProxyInterfaces(system_bus, "org.bluez", adapter.GetObjectPath()) + { + registerProxy(); + } + ~LEAdvertisementManager() { unregisterProxy(); } + + LEAdvertisementManager(const LEAdvertisementManager &) = delete; + LEAdvertisementManager(LEAdvertisementManager &&) = delete; + LEAdvertisementManager &operator=(const LEAdvertisementManager &) = delete; + LEAdvertisementManager &operator=(LEAdvertisementManager &&) = delete; +}; +} // namespace linux +} // namespace nearby + +#endif diff --git a/internal/platform/implementation/linux/bluez.h b/internal/platform/implementation/linux/bluez.h index 2d595a67..d5c79526 100644 --- a/internal/platform/implementation/linux/bluez.h +++ b/internal/platform/implementation/linux/bluez.h @@ -63,7 +63,7 @@ int16_t TxPowerLevelDbm(api::ble_v2::TxPowerLevel level); class BluezObjectManager : public sdbus::ProxyInterfaces { public: - explicit BluezObjectManager(sdbus::IConnection &system_bus) + explicit BluezObjectManager(sdbus::IConnection& system_bus) : ProxyInterfaces(system_bus, "org.bluez", "/") { registerProxy(); } diff --git a/internal/platform/implementation/linux/bluez_profiles.cc b/internal/platform/implementation/linux/bluez_profiles.cc new file mode 100644 index 00000000..bbe633ab --- /dev/null +++ b/internal/platform/implementation/linux/bluez_profiles.cc @@ -0,0 +1,16 @@ +#include "internal/platform/implementation/linux/bluez_profiles.h" + +#include "internal/platform/logging.h" + +namespace nearby{ +namespace linux{ +namespace bluez{ +void BluezProfile::NewConnection(const sdbus::ObjectPath& device, const sdbus::UnixFd& fd, const std::map& fd_properties) +{ + NEARBY_VLOG(1) << __func__ << ": New connection to device " << device; +} + + +} +} +} diff --git a/internal/platform/implementation/linux/bluez_profiles.h b/internal/platform/implementation/linux/bluez_profiles.h new file mode 100644 index 00000000..c34998cf --- /dev/null +++ b/internal/platform/implementation/linux/bluez_profiles.h @@ -0,0 +1,53 @@ +#include + +#include "internal/platform/implementation/linux/bluez.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_manager_client.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h" + +namespace nearby { +class CancellationFlag; +namespace linux { +namespace bluez { +class BluezProfile : public sdbus::AdaptorInterfaces +{ + BluezProfile(const BluezProfile &) = delete; + BluezProfile(BluezProfile &&) = delete; + BluezProfile &operator=(const BluezProfile &) = delete; + BluezProfile &operator=(BluezProfile &&) = delete; + BluezProfile(sdbus::IConnection &system_bus, sdbus::ObjectPath profile_object_path) : + AdaptorInterfaces(system_bus, std::move(profile_object_path)) + { + registerAdaptor(); + } + ~BluezProfile() { unregisterAdaptor(); } + + void NewConnection(const sdbus::ObjectPath &device, const sdbus::UnixFd &fd, + const std::map &fd_properties) override; + +}; +class BluezProfileManager : public sdbus::ProxyInterfaces +{ + + BluezProfileManager(const BluezProfileManager &) = delete; + BluezProfileManager(BluezProfileManager &&) = delete; + BluezProfileManager &operator=(const BluezProfileManager &) = delete; + BluezProfileManager &operator=(BluezProfileManager &&) = delete; + BluezProfileManager(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, bluez::SERVICE_DEST, "/org/bluez") + { + registerProxy(); + } + ~BluezProfileManager() { unregisterProxy(); } + +public: + bool ProfileRegistered(absl::string_view service_uuid); + bool Register(std::optional service_name, absl::string_view service_uuid); + bool Register(absl::string_view service_uuid) { return Register(std::nullopt, service_uuid); } + void Unregister(absl::string_view service_uuid); + + +private: + std::map> registered_services_; +}; +} // namespace bluez +} // namespace linux +} // namespace nearby diff --git a/internal/platform/implementation/linux/platform.cc b/internal/platform/implementation/linux/platform.cc index 559bcd6e..ea80bec8 100644 --- a/internal/platform/implementation/linux/platform.cc +++ b/internal/platform/implementation/linux/platform.cc @@ -31,12 +31,12 @@ #include "internal/platform/implementation/linux/atomic_uint32.h" // #include "internal/platform/implementation/linux/ble_medium.h" // #include "internal/platform/implementation/linux/ble_v2_medium.h" -// #include "internal/platform/implementation/linux/bluetooth_adapter.h" +#include "internal/platform/implementation/linux/bluetooth_adapter.h" // #include "internal/platform/implementation/linux/bluetooth_classic_medium.h" -// #include "internal/platform/implementation/linux/bluez.h" +#include "internal/platform/implementation/linux/bluez.h" #include "internal/platform/implementation/linux/condition_variable.h" #include "internal/platform/implementation/linux/dbus.h" -// #include "internal/platform/implementation/linux/generated/dbus/bluez/adapter_client.h" +#include "internal/platform/implementation/linux/generated/dbus/bluez/adapter_client.h" #include "internal/platform/implementation/linux/mutex.h" #include "internal/platform/implementation/linux/preferences_manager.h" #include "internal/platform/implementation/linux/submittable_executor.h" @@ -50,10 +50,7 @@ #include "internal/platform/implementation/shared/file.h" #include "internal/platform/implementation/submittable_executor.h" // #include "internal/platform/implementation/wifi_hotspot.h" -#include "ble_medium.h" -#include "ble_v2_medium.h" -#include "bluetooth_adapter.h" -#include "bluez.h" +#include "internal/platform/implementation/linux/ble_v2.h" #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/payload_id.h" #include "log_message.h" @@ -171,15 +168,14 @@ ImplementationPlatform::CreateScheduledExecutor() { std::unique_ptr ImplementationPlatform::CreateBluetoothAdapter() { - auto manager = - linux::bluez::BluezObjectManager(*linux::getSystemBusConnection()); + auto system_bus = linux::getSystemBusConnection(); + auto manager = linux::bluez::BluezObjectManager(*system_bus); try { auto interfaces = manager.GetManagedObjects(); for (auto &[object, properties] : interfaces) { if (properties.count(org::bluez::Adapter1_proxy::INTERFACE_NAME) == 1) { NEARBY_LOGS(INFO) << __func__ << ": found bluetooth adapter " << object; - return std::make_unique( - linux::getSystemBusConnection(), object); + return std::make_unique(system_bus, object); } } } catch (const sdbus::Error &e) { @@ -189,7 +185,7 @@ ImplementationPlatform::CreateBluetoothAdapter() { NEARBY_LOGS(ERROR) << __func__ << ": couldn't find a bluetooth adapter on this system"; - return nullptr; //TODO: implement bluetooth adapter + return nullptr; } std::unique_ptr @@ -209,7 +205,7 @@ std::unique_ptr ImplementationPlatform::CreateBleMedium( // std::unique_ptr ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter &adapter) { - return std::make_unique(adapter); + return std::make_unique(adapter); } namespace {