Implement NearbyDevice for PresenceDevice.

PiperOrigin-RevId: 491705794
This commit is contained in:
Anay Wadhera
2022-11-29 12:19:47 -08:00
committed by Copybara-Service
parent 82f35a1401
commit b7415ecd40
7 changed files with 90 additions and 18 deletions
+2 -2
View File
@@ -8,8 +8,8 @@ cc_library(
"//presence:__subpackages__",
],
deps = [
"//internal/platform:base",
"//internal/platform:connection_info",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:variant",
],
)
+11 -5
View File
@@ -16,9 +16,11 @@
#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_DEVICE_H_
#include <string>
#include <vector>
#include "absl/status/statusor.h"
#include "internal/platform/connection_info.h"
#include "absl/strings/string_view.h"
#include "absl/types/variant.h"
#include "internal/platform/bluetooth_connection_info.h"
namespace location {
namespace nearby {
@@ -30,14 +32,18 @@ class NearbyDevice {
kConnectionsDevice = 1,
kPresenceDevice = 2,
};
NearbyDevice() = default;
virtual ~NearbyDevice() = default;
NearbyDevice(NearbyDevice&&) = default;
NearbyDevice& operator=(NearbyDevice&&) = default;
NearbyDevice(const NearbyDevice&) = delete;
NearbyDevice& operator=(const NearbyDevice&) = delete;
virtual absl::StatusOr<std::string> GetEndpointId() const = 0;
virtual std::string GetEndpointInfo() const = 0;
virtual absl::Span<ConnectionInfo*> GetConnectionInfos() const = 0;
virtual absl::string_view GetEndpointId() const = 0;
virtual absl::string_view GetEndpointInfo() const = 0;
// We will be adding more ConnectionInfo types to this variant as they are
// implemented.
virtual std::vector<absl::variant<BluetoothConnectionInfo>>
GetConnectionInfos() const = 0;
virtual Type GetType() const { return Type::kUnknownDevice; }
};
+6
View File
@@ -61,7 +61,10 @@ cc_library(
"status.h",
],
deps = [
"//internal:device",
"//internal/crypto",
"//internal/platform:base",
"//internal/platform:connection_info",
"//internal/platform:logging",
"//internal/platform/implementation:types",
"//internal/proto:credential_cc_proto",
@@ -92,11 +95,14 @@ cc_test(
shard_count = 6,
deps = [
":types",
"//internal/platform:connection_info",
"//internal/platform:logging",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/proto:credential_cc_proto",
"//internal/proto:device_metadata_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:variant",
"@com_google_googletest//:gtest_main",
],
)
+2 -2
View File
@@ -102,7 +102,7 @@ std::unique_ptr<ScanSession> ScanManager::StartScan(ScanRequest scan_request,
void ScanManager::NotifyFoundBle(BleAdvertisementData data,
const BlePeripheral& peripheral) {
std::vector<std::pair<ScanCallback, DeviceMetadata>> callbacks;
std::vector<std::pair<ScanCallback, internal::DeviceMetadata>> callbacks;
{
absl::MutexLock lock(&mutex_);
auto advertisement_data =
@@ -116,7 +116,7 @@ void ScanManager::NotifyFoundBle(BleAdvertisementData data,
}
if (candidate.decoder.MatchesScanFilter(advert.value())) {
std::string bt_addr = peripheral.GetAddress();
DeviceMetadata metadata;
internal::DeviceMetadata metadata;
metadata.set_bluetooth_mac_address(bt_addr);
callbacks.push_back({candidate.callback, metadata});
}
+24 -2
View File
@@ -14,21 +14,43 @@
#include "presence/presence_device.h"
#include <string>
#include <vector>
#include "internal/crypto/random.h"
#include "internal/platform/bluetooth_connection_info.h"
#include "internal/platform/implementation/system_clock.h"
#include "presence/device_motion.h"
namespace nearby {
namespace presence {
namespace {
std::string GenerateRandomEndpointId() {
return crypto::RandBytes(kEndpointIdLength);
}
} // namespace
PresenceDevice::PresenceDevice(DeviceMetadata device_metadata) noexcept
: discovery_timestamp_(location::nearby::SystemClock::ElapsedRealtime()),
device_motion_(DeviceMotion()),
device_metadata_(device_metadata) {}
device_metadata_(device_metadata) {
endpoint_id_ = GenerateRandomEndpointId();
}
PresenceDevice::PresenceDevice(DeviceMotion device_motion,
DeviceMetadata device_metadata) noexcept
: discovery_timestamp_(location::nearby::SystemClock::ElapsedRealtime()),
device_motion_(device_motion),
device_metadata_(device_metadata) {}
device_metadata_(device_metadata) {
endpoint_id_ = GenerateRandomEndpointId();
}
std::vector<absl::variant<location::nearby::BluetoothConnectionInfo>>
PresenceDevice::GetConnectionInfos() const {
location::nearby::BluetoothConnectionInfo bluetooth_connection_info(
location::nearby::ByteArray(device_metadata_.bluetooth_mac_address()),
"Nearby Presence");
return {bluetooth_connection_info};
}
} // namespace presence
} // namespace nearby
+22 -2
View File
@@ -15,22 +15,40 @@
#ifndef THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_H_
#define THIRD_PARTY_NEARBY_PRESENCE_PRESENCE_DEVICE_H_
#include <memory>
#include <string>
#include <vector>
#include "absl/time/time.h"
#include "absl/types/variant.h"
#include "internal/device.h"
#include "internal/platform/bluetooth_connection_info.h"
#include "internal/proto/device_metadata.pb.h"
#include "presence/device_motion.h"
namespace nearby {
namespace presence {
using nearby::internal::DeviceMetadata;
constexpr int kEndpointIdLength = 4;
class PresenceDevice : public location::nearby::NearbyDevice {
using DeviceMetadata = ::nearby::internal::DeviceMetadata;
class PresenceDevice {
public:
explicit PresenceDevice(DeviceMetadata metadata) noexcept;
explicit PresenceDevice(DeviceMotion device_motion,
DeviceMetadata metadata) noexcept;
absl::string_view GetEndpointId() const override { return endpoint_id_; };
void SetEndpointInfo(absl::string_view endpoint_info) {
endpoint_info_ = std::string(endpoint_info);
}
absl::string_view GetEndpointInfo() const override { return endpoint_info_; }
NearbyDevice::Type GetType() const override {
return NearbyDevice::Type::kPresenceDevice;
}
// Add more medium ConnectionInfos as we introduce them.
std::vector<absl::variant<location::nearby::BluetoothConnectionInfo>>
GetConnectionInfos() const override;
DeviceMotion GetDeviceMotion() const { return device_motion_; }
DeviceMetadata GetMetadata() const { return device_metadata_; }
absl::Time GetDiscoveryTimestamp() const { return discovery_timestamp_; }
@@ -39,6 +57,8 @@ class PresenceDevice {
const absl::Time discovery_timestamp_;
const DeviceMotion device_motion_;
const DeviceMetadata device_metadata_;
std::string endpoint_id_;
std::string endpoint_info_;
};
// Timestamp is not used for equality since if the same device is discovered
+23 -5
View File
@@ -14,14 +14,22 @@
#include "presence/presence_device.h"
#include <memory>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/types/variant.h"
#include "internal/platform/bluetooth_connection_info.h"
#include "internal/platform/logging.h"
#include "internal/proto/device_metadata.proto.h"
namespace nearby {
namespace presence {
namespace {
using internal::DeviceMetadata;
constexpr DeviceMotion::MotionType kDefaultMotionType =
DeviceMotion::MotionType::kPointAndHold;
constexpr float kDefaultConfidence = 0;
@@ -63,12 +71,22 @@ TEST(PresenceDeviceTest, ExplicitInitNotEquals) {
EXPECT_NE(device1, device2);
}
TEST(PresenceDeviceTest, CopyInitEquals) {
TEST(PresenceDeviceTest, TestGetBluetoothAddress) {
DeviceMetadata metadata = CreateTestDeviceMetadata();
PresenceDevice device1 =
PresenceDevice({kDefaultMotionType, kTestConfidence}, metadata);
PresenceDevice device2 = {device1};
EXPECT_EQ(device1, device2);
PresenceDevice device = PresenceDevice({kDefaultMotionType}, metadata);
auto info = (device.GetConnectionInfos().at(0));
ASSERT_TRUE(
absl::holds_alternative<location::nearby::BluetoothConnectionInfo>(info));
EXPECT_EQ(absl::get<location::nearby::BluetoothConnectionInfo>(info)
.GetMacAddress()
.AsStringView(),
kMacAddr);
}
TEST(PresenceDeviceTest, TestEndpointIdIsCorrectLength) {
DeviceMetadata metadata = CreateTestDeviceMetadata();
PresenceDevice device = PresenceDevice({kDefaultMotionType}, metadata);
EXPECT_EQ(device.GetEndpointId().length(), kEndpointIdLength);
}
} // namespace