diff --git a/internal/base/bluetooth_address.cc b/internal/base/bluetooth_address.cc index 5cdfba29..ac1de37d 100644 --- a/internal/base/bluetooth_address.cc +++ b/internal/base/bluetooth_address.cc @@ -15,10 +15,7 @@ #include "internal/base/bluetooth_address.h" #include -#include -#include -#include "absl/strings/str_format.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" @@ -92,33 +89,5 @@ bool ParseBluetoothAddress(absl::string_view input, return false; } -std::string ConvertBluetoothAddressUIntToString(uint64_t address) { - std::string mac_address = absl::StrFormat( - "%02llX:%02llX:%02llX:%02llX:%02llX:%02llX", address >> 40, - (address >> 32) & 0xff, (address >> 24) & 0xff, (address >> 16) & 0xff, - (address >> 8) & 0xff, address & 0xff); - return CanonicalizeBluetoothAddress(mac_address); -} - -std::string CanonicalizeBluetoothAddress(absl::string_view address) { - std::array bytes; - - if (!ParseBluetoothAddress(address, absl::MakeSpan(bytes.data(), 6))) - return std::string(); - - return CanonicalizeBluetoothAddress(bytes); -} - -std::string CanonicalizeBluetoothAddress( - const std::array& address_bytes) { - return absl::StrFormat("%02X:%02X:%02X:%02X:%02X:%02X", address_bytes[0], - address_bytes[1], address_bytes[2], address_bytes[3], - address_bytes[4], address_bytes[5]); -} - -std::string CanonicalizeBluetoothAddress(uint64_t address) { - return ConvertBluetoothAddressUIntToString(address); -} - } // namespace device } // namespace nearby diff --git a/internal/base/bluetooth_address.h b/internal/base/bluetooth_address.h index 81c4e750..14bdde05 100644 --- a/internal/base/bluetooth_address.h +++ b/internal/base/bluetooth_address.h @@ -18,9 +18,6 @@ #include #include -#include -#include - #include "absl/strings/string_view.h" #include "absl/types/span.h" @@ -35,16 +32,6 @@ namespace device { // 1A2B3C4D5E6F bool ParseBluetoothAddress(absl::string_view input, absl::Span output); -// Converts a uint64_t Bluetooth address to string. -std::string ConvertBluetoothAddressUIntToString(uint64_t address); - -// Returns |address| in the canonical format: XX:XX:XX:XX:XX:XX, where each 'X' -// is a hex digit. If the input |address| is invalid, returns an empty string. -std::string CanonicalizeBluetoothAddress(absl::string_view address); -std::string CanonicalizeBluetoothAddress( - const std::array& address_bytes); -std::string CanonicalizeBluetoothAddress(uint64_t address); - } // namespace device } // namespace nearby diff --git a/internal/base/bluetooth_address_test.cc b/internal/base/bluetooth_address_test.cc index 1530f9b2..642ef4c6 100644 --- a/internal/base/bluetooth_address_test.cc +++ b/internal/base/bluetooth_address_test.cc @@ -15,7 +15,6 @@ #include "internal/base/bluetooth_address.h" #include -#include #include "gtest/gtest.h" #include "absl/types/span.h" @@ -32,28 +31,6 @@ TEST(BluetoothUtil, ParseBluetoothAddress) { EXPECT_EQ(output, expected_output); } -TEST(BluetoothUtil, ConvertBluetoothAddressUIntToString) { - const uint64_t input = 0x00001A2B3C4D5E6F; - std::string expected_output = "1A:2B:3C:4D:5E:6F"; - std::string output = ConvertBluetoothAddressUIntToString(input); - EXPECT_EQ(output, expected_output); -} - -TEST(BluetoothUtil, CanonicalizeBluetoothAddress) { - std::array address{{26, 43, 60, 77, 94, 111}}; - EXPECT_EQ(CanonicalizeBluetoothAddress(address), "1A:2B:3C:4D:5E:6F"); - EXPECT_EQ(CanonicalizeBluetoothAddress("1A-2B-3C-4D-5E-6F"), - "1A:2B:3C:4D:5E:6F"); - EXPECT_EQ(CanonicalizeBluetoothAddress(0x00001A2B3C4D5E6F), - "1A:2B:3C:4D:5E:6F"); - - // Canonicalizes invalid address - EXPECT_EQ(CanonicalizeBluetoothAddress("1A-2B-3C-4D-5E-6F-89"), ""); - EXPECT_EQ(CanonicalizeBluetoothAddress("nearby"), ""); - EXPECT_EQ(CanonicalizeBluetoothAddress("MA-2M-3C-4D-5E-6F"), ""); - EXPECT_EQ(CanonicalizeBluetoothAddress(0x001A2B3C4D5E6F89), ""); -} - } // namespace } // namespace device } // namespace nearby diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 724f01fc..e4a128aa 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -83,6 +83,7 @@ cc_library( "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", + "@com_google_absl//absl/types:span", ], ) @@ -262,7 +263,6 @@ cc_library( "//internal/interop:__pkg__", "//internal/network:__subpackages__", "//internal/platform:__subpackages__", - "//internal/platform/implementation/g3:__pkg__", "//internal/preferences:__subpackages__", "//internal/proto/analytics:__subpackages__", "//internal/test:__subpackages__", @@ -367,6 +367,7 @@ cc_library( deps = [ "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/types:span", ], ) @@ -414,7 +415,7 @@ cc_test( deps = [ ":base", ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/hash:hash_testing", "@com_google_absl//absl/status", @@ -449,7 +450,7 @@ cc_test( ":base", ":cancellation_flag", ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", ], @@ -474,7 +475,7 @@ cc_test( deps = [ ":error_code_recorder", ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_googletest//:gtest_main", ], @@ -511,7 +512,7 @@ cc_test( "//connections/implementation/flags:connections_flags", "//internal/flags:nearby_flags", "//internal/platform/implementation:comm", - "//internal/platform/implementation/g3", # build_cleaner: keep + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "//proto:connections_enums_cc_proto", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/status", @@ -527,7 +528,7 @@ cc_test( deps = [ ":comm", "//internal/platform/implementation:comm", - "//internal/platform/implementation/g3", # build_cleaner: keep + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "//internal/proto:credential_cc_proto", "@com_github_protobuf_matchers//protobuf-matchers", "@com_google_absl//absl/status", @@ -576,6 +577,7 @@ cc_test( "//internal/flags:nearby_flags", "//internal/platform/flags:platform_flags", "//internal/platform/implementation:comm", + "//internal/platform/implementation:platform_impl", # build_cleaner: keep "//internal/platform/implementation:types", "//internal/proto:credential_cc_proto", "//internal/test", @@ -590,17 +592,7 @@ cc_test( "@com_google_absl//absl/time", "@com_google_absl//absl/types:variant", "@com_google_googletest//:gtest_main", - ] + select({ - "@platforms//os:windows": [ - "//internal/platform/implementation/windows", - ], - "@platforms//os:platform_ios": [ - "//internal/platform/implementation/apple", - ], - "//conditions:default": [ - "//internal/platform/implementation/g3", - ], - }), + ], ) cc_test( diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 44be2fd8..7785502e 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -627,6 +627,7 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { // Start GattClient MacAddress mac_address; EXPECT_TRUE(MacAddress::FromString(adapter_a.GetMacAddress(), mac_address)); + EXPECT_TRUE(mac_address.IsSet()); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{}); diff --git a/internal/platform/bluetooth_utils.cc b/internal/platform/bluetooth_utils.cc index a21bb071..3a2a3581 100644 --- a/internal/platform/bluetooth_utils.cc +++ b/internal/platform/bluetooth_utils.cc @@ -18,41 +18,22 @@ #include #include "absl/strings/string_view.h" +#include "absl/types/span.h" #include "internal/platform/byte_array.h" #include "internal/platform/mac_address.h" namespace nearby { -namespace { - -uint64_t ByteArrayToUint64(const ByteArray& byte_array) { - if (byte_array.size() != BluetoothUtils::kBluetoothMacAddressLength) { - return 0; - } - uint64_t result = 0; - for (int i = 0; i < byte_array.size(); i++) { - result <<= 8; - result |= ((static_cast(byte_array.data()[i])) & 0xFF); - } - return result; -} - -ByteArray Uint64AddressToByteArray(uint64_t address) { - ByteArray address_bytes(BluetoothUtils::kBluetoothMacAddressLength); - address_bytes.data()[0] = (address >> 40) & 0xFF; - address_bytes.data()[1] = (address >> 32) & 0xFF; - address_bytes.data()[2] = (address >> 24) & 0xFF; - address_bytes.data()[3] = (address >> 16) & 0xFF; - address_bytes.data()[4] = (address >> 8) & 0xFF; - address_bytes.data()[5] = address & 0xFF; - return address_bytes; -} - -} // namespace std::string BluetoothUtils::ToString(const ByteArray& bluetooth_mac_address) { + if (bluetooth_mac_address.size() != kBluetoothMacAddressLength) { + return ""; + } MacAddress mac_address; - if (!MacAddress::FromUint64(ByteArrayToUint64(bluetooth_mac_address), - mac_address) || + if (!MacAddress::FromBytes( + absl::MakeConstSpan( + reinterpret_cast(bluetooth_mac_address.data()), + bluetooth_mac_address.size()), + mac_address) || !mac_address.IsSet()) { return ""; } @@ -65,28 +46,10 @@ ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) { !mac_address.IsSet()) { return ByteArray(); } - return Uint64AddressToByteArray(mac_address.address()); -} - -bool BluetoothUtils::IsBluetoothMacAddressUnset( - const ByteArray& bluetooth_mac_address_bytes) { - return ByteArrayToUint64(bluetooth_mac_address_bytes) == 0; -} - -std::string BluetoothUtils::FromNumber(std::uint64_t address) { - MacAddress mac_address; - if (!MacAddress::FromUint64(address, mac_address) || !mac_address.IsSet()) { - return ""; - } - return mac_address.ToString(); -} - -std::uint64_t BluetoothUtils::ToNumber(absl::string_view address) { - MacAddress mac_address; - if (!MacAddress::FromString(address, mac_address)) { - return 0; - } - return mac_address.address(); + ByteArray address_bytes(BluetoothUtils::kBluetoothMacAddressLength); + mac_address.ToBytes(absl::MakeSpan( + reinterpret_cast(address_bytes.data()), address_bytes.size())); + return address_bytes; } } // namespace nearby diff --git a/internal/platform/bluetooth_utils.h b/internal/platform/bluetooth_utils.h index 3ef7bfde..561c2155 100644 --- a/internal/platform/bluetooth_utils.h +++ b/internal/platform/bluetooth_utils.h @@ -39,21 +39,6 @@ class BluetoothUtils { // e.g. "AC:37:43:BC:A9:28" -> {-84, 55, 67, -68, -87, 40}. ABSL_DEPRECATED("Use MacAddress class instead.") static ByteArray FromString(absl::string_view bluetooth_mac_address); - - // Converts a MAC address from binary to canonical format. - // Example: 0xF1F2F3F4F5F6 -> "F1:F2:F3:F4:F5:F6" - ABSL_DEPRECATED("Use MacAddress class instead.") - static std::string FromNumber(std::uint64_t address); - - // Converts a MAC address from canonical format to binary - // Example: "F1:F2:F3:F4:F5:F6" ->0xF1F2F3F4F5F6 - ABSL_DEPRECATED("Use MacAddress class instead.") - static std::uint64_t ToNumber(absl::string_view address); - - // Checks if a Bluetooth MAC address is zero for every byte. - ABSL_DEPRECATED("Use MacAddress class instead.") - static bool IsBluetoothMacAddressUnset( - const ByteArray& bluetooth_mac_address); }; } // namespace nearby diff --git a/internal/platform/bluetooth_utils_test.cc b/internal/platform/bluetooth_utils_test.cc index 82ffea5e..80dabc35 100644 --- a/internal/platform/bluetooth_utils_test.cc +++ b/internal/platform/bluetooth_utils_test.cc @@ -88,16 +88,4 @@ TEST(BluetoothUtilsTest, InvalidStringReturnsEmptyByteArray) { EXPECT_TRUE(bytes_result.Empty()); } -TEST(BluetoothUtilsTest, FromNumber) { - EXPECT_EQ(BluetoothUtils::FromNumber(0xF1F2F3F4F5F6), "F1:F2:F3:F4:F5:F6"); -} - -TEST(BluetoothUtilsTest, ToNumber) { - EXPECT_EQ(BluetoothUtils::ToNumber("F1:F2:F3:F4:F5:F6"), 0xF1F2F3F4F5F6); -} - -TEST(BluetoothUtilsTest, ToNumberInvalidString) { - EXPECT_EQ(BluetoothUtils::ToNumber("22:00:11:33:77:aa::bb::99"), 0); -} - } // namespace nearby diff --git a/internal/platform/implementation/g3/bluetooth_adapter.cc b/internal/platform/implementation/g3/bluetooth_adapter.cc index de2b8c95..db48e5d8 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.cc +++ b/internal/platform/implementation/g3/bluetooth_adapter.cc @@ -17,8 +17,7 @@ #include #include -#include "internal/platform/bluetooth_utils.h" -#include "internal/platform/implementation/g3/bluetooth_classic.h" +#include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/prng.h" @@ -53,7 +52,11 @@ std::string BluetoothDevice::GetMacAddress() const { BluetoothAdapter::BluetoothAdapter() { std::uint64_t raw_mac_addr = Prng().NextInt64() & kMacAddressMask; - SetMacAddress(BluetoothUtils::FromNumber(raw_mac_addr)); + MacAddress mac_address; + if (MacAddress::FromUint64(raw_mac_addr, mac_address) && + mac_address.IsSet()) { + SetMacAddress(mac_address); + } unique_id_ = raw_mac_addr; } diff --git a/internal/platform/implementation/g3/bluetooth_adapter.h b/internal/platform/implementation/g3/bluetooth_adapter.h index 1d7df9fa..32dde75e 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.h +++ b/internal/platform/implementation/g3/bluetooth_adapter.h @@ -16,9 +16,7 @@ #define PLATFORM_IMPL_G3_BLUETOOTH_ADAPTER_H_ #include -#include #include -#include #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" @@ -27,7 +25,6 @@ #include "internal/platform/implementation/ble_v2.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" -#include "internal/platform/implementation/g3/single_thread_executor.h" #include "internal/platform/mac_address.h" namespace nearby { @@ -131,8 +128,8 @@ class BluetoothAdapter : public api::BluetoothAdapter { void SetBleV2Medium(api::ble_v2::BleMedium* medium); api::ble_v2::BleMedium* GetBleV2Medium() { return ble_v2_medium_; } - void SetMacAddress(absl::string_view mac_address) { - MacAddress::FromString(mac_address, mac_address_); + void SetMacAddress(MacAddress mac_address) { + mac_address_ = mac_address; } std::uint64_t GetUniqueId() { return unique_id_; } diff --git a/internal/platform/mac_address.cc b/internal/platform/mac_address.cc index 45e826e4..a0f7b425 100644 --- a/internal/platform/mac_address.cc +++ b/internal/platform/mac_address.cc @@ -21,6 +21,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" +#include "absl/types/span.h" namespace nearby { @@ -56,6 +57,38 @@ bool MacAddress::FromUint64(uint64_t address, MacAddress& mac_address) { return true; } +bool MacAddress::FromBytes(absl::Span bytes, + MacAddress& mac_address) { + if (bytes.size() < 6) { + return false; + } + mac_address.address_ = bytes[0]; + mac_address.address_ <<= 8; + mac_address.address_ |= bytes[1]; + mac_address.address_ <<= 8; + mac_address.address_ |= bytes[2]; + mac_address.address_ <<= 8; + mac_address.address_ |= bytes[3]; + mac_address.address_ <<= 8; + mac_address.address_ |= bytes[4]; + mac_address.address_ <<= 8; + mac_address.address_ |= bytes[5]; + return true; +} + +bool MacAddress::ToBytes(absl::Span bytes) const { + if (bytes.size() < 6) { + return false; + } + bytes[0] = (address_ >> 40) & 0xff; + bytes[1] = (address_ >> 32) & 0xff; + bytes[2] = (address_ >> 24) & 0xff; + bytes[3] = (address_ >> 16) & 0xff; + bytes[4] = (address_ >> 8) & 0xff; + bytes[5] = address_ & 0xff; + return true; +} + std::string MacAddress::ToString() const { return absl::StrCat(absl::StrFormat("%02X", (address_ >> 40) & 0xff), ":", absl::StrFormat("%02X", (address_ >> 32) & 0xff), ":", diff --git a/internal/platform/mac_address.h b/internal/platform/mac_address.h index 7d1f1a0c..7b438eb6 100644 --- a/internal/platform/mac_address.h +++ b/internal/platform/mac_address.h @@ -19,6 +19,7 @@ #include #include "absl/strings/string_view.h" +#include "absl/types/span.h" namespace nearby { @@ -37,12 +38,21 @@ class MacAddress { // Returns false if the integer is not a valid MAC address. static bool FromUint64(uint64_t address, MacAddress& mac_address); + // Creates a MAC address from a span of bytes. + // Returns false if the span is less than 6 bytes long. + static bool FromBytes(absl::Span bytes, + MacAddress& mac_address); + // Packs the MAC address into the lower 48 bits of a 64-bit integer. uint64_t address() const { return address_; } // Returns the MAC address in the format of "00:B0:D0:63:C2:26". std::string ToString() const; + // Packs the MAC address into the given bytes. + // Returns false if the span is less than 6 bytes long. + bool ToBytes(absl::Span bytes) const; + // Returns true if the MAC address is set. bool IsSet() const { return address_ != 0; } diff --git a/internal/platform/mac_address_test.cc b/internal/platform/mac_address_test.cc index 5d7ca1f3..b7121dcd 100644 --- a/internal/platform/mac_address_test.cc +++ b/internal/platform/mac_address_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "internal/platform/mac_address.h" +#include #include "gtest/gtest.h" #include "absl/hash/hash_testing.h" @@ -77,5 +78,38 @@ TEST(MacAddressTest, Hash) { })); } +TEST(MacAddressTest, ToBytesSuccess) { + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromUint64(0x00B0D063C226, mac_address)); + uint8_t bytes[6]; + EXPECT_TRUE(mac_address.ToBytes(bytes)); + EXPECT_EQ(bytes[0], 0x00); + EXPECT_EQ(bytes[1], 0xB0); + EXPECT_EQ(bytes[2], 0xD0); + EXPECT_EQ(bytes[3], 0x63); + EXPECT_EQ(bytes[4], 0xC2); + EXPECT_EQ(bytes[5], 0x26); +} + +TEST(MacAddressTest, ToBytesInvalidLength) { + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromUint64(0x00B0D063C226, mac_address)); + uint8_t bytes[5]; + EXPECT_FALSE(mac_address.ToBytes(bytes)); +} + +TEST(MacAddressTest, FromBytesSuccess) { + uint8_t bytes[6] = {0x00, 0xB0, 0xD0, 0x63, 0xC2, 0x26}; + MacAddress mac_address; + EXPECT_TRUE(MacAddress::FromBytes(bytes, mac_address)); + EXPECT_EQ(mac_address.address(), 0x00B0D063C226); +} + +TEST(MacAddressTest, FromBytesInvalidLength) { + uint8_t bytes[5] = {0x00, 0xB0, 0xD0, 0x63, 0xC2}; + MacAddress mac_address; + EXPECT_FALSE(MacAddress::FromBytes(bytes, mac_address)); +} + } // namespace } // namespace nearby diff --git a/sharing/BUILD b/sharing/BUILD index fa80ce65..6f2b47e8 100644 --- a/sharing/BUILD +++ b/sharing/BUILD @@ -321,9 +321,7 @@ cc_library( "//connections/implementation:internal", "//internal/analytics:event_logger", "//internal/base", - "//internal/base:bluetooth_address", "//internal/base:file_path", - "//internal/base:files", "//internal/flags:nearby_flags", "//internal/network:url", "//internal/platform:base", @@ -348,7 +346,6 @@ cc_library( "//sharing/proto:share_cc_proto", "//sharing/proto:wire_format_cc_proto", "//sharing/scheduling", - "@com_google_absl//absl/algorithm:container", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", diff --git a/sharing/certificates/BUILD b/sharing/certificates/BUILD index 28afdb51..fa7d93ea 100644 --- a/sharing/certificates/BUILD +++ b/sharing/certificates/BUILD @@ -42,6 +42,7 @@ cc_library( "//internal/base", "//internal/base:file_path", "//internal/crypto_cros", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform/implementation:account_manager", "//proto/identity/v1:resources_cc_proto", @@ -116,6 +117,7 @@ cc_test( deps = [ ":certificates", ":test_support", + "//internal/platform:mac_address", "//internal/platform/implementation:account_manager", "//internal/platform/implementation/g3", # fixdeps: keep "//internal/test", @@ -126,7 +128,6 @@ cc_test( "//sharing/contacts:test_support", "//sharing/internal/api:mock_sharing_platform", "//sharing/internal/api:platform", - "//sharing/internal/public:logging", "//sharing/internal/test:nearby_test", "//sharing/local_device_data:test_support", "//sharing/proto:enums_cc_proto", diff --git a/sharing/certificates/nearby_share_certificate_manager_impl.cc b/sharing/certificates/nearby_share_certificate_manager_impl.cc index 0a62c434..27005e5a 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl.cc @@ -40,6 +40,7 @@ #include "absl/types/span.h" #include "internal/base/file_path.h" #include "internal/platform/implementation/account_manager.h" +#include "internal/platform/mac_address.h" #include "proto/identity/v1/resources.pb.h" #include "proto/identity/v1/rpcs.pb.h" #include "sharing/certificates/common.h" @@ -131,10 +132,14 @@ std::optional BuildMetadata( } metadata.set_vendor_id(vendor_id); - auto bluetooth_mac_address = context->GetBluetoothAdapter().GetAddress(); - if (!bluetooth_mac_address) return std::nullopt; - - metadata.set_bluetooth_mac_address(bluetooth_mac_address->data(), 6u); + MacAddress mac_address = context->GetBluetoothAdapter().GetAddress(); + if (mac_address.IsSet()) { + std::string mac_address_string(6, ' '); + if (mac_address.ToBytes(absl::MakeSpan( + reinterpret_cast(mac_address_string.data()), 6))) { + metadata.set_bluetooth_mac_address(mac_address_string); + } + } return metadata; } @@ -316,10 +321,9 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext:: request, [this](const absl::StatusOr& response) mutable { if (!response.ok()) { - LOG(WARNING) - << __func__ - << ": Failed to download public certificates: " - << response.status(); + LOG(WARNING) << __func__ + << ": Failed to download public certificates: " + << response.status(); std::move(download_failure_callback_)(); return; } diff --git a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc index c559edc4..ed57b295 100644 --- a/sharing/certificates/nearby_share_certificate_manager_impl_test.cc +++ b/sharing/certificates/nearby_share_certificate_manager_impl_test.cc @@ -36,6 +36,7 @@ #include "absl/time/time.h" #include "absl/types/span.h" #include "internal/platform/implementation/account_manager.h" +#include "internal/platform/mac_address.h" #include "internal/test/fake_account_manager.h" #include "proto/identity/v1/resources.pb.h" #include "proto/identity/v1/rpcs.pb.h" @@ -130,8 +131,10 @@ class NearbyShareCertificateManagerImplTest // Set default device data. local_device_data_manager_->SetDeviceName( GetNearbyShareTestMetadata().device_name()); - SetBluetoothMacAddress(kTestUnparsedBluetoothMacAddress); - SetMockBluetoothAddress(kTestUnparsedBluetoothMacAddress); + MacAddress mac_address; + MacAddress::FromString(kTestUnparsedBluetoothMacAddress, mac_address); + bluetooth_mac_address_ = mac_address; + SetMockBluetoothAddress(mac_address); } void TearDown() override { @@ -175,10 +178,6 @@ class NearbyShareCertificateManagerImplTest cert_manager_->Start(); } - void SetBluetoothMacAddress(absl::string_view bluetooth_mac_address) { - bluetooth_mac_address_ = bluetooth_mac_address; - } - // NearbyShareCertificateManager::Observer: void OnPublicCertificatesDownloaded() override { ++num_public_certs_downloaded_notifications_; @@ -211,7 +210,7 @@ class NearbyShareCertificateManagerImplTest absl::Milliseconds(1000))); } - void SetMockBluetoothAddress(absl::string_view bluetooth_mac_address) { + void SetMockBluetoothAddress(MacAddress bluetooth_mac_address) { FakeBluetoothAdapter& bluetooth_adapter = *fake_context_.fake_bluetooth_adapter(); bluetooth_adapter.SetAddress(bluetooth_mac_address); @@ -221,7 +220,7 @@ class NearbyShareCertificateManagerImplTest if (!is_present) { FakeBluetoothAdapter& bluetooth_adapter = *fake_context_.fake_bluetooth_adapter(); - bluetooth_adapter.SetAddress(""); + bluetooth_adapter.SetAddress(MacAddress{}); } } @@ -480,7 +479,7 @@ class NearbyShareCertificateManagerImplTest FakeNearbyShareScheduler* public_cert_exp_scheduler_ = nullptr; FakeNearbyShareScheduler* upload_scheduler_ = nullptr; FakeNearbyShareScheduler* download_scheduler_ = nullptr; - std::string bluetooth_mac_address_ = kTestUnparsedBluetoothMacAddress; + MacAddress bluetooth_mac_address_; size_t num_public_certs_downloaded_notifications_ = 0; size_t num_private_certs_changed_notifications_ = 0; std::vector private_certificates_; @@ -869,8 +868,8 @@ TEST_F(NearbyShareCertificateManagerImplTest, cert_manager_->Start(); - // Expect failure because a Bluetooth MAC address is required. - InvokePrivateCertificateRefresh(/*expected_success=*/false); + // Bluetooth MAC address is optional, so the refresh should still succeed. + InvokePrivateCertificateRefresh(/*expected_success=*/true); } TEST_F(NearbyShareCertificateManagerImplTest, diff --git a/sharing/internal/api/BUILD b/sharing/internal/api/BUILD index 17c7ecd2..6ea754c9 100644 --- a/sharing/internal/api/BUILD +++ b/sharing/internal/api/BUILD @@ -38,6 +38,7 @@ cc_library( ], deps = [ "//internal/base:file_path", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform/implementation:account_manager", "//proto/identity/v1:rpcs_cc_proto", @@ -74,6 +75,7 @@ cc_library( deps = [ ":platform", "//internal/base:file_path", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform/implementation:account_manager", "//sharing/analytics", diff --git a/sharing/internal/api/bluetooth_adapter.h b/sharing/internal/api/bluetooth_adapter.h index 63527d54..773f0085 100644 --- a/sharing/internal/api/bluetooth_adapter.h +++ b/sharing/internal/api/bluetooth_adapter.h @@ -15,11 +15,12 @@ #ifndef THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_BLUETOOTH_ADAPTER_H_ #define THIRD_PARTY_NEARBY_SHARING_INTERNAL_API_BLUETOOTH_ADAPTER_H_ -#include #include #include #include +#include "internal/platform/mac_address.h" + namespace nearby { namespace sharing { namespace api { @@ -88,7 +89,7 @@ class BluetoothAdapter { virtual std::optional GetAdapterId() const = 0; // The mac address of this adapter. - virtual std::optional> GetAddress() const = 0; + virtual MacAddress GetAddress() const = 0; // Adds and removes observers for events on this bluetooth adapter. If // monitoring multiple adapters, check the |adapter| parameter of observer diff --git a/sharing/internal/api/mock_bluetooth_adapter.h b/sharing/internal/api/mock_bluetooth_adapter.h index 66cce74f..e188b631 100644 --- a/sharing/internal/api/mock_bluetooth_adapter.h +++ b/sharing/internal/api/mock_bluetooth_adapter.h @@ -22,6 +22,7 @@ #include #include "gmock/gmock.h" +#include "internal/platform/mac_address.h" #include "sharing/internal/api/bluetooth_adapter.h" namespace nearby::sharing::api { @@ -46,7 +47,7 @@ class MockBluetoothAdapter : public nearby::sharing::api::BluetoothAdapter { std::function error_callback), (override)); MOCK_METHOD(std::optional, GetAdapterId, (), (const, override)); - MOCK_METHOD((std::optional>), GetAddress, (), + MOCK_METHOD((MacAddress), GetAddress, (), (const, override)); MOCK_METHOD(void, AddObserver, (Observer * observer), (override)); MOCK_METHOD(void, RemoveObserver, (Observer * observer), (override)); diff --git a/sharing/internal/test/BUILD b/sharing/internal/test/BUILD index 682d30ab..7eddde77 100644 --- a/sharing/internal/test/BUILD +++ b/sharing/internal/test/BUILD @@ -38,11 +38,10 @@ cc_library( deps = [ "//internal/base", "//internal/base:bluetooth_address", - "//internal/network:url", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/test", "//sharing/internal/api:platform", - "//sharing/internal/public:logging", "//sharing/internal/public:types", "//sharing/proto:share_cc_proto", "@com_google_absl//absl/base:core_headers", @@ -70,15 +69,12 @@ cc_test( shard_count = 8, deps = [ ":nearby_test", - "//internal/network:types", + "//internal/platform:mac_address", "//internal/platform:types", "//internal/platform/implementation/g3", # fixdeps: keep "//sharing/internal/api:platform", # fixdeps: keep "//sharing/internal/public:types", "@com_github_protobuf_matchers//protobuf-matchers", - "@com_google_absl//absl/container:flat_hash_map", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", diff --git a/sharing/internal/test/fake_bluetooth_adapter.h b/sharing/internal/test/fake_bluetooth_adapter.h index 533d46ec..8c762573 100644 --- a/sharing/internal/test/fake_bluetooth_adapter.h +++ b/sharing/internal/test/fake_bluetooth_adapter.h @@ -17,15 +17,12 @@ #include -#include #include #include #include -#include "absl/strings/string_view.h" -#include "absl/types/span.h" -#include "internal/base/bluetooth_address.h" #include "internal/base/observer_list.h" +#include "internal/platform/mac_address.h" #include "sharing/internal/api/bluetooth_adapter.h" namespace nearby { @@ -82,21 +79,12 @@ class FakeBluetoothAdapter : public sharing::api::BluetoothAdapter { std::optional GetAdapterId() const override { return "nearby"; } - std::optional> GetAddress() const override { - std::array output; - if (mac_address_.has_value() && device::ParseBluetoothAddress( - mac_address_.value(), - absl::MakeSpan(output.data(), output.size()))) { - return output; - } - return {}; + MacAddress GetAddress() const override { + return mac_address_; } - void SetAddress(std::optional bluetooth_address) { - mac_address_ = std::nullopt; - if (bluetooth_address.has_value()) { - mac_address_ = std::make_optional(std::string(bluetooth_address.value())); - } + void SetAddress(MacAddress bluetooth_address) { + mac_address_ = bluetooth_address; } void AddObserver(Observer* observer) override { @@ -175,7 +163,7 @@ class FakeBluetoothAdapter : public sharing::api::BluetoothAdapter { private: ObserverList observer_list_; - std::optional mac_address_; + MacAddress mac_address_; bool is_present_ = true; bool is_powered_ = true; bool is_low_energy_supported_ = true; diff --git a/sharing/internal/test/fake_bluetooth_adapter_test.cc b/sharing/internal/test/fake_bluetooth_adapter_test.cc index 268b2a1f..2e447dc7 100644 --- a/sharing/internal/test/fake_bluetooth_adapter_test.cc +++ b/sharing/internal/test/fake_bluetooth_adapter_test.cc @@ -16,12 +16,11 @@ #include -#include #include -#include #include "gtest/gtest.h" #include "absl/strings/string_view.h" +#include "internal/platform/mac_address.h" #include "sharing/internal/api/bluetooth_adapter.h" #include "sharing/internal/test/fake_bluetooth_adapter_observer.h" @@ -89,10 +88,11 @@ TEST(FakeBluetoothAdapter, GetAdapterId) { TEST(FakeBluetoothAdapter, GetAddress) { FakeBluetoothAdapter fake_bluetooth_adapter; - fake_bluetooth_adapter.SetAddress("1a:1b:1c:1d:1e:1f"); - // Expected conversion from "1a:1b:1c:1d:1e:1f" - std::array expected_output{{26, 27, 28, 29, 30, 31}}; - EXPECT_EQ(fake_bluetooth_adapter.GetAddress(), expected_output); + MacAddress mac_address; + MacAddress::FromString("1a:1b:1c:1d:1e:1f", mac_address); + fake_bluetooth_adapter.SetAddress(mac_address); + EXPECT_EQ(fake_bluetooth_adapter.GetAddress().ToString(), + "1A:1B:1C:1D:1E:1F"); } TEST(FakeBluetoothAdapter, AddObserver) { diff --git a/sharing/nearby_sharing_service_impl.cc b/sharing/nearby_sharing_service_impl.cc index 1b90f1a0..d6bfd95e 100644 --- a/sharing/nearby_sharing_service_impl.cc +++ b/sharing/nearby_sharing_service_impl.cc @@ -42,7 +42,6 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "absl/types/span.h" -#include "internal/base/bluetooth_address.h" #include "internal/base/file_path.h" #include "internal/flags/nearby_flags.h" #include "internal/network/url.h" @@ -639,9 +638,7 @@ void NearbySharingServiceImpl::RegisterReceiveSurface( } else { VLOG(1) << __func__ << ": This device's MAC address is: " - << nearby::device::CanonicalizeBluetoothAddress( - context_->GetBluetoothAdapter().GetAddress().value_or( - std::array{})); + << context_->GetBluetoothAdapter().GetAddress().ToString(); } }