analytics: Modify the returned code with a new OperationResultCodeWithMediums for Ble - advertising/discovery/acceptConnections

PiperOrigin-RevId: 704911325
This commit is contained in:
Edwin Wu
2024-12-10 18:08:28 -08:00
committed by Copybara-Service
parent 1a35a5c890
commit 6f63efa8db
4 changed files with 270 additions and 166 deletions
+42 -36
View File
@@ -44,6 +44,7 @@
#include "internal/platform/byte_array.h"
#include "internal/platform/cancelable_alarm.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/expected.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/logging.h"
@@ -56,7 +57,7 @@ namespace nearby {
namespace connections {
namespace {
using ::location::nearby::proto::connections::OperationResultCode;
using ::nearby::api::ble_v2::BleAdvertisementData;
using ::nearby::api::ble_v2::GattCharacteristic;
using ::nearby::api::ble_v2::TxPowerLevel;
@@ -109,16 +110,16 @@ bool BleV2::IsAvailable() const {
return IsAvailableLocked();
}
bool BleV2::StartAdvertising(const std::string& service_id,
const ByteArray& advertisement_bytes,
PowerLevel power_level,
bool is_fast_advertisement) {
ErrorOr<bool> BleV2::StartAdvertising(const std::string& service_id,
const ByteArray& advertisement_bytes,
PowerLevel power_level,
bool is_fast_advertisement) {
MutexLock lock(&mutex_);
if (advertisement_bytes.Empty()) {
LOG(INFO)
<< "Refusing to turn on BLE advertising. Empty advertisement data.";
return false;
return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)};
}
if (advertisement_bytes.size() > kMaxAdvertisementLength) {
@@ -126,23 +127,23 @@ bool BleV2::StartAdvertising(const std::string& service_id,
"advertisement was too long. Expected at most "
<< kMaxAdvertisementLength << " bytes but received "
<< advertisement_bytes.size() << " bytes.";
return false;
return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)};
}
if (IsAdvertisingLocked(service_id)) {
LOG(INFO) << "Failed to BLE advertise because we're already advertising.";
return false;
return {Error(OperationResultCode::CLIENT_BLE_DUPLICATE_DISCOVERING)};
}
if (!radio_.IsEnabled()) {
LOG(INFO)
<< "Can't start BLE advertising because Bluetooth was never turned on";
return false;
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
}
if (!IsAvailableLocked()) {
LOG(INFO) << "Can't turn on BLE advertising. BLE is not available.";
return false;
return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)};
}
// Wrap the connections advertisement to the medium advertisement.
@@ -161,7 +162,7 @@ bool BleV2::StartAdvertising(const std::string& service_id,
if (!medium_advertisement.IsValid()) {
LOG(INFO) << "Failed to BLE advertise because we could not wrap a "
"connection advertisement to medium advertisement.";
return false;
return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)};
}
advertising_infos_.insert(
@@ -180,9 +181,10 @@ bool BleV2::StartAdvertising(const std::string& service_id,
if (!StartAdvertisingLocked(service_id)) {
advertising_infos_.erase(service_id);
return false;
return {
Error(OperationResultCode::CONNECTIVITY_BLE_START_ADVERTISING_FAILURE)};
}
return true;
return {true};
}
bool BleV2::StopAdvertising(const std::string& service_id) {
@@ -257,7 +259,7 @@ bool BleV2::IsAdvertisingForLegacyDevice(const std::string& service_id) const {
return IsAdvertisingForLegacyDeviceLocked(service_id);
}
bool BleV2::StartLegacyAdvertising(
ErrorOr<bool> BleV2::StartLegacyAdvertising(
const std::string& input_service_id, const std::string& local_endpoint_id,
const std::string& fast_advertisement_service_uuid) {
LOG(INFO) << "StartLegacyAdvertising: " << input_service_id
@@ -267,12 +269,12 @@ bool BleV2::StartLegacyAdvertising(
if (!radio_.IsEnabled()) {
LOG(INFO) << "Can't start BLE v2 legacy advertising because "
"Bluetooth was never turned on";
return false;
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
}
if (!IsAvailableLocked()) {
LOG(INFO)
<< "Can't turn on BLE v2 legacy advertising. BLE is not available.";
return false;
return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)};
}
if (medium_.IsExtendedAdvertisementsAvailable()) {
LOG(INFO) << "Skip dummy advertising for non legacy device";
@@ -282,7 +284,7 @@ bool BleV2::StartLegacyAdvertising(
if (service_ids_to_advertising_sessions_.find(service_id) !=
service_ids_to_advertising_sessions_.end()) {
LOG(INFO) << "Already started legacy device advertising for " << service_id;
return false;
return {Error(OperationResultCode::CLIENT_BLE_DUPLICATE_ADVERTISING)};
}
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession>
@@ -310,12 +312,13 @@ bool BleV2::StartLegacyAdvertising(
LOG(ERROR) << "Failed to turn on BLE v2 advertising for legacy "
"device for service ID "
<< service_id;
return false;
return {
Error(OperationResultCode::CONNECTIVITY_BLE_START_ADVERTISING_FAILURE)};
}
service_ids_to_advertising_sessions_.insert(
{std::string(service_id), std::move(legacy_device_advertizing_session)});
return true;
return {true};
}
bool BleV2::StopLegacyAdvertising(const std::string& input_service_id) {
@@ -343,29 +346,30 @@ bool BleV2::StopLegacyAdvertising(const std::string& input_service_id) {
return status.ok();
}
bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
DiscoveredPeripheralCallback callback) {
ErrorOr<bool> BleV2::StartScanning(const std::string& service_id,
PowerLevel power_level,
DiscoveredPeripheralCallback callback) {
MutexLock lock(&mutex_);
if (service_id.empty()) {
LOG(INFO) << "Can not start BLE scanning with empty service id.";
return false;
return {Error(OperationResultCode::DETAIL_UNKNOWN)};
}
if (IsScanningLocked(service_id)) {
LOG(INFO) << "Cannot start scan of BLE peripherals because "
"scanning is already in-progress.";
return false;
return {Error(OperationResultCode::CLIENT_BLE_DUPLICATE_DISCOVERING)};
}
if (!radio_.IsEnabled()) {
LOG(INFO) << "Can't start BLE scanning because Bluetooth is disabled";
return false;
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
}
if (!IsAvailableLocked()) {
LOG(INFO) << "Can't scan BLE peripherals because BLE isn't available.";
return false;
return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)};
}
// Start to track the advertisement found for specific `service_id`.
@@ -383,7 +387,7 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
scanned_service_ids_.insert(service_id);
LOG(INFO) << "Turned on BLE scanning with service id=" << service_id
<< " without start client scanning";
return true;
return {true};
}
scanned_service_ids_.insert(service_id);
@@ -426,7 +430,7 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
discovered_peripheral_tracker_.StopTracking(service_id);
// Erase the service id that is just added.
scanned_service_ids_.erase(service_id);
return false;
return {Error(OperationResultCode::CONNECTIVITY_BLE_SCAN_FAILURE)};
}
absl::Duration peripheral_lost_timeout =
@@ -443,7 +447,7 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
peripheral_lost_timeout, &alarm_executor_, /*is_recurring=*/true);
LOG(INFO) << "Turned on BLE scanning with service id=" << service_id;
return true;
return {true};
}
bool BleV2::StopScanning(const std::string& service_id) {
@@ -482,40 +486,42 @@ bool BleV2::IsScanning(const std::string& service_id) const {
return IsScanningLocked(service_id);
}
bool BleV2::StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback) {
ErrorOr<bool> BleV2::StartAcceptingConnections(
const std::string& service_id, AcceptedConnectionCallback callback) {
MutexLock lock(&mutex_);
if (service_id.empty()) {
LOG(INFO)
<< "Refusing to start accepting BLE connections with empty service id.";
return false;
return {Error(OperationResultCode::CLIENT_BLE_DUPLICATE_DISCOVERING)};
}
if (IsAcceptingConnectionsLocked(service_id)) {
LOG(INFO)
<< "Refusing to start accepting BLE connections for " << service_id
<< " because another BLE peripheral socket is already in-progress.";
return false;
return {Error(OperationResultCode::
CLIENT_DUPLICATE_ACCEPTING_BLE_CONNECTION_REQUEST)};
}
if (!radio_.IsEnabled()) {
LOG(INFO) << "Can't start accepting BLE connections for " << service_id
<< " because Bluetooth isn't enabled.";
return false;
return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)};
}
if (!IsAvailableLocked()) {
LOG(INFO) << "Can't start accepting BLE connections for " << service_id
<< " because BLE isn't available.";
return false;
return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)};
}
BleV2ServerSocket server_socket = medium_.OpenServerSocket(service_id);
if (!server_socket.IsValid()) {
LOG(INFO) << "Failed to start accepting Ble connections for service_id="
<< service_id;
return false;
return {Error(
OperationResultCode::CONNECTIVITY_BLE_SERVER_SOCKET_CREATION_FAILURE)};
}
// Mark the fact that there's an in-progress Ble server accepting
@@ -552,7 +558,7 @@ bool BleV2::StartAcceptingConnections(const std::string& service_id,
}
});
return true;
return {true};
}
bool BleV2::StopAcceptingConnections(const std::string& service_id) {
+11 -8
View File
@@ -38,6 +38,7 @@
#include "internal/platform/byte_array.h"
#include "internal/platform/cancelable_alarm.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/expected.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/multi_thread_executor.h"
#include "internal/platform/mutex.h"
@@ -74,9 +75,10 @@ class BleV2 final {
// power_level - The power level to use for the advertisement.
// is_fast_advertisement - True to use fast advertisements, which are smaller
// but much more efficient to discover.
bool StartAdvertising(const std::string& service_id,
const ByteArray& advertisement_bytes,
PowerLevel power_level, bool is_fast_advertisement)
ErrorOr<bool> StartAdvertising(const std::string& service_id,
const ByteArray& advertisement_bytes,
PowerLevel power_level,
bool is_fast_advertisement)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables BLE advertising.
@@ -91,7 +93,7 @@ class BleV2 final {
// Use dummy bytes to do ble advertising, only for legacy devices.
// Returns true, if data is successfully set, and false otherwise.
bool StartLegacyAdvertising(
ErrorOr<bool> StartLegacyAdvertising(
const std::string& service_id, const std::string& local_endpoint_id,
const std::string& fast_advertisement_service_uuid)
ABSL_LOCKS_EXCLUDED(mutex_);
@@ -109,8 +111,9 @@ class BleV2 final {
// power_level - The power level to use for the discovery.
// discovered_peripheral_callback - The callback to invoke for discovery
// events.
bool StartScanning(const std::string& service_id, PowerLevel power_level,
DiscoveredPeripheralCallback callback)
ErrorOr<bool> StartScanning(const std::string& service_id,
PowerLevel power_level,
DiscoveredPeripheralCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables BLE scanning for a service ID.
@@ -123,8 +126,8 @@ class BleV2 final {
// Starts a worker thread, creates a Ble socket, associates it with a
// service id.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback)
ErrorOr<bool> StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_);
// Closes socket corresponding to a service id.
@@ -253,34 +253,34 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
operation_result_with_mediums.push_back(*operation_result_with_medium);
}
// TODO(edwinwu): Modify the returned code with a new OperationResultCode
// for BLE.
if (advertising_options.allowed.ble) {
ErrorOr<Medium> ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)};
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::kEnableBleV2)) {
Medium ble_v2_medium = StartBleV2Advertising(
client, service_id, local_endpoint_id, local_endpoint_info,
advertising_options, web_rtc_state);
if (ble_v2_medium != UNKNOWN_MEDIUM) {
ble_result = StartBleV2Advertising(client, service_id, local_endpoint_id,
local_endpoint_info,
advertising_options, web_rtc_state);
if (ble_result.has_value() && ble_result.value() != UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartAdvertisingImpl: Ble added";
mediums_started_successfully.push_back(ble_v2_medium);
mediums_started_successfully.push_back(ble_result.value());
}
} else {
Medium ble_medium = StartBleAdvertising(
client, service_id, local_endpoint_id, local_endpoint_info,
advertising_options, web_rtc_state);
if (ble_medium != UNKNOWN_MEDIUM) {
ble_result = StartBleAdvertising(client, service_id, local_endpoint_id,
local_endpoint_info, advertising_options,
web_rtc_state);
if (ble_result.has_value() && ble_result.value() != UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartAdvertisingImpl: Ble added";
mediums_started_successfully.push_back(ble_medium);
mediums_started_successfully.push_back(ble_result.value());
}
}
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
operation_result_with_medium = GetOperationResultWithMediumByResultCode(
client, BLE,
/*update_index=*/0, OperationResultCode::DETAIL_UNKNOWN);
operation_result_with_mediums.push_back(*operation_result_with_medium);
client, BLE, /*update_index=*/0,
ble_result.has_error()
? ble_result.error().operation_result_code().value()
: OperationResultCode::DETAIL_SUCCESS);
}
if (mediums_started_successfully.empty()) {
@@ -1181,32 +1181,41 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
operation_result_with_mediums.push_back(*operation_result_with_medium);
}
// TODO(edwinwu): Modify the returned code with a new OperationResultCode
// for BLE.
if (discovery_options.allowed.ble) {
ErrorOr<Medium> ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)};
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::kEnableBleV2)) {
Medium ble_v2_medium =
StartBleV2Scanning(client, service_id, discovery_options);
ble_result = StartBleV2Scanning(client, service_id, discovery_options);
Medium ble_v2_medium = UNKNOWN_MEDIUM;
if (ble_result.has_value()) {
ble_v2_medium = ble_result.value();
}
if (ble_v2_medium != UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartDiscoveryImpl: Ble v2 added.";
<< "P2pClusterPcpHandler::StartDiscoveryImpl: Ble v2 added";
mediums_started_successfully.push_back(ble_v2_medium);
}
} else {
Medium ble_medium =
ble_result =
StartBleScanning(client, service_id,
discovery_options.fast_advertisement_service_uuid);
Medium ble_medium = UNKNOWN_MEDIUM;
if (ble_result.has_value()) {
ble_medium = ble_result.value();
}
if (ble_medium != UNKNOWN_MEDIUM) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::StartDiscoveryImpl: Ble added.";
<< "P2pClusterPcpHandler::StartDiscoveryImpl: Ble added";
mediums_started_successfully.push_back(ble_medium);
}
}
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
operation_result_with_medium = GetOperationResultWithMediumByResultCode(
client, BLE,
/*update_index=*/0, OperationResultCode::DETAIL_UNKNOWN);
/*update_index=*/0,
ble_result.has_error()
? ble_result.error().operation_result_code().value()
: OperationResultCode::DETAIL_SUCCESS);
operation_result_with_mediums.push_back(*operation_result_with_medium);
}
@@ -1588,34 +1597,37 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl(
OperationResultCode::DETAIL_SUCCESS);
operation_result_with_mediums.push_back(*operation_result_with_medium);
} else {
ErrorOr<Medium> ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)};
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableBleV2)) {
if (StartBleV2Advertising(
client, std::string(service_id), std::string(local_endpoint_id),
ByteArray(std::string(local_endpoint_info)),
advertising_options, web_rtc_state) != UNKNOWN_MEDIUM) {
ble_result = StartBleV2Advertising(
client, std::string(service_id), std::string(local_endpoint_id),
ByteArray(std::string(local_endpoint_info)), advertising_options,
web_rtc_state);
if (ble_result.has_value() && ble_result.value() != UNKNOWN_MEDIUM) {
restarted_mediums.push_back(BLE);
} else {
status = {Status::kBleError};
}
} else {
if (StartBleAdvertising(
client, std::string(service_id), std::string(local_endpoint_id),
ByteArray(std::string(local_endpoint_info)),
advertising_options, web_rtc_state) != UNKNOWN_MEDIUM) {
ble_result = StartBleAdvertising(
client, std::string(service_id), std::string(local_endpoint_id),
ByteArray(std::string(local_endpoint_info)), advertising_options,
web_rtc_state);
if (ble_result.has_value() && ble_result.value() != UNKNOWN_MEDIUM) {
restarted_mediums.push_back(BLE);
} else {
status = {Status::kBleError};
}
}
// TODO(edwinwu): Modify the returned code with a new
// OperationResultCode for BLE.
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
operation_result_with_medium =
GetOperationResultWithMediumByResultCode(
client, BLE, update_index,
OperationResultCode::DETAIL_UNKNOWN);
ble_result.has_error()
? ble_result.error().operation_result_code().value()
: OperationResultCode::DETAIL_SUCCESS);
operation_result_with_mediums.push_back(*operation_result_with_medium);
}
}
@@ -1795,28 +1807,44 @@ P2pClusterPcpHandler::UpdateDiscoveryOptionsImpl(
should_start_discovery = true;
if (old_mediums.ble) {
restarted_mediums.push_back(BLE);
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
operation_result_with_medium =
GetOperationResultWithMediumByResultCode(
client, BLE, update_index,
OperationResultCode::DETAIL_SUCCESS);
operation_result_with_mediums.push_back(*operation_result_with_medium);
} else {
ErrorOr<Medium> ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)};
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kEnableBleV2)) {
if (StartBleV2Scanning(client, std::string(service_id),
discovery_options) != UNKNOWN_MEDIUM) {
ble_result = StartBleV2Scanning(client, std::string(service_id),
discovery_options);
if (ble_result.has_value()) {
restarted_mediums.push_back(BLE);
} else {
NEARBY_LOGS(WARNING) << "UpdateDiscoveryOptionsImpl: unable to "
"restart blev2 scanning";
}
} else {
if (StartBleScanning(
client, std::string(service_id),
discovery_options.fast_advertisement_service_uuid) !=
UNKNOWN_MEDIUM) {
ble_result =
StartBleScanning(client, std::string(service_id),
discovery_options.fast_advertisement_service_uuid);
if (ble_result.has_value()) {
restarted_mediums.push_back(BLE);
} else {
NEARBY_LOGS(WARNING)
<< "UpdateDiscoveryOptionsImpl: unable to restart ble scanning";
}
}
std::unique_ptr<ConnectionsLog::OperationResultWithMedium>
operation_result_with_medium =
GetOperationResultWithMediumByResultCode(
client, BLE, update_index,
ble_result.has_error()
? ble_result.error().operation_result_code().value()
: OperationResultCode::DETAIL_SUCCESS);
operation_result_with_mediums.push_back(*operation_result_with_medium);
}
}
// bt classic
@@ -1938,6 +1966,8 @@ ErrorOr<Medium> P2pClusterPcpHandler::StartBluetoothAdvertising(
<< service_id << ": start";
if (!bluetooth_medium_.IsAcceptingConnections(service_id)) {
ErrorOr<bool> error = true;
// TODO(b/380411884): Remove this check since we shouldn't enable radio by
// NC.
if (!bluetooth_radio_.Enable()) {
error = {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
} else {
@@ -2023,6 +2053,7 @@ ErrorOr<Medium> P2pClusterPcpHandler::StartBluetoothAdvertising(
ErrorOr<Medium> P2pClusterPcpHandler::StartBluetoothDiscovery(
ClientProxy* client, const std::string& service_id) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(INFO) << "In StartBluetoothDiscovery(), client="
<< client->GetClientId()
@@ -2186,7 +2217,7 @@ void P2pClusterPcpHandler::BleConnectionAcceptedHandler(
});
}
Medium P2pClusterPcpHandler::StartBleAdvertising(
ErrorOr<Medium> P2pClusterPcpHandler::StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id, const ByteArray& local_endpoint_info,
const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state) {
@@ -2203,12 +2234,9 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::StartBleAdvertising: service_id="
<< service_id << " : start";
if (!ble_medium_.IsAcceptingConnections(service_id)) {
if (!bluetooth_radio_.Enable() ||
!ble_medium_.StartAcceptingConnections(
service_id, absl::bind_front(
&P2pClusterPcpHandler::BleConnectionAcceptedHandler,
this, client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice))) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by
// NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(WARNING)
<< "In StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2216,7 +2244,22 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return UNKNOWN_MEDIUM;
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> accept_result = ble_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(&P2pClusterPcpHandler::BleConnectionAcceptedHandler,
this, client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice));
if (!accept_result.has_value()) {
NEARBY_LOGS(WARNING)
<< "In StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return {Error(accept_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO)
<< "In StartBleAdvertising("
@@ -2230,13 +2273,26 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
ShouldAcceptBluetoothConnections(advertising_options)) {
if (bluetooth_medium_.IsAvailable() &&
!bluetooth_medium_.IsAcceptingConnections(service_id)) {
if (!bluetooth_radio_.Enable() ||
!bluetooth_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(
&P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler,
this, client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice))) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by
// NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(WARNING)
<< "In BT StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return {
Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> accept_result = bluetooth_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(
&P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, this,
client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice));
if (!accept_result.has_value()) {
NEARBY_LOGS(WARNING)
<< "In BT StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2245,7 +2301,7 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
"service_id="
<< service_id;
ble_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(accept_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO)
<< "In BT StartBleAdvertising("
@@ -2289,7 +2345,7 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< "), client=" << client->GetClientId()
<< " failed to create an advertisement.";
ble_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)};
}
NEARBY_LOGS(INFO) << "In StartBleAdvertising("
@@ -2299,9 +2355,10 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< service_id << ", bytes: "
<< absl::BytesToHexString(advertisement_bytes.data());
if (!ble_medium_.StartAdvertising(
service_id, advertisement_bytes,
advertising_options.fast_advertisement_service_uuid)) {
ErrorOr<bool> ble_result = ble_medium_.StartAdvertising(
service_id, advertisement_bytes,
advertising_options.fast_advertisement_service_uuid);
if (ble_result.has_error()) {
NEARBY_LOGS(WARNING)
<< "In StartBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2309,7 +2366,7 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< " couldn't start BLE Advertising with BleAdvertisement "
<< absl::BytesToHexString(advertisement_bytes.data());
ble_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(ble_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO) << "In startBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2317,34 +2374,41 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< "), client=" << client->GetClientId()
<< " started BLE Advertising with BleAdvertisement "
<< absl::BytesToHexString(advertisement_bytes.data());
return BLE;
return {BLE};
}
Medium P2pClusterPcpHandler::StartBleScanning(
ErrorOr<Medium> P2pClusterPcpHandler::StartBleScanning(
ClientProxy* client, const std::string& service_id,
const std::string& fast_advertisement_service_uuid) {
if (bluetooth_radio_.Enable() &&
ble_medium_.StartScanning(
service_id, fast_advertisement_service_uuid,
{
.peripheral_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BlePeripheralDiscoveredHandler, this,
client),
.peripheral_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BlePeripheralLostHandler, this,
client),
})) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(INFO) << "In StartBleScanning(), client="
<< client->GetClientId()
<< " couldn't start scanning on BLE for service_id="
<< service_id;
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> result = ble_medium_.StartScanning(
service_id, fast_advertisement_service_uuid,
{
.peripheral_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BlePeripheralDiscoveredHandler, this,
client),
.peripheral_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BlePeripheralLostHandler, this, client),
});
if (!result.has_error()) {
NEARBY_LOGS(INFO)
<< "In StartBleScanning(), client=" << client->GetClientId()
<< " started scanning for BLE advertisements for service_id="
<< service_id;
return BLE;
return {BLE};
} else {
NEARBY_LOGS(INFO) << "In StartBleScanning(), client="
<< client->GetClientId()
<< " couldn't start scanning on BLE for service_id="
<< service_id;
return UNKNOWN_MEDIUM;
return {Error(result.error().operation_result_code().value())};
}
}
@@ -2402,7 +2466,7 @@ void P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler(
});
}
Medium P2pClusterPcpHandler::StartBleV2Advertising(
ErrorOr<Medium> P2pClusterPcpHandler::StartBleV2Advertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id, const ByteArray& local_endpoint_info,
const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state) {
@@ -2414,13 +2478,9 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
<< "P2pClusterPcpHandler::StartBleV2Advertising: service_id="
<< service_id << " : start";
if (!ble_v2_medium_.IsAcceptingConnections(service_id)) {
if (!bluetooth_radio_.Enable() ||
!ble_v2_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(
&P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this,
client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice))) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by
// NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(WARNING)
<< "In StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2428,7 +2488,22 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return UNKNOWN_MEDIUM;
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> ble_v2_result = ble_v2_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(&P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler,
this, client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice));
if (ble_v2_result.has_error()) {
NEARBY_LOGS(WARNING)
<< "In StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return {Error(ble_v2_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO)
<< "In StartBleV2Advertising("
@@ -2445,13 +2520,26 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
ShouldAcceptBluetoothConnections(advertising_options)) {
if (bluetooth_medium_.IsAvailable() &&
!bluetooth_medium_.IsAcceptingConnections(service_id)) {
if (!bluetooth_radio_.Enable() ||
!bluetooth_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(
&P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler,
this, client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice))) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by
// NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(WARNING)
<< "In BT StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " failed to start accepting for incoming BLE connections to "
"service_id="
<< service_id;
return {
Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> accept_result = bluetooth_medium_.StartAcceptingConnections(
service_id,
absl::bind_front(
&P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, this,
client, local_endpoint_info.AsStringView(),
NearbyDevice::Type::kConnectionsDevice));
if (accept_result.has_error()) {
NEARBY_LOGS(WARNING)
<< "In BT StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2460,7 +2548,7 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
"service_id="
<< service_id;
ble_v2_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(accept_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO)
<< "In BT StartBleV2Advertising("
@@ -2505,7 +2593,7 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
<< "), client=" << client->GetClientId()
<< " failed to create an advertisement.";
ble_v2_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)};
}
NEARBY_LOGS(INFO) << "In StartBleV2Advertising("
@@ -2514,9 +2602,10 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
<< " generated BleAdvertisement with service_id="
<< service_id;
if (!ble_v2_medium_.StartAdvertising(
service_id, advertisement_bytes, power_level,
!advertising_options.fast_advertisement_service_uuid.empty())) {
ErrorOr<bool> ble_v2_result = ble_v2_medium_.StartAdvertising(
service_id, advertisement_bytes, power_level,
!advertising_options.fast_advertisement_service_uuid.empty());
if (ble_v2_result.has_error()) {
NEARBY_LOGS(WARNING)
<< "In StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
@@ -2524,49 +2613,55 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising(
<< " couldn't start BLE Advertising with BleAdvertisement "
<< absl::BytesToHexString(advertisement_bytes.data());
ble_v2_medium_.StopAcceptingConnections(service_id);
return UNKNOWN_MEDIUM;
return {Error(ble_v2_result.error().operation_result_code().value())};
}
NEARBY_LOGS(INFO) << "In StartBleV2Advertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " started BLE Advertising with BleAdvertisement "
<< absl::BytesToHexString(advertisement_bytes.data());
return BLE;
return {BLE};
}
Medium P2pClusterPcpHandler::StartBleV2Scanning(
ErrorOr<Medium> P2pClusterPcpHandler::StartBleV2Scanning(
ClientProxy* client, const std::string& service_id,
const DiscoveryOptions& discovery_options) {
PowerLevel power_level = discovery_options.low_power ? PowerLevel::kLowPower
: PowerLevel::kHighPower;
if (bluetooth_radio_.Enable() &&
ble_v2_medium_.StartScanning(
service_id, power_level,
{
.peripheral_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler, this,
client),
.peripheral_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2PeripheralLostHandler, this,
client),
.instant_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2InstantLostHandler, this, client),
.legacy_device_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2LegacyDeviceDiscoveredHandler,
this),
})) {
// TODO(b/380411884): Remove this check since we shouldn't enable radio by NC.
if (!bluetooth_radio_.Enable()) {
NEARBY_LOGS(INFO) << "In StartBleV2Scanning(), client="
<< client->GetClientId()
<< " couldn't start scanning on BLE for service_id="
<< service_id;
return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)};
}
ErrorOr<bool> ble_v2_result = ble_v2_medium_.StartScanning(
service_id, power_level,
{
.peripheral_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler, this,
client),
.peripheral_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2PeripheralLostHandler, this, client),
.instant_lost_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2InstantLostHandler, this, client),
.legacy_device_discovered_cb = absl::bind_front(
&P2pClusterPcpHandler::BleV2LegacyDeviceDiscoveredHandler, this),
});
if (!ble_v2_result.has_error()) {
NEARBY_LOGS(INFO)
<< "In StartBleV2Scanning(), client=" << client->GetClientId()
<< " started scanning for BLE advertisements for service_id="
<< service_id;
return BLE;
return {BLE};
}
NEARBY_LOGS(INFO) << "In StartBleV2Scanning(), client="
<< client->GetClientId()
<< " couldn't start scanning on BLE for service_id="
<< service_id;
return UNKNOWN_MEDIUM;
return {Error(ble_v2_result.error().operation_result_code().value())};
}
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl(
@@ -230,12 +230,12 @@ class P2pClusterPcpHandler : public BasePcpHandler {
NearbyDevice::Type device_type,
BleSocket socket,
const std::string& service_id);
location::nearby::proto::connections::Medium StartBleAdvertising(
ErrorOr<location::nearby::proto::connections::Medium> StartBleAdvertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info,
const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state);
location::nearby::proto::connections::Medium StartBleScanning(
ErrorOr<location::nearby::proto::connections::Medium> StartBleScanning(
ClientProxy* client, const std::string& service_id,
const std::string& fast_advertisement_service_uuid);
BasePcpHandler::ConnectImplResult BleConnectImpl(ClientProxy* client,
@@ -265,12 +265,12 @@ class P2pClusterPcpHandler : public BasePcpHandler {
NearbyDevice::Type device_type,
BleV2Socket socket,
const std::string& service_id);
location::nearby::proto::connections::Medium StartBleV2Advertising(
ErrorOr<location::nearby::proto::connections::Medium> StartBleV2Advertising(
ClientProxy* client, const std::string& service_id,
const std::string& local_endpoint_id,
const ByteArray& local_endpoint_info,
const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state);
location::nearby::proto::connections::Medium StartBleV2Scanning(
ErrorOr<location::nearby::proto::connections::Medium> StartBleV2Scanning(
ClientProxy* client, const std::string& service_id,
const DiscoveryOptions& discovery_options);
BasePcpHandler::ConnectImplResult BleV2ConnectImpl(ClientProxy* client,