Skip to content

Commit

Permalink
WIFI Hotspot implementation (3)
Browse files Browse the repository at this point in the history
Part 3: WIFI Hotspot platform code for Windows

PiperOrigin-RevId: 441826092
  • Loading branch information
hai007 authored and copybara-github committed Apr 14, 2022
1 parent bc01739 commit 93f203d
Show file tree
Hide file tree
Showing 10 changed files with 1,059 additions and 4 deletions.
18 changes: 17 additions & 1 deletion internal/platform/implementation/windows/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ cc_library(
"thread_pool.h",
"webrtc.h",
"wifi.h",
"wifi_hotspot.h",
"wifi_lan.h",
],
visibility = ["//visibility:private"],
deps = [
"//internal/platform:base",
"//internal/platform:types",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:types",
"//internal/platform/implementation/windows/generated:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:optional",
],
)

Expand Down Expand Up @@ -103,6 +112,9 @@ cc_library(
"system_clock.cc",
"thread_pool.cc",
"utils.cc",
"wifi_hotspot_medium.cc",
"wifi_hotspot_server_socket.cc",
"wifi_hotspot_socket.cc",
"wifi_lan_medium.cc",
"wifi_lan_server_socket.cc",
"wifi_lan_socket.cc",
Expand All @@ -117,12 +129,16 @@ cc_library(
":comm",
":crypto", # build_cleaner: keep
":types",
"//internal/platform:base",
"//internal/platform:cancellation_flag",
"//internal/platform:types",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:platform",
"//internal/platform/implementation:types",
"//internal/platform/implementation/shared:count_down_latch",
"//internal/platform/implementation/shared:file",
"//internal/platform/implementation/windows/generated:types",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)

Expand Down
4 changes: 3 additions & 1 deletion internal/platform/implementation/windows/platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <windows.h>

#include <xstring>
#include <string>
#include <sstream>

#include "internal/platform/implementation/shared/count_down_latch.h"
Expand All @@ -42,6 +43,7 @@
#include "internal/platform/implementation/windows/webrtc.h"
#include "internal/platform/implementation/windows/wifi.h"
#include "internal/platform/implementation/windows/wifi_lan.h"
#include "internal/platform/implementation/windows/wifi_hotspot.h"

namespace location {
namespace nearby {
Expand Down Expand Up @@ -234,7 +236,7 @@ std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {

std::unique_ptr<WifiHotspotMedium>
ImplementationPlatform::CreateWifiHotspotMedium() {
return nullptr;
return std::make_unique<windows::WifiHotspotMedium>();
}

// TODO(b/184975123): replace with real implementation.
Expand Down
30 changes: 30 additions & 0 deletions internal/platform/implementation/windows/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "internal/platform/implementation/windows/utils.h"

// Windows headers
#include <inaddr.h>
#include <strsafe.h>
#include <winsock.h>

// Standard C/C++ headers
#include <codecvt>
Expand Down Expand Up @@ -43,6 +45,34 @@ std::string uint64_to_mac_address_string(uint64_t bluetoothAddress) {
return absl::AsciiStrToUpper(buffer);
}

std::string ipaddr_4bytes_to_dotdecimal_string(
absl::string_view ipaddr_4bytes) {
in_addr address;
address.S_un.S_un_b.s_b1 = ipaddr_4bytes[0];
address.S_un.S_un_b.s_b2 = ipaddr_4bytes[1];
address.S_un.S_un_b.s_b3 = ipaddr_4bytes[2];
address.S_un.S_un_b.s_b4 = ipaddr_4bytes[3];
char* ipv4_address = inet_ntoa(address);
if (ipv4_address == nullptr) {
return {};
}

return std::string(ipv4_address);
}

std::string ipaddr_dotdecimal_to_4bytes_string(std::string ipv4_s) {
in_addr address;
address.S_un.S_addr = inet_addr(ipv4_s.c_str());
char ipv4_b[5];
ipv4_b[0] = address.S_un.S_un_b.s_b1;
ipv4_b[1] = address.S_un.S_un_b.s_b2;
ipv4_b[2] = address.S_un.S_un_b.s_b3;
ipv4_b[3] = address.S_un.S_un_b.s_b4;
ipv4_b[4] = 0;

return std::string(ipv4_b, 4);
}

std::wstring string_to_wstring(std::string str) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(str);
Expand Down
3 changes: 3 additions & 0 deletions internal/platform/implementation/windows/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ using winrt::Windows::Foundation::IInspectable;

std::string uint64_to_mac_address_string(uint64_t bluetoothAddress);

std::string ipaddr_4bytes_to_dotdecimal_string(absl::string_view ipaddr_4bytes);
std::string ipaddr_dotdecimal_to_4bytes_string(std::string ipv4_s);

// Helpers to windows platform
std::wstring string_to_wstring(std::string str);
std::string wstring_to_string(std::wstring wstr);
Expand Down
29 changes: 28 additions & 1 deletion internal/platform/implementation/windows/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@

#include "internal/platform/implementation/windows/utils.h"

#include <windows.h>
#include <string>

#include "gtest/gtest.h"

using location::nearby::windows::uint64_to_mac_address_string;
// using location::nearby::windows::uint64_to_mac_address_string;
namespace location {
namespace nearby {
namespace windows {

TEST(UtilsTests, MacAddressToString) {
// Arrange
Expand All @@ -29,3 +35,24 @@ TEST(UtilsTests, MacAddressToString) {
// Assert
EXPECT_EQ(result, expected);
}

constexpr absl::string_view kIpDotdecimal{"192.168.1.37"};
constexpr char kIp4Bytes[] = {192, 168, 1, 37};

TEST(UtilsTests, Ip4BytesToDotdecimal) {
std::string result =
ipaddr_4bytes_to_dotdecimal_string(absl::string_view(kIp4Bytes));

EXPECT_EQ(result, kIpDotdecimal);
}

TEST(UtilsTests, IpDotdecimalTo4Bytes) {
std::string result =
ipaddr_dotdecimal_to_4bytes_string(std::string(kIpDotdecimal));

EXPECT_EQ(result, std::string(kIp4Bytes));
}

} // namespace windows
} // namespace nearby
} // namespace location
Loading

0 comments on commit 93f203d

Please sign in to comment.