Internal fix on p2p_cluster_pcp_handler

PiperOrigin-RevId: 584669043
This commit is contained in:
Hai Shang
2023-11-22 10:44:38 -08:00
committed by Copybara-Service
parent 8987ddd461
commit 6ee53db8f5
5 changed files with 176 additions and 7 deletions
+75 -1
View File
@@ -14,6 +14,7 @@
#include "connections/implementation/mediums/ble.h"
#include <array>
#include <memory>
#include <string>
#include <utility>
@@ -76,7 +77,7 @@ bool Ble::StartAdvertising(const std::string& service_id,
if (!radio_.IsEnabled()) {
NEARBY_LOGS(INFO)
<< "Can't start BLE scanning because Bluetooth was never turned on";
<< "Can't start BLE adveertising because Bluetooth was never turned on";
return false;
}
@@ -138,6 +139,79 @@ bool Ble::StopAdvertising(const std::string& service_id) {
return ret;
}
bool Ble::StartLegacyAdvertising(
const std::string& input_service_id, const std::string& local_endpoint_id,
const std::string& fast_advertisement_service_uuid) {
NEARBY_LOGS(INFO) << "StartLegacyAdvertising: " << input_service_id.c_str()
<< ", local_endpoint_id: " << local_endpoint_id.c_str();
MutexLock lock(&mutex_);
std::string service_id = input_service_id + "-Legacy";
if (IsAdvertisingLocked(service_id)) {
NEARBY_LOGS(INFO)
<< "Failed to BLE legacy advertise because we're already advertising.";
return false;
}
if (!radio_.IsEnabled()) {
NEARBY_LOGS(INFO) << "Can't start BLE legacy advertising because Bluetooth "
"was never turned on";
return false;
}
if (!IsAvailableLocked()) {
NEARBY_LOGS(INFO)
<< "Can't turn on BLE legacy advertising. BLE is not available.";
return false;
}
// TODO(hais) improve working dummy set to feed proper hash value.
std::array<char, 23> encoded_legacy_char_array = {
0x51, 0x43, 0x41, 0x41, 0x41, 0x42, 0x41, 0x43, 0x41, 0x41, 0x41, 0x44,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41};
ByteArray encoded_bytes{encoded_legacy_char_array};
NEARBY_LOGS(INFO) << "Turning on BLE advertising (advertisement size="
<< encoded_bytes.size()
<< "): " << absl::BytesToHexString(encoded_bytes.data())
<< ", service id=" << service_id
<< ", fast advertisement service uuid="
<< fast_advertisement_service_uuid;
if (!medium_.StartAdvertising(service_id, encoded_bytes,
fast_advertisement_service_uuid)) {
NEARBY_LOGS(ERROR)
<< "Failed to turn on BLE advertising with advertisement bytes="
<< absl::BytesToHexString(encoded_bytes.data())
<< ", size=" << encoded_bytes.size()
<< ", fast advertisement service uuid="
<< fast_advertisement_service_uuid;
return false;
}
advertising_info_.Add(service_id);
return true;
}
bool Ble::StopLegacyAdvertising(const std::string& input_service_id) {
NEARBY_LOGS(INFO) << "StopLegacyAdvertising:" << input_service_id.c_str();
MutexLock lock(&mutex_);
std::string service_id = input_service_id + "-Legacy";
if (!IsAdvertisingLocked(service_id)) {
NEARBY_LOGS(INFO)
<< "Can't turn off BLE legacy advertising; it is already off";
return false;
}
NEARBY_LOGS(INFO) << "Turned off BLE legacy advertising with service id="
<< service_id;
bool ret = medium_.StopAdvertising(service_id);
// Reset our bundle of advertising state to mark that we're no longer
// advertising.
advertising_info_.Remove(service_id);
return ret;
}
bool Ble::IsAdvertising(const std::string& service_id) {
MutexLock lock(&mutex_);
+13
View File
@@ -53,6 +53,19 @@ class Ble {
bool StopAdvertising(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
// (TODO:hais) remove this after ble_v2 refactor
// Sets custom advertisement data, and then enables Ble advertising.
// Returns true, if data is successfully set, and false otherwise.
bool StartLegacyAdvertising(
const std::string& service_id, const std::string& local_endpoint_id,
const std::string& fast_advertisement_service_uuid)
ABSL_LOCKS_EXCLUDED(mutex_);
// (TODO:hais) remove this after ble_v2 refactor
// Disables Ble advertising.
bool StopLegacyAdvertising(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
bool IsAdvertising(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
// Enables Ble scanning mode. Will report any discoverable peripherals in
@@ -247,6 +247,24 @@ TEST_F(BleTest, CanStartDiscovery) {
env_.Stop();
}
TEST_F(BleTest, CanStartAndStopLegacyAdvertising) {
env_.Start();
BluetoothRadio radio_a;
Ble ble_a{radio_a};
radio_a.Enable();
std::string service_id(kServiceID);
std::string legacy_service_id(std::string{kServiceID} + "-Legacy");
std::string device_a_endpoint_id{"1A1A"};
std::string fast_advertisement_service_uuid(kFastAdvertisementServiceUuid);
EXPECT_TRUE(ble_a.StartLegacyAdvertising(service_id, device_a_endpoint_id,
fast_advertisement_service_uuid));
EXPECT_FALSE(ble_a.IsAdvertising(service_id));
EXPECT_TRUE(ble_a.IsAdvertising(legacy_service_id));
EXPECT_TRUE(ble_a.StopLegacyAdvertising(service_id));
EXPECT_FALSE(ble_a.IsAdvertising(legacy_service_id));
env_.Stop();
}
} // namespace
} // namespace connections
} // namespace nearby
@@ -76,6 +76,8 @@ bool BluetoothClassic::IsAvailableLocked() const {
}
bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
NEARBY_LOGS(INFO) << "Turning on BT discoverability with device_name="
<< device_name;
MutexLock lock(&mutex_);
if (device_name.empty()) {
@@ -126,6 +128,7 @@ bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {
}
bool BluetoothClassic::TurnOffDiscoverability() {
NEARBY_LOGS(INFO) << "Turning off Bluetooth discoverability.";
MutexLock lock(&mutex_);
if (!IsDiscoverable()) {
@@ -136,7 +139,7 @@ bool BluetoothClassic::TurnOffDiscoverability() {
RestoreScanMode();
RestoreDeviceName();
NEARBY_LOGS(INFO) << "Turned Bluetooth discoverability off";
NEARBY_LOGS(INFO) << "Turned Bluetooth discoverability off.";
return true;
}
@@ -36,8 +36,10 @@
#include "connections/status.h"
#include "internal/flags/nearby_flags.h"
#include "internal/interop/device.h"
#include "internal/platform/implementation/platform.h"
#include "internal/platform/logging.h"
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/os_name.h"
#include "internal/platform/types.h"
#include "proto/connections_enums.pb.h"
@@ -140,9 +142,35 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
local_endpoint_info, web_rtc_state);
if (bluetooth_medium !=
location::nearby::proto::connections::UNKNOWN_MEDIUM) {
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
mediums_started_successfully.push_back(bluetooth_medium);
bluetooth_classic_advertiser_client_id_ = client->GetClientId();
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: BT started");
// TODO(hais): update this after ble_v2 refactor.
if (api::ImplementationPlatform::GetCurrentOS() ==
api::OSName::kChromeOS) {
if (ble_medium_.StartLegacyAdvertising(
service_id, local_endpoint_id,
advertising_options.fast_advertisement_service_uuid)) {
NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::StartAdvertisingImpl: "
"Ble legacy started advertising";
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
mediums_started_successfully.push_back(bluetooth_medium);
bluetooth_classic_advertiser_client_id_ = client->GetClientId();
} else {
// TODO(hais): update this after ble_v2 refactor.
NEARBY_LOG(WARNING,
"P2pClusterPcpHandler::StartAdvertisingImpl: BLE legacy "
"failed, revert BTC");
bluetooth_medium_.TurnOffDiscoverability();
bluetooth_medium_.StopAcceptingConnections(service_id);
}
} else {
NEARBY_LOG(INFO,
"P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
mediums_started_successfully.push_back(bluetooth_medium);
bluetooth_classic_advertiser_client_id_ = client->GetClientId();
}
}
}
@@ -192,6 +220,10 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) {
if (client->GetClientId() == bluetooth_classic_advertiser_client_id_) {
bluetooth_medium_.TurnOffDiscoverability();
// TODO(hais): update this after ble_v2 refactor.
if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS) {
ble_medium_.StopLegacyAdvertising(client->GetAdvertisingServiceId());
}
bluetooth_classic_advertiser_client_id_ = 0;
} else {
NEARBY_LOGS(INFO) << "Skipped BT TurnOffDiscoverability for client="
@@ -1235,6 +1267,10 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl(
mediums_->GetBluetoothClassic().TurnOffDiscoverability();
mediums_->GetBluetoothClassic().StopAcceptingConnections(
std::string(service_id));
// TODO(hais): update this after ble_v2 refactor.
if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS) {
mediums_->GetBle().StopLegacyAdvertising(std::string(service_id));
}
}
// restart
@@ -1300,7 +1336,30 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl(
std::string(local_endpoint_id),
ByteArray(std::string(local_endpoint_info)),
web_rtc_state) != Medium::UNKNOWN_MEDIUM) {
restarted_mediums.push_back(Medium::BLUETOOTH);
// TODO(hais): update this after ble_v2 refactor.
if (api::ImplementationPlatform::GetCurrentOS() ==
api::OSName::kChromeOS) {
if (ble_medium_.StartLegacyAdvertising(
std::string(service_id), std::string(local_endpoint_id),
advertising_options.fast_advertisement_service_uuid)) {
NEARBY_LOGS(INFO)
<< "P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: "
"Ble legacy started advertising";
NEARBY_LOG(
INFO,
"P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: BT added");
restarted_mediums.push_back(Medium::BLUETOOTH);
} else {
NEARBY_LOG(WARNING,
"P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl: "
"BLE legacy "
"failed, revert BTC");
bluetooth_medium_.TurnOffDiscoverability();
bluetooth_medium_.StopAcceptingConnections(std::string(service_id));
}
} else {
restarted_mediums.push_back(Medium::BLUETOOTH);
}
} else {
return StartOperationResult{.status = {Status::kBluetoothError},
.mediums = restarted_mediums};
@@ -1726,7 +1785,8 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
<< absl::BytesToHexString(local_endpoint_info.data())
<< "), client=" << client->GetClientId()
<< " generated BleAdvertisement with service_id="
<< service_id;
<< service_id << ", bytes: "
<< absl::BytesToHexString(advertisement_bytes.data());
if (!ble_medium_.StartAdvertising(
service_id, advertisement_bytes,
@@ -1742,6 +1802,7 @@ Medium P2pClusterPcpHandler::StartBleAdvertising(
}
NEARBY_LOGS(INFO) << "In startBleAdvertising("
<< absl::BytesToHexString(local_endpoint_info.data())
<< ", fast_advertisement: " << fast_advertisement
<< "), client=" << client->GetClientId()
<< " started BLE Advertising with BleAdvertisement "
<< absl::BytesToHexString(advertisement_bytes.data());