From d7a1fb8c48b0f1c5c580c75fedf2d79aab0e8f50 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Mon, 9 Dec 2024 00:19:59 -0800 Subject: [PATCH] analytics: Update the returned code for CreateUpgradedEndpointChannel of BWUHanlder and its enherited class for NC mediums. PiperOrigin-RevId: 704171725 --- .../implementation/base_bwu_handler_test.cc | 22 ++++++++++---- .../implementation/bluetooth_bwu_handler.cc | 27 ++++++++++++----- .../implementation/bluetooth_bwu_handler.h | 19 ++++++++---- .../implementation/bluetooth_bwu_test.cc | 12 ++++++-- connections/implementation/bwu_handler.h | 17 ++++++----- connections/implementation/bwu_manager.cc | 6 ++-- connections/implementation/fake_bwu_handler.h | 12 ++++++-- .../implementation/webrtc_bwu_handler.cc | 13 ++++++-- .../implementation/webrtc_bwu_handler.h | 15 ++++++---- .../implementation/webrtc_bwu_handler_stub.cc | 9 ++++-- .../implementation/webrtc_bwu_handler_stub.h | 14 +++++---- .../implementation/wifi_direct_bwu_handler.cc | 19 +++++++----- .../implementation/wifi_direct_bwu_handler.h | 21 +++++++++---- .../implementation/wifi_direct_bwu_test.cc | 21 +++++++++++-- .../wifi_hotspot_bwu_handler.cc | 18 ++++++----- .../implementation/wifi_hotspot_bwu_handler.h | 22 ++++++++++---- .../implementation/wifi_hotspot_bwu_test.cc | 22 ++++++++++++-- .../implementation/wifi_lan_bwu_handler.cc | 30 +++++++++++++------ .../implementation/wifi_lan_bwu_handler.h | 22 ++++++++++---- 19 files changed, 250 insertions(+), 91 deletions(-) diff --git a/connections/implementation/base_bwu_handler_test.cc b/connections/implementation/base_bwu_handler_test.cc index 13fe331b..b87c8d5b 100644 --- a/connections/implementation/base_bwu_handler_test.cc +++ b/connections/implementation/base_bwu_handler_test.cc @@ -14,18 +14,27 @@ #include "connections/implementation/base_bwu_handler.h" +#include +#include #include +#include #include "gtest/gtest.h" #include "absl/strings/string_view.h" +#include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" #include "connections/implementation/service_id_constants.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { namespace { -// Because BaseBwuHandler is still an abstract class, we need to implement the -// pure virtual functions in order to test BaseBwuHandler's bookkeeping logic. +using ::location::nearby::proto::connections::OperationResultCode; + +// Because BaseBwuHandler is still an abstract class, we need to implement the +// pure virtual functions in order to test BaseBwuHandler's bookkeeping logic. class BwuHandlerImpl : public BaseBwuHandler { public: using Medium = ::location::nearby::proto::connections::Medium; @@ -34,8 +43,8 @@ class BwuHandlerImpl : public BaseBwuHandler { // for every method. struct InputData { ClientProxy* client = nullptr; - absl::optional service_id; - absl::optional endpoint_id; + std::optional service_id; + std::optional endpoint_id; }; BwuHandlerImpl() : BaseBwuHandler(nullptr) {} @@ -52,11 +61,12 @@ class BwuHandlerImpl : public BaseBwuHandler { private: // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( + ErrorOr> + CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) final { - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } Medium GetUpgradeMedium() const final { return Medium::UNKNOWN_MEDIUM; } void OnEndpointDisconnect(ClientProxy* client, diff --git a/connections/implementation/bluetooth_bwu_handler.cc b/connections/implementation/bluetooth_bwu_handler.cc index 74e72312..24a49e53 100644 --- a/connections/implementation/bluetooth_bwu_handler.cc +++ b/connections/implementation/bluetooth_bwu_handler.cc @@ -14,13 +14,21 @@ #include "connections/implementation/bluetooth_bwu_handler.h" +#include #include #include #include "absl/functional/bind_front.h" +#include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bluetooth_endpoint_channel.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/offline_frames.h" +#include "internal/platform/bluetooth_adapter.h" +#include "internal/platform/bluetooth_classic.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" #include "internal/platform/logging.h" // Manages the Bluetooth-specific methods needed to upgrade an {@link @@ -29,6 +37,11 @@ namespace nearby { namespace connections { +namespace { +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace + +// TODO(edwinwu): Add exact OperationResultCode for BluetoothBwuHandler. BluetoothBwuHandler::BluetoothBwuHandler( Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), @@ -37,7 +50,7 @@ BluetoothBwuHandler::BluetoothBwuHandler( // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over BT using this info. // Returns a channel ready to exchange data or nullptr on error. -std::unique_ptr +ErrorOr> BluetoothBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { @@ -47,7 +60,7 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel( !bluetooth_credentials.has_mac_address()) { NEARBY_LOGS(ERROR) << "BluetoothBwuHandler failed to parse UpgradePathInfo."; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } const std::string& service_name = bluetooth_credentials.service_name(); @@ -64,7 +77,7 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel( << "BluetoothBwuHandler failed to derive a valid Bluetooth device " "from the MAC address (" << mac_address << ") for endpoint " << endpoint_id; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } BluetoothSocket socket = bluetooth_medium_.Connect( @@ -74,7 +87,7 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel( << "BluetoothBwuHandler failed to connect to the Bluetooth device (" << service_name << ", " << mac_address << ") for endpoint " << endpoint_id << " and service ID " << service_id; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } NEARBY_VLOG(1) @@ -91,11 +104,11 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel( << service_name << ", " << mac_address << ") for endpoint " << endpoint_id << " and service ID " << service_id; socket.Close(); - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } client->SetBluetoothMacAddress(endpoint_id, mac_address); - return channel; + return {std::move(channel)}; } ByteArray BluetoothBwuHandler::HandleInitializeUpgradedMediumForEndpoint( @@ -146,7 +159,7 @@ void BluetoothBwuHandler::HandleRevertInitiatorStateForService( void BluetoothBwuHandler::OnIncomingBluetoothConnection( ClientProxy* client, const std::string& upgrade_service_id, BluetoothSocket socket) { - auto channel = absl::make_unique( + auto channel = std::make_unique( upgrade_service_id, /*channel_name=*/upgrade_service_id, socket); std::unique_ptr connection{ new IncomingSocketConnection{ diff --git a/connections/implementation/bluetooth_bwu_handler.h b/connections/implementation/bluetooth_bwu_handler.h index 6563eebf..46466ee2 100644 --- a/connections/implementation/bluetooth_bwu_handler.h +++ b/connections/implementation/bluetooth_bwu_handler.h @@ -15,14 +15,20 @@ #ifndef CORE_INTERNAL_BLUETOOTH_BWU_HANDLER_H_ #define CORE_INTERNAL_BLUETOOTH_BWU_HANDLER_H_ +#include #include +#include #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/bluetooth_classic.h" +#include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/mediums.h" -#include "connections/implementation/mediums/utils.h" +#include "connections/medium_selector.h" #include "internal/platform/bluetooth_classic.h" -#include "internal/platform/count_down_latch.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { @@ -51,10 +57,11 @@ class BluetoothBwuHandler : public BaseBwuHandler { }; // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; Medium GetUpgradeMedium() const final { return Medium::BLUETOOTH; } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {} diff --git a/connections/implementation/bluetooth_bwu_test.cc b/connections/implementation/bluetooth_bwu_test.cc index 6b0b9897..5e1631c5 100644 --- a/connections/implementation/bluetooth_bwu_test.cc +++ b/connections/implementation/bluetooth_bwu_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include "gtest/gtest.h" #include "absl/time/time.h" @@ -25,6 +26,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" +#include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" @@ -35,6 +37,7 @@ namespace connections { namespace { using ::location::nearby::connections::OfflineFrame; +using ::location::nearby::proto::connections::OperationResultCode; constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000); } // namespace @@ -101,17 +104,22 @@ TEST_F(BluetoothBwuTest, SoftAPBWUInit_STACreateEndpointChannel) { auto bwu_frame = upgrade_frame.result().v1().bandwidth_upgrade_negotiation(); - std::unique_ptr new_channel = + ErrorOr> result = handler_2->CreateUpgradedEndpointChannel(&client_2, /*service_id=*/"A", /*endpoint_id=*/"1", bwu_frame.upgrade_path_info()); if (!FeatureFlags::GetInstance().GetFlags().enable_cancellation_flag) { + ASSERT_TRUE(result.has_value()); + std::unique_ptr new_channel = std::move(result.value()); EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); EXPECT_EQ(new_channel->GetMedium(), location::nearby::proto::connections::Medium::BLUETOOTH); } else { + EXPECT_FALSE(result.has_value()); + EXPECT_TRUE(result.has_error()); + EXPECT_EQ(result.error().operation_result_code(), + OperationResultCode::DETAIL_UNKNOWN); accept_latch.CountDown(); - EXPECT_EQ(new_channel, nullptr); } EXPECT_FALSE(mediums_2.GetBluetoothClassic().GetMacAddress().empty()); handler_2->RevertResponderState(/*service_id=*/"A"); diff --git a/connections/implementation/bwu_handler.h b/connections/implementation/bwu_handler.h index de1ee7e0..0deb84bc 100644 --- a/connections/implementation/bwu_handler.h +++ b/connections/implementation/bwu_handler.h @@ -15,14 +15,15 @@ #ifndef CORE_INTERNAL_BWU_HANDLER_H_ #define CORE_INTERNAL_BWU_HANDLER_H_ -#include +#include #include #include "absl/functional/any_invocable.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/offline_frames.h" -#include "internal/platform/count_down_latch.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { @@ -81,14 +82,16 @@ class BwuHandler { // that hasn't already been done) using the UpgradePathInfo sent by the // Initiator, and returns a new EndpointChannel for the upgraded medium. // @BwuHandlerThread - virtual std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) = 0; + virtual ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) = 0; // Returns the upgrade medium of the BwuHandler. // @BwuHandlerThread - virtual Medium GetUpgradeMedium() const = 0; + virtual location::nearby::proto::connections::Medium GetUpgradeMedium() + const = 0; virtual void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) = 0; diff --git a/connections/implementation/bwu_manager.cc b/connections/implementation/bwu_manager.cc index 362f834a..f448b46f 100644 --- a/connections/implementation/bwu_manager.cc +++ b/connections/implementation/bwu_manager.cc @@ -45,6 +45,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/count_down_latch.h" +#include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" #include "internal/platform/implementation/system_clock.h" #include "internal/platform/logging.h" @@ -882,10 +883,10 @@ BwuManager::ProcessBwuPathAvailableEventInternal( service_id = old_channel->GetServiceId(); } - std::unique_ptr new_channel = + ErrorOr> result = handler->CreateUpgradedEndpointChannel(client, service_id, endpoint_id, upgrade_path_info); - if (!new_channel) { + if (result.has_error() || !result.has_value()) { NEARBY_LOGS(ERROR) << "BwuManager failed to create an endpoint " "channel to endpoint" << endpoint_id << ", aborting upgrade."; @@ -894,6 +895,7 @@ BwuManager::ProcessBwuPathAvailableEventInternal( location::nearby::proto::connections::SOCKET_CREATION); return nullptr; } + std::unique_ptr new_channel = std::move(result.value()); // Write the requisite BANDWIDTH_UPGRADE_NEGOTIATION.CLIENT_INTRODUCTION as // the first OfflineFrame on this new EndpointChannel. diff --git a/connections/implementation/fake_bwu_handler.h b/connections/implementation/fake_bwu_handler.h index 6f9aaa91..d87e99f2 100644 --- a/connections/implementation/fake_bwu_handler.h +++ b/connections/implementation/fake_bwu_handler.h @@ -15,6 +15,7 @@ #ifndef NEARBY_CONNECTIONS_IMPLEMENTATION_FAKE_BWU_HANDLER_H_ #define NEARBY_CONNECTIONS_IMPLEMENTATION_FAKE_BWU_HANDLER_H_ +#include #include #include #include @@ -22,9 +23,15 @@ #include #include "connections/implementation/base_bwu_handler.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/bwu_manager.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" #include "connections/implementation/fake_endpoint_channel.h" +#include "connections/implementation/offline_frames.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/exception.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { @@ -104,14 +111,15 @@ class FakeBwuHandler : public BaseBwuHandler { }; // BwuHandler: - std::unique_ptr CreateUpgradedEndpointChannel( + ErrorOr> + CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) final { create_calls_.push_back({.client = client, .service_id = service_id, .endpoint_id = endpoint_id}); - return std::make_unique(medium_, service_id); + return {std::make_unique(medium_, service_id)}; } Medium GetUpgradeMedium() const final { return medium_; } diff --git a/connections/implementation/webrtc_bwu_handler.cc b/connections/implementation/webrtc_bwu_handler.cc index 8d2b0652..3b470f23 100644 --- a/connections/implementation/webrtc_bwu_handler.cc +++ b/connections/implementation/webrtc_bwu_handler.cc @@ -32,13 +32,19 @@ #include "connections/implementation/offline_frames.h" #include "connections/implementation/webrtc_endpoint_channel.h" #include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" #include "internal/platform/logging.h" namespace nearby { namespace connections { + +namespace { using ::location::nearby::connections::LocationHint; using ::location::nearby::connections::LocationStandard; +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace +// TODO(edwinwu): Add exact OperationResultCode for WebrtcBwuHandler. WebrtcBwuHandler::WebrtcIncomingSocket::WebrtcIncomingSocket( const std::string& name, mediums::WebRtcSocketWrapper socket) : name_(name), socket_(socket) {} @@ -54,7 +60,7 @@ WebrtcBwuHandler::WebrtcBwuHandler( // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WebRTC using this info. -std::unique_ptr +ErrorOr> WebrtcBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { @@ -79,7 +85,7 @@ WebrtcBwuHandler::CreateUpgradedEndpointChannel( NEARBY_LOGS(ERROR) << "WebRtcBwuHandler failed to connect to remote peer (" << peer_id.GetId() << ") on endpoint " << endpoint_id << ", aborting upgrade."; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } NEARBY_LOGS(INFO) << "WebRtcBwuHandler successfully connected to remote " @@ -95,9 +101,10 @@ WebrtcBwuHandler::CreateUpgradedEndpointChannel( NEARBY_LOGS(ERROR) << "WebRtcBwuHandler failed to create new EndpointChannel for " "outgoing socket, aborting upgrade."; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } - return channel; + return {std::move(channel)}; } void WebrtcBwuHandler::HandleRevertInitiatorStateForService( diff --git a/connections/implementation/webrtc_bwu_handler.h b/connections/implementation/webrtc_bwu_handler.h index 97e6cf44..8ad821b8 100644 --- a/connections/implementation/webrtc_bwu_handler.h +++ b/connections/implementation/webrtc_bwu_handler.h @@ -19,6 +19,7 @@ #include #include +#include #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" @@ -29,6 +30,7 @@ #include "connections/implementation/mediums/webrtc_socket.h" #include "connections/medium_selector.h" #include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { @@ -56,11 +58,14 @@ class WebrtcBwuHandler : public BaseBwuHandler { }; // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; - Medium GetUpgradeMedium() const final { return Medium::WEB_RTC; } + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; + location::nearby::proto::connections::Medium GetUpgradeMedium() const final { + return Medium::WEB_RTC; + } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {} diff --git a/connections/implementation/webrtc_bwu_handler_stub.cc b/connections/implementation/webrtc_bwu_handler_stub.cc index 91f19c1f..4bbb292f 100644 --- a/connections/implementation/webrtc_bwu_handler_stub.cc +++ b/connections/implementation/webrtc_bwu_handler_stub.cc @@ -25,10 +25,15 @@ #include "connections/implementation/mediums/webrtc_peer_id_stub.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/webrtc_endpoint_channel.h" +#include "internal/platform/expected.h" namespace nearby { namespace connections { +namespace { +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace + WebrtcBwuHandler::WebrtcIncomingSocket::WebrtcIncomingSocket( const std::string& name, mediums::WebRtcSocketWrapper socket) : name_(name), socket_(socket) {} @@ -44,11 +49,11 @@ WebrtcBwuHandler::WebrtcBwuHandler( // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WebRTC using this info. -std::unique_ptr +ErrorOr> WebrtcBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } void WebrtcBwuHandler::HandleRevertInitiatorStateForService( diff --git a/connections/implementation/webrtc_bwu_handler_stub.h b/connections/implementation/webrtc_bwu_handler_stub.h index de0a9ff1..e3b0aebb 100644 --- a/connections/implementation/webrtc_bwu_handler_stub.h +++ b/connections/implementation/webrtc_bwu_handler_stub.h @@ -28,6 +28,7 @@ #else #include "connections/implementation/mediums/webrtc_socket.h" #endif +#include "internal/platform/expected.h" namespace nearby { namespace connections { @@ -55,11 +56,14 @@ class WebrtcBwuHandler : public BaseBwuHandler { }; // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; - Medium GetUpgradeMedium() const final { return Medium::WEB_RTC; } + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; + location::nearby::proto::connections::Medium GetUpgradeMedium() const final { + return Medium::WEB_RTC; + } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {} diff --git a/connections/implementation/wifi_direct_bwu_handler.cc b/connections/implementation/wifi_direct_bwu_handler.cc index 742aa002..e7e11157 100644 --- a/connections/implementation/wifi_direct_bwu_handler.cc +++ b/connections/implementation/wifi_direct_bwu_handler.cc @@ -15,7 +15,6 @@ #include "connections/implementation/wifi_direct_bwu_handler.h" #include -#include #include #include #include @@ -28,6 +27,7 @@ #include "connections/implementation/offline_frames.h" #include "connections/implementation/wifi_direct_endpoint_channel.h" #include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" #include "internal/platform/logging.h" #include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_direct.h" @@ -35,6 +35,11 @@ namespace nearby { namespace connections { +namespace { +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace + +// TODO(edwinwu): Add exact OperationResultCode for WifiDirectBwuHandler. WifiDirectBwuHandler::WifiDirectBwuHandler( Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), @@ -101,13 +106,13 @@ void WifiDirectBwuHandler::HandleRevertInitiatorStateForService( << "upgrade service ID " << upgrade_service_id; } -std::unique_ptr +ErrorOr> WifiDirectBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { if (!upgrade_path_info.has_wifi_direct_credentials()) { NEARBY_LOGS(INFO) << "No WifiDirect Credential"; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } const UpgradePathInfo::WifiDirectCredentials& upgrade_path_info_credentials = upgrade_path_info.wifi_direct_credentials(); @@ -123,7 +128,7 @@ WifiDirectBwuHandler::CreateUpgradedEndpointChannel( if (!wifi_direct_medium_.ConnectWifiDirect(ssid, password)) { NEARBY_LOGS(ERROR) << "Connect to WifiDiret GO failed"; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } WifiDirectSocket socket = wifi_direct_medium_.Connect( @@ -132,7 +137,7 @@ WifiDirectBwuHandler::CreateUpgradedEndpointChannel( NEARBY_LOGS(ERROR) << "WifiDirectBwuHandler failed to connect to the WifiDirect service(" << port << ") for endpoint " << endpoint_id; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } NEARBY_VLOG(1) @@ -140,8 +145,8 @@ WifiDirectBwuHandler::CreateUpgradedEndpointChannel( << port << ") while upgrading endpoint " << endpoint_id; // Create a new WifiDirectEndpointChannel. - return std::make_unique( - service_id, /*channel_name=*/service_id, socket); + return {std::make_unique( + service_id, /*channel_name=*/service_id, socket)}; } void WifiDirectBwuHandler::OnIncomingWifiDirectConnection( diff --git a/connections/implementation/wifi_direct_bwu_handler.h b/connections/implementation/wifi_direct_bwu_handler.h index e7f159ea..c189b87d 100644 --- a/connections/implementation/wifi_direct_bwu_handler.h +++ b/connections/implementation/wifi_direct_bwu_handler.h @@ -19,8 +19,16 @@ #include #include "connections/implementation/base_bwu_handler.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/mediums/wifi.h" +#include "connections/implementation/mediums/wifi_direct.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" +#include "internal/platform/wifi_direct.h" +#include "utility" namespace nearby { namespace connections { @@ -53,11 +61,14 @@ class WifiDirectBwuHandler : public BaseBwuHandler { // WFD protocol to established connection while WINRT follow the standard WFD // spec to achieve the connection. So return fail to stop the upgrade request // from phone side. - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; - Medium GetUpgradeMedium() const final { return Medium::WIFI_DIRECT; } + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; + location::nearby::proto::connections::Medium GetUpgradeMedium() const final { + return location::nearby::proto::connections::Medium::WIFI_DIRECT; + } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {} diff --git a/connections/implementation/wifi_direct_bwu_test.cc b/connections/implementation/wifi_direct_bwu_test.cc index b01d9998..04d95412 100644 --- a/connections/implementation/wifi_direct_bwu_test.cc +++ b/connections/implementation/wifi_direct_bwu_test.cc @@ -16,16 +16,28 @@ #include #include "gtest/gtest.h" +#include "absl/time/time.h" #include "connections/implementation/bwu_handler.h" +#include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/offline_frames.h" #include "connections/implementation/wifi_direct_bwu_handler.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/count_down_latch.h" +#include "internal/platform/exception.h" +#include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" +#include "internal/platform/single_thread_executor.h" namespace nearby { namespace connections { namespace { using ::location::nearby::connections::OfflineFrame; +using ::location::nearby::proto::connections::OperationResultCode; constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000); } // namespace @@ -97,17 +109,22 @@ TEST_F(WifiDirectTest, WFDGOBWUInit_GCCreateEndpointChannel) { auto bwu_frame = upgrade_frame.result().v1().bandwidth_upgrade_negotiation(); - std::unique_ptr new_channel = + ErrorOr> result = handler_2->CreateUpgradedEndpointChannel( &wifi_direct_gc, /*service_id=*/"A", /*endpoint_id=*/"1", bwu_frame.upgrade_path_info()); if (!FeatureFlags::GetInstance().GetFlags().enable_cancellation_flag) { + ASSERT_TRUE(result.has_value()); + std::unique_ptr new_channel = std::move(result.value()); EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); EXPECT_EQ(new_channel->GetMedium(), location::nearby::proto::connections::Medium::WIFI_DIRECT); } else { + EXPECT_FALSE(result.has_value()); + EXPECT_TRUE(result.has_error()); + EXPECT_EQ(result.error().operation_result_code(), + OperationResultCode::DETAIL_UNKNOWN); accept_latch.CountDown(); - EXPECT_EQ(new_channel, nullptr); } EXPECT_TRUE(mediums_2.GetWifiDirect().IsConnectedToGO()); handler_2->RevertResponderState(/*service_id=*/"A"); diff --git a/connections/implementation/wifi_hotspot_bwu_handler.cc b/connections/implementation/wifi_hotspot_bwu_handler.cc index 4e8b1247..43171578 100644 --- a/connections/implementation/wifi_hotspot_bwu_handler.cc +++ b/connections/implementation/wifi_hotspot_bwu_handler.cc @@ -15,7 +15,6 @@ #include "connections/implementation/wifi_hotspot_bwu_handler.h" #include -#include #include #include #include @@ -25,11 +24,11 @@ #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/mediums.h" -#include "connections/implementation/mediums/utils.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/wifi_hotspot_endpoint_channel.h" #include "connections/strategy.h" #include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" #include "internal/platform/logging.h" #include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_hotspot.h" @@ -37,6 +36,11 @@ namespace nearby { namespace connections { +namespace { +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace + +// TODO(edwinwu): Add exact OperationResultCode for WifiHotspotBwuHandler. WifiHotspotBwuHandler::WifiHotspotBwuHandler( Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), @@ -108,13 +112,13 @@ void WifiHotspotBwuHandler::HandleRevertInitiatorStateForService( // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WifiHotspot using this info. -std::unique_ptr +ErrorOr> WifiHotspotBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { if (!upgrade_path_info.has_wifi_hotspot_credentials()) { NEARBY_LOGS(INFO) << "No Hotspot Credential"; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } const UpgradePathInfo::WifiHotspotCredentials& upgrade_path_info_credentials = upgrade_path_info.wifi_hotspot_credentials(); @@ -131,7 +135,7 @@ WifiHotspotBwuHandler::CreateUpgradedEndpointChannel( if (!wifi_hotspot_medium_.ConnectWifiHotspot(ssid, password, frequency)) { NEARBY_LOGS(ERROR) << "Connect to Hotspot failed"; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } WifiHotspotSocket socket = wifi_hotspot_medium_.Connect( @@ -140,7 +144,7 @@ WifiHotspotBwuHandler::CreateUpgradedEndpointChannel( NEARBY_LOGS(ERROR) << "WifiHotspotBwuHandler failed to connect to the WifiHotspot service(" << gateway << ":" << port << ") for endpoint " << endpoint_id; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } NEARBY_VLOG(1) @@ -151,7 +155,7 @@ WifiHotspotBwuHandler::CreateUpgradedEndpointChannel( auto channel = std::make_unique( service_id, /*channel_name=*/service_id, socket); - return channel; + return {std::move(channel)}; } // Accept Connection Callback. diff --git a/connections/implementation/wifi_hotspot_bwu_handler.h b/connections/implementation/wifi_hotspot_bwu_handler.h index 3e4fd611..f19f708d 100644 --- a/connections/implementation/wifi_hotspot_bwu_handler.h +++ b/connections/implementation/wifi_hotspot_bwu_handler.h @@ -15,12 +15,19 @@ #ifndef CORE_INTERNAL_WIFI_HOTSPOT_BWU_HANDLER_H_ #define CORE_INTERNAL_WIFI_HOTSPOT_BWU_HANDLER_H_ +#include #include +#include #include "connections/implementation/base_bwu_handler.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" -#include "connections/implementation/endpoint_channel_manager.h" +#include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/mediums/wifi_hotspot.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" +#include "internal/platform/wifi_hotspot.h" namespace nearby { namespace connections { @@ -49,11 +56,14 @@ class WifiHotspotBwuHandler : public BaseBwuHandler { }; // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; - Medium GetUpgradeMedium() const final { return Medium::WIFI_HOTSPOT; } + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; + location::nearby::proto::connections::Medium GetUpgradeMedium() const final { + return location::nearby::proto::connections::Medium::WIFI_HOTSPOT; + } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {} diff --git a/connections/implementation/wifi_hotspot_bwu_test.cc b/connections/implementation/wifi_hotspot_bwu_test.cc index 091087c3..44a47333 100644 --- a/connections/implementation/wifi_hotspot_bwu_test.cc +++ b/connections/implementation/wifi_hotspot_bwu_test.cc @@ -12,19 +12,32 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include "gtest/gtest.h" +#include "absl/time/time.h" #include "connections/implementation/bwu_handler.h" +#include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/offline_frames.h" #include "connections/implementation/wifi_hotspot_bwu_handler.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/count_down_latch.h" +#include "internal/platform/exception.h" +#include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" +#include "internal/platform/logging.h" #include "internal/platform/medium_environment.h" +#include "internal/platform/single_thread_executor.h" namespace nearby { namespace connections { namespace { using ::location::nearby::connections::OfflineFrame; +using ::location::nearby::proto::connections::OperationResultCode; constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000); } // namespace @@ -91,17 +104,22 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) { auto bwu_frame = upgrade_frame.result().v1().bandwidth_upgrade_negotiation(); - std::unique_ptr new_channel = + ErrorOr> result = handler_2->CreateUpgradedEndpointChannel(&client_2, /*service_id=*/"A", /*endpoint_id=*/"1", bwu_frame.upgrade_path_info()); if (!FeatureFlags::GetInstance().GetFlags().enable_cancellation_flag) { + ASSERT_TRUE(result.has_value()); + std::unique_ptr new_channel = std::move(result.value()); EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); EXPECT_EQ(new_channel->GetMedium(), location::nearby::proto::connections::Medium::WIFI_HOTSPOT); } else { + EXPECT_FALSE(result.has_value()); + EXPECT_TRUE(result.has_error()); + EXPECT_EQ(result.error().operation_result_code(), + OperationResultCode::DETAIL_UNKNOWN); accept_latch.CountDown(); - EXPECT_EQ(new_channel, nullptr); } EXPECT_TRUE(mediums_2.GetWifiHotspot().IsConnectedToHotspot()); handler_2->RevertResponderState(/*service_id=*/"A"); diff --git a/connections/implementation/wifi_lan_bwu_handler.cc b/connections/implementation/wifi_lan_bwu_handler.cc index b1cb3650..04c751f6 100644 --- a/connections/implementation/wifi_lan_bwu_handler.cc +++ b/connections/implementation/wifi_lan_bwu_handler.cc @@ -14,13 +14,20 @@ #include "connections/implementation/wifi_lan_bwu_handler.h" +#include +#include #include #include #include "absl/functional/bind_front.h" +#include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/wifi_lan_endpoint_channel.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" #include "internal/platform/implementation/wifi_utils.h" #include "internal/platform/logging.h" #include "internal/platform/wifi_lan.h" @@ -28,6 +35,11 @@ namespace nearby { namespace connections { +namespace { +using ::location::nearby::proto::connections::OperationResultCode; +} // namespace + +// TODO(edwinwu): Add exact OperationResultCode for WifiLanBwuHandler. WifiLanBwuHandler::WifiLanBwuHandler( Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), @@ -35,19 +47,19 @@ WifiLanBwuHandler::WifiLanBwuHandler( // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WifiLan using this info. -std::unique_ptr +ErrorOr> WifiLanBwuHandler::CreateUpgradedEndpointChannel( ClientProxy* client, const std::string& service_id, const std::string& endpoint_id, const UpgradePathInfo& upgrade_path_info) { if (!upgrade_path_info.has_wifi_lan_socket()) { - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } const UpgradePathInfo::WifiLanSocket& upgrade_path_info_socket = upgrade_path_info.wifi_lan_socket(); if (!upgrade_path_info_socket.has_ip_address() || !upgrade_path_info_socket.has_wifi_port()) { NEARBY_LOGS(ERROR) << "WifiLanBwuHandler failed to parse UpgradePathInfo."; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } const std::string& ip_address = upgrade_path_info_socket.ip_address(); @@ -64,7 +76,7 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel( << "WifiLanBwuHandler failed to connect to the WifiLan service (" << WifiUtils::GetHumanReadableIpAddress(ip_address) << ":" << port << ") for endpoint " << endpoint_id; - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } NEARBY_VLOG(1) @@ -80,10 +92,10 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel( << "channel to the WifiLan service (" << ip_address << ":" << port << ") for endpoint " << endpoint_id; socket.Close(); - return nullptr; + return {Error(OperationResultCode::DETAIL_UNKNOWN)}; } - return channel; + return {std::move(channel)}; } // Called by BWU initiator. Set up WifiLan upgraded medium for this endpoint, @@ -143,12 +155,12 @@ void WifiLanBwuHandler::HandleRevertInitiatorStateForService( void WifiLanBwuHandler::OnIncomingWifiLanConnection( ClientProxy* client, const std::string& upgrade_service_id, WifiLanSocket socket) { - auto channel = absl::make_unique( + auto channel = std::make_unique( upgrade_service_id, /*channel_name=*/upgrade_service_id, socket); std::unique_ptr connection( new IncomingSocketConnection{ - .socket = absl::make_unique(upgrade_service_id, - socket), + .socket = std::make_unique(upgrade_service_id, + socket), .channel = std::move(channel), }); NotifyOnIncomingConnection(client, std::move(connection)); diff --git a/connections/implementation/wifi_lan_bwu_handler.h b/connections/implementation/wifi_lan_bwu_handler.h index aee1ae01..64bad310 100644 --- a/connections/implementation/wifi_lan_bwu_handler.h +++ b/connections/implementation/wifi_lan_bwu_handler.h @@ -15,12 +15,19 @@ #ifndef CORE_INTERNAL_WIFI_LAN_BWU_HANDLER_H_ #define CORE_INTERNAL_WIFI_LAN_BWU_HANDLER_H_ +#include #include +#include #include "connections/implementation/base_bwu_handler.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" -#include "connections/implementation/endpoint_channel_manager.h" +#include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/mediums/wifi_lan.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/expected.h" +#include "internal/platform/wifi_lan.h" namespace nearby { namespace connections { @@ -49,11 +56,14 @@ class WifiLanBwuHandler : public BaseBwuHandler { }; // BwuHandler implementation: - std::unique_ptr CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const UpgradePathInfo& upgrade_path_info) final; - Medium GetUpgradeMedium() const final { return Medium::WIFI_LAN; } + ErrorOr> + CreateUpgradedEndpointChannel(ClientProxy* client, + const std::string& service_id, + const std::string& endpoint_id, + const UpgradePathInfo& upgrade_path_info) final; + location::nearby::proto::connections::Medium GetUpgradeMedium() const final { + return location::nearby::proto::connections::Medium::WIFI_LAN; + } void OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) final {}