diff --git a/internal/platform/implementation/windows/wifi_medium.cc b/internal/platform/implementation/windows/wifi_medium.cc index 100e2dae..8ec4ffa5 100644 --- a/internal/platform/implementation/windows/wifi_medium.cc +++ b/internal/platform/implementation/windows/wifi_medium.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + // #include #include "absl/strings/str_format.h" #include "internal/platform/implementation/windows/utils.h" @@ -101,6 +103,7 @@ void WifiMedium::InitCapability() { WlanCloseHandle(client_handle, NULL); } +// TODO(b/259414512): the return type should be optional. api::WifiInformation& WifiMedium::GetInformation() { HANDLE client_handle = NULL; PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL; @@ -192,39 +195,53 @@ api::WifiInformation& WifiMedium::GetInformation() { WlanCloseHandle(client_handle, NULL); wifi_information_.ip_address_dot_decimal = GetIpAddress(); - wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string( - wifi_information_.ip_address_dot_decimal); + if (!wifi_information_.ip_address_dot_decimal.empty()) { + wifi_information_.ip_address_4_bytes = ipaddr_dotdecimal_to_4bytes_string( + wifi_information_.ip_address_dot_decimal); + } return wifi_information_; } std::string WifiMedium::GetIpAddress() { - auto host_names = NetworkInformation::GetHostNames(); - for (auto host_name : host_names) { - if (host_name.IPInformation() != nullptr && - host_name.IPInformation().NetworkAdapter() != nullptr && - host_name.Type() == HostNameType::Ipv4) { - std::string ipv4_s = winrt::to_string(host_name.ToString()); - NEARBY_LOGS(INFO) << "Found IP: " << ipv4_s; + try { + auto host_names = NetworkInformation::GetHostNames(); + for (auto host_name : host_names) { + if (host_name.IPInformation() != nullptr && + host_name.IPInformation().NetworkAdapter() != nullptr && + host_name.Type() == HostNameType::Ipv4) { + std::string ipv4_s = winrt::to_string(host_name.ToString()); + NEARBY_LOGS(INFO) << "Found IP: " << ipv4_s; - auto profile = host_name.IPInformation() - .NetworkAdapter() - .GetConnectedProfileAsync() - .get(); - if (profile != nullptr && profile.IsWlanConnectionProfile()) { - auto profile_details = profile.WlanConnectionProfileDetails(); - if (profile_details != nullptr && - wifi_information_.ssid == - winrt::to_string(profile_details.GetConnectedSsid())) { - NEARBY_LOGS(INFO) - << "SSID of this IP matches with this WiFi interface's SSID:" - << wifi_information_.ssid << ", return this IP"; - return ipv4_s; + auto profile = host_name.IPInformation() + .NetworkAdapter() + .GetConnectedProfileAsync() + .get(); + if (profile != nullptr && profile.IsWlanConnectionProfile()) { + auto profile_details = profile.WlanConnectionProfileDetails(); + if (profile_details != nullptr && + wifi_information_.ssid == + winrt::to_string(profile_details.GetConnectedSsid())) { + NEARBY_LOGS(INFO) + << "SSID of this IP matches with this WiFi interface's SSID:" + << wifi_information_.ssid << ", return this IP"; + return ipv4_s; + } } } } + return {}; + } catch (std::exception exception) { + NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); + return {}; + } catch (const winrt::hresult_error& error) { + NEARBY_LOGS(ERROR) << __func__ << ": WinRT exception: " << error.code() + << ": " << winrt::to_string(error.message()); + return {}; + } catch (...) { + NEARBY_LOGS(ERROR) << __func__ << ": Unknown exeption."; + return {}; } - return {}; } } // namespace windows