mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/321106672
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I6e34e075aa2cbbacba714019f6d0e7506313e53d
This commit is contained in:
@@ -36,9 +36,10 @@ MediumEnvironment& MediumEnvironment::Instance() {
|
||||
return *env;
|
||||
}
|
||||
|
||||
void MediumEnvironment::Start() {
|
||||
void MediumEnvironment::Start(EnvironmentConfig config) {
|
||||
if (!enabled_.exchange(true)) {
|
||||
NEARBY_LOG(INFO, "MediumEnvironment::Start()");
|
||||
config_ = std::move(config);
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
@@ -79,6 +80,10 @@ void MediumEnvironment::Sync(bool enable_notifications) {
|
||||
NEARBY_LOG(INFO, "MediumEnvironment::Sync(): done [count=%d]", count);
|
||||
}
|
||||
|
||||
const EnvironmentConfig& MediumEnvironment::GetEnvironmentConfig() {
|
||||
return config_;
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
api::BluetoothAdapter& adapter, api::BluetoothDevice& adapter_device,
|
||||
std::string name, bool enabled, api::BluetoothAdapter::ScanMode mode) {
|
||||
@@ -88,7 +93,8 @@ void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
NEARBY_LOG(INFO,
|
||||
"[adapter=%p, device=%p] update: name=%s, enabled=%d, mode=%d",
|
||||
&adapter, &adapter_device, name.c_str(), enabled, mode);
|
||||
for (auto& [medium, info] : bluetooth_mediums_) {
|
||||
for (auto& medium_info : bluetooth_mediums_) {
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to medium that owns this adapter.
|
||||
if (info.adapter == &adapter) continue;
|
||||
NEARBY_LOG(INFO, "[adapter=%p, device=%p] notify: adapter=%p", &adapter,
|
||||
@@ -167,19 +173,22 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
const std::string& service_id, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 OnWifiLanServiceStateChanged [service impl=%p]; context=%p, "
|
||||
"notify=%d",
|
||||
&info, &service, enable_notifications_.load());
|
||||
"G3 OnWifiLanServiceStateChanged [service impl=%p]; context=%p; "
|
||||
"service_id=%s; notify=%d",
|
||||
&service, &info, service_id.c_str(), enable_notifications_.load());
|
||||
if (!enable_notifications_) return;
|
||||
if (enabled) {
|
||||
RunOnMediumEnvironmentThread([&info, &service, service_id]() {
|
||||
info.discovery_callback.service_discovered_cb(service, service_id);
|
||||
});
|
||||
} else {
|
||||
RunOnMediumEnvironmentThread([&info, &service, service_id]() {
|
||||
info.discovery_callback.service_lost_cb(service, service_id);
|
||||
});
|
||||
}
|
||||
RunOnMediumEnvironmentThread([&info, enabled, &service, service_id]() {
|
||||
auto service_id_context = info.services.find(service_id);
|
||||
if (service_id_context == info.services.end()) return;
|
||||
|
||||
if (enabled) {
|
||||
service_id_context->second.discovery_callback.service_discovered_cb(
|
||||
service, service_id);
|
||||
} else {
|
||||
service_id_context->second.discovery_callback.service_lost_cb(service,
|
||||
service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RunOnMediumEnvironmentThread(
|
||||
@@ -202,7 +211,9 @@ void MediumEnvironment::RegisterBluetoothMedium(
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p; adapter=%p", &medium,
|
||||
owned_adapter);
|
||||
for (auto& [adapter, device] : bluetooth_adapters_) {
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
if (adapter == nullptr) continue;
|
||||
OnBluetoothDeviceStateChanged(context, *device, adapter->GetName(),
|
||||
adapter->GetScanMode(),
|
||||
@@ -226,7 +237,9 @@ void MediumEnvironment::UpdateBluetoothMedium(
|
||||
"Updated: this=%p; medium=%p; adapter=%p; name=%s; enabled=%d; mode=%d",
|
||||
this, &medium, owned_adapter, owned_adapter->GetName().c_str(),
|
||||
owned_adapter->IsEnabled(), owned_adapter->GetScanMode());
|
||||
for (auto& [adapter, device] : bluetooth_adapters_) {
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
if (adapter == nullptr) continue;
|
||||
OnBluetoothDeviceStateChanged(context, *device, adapter->GetName(),
|
||||
adapter->GetScanMode(),
|
||||
@@ -285,13 +298,10 @@ void MediumEnvironment::SendWebRtcSignalingMessage(absl::string_view peer_id,
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium,
|
||||
api::WifiLanService& service) {
|
||||
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{
|
||||
.service = &service,
|
||||
}});
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{}});
|
||||
NEARBY_LOG(INFO, "Registered: medium=%p", &medium);
|
||||
});
|
||||
}
|
||||
@@ -304,18 +314,31 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Update WifiLan medium failed. There is no medium registered.");
|
||||
NEARBY_LOG(INFO,
|
||||
"UpdateWifiLanMediumForAdvertising failed. There is no medium "
|
||||
"registered.");
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.advertising = enabled;
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Update WifiLan medium for advertising: this=%p; medium=%p; name=%s; "
|
||||
"enabled=%d; advertising=%d",
|
||||
this, &medium, service.GetName().c_str(), enabled, context.advertising);
|
||||
for (auto& [local_medium, info] : wifi_lan_mediums_) {
|
||||
context.wifi_lan_service = &service;
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.advertising = enabled,
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.advertising = enabled;
|
||||
}
|
||||
NEARBY_LOG(INFO,
|
||||
"Update WifiLan medium for advertising: this=%p; medium=%p; "
|
||||
"service_id=%s; name=%s; "
|
||||
"enabled=%d",
|
||||
this, &medium, service_id.c_str(), service.GetName().c_str(),
|
||||
enabled);
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
OnWifiLanServiceStateChanged(info, service, service_id, enabled);
|
||||
@@ -324,45 +347,56 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id, WifiLanDiscoveredServiceCallback callback,
|
||||
bool enabled) {
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback callback, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service, service_id,
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Update WifiLan medium failed. There is no medium registered.");
|
||||
NEARBY_LOG(INFO,
|
||||
"UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.");
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovery_callback = std::move(callback);
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Update WifiLan medium for discovery: this=%p; medium=%p; name=%s; "
|
||||
"enabled=%d; advertising=%d",
|
||||
this, &medium, service.GetName().c_str(), enabled, context.advertising);
|
||||
for (auto& [local_medium, info] : wifi_lan_mediums_) {
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.discovery_callback = std::move(callback),
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.discovery_callback = std::move(callback);
|
||||
}
|
||||
NEARBY_LOG(INFO,
|
||||
"Update WifiLan medium for discovery: this=%p; medium=%p; "
|
||||
"service_id=%s; enabled=%d; ",
|
||||
this, &medium, service_id.c_str(), enabled);
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
// Search advertising mediums and send notification.
|
||||
if (info.advertising && enabled) {
|
||||
OnWifiLanServiceStateChanged(context, *(info.service), service_id,
|
||||
enabled);
|
||||
for (auto& service_id_context : info.services) {
|
||||
auto& service_id = service_id_context.first;
|
||||
auto& id_context = service_id_context.second;
|
||||
if (id_context.advertising && enabled) {
|
||||
OnWifiLanServiceStateChanged(context, *(info.wifi_lan_service),
|
||||
service_id, enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback) {
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &service, service_id,
|
||||
accepted_connection_callback =
|
||||
std::move(accepted_connection_callback)]() {
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
callback = std::move(callback)]() {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOG(
|
||||
@@ -370,12 +404,20 @@ void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.accepted_connection_callback =
|
||||
std::move(accepted_connection_callback);
|
||||
auto service_id_context = context.services.find(service_id);
|
||||
if (service_id_context == context.services.end()) {
|
||||
WifiLanServiceIdContext id_context{
|
||||
.accepted_connection_callback = std::move(callback),
|
||||
};
|
||||
context.services.emplace(service_id, std::move(id_context));
|
||||
} else {
|
||||
service_id_context->second.accepted_connection_callback =
|
||||
std::move(callback);
|
||||
}
|
||||
NEARBY_LOG(INFO,
|
||||
"Update WifiLan medium for accepted callback: this=%p; "
|
||||
"medium=%p; name=%s; ",
|
||||
this, &medium, service.GetName().c_str());
|
||||
"medium=%p; service_id=%s; ",
|
||||
this, &medium, service_id.c_str());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -401,7 +443,11 @@ void MediumEnvironment::CallWifiLanAcceptedConnectionCallback(
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
info.accepted_connection_callback.accepted_cb(socket, service_id);
|
||||
auto service_id_context = info.services.find(service_id);
|
||||
if (service_id_context != info.services.end()) {
|
||||
service_id_context->second.accepted_connection_callback.accepted_cb(
|
||||
socket, service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// Environment config that can control availability of certain mediums for
|
||||
// testing.
|
||||
struct EnvironmentConfig {
|
||||
// Control whether WEB_RTC medium is enabled in the environment.
|
||||
// This is currently set to false, due to http://b/139734036 that would lead
|
||||
// to flaky tests.
|
||||
bool webrtc_enabled = false;
|
||||
};
|
||||
|
||||
// MediumEnvironment is a simulated environment which allows multiple instances
|
||||
// of simulated HW devices to "work" together as if they are physical.
|
||||
// For each medium type it provides necessary methods to implement
|
||||
@@ -44,6 +53,7 @@ class MediumEnvironment {
|
||||
api::WifiLanMedium::DiscoveredServiceCallback;
|
||||
using WifiLanAcceptedConnectionCallback =
|
||||
api::WifiLanMedium::AcceptedConnectionCallback;
|
||||
|
||||
MediumEnvironment(const MediumEnvironment&) = delete;
|
||||
MediumEnvironment& operator=(const MediumEnvironment&) = delete;
|
||||
|
||||
@@ -56,7 +66,7 @@ class MediumEnvironment {
|
||||
// tests that are already using it and relying on it being ON.
|
||||
|
||||
// Enables Medium environment.
|
||||
void Start();
|
||||
void Start(EnvironmentConfig config = EnvironmentConfig());
|
||||
// Disables Medium environment.
|
||||
void Stop();
|
||||
|
||||
@@ -107,6 +117,8 @@ class MediumEnvironment {
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterBluetoothMedium(api::BluetoothClassicMedium& medium);
|
||||
|
||||
const EnvironmentConfig& GetEnvironmentConfig();
|
||||
|
||||
// Registers |callback| to receive messages sent to device with id |self_id|.
|
||||
void RegisterWebRtcSignalingMessenger(absl::string_view self_id,
|
||||
OnSignalingMessageCallback callback);
|
||||
@@ -121,8 +133,7 @@ class MediumEnvironment {
|
||||
// Adds medium-related info to allow for discovery/advertising to work.
|
||||
// This provides acccess to this medium from other mediums, when protocol
|
||||
// expects they should communicate.
|
||||
void RegisterWifiLanMedium(api::WifiLanMedium& medium,
|
||||
api::WifiLanService& service);
|
||||
void RegisterWifiLanMedium(api::WifiLanMedium& medium);
|
||||
|
||||
// Updates advertising info to indicate the current medium is exposing
|
||||
// advertising event.
|
||||
@@ -140,16 +151,14 @@ class MediumEnvironment {
|
||||
// with user-specified callback when discovery is enabled, and with default
|
||||
// (empty) callback otherwise.
|
||||
void UpdateWifiLanMediumForDiscovery(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback discovery_callback, bool enabled);
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanDiscoveredServiceCallback callback, bool enabled);
|
||||
|
||||
// Updates Accepted connection callback info to allow for dispatch of
|
||||
// advertising events.
|
||||
void UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, api::WifiLanService& service,
|
||||
const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback);
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback);
|
||||
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterWifiLanMedium(api::WifiLanMedium& medium);
|
||||
@@ -168,13 +177,17 @@ class MediumEnvironment {
|
||||
absl::flat_hash_map<api::BluetoothDevice*, std::string> devices;
|
||||
};
|
||||
|
||||
struct WifiLanMediumContext {
|
||||
struct WifiLanServiceIdContext {
|
||||
WifiLanDiscoveredServiceCallback discovery_callback;
|
||||
WifiLanAcceptedConnectionCallback accepted_connection_callback;
|
||||
api::WifiLanService* service = nullptr;
|
||||
bool advertising = false;
|
||||
};
|
||||
|
||||
struct WifiLanMediumContext {
|
||||
api::WifiLanService* wifi_lan_service = nullptr;
|
||||
absl::flat_hash_map<std::string, WifiLanServiceIdContext> services;
|
||||
};
|
||||
|
||||
// This is a singleton object, for which destructor will never be called.
|
||||
// Constructor will be invoked once from Instance() static method.
|
||||
// Object is create in-place (with a placement new) to guarantee that
|
||||
@@ -199,6 +212,7 @@ class MediumEnvironment {
|
||||
std::atomic_int job_count_ = 0;
|
||||
std::atomic_bool enable_notifications_ = false;
|
||||
SingleThreadExecutor executor_;
|
||||
EnvironmentConfig config_;
|
||||
|
||||
// The following data members are accessed in the context of a private
|
||||
// executor_ thread.
|
||||
|
||||
Reference in New Issue
Block a user