Make api::ble_v2::BlePeripheral into a concrete class.

PiperOrigin-RevId: 753745108
This commit is contained in:
Francis Tsui
2025-05-01 15:11:08 -07:00
committed by Copybara-Service
parent 065a79e9ac
commit 866b3fa9e1
10 changed files with 42 additions and 157 deletions
+2
View File
@@ -173,6 +173,7 @@ cc_library(
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//internal/platform:base",
"//internal/platform:cancellation_flag",
"//internal/platform:mac_address",
"//internal/platform:uuid",
"//internal/proto:credential_cc_proto",
"//internal/proto:local_credential_cc_proto",
@@ -183,6 +184,7 @@ cc_library(
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
],
)
+21 -2
View File
@@ -19,6 +19,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
@@ -27,10 +28,12 @@
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/mac_address.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/uuid.h"
@@ -84,6 +87,11 @@ struct BleAdvertisementData {
class BlePeripheral {
public:
using UniqueId = std::uint64_t;
BlePeripheral(UniqueId unique_id, MacAddress address)
: unique_id_(unique_id), address_(std::move(address)) {}
explicit BlePeripheral(UniqueId unique_id = 0)
: unique_id_(unique_id) {}
virtual ~BlePeripheral() = default;
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice#getAddress()
@@ -91,11 +99,22 @@ class BlePeripheral {
// Returns the current address.
//
// This will always be an empty string on Apple platforms.
virtual std::string GetAddress() const = 0;
virtual std::string GetAddress() const {
if (address_.IsSet()) {
return address_.ToString();
}
return "";
}
// Returns an immutable unique identifier. The identifier must not change when
// the BLE address is rotated.
virtual UniqueId GetUniqueId() const = 0;
virtual UniqueId GetUniqueId() const { return unique_id_; };
bool IsSet() const { return unique_id_ != 0 || address_.IsSet(); }
private:
UniqueId unique_id_ = 0;
MacAddress address_;
};
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic
@@ -77,7 +77,6 @@ cc_library(
"ble_peripheral.h",
"ble_socket.h",
"ble_v2.h",
"ble_v2_peripheral.h",
"ble_v2_server_socket.h",
"ble_v2_socket.h",
"bluetooth_adapter.h",
@@ -177,7 +176,6 @@ cc_library(
"ble_medium.cc",
"ble_socket.cc",
"ble_v2.cc",
"ble_v2_peripheral.cc",
"ble_v2_server_socket.cc",
"ble_v2_socket.cc",
"bluetooth_adapter.cc",
@@ -306,7 +304,6 @@ cc_test(
"atomic_reference_test.cc",
"ble_gatt_server_test.cc",
"ble_medium_test.cc",
"ble_v2_peripheral_test.cc",
"ble_v2_test.cc",
"bluetooth_adapter_test.cc",
"count_down_latch_test.cc",
@@ -115,7 +115,7 @@ std::string ConvertGattStatusToString(
BleGattServer::BleGattServer(api::BluetoothAdapter* adapter,
api::ble_v2::ServerGattConnectionCallback callback)
: adapter_(dynamic_cast<BluetoothAdapter*>(adapter)),
peripheral_(adapter_->mac_address()),
peripheral_(adapter_->mac_address().address(), adapter_->mac_address()),
gatt_connection_callback_(std::move(callback)) {
DCHECK(adapter_ != nullptr);
}
@@ -31,7 +31,6 @@
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_v2_peripheral.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
#include "winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h"
@@ -132,7 +131,7 @@ class BleGattServer : public api::ble_v2::GattServer {
absl::Mutex mutex_;
BluetoothAdapter* const adapter_ = nullptr;
BleV2Peripheral peripheral_;
api::ble_v2::BlePeripheral peripheral_;
api::ble_v2::ServerGattConnectionCallback gatt_connection_callback_{};
::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
@@ -46,7 +46,6 @@
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_gatt_client.h"
#include "internal/platform/implementation/windows/ble_gatt_server.h"
#include "internal/platform/implementation/windows/ble_v2_peripheral.h"
#include "internal/platform/implementation/windows/ble_v2_server_socket.h"
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
@@ -1149,7 +1148,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
<< absl::BytesToHexString(advertisement_data.AsStringView())
<< "(" << advertisement_data.size() << ")";
BleV2Peripheral* peripheral_ptr = nullptr;
api::ble_v2::BlePeripheral* peripheral_ptr = nullptr;
{
absl::MutexLock lock(&mutex_);
peripheral_ptr = GetOrCreatePeripheral(bluetooth_address);
@@ -1186,7 +1185,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
}
// Only process alternate service data if there is no primary service data.
if (!has_primary_service_data && !alt_service_ids.empty()) {
BleV2Peripheral* peripheral_ptr = nullptr;
api::ble_v2::BlePeripheral* peripheral_ptr = nullptr;
{
absl::MutexLock lock(&mutex_);
peripheral_ptr = GetOrCreatePeripheral(bluetooth_address);
@@ -1267,14 +1266,14 @@ void BleV2Medium::AdvertisementFoundHandler(
"corresponding data, skipping";
return;
}
// Save the BleV2Peripheral.
// Save the BlePeripheral.
MacAddress bluetooth_address;
if (!MacAddress::FromUint64(args.BluetoothAddress(), bluetooth_address)) {
LOG(ERROR) << "Invalid MAC address: " << args.BluetoothAddress();
return;
}
BleV2Peripheral* peripheral_ptr = nullptr;
api::ble_v2::BlePeripheral* peripheral_ptr = nullptr;
{
absl::MutexLock lock(&mutex_);
peripheral_ptr = GetOrCreatePeripheral(bluetooth_address);
@@ -1307,13 +1306,13 @@ bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address,
LOG(WARNING) << __func__ << ": Invalid MAC address: " << mac_address;
return false;
}
BleV2Peripheral* peripheral = nullptr;
api::ble_v2::BlePeripheral* peripheral = nullptr;
{
absl::MutexLock lock(&mutex_);
peripheral = GetOrCreatePeripheral(bluetooth_address);
}
if (peripheral != nullptr && peripheral->Ok()) {
if (peripheral != nullptr && peripheral->IsSet()) {
callback(*peripheral);
return true;
}
@@ -1328,7 +1327,7 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
<< absl::StrCat(absl::Hex(id));
return false;
}
BleV2Peripheral* peripheral = nullptr;
api::ble_v2::BlePeripheral* peripheral = nullptr;
{
absl::MutexLock lock(&mutex_);
peripheral = GetPeripheral(bluetooth_address);
@@ -1354,20 +1353,22 @@ uint64_t BleV2Medium::GenerateSessionId() {
return kFailedGenerateSessionId;
}
BleV2Peripheral* BleV2Medium::GetOrCreatePeripheral(MacAddress address) {
api::ble_v2::BlePeripheral* BleV2Medium::GetOrCreatePeripheral(
MacAddress address) {
if (!address.IsSet()) {
LOG(WARNING) << __func__ << "empty MAC address is not allowed.";
return nullptr;
}
// For Windows peripheral uniqueId is the same as the address.
BleV2Peripheral* peripheral = GetPeripheral(address);
api::ble_v2::BlePeripheral* peripheral = GetPeripheral(address);
if (peripheral != nullptr) {
return peripheral;
}
RemoveExpiredPeripherals();
PeripheralInfo peripheral_info{
.last_access_time = absl::Now(),
.peripheral = std::make_unique<BleV2Peripheral>(address),
.last_access_time = absl::Now(),
.peripheral = std::make_unique<api::ble_v2::BlePeripheral>(
address.address(), address),
};
peripheral = peripheral_info.peripheral.get();
VLOG(1) << "New BLE peripheral with address: " << address.ToString();
@@ -1376,7 +1377,7 @@ BleV2Peripheral* BleV2Medium::GetOrCreatePeripheral(MacAddress address) {
return peripheral;
}
BleV2Peripheral* BleV2Medium::GetPeripheral(MacAddress address) {
api::ble_v2::BlePeripheral* BleV2Medium::GetPeripheral(MacAddress address) {
auto it = peripheral_map_.find(address);
if (it == peripheral_map_.end()) {
return nullptr;
@@ -30,7 +30,6 @@
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_gatt_server.h"
#include "internal/platform/implementation/windows/ble_v2_peripheral.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/mac_address.h"
#include "internal/platform/uuid.h"
@@ -184,10 +183,10 @@ class BleV2Medium : public api::ble_v2::BleMedium {
uint64_t GenerateSessionId() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns nullptr if `address` is invalid.
BleV2Peripheral* GetOrCreatePeripheral(MacAddress address)
api::ble_v2::BlePeripheral* GetOrCreatePeripheral(MacAddress address)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns nullptr if `address` does not match a known peripheral.
BleV2Peripheral* GetPeripheral(MacAddress address)
api::ble_v2::BlePeripheral* GetPeripheral(MacAddress address)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void RemoveExpiredPeripherals() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -223,7 +222,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
// DiscoveredPeripheralCallback only keeps the pointer to the object
struct PeripheralInfo {
absl::Time last_access_time;
std::unique_ptr<BleV2Peripheral> peripheral;
std::unique_ptr<api::ble_v2::BlePeripheral> peripheral;
};
absl::flat_hash_map<MacAddress, PeripheralInfo> peripheral_map_
ABSL_GUARDED_BY(mutex_);
@@ -1,31 +0,0 @@
// 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 "internal/platform/implementation/windows/ble_v2_peripheral.h"
#include <string>
#include "internal/platform/mac_address.h"
namespace nearby {
namespace windows {
std::string BleV2Peripheral::GetAddress() const {
if (mac_address_.IsSet()) {
return mac_address_.ToString();
}
return "";
}
} // namespace windows
} // namespace nearby
@@ -1,52 +0,0 @@
// 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_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_
#include <string>
#include <utility>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/mac_address.h"
namespace nearby {
namespace windows {
// BLE peripheral implementation for BLE version 2. Must contain enough data
// about a particular BLE device to connect to its GATT server.
class BleV2Peripheral : public api::ble_v2::BlePeripheral {
public:
using UniqueId = api::ble_v2::BlePeripheral::UniqueId;
explicit BleV2Peripheral(MacAddress address)
: mac_address_(std::move(address)) {}
~BleV2Peripheral() override = default;
// Returns the MAC address of the peripheral or empty string. The format is in
// "00:B0:D0:63:C2:26".
std::string GetAddress() const override;
UniqueId GetUniqueId() const override { return mac_address_.address(); }
bool Ok() const { return mac_address_.IsSet(); }
explicit operator bool() const { return Ok(); }
private:
const MacAddress mac_address_;
};
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_
@@ -1,49 +0,0 @@
// 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 "internal/platform/implementation/windows/ble_v2_peripheral.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/platform/mac_address.h"
namespace nearby {
namespace windows {
namespace {
TEST(BleV2Peripheral, Constructor) {
constexpr absl::string_view kAddress = "F1:F2:F3:F4:F5:F6";
MacAddress address;
ASSERT_TRUE(MacAddress::FromString(kAddress, address));
BleV2Peripheral ble_peripheral(address);
EXPECT_TRUE(ble_peripheral);
EXPECT_TRUE(ble_peripheral.Ok());
EXPECT_EQ(ble_peripheral.GetUniqueId(), 0xf1f2f3f4f5f6);
EXPECT_EQ(ble_peripheral.GetAddress(), kAddress);
}
TEST(BleV2Peripheral, ConstructFromBadAddress) {
MacAddress address;
BleV2Peripheral ble_peripheral(address);
EXPECT_FALSE(ble_peripheral);
EXPECT_FALSE(ble_peripheral.Ok());
EXPECT_EQ(ble_peripheral.GetUniqueId(), 0);
EXPECT_EQ(ble_peripheral.GetAddress(), "");
}
} // namespace
} // namespace windows
} // namespace nearby