From 408900e702103c8ed39af851aae3095da03c076f Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Thu, 25 Aug 2022 13:14:43 -0700 Subject: [PATCH] Define StartBroadcast API. Changed StartBroadcast() API to be more generic - the client app can use the same call to start presence, fast pair or eddystone advertisement. PiperOrigin-RevId: 470066137 --- presence/BUILD | 6 ++- presence/broadcast_request.h | 75 ++++++++++++++++++++++++++++++++ presence/presence_client.cc | 22 ++++------ presence/presence_client.h | 71 +++++++++--------------------- presence/presence_client_test.cc | 12 +++-- 5 files changed, 118 insertions(+), 68 deletions(-) create mode 100644 presence/broadcast_request.h diff --git a/presence/BUILD b/presence/BUILD index b31637c4..30f168e5 100644 --- a/presence/BUILD +++ b/presence/BUILD @@ -27,6 +27,7 @@ cc_library( deps = [ ":types", "//presence/implementation:internal", # build_cleaner: keep + "@com_google_absl//absl/functional:any_invocable", ], ) @@ -43,6 +44,7 @@ cc_library( ], hdrs = [ "broadcast_options.h", + "broadcast_request.h", "data_element.h", "device_motion.h", "discovery_filter.h", @@ -58,10 +60,11 @@ cc_library( "status.h", ], deps = [ - "//net/proto2/util/public:differencer", "//internal/platform:base", "//internal/proto:credential_cc_proto", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:optional", "@com_google_absl//absl/types:variant", "@com_google_glog//:glog", ], @@ -119,6 +122,7 @@ cc_test( shard_count = 6, deps = [ ":presence", + ":types", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", diff --git a/presence/broadcast_request.h b/presence/broadcast_request.h new file mode 100644 index 00000000..7dbb0487 --- /dev/null +++ b/presence/broadcast_request.h @@ -0,0 +1,75 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_PRESENCE_BROADCAST_REQUEST_H_ +#define THIRD_PARTY_NEARBY_PRESENCE_BROADCAST_REQUEST_H_ + +#include +#include +#include + +#include "absl/types/optional.h" +#include "internal/proto/credential.pb.h" +#include "presence/data_element.h" +#include "presence/power_mode.h" + +namespace nearby { +namespace presence { + +// Broadcast parameter for presence features. +struct PresenceBroadcast { + struct BroadcastSection { + // Presence identity type. + ::nearby::internal::IdentityType identity = + ::nearby::internal::IdentityType::IDENTITY_TYPE_UNSPECIFIED; + + // Additional Data Elements. + // The Presence SDK generates: + // - Salt, + // - (Private/Trusted/Public/Provisioned) Identity, + // - TX power, + // - Advertisement signature + // Data Elements when they are required in the advertisement. Other Data + // Elements are provided by the client application. + // Nearby SDK encrypts Data ELements before broadcasting if a non-public + // `PresenceIdentity` is provided. + std::vector extended_properties; + }; + + // Account name used to select private credentials. + std::string account_name; + + std::vector sections; +}; + +// Broadcast request for legacy Android T, which needs to provide credential +// and salt in the broadcast parameters. +// TODO(b/243443813) - Support Legacy Broadcast Request +struct LegacyPresenceBroadcast {}; + +// Nearby Presence advertisement request options. +struct BroadcastRequest { + // Calibrated TX power. The broadcast recipient uses it to calculate the + // distance between both devices. + int tx_power; + + // The broadcast frequency hint. + PowerMode power_mode; + + std::variant variant; +}; + +} // namespace presence +} // namespace nearby +#endif // THIRD_PARTY_NEARBY_PRESENCE_BROADCAST_REQUEST_H_ diff --git a/presence/presence_client.cc b/presence/presence_client.cc index a1a53708..8b2c76cf 100644 --- a/presence/presence_client.cc +++ b/presence/presence_client.cc @@ -14,13 +14,15 @@ #include "presence/presence_client.h" +#include +#include + #include "presence/presence_device.h" +#include "presence/status.h" namespace nearby { namespace presence { -using ::nearby::internal::IdentityType; - void PresenceClient::StartDiscovery(const DiscoveryFilter& filter, const DiscoveryOptions& options, ResultCallback callback) {} @@ -35,17 +37,11 @@ std::vector PresenceClient::GetCachedDevices( return std::vector{}; } -void PresenceClient::StartBroadcast(const IdentityType& identity, - const std::vector& actions, - const BroadcastOptions& options, - ResultCallback callback) {} - -void PresenceClient::UpdateBroadcastActions( - const IdentityType& identity, const std::vector& actions, - ResultCallback callback) {} - -void PresenceClient::StopBroadcast(const IdentityType& identity, - ResultCallback callback) {} +std::unique_ptr PresenceClient::StartBroadcast( + const BroadcastRequest& request, const ResultCallback& callback) { + callback.result_cb({Status::Value::kError}); + return std::make_unique(); +} } // namespace presence } // namespace nearby diff --git a/presence/presence_client.h b/presence/presence_client.h index a4628a3e..1426c2bb 100644 --- a/presence/presence_client.h +++ b/presence/presence_client.h @@ -15,17 +15,26 @@ #ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_ #define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_ +#include +#include #include -#include "presence/broadcast_options.h" +#include "absl/functional/any_invocable.h" +#include "presence/broadcast_request.h" #include "presence/discovery_filter.h" #include "presence/discovery_options.h" #include "presence/listeners.h" #include "presence/presence_device.h" +#include "presence/status.h" namespace nearby { namespace presence { +// The callback of stop broadcast for client to invoke later. +struct BroadcastSession { + absl::AnyInvocable stop_broadcast_callback; +}; + /** * Interface for detecting and interacting with nearby devices that are also * part of the Presence ecosystem. @@ -91,57 +100,17 @@ class PresenceClient { */ std::vector GetCachedDevices(const DiscoveryFilter& filter); - /** - * Advertising APIs. - */ + // Advertising APIs. - /** - * Requests that an {@link PresenceIdentity} and associated {@link - * PresenceAction}s be broadcast to other devices nearby. - * - *

These actions can then be detected by those devices and trigger a - * callback to a client which as invoked {@link #startDiscovery} for the same - * action. - * - *

Clients set a {@link PresenceIdentity} to determine who should be able - * to resolve their advertisements. - * - *

One client can advertise up to one advertisement per identity. Due to - * hardware capabilities, Presence needs to rotate advertisement or release - * resources. Frequent callers might be throttled based on resource - * limitations. - * - *

The returned {@link Task} may contain the value {@link - * PresenceStatusCodes#RESOLUTION_REQUIRED}. If that is the case, client - * should call {@linkStatus#startResolutionForResult} to get the users - * consents/permissions before retrying the request. - */ - void StartBroadcast(const ::nearby::internal::IdentityType& identity, - const std::vector& actions, - const BroadcastOptions& options, ResultCallback callback); - - /** - * Updates {@link PresenceAction}s of an ongoing advertising for a - * {@link PresenceIdentity}. - * - *

The returned {@link Task} may contain the value {@link - * PresenceStatusCodes#RESOLUTION_REQUIRED}. If that is the case, client - * should call {@link Status#startResolutionForResult} to get the users - * consents/permissions before retrying the request. - */ - void UpdateBroadcastActions(const ::nearby::internal::IdentityType& identity, - const std::vector& actions, - ResultCallback callback); - - /** - * Removes an identity broadcast that was currently requested via - * {@link #startBroadcast}. - * - *

This should be invoked after the use case has been fulfilled and the - * device no longer needs remote devices to know that it is nearby. - */ - void StopBroadcast(const ::nearby::internal::IdentityType& identity, - ResultCallback callback); + // Starts broadcasting an advertisement with attributes defined by `request`. + // The advertisement is sent over BLE4.2 or BLE5.0, or both if they are + // supported by the platform. The `callback` is invoked when the + // advertisement has started. + // + // Returns a `BroadcastSession`, which can be used to stop the broadcast + // later. + std::unique_ptr StartBroadcast( + const BroadcastRequest& request, const ResultCallback& callback); }; } // namespace presence diff --git a/presence/presence_client_test.cc b/presence/presence_client_test.cc index 3db1bc6f..610d3d9e 100644 --- a/presence/presence_client_test.cc +++ b/presence/presence_client_test.cc @@ -14,9 +14,8 @@ #include "presence/presence_client.h" -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "presence/status.h" namespace nearby { namespace presence { @@ -24,7 +23,14 @@ namespace { TEST(PresenceClientTest, DefaultConstructorWorks) { PresenceClient presence_client; - presence_client.StartBroadcast({}, {}, {}, {}); + Status result = {Status::Value::kSuccess}; + ResultCallback callback = { + .result_cb = [&](Status status) { result = status; }, + }; + + presence_client.StartBroadcast({}, callback); + + EXPECT_FALSE(result.Ok()); } } // namespace