26
26
#include " internal/platform/bluetooth_adapter.h"
27
27
#include " internal/platform/bluetooth_classic.h"
28
28
#include " internal/platform/cancellation_flag.h"
29
+ #include " internal/platform/expected.h"
29
30
#include " internal/platform/logging.h"
30
31
#include " internal/platform/mutex_lock.h"
31
32
#include " internal/platform/socket.h"
@@ -36,6 +37,8 @@ namespace nearby {
36
37
namespace connections {
37
38
38
39
namespace {
40
+ using location::nearby::proto::connections::OperationResultCode;
41
+
39
42
std::string ScanModeToString (BluetoothAdapter::ScanMode mode) {
40
43
switch (mode) {
41
44
case BluetoothAdapter::ScanMode::kUnknown :
@@ -103,36 +106,42 @@ bool BluetoothClassic::IsAvailableLocked() const {
103
106
return medium_->IsValid () && adapter_.IsValid () && adapter_.IsEnabled ();
104
107
}
105
108
106
- bool BluetoothClassic::TurnOnDiscoverability (const std::string& device_name) {
109
+ ErrorOr<bool > BluetoothClassic::TurnOnDiscoverability (
110
+ const std::string& device_name) {
107
111
LOG (INFO) << " Turning on BT discoverability with device_name=" << device_name;
108
112
MutexLock lock (&mutex_);
109
113
110
114
if (device_name.empty ()) {
111
115
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)};
113
118
}
114
119
115
120
if (!radio_.IsEnabled ()) {
116
121
LOG (INFO) << " Can't turn on BT discoverability. BT is off." ;
117
- return false ;
122
+ return { Error (OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)} ;
118
123
}
119
124
120
125
if (!IsAvailableLocked ()) {
121
126
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)};
123
129
}
124
130
125
131
if (IsDiscoverable ()) {
126
132
LOG (INFO) << " Refusing to turn on BT discoverability; new name='"
127
133
<< device_name << " '; current name='" << adapter_.GetName ()
128
134
<< " '" ;
129
- return false ;
135
+ // TODO(edwinwu): Modify new OperationResultCode
136
+ return {Error (
137
+ OperationResultCode::CONNECTIVITY_BLUETOOTH_CHANGE_SCAN_MODE_FAILURE)};
130
138
}
131
139
132
140
if (!ModifyDeviceName (device_name)) {
133
141
LOG (INFO) << " Failed to turn on BT discoverability; failed to set name to "
134
142
<< device_name;
135
- return false ;
143
+ return {Error (OperationResultCode::
144
+ MISCELLEANEOUS_BLUETOOTH_CHANGE_DEVICE_NAME_FAILURE)};
136
145
}
137
146
138
147
if (!ModifyScanMode (ScanMode::kConnectableDiscoverable )) {
@@ -143,11 +152,12 @@ bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
143
152
// Don't forget to perform this rollback of the partial state changes we've
144
153
// made til now.
145
154
RestoreDeviceName ();
146
- return false ;
155
+ return {Error (
156
+ OperationResultCode::CONNECTIVITY_BLUETOOTH_CHANGE_SCAN_MODE_FAILURE)};
147
157
}
148
158
149
159
LOG (INFO) << " Turned on BT discoverability with device_name=" << device_name;
150
- return true ;
160
+ return { true } ;
151
161
}
152
162
153
163
bool BluetoothClassic::TurnOffDiscoverability () {
@@ -324,41 +334,45 @@ void BluetoothClassic::StopAllDiscovery() {
324
334
scan_info_.valid = false ;
325
335
}
326
336
327
- bool BluetoothClassic::StartAcceptingConnections (
337
+ ErrorOr< bool > BluetoothClassic::StartAcceptingConnections (
328
338
const std::string& service_id, AcceptedConnectionCallback callback) {
329
339
MutexLock lock (&mutex_);
330
340
331
341
if (service_id.empty ()) {
332
342
LOG (INFO)
333
343
<< " 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)};
335
346
}
336
347
337
348
if (!radio_.IsEnabled ()) {
338
349
LOG (INFO) << " Can't create BT server socket [service=" << service_id
339
350
<< " ]; BT is disabled." ;
340
- return false ;
351
+ return { Error (OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)} ;
341
352
}
342
353
343
354
if (!IsAvailableLocked ()) {
344
355
LOG (INFO) << " Can't start accepting BT connections [service=" << service_id
345
356
<< " ]; BT not available." ;
346
- return false ;
357
+ return {
358
+ Error (OperationResultCode::MEDIUM_UNAVAILABLE_BLUETOOTH_NOT_AVAILABLE)};
347
359
}
348
360
349
361
if (IsAcceptingConnectionsLocked (service_id)) {
350
362
LOG (INFO) << " Refusing to start accepting BT connections [service="
351
363
<< service_id
352
364
<< " ]; BT server is already in-progress with the same name." ;
353
- return false ;
365
+ return {Error (
366
+ OperationResultCode::CLIENT_DUPLICATE_ACCEPTING_BT_CONNECTION_REQUEST)};
354
367
}
355
368
356
369
BluetoothServerSocket socket =
357
370
medium_->ListenForService (service_id, GenerateUuidFromString (service_id));
358
371
if (!socket.IsValid ()) {
359
372
LOG (INFO) << " Failed to start accepting Bluetooth connections for "
360
373
<< service_id;
361
- return false ;
374
+ return {Error (
375
+ OperationResultCode::CONNECTIVITY_BT_SERVER_SOCKET_CREATION_FAILURE)};
362
376
}
363
377
364
378
// Mark the fact that there's an in-progress Bluetooth server accepting
@@ -428,7 +442,7 @@ bool BluetoothClassic::StartAcceptingConnections(
428
442
}
429
443
});
430
444
431
- return true ;
445
+ return { true } ;
432
446
}
433
447
434
448
bool BluetoothClassic::IsAcceptingConnections (const std::string& service_id) {
0 commit comments