mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Implement BleConnectionInfo as a container for BLE mac address.
Move PresenceDevice to use BleConnectionInfo. PiperOrigin-RevId: 494023973
This commit is contained in:
committed by
Copybara-Service
parent
c5bb844509
commit
82b2c63a7c
@@ -501,6 +501,7 @@ let package = Package(
|
||||
"internal/platform/atomic_reference_test.cc",
|
||||
"internal/platform/logging_test.cc",
|
||||
"internal/platform/multi_thread_executor_test.cc",
|
||||
"internal/platform/ble_connection_info_test.cc",
|
||||
"internal/platform/ble_test.cc",
|
||||
"internal/platform/ble_v2_test.cc",
|
||||
"internal/platform/prng_test.cc",
|
||||
|
||||
+1
-2
@@ -99,8 +99,8 @@ cc_test(
|
||||
deps = [
|
||||
":core",
|
||||
":core_types",
|
||||
"//connections/implementation:internal",
|
||||
"//connections/implementation:internal_test",
|
||||
"//internal:device",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:types",
|
||||
@@ -108,7 +108,6 @@ cc_test(
|
||||
"@com_github_protobuf_matchers//protobuf-matchers",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/time",
|
||||
"@com_google_absl//absl/types:variant",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
+6
-3
@@ -20,12 +20,17 @@
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
#include "internal/platform/bluetooth_connection_info.h"
|
||||
#include "internal/platform/wifi_lan_connection_info.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
using ConnectionInfoVariant =
|
||||
absl::variant<BleConnectionInfo, BluetoothConnectionInfo,
|
||||
WifiLanConnectionInfo>;
|
||||
|
||||
class NearbyDevice {
|
||||
public:
|
||||
enum Type {
|
||||
@@ -43,9 +48,7 @@ class NearbyDevice {
|
||||
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, WifiLanConnectionInfo>>
|
||||
GetConnectionInfos() const = 0;
|
||||
virtual std::vector<ConnectionInfoVariant> GetConnectionInfos() const = 0;
|
||||
virtual Type GetType() const { return Type::kUnknownDevice; }
|
||||
};
|
||||
|
||||
|
||||
@@ -144,10 +144,12 @@ cc_library(
|
||||
cc_library(
|
||||
name = "connection_info",
|
||||
srcs = [
|
||||
"ble_connection_info.cc",
|
||||
"bluetooth_connection_info.cc",
|
||||
"wifi_lan_connection_info.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"ble_connection_info.h",
|
||||
"bluetooth_connection_info.h",
|
||||
"connection_info.h",
|
||||
"wifi_lan_connection_info.h",
|
||||
@@ -161,6 +163,7 @@ cc_library(
|
||||
],
|
||||
deps = [
|
||||
":base",
|
||||
"@com_google_absl//absl/log",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/status:statusor",
|
||||
"@com_google_absl//absl/strings",
|
||||
@@ -424,6 +427,7 @@ cc_test(
|
||||
srcs = [
|
||||
"atomic_boolean_test.cc",
|
||||
"atomic_reference_test.cc",
|
||||
"ble_connection_info_test.cc",
|
||||
"ble_test.cc",
|
||||
"ble_v2_test.cc",
|
||||
"bluetooth_adapter_test.cc",
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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.
|
||||
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
ByteArray BleConnectionInfo::ToBytes() const { return ByteArray(mac_address_); }
|
||||
|
||||
BleConnectionInfo BleConnectionInfo::FromBytes(ByteArray bytes) {
|
||||
std::string serial(bytes.AsStringView());
|
||||
return BleConnectionInfo(serial.substr(0, kMacAddressLength));
|
||||
}
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,65 @@
|
||||
// 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_INTERNAL_PLATFORM_BLE_CONNECTION_INFO_H_
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_BLE_CONNECTION_INFO_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/log/log.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/connection_info.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// 6 bytes that spell "BADMAC"
|
||||
constexpr absl::string_view kDefunctMacAddr = "\x42\x41\x44\x4D\x41\x43";
|
||||
|
||||
class BleConnectionInfo : public ConnectionInfo {
|
||||
public:
|
||||
explicit BleConnectionInfo(absl::string_view mac_address)
|
||||
: mac_address_(std::string(mac_address)) {
|
||||
if (mac_address_.size() != kMacAddressLength) {
|
||||
LOG(WARNING) << "MAC address is not of the expected length! Trying to "
|
||||
"connect to this MAC address will not work!";
|
||||
mac_address_ = std::string(kDefunctMacAddr);
|
||||
}
|
||||
}
|
||||
|
||||
BleConnectionInfo(BleConnectionInfo const& info) {
|
||||
mac_address_ = info.mac_address_;
|
||||
}
|
||||
MediumType GetMediumType() const override { return MediumType::kBle; }
|
||||
ByteArray ToBytes() const override;
|
||||
static BleConnectionInfo FromBytes(ByteArray bytes);
|
||||
ByteArray GetMacAddress() const { return ByteArray(mac_address_); }
|
||||
|
||||
private:
|
||||
std::string mac_address_;
|
||||
};
|
||||
|
||||
inline bool operator==(const BleConnectionInfo& a, const BleConnectionInfo& b) {
|
||||
return a.GetMacAddress() == b.GetMacAddress();
|
||||
}
|
||||
|
||||
inline bool operator!=(const BleConnectionInfo& a, const BleConnectionInfo& b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_BLE_CONNECTION_INFO_H_
|
||||
@@ -0,0 +1,86 @@
|
||||
// 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.
|
||||
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
constexpr absl::string_view kMacAddr = "\x4C\x8B\x1D\xCE\xBA\xD1";
|
||||
|
||||
TEST(BleConnectionInfoTest, TestMediumType) {
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
EXPECT_EQ(info.GetMediumType(), BleConnectionInfo::MediumType::kBle);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToBytes) {
|
||||
ByteArray mac_addr_bytes = ByteArray(std::string(kMacAddr));
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
EXPECT_EQ(info.ToBytes(), mac_addr_bytes);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBytes) {
|
||||
ByteArray mac_addr_bytes = ByteArray(std::string(kMacAddr));
|
||||
BleConnectionInfo info = BleConnectionInfo::FromBytes(mac_addr_bytes);
|
||||
EXPECT_EQ(info.GetMacAddress(), mac_addr_bytes);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestGetMacAddress) {
|
||||
ByteArray mac_addr_bytes = ByteArray(std::string(kMacAddr));
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
EXPECT_EQ(info.GetMacAddress(), mac_addr_bytes);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestGetLongMacAddr) {
|
||||
BleConnectionInfo info(absl::StrCat(kMacAddr, "\x56\x70\x89"));
|
||||
EXPECT_EQ(info.GetMacAddress().AsStringView(), kDefunctMacAddr);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestGetShortMacAddr) {
|
||||
BleConnectionInfo info("\x56\x70\x89");
|
||||
EXPECT_EQ(info.GetMacAddress().AsStringView(), kDefunctMacAddr);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytes) {
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
ByteArray serialized = info.ToBytes();
|
||||
BleConnectionInfo result = BleConnectionInfo::FromBytes(serialized);
|
||||
EXPECT_EQ(result, info);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestCopy) {
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
BleConnectionInfo copy(info);
|
||||
EXPECT_EQ(info, copy);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestEquals) {
|
||||
BleConnectionInfo info(kMacAddr);
|
||||
BleConnectionInfo info2(kMacAddr);
|
||||
EXPECT_EQ(info, info2);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -24,8 +24,6 @@
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
constexpr int kMacAddressLength = 6;
|
||||
|
||||
class BluetoothConnectionInfo : public ConnectionInfo {
|
||||
public:
|
||||
MediumType GetMediumType() const override { return MediumType::kBluetooth; }
|
||||
|
||||
@@ -19,12 +19,16 @@
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
constexpr int kMacAddressLength = 6;
|
||||
|
||||
class ConnectionInfo {
|
||||
public:
|
||||
enum class MediumType {
|
||||
kUnknown = 0,
|
||||
kBluetooth = 1,
|
||||
kWifiLan = 2,
|
||||
kBle = 3,
|
||||
};
|
||||
virtual ~ConnectionInfo() = default;
|
||||
virtual MediumType GetMediumType() const = 0;
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include "internal/crypto/random.h"
|
||||
#include "internal/platform/bluetooth_connection_info.h"
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
#include "internal/platform/implementation/system_clock.h"
|
||||
#include "internal/platform/wifi_lan_connection_info.h"
|
||||
#include "presence/device_motion.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -46,13 +45,10 @@ PresenceDevice::PresenceDevice(DeviceMotion device_motion,
|
||||
endpoint_id_ = GenerateRandomEndpointId();
|
||||
}
|
||||
|
||||
std::vector<absl::variant<location::nearby::BluetoothConnectionInfo,
|
||||
location::nearby::WifiLanConnectionInfo>>
|
||||
std::vector<location::nearby::ConnectionInfoVariant>
|
||||
PresenceDevice::GetConnectionInfos() const {
|
||||
location::nearby::BluetoothConnectionInfo bluetooth_connection_info(
|
||||
location::nearby::ByteArray(device_metadata_.bluetooth_mac_address()),
|
||||
"Nearby Presence");
|
||||
return {bluetooth_connection_info};
|
||||
return {location::nearby::BleConnectionInfo(
|
||||
device_metadata_.bluetooth_mac_address())};
|
||||
}
|
||||
} // namespace presence
|
||||
} // namespace nearby
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
#include "absl/time/time.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/device.h"
|
||||
#include "internal/platform/bluetooth_connection_info.h"
|
||||
#include "internal/platform/wifi_lan_connection_info.h"
|
||||
#include "internal/proto/device_metadata.pb.h"
|
||||
#include "presence/device_motion.h"
|
||||
|
||||
@@ -48,9 +46,8 @@ class PresenceDevice : public location::nearby::NearbyDevice {
|
||||
return NearbyDevice::Type::kPresenceDevice;
|
||||
}
|
||||
// Add more medium ConnectionInfos as we introduce them.
|
||||
std::vector<absl::variant<location::nearby::BluetoothConnectionInfo,
|
||||
location::nearby::WifiLanConnectionInfo>>
|
||||
GetConnectionInfos() const override;
|
||||
std::vector<location::nearby::ConnectionInfoVariant> GetConnectionInfos()
|
||||
const override;
|
||||
DeviceMotion GetDeviceMotion() const { return device_motion_; }
|
||||
DeviceMetadata GetMetadata() const { return device_metadata_; }
|
||||
absl::Time GetDiscoveryTimestamp() const { return discovery_timestamp_; }
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#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/ble_connection_info.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/proto/device_metadata.pb.h"
|
||||
|
||||
@@ -76,8 +76,8 @@ TEST(PresenceDeviceTest, TestGetBluetoothAddress) {
|
||||
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)
|
||||
absl::holds_alternative<location::nearby::BleConnectionInfo>(info));
|
||||
EXPECT_EQ(absl::get<location::nearby::BleConnectionInfo>(info)
|
||||
.GetMacAddress()
|
||||
.AsStringView(),
|
||||
kMacAddr);
|
||||
|
||||
Reference in New Issue
Block a user