From acaf963c4071d8728e75af373e0cf6b7050b66d9 Mon Sep 17 00:00:00 2001 From: Crisrael Lucero Date: Thu, 22 May 2025 17:07:50 -0700 Subject: [PATCH] Migrate usage of BluetoothUtils::ToString() to MacAddress PiperOrigin-RevId: 762184790 --- connections/implementation/BUILD | 4 ++++ .../implementation/ble_advertisement.cc | 20 +++++++++++++++-- .../injected_bluetooth_device_store.cc | 21 +++++++++++++----- internal/platform/BUILD | 3 +++ internal/platform/byte_array.h | 19 ++++++++++++++++ internal/platform/byte_array_test.cc | 22 +++++++++++++++++++ 6 files changed, 81 insertions(+), 8 deletions(-) diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index a8e9372f..398c5b95 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -40,6 +40,7 @@ cc_library( deps = [ ":types", "//internal/platform:base", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform:util", "@com_google_absl//absl/status", @@ -176,6 +177,7 @@ cc_library( "//internal/platform:comm", "//internal/platform:connection_info", "//internal/platform:error_code_recorder", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform:util", "//internal/platform/implementation:comm", @@ -335,6 +337,7 @@ cc_test( ":internal", ":types", "//internal/platform:base", + "//internal/platform:mac_address", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/status", @@ -551,6 +554,7 @@ cc_test( ":internal", "//internal/platform:base", "//internal/platform:comm", + "//internal/platform:mac_address", "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", diff --git a/connections/implementation/ble_advertisement.cc b/connections/implementation/ble_advertisement.cc index 32e2619d..9776a797 100644 --- a/connections/implementation/ble_advertisement.cc +++ b/connections/implementation/ble_advertisement.cc @@ -14,6 +14,7 @@ #include "connections/implementation/ble_advertisement.h" +#include #include #include #include @@ -26,6 +27,7 @@ #include "internal/platform/bluetooth_utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/logging.h" +#include "internal/platform/mac_address.h" #include "internal/platform/stream_reader.h" namespace nearby { @@ -201,8 +203,22 @@ absl::StatusOr BleAdvertisement::CreateBleAdvertisement( return absl::InvalidArgumentError( "Cannot deserialize BleAdvertisement: bluetooth_mac_address."); } - bluetooth_mac_address = - BluetoothUtils::ToString(*bluetooth_mac_address_bytes); + + absl::StatusOr bluetooth_mac_address_bytes_uint64 = + bluetooth_mac_address_bytes->Read6BytesAsUint64(); + if (!bluetooth_mac_address_bytes_uint64.ok()) { + return absl::InvalidArgumentError( + "Cannot deserialize BleAdvertisement: bluetooth_mac_address."); + } + + MacAddress mac_address; + if (!MacAddress::FromUint64(bluetooth_mac_address_bytes_uint64.value(), + mac_address) || + !mac_address.IsSet()) { + return absl::InvalidArgumentError( + "Cannot convert BleAdvertisement: bluetooth_mac_address."); + } + bluetooth_mac_address = mac_address.ToString(); } // The next 1 byte is supposed to be the length of the uwb_address. If the diff --git a/connections/implementation/injected_bluetooth_device_store.cc b/connections/implementation/injected_bluetooth_device_store.cc index 9162a3d4..9eff2466 100644 --- a/connections/implementation/injected_bluetooth_device_store.cc +++ b/connections/implementation/injected_bluetooth_device_store.cc @@ -14,11 +14,13 @@ #include "connections/implementation/injected_bluetooth_device_store.h" +#include #include +#include "absl/status/statusor.h" #include "connections/implementation/bluetooth_device_name.h" #include "internal/platform/implementation/bluetooth_classic.h" -#include "internal/platform/bluetooth_utils.h" +#include "internal/platform/mac_address.h" namespace nearby { namespace connections { @@ -54,12 +56,19 @@ BluetoothDevice InjectedBluetoothDeviceStore::CreateInjectedBluetoothDevice( const ByteArray& remote_bluetooth_mac_address, const std::string& endpoint_id, const ByteArray& endpoint_info, const ByteArray& service_id_hash, Pcp pcp) { - std::string remote_bluetooth_mac_address_str = - BluetoothUtils::ToString(remote_bluetooth_mac_address); - // Valid MAC address is required. - if (remote_bluetooth_mac_address_str.empty()) + absl::StatusOr bluetooth_mac_address_bytes_uint64 = + remote_bluetooth_mac_address.Read6BytesAsUint64(); + if (!bluetooth_mac_address_bytes_uint64.ok()) { return BluetoothDevice(/*device=*/nullptr); + } + + MacAddress remote_mac_address; + if (!MacAddress::FromUint64(bluetooth_mac_address_bytes_uint64.value(), + remote_mac_address) + || !remote_mac_address.IsSet()) { + return BluetoothDevice(/*device=*/nullptr); + } // Non-empty endpoint info is required. if (endpoint_info.Empty()) return BluetoothDevice(/*device=*/nullptr); @@ -75,7 +84,7 @@ BluetoothDevice InjectedBluetoothDeviceStore::CreateInjectedBluetoothDevice( if (!name.IsValid()) return BluetoothDevice(/*device=*/nullptr); auto injected_device = std::make_unique( - static_cast(name), remote_bluetooth_mac_address_str); + static_cast(name), remote_mac_address.ToString()); BluetoothDevice device_to_return(injected_device.get()); // Store underlying device to ensure that it is kept alive for future use. diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 6665ed10..5ccab0a2 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -73,6 +73,8 @@ cc_library( "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/meta:type_traits", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/synchronization", @@ -409,6 +411,7 @@ cc_test( "//internal/platform/implementation/g3", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/hash:hash_testing", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings:string_view", "@com_google_absl//absl/synchronization", "@com_google_googletest//:gtest_main", diff --git a/internal/platform/byte_array.h b/internal/platform/byte_array.h index b43f9c39..70dbe02d 100644 --- a/internal/platform/byte_array.h +++ b/internal/platform/byte_array.h @@ -18,9 +18,12 @@ #include #include #include +#include #include #include +#include "absl/status/status.h" +#include "absl/status/statusor.h" #include "absl/strings/string_view.h" namespace nearby { @@ -78,6 +81,22 @@ class ByteArray { return true; } + // Returns the value of the ByteArray as a uint64_t. If the ByteArray is + // not exactly 6 bytes, returns an error status instead. + absl::StatusOr Read6BytesAsUint64() const { + if (data_.size() != 6) { + return absl::FailedPreconditionError("ByteArray must be 6 bytes."); + } + + uint64_t result = 0; + for (int i = 0; i < data_.size(); i++) { + result <<= 8; + result |= ((static_cast(data()[i])) & 0xFF); + } + + return result; + } + char* data() { return &data_[0]; } std::string string_data() const { return data_; } const char* data() const { return data_.data(); } diff --git a/internal/platform/byte_array_test.cc b/internal/platform/byte_array_test.cc index 7b8a8fa5..973b24fc 100644 --- a/internal/platform/byte_array_test.cc +++ b/internal/platform/byte_array_test.cc @@ -24,6 +24,7 @@ #include "gtest/gtest.h" #include "absl/hash/hash_testing.h" +#include "absl/status/status.h" #include "absl/strings/string_view.h" namespace { @@ -103,6 +104,27 @@ TEST(ByteArrayTest, CreateFromAbslStringReturnsTheSame) { EXPECT_EQ(bytes.AsStringView(), kTestString); } +TEST(ByteArrayTest, Read6BytesAsUint64EmptyByteArrayReturnsError) { + ByteArray bytes; + auto result = bytes.Read6BytesAsUint64(); + EXPECT_FALSE(result.ok()); + EXPECT_EQ(result.status().code(), absl::StatusCode::kFailedPrecondition); +} + +TEST(ByteArrayTest, Read6BytesAsUint64NotEnoughBytesReturnsError) { + ByteArray bytes({0x01, 0x02, 0x03, 0x04, 0x05}); + auto result = bytes.Read6BytesAsUint64(); + EXPECT_FALSE(result.ok()); + EXPECT_EQ(result.status().code(), absl::StatusCode::kFailedPrecondition); +} + +TEST(ByteArrayTest, Read6BytesAsUint64) { + ByteArray bytes({0x01, 0x02, 0x03, 0x04, 0x05, 0x06}); + auto result = bytes.Read6BytesAsUint64(); + EXPECT_TRUE(result.ok()); + EXPECT_EQ(bytes.Read6BytesAsUint64().value(), 0x010203040506); +} + TEST(ByteArrayTest, IteratorTypes) { static_assert(std::same_as().begin()), ByteArray::iterator>);