Add default callbacks in broadcast & scan operations

PiperOrigin-RevId: 484029311
This commit is contained in:
Hai Shang
2022-10-26 11:45:28 -07:00
committed by Copybara-Service
parent 2206f21c3f
commit 04b888ace4
3 changed files with 32 additions and 11 deletions
+23 -7
View File
@@ -25,33 +25,49 @@ namespace presence {
// Holds the callback of stop scan for client to invoke later.
struct ScanSession {
std::function<void(Status)> 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<Status(void)> 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<void(Status)> start_scan_cb;
std::function<void(Status)> start_scan_cb = [](Status) {};
// Reports a {@link PresenceDevice} being discovered.
std::function<void(PresenceDevice)> on_discovered_cb;
std::function<void(PresenceDevice)> on_discovered_cb = [](PresenceDevice) {};
// Reports a {@link PresenceDevice} information(distance, and etc)
// changed.
std::function<void(PresenceDevice)> on_updated_cb;
std::function<void(PresenceDevice)> on_updated_cb = [](PresenceDevice) {};
// Reports a {@link PresenceDevice} is no longer within range.
std::function<void(PresenceDevice)> on_lost_cb;
std::function<void(PresenceDevice)> on_lost_cb = [](PresenceDevice) {};
};
/**
* Holds the callback of stop broadcast for client to invoke later.
*/
struct BroadcastSession {
std::function<void(Status)> 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<Status(void)> 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<void(Status)> start_broadcast_cb;
std::function<void(Status)> start_broadcast_cb = [](Status) {};
};
} // namespace presence
+8 -4
View File
@@ -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
+1
View File
@@ -26,6 +26,7 @@ struct Status {
enum class Value {
kError = 0,
kSuccess,
kNotImplemented,
};
Value value{Value::kError};
bool Ok() const { return value == Value::kSuccess; }