mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
nearbyconnections : Implement WifiLanV2 Discovery functions for /medium, /public(wrapper), /g3.
PiperOrigin-RevId: 405782006
This commit is contained in:
committed by
Copybara-Service
parent
ef57b09fc3
commit
83480d6473
@@ -97,13 +97,10 @@ class WifiLanMediumV2 {
|
||||
|
||||
// Callback that is invoked when a discovered service is found or lost.
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_lost_cb = DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info)> service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
std::function<void(NsdServiceInfo service_info)> service_lost_cb =
|
||||
DefaultCallback<NsdServiceInfo>();
|
||||
};
|
||||
|
||||
// Starts the discovery of nearby WifiLan services.
|
||||
|
||||
@@ -22,14 +22,9 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/base/feature_flags.h"
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/prng.h"
|
||||
#include "platform/public/count_down_latch.h"
|
||||
|
||||
namespace location {
|
||||
@@ -260,6 +255,66 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnWifiLanServiceV2StateChanged(
|
||||
WifiLanMediumV2Context& info, const NsdServiceInfo& service_info,
|
||||
bool enabled) {
|
||||
if (!enabled_) return;
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = info.discovered_services.find(service_type);
|
||||
if (item == info.discovered_services.end()) {
|
||||
NEARBY_LOGS(INFO) << "G3 OnWifiLanServiceStateChanged; context=" << &info
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
// Find advertising service with matched service_type. Report it as
|
||||
// discovered by assigning the fake ip address and port.
|
||||
NsdServiceInfo discovered_service_info(service_info);
|
||||
discovered_service_info.SetIPAddress(GetFakeIPAddress());
|
||||
discovered_service_info.SetPort(GetFakePort());
|
||||
info.discovered_services.insert({service_type, discovered_service_info});
|
||||
if (enable_notifications_) {
|
||||
RunOnMediumEnvironmentThread(
|
||||
[&info, discovered_service_info, service_type]() {
|
||||
auto item = info.discovered_callbacks.find(service_type);
|
||||
if (item != info.discovered_callbacks.end()) {
|
||||
item->second.service_discovered_cb(discovered_service_info);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 OnWifiLanServiceStateChanged: exisitng service; context="
|
||||
<< &info << "; service_type=" << service_type << "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
if (enable_notifications_) {
|
||||
RunOnMediumEnvironmentThread(
|
||||
[&info, service_info = service_info, service_type]() {
|
||||
auto item = info.discovered_callbacks.find(service_type);
|
||||
if (item != info.discovered_callbacks.end()) {
|
||||
item->second.service_discovered_cb(service_info);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Known service is off.
|
||||
// Erase it from the map, and report as lost.
|
||||
if (enable_notifications_) {
|
||||
RunOnMediumEnvironmentThread(
|
||||
[&info, service_info = service_info, service_type]() {
|
||||
auto item = info.discovered_callbacks.find(service_type);
|
||||
if (item != info.discovered_callbacks.end()) {
|
||||
item->second.service_lost_cb(service_info);
|
||||
}
|
||||
});
|
||||
}
|
||||
info.discovered_services.erase(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MediumEnvironment::RunOnMediumEnvironmentThread(
|
||||
std::function<void()> runnable) {
|
||||
job_count_++;
|
||||
@@ -625,6 +680,24 @@ void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
});
|
||||
}
|
||||
|
||||
std::string MediumEnvironment::GetFakeIPAddress() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
uint32_t raw_ip_addr = Prng().NextUint32();
|
||||
ip_address[0] = static_cast<char>(raw_ip_addr >> 24);
|
||||
ip_address[1] = static_cast<char>(raw_ip_addr >> 16);
|
||||
ip_address[2] = static_cast<char>(raw_ip_addr >> 8);
|
||||
ip_address[3] = static_cast<char>(raw_ip_addr >> 0);
|
||||
|
||||
return ip_address;
|
||||
}
|
||||
|
||||
int MediumEnvironment::GetFakePort() const {
|
||||
uint16_t port = Prng().NextUint32();
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForAcceptedConnection(
|
||||
api::WifiLanMedium& medium, const std::string& service_id,
|
||||
WifiLanAcceptedConnectionCallback callback) {
|
||||
@@ -741,6 +814,40 @@ void MediumEnvironment::UpdateWifiLanMediumV2ForAdvertising(
|
||||
}
|
||||
continue;
|
||||
}
|
||||
OnWifiLanServiceV2StateChanged(info, service_info, enabled);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumV2ForDiscovery(
|
||||
api::WifiLanMediumV2& medium, WifiLanDiscoveredServiceV2Callback callback,
|
||||
const std::string& service_type, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, callback = std::move(callback),
|
||||
service_type, enabled]() {
|
||||
auto item = wifi_lan_mediums_v2_.find(&medium);
|
||||
if (item == wifi_lan_mediums_v2_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovered_callbacks.insert({service_type, std::move(callback)});
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for discovery: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_v2_) {
|
||||
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 services and send notification.
|
||||
for (auto& advertising_service : info.advertising_services) {
|
||||
auto& service_info = advertising_service.second;
|
||||
OnWifiLanServiceV2StateChanged(context, service_info, /*enabled=*/true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -751,7 +858,7 @@ void MediumEnvironment::UnregisterWifiLanMediumV2(
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = wifi_lan_mediums_v2_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOG(INFO, "Unregistered WifiLan medium");
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiLan medium";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,11 @@
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/wifi_lan.h"
|
||||
#include "platform/api/wifi_lan_v2.h"
|
||||
#include "platform/api/webrtc.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "platform/base/feature_flags.h"
|
||||
@@ -62,6 +65,8 @@ class MediumEnvironment {
|
||||
api::WifiLanMedium::DiscoveredServiceCallback;
|
||||
using WifiLanAcceptedConnectionCallback =
|
||||
api::WifiLanMedium::AcceptedConnectionCallback;
|
||||
using WifiLanDiscoveredServiceV2Callback =
|
||||
api::WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
MediumEnvironment(const MediumEnvironment&) = delete;
|
||||
MediumEnvironment& operator=(const MediumEnvironment&) = delete;
|
||||
@@ -255,6 +260,21 @@ class MediumEnvironment {
|
||||
api::WifiLanMediumV2& medium, const NsdServiceInfo& nsd_service_info,
|
||||
bool enabled);
|
||||
|
||||
// Updates discovery callback info to allow for dispatch of discovery events.
|
||||
//
|
||||
// This should be called when discoverable state changes.
|
||||
// with user-specified callback when discovery is enabled, and with default
|
||||
// (empty) callback otherwise.
|
||||
void UpdateWifiLanMediumV2ForDiscovery(
|
||||
api::WifiLanMediumV2& medium, WifiLanDiscoveredServiceV2Callback callback,
|
||||
const std::string& service_type, bool enabled);
|
||||
|
||||
// Gets Fake IP address for WifiLan medium.
|
||||
std::string GetFakeIPAddress() const;
|
||||
|
||||
// Gets Fake port number for WifiLan medium.
|
||||
int GetFakePort() const;
|
||||
|
||||
// Removes medium-related info. This should correspond to device power off.
|
||||
void UnregisterWifiLanMediumV2(api::WifiLanMediumV2& medium);
|
||||
|
||||
@@ -290,6 +310,12 @@ class MediumEnvironment {
|
||||
struct WifiLanMediumV2Context {
|
||||
// advertising service type vs NsdServiceInfo map.
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo> advertising_services;
|
||||
// discovered service type vs callback map.
|
||||
absl::flat_hash_map<std::string, WifiLanDiscoveredServiceV2Callback>
|
||||
discovered_callbacks;
|
||||
// discovered service vs service type map.
|
||||
absl::flat_hash_map<std::string, NsdServiceInfo>
|
||||
discovered_services;
|
||||
};
|
||||
|
||||
// This is a singleton object, for which destructor will never be called.
|
||||
@@ -315,6 +341,10 @@ class MediumEnvironment {
|
||||
const std::string& service_id,
|
||||
bool enabled);
|
||||
|
||||
void OnWifiLanServiceV2StateChanged(WifiLanMediumV2Context& info,
|
||||
const NsdServiceInfo& service_info,
|
||||
bool enabled);
|
||||
|
||||
void RunOnMediumEnvironmentThread(std::function<void()> runnable);
|
||||
|
||||
std::atomic_bool enabled_ = true;
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include "platform/base/logging.h"
|
||||
#include "platform/base/medium_environment.h"
|
||||
#include "platform/base/nsd_service_info.h"
|
||||
#include "platform/base/prng.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -195,11 +194,44 @@ bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
|
||||
bool WifiLanMediumV2::StartDiscovery(const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
return false;
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StartDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StartDiscovery: Can't start discovery because "
|
||||
"service_type="
|
||||
<< service_type << " has started already.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForDiscovery(*this, std::move(callback),
|
||||
service_type, true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
discovering_info_.Add(service_type);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopDiscovery(const std::string& service_type) {
|
||||
return false;
|
||||
NEARBY_LOGS(INFO) << "G3 WifiLan StopDiscovery: service_type="
|
||||
<< service_type;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (!discovering_info_.Existed(service_type)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.";
|
||||
return false;
|
||||
}
|
||||
discovering_info_.Remove(service_type);
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumV2ForDiscovery(*this, {}, service_type, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WifiLanSocketV2> WifiLanMediumV2::ConnectToService(
|
||||
@@ -218,19 +250,6 @@ std::unique_ptr<api::WifiLanServerSocketV2> WifiLanMediumV2::ListenForService(
|
||||
return {};
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMediumV2::GetFakeCredentials() const {
|
||||
std::string ip_address;
|
||||
ip_address.resize(4);
|
||||
uint32_t raw_ip_addr = Prng().NextUint32();
|
||||
uint16_t port = Prng().NextUint32();
|
||||
ip_address[0] = static_cast<char>(raw_ip_addr >> 24);
|
||||
ip_address[1] = static_cast<char>(raw_ip_addr >> 16);
|
||||
ip_address[2] = static_cast<char>(raw_ip_addr >> 8);
|
||||
ip_address[3] = static_cast<char>(raw_ip_addr >> 0);
|
||||
|
||||
return std::make_pair(ip_address, port);
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -223,11 +223,25 @@ class WifiLanMediumV2 : public api::WifiLanMediumV2 {
|
||||
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_types.empty(); }
|
||||
void Clear() { service_types.clear(); }
|
||||
void Add(const std::string& service_type) {
|
||||
service_types.insert(service_type);
|
||||
}
|
||||
void Remove(const std::string& service_type) {
|
||||
service_types.erase(service_type);
|
||||
}
|
||||
bool Existed(const std::string& service_type) const {
|
||||
return service_types.contains(service_type);
|
||||
}
|
||||
|
||||
std::pair<std::string, int> GetFakeCredentials() const;
|
||||
absl::flat_hash_set<std::string> service_types;
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
@@ -37,6 +37,8 @@ constexpr FeatureFlags kTestCases[] = {
|
||||
},
|
||||
};
|
||||
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kServiceId{"service_id"};
|
||||
constexpr absl::string_view kServiceType{"_service.tcp_"};
|
||||
constexpr absl::string_view kServiceInfoName{"Simulated service info name"};
|
||||
constexpr absl::string_view kEndpointName{"Simulated endpoint name"};
|
||||
@@ -44,6 +46,8 @@ constexpr absl::string_view kEndpointInfoKey{"n"};
|
||||
|
||||
class WifiLanMediumV2Test : public ::testing::TestWithParam<FeatureFlags> {
|
||||
protected:
|
||||
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
|
||||
|
||||
WifiLanMediumV2Test() { env_.Stop(); }
|
||||
|
||||
MediumEnvironment& env_{MediumEnvironment::Instance()};
|
||||
@@ -67,16 +71,15 @@ TEST_F(WifiLanMediumV2Test, CanStartAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
wifi_lan_a.StartAdvertising(nsd_service_info);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
env_.Stop();
|
||||
}
|
||||
@@ -84,19 +87,23 @@ TEST_F(WifiLanMediumV2Test, CanStartAdvertising) {
|
||||
TEST_F(WifiLanMediumV2Test, CanStartMultipleAdvertising) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_tye_1("_service_1.tcp_");
|
||||
std::string service_info_name{kServiceInfoName};
|
||||
std::string endpoint_info_name{kEndpointName};
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
std::string service_info_name_1(kServiceInfoName);
|
||||
std::string service_info_name_2(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
|
||||
NsdServiceInfo nsd_service_info_1;
|
||||
nsd_service_info_1.SetServiceName(service_info_name);
|
||||
nsd_service_info_1.SetServiceName(service_info_name_1);
|
||||
nsd_service_info_1.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_1.SetServiceType(service_type);
|
||||
nsd_service_info_1.SetServiceType(service_type_1);
|
||||
|
||||
NsdServiceInfo nsd_service_info_2 = nsd_service_info_1;
|
||||
nsd_service_info_2.SetServiceType(service_tye_1);
|
||||
NsdServiceInfo nsd_service_info_2;
|
||||
nsd_service_info_2.SetServiceName(service_info_name_2);
|
||||
nsd_service_info_2.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info_2.SetServiceType(service_type_2);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info_2));
|
||||
@@ -105,6 +112,113 @@ TEST_F(WifiLanMediumV2Test, CanStartMultipleAdvertising) {
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id, service_type,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanStartMultipleDiscovery) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
std::string service_id_1(kServiceId);
|
||||
std::string service_id_2("service_id_2");
|
||||
std::string service_type_1(kServiceType);
|
||||
std::string service_type_2("_service_1.tcp_");
|
||||
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_1, service_type_1,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StartDiscovery(service_id_2, service_type_2,
|
||||
DiscoveredServiceCallback{}));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_1));
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type_2));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanAdvertiseThatOtherMediumDiscover) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_lan_b.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_a.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(WifiLanMediumV2Test, CanDiscoverThatOtherMediumAdvertise) {
|
||||
env_.Start();
|
||||
WifiLanMediumV2 wifi_lan_a;
|
||||
WifiLanMediumV2 wifi_lan_b;
|
||||
std::string service_id(kServiceId);
|
||||
std::string service_type(kServiceType);
|
||||
std::string service_info_name(kServiceInfoName);
|
||||
std::string endpoint_info_name(kEndpointName);
|
||||
CountDownLatch discovered_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
wifi_lan_a.StartDiscovery(
|
||||
service_id, service_type,
|
||||
DiscoveredServiceCallback{
|
||||
.service_discovered_cb =
|
||||
[&discovered_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
discovered_latch.CountDown();
|
||||
},
|
||||
.service_lost_cb =
|
||||
[&lost_latch](NsdServiceInfo service_info,
|
||||
const std::string& service_type) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
});
|
||||
|
||||
NsdServiceInfo nsd_service_info;
|
||||
nsd_service_info.SetServiceName(service_info_name);
|
||||
nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey),
|
||||
endpoint_info_name);
|
||||
nsd_service_info.SetServiceType(service_type);
|
||||
EXPECT_TRUE(wifi_lan_b.StartAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(discovered_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_b.StopAdvertising(nsd_service_info));
|
||||
EXPECT_TRUE(lost_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(wifi_lan_a.StopDiscovery(service_type));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -27,13 +27,101 @@ bool WifiLanMediumV2::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return impl_->StopAdvertising(nsd_service_info);
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StartDiscovery(const std::string& service_type,
|
||||
bool WifiLanMediumV2::StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback) {
|
||||
return false;
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (discovery_callbacks_.contains(service_type)) {
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery already start with service_type="
|
||||
<< service_type << "; impl=" << &GetImpl();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
api::WifiLanMediumV2::DiscoveredServiceCallback api_callback = {
|
||||
.service_discovered_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto pair = discovery_services_.insert(service_type);
|
||||
if (!pair.second) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Discovering (again) service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
return;
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Adding service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_name=" << service_info.GetServiceName();
|
||||
// Callback service found.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_discovered_cb(service_info, service_id);
|
||||
} else {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "There is no callback found for service_type="
|
||||
<< service_type;
|
||||
}
|
||||
},
|
||||
.service_lost_cb =
|
||||
[this](NsdServiceInfo service_info) {
|
||||
MutexLock lock(&mutex_);
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = discovery_services_.extract(service_type);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Removing service_info=" << &service_info
|
||||
<< ", service_type=" << service_type
|
||||
<< ", service_info_name=" << service_info.GetServiceName();
|
||||
// Callback service lost.
|
||||
const auto& it = discovery_callbacks_.find(service_type);
|
||||
if (it != discovery_callbacks_.end()) {
|
||||
std::string service_id = it->second->service_id;
|
||||
DiscoveredServiceCallback medium_callback =
|
||||
it->second->medium_callback;
|
||||
medium_callback.service_lost_cb(service_info, service_id);
|
||||
}
|
||||
},
|
||||
};
|
||||
{
|
||||
// Insert callback to the map first no matter it succeeds or not.
|
||||
MutexLock lock(&mutex_);
|
||||
auto pair = discovery_callbacks_.insert(
|
||||
{service_type, absl::make_unique<DiscoveryCallbackInfo>()});
|
||||
auto& context = *pair.first->second;
|
||||
context.medium_callback = std::move(callback);
|
||||
context.service_id = service_id;
|
||||
}
|
||||
|
||||
bool success = impl_->StartDiscovery(service_type, std::move(api_callback));
|
||||
if (!success) {
|
||||
// If failed, then revert back the insertion.
|
||||
MutexLock lock(&mutex_);
|
||||
discovery_callbacks_.erase(service_type);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery started for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl()
|
||||
<< ", success=" << success;
|
||||
return success;
|
||||
}
|
||||
|
||||
bool WifiLanMediumV2::StopDiscovery(const std::string& service_type) {
|
||||
return false;
|
||||
MutexLock lock(&mutex_);
|
||||
if (!discovery_callbacks_.contains(service_type)) {
|
||||
return false;
|
||||
}
|
||||
discovery_callbacks_.erase(service_type);
|
||||
if (discovery_services_.contains(service_type)) {
|
||||
discovery_services_.erase(service_type);
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "WifiLan Discovery disabled for service_type="
|
||||
<< service_type << ", impl=" << &GetImpl();
|
||||
return impl_->StopDiscovery(service_type);
|
||||
}
|
||||
|
||||
WifiLanSocketV2 WifiLanMediumV2::ConnectToService(
|
||||
|
||||
@@ -126,15 +126,9 @@ class WifiLanMediumV2 {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
|
||||
// WifiLanService is a proxy object created as a result of WifiLan discovery.
|
||||
// Its lifetime spans between calls to service_discovered_cb and
|
||||
// service_lost_cb.
|
||||
// It is safe to use WifiLanService in service_discovered_cb() callback
|
||||
// and at any time afterwards, until service_lost_cb() is called.
|
||||
// It is not safe to use WifiLanService after returning from
|
||||
// service_lost_cb() callback.
|
||||
struct DiscoveredServiceCallback {
|
||||
std::function<void(NsdServiceInfo, const std::string& service_type)>
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
const std::string& service_type)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<NsdServiceInfo, const std::string&>();
|
||||
std::function<void(NsdServiceInfo service_info,
|
||||
@@ -143,6 +137,7 @@ class WifiLanMediumV2 {
|
||||
};
|
||||
|
||||
struct DiscoveryCallbackInfo {
|
||||
std::string service_id;
|
||||
DiscoveredServiceCallback medium_callback;
|
||||
};
|
||||
|
||||
@@ -169,7 +164,8 @@ class WifiLanMediumV2 {
|
||||
bool StopAdvertising(const NsdServiceInfo& nsd_service_info);
|
||||
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
bool StartDiscovery(const std::string& service_type,
|
||||
bool StartDiscovery(const std::string& service_id,
|
||||
const std::string& service_type,
|
||||
DiscoveredServiceCallback callback);
|
||||
|
||||
// Returns true once service_type is associated to existing callback. If the
|
||||
@@ -201,6 +197,7 @@ class WifiLanMediumV2 {
|
||||
std::unique_ptr<api::WifiLanMediumV2> impl_;
|
||||
absl::flat_hash_map<std::string, std::unique_ptr<DiscoveryCallbackInfo>>
|
||||
discovery_callbacks_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_set<std::string> discovery_services_ ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user