Skip to content

Commit a08b420

Browse files
edwinwugoogcopybara-github
authored andcommitted
analytics: Modify the returned code with a new OperationResultCodeWithMediums for Bluetooth - StartAdvertising/UpdateAdvertisingOptions, I
PiperOrigin-RevId: 704310956
1 parent 9a434f3 commit a08b420

File tree

6 files changed

+215
-126
lines changed

6 files changed

+215
-126
lines changed

connections/implementation/base_pcp_handler.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,21 +1147,22 @@ void BasePcpHandler::StripOutUnavailableMediums(
11471147
}
11481148
}
11491149

1150-
ConnectionsLog::OperationResultWithMedium
1150+
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
11511151
BasePcpHandler::GetOperationResultWithMediumByResultCode(
11521152
ClientProxy* client, location::nearby::proto::connections::Medium medium,
11531153
int update_index,
11541154
location::nearby::proto::connections::OperationResultCode
11551155
operation_result_code,
11561156
location::nearby::proto::connections::ConnectionMode connection_mode) {
1157-
ConnectionsLog::OperationResultWithMedium operation_result_with_medium;
1158-
operation_result_with_medium.set_medium(medium);
1159-
operation_result_with_medium.set_result_code(operation_result_code);
1160-
operation_result_with_medium.set_result_category(
1157+
auto operation_result_with_medium =
1158+
std::make_unique<ConnectionsLog::OperationResultWithMedium>();
1159+
operation_result_with_medium->set_medium(medium);
1160+
operation_result_with_medium->set_result_code(operation_result_code);
1161+
operation_result_with_medium->set_result_category(
11611162
client->GetAnalyticsRecorder().GetOperationResultCateory(
11621163
operation_result_code));
1163-
operation_result_with_medium.set_connection_mode(connection_mode);
1164-
operation_result_with_medium.set_update_index(update_index);
1164+
operation_result_with_medium->set_connection_mode(connection_mode);
1165+
operation_result_with_medium->set_update_index(update_index);
11651166

11661167
return operation_result_with_medium;
11671168
}

connections/implementation/base_pcp_handler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ class BasePcpHandler : public PcpHandler,
418418

419419
void StripOutWifiHotspotMedium(ConnectionInfo& connection_info);
420420

421-
location::nearby::analytics::proto::ConnectionsLog::OperationResultWithMedium
421+
std::unique_ptr<location::nearby::analytics::proto::ConnectionsLog::
422+
OperationResultWithMedium>
422423
GetOperationResultWithMediumByResultCode(
423424
ClientProxy* client, location::nearby::proto::connections::Medium medium,
424425
int update_index,

connections/implementation/mediums/bluetooth_classic.cc

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "internal/platform/bluetooth_adapter.h"
2727
#include "internal/platform/bluetooth_classic.h"
2828
#include "internal/platform/cancellation_flag.h"
29+
#include "internal/platform/expected.h"
2930
#include "internal/platform/logging.h"
3031
#include "internal/platform/mutex_lock.h"
3132
#include "internal/platform/socket.h"
@@ -36,6 +37,8 @@ namespace nearby {
3637
namespace connections {
3738

3839
namespace {
40+
using location::nearby::proto::connections::OperationResultCode;
41+
3942
std::string ScanModeToString(BluetoothAdapter::ScanMode mode) {
4043
switch (mode) {
4144
case BluetoothAdapter::ScanMode::kUnknown:
@@ -103,36 +106,42 @@ bool BluetoothClassic::IsAvailableLocked() const {
103106
return medium_->IsValid() && adapter_.IsValid() && adapter_.IsEnabled();
104107
}
105108

106-
bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
109+
ErrorOr<bool> BluetoothClassic::TurnOnDiscoverability(
110+
const std::string& device_name) {
107111
LOG(INFO) << "Turning on BT discoverability with device_name=" << device_name;
108112
MutexLock lock(&mutex_);
109113

110114
if (device_name.empty()) {
111115
LOG(INFO) << "Refusing to turn on BT discoverability. Empty device name.";
112-
return false;
116+
return {Error(
117+
OperationResultCode::NEARBY_BLUETOOTH_ADVERTISE_TO_BYTES_FAILURE)};
113118
}
114119

115120
if (!radio_.IsEnabled()) {
116121
LOG(INFO) << "Can't turn on BT discoverability. BT is off.";
117-
return false;
122+
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
118123
}
119124

120125
if (!IsAvailableLocked()) {
121126
LOG(INFO) << "Can't turn on BT discoverability. BT is not available.";
122-
return false;
127+
return {Error(
128+
OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
123129
}
124130

125131
if (IsDiscoverable()) {
126132
LOG(INFO) << "Refusing to turn on BT discoverability; new name='"
127133
<< device_name << "'; current name='" << adapter_.GetName()
128134
<< "'";
129-
return false;
135+
// TODO(edwinwu): Modify new OperationResultCode
136+
return {Error(
137+
OperationResultCode::CONNECTIVITY_BLUETOOTH_CHANGE_SCAN_MODE_FAILURE)};
130138
}
131139

132140
if (!ModifyDeviceName(device_name)) {
133141
LOG(INFO) << "Failed to turn on BT discoverability; failed to set name to "
134142
<< device_name;
135-
return false;
143+
return {Error(OperationResultCode::
144+
MISCELLEANEOUS_BLUETOOTH_CHANGE_DEVICE_NAME_FAILURE)};
136145
}
137146

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

149159
LOG(INFO) << "Turned on BT discoverability with device_name=" << device_name;
150-
return true;
160+
return {true};
151161
}
152162

153163
bool BluetoothClassic::TurnOffDiscoverability() {
@@ -324,41 +334,45 @@ void BluetoothClassic::StopAllDiscovery() {
324334
scan_info_.valid = false;
325335
}
326336

327-
bool BluetoothClassic::StartAcceptingConnections(
337+
ErrorOr<bool> BluetoothClassic::StartAcceptingConnections(
328338
const std::string& service_id, AcceptedConnectionCallback callback) {
329339
MutexLock lock(&mutex_);
330340

331341
if (service_id.empty()) {
332342
LOG(INFO)
333343
<< "Refusing to start accepting BT connections; service ID is empty.";
334-
return false;
344+
// TODO(edwinwu): Modify new OperationResultCode
345+
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
335346
}
336347

337348
if (!radio_.IsEnabled()) {
338349
LOG(INFO) << "Can't create BT server socket [service=" << service_id
339350
<< "]; BT is disabled.";
340-
return false;
351+
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
341352
}
342353

343354
if (!IsAvailableLocked()) {
344355
LOG(INFO) << "Can't start accepting BT connections [service=" << service_id
345356
<< "]; BT not available.";
346-
return false;
357+
return {
358+
Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
347359
}
348360

349361
if (IsAcceptingConnectionsLocked(service_id)) {
350362
LOG(INFO) << "Refusing to start accepting BT connections [service="
351363
<< service_id
352364
<< "]; BT server is already in-progress with the same name.";
353-
return false;
365+
return {Error(
366+
OperationResultCode::CLIENT_DUPLICATE_ACCEPTING_BT_CONNECTION_REQUEST)};
354367
}
355368

356369
BluetoothServerSocket socket =
357370
medium_->ListenForService(service_id, GenerateUuidFromString(service_id));
358371
if (!socket.IsValid()) {
359372
LOG(INFO) << "Failed to start accepting Bluetooth connections for "
360373
<< service_id;
361-
return false;
374+
return {Error(
375+
OperationResultCode::CONNECTIVITY_BT_SERVER_SOCKET_CREATION_FAILURE)};
362376
}
363377

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

431-
return true;
445+
return {true};
432446
}
433447

434448
bool BluetoothClassic::IsAcceptingConnections(const std::string& service_id) {

connections/implementation/mediums/bluetooth_classic.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "internal/platform/bluetooth_adapter.h"
3030
#include "internal/platform/bluetooth_classic.h"
3131
#include "internal/platform/cancellation_flag.h"
32+
#include "internal/platform/expected.h"
3233
#include "internal/platform/multi_thread_executor.h"
3334
#include "internal/platform/mutex.h"
3435

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

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

9091
// Returns true, if object is currently running a Accept() loop.

0 commit comments

Comments
 (0)