Skip to content

Commit

Permalink
analytics: Modify the returned code with a new OperationResultCodeWit…
Browse files Browse the repository at this point in the history
…hMediums for Bluetooth - StartAdvertising/UpdateAdvertisingOptions, I

PiperOrigin-RevId: 704310956
  • Loading branch information
edwinwugoog authored and copybara-github committed Dec 9, 2024
1 parent 9a434f3 commit a08b420
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 126 deletions.
15 changes: 8 additions & 7 deletions connections/implementation/base_pcp_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1147,21 +1147,22 @@ void BasePcpHandler::StripOutUnavailableMediums(
}
}

ConnectionsLog::OperationResultWithMedium
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
BasePcpHandler::GetOperationResultWithMediumByResultCode(
ClientProxy* client, location::nearby::proto::connections::Medium medium,
int update_index,
location::nearby::proto::connections::OperationResultCode
operation_result_code,
location::nearby::proto::connections::ConnectionMode connection_mode) {
ConnectionsLog::OperationResultWithMedium operation_result_with_medium;
operation_result_with_medium.set_medium(medium);
operation_result_with_medium.set_result_code(operation_result_code);
operation_result_with_medium.set_result_category(
auto operation_result_with_medium =
std::make_unique<ConnectionsLog::OperationResultWithMedium>();
operation_result_with_medium->set_medium(medium);
operation_result_with_medium->set_result_code(operation_result_code);
operation_result_with_medium->set_result_category(
client->GetAnalyticsRecorder().GetOperationResultCateory(
operation_result_code));
operation_result_with_medium.set_connection_mode(connection_mode);
operation_result_with_medium.set_update_index(update_index);
operation_result_with_medium->set_connection_mode(connection_mode);
operation_result_with_medium->set_update_index(update_index);

return operation_result_with_medium;
}
Expand Down
3 changes: 2 additions & 1 deletion connections/implementation/base_pcp_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ class BasePcpHandler : public PcpHandler,

void StripOutWifiHotspotMedium(ConnectionInfo& connection_info);

location::nearby::analytics::proto::ConnectionsLog::OperationResultWithMedium
std::unique_ptr<location::nearby::analytics::proto::ConnectionsLog::
OperationResultWithMedium>
GetOperationResultWithMediumByResultCode(
ClientProxy* client, location::nearby::proto::connections::Medium medium,
int update_index,
Expand Down
44 changes: 29 additions & 15 deletions connections/implementation/mediums/bluetooth_classic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/expected.h"
#include "internal/platform/logging.h"
#include "internal/platform/mutex_lock.h"
#include "internal/platform/socket.h"
Expand All @@ -36,6 +37,8 @@ namespace nearby {
namespace connections {

namespace {
using location::nearby::proto::connections::OperationResultCode;

std::string ScanModeToString(BluetoothAdapter::ScanMode mode) {
switch (mode) {
case BluetoothAdapter::ScanMode::kUnknown:
Expand Down Expand Up @@ -103,36 +106,42 @@ bool BluetoothClassic::IsAvailableLocked() const {
return medium_->IsValid() && adapter_.IsValid() && adapter_.IsEnabled();
}

bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
ErrorOr<bool> BluetoothClassic::TurnOnDiscoverability(
const std::string& device_name) {
LOG(INFO) << "Turning on BT discoverability with device_name=" << device_name;
MutexLock lock(&mutex_);

if (device_name.empty()) {
LOG(INFO) << "Refusing to turn on BT discoverability. Empty device name.";
return false;
return {Error(
OperationResultCode::NEARBY_BLUETOOTH_ADVERTISE_TO_BYTES_FAILURE)};
}

if (!radio_.IsEnabled()) {
LOG(INFO) << "Can't turn on BT discoverability. BT is off.";
return false;
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
}

if (!IsAvailableLocked()) {
LOG(INFO) << "Can't turn on BT discoverability. BT is not available.";
return false;
return {Error(
OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
}

if (IsDiscoverable()) {
LOG(INFO) << "Refusing to turn on BT discoverability; new name='"
<< device_name << "'; current name='" << adapter_.GetName()
<< "'";
return false;
// TODO(edwinwu): Modify new OperationResultCode
return {Error(
OperationResultCode::CONNECTIVITY_BLUETOOTH_CHANGE_SCAN_MODE_FAILURE)};
}

if (!ModifyDeviceName(device_name)) {
LOG(INFO) << "Failed to turn on BT discoverability; failed to set name to "
<< device_name;
return false;
return {Error(OperationResultCode::
MISCELLEANEOUS_BLUETOOTH_CHANGE_DEVICE_NAME_FAILURE)};
}

if (!ModifyScanMode(ScanMode::kConnectableDiscoverable)) {
Expand All @@ -143,11 +152,12 @@ bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
// Don't forget to perform this rollback of the partial state changes we've
// made til now.
RestoreDeviceName();
return false;
return {Error(
OperationResultCode::CONNECTIVITY_BLUETOOTH_CHANGE_SCAN_MODE_FAILURE)};
}

LOG(INFO) << "Turned on BT discoverability with device_name=" << device_name;
return true;
return {true};
}

bool BluetoothClassic::TurnOffDiscoverability() {
Expand Down Expand Up @@ -324,41 +334,45 @@ void BluetoothClassic::StopAllDiscovery() {
scan_info_.valid = false;
}

bool BluetoothClassic::StartAcceptingConnections(
ErrorOr<bool> BluetoothClassic::StartAcceptingConnections(
const std::string& service_id, AcceptedConnectionCallback callback) {
MutexLock lock(&mutex_);

if (service_id.empty()) {
LOG(INFO)
<< "Refusing to start accepting BT connections; service ID is empty.";
return false;
// TODO(edwinwu): Modify new OperationResultCode
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
}

if (!radio_.IsEnabled()) {
LOG(INFO) << "Can't create BT server socket [service=" << service_id
<< "]; BT is disabled.";
return false;
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}

if (!IsAvailableLocked()) {
LOG(INFO) << "Can't start accepting BT connections [service=" << service_id
<< "]; BT not available.";
return false;
return {
Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
}

if (IsAcceptingConnectionsLocked(service_id)) {
LOG(INFO) << "Refusing to start accepting BT connections [service="
<< service_id
<< "]; BT server is already in-progress with the same name.";
return false;
return {Error(
OperationResultCode::CLIENT_DUPLICATE_ACCEPTING_BT_CONNECTION_REQUEST)};
}

BluetoothServerSocket socket =
medium_->ListenForService(service_id, GenerateUuidFromString(service_id));
if (!socket.IsValid()) {
LOG(INFO) << "Failed to start accepting Bluetooth connections for "
<< service_id;
return false;
return {Error(
OperationResultCode::CONNECTIVITY_BT_SERVER_SOCKET_CREATION_FAILURE)};
}

// Mark the fact that there's an in-progress Bluetooth server accepting
Expand Down Expand Up @@ -428,7 +442,7 @@ bool BluetoothClassic::StartAcceptingConnections(
}
});

return true;
return {true};
}

bool BluetoothClassic::IsAcceptingConnections(const std::string& service_id) {
Expand Down
7 changes: 4 additions & 3 deletions connections/implementation/mediums/bluetooth_classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/bluetooth_classic.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/expected.h"
#include "internal/platform/multi_thread_executor.h"
#include "internal/platform/mutex.h"

Expand All @@ -54,7 +55,7 @@ class BluetoothClassic {
// Returns true, if name and scan mode are successfully set, and false
// otherwise.
// Called by server.
bool TurnOnDiscoverability(const std::string& device_name)
ErrorOr<bool> TurnOnDiscoverability(const std::string& device_name)
ABSL_LOCKS_EXCLUDED(mutex_);

// Disables BT discoverability, and restores scan mode and device name to
Expand Down Expand Up @@ -83,8 +84,8 @@ class BluetoothClassic {
// Any connected sockets returned from Accept() are passed to a callback.
// Returns true, if server socket was successfully created, false otherwise.
// Called by server.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback)
ErrorOr<bool> StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_);

// Returns true, if object is currently running a Accept() loop.
Expand Down
Loading

0 comments on commit a08b420

Please sign in to comment.