Merge branch 'master' into release

Change-Id: I6e5c45cffa1cae932ca1e7294f3b6ef1be26b8ee
This commit is contained in:
Alexey Polyudov
2020-06-04 13:04:42 -07:00
29 changed files with 2042 additions and 20 deletions
@@ -35,8 +35,23 @@ MediumEnvironment& MediumEnvironment::Instance() {
return *env;
}
void MediumEnvironment::Start() {
if (!enabled_.exchange(true)) {
NEARBY_LOG(INFO, "MediumEnvironment::Start()");
Reset();
}
}
void MediumEnvironment::Stop() {
if (enabled_.exchange(false)) {
NEARBY_LOG(INFO, "MediumEnvironment::Stop()");
Sync(false);
}
}
void MediumEnvironment::Reset() {
RunOnMediumEnvironmentThread([this]() {
NEARBY_LOG(INFO, "MediumEnvironment::Reset()");
bluetooth_adapters_.clear();
bluetooth_mediums_.clear();
});
@@ -45,6 +60,7 @@ void MediumEnvironment::Reset() {
void MediumEnvironment::Sync(bool enable_notifications) {
enable_notifications_ = enable_notifications;
NEARBY_LOG(INFO, "MediumEnvironment::sync(%d)", enable_notifications);
int count = 0;
do {
CountDownLatch latch(1);
@@ -64,6 +80,7 @@ void MediumEnvironment::Sync(bool enable_notifications) {
void MediumEnvironment::OnBluetoothAdapterChangedState(
api::BluetoothAdapter& adapter, api::BluetoothDevice& adapter_device,
std::string name, bool enabled, api::BluetoothAdapter::ScanMode mode) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &adapter, &adapter_device,
name = std::move(name), enabled, mode]() {
NEARBY_LOG(INFO,
@@ -88,6 +105,7 @@ void MediumEnvironment::OnDeviceStateChanged(
BluetoothMediumContext& info, api::BluetoothDevice& device,
const std::string& name, api::BluetoothAdapter::ScanMode mode,
bool enabled) {
if (!enabled_) return;
auto item = info.devices.find(&device);
if (item == info.devices.end()) {
NEARBY_LOG(
@@ -150,6 +168,7 @@ void MediumEnvironment::RunOnMediumEnvironmentThread(
void MediumEnvironment::RegisterBluetoothMedium(
api::BluetoothClassicMedium& medium,
api::BluetoothAdapter& medium_adapter) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium, &medium_adapter]() {
auto& context = bluetooth_mediums_
.insert({&medium,
@@ -170,6 +189,7 @@ void MediumEnvironment::RegisterBluetoothMedium(
void MediumEnvironment::UpdateBluetoothMedium(
api::BluetoothClassicMedium& medium, BluetoothDiscoveryCallback callback) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium,
callback = std::move(callback)]() {
auto item = bluetooth_mediums_.find(&medium);
@@ -192,6 +212,7 @@ void MediumEnvironment::UpdateBluetoothMedium(
void MediumEnvironment::UnregisterBluetoothMedium(
api::BluetoothClassicMedium& medium) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium]() {
auto item = bluetooth_mediums_.extract(&medium);
if (item.empty()) return;
+11
View File
@@ -41,6 +41,16 @@ class MediumEnvironment {
// Creates and returns a reference to the global test environment instance.
static MediumEnvironment& Instance();
// Global ON/OFF switch for medium environment.
// Start & Stop work as On/Off switch for this object.
// Default state (after creation) is ON, to make it compatible with early
// tests that are already using it and relying on it being ON.
// Enables Medium environment.
void Start();
// Disables Medium environment.
void Stop();
// Clears state. No notifications are sent.
void Reset();
@@ -109,6 +119,7 @@ class MediumEnvironment {
api::BluetoothAdapter::ScanMode mode, bool enabled);
void RunOnMediumEnvironmentThread(std::function<void()> runnable);
std::atomic_bool enabled_ = true;
std::atomic_int job_count_ = 0;
std::atomic_bool enable_notifications_ = false;
SingleThreadExecutor executor_;