Check the WiFiDirect support with Win32 API

Close the WiFi Handle after open it

PiperOrigin-RevId: 469602395
This commit is contained in:
hai007
2022-08-23 18:02:15 -07:00
committed by Copybara-Service
parent 3e78cef57f
commit 01a4daffd2
5 changed files with 29 additions and 5 deletions
+1
View File
@@ -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
@@ -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__",
@@ -15,6 +15,10 @@
#ifndef PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_H_
#define PLATFORM_IMPL_WINDOWS_WIFI_HOTSPOT_H_
// Windows headers
#include <windows.h>
#include <wlanapi.h>
// Standard C/C++ headers
#include <functional>
#include <optional>
@@ -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<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// #include <cstddef>
#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(