diff --git a/internal/platform/BUILD b/internal/platform/BUILD index d52862de..3b866f20 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -204,10 +204,12 @@ cc_library( "//internal/base", "//internal/platform/implementation:comm", "//internal/test", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/status", "@com_google_absl//absl/strings", + "@com_google_absl//absl/time", "@com_google_absl//absl/types:optional", ], ) diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index dce7d554..09e663ab 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -180,7 +180,7 @@ bool BleV2Medium::StartAdvertising( << TxPowerLevelToName(advertise_parameters.tx_power_level) << ", is_connectable=" << advertise_parameters.is_connectable; if (advertising_data.is_extended_advertisement && - !is_support_extended_advertisement_) { + !IsExtendedAdvertisementsAvailable()) { NEARBY_LOGS(INFO) << "G3 Ble StartAdvertising does not support extended advertisement"; return false; @@ -215,7 +215,7 @@ std::unique_ptr BleV2Medium::StartAdvertising( << TxPowerLevelToName(advertise_parameters.tx_power_level) << ", is_connectable=" << advertise_parameters.is_connectable; if (advertising_data.is_extended_advertisement && - !is_support_extended_advertisement_) { + !IsExtendedAdvertisementsAvailable()) { NEARBY_LOGS(INFO) << "G3 Ble StartAdvertising does not support extended advertisement"; return nullptr; @@ -326,7 +326,7 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( } bool BleV2Medium::IsExtendedAdvertisementsAvailable() { - return is_support_extended_advertisement_; + return MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable(); } bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 2165c014..e51178a8 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -23,15 +23,24 @@ #include #include +#include "absl/base/thread_annotations.h" +#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" +#include "absl/functional/any_invocable.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "internal/platform/borrowable.h" #include "internal/platform/byte_array.h" +#include "internal/platform/cancellation_flag.h" +#include "internal/platform/exception.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/implementation/g3/socket_base.h" -#include "internal/platform/medium_environment.h" -#include "internal/platform/prng.h" +#include "internal/platform/input_stream.h" +#include "internal/platform/output_stream.h" #include "internal/platform/uuid.h" namespace nearby { @@ -323,8 +332,6 @@ class BleV2Medium : public api::ble_v2::BleMedium { ABSL_GUARDED_BY(mutex_); absl::flat_hash_set> scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_); - // TODO(edwinwu): Adds extended advertisement for testing. - bool is_support_extended_advertisement_ = false; }; } // namespace g3 diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 67fb5e48..8f1dccbb 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -14,27 +14,36 @@ #include "internal/platform/medium_environment.h" -#include #include -#include #include #include #include #include #include #include -#include #include "absl/container/flat_hash_set.h" #include "absl/status/status.h" -#include "absl/strings/escaping.h" +#include "absl/strings/string_view.h" +#include "absl/time/time.h" #include "absl/types/optional.h" +#include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" +#include "internal/platform/implementation/wifi_direct.h" +#include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/logging.h" +#include "internal/platform/mutex_lock.h" +#include "internal/platform/nsd_service_info.h" #include "internal/platform/prng.h" +#include "internal/platform/runnable.h" +#include "internal/platform/uuid.h" +#include "internal/platform/wifi_credential.h" +#include "internal/test/fake_clock.h" namespace nearby { @@ -126,8 +135,9 @@ void MediumEnvironment::OnBluetoothAdapterChangedState( name = std::move(name), enabled, mode, &latch]() { NEARBY_LOGS(INFO) << "[adapter=" << &adapter - << ", device=" << &adapter_device << "] update: name=" - << ", enabled=" << enabled << ", mode=" << int32_t(mode); + << ", device=" << &adapter_device + << "] update: name=" << ", enabled=" << enabled + << ", mode=" << int32_t(mode); for (auto& medium_info : bluetooth_mediums_) { auto& info = medium_info.second; // Do not send notification to medium that owns this adapter. @@ -616,10 +626,11 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( context.advertising = enabled; context.advertisement_data = advertisement_data; - NEARBY_LOGS(INFO) - << "G3 UpdateBleV2MediumForAdvertising: this=" << this - << ", medium=" << &medium << ", medium_context=" << &context - << ", peripheral=" << &peripheral << ", enabled=" << enabled; + NEARBY_LOGS(INFO) << "G3 UpdateBleV2MediumForAdvertising: this=" << this + << ", medium=" << &medium + << ", medium_context=" << &context + << ", peripheral=" << &peripheral + << ", enabled=" << enabled; for (auto& medium_info : ble_v2_mediums_) { const api::ble_v2::BleMedium* remote_medium = medium_info.first; @@ -636,15 +647,15 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( } for (auto& remote_scanning_service_uuid : - remote_scanning_service_uuids) { + remote_scanning_service_uuids) { auto const it = context.advertisement_data.service_data.find( remote_scanning_service_uuid); // Only skip when service data is not found and the medium is // enabled. Mediums that stop advertising (disabled) pass in empty // advertisement data but should still be processed. - if (it == context.advertisement_data.service_data.end() - && enabled) continue; + if (it == context.advertisement_data.service_data.end() && enabled) + continue; NEARBY_LOGS(INFO) << "G3 UpdateBleV2MediumForAdvertising, found other medium=" @@ -732,17 +743,17 @@ void MediumEnvironment::UnregisterBleV2Medium(api::ble_v2::BleMedium& medium) { NEARBY_LOGS(INFO) << "G3 Unregistered Ble medium"; }); } -absl::optional +std::optional MediumEnvironment::GetBleV2MediumStatus(const api::ble_v2::BleMedium& medium) { - if (!enabled_) return absl::nullopt; + if (!enabled_) return std::nullopt; - absl::optional result; + std::optional result; CountDownLatch latch(1); RunOnMediumEnvironmentThread([this, &medium, &latch, &result]() { auto it = ble_v2_mediums_.find(&medium); if (it == ble_v2_mediums_.end()) { - result = absl::nullopt; + result = std::nullopt; latch.CountDown(); return; } @@ -1137,10 +1148,10 @@ void MediumEnvironment::SetFeatureFlags(const FeatureFlags::Flags& flags) { const_cast(FeatureFlags::GetInstance()).SetFlags(flags); } -absl::optional MediumEnvironment::GetSimulatedClock() { +std::optional MediumEnvironment::GetSimulatedClock() { MutexLock lock(&mutex_); if (simulated_clock_) { - return absl::optional(simulated_clock_.get()); + return std::optional(simulated_clock_.get()); } return absl::nullopt; } @@ -1372,4 +1383,12 @@ void MediumEnvironment::RemoveObserver( observers_.RemoveObserver(observer); } +void MediumEnvironment::SetBleExtendedAdvertisementsAvailable(bool enabled) { + ble_extended_advertisements_available_ = enabled; +} + +bool MediumEnvironment::IsBleExtendedAdvertisementsAvailable() const { + return ble_extended_advertisements_available_; +} + } // namespace nearby diff --git a/internal/platform/medium_environment.h b/internal/platform/medium_environment.h index 2e6d099d..bad815a4 100644 --- a/internal/platform/medium_environment.h +++ b/internal/platform/medium_environment.h @@ -16,15 +16,16 @@ #define PLATFORM_BASE_MEDIUM_ENVIRONMENT_H_ #include +#include #include #include #include #include -#include +#include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" -#include "absl/container/flat_hash_set.h" #include "absl/strings/string_view.h" +#include "absl/time/time.h" #include "absl/types/optional.h" #include "internal/base/observer_list.h" #include "internal/platform/borrowable.h" @@ -32,6 +33,7 @@ #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" +#include "internal/platform/runnable.h" #include "internal/platform/uuid.h" #include "internal/test/fake_clock.h" #ifndef NO_WEBRTC @@ -91,6 +93,8 @@ class MediumEnvironment { bool is_scanning; }; + MediumEnvironment(MediumEnvironment&&) = delete; + MediumEnvironment& operator=(MediumEnvironment&&) = delete; MediumEnvironment(const MediumEnvironment&) = delete; MediumEnvironment& operator=(const MediumEnvironment&) = delete; @@ -262,8 +266,8 @@ class MediumEnvironment { void UnregisterBleV2Medium(api::ble_v2::BleMedium& mediumum); // Collects the status for the given BleMedium. Mainly used in unit tests - // to verify if the BleMedum is in expected status after opeartions. - absl::optional GetBleV2MediumStatus( + // to verify if the BleMedum is in expected status after operations. + std::optional GetBleV2MediumStatus( const api::ble_v2::BleMedium& medium); // Adds medium-related info to allow for discovery/advertising to work. @@ -343,7 +347,7 @@ class MediumEnvironment { void SetFeatureFlags(const FeatureFlags::Flags& flags); - absl::optional GetSimulatedClock(); + std::optional GetSimulatedClock(); api::ble_v2::BleMedium* FindBleV2Medium(absl::string_view address); api::ble_v2::BleMedium* FindBleV2Medium(uint64_t id); @@ -388,6 +392,11 @@ class MediumEnvironment { void AddObserver(api::BluetoothClassicMedium::Observer* observer); void RemoveObserver(api::BluetoothClassicMedium::Observer* observer); + // Sets the availability of BLE extended advertisements. It is false by + // default. + void SetBleExtendedAdvertisementsAvailable(bool enabled); + bool IsBleExtendedAdvertisementsAvailable() const; + private: struct BluetoothMediumContext { BluetoothDiscoveryCallback callback; @@ -519,6 +528,7 @@ class MediumEnvironment { absl::Duration peer_connection_latency_ = absl::ZeroDuration(); std::unique_ptr simulated_clock_ ABSL_GUARDED_BY(mutex_); ObserverList observers_; + bool ble_extended_advertisements_available_ = false; }; } // namespace nearby