From b66e7aa6d8d2ea70a0eee8c12b2c028b43e8a8af Mon Sep 17 00:00:00 2001 From: hai007 Date: Wed, 2 Feb 2022 17:09:29 -0800 Subject: [PATCH] Fix WiFiLan discovery failure problem PiperOrigin-RevId: 426014146 --- .../implementation/windows/wifi_lan.h | 2 +- .../implementation/windows/wifi_lan_medium.cc | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/platform/implementation/windows/wifi_lan.h b/internal/platform/implementation/windows/wifi_lan.h index b226619d..90cb4bcf 100644 --- a/internal/platform/implementation/windows/wifi_lan.h +++ b/internal/platform/implementation/windows/wifi_lan.h @@ -263,7 +263,7 @@ class WifiLanMedium : public api::WifiLanMedium { static constexpr std::string_view MDNS_DEVICE_SELECTOR_FORMAT = "System.Devices.AepService.ProtocolId:=\"{4526e8c1-8aac-4153-9b16-" "55e86ada0e54}\" " - "AND System.Devices.Dnssd.ServiceName:=\"%s._tcp\" AND " + "AND System.Devices.Dnssd.ServiceName:=\"%s\" AND " "System.Devices.Dnssd.Domain:=\"local\""; // Nsd status diff --git a/internal/platform/implementation/windows/wifi_lan_medium.cc b/internal/platform/implementation/windows/wifi_lan_medium.cc index 162b165b..c69eea5a 100644 --- a/internal/platform/implementation/windows/wifi_lan_medium.cc +++ b/internal/platform/implementation/windows/wifi_lan_medium.cc @@ -194,8 +194,17 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_type, return false; } + // In WifiLan::StartDiscovery(), service_type is appended with "._tcp." for + // ios and android platform. For windows, this has to be removed because + // "._tcp" will be appended in following "selector" + std::string service_type_trim = service_type; + if (service_type.size() > 5 && + (service_type.rfind("_tcp.") == service_type.size() - 5)) { + service_type_trim.resize(service_type_trim.size() - 1); + } + std::string selector = - absl::StrFormat(MDNS_DEVICE_SELECTOR_FORMAT.data(), service_type); + absl::StrFormat(MDNS_DEVICE_SELECTOR_FORMAT.data(), service_type_trim); std::vector requestedProperties{ L"System.Devices.IpAddress", @@ -345,6 +354,21 @@ NsdServiceInfo WifiLanMedium::GetNsdServiceInformation( } nsd_service_info.SetServiceName(InspectableReader::ReadString(inspectable)); + // Service type information + inspectable = properties.TryLookup(L"System.Devices.Dnssd.ServiceName"); + if (inspectable == nullptr) { + NEARBY_LOGS(WARNING) + << "no service type information in device information."; + return nsd_service_info; + } + + // In WifiLan::StartDiscovery(), service_type is appended with "._tcp." for + // ios and android platform. For windows, we only have "._tcp" as appendix. + // Here "." is added back to match the upper layer service_type, because + // service_type is used to get the corresponding call back function. + nsd_service_info.SetServiceType( + (InspectableReader::ReadString(inspectable)).append(".")); + // IP Address information inspectable = properties.TryLookup(L"System.Devices.IPAddress"); if (inspectable == nullptr) {