Use AnyInvocable in DiscoveredPeripheralCallback

PiperOrigin-RevId: 564523999
This commit is contained in:
Janusz Sobczak
2023-09-11 15:48:25 -07:00
committed by Copybara-Service
parent 533e145097
commit 52c3360bd3
4 changed files with 15 additions and 16 deletions
@@ -39,7 +39,6 @@ cc_library(
"//connections/implementation:__subpackages__",
],
deps = [
"//connections:core_types",
"//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums:utils",
"//internal/flags:nearby_flags",
@@ -53,6 +52,7 @@ cc_library(
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/numeric:int128",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
@@ -15,9 +15,9 @@
#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_
#define CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_
#include <functional>
#include <string>
#include "absl/functional/any_invocable.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h"
@@ -27,14 +27,14 @@ namespace mediums {
// Callback that is invoked when a {@link BlePeripheral} is discovered.
struct DiscoveredPeripheralCallback {
std::function<void(BleV2Peripheral peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement)>
absl::AnyInvocable<void(
BleV2Peripheral peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes, bool fast_advertisement)>
peripheral_discovered_cb =
[](BleV2Peripheral, const std::string&, const ByteArray&, bool) {};
std::function<void(BleV2Peripheral peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement)>
absl::AnyInvocable<void(
BleV2Peripheral peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes, bool fast_advertisement)>
peripheral_lost_cb =
[](BleV2Peripheral, const std::string&, const ByteArray&, bool) {};
};
@@ -28,6 +28,7 @@
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/ble_v2.h"
#include "internal/platform/byte_array.h"
@@ -66,7 +67,7 @@ DiscoveredPeripheralTracker::~DiscoveredPeripheralTracker() {
void DiscoveredPeripheralTracker::StartTracking(
const std::string& service_id,
const DiscoveredPeripheralCallback& discovered_peripheral_callback,
DiscoveredPeripheralCallback discovered_peripheral_callback,
const Uuid& fast_advertisement_service_uuid) {
MutexLock lock(&mutex_);
@@ -129,11 +130,9 @@ void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement(
void DiscoveredPeripheralTracker::ProcessLostGattAdvertisements() {
MutexLock lock(&mutex_);
for (const auto& it : service_id_infos_) {
for (auto& it : service_id_infos_) {
const std::string& service_id = it.first;
const ServiceIdInfo& service_id_info = it.second;
DiscoveredPeripheralCallback discovered_peripheral_callback =
service_id_info.discovered_peripheral_callback;
ServiceIdInfo& service_id_info = it.second;
BleAdvertisementSet lost_gatt_advertisements =
service_id_info.lost_entity_tracker->ComputeLostEntities();
@@ -145,7 +144,7 @@ void DiscoveredPeripheralTracker::ProcessLostGattAdvertisements() {
BleV2Peripheral lost_peripheral = it->second.peripheral;
if (lost_peripheral.IsValid()) {
lost_peripheral.SetId(ByteArray(gatt_advertisement));
discovered_peripheral_callback.peripheral_lost_cb(
service_id_info.discovered_peripheral_callback.peripheral_lost_cb(
std::move(lost_peripheral), service_id,
gatt_advertisement.GetData(),
gatt_advertisement.IsFastAdvertisement());
@@ -525,7 +524,7 @@ void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
ByteArray advertisement_data{advertisement_header};
if (fetching_advertisements_.contains(advertisement_data)) {
NEARBY_LOGS(VERBOSE) << ": Ignore the advertisement header due to it "
"is already in fetcing.";
"is already in fetching.";
return;
}
@@ -79,7 +79,7 @@ class DiscoveredPeripheralTracker {
// advertisement.
void StartTracking(
const std::string& service_id,
const DiscoveredPeripheralCallback& discovered_peripheral_callback,
DiscoveredPeripheralCallback discovered_peripheral_callback,
const Uuid& fast_advertisement_service_uuid) ABSL_LOCKS_EXCLUDED(mutex_);
// Stops tracking discoveries for a particular service Id.