WIFI Direct implementation (1)

Part 1: Implement internal common code
Add implementation/g3 skeleton code for testing coverage.

PiperOrigin-RevId: 485067018
This commit is contained in:
Suet-Fei Li
2022-10-31 09:14:09 -07:00
committed by Copybara-Service
parent 81f6b547bb
commit b6b692b0ea
14 changed files with 448 additions and 0 deletions
+1
View File
@@ -479,6 +479,7 @@ let package = Package(
"internal/platform/count_down_latch_test.cc",
"internal/platform/pipe_test.cc",
"internal/platform/uuid_test.cc",
"internal/platform/wifi_direct_test.cc",
"internal/platform/wifi_hotspot_test.cc",
"internal/platform/wifi_lan_test.cc",
"internal/platform/wifi_test.cc",
+3
View File
@@ -365,6 +365,7 @@ cc_library(
"bluetooth_classic.cc",
"credential_storage_impl.cc",
"file.cc",
"wifi_direct.cc",
"wifi_hotspot.cc",
"wifi_lan.cc",
"wifi_utils.cc",
@@ -376,6 +377,7 @@ cc_library(
"bluetooth_classic.h",
"credential_storage_impl.h",
"wifi.h",
"wifi_direct.h",
"wifi_hotspot.h",
"wifi_lan.h",
"wifi_utils.h",
@@ -431,6 +433,7 @@ cc_test(
"scheduled_executor_test.cc",
"single_thread_executor_test.cc",
"uuid_test.cc",
"wifi_direct_test.cc",
"wifi_hotspot_test.cc",
"wifi_lan_test.cc",
"wifi_test.cc",
+1
View File
@@ -61,6 +61,7 @@ cc_library(
"credential_storage.h",
"server_sync.h",
"wifi.h",
"wifi_direct.h",
"wifi_hotspot.h",
"wifi_lan.h",
],
@@ -57,6 +57,7 @@ cc_library(
"bluetooth_adapter.cc",
"bluetooth_classic.cc",
"credential_storage_impl.cc",
"wifi_direct.cc",
"wifi_hotspot.cc",
"wifi_lan.cc",
],
@@ -66,6 +67,7 @@ cc_library(
"bluetooth_adapter.h",
"bluetooth_classic.h",
"credential_storage_impl.h",
"wifi_direct.h",
"wifi_hotspot.h",
"wifi_lan.h",
],
@@ -51,6 +51,7 @@
#include "internal/platform/implementation/g3/mutex.h"
#include "internal/platform/implementation/g3/scheduled_executor.h"
#include "internal/platform/implementation/g3/single_thread_executor.h"
#include "internal/platform/implementation/g3/wifi_direct.h"
#include "internal/platform/implementation/g3/wifi_hotspot.h"
#include "internal/platform/implementation/g3/wifi_lan.h"
#include "internal/platform/implementation/shared/file.h"
@@ -183,6 +184,11 @@ ImplementationPlatform::CreateWifiHotspotMedium() {
return std::make_unique<g3::WifiHotspotMedium>();
}
std::unique_ptr<WifiDirectMedium>
ImplementationPlatform::CreateWifiDirectMedium() {
return std::make_unique<g3::WifiDirectMedium>();
}
#ifndef NO_WEBRTC
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
if (MediumEnvironment::Instance().GetEnvironmentConfig().webrtc_enabled) {
@@ -0,0 +1,64 @@
// 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/g3/wifi_direct.h"
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/str_format.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/g3/wifi_hotspot.h"
#include "internal/platform/implementation/wifi_direct.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/logging.h"
namespace location {
namespace nearby {
namespace g3 {
bool WifiDirectMedium::StartWifiDirect(
HotspotCredentials* wifi_direct_credentials) {
return true;
}
bool WifiDirectMedium::StopWifiDirect() {
absl::MutexLock lock(&mutex_);
return true;
}
bool WifiDirectMedium::ConnectWifiDirect(
HotspotCredentials* wifi_direct_credentials) {
return true;
}
bool WifiDirectMedium::DisconnectWifiDirect() { return true; }
std::unique_ptr<api::WifiHotspotSocket> WifiDirectMedium::ConnectToService(
absl::string_view ip_address, int port,
CancellationFlag* cancellation_flag) {
return nullptr;
}
std::unique_ptr<api::WifiHotspotServerSocket>
WifiDirectMedium::ListenForService(int port) {
return nullptr;
}
} // namespace g3
} // namespace nearby
} // namespace location
@@ -0,0 +1,88 @@
// 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_IMPL_G3_WIFI_DIRECT_H_
#define PLATFORM_IMPL_G3_WIFI_DIRECT_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/synchronization/mutex.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/pipe.h"
#include "internal/platform/implementation/g3/wifi_hotspot.h"
#include "internal/platform/implementation/wifi_direct.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
namespace location {
namespace nearby {
namespace g3 {
class WifiDirectMedium;
// Container of operations that can be performed over the WifiDirect medium.
class WifiDirectMedium : public api::WifiDirectMedium {
public:
WifiDirectMedium() = default;
~WifiDirectMedium() override = default;
WifiDirectMedium(const WifiDirectMedium&) = delete;
WifiDirectMedium(WifiDirectMedium&&) = delete;
WifiDirectMedium& operator=(const WifiDirectMedium&) = delete;
WifiDirectMedium& operator=(WifiDirectMedium&&) = delete;
// If the WiFi Adaptor supports to start a WifiDirect interface.
bool IsInterfaceValid() const override { return true; }
// Discoverer connects to server socket
std::unique_ptr<api::WifiHotspotSocket> ConnectToService(
absl::string_view ip_address, int port,
CancellationFlag* cancellation_flag) override;
// Advertiser starts to listen on server socket
std::unique_ptr<api::WifiHotspotServerSocket> ListenForService(
int port) override;
// Advertiser start WiFiDirect GO with specific Crendentials
bool StartWifiDirect(HotspotCredentials* wifi_direct_credentials) override;
// Advertiser stop the current WiFiDirect GO
bool StopWifiDirect() override;
// Discoverer connects to the WiFiDirect GO
bool ConnectWifiDirect(HotspotCredentials* wifi_direct_credentials) override;
// Discoverer disconnects from the WiFiDirect GO
bool DisconnectWifiDirect() override;
std::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
override {
return std::nullopt;
}
private:
absl::Mutex mutex_;
absl::flat_hash_map<std::string, WifiHotspotServerSocket*> server_sockets_
ABSL_GUARDED_BY(mutex_);
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_IMPL_G3_WIFI_DIRECT_H_
@@ -150,6 +150,10 @@ std::unique_ptr<WifiHotspotMedium> ImplementationPlatform::CreateWifiHotspotMedi
return nullptr;
}
std::unique_ptr<WifiDirectMedium> ImplementationPlatform::CreateWifiDirectMedium() {
return nullptr;
}
#ifndef NO_WEBRTC
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() { return nullptr; }
#endif
@@ -43,6 +43,7 @@
#include "internal/platform/implementation/webrtc.h"
#endif
#include "internal/platform/implementation/wifi.h"
#include "internal/platform/implementation/wifi_direct.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/os_name.h"
@@ -122,6 +123,7 @@ class ImplementationPlatform {
static std::unique_ptr<WifiMedium> CreateWifiMedium();
static std::unique_ptr<WifiLanMedium> CreateWifiLanMedium();
static std::unique_ptr<WifiHotspotMedium> CreateWifiHotspotMedium();
static std::unique_ptr<WifiDirectMedium> CreateWifiDirectMedium();
#ifndef NO_WEBRTC
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
#endif
@@ -0,0 +1,76 @@
// 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_API_WIFI_DIRECT_H_
#define PLATFORM_API_WIFI_DIRECT_H_
#include <memory>
#include <string>
#include <utility>
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/wifi_hotspot_credential.h"
namespace location {
namespace nearby {
namespace api {
// Container of operations that can be performed over the WifiLan medium.
class WifiDirectMedium {
public:
virtual ~WifiDirectMedium() = default;
// If the WiFi Adaptor supports to start a WifiDirect interface.
virtual bool IsInterfaceValid() const = 0;
// Connects to a WifiDirect service by port.
// On success, returns a new WifiDirectSocket.
// On error, returns nullptr.
virtual std::unique_ptr<WifiHotspotSocket> ConnectToService(
absl::string_view ip_address, int port,
CancellationFlag* cancellation_flag) = 0;
// Listens for incoming connection.
//
// port - A port number.
// 0 : use a random port.
// 1~65536 : open a server socket on that exact port.
// On success, returns a new WifiDirectServerSocket.
// On error, returns nullptr.
virtual std::unique_ptr<WifiHotspotServerSocket> ListenForService(
int port) = 0;
// Start a WifiDirect GO with platform dependent APIs and set the
// SSID/password pair back to the credentials. BWU module will retrieve these
// credentials and send to the client device through established channel and
// then client may connect to this WifiDirect GO with these credentials.
virtual bool StartWifiDirect(HotspotCredentials* wifi_direct_credentials) = 0;
virtual bool StopWifiDirect() = 0;
// Client device connect to a softAP with specified credential.
virtual bool ConnectWifiDirect(
HotspotCredentials* wifi_direct_credentials) = 0;
virtual bool DisconnectWifiDirect() = 0;
// Returns the port range as a pair of min and max port.
virtual absl::optional<std::pair<std::int32_t, std::int32_t>>
GetDynamicPortRange() = 0;
};
} // namespace api
} // namespace nearby
} // namespace location
#endif // PLATFORM_API_WIFI_DIRECT_H_
@@ -29,6 +29,7 @@
#include <cstddef>
#include <fstream>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
@@ -406,6 +407,11 @@ ImplementationPlatform::CreateWifiHotspotMedium() {
return std::make_unique<windows::WifiHotspotMedium>();
}
std::unique_ptr<WifiDirectMedium>
ImplementationPlatform::CreateWifiDirectMedium() {
return nullptr;
}
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
return absl::make_unique<windows::WebRtcMedium>();
+32
View File
@@ -0,0 +1,32 @@
// 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/wifi_direct.h"
#include "internal/platform/wifi_hotspot.h"
namespace location {
namespace nearby {
WifiHotspotSocket WifiDirectMedium::ConnectToService(
absl::string_view ip_address, int port,
CancellationFlag* cancellation_flag) {
NEARBY_LOGS(INFO) << "WifiDirectMedium::ConnectToService: ip address="
<< ip_address << ", port=" << port;
return WifiHotspotSocket(
impl_->ConnectToService(ip_address, port, cancellation_flag));
}
} // namespace nearby
} // namespace location
+92
View File
@@ -0,0 +1,92 @@
// 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_DIRECT_H_
#define PLATFORM_PUBLIC_WIFI_DIRECT_H_
#include <memory>
#include <string>
#include <utility>
#include "internal/platform/implementation/platform.h"
#include "internal/platform/implementation/wifi_direct.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/mutex_lock.h"
#include "internal/platform/wifi_hotspot.h"
#include "internal/platform/wifi_hotspot_credential.h"
namespace location {
namespace nearby {
// Container of operations that can be performed over the WifiLan medium.
class WifiDirectMedium {
public:
using Platform = api::ImplementationPlatform;
WifiDirectMedium() : impl_(Platform::CreateWifiDirectMedium()) {}
~WifiDirectMedium() = default;
// Returns a new WifiDirectSocket by ip address and port.
// On Success, WifiDirectSocket::IsValid() returns true.
WifiHotspotSocket ConnectToService(absl::string_view ip_address, int port,
CancellationFlag* cancellation_flag);
// Returns a new WifiDirectServerSocket.
// On Success, WifiDirectServerSocket::IsValid() returns true.
WifiHotspotServerSocket ListenForService(int port = 0) {
return WifiHotspotServerSocket(impl_->ListenForService(port));
}
// Returns the port range as a pair of min and max port.
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange() {
return impl_->GetDynamicPortRange();
}
bool StartWifiDirect() {
MutexLock lock(&mutex_);
return impl_->StartWifiDirect(&wifi_direct_credentials_);
}
bool StopWifiDirect() { return impl_->StopWifiDirect(); }
bool ConnectWifiDirect(absl::string_view ssid, absl::string_view password) {
MutexLock lock(&mutex_);
wifi_direct_credentials_.SetSSID(std::string(ssid));
wifi_direct_credentials_.SetPassword(std::string(password));
return impl_->ConnectWifiDirect(&wifi_direct_credentials_);
}
bool DisconnectWifiDirect() { return impl_->DisconnectWifiDirect(); }
HotspotCredentials* GetCredential() { return &wifi_direct_credentials_; }
bool IsInterfaceValid() const {
CHECK(impl_);
return impl_->IsInterfaceValid();
}
bool IsValid() const { return impl_ != nullptr; }
api::WifiDirectMedium& GetImpl() {
CHECK(impl_);
return *impl_;
}
private:
Mutex mutex_;
std::unique_ptr<api::WifiDirectMedium> impl_;
HotspotCredentials wifi_direct_credentials_ ABSL_GUARDED_BY(mutex_);
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_PUBLIC_WIFI_DIRECT_H_
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2020 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/wifi_direct.h"
#include <memory>
#include <string>
#include "gtest/gtest.h"
#include "internal/platform/wifi_hotspot.h"
#include "internal/platform/wifi_hotspot_credential.h"
namespace location {
namespace nearby {
namespace {
constexpr absl::string_view kSsid = "Direct-357a2d8c";
constexpr absl::string_view kPassword = "b592f7d3";
constexpr absl::string_view kIp = "123.234.23.1";
constexpr const size_t kPort = 20;
class WifiDirectMediumTest : public ::testing::Test {};
TEST_F(WifiDirectMediumTest, ConstructorDestructorWorks) {
auto wifi_direct_a = WifiDirectMedium();
auto wifi_direct_b = WifiDirectMedium();
ASSERT_TRUE(wifi_direct_a.IsInterfaceValid());
ASSERT_TRUE(wifi_direct_b.IsInterfaceValid());
// Make sure we can create 2 distinct mediums.
EXPECT_NE(&wifi_direct_a.GetImpl(), &wifi_direct_b.GetImpl());
}
TEST_F(WifiDirectMediumTest, CanStartStopDirect) {
auto wifi_direct_a = WifiDirectMedium();
ASSERT_TRUE(wifi_direct_a.IsInterfaceValid());
EXPECT_TRUE(wifi_direct_a.StartWifiDirect());
EXPECT_EQ(wifi_direct_a.GetDynamicPortRange(), std::nullopt);
WifiHotspotServerSocket server_socket = wifi_direct_a.ListenForService();
EXPECT_FALSE(server_socket.IsValid());
EXPECT_TRUE(wifi_direct_a.StopWifiDirect());
}
TEST_F(WifiDirectMediumTest, CanConnectDisconnectDirect) {
auto wifi_direct_a = WifiDirectMedium();
ASSERT_TRUE(wifi_direct_a.IsInterfaceValid());
EXPECT_TRUE(wifi_direct_a.ConnectWifiDirect(kSsid, kPassword));
EXPECT_TRUE(wifi_direct_a.DisconnectWifiDirect());
}
TEST_F(WifiDirectMediumTest, CanConnectToService) {
auto wifi_direct = WifiDirectMedium();
CancellationFlag flag;
EXPECT_FALSE(wifi_direct.ConnectToService(kIp, kPort, &flag).IsValid());
}
} // namespace
} // namespace nearby
} // namespace location