Skip to content

Commit

Permalink
Create PresenceDeviceProvider
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 528913199
  • Loading branch information
anayw2001 authored and copybara-github committed May 2, 2023
1 parent 6b25d43 commit 21d2efb
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 11 deletions.
5 changes: 4 additions & 1 deletion connections/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <functional>
#include <string>
#include <type_traits>

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
Expand Down Expand Up @@ -507,7 +508,9 @@ class Core {

// Registers a DeviceProvider to provide functionality for Nearby Connections
// to interact with the DeviceProvider for retrieving the local device.
void RegisterDeviceProvider(NearbyDeviceProvider&& provider);
template <typename T, typename std::enable_if<std::is_base_of<
NearbyDevice, T>::value>::type* = nullptr>
void RegisterDeviceProvider(NearbyDeviceProvider<T>* provider);

private:
ClientProxy client_;
Expand Down
1 change: 1 addition & 0 deletions internal/interop/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cc_library(
],
deps = [
"//internal/platform:connection_info",
"//internal/platform:types",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:variant",
],
Expand Down
4 changes: 0 additions & 4 deletions internal/interop/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class NearbyDevice {
};
NearbyDevice() = default;
virtual ~NearbyDevice() = default;
NearbyDevice(NearbyDevice&&) = default;
NearbyDevice& operator=(NearbyDevice&&) = default;
NearbyDevice(const NearbyDevice&) = delete;
NearbyDevice& operator=(const NearbyDevice&) = delete;
virtual std::string GetEndpointId() const = 0;
// We will be adding more ConnectionInfo types to this variant as they are
// implemented.
Expand Down
5 changes: 4 additions & 1 deletion internal/interop/device_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ namespace nearby {
// The base device provider class for use with the Nearby Connections V3 APIs.
// This class currently provides a function to get the local device for whatever
// client implements it.
template <typename T, typename std::enable_if<std::is_base_of<
::nearby::NearbyDevice, T>::value>::type* = nullptr>
class NearbyDeviceProvider {
public:
virtual ~NearbyDeviceProvider() = default;

virtual NearbyDevice* GetLocalDevice() = 0;
const virtual T& GetLocalDevice() = 0;
};
} // namespace nearby

Expand Down
1 change: 1 addition & 0 deletions internal/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ cc_library(
"//fastpair:__subpackages__",
"//internal/base:__subpackages__",
"//internal/flags:__subpackages__",
"//internal/interop:__pkg__",
"//internal/platform/implementation/windows:__subpackages__",
"//internal/preferences:__subpackages__",
"//internal/test:__subpackages__",
Expand Down
8 changes: 8 additions & 0 deletions presence/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ cc_library(
],
hdrs = [
"presence_client.h",
"presence_device_provider.h",
"presence_service.h",
],
deps = [
":types",
"//internal/interop:device",
"//internal/platform:types",
"//internal/proto:metadata_cc_proto",
"//presence/implementation:internal", # build_cleaner: keep
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down Expand Up @@ -64,6 +67,7 @@ cc_library(
"//internal/interop:device",
"//internal/platform:connection_info",
"//internal/platform:logging",
"//internal/platform:types",
"//internal/platform/implementation:types",
"//internal/proto:credential_cc_proto",
"//internal/proto:metadata_cc_proto",
Expand Down Expand Up @@ -138,15 +142,19 @@ cc_test(
size = "small",
srcs = [
"presence_client_test.cc",
"presence_device_provider_test.cc",
"presence_service_test.cc",
],
shard_count = 6,
deps = [
":presence",
":types",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/proto:metadata_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
] + select({
"//tools/cc_target_os:windows": [
Expand Down
1 change: 1 addition & 0 deletions presence/implementation/service_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>

#include "absl/status/statusor.h"
#include "internal/proto/metadata.pb.h"
#include "presence/broadcast_request.h"
#include "presence/data_types.h"
#include "presence/scan_request.h"
Expand Down
2 changes: 2 additions & 0 deletions presence/implementation/service_controller_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <utility>

#include "absl/status/statusor.h"
#include "internal/proto/metadata.pb.h"
#include "presence/implementation/broadcast_manager.h"
#include "presence/implementation/credential_manager_impl.h"
#include "presence/implementation/mediums/mediums.h"
Expand Down Expand Up @@ -52,6 +53,7 @@ class ServiceControllerImpl : public ServiceController {
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb) override;

::nearby::internal::Metadata GetLocalDeviceMetadata() override {
return credential_manager_.GetLocalDeviceMetadata();
}
Expand Down
12 changes: 10 additions & 2 deletions presence/presence_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

#include "presence/presence_client.h"

#include <memory>
#include <optional>
#include <utility>
#include <vector>

#include "absl/status/status.h"
#include "internal/platform/borrowable.h"
#include "internal/platform/logging.h"
#include "presence/presence_device.h"
#include "presence/presence_service.h"

namespace nearby {
Expand Down Expand Up @@ -62,5 +62,13 @@ void PresenceClient::StopBroadcast(BroadcastSessionId session_id) {
}
}

std::optional<PresenceDevice> PresenceClient::GetLocalDevice() {
::nearby::Borrowed<PresenceService*> borrowed = service_.Borrow();
if (borrowed) {
return (*borrowed)->GetLocalDeviceProvider()->GetLocalDevice();
}
return std::nullopt;
}

} // namespace presence
} // namespace nearby
6 changes: 6 additions & 0 deletions presence/presence_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "internal/platform/borrowable.h"
#include "presence/broadcast_request.h"
#include "presence/data_types.h"
#include "presence/presence_device.h"
#include "presence/scan_request.h"

namespace nearby {
Expand Down Expand Up @@ -78,6 +79,11 @@ class PresenceClient {
// terminated.
void StopBroadcast(BroadcastSessionId session_id);

// Returns the local PresenceDevice describing the current device's actions,
// connectivity info and unique identifier for use in Connections and
// Presence.
std::optional<PresenceDevice> GetLocalDevice();

private:
BorrowablePresenceService service_;
};
Expand Down
32 changes: 32 additions & 0 deletions presence/presence_client_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,36 @@
#include "absl/status/status.h"
#include "internal/platform/medium_environment.h"
#include "presence/data_types.h"
#include "presence/presence_device.h"
#include "presence/presence_service.h"

namespace nearby {
namespace presence {
namespace {

using ::nearby::internal::Metadata;
using ::testing::status::StatusIs;

constexpr absl::string_view kMacAddr = "\x4C\x8B\x1D\xCE\xBA\xD1";

// Creates a PresenceClient and destroys PresenceService that was used to create
// it.
PresenceClient CreateDefunctPresenceClient() {
PresenceService presence_service;
return presence_service.CreatePresenceClient();
}

Metadata CreateTestMetadata() {
Metadata metadata;
metadata.set_device_type(internal::DEVICE_TYPE_PHONE);
metadata.set_account_name("test_account");
metadata.set_device_name("NP test device");
metadata.set_user_name("Test user");
metadata.set_device_profile_url("test_image.test.com");
metadata.set_bluetooth_mac_address(kMacAddr);
return metadata;
}

class PresenceClientTest : public testing::Test {
protected:
nearby::MediumEnvironment& env_{nearby::MediumEnvironment::Instance()};
Expand Down Expand Up @@ -104,6 +119,23 @@ TEST_F(PresenceClientTest, StartScanFailsWhenPresenceServiceIsGone) {
env_.Stop();
}

TEST_F(PresenceClientTest, GettingDeviceWorks) {
PresenceService presence_service;
PresenceClient presence_client = presence_service.CreatePresenceClient();
presence_service.UpdateLocalDeviceMetadata(CreateTestMetadata(), false, "",
{}, 0, 0, {});
auto device = presence_client.GetLocalDevice();
ASSERT_NE(device, std::nullopt);
EXPECT_EQ(device->GetEndpointId().length(), kEndpointIdLength);
EXPECT_EQ(device->GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
}

TEST_F(PresenceClientTest, TestGettingDeviceDefunct) {
PresenceClient presence_client = CreateDefunctPresenceClient();
auto device = presence_client.GetLocalDevice();
EXPECT_EQ(device, std::nullopt);
}
} // namespace
} // namespace presence
} // namespace nearby
7 changes: 5 additions & 2 deletions presence/presence_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ class PresenceDevice : public nearby::NearbyDevice {
const override;
DeviceMotion GetDeviceMotion() const { return device_motion_; }
Metadata GetMetadata() const { return metadata_; }
void SetMetadata(const Metadata metadata) { metadata_ = metadata; }
absl::Time GetDiscoveryTimestamp() const { return discovery_timestamp_; }

private:
const absl::Time discovery_timestamp_;
const DeviceMotion device_motion_;
const Metadata metadata_;
Metadata metadata_;
std::vector<DataElement> extended_properties_;
std::vector<PresenceAction> actions_;
std::string endpoint_id_;
Expand All @@ -77,7 +78,9 @@ class PresenceDevice : public nearby::NearbyDevice {
inline bool operator==(const PresenceDevice& d1, const PresenceDevice& d2) {
return d1.GetDeviceMotion() == d2.GetDeviceMotion() &&
d1.GetMetadata().SerializeAsString() ==
d2.GetMetadata().SerializeAsString();
d2.GetMetadata().SerializeAsString() &&
d1.GetActions() == d2.GetActions() &&
d1.GetExtendedProperties() == d2.GetExtendedProperties();
}

inline bool operator!=(const PresenceDevice& d1, const PresenceDevice& d2) {
Expand Down
42 changes: 42 additions & 0 deletions presence/presence_device_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 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_PRESENCE_DEVICE_PROVIDER_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_PROVIDER_H_

#include "internal/interop/device_provider.h"
#include "internal/proto/metadata.proto.h"
#include "presence/presence_device.h"

namespace nearby {
namespace presence {

class PresenceDeviceProvider : public NearbyDeviceProvider<PresenceDevice> {
public:
explicit PresenceDeviceProvider(::nearby::internal::Metadata metadata)
: device_{metadata} {}

const PresenceDevice& GetLocalDevice() override { return device_; }

void UpdateMetadata(const ::nearby::internal::Metadata& metadata) {
device_.SetMetadata(metadata);
}

private:
PresenceDevice device_;
};
} // namespace presence
} // namespace nearby

#endif // THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_PROVIDER_H_
72 changes: 72 additions & 0 deletions presence/presence_device_provider_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 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.

#include "presence/presence_device_provider.h"

#include <type_traits>

#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/proto/metadata.pb.h"
#include "internal/proto/metadata.proto.h"
#include "presence/presence_device.h"

namespace nearby {
namespace presence {
namespace {
using ::nearby::internal::Metadata;

constexpr absl::string_view kMacAddr = "\x4C\x8B\x1D\xCE\xBA\xD1";

Metadata CreateTestMetadata() {
Metadata metadata;
metadata.set_device_type(internal::DEVICE_TYPE_PHONE);
metadata.set_account_name("test_account");
metadata.set_device_name("NP test device");
metadata.set_user_name("Test user");
metadata.set_device_profile_url("test_image.test.com");
metadata.set_bluetooth_mac_address(kMacAddr);
return metadata;
}

TEST(PresenceDeviceProviderTest, ProviderIsNotTriviallyConstructible) {
EXPECT_FALSE(std::is_trivially_constructible<PresenceDeviceProvider>::value);
}

TEST(PresenceDeviceProviderTest, DeviceProviderWorks) {
PresenceDeviceProvider provider(CreateTestMetadata());
auto device = provider.GetLocalDevice();
EXPECT_EQ(device.GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
}

TEST(PresenceDeviceProviderTest, DeviceProviderCanUpdateDevice) {
PresenceDeviceProvider provider(CreateTestMetadata());
auto device = provider.GetLocalDevice();
EXPECT_EQ(device.GetMetadata().SerializeAsString(),
CreateTestMetadata().SerializeAsString());
Metadata new_metadata = CreateTestMetadata();
new_metadata.set_device_name("NP interop device");
provider.UpdateMetadata(new_metadata);
EXPECT_NE(device.GetMetadata().SerializeAsString(),
new_metadata.SerializeAsString());
EXPECT_EQ(provider.GetLocalDevice().GetMetadata().SerializeAsString(),
new_metadata.SerializeAsString());
}

} // namespace
} // namespace presence
} // namespace nearby
5 changes: 4 additions & 1 deletion presence/presence_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
namespace nearby {
namespace presence {
PresenceService::PresenceService() {
this->service_controller_ = std::make_unique<ServiceControllerImpl>();
service_controller_ = std::make_unique<ServiceControllerImpl>();
provider_ = std::make_unique<PresenceDeviceProvider>(
service_controller_->GetLocalDeviceMetadata());
}

PresenceClient PresenceService::CreatePresenceClient() {
Expand All @@ -35,6 +37,7 @@ absl::StatusOr<ScanSessionId> PresenceService::StartScan(
ScanRequest scan_request, ScanCallback callback) {
return service_controller_->StartScan(scan_request, std::move(callback));
}

void PresenceService::StopScan(ScanSessionId id) {
service_controller_->StopScan(id);
}
Expand Down
Loading

0 comments on commit 21d2efb

Please sign in to comment.