From 04b888ace4714c1bf1975a5c4e979895d19231ab Mon Sep 17 00:00:00 2001 From: Hai Shang Date: Wed, 26 Oct 2022 11:44:01 -0700 Subject: [PATCH] Add default callbacks in broadcast & scan operations PiperOrigin-RevId: 484029311 --- presence/data_types.h | 30 +++++++++++++++++++++++------- presence/presence_service_test.cc | 12 ++++++++---- presence/status.h | 1 + 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/presence/data_types.h b/presence/data_types.h index 37d2dd02..7deaa8a8 100644 --- a/presence/data_types.h +++ b/presence/data_types.h @@ -25,33 +25,49 @@ namespace presence { // Holds the callback of stop scan for client to invoke later. struct ScanSession { - std::function stop_scan_callback; + // Nearby library would provide the implementation of this callback in + // runtime. Assigning with a default value NotImplemented to surface potential + // issue where library failed to provide the implementation. + std::function stop_scan_callback = []() { + return Status{Status::Value::kNotImplemented}; + }; }; +// Callers would provide the implementation of these callbacks. If callers +// don't need these signal updates, they can skip with the provided default +// empty functions. struct ScanCallback { // Updates client with the result of start scanning. - std::function start_scan_cb; + std::function start_scan_cb = [](Status) {}; // Reports a {@link PresenceDevice} being discovered. - std::function on_discovered_cb; + std::function on_discovered_cb = [](PresenceDevice) {}; // Reports a {@link PresenceDevice} information(distance, and etc) // changed. - std::function on_updated_cb; + std::function on_updated_cb = [](PresenceDevice) {}; // Reports a {@link PresenceDevice} is no longer within range. - std::function on_lost_cb; + std::function on_lost_cb = [](PresenceDevice) {}; }; /** * Holds the callback of stop broadcast for client to invoke later. */ struct BroadcastSession { - std::function stop_broadcast_callback; + // Nearby library would provide the implementation of this callback in + // runtime. Assiging with a default value NotImplemented to surface potential + // issue where library failed to provide the implementation. + std::function stop_broadcast_callback = []() { + return Status{Status::Value::kNotImplemented}; + }; }; +// Callers would provide the implementation of these callbacks. If callers +// don't need these signal updates, they can skip with the provided default +// empty functions. struct BroadcastCallback { - std::function start_broadcast_cb; + std::function start_broadcast_cb = [](Status) {}; }; } // namespace presence diff --git a/presence/presence_service_test.cc b/presence/presence_service_test.cc index a71ab736..806d4f16 100644 --- a/presence/presence_service_test.cc +++ b/presence/presence_service_test.cc @@ -33,11 +33,15 @@ TEST(PresenceServiceTest, StartScan) { PresenceService presence_service; PresenceClient client = presence_service.CreatePresenceClient(); - auto scan_session = client.StartScan({}, { - .start_scan_cb = [&](Status status) { scan_result = status; }, - }); + auto scan_session = client.StartScan( + {}, { + .start_scan_cb = [&](Status status) { scan_result = status; }, + }); + auto scan_session_with_default_params = + client.StartScan(ScanRequest(), ScanCallback()); - ASSERT_EQ(scan_session, nullptr); + EXPECT_EQ(scan_session, nullptr); + EXPECT_EQ(scan_session_with_default_params, nullptr); } } // namespace diff --git a/presence/status.h b/presence/status.h index 84713fe0..0506238d 100644 --- a/presence/status.h +++ b/presence/status.h @@ -26,6 +26,7 @@ struct Status { enum class Value { kError = 0, kSuccess, + kNotImplemented, }; Value value{Value::kError}; bool Ok() const { return value == Value::kSuccess; }