mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36: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
+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;
|
||||
|
||||
Reference in New Issue
Block a user