mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
analytics: Add exact OperationResultCode for Bluetooth - Connect/AttemptConnect
PiperOrigin-RevId: 704918371
This commit is contained in:
committed by
Copybara-Service
parent
6f63efa8db
commit
5ac9bb14f6
@@ -41,7 +41,6 @@ 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)),
|
||||
@@ -60,7 +59,8 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel(
|
||||
!bluetooth_credentials.has_mac_address()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "BluetoothBwuHandler failed to parse UpgradePathInfo.";
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
return {
|
||||
Error(OperationResultCode::CONNECTIVITY_BLUETOOTH_INVALID_CREDENTIAL)};
|
||||
}
|
||||
|
||||
const std::string& service_name = bluetooth_credentials.service_name();
|
||||
@@ -77,17 +77,18 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel(
|
||||
<< "BluetoothBwuHandler failed to derive a valid Bluetooth device "
|
||||
"from the MAC address ("
|
||||
<< mac_address << ") for endpoint " << endpoint_id;
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
return {Error(
|
||||
OperationResultCode::CONNECTIVITY_BLUETOOTH_DEVICE_OBTAIN_FAILURE)};
|
||||
}
|
||||
|
||||
BluetoothSocket socket = bluetooth_medium_.Connect(
|
||||
ErrorOr<BluetoothSocket> socket_result = bluetooth_medium_.Connect(
|
||||
device, service_id, client->GetCancellationFlag(endpoint_id));
|
||||
if (!socket.IsValid()) {
|
||||
if (socket_result.has_error()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "BluetoothBwuHandler failed to connect to the Bluetooth device ("
|
||||
<< service_name << ", " << mac_address << ") for endpoint "
|
||||
<< endpoint_id << " and service ID " << service_id;
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
return {Error(socket_result.error().operation_result_code().value())};
|
||||
}
|
||||
|
||||
NEARBY_VLOG(1)
|
||||
@@ -96,15 +97,16 @@ BluetoothBwuHandler::CreateUpgradedEndpointChannel(
|
||||
<< endpoint_id;
|
||||
|
||||
auto channel = std::make_unique<BluetoothEndpointChannel>(
|
||||
service_id, /*channel_name=*/service_id, socket);
|
||||
service_id, /*channel_name=*/service_id, socket_result.value());
|
||||
if (channel == nullptr) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "BluetoothBwuHandler failed to create Bluetooth endpoint "
|
||||
"channel to the Bluetooth device ("
|
||||
<< service_name << ", " << mac_address << ") for endpoint "
|
||||
<< endpoint_id << " and service ID " << service_id;
|
||||
socket.Close();
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
socket_result.value().Close();
|
||||
return {Error(
|
||||
OperationResultCode::NEARBY_BT_ENDPOINT_CHANNEL_CREATION_FAILURE)};
|
||||
}
|
||||
|
||||
client->SetBluetoothMacAddress(endpoint_id, mac_address);
|
||||
|
||||
@@ -117,8 +117,9 @@ TEST_F(BluetoothBwuTest, SoftAPBWUInit_STACreateEndpointChannel) {
|
||||
} else {
|
||||
EXPECT_FALSE(result.has_value());
|
||||
EXPECT_TRUE(result.has_error());
|
||||
EXPECT_EQ(result.error().operation_result_code(),
|
||||
OperationResultCode::DETAIL_UNKNOWN);
|
||||
EXPECT_EQ(
|
||||
result.error().operation_result_code(),
|
||||
OperationResultCode::CONNECTIVITY_BLUETOOTH_DEVICE_OBTAIN_FAILURE);
|
||||
accept_latch.CountDown();
|
||||
}
|
||||
EXPECT_FALSE(mediums_2.GetBluetoothClassic().GetMacAddress().empty());
|
||||
|
||||
@@ -889,8 +889,6 @@ BwuManager::ProcessBwuPathAvailableEventInternal(
|
||||
// pointer in scope longer than necessary.
|
||||
std::string service_id;
|
||||
{
|
||||
// TBD(edwinwu): Change return type to ErrorOr<> for GetChannelForEndpoint
|
||||
// if possible.
|
||||
std::shared_ptr<EndpointChannel> old_channel =
|
||||
channel_manager_->GetChannelForEndpoint(endpoint_id);
|
||||
if (!old_channel) {
|
||||
@@ -899,8 +897,8 @@ BwuManager::ProcessBwuPathAvailableEventInternal(
|
||||
<< endpoint_id << " medium "
|
||||
<< location::nearby::proto::connections::Medium_Name(medium)
|
||||
<< ". Old endpoint channel is missing.";
|
||||
// TBD(edwinwu): Add a new operation result code for this.
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
return {
|
||||
Error(OperationResultCode::NEARBY_GENERIC_OLD_ENDPOINT_CHANNEL_NULL)};
|
||||
}
|
||||
service_id = old_channel->GetServiceId();
|
||||
}
|
||||
|
||||
@@ -124,8 +124,8 @@ ErrorOr<bool> BluetoothClassic::TurnOnDiscoverability(
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
LOG(INFO) << "Can't turn on BT discoverability. BT is not available.";
|
||||
return {Error(
|
||||
OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
|
||||
return {
|
||||
Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
|
||||
}
|
||||
|
||||
if (IsDiscoverable()) {
|
||||
@@ -252,8 +252,7 @@ ErrorOr<bool> BluetoothClassic::StartDiscovery(
|
||||
LOG(INFO) << "Refusing to start discovery of BT devices because another "
|
||||
"discovery is already in-progress for service_id="
|
||||
<< serviceId;
|
||||
return {Error(
|
||||
OperationResultCode::CLIENT_BLUETOOTH_DUPLICATE_DISCOVERING)};
|
||||
return {Error(OperationResultCode::CLIENT_BLUETOOTH_DUPLICATE_DISCOVERING)};
|
||||
}
|
||||
|
||||
if (!HasDiscoveryCallbacks()) {
|
||||
@@ -292,8 +291,7 @@ ErrorOr<bool> BluetoothClassic::StartDiscovery(
|
||||
if (!medium_->StartDiscovery(std::move(medium_callback))) {
|
||||
LOG(INFO) << "Failed to start discovery of BT devices.";
|
||||
RemoveDiscoveryCallback(serviceId);
|
||||
return {
|
||||
Error(OperationResultCode::CONNECTIVITY_BLUETOOTH_SCAN_FAILURE)};
|
||||
return {Error(OperationResultCode::CONNECTIVITY_BLUETOOTH_SCAN_FAILURE)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -505,9 +503,9 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
ErrorOr<BluetoothSocket> BluetoothClassic::Connect(
|
||||
BluetoothDevice& bluetooth_device, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
if (is_multiplex_enabled_) {
|
||||
@@ -523,7 +521,8 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device,
|
||||
if (bluetooth_socket == nullptr) {
|
||||
LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id
|
||||
<< " with " << bluetooth_device.GetName();
|
||||
return BluetoothSocket{};
|
||||
return {Error(OperationResultCode::
|
||||
NEARBY_BT_VIRTUAL_SOCKET_CREATION_FAILURE)};
|
||||
}
|
||||
return *bluetooth_socket;
|
||||
}
|
||||
@@ -537,26 +536,30 @@ BluetoothSocket BluetoothClassic::Connect(BluetoothDevice& bluetooth_device,
|
||||
LOG(WARNING) << "Attempt #"
|
||||
<< service_id_to_connect_attempts_count_map_[service_id]
|
||||
<< ": Cannot start creating client BT socket due to cancel.";
|
||||
return BluetoothSocket{};
|
||||
return {Error(OperationResultCode::
|
||||
CLIENT_CANCELLATION_CANCEL_BT_OUTGOING_CONNECTION)};
|
||||
}
|
||||
|
||||
auto wrapper_result =
|
||||
ErrorOr<BluetoothSocket> wrapper_result =
|
||||
AttemptToConnect(bluetooth_device, service_id, cancellation_flag);
|
||||
LOG(INFO) << "Attempt #"
|
||||
<< service_id_to_connect_attempts_count_map_[service_id]
|
||||
<< " to connect: " << wrapper_result.IsValid();
|
||||
if (wrapper_result.IsValid()) {
|
||||
return wrapper_result;
|
||||
<< " to connect: "
|
||||
<< (wrapper_result.has_value() ? wrapper_result.value().IsValid()
|
||||
: false);
|
||||
if (wrapper_result.has_value() && wrapper_result.value().IsValid()) {
|
||||
return std::move(wrapper_result.value());
|
||||
}
|
||||
|
||||
service_id_to_connect_attempts_count_map_[service_id]++;
|
||||
}
|
||||
|
||||
LOG(WARNING) << "Giving up after " << kConnectAttemptsLimit << " attempts";
|
||||
return BluetoothSocket{};
|
||||
return {Error(
|
||||
OperationResultCode::DEVICE_STATE_ERROR_UNFINISHED_UPGRADE_ATTEMPTS)};
|
||||
}
|
||||
|
||||
BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
ErrorOr<BluetoothSocket> BluetoothClassic::AttemptToConnect(
|
||||
BluetoothDevice& bluetooth_device, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -568,24 +571,27 @@ BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
if (service_id.empty()) {
|
||||
LOG(WARNING)
|
||||
<< "Refusing to create client BT socket because service_id is empty.";
|
||||
return socket;
|
||||
// TODO(edwinwu): Modify new OperationResultCode
|
||||
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
|
||||
}
|
||||
|
||||
if (!radio_.IsEnabled()) {
|
||||
LOG(WARNING) << "Can't create client BT socket [service=" << service_id
|
||||
<< "]: BT isn't enabled.";
|
||||
return socket;
|
||||
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
|
||||
}
|
||||
|
||||
if (!IsAvailableLocked()) {
|
||||
LOG(WARNING) << "Can't create client BT socket [service=" << service_id
|
||||
<< "]; BT isn't available.";
|
||||
return socket;
|
||||
return {
|
||||
Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
|
||||
}
|
||||
|
||||
if (!bluetooth_device.IsValid()) {
|
||||
LOG(WARNING) << "Bluetooth device is not valid.";
|
||||
return socket;
|
||||
return {Error(
|
||||
OperationResultCode::CONNECTIVITY_BLUETOOTH_DEVICE_OBTAIN_FAILURE)};
|
||||
}
|
||||
|
||||
socket = medium_->ConnectToService(
|
||||
@@ -596,7 +602,8 @@ BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
// CancellationFlagListener because the attempt logic is not asynchronous.
|
||||
if (!socket.IsValid() || cancellation_flag->Cancelled()) {
|
||||
LOG(INFO) << "Failed to Connect via BT [service=" << service_id << "]";
|
||||
return BluetoothSocket{};
|
||||
return {Error(
|
||||
OperationResultCode::CONNECTIVITY_BT_CLIENT_SOCKET_CREATION_FAILURE)};
|
||||
}
|
||||
|
||||
if (is_multiplex_enabled_) {
|
||||
@@ -612,7 +619,8 @@ BluetoothSocket BluetoothClassic::AttemptToConnect(
|
||||
if (bluetooth_socket == nullptr) {
|
||||
LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id
|
||||
<< " with " << bluetooth_device.GetName();
|
||||
return BluetoothSocket{};
|
||||
return {Error(
|
||||
OperationResultCode::NEARBY_BT_VIRTUAL_SOCKET_CREATION_FAILURE)};
|
||||
}
|
||||
LOG(INFO) << "Multiplex socket created for " << bluetooth_device.GetName();
|
||||
multiplex_sockets_.emplace(bluetooth_device.GetMacAddress(),
|
||||
|
||||
@@ -115,9 +115,9 @@ class BluetoothClassic {
|
||||
// Blocks until connection is established, or server-side is terminated.
|
||||
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
|
||||
// Called by client.
|
||||
BluetoothSocket Connect(BluetoothDevice& bluetooth_device,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag)
|
||||
ErrorOr<BluetoothSocket> Connect(BluetoothDevice& bluetooth_device,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
std::string GetMacAddress() const ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -186,9 +186,9 @@ class BluetoothClassic {
|
||||
// Blocks until connection is established, or server-side is terminated.
|
||||
// Returns socket instance. On success, BluetoothSocket.IsValid() return true.
|
||||
// Called by client.
|
||||
BluetoothSocket AttemptToConnect(BluetoothDevice& bluetooth_device,
|
||||
const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag);
|
||||
ErrorOr<BluetoothSocket> AttemptToConnect(
|
||||
BluetoothDevice& bluetooth_device, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag);
|
||||
|
||||
// Accesses to discovery callbacks.
|
||||
bool HasDiscoveryCallbacks() const
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -26,6 +25,7 @@
|
||||
#include "internal/platform/bluetooth_classic.h"
|
||||
#include "internal/platform/cancellation_flag.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"
|
||||
@@ -162,24 +162,24 @@ TEST_P(BluetoothClassicTest, CanNotConnect) {
|
||||
// Cannot connect to an empty service id.
|
||||
CancellationFlag flag;
|
||||
BluetoothDevice discovered_device;
|
||||
BluetoothSocket socket_for_client =
|
||||
ErrorOr<BluetoothSocket> socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, "", &flag);
|
||||
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_error());
|
||||
|
||||
// Cannot connect when radio is disabled.
|
||||
radio_for_client.Disable();
|
||||
socket_for_client =
|
||||
socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_error());
|
||||
radio_for_client.Enable();
|
||||
|
||||
// Cannot connect when adapter is disabled.
|
||||
radio_for_client.GetBluetoothAdapter().SetStatus(
|
||||
BluetoothAdapter::Status::kDisabled);
|
||||
socket_for_client =
|
||||
socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_error());
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CannotStartAcceptingConnections) {
|
||||
@@ -285,14 +285,15 @@ TEST_P(BluetoothClassicTest, CanConnect) {
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client =
|
||||
ErrorOr<BluetoothSocket> socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.value().GetRemoteDevice().IsValid());
|
||||
}
|
||||
|
||||
TEST_P(BluetoothClassicTest, CanCancelBeforeConnect) {
|
||||
@@ -335,21 +336,22 @@ TEST_P(BluetoothClassicTest, CanCancelBeforeConnect) {
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag(true);
|
||||
BluetoothSocket socket_for_client =
|
||||
ErrorOr<BluetoothSocket> socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.value().GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_error());
|
||||
|
||||
// Expect an invalid socket from stopping during the first attempt to
|
||||
// connect, because `Connect` returned immediately when it checked for
|
||||
@@ -401,21 +403,22 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect) {
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client =
|
||||
ErrorOr<BluetoothSocket> socket_for_client_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
if (!feature_flags.enable_cancellation_flag) {
|
||||
EXPECT_TRUE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(socket_for_server.IsValid());
|
||||
EXPECT_TRUE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_server.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.value().GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_FALSE(accept_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_FALSE(socket_for_server.IsValid());
|
||||
EXPECT_FALSE(socket_for_client.IsValid());
|
||||
EXPECT_TRUE(socket_for_client_result.has_error());
|
||||
|
||||
// Since the flag was cancelled during the initial `AttemptToConnect`,
|
||||
// except only one attempt instead of the usual three, because the
|
||||
@@ -468,7 +471,7 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
accept_latch.CountDown();
|
||||
}));
|
||||
CancellationFlag flag;
|
||||
BluetoothSocket socket_for_client1 =
|
||||
ErrorOr<BluetoothSocket> socket_for_client1_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId1), &flag);
|
||||
|
||||
// Simulate the flag being cancelled during connection attempt to a different
|
||||
@@ -485,7 +488,7 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
}));
|
||||
|
||||
CancellationFlag flag2;
|
||||
BluetoothSocket socket_for_client2 =
|
||||
ErrorOr<BluetoothSocket> socket_for_client2_result =
|
||||
bt_client.Connect(discovered_device, std::string(kServiceId2), &flag2);
|
||||
|
||||
// If FeatureFlag is disabled, Cancelled is false as no-op.
|
||||
@@ -495,17 +498,20 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) {
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId2)));
|
||||
EXPECT_TRUE(socket_for_server1.IsValid());
|
||||
EXPECT_TRUE(socket_for_server2.IsValid());
|
||||
EXPECT_TRUE(socket_for_client1.IsValid());
|
||||
EXPECT_TRUE(socket_for_client2.IsValid());
|
||||
EXPECT_TRUE(socket_for_client1_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client1_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_client2_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client2_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_server1.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_server2.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client1.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client2.GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client1_result.value().GetRemoteDevice().IsValid());
|
||||
EXPECT_TRUE(socket_for_client2_result.value().GetRemoteDevice().IsValid());
|
||||
} else {
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId1)));
|
||||
EXPECT_TRUE(bt_server.StopAcceptingConnections(std::string(kServiceId2)));
|
||||
EXPECT_TRUE(socket_for_client1.IsValid());
|
||||
EXPECT_FALSE(socket_for_client2.IsValid());
|
||||
EXPECT_TRUE(socket_for_client1_result.has_value());
|
||||
EXPECT_TRUE(socket_for_client1_result.value().IsValid());
|
||||
EXPECT_TRUE(socket_for_client2_result.has_error());
|
||||
|
||||
// Since the flag was cancelled during the initial `AttemptToConnect`,
|
||||
// except only one attempt instead of the usual three, because the
|
||||
|
||||
@@ -2163,22 +2163,23 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl(
|
||||
<< endpoint->endpoint_id << ") over Bluetooth Classic.";
|
||||
BluetoothDevice& device = endpoint->bluetooth_device;
|
||||
|
||||
BluetoothSocket bluetooth_socket = bluetooth_medium_.Connect(
|
||||
ErrorOr<BluetoothSocket> bluetooth_socket_result = bluetooth_medium_.Connect(
|
||||
device, endpoint->service_id,
|
||||
client->GetCancellationFlag(endpoint->endpoint_id));
|
||||
if (!bluetooth_socket.IsValid()) {
|
||||
if (bluetooth_socket_result.has_error()) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "In BluetoothConnectImpl(), failed to connect to Bluetooth device "
|
||||
<< device.GetName() << " for endpoint(id=" << endpoint->endpoint_id
|
||||
<< ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.status = {Status::kBluetoothError},
|
||||
};
|
||||
.operation_result_code =
|
||||
bluetooth_socket_result.error().operation_result_code().value()};
|
||||
}
|
||||
|
||||
auto channel = std::make_unique<BluetoothEndpointChannel>(
|
||||
endpoint->service_id, /*channel_name=*/endpoint->endpoint_id,
|
||||
bluetooth_socket);
|
||||
bluetooth_socket_result.value());
|
||||
NEARBY_VLOG(1) << "Client" << client->GetClientId()
|
||||
<< " created Bluetooth endpoint channel to endpoint(id="
|
||||
<< endpoint->endpoint_id << ").";
|
||||
@@ -2186,7 +2187,8 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl(
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.medium = BLUETOOTH,
|
||||
.status = {Status::kSuccess},
|
||||
.endpoint_channel = std::move(channel),
|
||||
.operation_result_code = OperationResultCode::DETAIL_SUCCESS,
|
||||
.endpoint_channel = std::move(channel)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2847,7 +2849,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
|
||||
<< endpoint->service_info.GetServiceName()
|
||||
<< " for endpoint(id=" << endpoint->endpoint_id << ").";
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.status = {Status::kWifiLanError},
|
||||
.status = {Status::kWifiLanError},
|
||||
};
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "In WifiLanConnectImpl(), connect to service "
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include "internal/platform/cancellation_flag_listener.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/implementation/system_clock.h"
|
||||
#include "internal/platform/logging.h"
|
||||
@@ -923,16 +924,17 @@ bool ReconnectManager::BluetoothImpl::ConnectOverMedium() {
|
||||
return false;
|
||||
}
|
||||
|
||||
bluetooth_socket_ =
|
||||
ErrorOr<BluetoothSocket> bluetooth_socket_result =
|
||||
bluetooth_medium.Connect(remote_bluetooth_device, reconnect_service_id_,
|
||||
client_->GetCancellationFlag(endpoint_id_));
|
||||
|
||||
if (!bluetooth_socket_.IsValid()) {
|
||||
if (bluetooth_socket_result.has_error()) {
|
||||
LOG(ERROR) << "Failed to reconnect to Bluetooth device "
|
||||
<< remote_bluetooth_device.GetName()
|
||||
<< " for endpoint(id=" << endpoint_id_ << ").";
|
||||
return false;
|
||||
}
|
||||
bluetooth_socket_ = std::move(bluetooth_socket_result.value());
|
||||
|
||||
reconnect_channel_ = std::make_unique<BluetoothEndpointChannel>(
|
||||
UnWrapInitiatorReconnectServiceId(reconnect_service_id_),
|
||||
|
||||
Reference in New Issue
Block a user