mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Remove reference to BlePeripheral.
PiperOrigin-RevId: 757830081
This commit is contained in:
committed by
Copybara-Service
parent
acc5f1b0d8
commit
b48b943275
@@ -1379,10 +1379,10 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id,
|
||||
}
|
||||
},
|
||||
.advertisement_found_cb =
|
||||
[this](api::ble_v2::BlePeripheral& peripheral,
|
||||
[this](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
AssumeHeld(mutex_);
|
||||
BleV2Peripheral proxy(medium_, peripheral.GetUniqueId());
|
||||
BleV2Peripheral proxy(medium_, peripheral_id);
|
||||
RunOnBleThread([this, proxy = std::move(proxy),
|
||||
advertisement_data]() {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -1406,7 +1406,7 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id,
|
||||
});
|
||||
},
|
||||
.advertisement_lost_cb =
|
||||
[](api::ble_v2::BlePeripheral& peripheral) {
|
||||
[](api::ble_v2::BlePeripheral::UniqueId peripheral_id) {
|
||||
// TODO(b/345514862): Implement.
|
||||
},
|
||||
});
|
||||
|
||||
@@ -190,13 +190,7 @@ class DiscoveredPeripheralTrackerTest : public testing::TestWithParam<bool> {
|
||||
BleV2Peripheral CreateBlePeripheral() {
|
||||
MacAddress mac_address;
|
||||
MacAddress::FromString(adapter_peripheral_->GetMacAddress(), mac_address);
|
||||
api::ble_v2::BlePeripheral api_peripheral;
|
||||
ble_central_->GetImpl()->GetRemotePeripheral(
|
||||
mac_address.address(),
|
||||
[&api_peripheral](api::ble_v2::BlePeripheral& peripheral) {
|
||||
api_peripheral = peripheral;
|
||||
});
|
||||
return BleV2Peripheral(*ble_central_, api_peripheral.GetUniqueId());
|
||||
return BleV2Peripheral(*ble_central_, mac_address.address());
|
||||
}
|
||||
|
||||
// Simulates to see a fast advertisement.
|
||||
|
||||
+21
-51
@@ -63,23 +63,17 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
ScanCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (scanning_enabled_) {
|
||||
LOG(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning already enabled";
|
||||
return false;
|
||||
}
|
||||
bool success = impl_->StartScanning(
|
||||
service_uuid, tx_power_level,
|
||||
api::ble_v2::BleMedium::ScanCallback{
|
||||
.advertisement_found_cb =
|
||||
[this](api::ble_v2::BlePeripheral& peripheral,
|
||||
[this](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!peripherals_.contains(&peripheral)) {
|
||||
LOG(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
peripherals_.insert(&peripheral);
|
||||
}
|
||||
|
||||
BleV2Peripheral proxy(*this, peripheral.GetUniqueId());
|
||||
BleV2Peripheral proxy(*this, peripheral_id);
|
||||
if (!scanning_enabled_) return;
|
||||
scan_callback_.advertisement_found_cb(std::move(proxy),
|
||||
advertisement_data);
|
||||
@@ -87,14 +81,8 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
});
|
||||
if (success) {
|
||||
scan_callback_ = std::move(callback);
|
||||
// Clear the `peripherals_` after succeeded in StartScanning and before the
|
||||
// advertisement_found callback has been reached. This prevents deleting the
|
||||
// existing `peripherals_` if the scanning is not started successfully. If
|
||||
// scanning is started successfully, we need to clear `peripherals_` to
|
||||
// prevent the stale data in cache.
|
||||
peripherals_.clear();
|
||||
scanning_enabled_ = true;
|
||||
LOG(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning enabled";
|
||||
}
|
||||
return success;
|
||||
}
|
||||
@@ -104,23 +92,17 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (scanning_enabled_) {
|
||||
LOG(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning already enabled";
|
||||
return false;
|
||||
}
|
||||
bool success = impl_->StartMultipleServicesScanning(
|
||||
service_uuids, tx_power_level,
|
||||
api::ble_v2::BleMedium::ScanCallback{
|
||||
.advertisement_found_cb =
|
||||
[this](api::ble_v2::BlePeripheral& peripheral,
|
||||
[this](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!peripherals_.contains(&peripheral)) {
|
||||
LOG(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
peripherals_.insert(&peripheral);
|
||||
}
|
||||
|
||||
BleV2Peripheral proxy(*this, peripheral.GetUniqueId());
|
||||
BleV2Peripheral proxy(*this, peripheral_id);
|
||||
if (!scanning_enabled_) return;
|
||||
scan_callback_.advertisement_found_cb(std::move(proxy),
|
||||
advertisement_data);
|
||||
@@ -128,9 +110,8 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
});
|
||||
if (success) {
|
||||
scan_callback_ = std::move(callback);
|
||||
peripherals_.clear();
|
||||
scanning_enabled_ = true;
|
||||
LOG(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning enabled";
|
||||
}
|
||||
return success;
|
||||
}
|
||||
@@ -143,9 +124,8 @@ bool BleV2Medium::StopScanning() {
|
||||
return true;
|
||||
}
|
||||
scanning_enabled_ = false;
|
||||
peripherals_.clear();
|
||||
scan_callback_ = {};
|
||||
LOG(INFO) << "Ble Scanning disabled: impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning disabled";
|
||||
return impl_->StopScanning();
|
||||
}
|
||||
bool BleV2Medium::PauseMediumScanning() {
|
||||
@@ -153,7 +133,7 @@ bool BleV2Medium::PauseMediumScanning() {
|
||||
if (!scanning_enabled_) {
|
||||
return true;
|
||||
}
|
||||
LOG(INFO) << "Pause Medium level BLE_V2 Scanning: impl=" << GetImpl();
|
||||
LOG(INFO) << "Pause Medium level BLE_V2 Scanning";
|
||||
return impl_->PauseMediumScanning();
|
||||
}
|
||||
|
||||
@@ -289,32 +269,22 @@ BleV2Socket BleV2Medium::Connect(const std::string& service_id,
|
||||
BleL2capSocket BleV2Medium::ConnectOverL2cap(
|
||||
const std::string& service_id, TxPowerLevel tx_power_level,
|
||||
const BleV2Peripheral& peripheral, CancellationFlag* cancellation_flag) {
|
||||
BleL2capSocket socket;
|
||||
api::ble_v2::BlePeripheral* device = peripheral.GetImpl();
|
||||
if (device != nullptr) {
|
||||
socket = BleL2capSocket(
|
||||
peripheral,
|
||||
impl_->ConnectOverL2cap(peripheral.GetPsm(), service_id, tx_power_level,
|
||||
device->GetUniqueId(), cancellation_flag));
|
||||
};
|
||||
return socket;
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId> id =
|
||||
peripheral.GetUniqueId();
|
||||
if (!id.has_value()) {
|
||||
LOG(ERROR) << "Failed to connect over L2cap, invalid peripheral";
|
||||
return {};
|
||||
}
|
||||
return BleL2capSocket(
|
||||
peripheral,
|
||||
impl_->ConnectOverL2cap(peripheral.GetPsm(), service_id, tx_power_level,
|
||||
*id, cancellation_flag));
|
||||
}
|
||||
|
||||
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
return IsValid() && impl_->IsExtendedAdvertisementsAvailable();
|
||||
}
|
||||
|
||||
bool BleV2Peripheral::IsValid() const { return GetImpl() != nullptr; }
|
||||
|
||||
api::ble_v2::BlePeripheral* BleV2Peripheral::GetImpl() const {
|
||||
if (!unique_id_.has_value()) return nullptr;
|
||||
api::ble_v2::BlePeripheral* result = nullptr;
|
||||
if (!medium_->GetImpl()->GetRemotePeripheral(
|
||||
unique_id_.value(),
|
||||
[&](api::ble_v2::BlePeripheral& device) { result = &device; })) {
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
bool BleV2Peripheral::IsValid() const { return unique_id_.has_value(); }
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -68,7 +68,6 @@ class BleV2Peripheral final {
|
||||
void SetPsm(int psm) { psm_ = psm; }
|
||||
|
||||
bool IsValid() const;
|
||||
explicit operator bool() const { return IsValid(); }
|
||||
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId> GetUniqueId() const {
|
||||
return unique_id_;
|
||||
@@ -588,8 +587,6 @@ class BleV2Medium final {
|
||||
BluetoothAdapter& adapter_;
|
||||
ServerGattConnectionCallback server_gatt_connection_callback_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_set<api::ble_v2::BlePeripheral*> peripherals_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
ScanCallback scan_callback_ ABSL_GUARDED_BY(mutex_);
|
||||
bool scanning_enabled_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
@@ -464,7 +464,7 @@ TEST_F(BleV2MediumTest, StartThenStopAsyncScanning) {
|
||||
service_uuid, kTxPowerLevel,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
.advertisement_found_cb =
|
||||
[&](api::ble_v2::BlePeripheral& peripheral,
|
||||
[&](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) -> void {
|
||||
found_latch_a.CountDown();
|
||||
},
|
||||
@@ -490,7 +490,7 @@ TEST_F(BleV2MediumTest, CanStartMultipleAsyncScanning) {
|
||||
service_uuid, kTxPowerLevel,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
.advertisement_found_cb =
|
||||
[&](api::ble_v2::BlePeripheral& peripheral,
|
||||
[&](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) -> void {
|
||||
found_latch_a.CountDown();
|
||||
},
|
||||
@@ -500,7 +500,7 @@ TEST_F(BleV2MediumTest, CanStartMultipleAsyncScanning) {
|
||||
service_uuid, kTxPowerLevel,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
.advertisement_found_cb =
|
||||
[&](api::ble_v2::BlePeripheral& peripheral,
|
||||
[&](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) -> void {
|
||||
found_latch_b.CountDown();
|
||||
},
|
||||
@@ -533,7 +533,7 @@ TEST_F(BleV2MediumTest, CanStartAsyncScanningAndAdvertising) {
|
||||
service_uuid, kTxPowerLevel,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
.advertisement_found_cb =
|
||||
[&](api::ble_v2::BlePeripheral& peripheral,
|
||||
[&](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) -> void {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
|
||||
@@ -165,12 +165,6 @@ class BleMedium : public api::ble_v2::BleMedium {
|
||||
// This is currently always false for all Apple hardware.
|
||||
bool IsExtendedAdvertisementsAvailable() override;
|
||||
|
||||
// Returns true if `id` refers to a known BLE peripheral and calls `callback` with a reference to
|
||||
// said peripheral that is only guaranteed to be available for the duration of the callback.
|
||||
// Otherwise, does not call the callback and returns false.
|
||||
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) override;
|
||||
|
||||
private:
|
||||
void HandleAdvertisementFound(id<GNCPeripheral> peripheral,
|
||||
NSDictionary<CBUUID *, NSData *> *serviceData);
|
||||
|
||||
@@ -139,10 +139,10 @@ void BleMedium::HandleAdvertisementFound(id<GNCPeripheral> peripheral,
|
||||
peripherals_[unique_id] = std::move(ble_peripheral);
|
||||
}
|
||||
if (scanning_cb_.advertisement_found_cb) {
|
||||
scanning_cb_.advertisement_found_cb(*peripherals_[unique_id], data);
|
||||
scanning_cb_.advertisement_found_cb(unique_id, data);
|
||||
}
|
||||
if (scan_cb_.advertisement_found_cb) {
|
||||
scan_cb_.advertisement_found_cb(*peripherals_[unique_id], data);
|
||||
scan_cb_.advertisement_found_cb(unique_id, data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,8 +471,8 @@ std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(
|
||||
serviceID:@(service_id.c_str())
|
||||
expectedIntroPacket:NO
|
||||
callbackQueue:dispatch_get_main_queue()];
|
||||
socket = std::make_unique<BleSocket>(connection,
|
||||
peripheral->GetUniqueId());
|
||||
socket =
|
||||
std::make_unique<BleSocket>(connection, peripheral_id);
|
||||
connection.connectionHandlers =
|
||||
socket->GetInputStream().GetConnectionHandlers();
|
||||
dispatch_semaphore_signal(semaphore);
|
||||
@@ -545,31 +545,5 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() {
|
||||
return [medium_ supportsExtendedAdvertisements];
|
||||
}
|
||||
|
||||
bool BleMedium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId unique_id,
|
||||
api::ble_v2::BleMedium::GetRemotePeripheralCallback callback) {
|
||||
// If the unique_id is 0, that means it's the local/empty peripheral. We must return "true"
|
||||
// otherwise the connection will be considered invalid and the application will crash.
|
||||
if (unique_id == BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {
|
||||
callback(BlePeripheral::DefaultBlePeripheral());
|
||||
return true;
|
||||
}
|
||||
|
||||
BlePeripheral *peripheral;
|
||||
{
|
||||
absl::MutexLock lock(&peripherals_mutex_);
|
||||
auto it = peripherals_.find(unique_id);
|
||||
if (it == peripherals_.end()) {
|
||||
return false;
|
||||
}
|
||||
peripheral = it->second.get();
|
||||
if (peripheral == nullptr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// We need to unlock before calling the callback, otherwise we will deadlock.
|
||||
callback(*peripheral);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace apple
|
||||
} // namespace nearby
|
||||
|
||||
@@ -451,7 +451,6 @@ class BleL2capServerSocket {
|
||||
// for all BLE and GATT related operations.
|
||||
class BleMedium {
|
||||
public:
|
||||
using GetRemotePeripheralCallback = absl::AnyInvocable<void(BlePeripheral&)>;
|
||||
virtual ~BleMedium() = default;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/le/BluetoothLeAdvertiser.html#startAdvertising(android.bluetooth.le.AdvertiseSettings,%20android.bluetooth.le.AdvertiseData,%20android.bluetooth.le.AdvertiseData,%20android.bluetooth.le.AdvertiseCallback)
|
||||
@@ -496,9 +495,10 @@ class BleMedium {
|
||||
// The peripheral is owned by platform implementation and it should outlive
|
||||
// for the whole peripheral(device) connection life cycle.
|
||||
struct ScanCallback {
|
||||
absl::AnyInvocable<void(BlePeripheral& peripheral,
|
||||
absl::AnyInvocable<void(BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb = [](BlePeripheral&, BleAdvertisementData) {};
|
||||
advertisement_found_cb =
|
||||
[](BlePeripheral::UniqueId, BleAdvertisementData) {};
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#startScan(java.util.List%3Candroid.bluetooth.le.ScanFilter%3E,%20android.bluetooth.le.ScanSettings,%20android.bluetooth.le.ScanCallback)
|
||||
@@ -546,11 +546,12 @@ class BleMedium {
|
||||
struct ScanningCallback {
|
||||
absl::AnyInvocable<void(absl::Status)> start_scanning_result =
|
||||
[](absl::Status) {};
|
||||
absl::AnyInvocable<void(BlePeripheral& peripheral,
|
||||
absl::AnyInvocable<void(BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data)>
|
||||
advertisement_found_cb = [](BlePeripheral&, BleAdvertisementData) {};
|
||||
absl::AnyInvocable<void(BlePeripheral& peripheral)> advertisement_lost_cb =
|
||||
[](BlePeripheral&) {};
|
||||
advertisement_found_cb =
|
||||
[](BlePeripheral::UniqueId, BleAdvertisementData) {};
|
||||
absl::AnyInvocable<void(BlePeripheral::UniqueId peripheral_id)>
|
||||
advertisement_lost_cb = [](BlePeripheral::UniqueId) {};
|
||||
};
|
||||
|
||||
// Async interface for StartScanning.
|
||||
@@ -624,11 +625,6 @@ class BleMedium {
|
||||
// Requests if support extended advertisement.
|
||||
virtual bool IsExtendedAdvertisementsAvailable() = 0;
|
||||
|
||||
// Calls `callback` and returns true if `id` refers to a known BLE peripheral.
|
||||
// Otherwise, does not call the callback and returns false.
|
||||
virtual bool GetRemotePeripheral(BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) = 0;
|
||||
|
||||
virtual void AddAlternateUuidForService(uint16_t uuid,
|
||||
const std::string& service_id) {}
|
||||
};
|
||||
|
||||
@@ -250,11 +250,12 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
for (const auto& service_uuid : service_uuids) {
|
||||
auto internal_session_id = Prng().NextUint32();
|
||||
ScanCallback multiple_scan_callback = {
|
||||
.advertisement_found_cb = [this](
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
scan_callback_.advertisement_found_cb(peripheral, advertisement_data);
|
||||
}};
|
||||
.advertisement_found_cb =
|
||||
[this](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
scan_callback_.advertisement_found_cb(peripheral_id,
|
||||
advertisement_data);
|
||||
}};
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuid, internal_session_id,
|
||||
@@ -348,35 +349,6 @@ bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
return is_extended_advertisements_available_;
|
||||
}
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
auto it = remote_peripherals_.find(id);
|
||||
if (it != remote_peripherals_.end()) {
|
||||
callback(*it->second);
|
||||
return true;
|
||||
}
|
||||
|
||||
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
|
||||
MediumEnvironment::Instance().FindBleV2Medium(id));
|
||||
if (remote_medium == nullptr) {
|
||||
LOG(INFO) << "Peripheral not found, id= " << id;
|
||||
return false;
|
||||
}
|
||||
BluetoothAdapter& adapter = remote_medium->GetAdapter();
|
||||
MacAddress address;
|
||||
if (!MacAddress::FromString(adapter.GetMacAddress(), address)) {
|
||||
LOG(ERROR) << "Adapter has invalid mac address: "
|
||||
<< adapter.GetMacAddress();
|
||||
return false;
|
||||
}
|
||||
remote_peripherals_[id] =
|
||||
std::make_unique<api::ble_v2::BlePeripheral>(id, address);
|
||||
remote_peripherals_[id]->SetPlatformData(&adapter);
|
||||
callback(*remote_peripherals_[id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
BleV2Medium::GattServer::GattServer(
|
||||
BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback)
|
||||
: medium_(medium), callback_(std::move(callback)) {
|
||||
|
||||
@@ -192,9 +192,6 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
|
||||
api::ble_v2::BlePeripheral& GetPeripheral() { return peripheral_; }
|
||||
|
||||
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) override;
|
||||
|
||||
private:
|
||||
class GattClient;
|
||||
// A concrete implementation for GattServer.
|
||||
|
||||
@@ -1173,7 +1173,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
|
||||
ble_advertisement_data.service_data[service_uuid_] = advertisement_data;
|
||||
|
||||
has_primary_service_data = true;
|
||||
scan_callback_.advertisement_found_cb(*peripheral_ptr,
|
||||
scan_callback_.advertisement_found_cb(peripheral_ptr->GetUniqueId(),
|
||||
ble_advertisement_data);
|
||||
} else {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -1203,7 +1203,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
|
||||
BleAdvertisementHeader header =
|
||||
CreateAdvertisementHeader(bluetooth_address, alt_service_ids);
|
||||
ble_advertisement_data.service_data[service_uuid_] = ByteArray(header);
|
||||
scan_callback_.advertisement_found_cb(*peripheral_ptr,
|
||||
scan_callback_.advertisement_found_cb(peripheral_ptr->GetUniqueId(),
|
||||
ble_advertisement_data);
|
||||
}
|
||||
}
|
||||
@@ -1292,36 +1292,14 @@ void BleV2Medium::AdvertisementFoundHandler(
|
||||
service_uuid_to_session_map_.end()) {
|
||||
for (auto& id_session_pair :
|
||||
service_uuid_to_session_map_[service_uuid]) {
|
||||
id_session_pair.second.advertisement_found_cb(*peripheral_ptr,
|
||||
ble_advertisement_data);
|
||||
id_session_pair.second.advertisement_found_cb(
|
||||
peripheral_ptr->GetUniqueId(), ble_advertisement_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
MacAddress bluetooth_address;
|
||||
if (!MacAddress::FromUint64(id, bluetooth_address)) {
|
||||
LOG(WARNING) << __func__ << ": Invalid MAC address: 0x"
|
||||
<< absl::StrCat(absl::Hex(id));
|
||||
return false;
|
||||
}
|
||||
api::ble_v2::BlePeripheral* peripheral = nullptr;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
peripheral = GetPeripheral(bluetooth_address);
|
||||
}
|
||||
|
||||
if (peripheral == nullptr) {
|
||||
LOG(WARNING) << __func__ << ": No matched peripheral device.";
|
||||
return false;
|
||||
}
|
||||
callback(*peripheral);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t BleV2Medium::GenerateSessionId() {
|
||||
for (int i = 0; i < kGenerateSessionIdRetryLimit; i++) {
|
||||
uint64_t session_id = Prng().NextInt64();
|
||||
|
||||
@@ -80,10 +80,6 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool IsExtendedAdvertisementsAvailable() override;
|
||||
|
||||
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
void AddAlternateUuidForService(uint16_t uuid,
|
||||
const std::string& service_id) override;
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
[&scan_response_received, &scan_response_notification](
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {
|
||||
scan_response_received = true;
|
||||
scan_response_notification.Notify();
|
||||
@@ -151,7 +151,7 @@ TEST(BleV2Medium, DISABLED_StopScanning) {
|
||||
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
[](api::ble_v2::BlePeripheral& peripheral,
|
||||
[](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {};
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StartScanning(
|
||||
@@ -171,7 +171,7 @@ TEST(BleV2Medium, DISABLED_StartThenStopScanning) {
|
||||
api::ble_v2::BleMedium::ScanningCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
[&scan_response_received, &scan_response_notification](
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {
|
||||
scan_response_received = true;
|
||||
scan_response_notification.Notify();
|
||||
|
||||
@@ -325,10 +325,10 @@ void MediumEnvironment::OnBleV2PeripheralStateChanged(
|
||||
for (auto& element : context.scan_callback_map) {
|
||||
if (element.first.first == service_id) {
|
||||
if (enabled) {
|
||||
element.second.advertisement_found_cb(peripheral,
|
||||
element.second.advertisement_found_cb(peripheral.GetUniqueId(),
|
||||
ble_advertisement_data);
|
||||
} else {
|
||||
element.second.advertisement_lost_cb(peripheral);
|
||||
element.second.advertisement_lost_cb(peripheral.GetUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,7 @@ cc_library(
|
||||
"//presence:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":internal",
|
||||
"//devtools/rust:rust_okay_here",
|
||||
"//internal/crypto",
|
||||
"//internal/crypto_cros",
|
||||
@@ -431,6 +432,7 @@ cc_test(
|
||||
":internal_test",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:mac_address",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform/implementation:comm",
|
||||
|
||||
@@ -143,7 +143,7 @@ TEST_P(BleTest, AdvertiseAndScan) {
|
||||
std::unique_ptr<ScanningSession> scanning_session = client.StartScanning(
|
||||
scan_request,
|
||||
ScanningCallback{.advertisement_found_cb =
|
||||
[&](BlePeripheral& peripheral,
|
||||
[&](BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
advertisements.push_back(advertisement_data);
|
||||
scan_latch.CountDown();
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/future.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
@@ -65,24 +66,21 @@ ScanSessionId ScanManager::StartScan(ScanRequest scan_request,
|
||||
start_scan_client(ble_status);
|
||||
},
|
||||
.advertisement_found_cb =
|
||||
[this, id](BlePeripheral& peripheral,
|
||||
[this, id](BlePeripheral::UniqueId peripheral_id,
|
||||
BleAdvertisementData data) {
|
||||
RunOnServiceControllerThread(
|
||||
"notify-found-ble",
|
||||
[this, id, data = std::move(data),
|
||||
address = peripheral.GetAddress()]()
|
||||
[this, id, data = std::move(data), peripheral_id]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
NotifyFoundBle(id, data, address);
|
||||
NotifyFoundBle(id, data, peripheral_id);
|
||||
});
|
||||
},
|
||||
.advertisement_lost_cb =
|
||||
[this, id](BlePeripheral& peripheral) {
|
||||
[this, id](BlePeripheral::UniqueId peripheral_id) {
|
||||
RunOnServiceControllerThread(
|
||||
"notify-lost-ble",
|
||||
[this, id, address = peripheral.GetAddress()]()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_) {
|
||||
NotifyLostBle(id, address);
|
||||
});
|
||||
[this, id, peripheral_id]() ABSL_EXCLUSIVE_LOCKS_REQUIRED(
|
||||
*executor_) { NotifyLostBle(id, peripheral_id); });
|
||||
}};
|
||||
FetchCredentials(id, scan_request);
|
||||
scan_sessions_.insert(
|
||||
@@ -115,7 +113,7 @@ void ScanManager::StopScan(ScanSessionId id) {
|
||||
}
|
||||
|
||||
void ScanManager::NotifyFoundBle(ScanSessionId id, BleAdvertisementData data,
|
||||
absl::string_view remote_address) {
|
||||
BlePeripheral::UniqueId peripheral_id) {
|
||||
auto it = scan_sessions_.find(id);
|
||||
if (it == scan_sessions_.end()) {
|
||||
return;
|
||||
@@ -130,12 +128,13 @@ void ScanManager::NotifyFoundBle(ScanSessionId id, BleAdvertisementData data,
|
||||
return;
|
||||
}
|
||||
|
||||
std::string remote_address = absl::StrCat(absl::Hex(peripheral_id));
|
||||
if (it->second.advertisement_filter.MatchesScanFilter(*advert)) {
|
||||
internal::DeviceIdentityMetaData device_identity_metadata;
|
||||
device_identity_metadata.set_bluetooth_mac_address(
|
||||
std::string(remote_address));
|
||||
remote_address);
|
||||
|
||||
if (!device_address_to_endpoint_id_map_.contains(remote_address)) {
|
||||
if (!device_unique_id_to_endpoint_id_map_.contains(peripheral_id)) {
|
||||
PresenceDevice device(DeviceMotion(), device_identity_metadata,
|
||||
advert->identity_type);
|
||||
// Ok if the advertisement is for trusted/private identity.
|
||||
@@ -150,13 +149,13 @@ void ScanManager::NotifyFoundBle(ScanSessionId id, BleAdvertisementData data,
|
||||
}
|
||||
}
|
||||
|
||||
device_address_to_endpoint_id_map_.emplace(remote_address,
|
||||
device_unique_id_to_endpoint_id_map_.emplace(peripheral_id,
|
||||
device.GetEndpointId());
|
||||
|
||||
it->second.callback.on_discovered_cb(std::move(device));
|
||||
} else {
|
||||
PresenceDevice device(
|
||||
device_address_to_endpoint_id_map_.at(remote_address));
|
||||
device_unique_id_to_endpoint_id_map_.at(peripheral_id));
|
||||
device.SetDeviceIdentityMetaData(device_identity_metadata);
|
||||
// Ok if the advertisement is for trusted/private identity.
|
||||
if (advert->public_credential.ok()) {
|
||||
@@ -176,21 +175,22 @@ void ScanManager::NotifyFoundBle(ScanSessionId id, BleAdvertisementData data,
|
||||
}
|
||||
|
||||
void ScanManager::NotifyLostBle(ScanSessionId id,
|
||||
absl::string_view remote_address) {
|
||||
BlePeripheral::UniqueId peripheral_id) {
|
||||
auto it = scan_sessions_.find(id);
|
||||
if (it == scan_sessions_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (device_address_to_endpoint_id_map_.contains(remote_address)) {
|
||||
std::string remote_address = absl::StrCat(absl::Hex(peripheral_id));
|
||||
if (device_unique_id_to_endpoint_id_map_.contains(peripheral_id)) {
|
||||
internal::DeviceIdentityMetaData device_identity_metadata;
|
||||
device_identity_metadata.set_bluetooth_mac_address(
|
||||
std::string(remote_address));
|
||||
PresenceDevice device(
|
||||
device_address_to_endpoint_id_map_.at(remote_address));
|
||||
device_unique_id_to_endpoint_id_map_.at(peripheral_id));
|
||||
device.SetDeviceIdentityMetaData(device_identity_metadata);
|
||||
|
||||
device_address_to_endpoint_id_map_.erase(remote_address);
|
||||
device_unique_id_to_endpoint_id_map_.erase(peripheral_id);
|
||||
|
||||
it->second.callback.on_lost_cb(std::move(device));
|
||||
}
|
||||
|
||||
@@ -81,10 +81,12 @@ class ScanManager {
|
||||
AdvertisementFilter advertisement_filter;
|
||||
std::unique_ptr<ScanningSession> scanning_session;
|
||||
};
|
||||
void NotifyFoundBle(ScanSessionId id, BleAdvertisementData data,
|
||||
absl::string_view remote_address)
|
||||
void NotifyFoundBle(
|
||||
ScanSessionId id, BleAdvertisementData data,
|
||||
nearby::api::ble_v2::BlePeripheral::UniqueId peripheral_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
|
||||
void NotifyLostBle(ScanSessionId id, absl::string_view remote_address)
|
||||
void NotifyLostBle(ScanSessionId id,
|
||||
nearby::api::ble_v2::BlePeripheral::UniqueId peripheral_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
|
||||
void FetchCredentials(ScanSessionId id, const ScanRequest& scan_request)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_);
|
||||
@@ -98,8 +100,8 @@ class ScanManager {
|
||||
CredentialManager* credential_manager_;
|
||||
absl::flat_hash_map<ScanSessionId, ScanSessionState> scan_sessions_
|
||||
ABSL_GUARDED_BY(*executor_);
|
||||
absl::flat_hash_map<std::string, std::string>
|
||||
device_address_to_endpoint_id_map_
|
||||
absl::flat_hash_map<nearby::api::ble_v2::BlePeripheral::UniqueId, std::string>
|
||||
device_unique_id_to_endpoint_id_map_
|
||||
ABSL_GUARDED_BY(*executor_);
|
||||
SingleThreadExecutor* executor_;
|
||||
};
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
@@ -31,6 +32,7 @@
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/credential_callbacks.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/single_thread_executor.h"
|
||||
#include "internal/proto/credential.proto.h"
|
||||
@@ -196,7 +198,10 @@ TEST_F(ScanManagerTest, PresenceMetadataIsRetained) {
|
||||
Ble ble2(server_adapter);
|
||||
std::unique_ptr<AdvertisingSession> advertising_session =
|
||||
StartAdvertisingOn(ble2);
|
||||
std::string address = server_adapter.GetMacAddress();
|
||||
MacAddress mac_address;
|
||||
EXPECT_TRUE(
|
||||
MacAddress::FromString(server_adapter.GetMacAddress(), mac_address));
|
||||
std::string address = absl::StrCat(absl::Hex(mac_address.address()));
|
||||
ScanCallback callback = {
|
||||
.start_scan_cb =
|
||||
[this](absl::Status status) {
|
||||
|
||||
Reference in New Issue
Block a user