Allowed G3 to simulated BLE device supported extended advertisement

PiperOrigin-RevId: 646503301
This commit is contained in:
Guogang Li
2024-06-25 09:20:58 -07:00
committed by Copybara-Service
parent 7db26f40c5
commit 0640832025
5 changed files with 69 additions and 31 deletions
+2
View File
@@ -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",
],
)
@@ -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::AdvertisingSession> 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<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
}
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
return is_support_extended_advertisement_;
return MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable();
}
bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address,
+11 -4
View File
@@ -23,15 +23,24 @@
#include <utility>
#include <vector>
#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<std::pair<Uuid, std::uint32_t>>
scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_);
// TODO(edwinwu): Adds extended advertisement for testing.
bool is_support_extended_advertisement_ = false;
};
} // namespace g3
+38 -19
View File
@@ -14,27 +14,36 @@
#include "internal/platform/medium_environment.h"
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#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<MediumEnvironment::BleV2MediumStatus>
std::optional<MediumEnvironment::BleV2MediumStatus>
MediumEnvironment::GetBleV2MediumStatus(const api::ble_v2::BleMedium& medium) {
if (!enabled_) return absl::nullopt;
if (!enabled_) return std::nullopt;
absl::optional<MediumEnvironment::BleV2MediumStatus> result;
std::optional<MediumEnvironment::BleV2MediumStatus> 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&>(FeatureFlags::GetInstance()).SetFlags(flags);
}
absl::optional<FakeClock*> MediumEnvironment::GetSimulatedClock() {
std::optional<FakeClock*> MediumEnvironment::GetSimulatedClock() {
MutexLock lock(&mutex_);
if (simulated_clock_) {
return absl::optional<FakeClock*>(simulated_clock_.get());
return std::optional<FakeClock*>(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
+15 -5
View File
@@ -16,15 +16,16 @@
#define PLATFORM_BASE_MEDIUM_ENVIRONMENT_H_
#include <atomic>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#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<BleV2MediumStatus> GetBleV2MediumStatus(
// to verify if the BleMedum is in expected status after operations.
std::optional<BleV2MediumStatus> 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<FakeClock*> GetSimulatedClock();
std::optional<FakeClock*> 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<FakeClock> simulated_clock_ ABSL_GUARDED_BY(mutex_);
ObserverList<api::BluetoothClassicMedium::Observer> observers_;
bool ble_extended_advertisements_available_ = false;
};
} // namespace nearby