mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
internal refactor
PiperOrigin-RevId: 676552917
This commit is contained in:
committed by
Copybara-Service
parent
a9adb87b53
commit
6707efeed8
@@ -84,6 +84,24 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "wifi_utils",
|
||||
srcs = [
|
||||
"wifi_utils.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"wifi.h",
|
||||
"wifi_utils.h",
|
||||
],
|
||||
visibility = [
|
||||
"//:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/strings:str_format",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "comm",
|
||||
hdrs = [
|
||||
@@ -153,3 +171,17 @@ cc_library(
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "wifi_utils_test",
|
||||
size = "small",
|
||||
timeout = "moderate",
|
||||
srcs = ["wifi_utils_test.cc"],
|
||||
shard_count = 8,
|
||||
deps = [
|
||||
":wifi_utils",
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/numbers.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/str_join.h"
|
||||
#include "absl/strings/str_split.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace nearby {
|
||||
|
||||
// Utility function to convert channel number to frequency in MHz
|
||||
// @param channel to convert
|
||||
// @return center frequency in Mhz of the channel, return "kUnspecified" if no
|
||||
// match
|
||||
// Add band support later
|
||||
int WifiUtils::ConvertChannelToFrequencyMhz(int channel, WifiBandType band) {
|
||||
if (band == WifiBandType::kUnknown || band == WifiBandType::kBand24Ghz ||
|
||||
band == WifiBandType::kBand5Ghz) {
|
||||
if (channel == 14) {
|
||||
return 2484;
|
||||
} else if (channel >= kBand24GhzFirstChNum &&
|
||||
channel <= kBand24GhzLastChNum) {
|
||||
return ((channel - kBand24GhzFirstChNum) * 5) + kBand24GhzStartFreqMhz;
|
||||
} else if (channel >= kBand5GhzFirstChNum &&
|
||||
channel <= kBand5GhzLastChNum) {
|
||||
return ((channel - kBand5GhzFirstChNum) * 5) + kBand5GhzStartFreqMhz;
|
||||
} else {
|
||||
return kUnspecified;
|
||||
}
|
||||
}
|
||||
|
||||
if (band == WifiBandType::kBand6Ghz) {
|
||||
if (channel >= kBand6GhzFirstChNum && channel <= kBand6GhzLastChNum) {
|
||||
if (channel == 2) {
|
||||
return kBand6GhzOpClass136Ch2FreqMhz;
|
||||
}
|
||||
return ((channel - kBand6GhzFirstChNum) * 5) + kBand6GhzStartFreqMhz;
|
||||
} else {
|
||||
return kUnspecified;
|
||||
}
|
||||
}
|
||||
|
||||
if (band == WifiBandType::kBand60Ghz) {
|
||||
if (channel >= kBand60GhzFirstChNum && channel <= kBand60GhzLastChNum) {
|
||||
return ((channel - kBand60GhzFirstChNum) * 2160) + kBand60GhzStartFreqMhz;
|
||||
} else {
|
||||
return kUnspecified;
|
||||
}
|
||||
}
|
||||
|
||||
return kUnspecified;
|
||||
}
|
||||
|
||||
// Utility function to convert frequency in MHz to channel number
|
||||
// @param freqMhz frequency in MHz
|
||||
// @return channel number associated with given frequency, return "kUnspecified"
|
||||
// if no match
|
||||
int WifiUtils::ConvertFrequencyMhzToChannel(int freq_mhz) {
|
||||
// Special case
|
||||
if (freq_mhz == kBand24GhzEndFreqMhz) {
|
||||
return 14;
|
||||
} else if (freq_mhz >= kBand24GhzStartFreqMhz &&
|
||||
freq_mhz <= kBand24GhzEndFreqMhz) {
|
||||
return (freq_mhz - kBand24GhzStartFreqMhz) / 5 + kBand24GhzFirstChNum;
|
||||
} else if (freq_mhz >= kBand5GhzStartFreqMhz &&
|
||||
freq_mhz <= kBand5GhzEndFreqMhz) {
|
||||
return (freq_mhz - kBand5GhzStartFreqMhz) / 5 + kBand5GhzFirstChNum;
|
||||
} else if (freq_mhz >= kBand6GhzStartFreqMhz &&
|
||||
freq_mhz <= kBand6GhzEndFreqMhz) {
|
||||
if (freq_mhz == kBand6GhzOpClass136Ch2FreqMhz) {
|
||||
return 2;
|
||||
}
|
||||
return (freq_mhz - kBand6GhzStartFreqMhz) / 5 + kBand6GhzFirstChNum;
|
||||
} else if (freq_mhz >= kBand60GhzStartFreqMhz &&
|
||||
freq_mhz <= kBand60GhzEndFreqMhz) {
|
||||
return (freq_mhz - kBand60GhzStartFreqMhz) / 2160 + kBand60GhzFirstChNum;
|
||||
}
|
||||
|
||||
return kUnspecified;
|
||||
}
|
||||
|
||||
// Function to validate an IP address
|
||||
bool WifiUtils::ValidateIPV4(std::string ipv4) {
|
||||
int result;
|
||||
// split the string into tokens
|
||||
std::vector<absl::string_view> list = absl::StrSplit(ipv4, '.');
|
||||
|
||||
// if the token size is not equal to four
|
||||
if (list.size() != 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// validate each token
|
||||
for (absl::string_view str : list) {
|
||||
// verify that the string is a number or not, and the numbers are in the
|
||||
// valid range
|
||||
if (!absl::SimpleAtoi(str, &result)) return false;
|
||||
if (result > 255 || result < 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string WifiUtils::GetHumanReadableIpAddress(
|
||||
absl::string_view binary_address) {
|
||||
std::vector<std::string> parts;
|
||||
for (unsigned int b : binary_address) {
|
||||
parts.push_back(absl::StrFormat("%d", b));
|
||||
}
|
||||
return absl::StrJoin(parts, ".");
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef PLATFORM_PUBLIC_WIFI_UTILS_H_
|
||||
#define PLATFORM_PUBLIC_WIFI_UTILS_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/implementation/wifi.h"
|
||||
|
||||
namespace nearby {
|
||||
|
||||
using ::nearby::api::WifiBandType;
|
||||
|
||||
class WifiUtils {
|
||||
public:
|
||||
static constexpr int kUnspecified = -1;
|
||||
static constexpr int kBand24GhzFirstChNum = 1;
|
||||
static constexpr int kBand24GhzLastChNum = 14;
|
||||
static constexpr int kBand24GhzStartFreqMhz = 2412;
|
||||
static constexpr int kBand24GhzEndFreqMhz = 2484;
|
||||
static constexpr int kBand5GhzFirstChNum = 32;
|
||||
static constexpr int kBand5GhzLastChNum = 177;
|
||||
static constexpr int kBand5GhzStartFreqMhz = 5160;
|
||||
static constexpr int kBand5GhzEndFreqMhz = 5885;
|
||||
static constexpr int kBand6GhzFirstChNum = 1;
|
||||
static constexpr int kBand6GhzLastChNum = 233;
|
||||
static constexpr int kBand6GhzStartFreqMhz = 5955;
|
||||
static constexpr int kBand6GhzEndFreqMhz = 7115;
|
||||
static constexpr int kBand6GhzPscStartMhz = 5975;
|
||||
static constexpr int kBand6GhzPscStepSizeMhz = 80;
|
||||
static constexpr int kBand6GhzOpClass136Ch2FreqMhz = 5935;
|
||||
static constexpr int kBand60GhzFirstChNum = 1;
|
||||
static constexpr int kBand60GhzLastChNum = 6;
|
||||
static constexpr int kBand60GhzStartFreqMhz = 58320;
|
||||
static constexpr int kBand60GhzEndFreqMhz = 70200;
|
||||
|
||||
static int ConvertChannelToFrequencyMhz(int channel, WifiBandType band_type);
|
||||
static int ConvertFrequencyMhzToChannel(int freq_mhz);
|
||||
|
||||
static bool ValidateIPV4(std::string ipv4);
|
||||
// Converts an IP address from binary format for human readable.
|
||||
static std::string GetHumanReadableIpAddress(
|
||||
absl::string_view binary_address);
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_PUBLIC_WIFI_UTILS_H_
|
||||
@@ -0,0 +1,102 @@
|
||||
// Copyright 2022 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
constexpr int kChan6Num_2G = 9;
|
||||
constexpr int kChan6NumFreq_2G = 2452;
|
||||
|
||||
constexpr int kChan48Num_5G = 48;
|
||||
constexpr int kChan48NumFreq_5G = 5240;
|
||||
|
||||
constexpr int kChan69Num_6G = 69;
|
||||
constexpr int kChan69NumFreq_6G = 6295;
|
||||
|
||||
constexpr int kChan4Num_60G = 4;
|
||||
constexpr int kChan4NumFreq_60G = 64800;
|
||||
|
||||
constexpr int kChan20Num_2G_NotExist = 20;
|
||||
constexpr int kChan180Num_5G_NotExist = 180;
|
||||
constexpr int kChan0Num_6G_NotExist = 0;
|
||||
constexpr int kChan30Num_60G_NotExist = 30;
|
||||
|
||||
constexpr int kFreqNotExist = 1002;
|
||||
|
||||
TEST(WifiUtilsTest, ConvertChannelToFrequency) {
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan6Num_2G,
|
||||
WifiBandType::kUnknown),
|
||||
kChan6NumFreq_2G);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan6Num_2G,
|
||||
WifiBandType::kBand24Ghz),
|
||||
kChan6NumFreq_2G);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan48Num_5G,
|
||||
WifiBandType::kBand5Ghz),
|
||||
kChan48NumFreq_5G);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan69Num_6G,
|
||||
WifiBandType::kBand6Ghz),
|
||||
kChan69NumFreq_6G);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan4Num_60G,
|
||||
WifiBandType::kBand60Ghz),
|
||||
kChan4NumFreq_60G);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan20Num_2G_NotExist,
|
||||
WifiBandType::kBand24Ghz),
|
||||
WifiUtils::kUnspecified);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan180Num_5G_NotExist,
|
||||
WifiBandType::kBand5Ghz),
|
||||
WifiUtils::kUnspecified);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan0Num_6G_NotExist,
|
||||
WifiBandType::kBand6Ghz),
|
||||
WifiUtils::kUnspecified);
|
||||
EXPECT_EQ(WifiUtils::ConvertChannelToFrequencyMhz(kChan30Num_60G_NotExist,
|
||||
WifiBandType::kBand60Ghz),
|
||||
WifiUtils::kUnspecified);
|
||||
}
|
||||
|
||||
TEST(WifiUtilsTest, ConvertFrequencyToChannel) {
|
||||
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kChan6NumFreq_2G),
|
||||
kChan6Num_2G);
|
||||
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kChan48NumFreq_5G),
|
||||
kChan48Num_5G);
|
||||
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kChan69NumFreq_6G),
|
||||
kChan69Num_6G);
|
||||
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kChan4NumFreq_60G),
|
||||
kChan4Num_60G);
|
||||
EXPECT_EQ(WifiUtils::ConvertFrequencyMhzToChannel(kFreqNotExist),
|
||||
WifiUtils::kUnspecified);
|
||||
}
|
||||
|
||||
TEST(WifiUtilsTest, Ipv4Validation) {
|
||||
EXPECT_FALSE(WifiUtils::ValidateIPV4("12.34.212.46.37"));
|
||||
EXPECT_FALSE(WifiUtils::ValidateIPV4("12.gt.212.46"));
|
||||
EXPECT_FALSE(WifiUtils::ValidateIPV4("192.168.358.46"));
|
||||
EXPECT_FALSE(WifiUtils::ValidateIPV4("192.-168.1.46"));
|
||||
EXPECT_TRUE(WifiUtils::ValidateIPV4("192.168.1.46"));
|
||||
}
|
||||
|
||||
TEST(WifiUtilsTest, GetHumanReadableIpAddress) {
|
||||
EXPECT_EQ(
|
||||
WifiUtils::GetHumanReadableIpAddress(absl::HexStringToBytes("000AFEFF")),
|
||||
"0.10.254.255");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
@@ -48,6 +48,7 @@ cc_library(
|
||||
"//internal/base:files",
|
||||
"//internal/flags:nearby_flags",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation/windows/generated:types",
|
||||
@@ -107,11 +108,11 @@ cc_library(
|
||||
"//connections/implementation/flags:connections_flags",
|
||||
"//internal/flags:nearby_flags",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/flags:platform_flags",
|
||||
"//internal/platform/implementation:account_manager",
|
||||
"//internal/platform/implementation:comm",
|
||||
"//internal/platform/implementation:wifi_utils",
|
||||
"//internal/platform/implementation/shared:count_down_latch",
|
||||
"//internal/platform/implementation/windows/generated:types",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
@@ -221,6 +222,7 @@ cc_library(
|
||||
"//internal/platform/implementation:comm",
|
||||
"//internal/platform/implementation:platform",
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform/implementation:wifi_utils",
|
||||
"//internal/platform/implementation/shared:count_down_latch",
|
||||
"//internal/platform/implementation/shared:file",
|
||||
"//internal/platform/implementation/windows/generated:types",
|
||||
|
||||
@@ -15,14 +15,14 @@
|
||||
#ifndef PLATFORM_IMPL_WINDOWS_WIFI_H_
|
||||
#define PLATFORM_IMPL_WINDOWS_WIFI_H_
|
||||
|
||||
#include <windows.h>
|
||||
#include <objbase.h>
|
||||
#include <windows.h>
|
||||
#include <wlanapi.h>
|
||||
#include <wtypes.h>
|
||||
|
||||
// Nearby connections headers
|
||||
#include "internal/platform/implementation/wifi.h"
|
||||
#include "internal/platform/wifi_utils.h"
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
|
||||
// WinRT headers
|
||||
#include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.Collections.h"
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/implementation/wifi_direct.h"
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
#include "internal/platform/implementation/windows/wifi_direct.h"
|
||||
#include "internal/platform/wifi_utils.h"
|
||||
|
||||
// Nearby connections headers
|
||||
#include "internal/platform/cancellation_flag_listener.h"
|
||||
|
||||
@@ -26,13 +26,13 @@
|
||||
#include "internal/platform/feature_flags.h"
|
||||
#include "internal/platform/flags/nearby_platform_feature_flags.h"
|
||||
#include "internal/platform/implementation/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/implementation/windows/wifi_hotspot.h"
|
||||
#include "internal/platform/implementation/windows/wifi_intel.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/prng.h"
|
||||
#include "internal/platform/wifi_credential.h"
|
||||
#include "internal/platform/wifi_utils.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
// #include <cstddef>
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "internal/platform/implementation/wifi.h"
|
||||
#include "internal/platform/implementation/wifi_utils.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/implementation/windows/wifi.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/wifi_utils.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
|
||||
Reference in New Issue
Block a user