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
This commit is contained in:
Janusz Sobczak
2022-08-25 13:16:17 -07:00
committed by Copybara-Service
parent 40a9f13938
commit 408900e702
5 changed files with 118 additions and 68 deletions
+5 -1
View File
@@ -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",
+75
View File
@@ -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 <string>
#include <variant>
#include <vector>
#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<DataElement> extended_properties;
};
// Account name used to select private credentials.
std::string account_name;
std::vector<BroadcastSection> 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<PresenceBroadcast, LegacyPresenceBroadcast> variant;
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_BROADCAST_REQUEST_H_
+9 -13
View File
@@ -14,13 +14,15 @@
#include "presence/presence_client.h"
#include <memory>
#include <vector>
#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<PresenceDevice> PresenceClient::GetCachedDevices(
return std::vector<PresenceDevice>{};
}
void PresenceClient::StartBroadcast(const IdentityType& identity,
const std::vector<PresenceAction>& actions,
const BroadcastOptions& options,
ResultCallback callback) {}
void PresenceClient::UpdateBroadcastActions(
const IdentityType& identity, const std::vector<PresenceAction>& actions,
ResultCallback callback) {}
void PresenceClient::StopBroadcast(const IdentityType& identity,
ResultCallback callback) {}
std::unique_ptr<BroadcastSession> PresenceClient::StartBroadcast(
const BroadcastRequest& request, const ResultCallback& callback) {
callback.result_cb({Status::Value::kError});
return std::make_unique<BroadcastSession>();
}
} // namespace presence
} // namespace nearby
+20 -51
View File
@@ -15,17 +15,26 @@
#ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_CLIENT_H_
#include <functional>
#include <memory>
#include <vector>
#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<void(Status)> 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<PresenceDevice> GetCachedDevices(const DiscoveryFilter& filter);
/**
* Advertising APIs.
*/
// Advertising APIs.
/**
* Requests that an {@link PresenceIdentity} and associated {@link
* PresenceAction}s be broadcast to other devices nearby.
*
* <p>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.
*
* <p>Clients set a {@link PresenceIdentity} to determine who should be able
* to resolve their advertisements.
*
* <p>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.
*
* <p>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<PresenceAction>& actions,
const BroadcastOptions& options, ResultCallback callback);
/**
* Updates {@link PresenceAction}s of an ongoing advertising for a
* {@link PresenceIdentity}.
*
* <p>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<PresenceAction>& actions,
ResultCallback callback);
/**
* Removes an identity broadcast that was currently requested via
* {@link #startBroadcast}.
*
* <p>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<BroadcastSession> StartBroadcast(
const BroadcastRequest& request, const ResultCallback& callback);
};
} // namespace presence
+9 -3
View File
@@ -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