From 01a4daffd286651c408572e0096448d83f1e51f6 Mon Sep 17 00:00:00 2001 From: hai007 Date: Tue, 23 Aug 2022 18:00:54 -0700 Subject: [PATCH] Check the WiFiDirect support with Win32 API Close the WiFi Handle after open it PiperOrigin-RevId: 469602395 --- internal/platform/implementation/wifi.h | 1 + internal/platform/implementation/windows/BUILD | 4 +++- .../implementation/windows/wifi_hotspot.h | 4 ++++ .../windows/wifi_hotspot_medium.cc | 18 ++++++++++++++---- .../implementation/windows/wifi_medium.cc | 7 +++++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/platform/implementation/wifi.h b/internal/platform/implementation/wifi.h index 6c149a44..81f71837 100644 --- a/internal/platform/implementation/wifi.h +++ b/internal/platform/implementation/wifi.h @@ -59,6 +59,7 @@ enum class WifiBandType { struct WifiCapability { bool supports_5_ghz = false; bool supports_6_ghz = false; + bool support_wifi_direct = false; }; // Native WiFi's information parameters diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index cb17ef5e..f402f637 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -127,7 +127,9 @@ cc_library( "wifi_lan_socket.cc", "wifi_medium.cc", ], - copts = ["-Ithird_party/nearby/internal/platform/implementation/windows/generated -Ithird_party/nearby/internal/platform/implementation/windows/json"], + # This is the temporary solution to solve compilation error of Win32 WFDxxx() related API. + # WFD API is only support after _WIN32_WINNT_WIN8, but the current lexan _WIN32_WINNT is set to _WIN32_WINNT_WIN7 + copts = ["-Ithird_party/nearby/internal/platform/implementation/windows/generated -Ithird_party/nearby/internal/platform/implementation/windows/json -D_WIN32_WINNT=_WIN32_WINNT_WIN10 -DWINVER=_WIN32_WINNT_WIN10"], defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"], visibility = [ "//location/nearby:__subpackages__", diff --git a/internal/platform/implementation/windows/wifi_hotspot.h b/internal/platform/implementation/windows/wifi_hotspot.h index 38d96667..852f2bb4 100644 --- a/internal/platform/implementation/windows/wifi_hotspot.h +++ b/internal/platform/implementation/windows/wifi_hotspot.h @@ -15,6 +15,10 @@ #ifndef PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_H_ #define PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_H_ +// Windows headers +#include +#include + // Standard C/C++ headers #include #include diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index 66e11f7f..dfd3104c 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -38,10 +38,20 @@ WifiHotspotMedium::~WifiHotspotMedium() { } bool WifiHotspotMedium::IsInterfaceValid() const { - // Windows 10 starts to support WiFi direct feature, so don't need to check - // feature by OS due to targeting OS version is at leat Windows 10. - NEARBY_LOGS(ERROR) << "WiFi hotspot: valid interface found."; - return true; + HANDLE wifi_direct_handle = NULL; + DWORD negotiated_version = 0; + DWORD result = 0; + + result = + WFDOpenHandle(WFD_API_VERSION, &negotiated_version, &wifi_direct_handle); + if (result == ERROR_SUCCESS) { + NEARBY_LOGS(INFO) << "WiFi can support Hotspot"; + WFDCloseHandle(wifi_direct_handle); + return true; + } + + NEARBY_LOGS(ERROR) << "WiFi can't support Hotspot"; + return false; } std::unique_ptr WifiHotspotMedium::ConnectToService( diff --git a/internal/platform/implementation/windows/wifi_medium.cc b/internal/platform/implementation/windows/wifi_medium.cc index 98eb3f89..3caeedd4 100644 --- a/internal/platform/implementation/windows/wifi_medium.cc +++ b/internal/platform/implementation/windows/wifi_medium.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// #include #include "absl/strings/str_format.h" #include "internal/platform/implementation/windows/wifi.h" #include "internal/platform/implementation/windows/utils.h" @@ -76,6 +77,7 @@ void WifiMedium::InitCapability() { } if (!p_intf_list) { NEARBY_LOGS(INFO) << "WlanEnumInterfaces failed with error: "; + WlanCloseHandle(client_handle, NULL); return; } wifi_interface_valid_ = true; @@ -87,6 +89,7 @@ void WifiMedium::InitCapability() { NEARBY_LOGS(INFO) << "Get Capability failed"; WlanFreeMemory(p_intf_list); p_intf_list = NULL; + WlanCloseHandle(client_handle, NULL); return; } @@ -100,6 +103,7 @@ void WifiMedium::InitCapability() { p_intf_capability = NULL; WlanFreeMemory(p_intf_list); p_intf_list = NULL; + WlanCloseHandle(client_handle, NULL); } api::WifiInformation& WifiMedium::GetInformation() { @@ -125,6 +129,7 @@ api::WifiInformation& WifiMedium::GetInformation() { } if (!p_intf_list) { NEARBY_LOGS(INFO) << "WlanEnumInterfaces failed with error: "; + WlanCloseHandle(client_handle, NULL); return wifi_information_; } @@ -159,6 +164,7 @@ api::WifiInformation& WifiMedium::GetInformation() { NEARBY_LOGS(INFO) << "WlanQueryInterface error = " << result; WlanFreeMemory(p_intf_list); p_intf_list = NULL; + WlanCloseHandle(client_handle, NULL); return wifi_information_; } @@ -188,6 +194,7 @@ api::WifiInformation& WifiMedium::GetInformation() { p_connect_info = NULL; WlanFreeMemory(p_intf_list); p_intf_list = NULL; + WlanCloseHandle(client_handle, NULL); wifi_information_.ip_address_dot_decimal = GetIpAddress(); wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string(