diff --git a/presence/fpp/BUILD b/presence/fpp/BUILD index f551c07b..2f12acb7 100644 --- a/presence/fpp/BUILD +++ b/presence/fpp/BUILD @@ -40,6 +40,7 @@ cc_test( srcs = ["fpp_manager_test.cc"], deps = [ ":fpp_manager", + "//presence/implementation:sensor_fusion", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/status", "@com_google_googletest//:gtest_main", diff --git a/presence/fpp/fpp_manager.cc b/presence/fpp/fpp_manager.cc index a70265a4..f0ce1ec2 100644 --- a/presence/fpp/fpp_manager.cc +++ b/presence/fpp/fpp_manager.cc @@ -17,10 +17,12 @@ #include #include #include +#include #include "absl/status/status.h" #include "internal/platform/logging.h" #include "presence/fpp/fpp_c_ffi/include/presence_detector.h" +#include "presence/implementation/sensor_fusion.h" #include "presence/presence_zone.h" namespace nearby { @@ -110,7 +112,7 @@ absl::Status FppManager::UpdateBleScanResult(uint64_t device_id, void FppManager::RegisterZoneTransitionListener( uint64_t callback_id, ZoneTransitionCallback callback) { - zone_transition_callbacks_[callback_id] = callback; + zone_transition_callbacks_[callback_id] = std::move(callback); } void FppManager::UnregisterZoneTransitionListener(uint64_t callback_id) { @@ -147,9 +149,10 @@ void FppManager::CheckPresenceZoneChanged(uint64_t device_id, NEARBY_LOG(WARNING, "Updating zone transition callbacks with new zone. Zone=%p", new_estimate.proximity_state); - for (const auto& pair : zone_transition_callbacks_) { - pair.second(device_id, ConvertProximityStateToRangeType( - new_estimate.proximity_state)); + for (auto& pair : zone_transition_callbacks_) { + pair.second.on_proximity_zone_changed( + device_id, + ConvertProximityStateToRangeType(new_estimate.proximity_state)); } } } diff --git a/presence/fpp/fpp_manager.h b/presence/fpp/fpp_manager.h index 50b616af..d13e4adb 100644 --- a/presence/fpp/fpp_manager.h +++ b/presence/fpp/fpp_manager.h @@ -31,7 +31,6 @@ namespace presence { // between fpp and NP sensor fusion class FppManager { public: - using ZoneTransitionCallback = SensorFusion::ZoneTransitionCallback; using RangeType = PresenceZone::DistanceBoundary::RangeType; FppManager() { presence_detector_handle_ = presence_detector_create(); } diff --git a/presence/fpp/fpp_manager_test.cc b/presence/fpp/fpp_manager_test.cc index b7ed5e72..76dc2fc2 100644 --- a/presence/fpp/fpp_manager_test.cc +++ b/presence/fpp/fpp_manager_test.cc @@ -19,6 +19,7 @@ #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "presence/implementation/sensor_fusion.h" namespace nearby { namespace presence { @@ -33,10 +34,12 @@ TEST(FppManager, UpdateBleScanResultSuccess) { bool callback_called = false; manager.RegisterZoneTransitionListener( kCallbackId, - [&callback_called](uint64_t device_id, - PresenceZone::DistanceBoundary::RangeType range_type) { - callback_called = true; - }); + {.on_proximity_zone_changed = + [&callback_called]( + uint64_t device_id, + PresenceZone::DistanceBoundary::RangeType range_type) { + callback_called = true; + }}); EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt, kReachRssi, /*elapsed_real_time_millis=*/0)); @@ -56,10 +59,12 @@ TEST(FppManager, ZoneTransitionDetected) { bool callback_called = false; manager.RegisterZoneTransitionListener( kCallbackId, - [&callback_called](uint64_t device_id, - PresenceZone::DistanceBoundary::RangeType range_type) { - callback_called = true; - }); + {.on_proximity_zone_changed = + [&callback_called]( + uint64_t device_id, + PresenceZone::DistanceBoundary::RangeType range_type) { + callback_called = true; + }}); // ProximityEstimate is only computed after consecutive scans is fulfilled EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt, kReachRssi, @@ -139,10 +144,12 @@ TEST(FppManager, UpdateBleScanResultWithTxPowerSuccess) { bool callback_called = false; manager.RegisterZoneTransitionListener( kCallbackId, - [&callback_called](uint64_t device_id, - PresenceZone::DistanceBoundary::RangeType range_type) { - callback_called = true; - }); + {.on_proximity_zone_changed = + [&callback_called]( + uint64_t device_id, + PresenceZone::DistanceBoundary::RangeType range_type) { + callback_called = true; + }}); EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/20, kReachRssi, /*elapsed_real_time_millis=*/0)); EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/20, kReachRssi, @@ -159,10 +166,12 @@ TEST(FppManager, UnregisterZoneTransitionListener) { bool callback_called = false; manager.RegisterZoneTransitionListener( kCallbackId, - [&callback_called](uint64_t device_id, - PresenceZone::DistanceBoundary::RangeType range_type) { - callback_called = true; - }); + {.on_proximity_zone_changed = + [&callback_called]( + uint64_t device_id, + PresenceZone::DistanceBoundary::RangeType range_type) { + callback_called = true; + }}); EXPECT_OK(manager.UpdateBleScanResult(kDeviceId, /*txPower=*/absl::nullopt, kReachRssi, /*elapsed_real_time_millis=*/0)); diff --git a/presence/implementation/BUILD b/presence/implementation/BUILD index 97fc31b2..a3da0f81 100644 --- a/presence/implementation/BUILD +++ b/presence/implementation/BUILD @@ -126,6 +126,7 @@ cc_library( ], deps = [ "//presence:types", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/types:optional", ], ) diff --git a/presence/implementation/sensor_fusion.h b/presence/implementation/sensor_fusion.h index 452b01fd..027f2225 100644 --- a/presence/implementation/sensor_fusion.h +++ b/presence/implementation/sensor_fusion.h @@ -19,6 +19,7 @@ #include #include +#include "absl/functional/any_invocable.h" #include "absl/types/optional.h" #include "presence/device_motion.h" #include "presence/presence_zone.h" @@ -58,16 +59,21 @@ struct RangingData { std::vector device_motions; }; +struct ZoneTransitionCallback { + absl::AnyInvocable + on_proximity_zone_changed = + [](uint64_t device_id, + PresenceZone::DistanceBoundary::RangeType proximity_zone) {}; + absl::AnyInvocable on_callback_id_generated = + [](uint64_t callback_id) {}; +}; + class SensorFusion { public: virtual ~SensorFusion() = default; - // Called when the proximity zone to a nearby peer device has changed. - typedef std::function - ZoneTransitionCallback; - // Called when a device motion gesture is detected. typedef std::function DeviceMotionCallback;