diff --git a/connections/c/BUILD b/connections/c/BUILD index 9c61322b..bab1e014 100644 --- a/connections/c/BUILD +++ b/connections/c/BUILD @@ -55,6 +55,7 @@ cc_library( "//internal/flags:flag_reader", "//internal/flags:nearby_flags", "//internal/platform:base", + "//internal/platform:mac_address", "//internal/platform:types", "@com_google_absl//absl/base:no_destructor", "@com_google_absl//absl/container:flat_hash_map", diff --git a/connections/c/nc.cc b/connections/c/nc.cc index e7e683be..c3dad08e 100644 --- a/connections/c/nc.cc +++ b/connections/c/nc.cc @@ -49,6 +49,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/file.h" #include "internal/platform/logging.h" +#include "internal/platform/mac_address.h" #if TARGET_OS_IOS #include "internal/platform/implementation/apple/nearby_logger.h" #endif // TARGET_OS_IOS @@ -552,10 +553,22 @@ void NcRequestConnection( connection_options->keep_alive_timeout_millis; cpp_connection_options.low_power = connection_options->low_power; if (connection_options->remote_bluetooth_mac_address.size > 0) { - cpp_connection_options.remote_bluetooth_mac_address = - nearby::BluetoothUtils::FromString( + nearby::MacAddress mac_address; + if (!nearby::MacAddress::FromString( std::string(connection_options->remote_bluetooth_mac_address.data, - connection_options->remote_bluetooth_mac_address.size)); + connection_options->remote_bluetooth_mac_address.size), + mac_address) || + !mac_address.IsSet()) { + cpp_connection_options.remote_bluetooth_mac_address = nearby::ByteArray(); + } else { + nearby::ByteArray address_bytes( + nearby::BluetoothUtils::kBluetoothMacAddressLength); + mac_address.ToBytes( + absl::MakeSpan(reinterpret_cast(address_bytes.data()), + address_bytes.size())); + cpp_connection_options.remote_bluetooth_mac_address = + std::move(address_bytes); + } } if (connection_options->common_options.strategy.type == NC_STRATEGY_TYPE_NONE) cpp_connection_options.strategy = ::nearby::connections::Strategy::kNone; diff --git a/internal/platform/BUILD b/internal/platform/BUILD index bcf30334..050b88e9 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -39,7 +39,6 @@ cc_library( name = "base", srcs = [ "base64_utils.cc", - "bluetooth_utils.cc", "input_stream.cc", "prng.cc", ], @@ -409,7 +408,6 @@ cc_library( cc_test( name = "platform_base_test", srcs = [ - "bluetooth_utils_test.cc", "byte_array_test.cc", "feature_flags_test.cc", "input_stream_test.cc", diff --git a/internal/platform/bluetooth_utils.cc b/internal/platform/bluetooth_utils.cc deleted file mode 100644 index 3a2a3581..00000000 --- a/internal/platform/bluetooth_utils.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2020 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/bluetooth_utils.h" - -#include -#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 { - -std::string BluetoothUtils::ToString(const ByteArray& bluetooth_mac_address) { - if (bluetooth_mac_address.size() != kBluetoothMacAddressLength) { - return ""; - } - MacAddress mac_address; - if (!MacAddress::FromBytes( - absl::MakeConstSpan( - reinterpret_cast(bluetooth_mac_address.data()), - bluetooth_mac_address.size()), - mac_address) || - !mac_address.IsSet()) { - return ""; - } - return mac_address.ToString(); -} - -ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) { - MacAddress mac_address; - if (!MacAddress::FromString(bluetooth_mac_address, mac_address) || - !mac_address.IsSet()) { - return ByteArray(); - } - 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 561c2155..c61390e3 100644 --- a/internal/platform/bluetooth_utils.h +++ b/internal/platform/bluetooth_utils.h @@ -15,30 +15,11 @@ #ifndef PLATFORM_BASE_BLUETOOTH_UTILS_H_ #define PLATFORM_BASE_BLUETOOTH_UTILS_H_ -#include -#include - -#include "absl/base/attributes.h" -#include "absl/strings/string_view.h" -#include "internal/platform/byte_array.h" - namespace nearby { class BluetoothUtils { public: static constexpr int kBluetoothMacAddressLength = 6; - - // Converts a Bluetooth MAC address from byte array to String format. Returns - // empty if input byte array is not of correct format. - // e.g. {-84, 55, 67, -68, -87, 40} -> "AC:37:43:BC:A9:28". - ABSL_DEPRECATED("Use MacAddress class instead.") - static std::string ToString(const ByteArray& bluetooth_mac_address); - - // Converts a Bluetooth MAC address from String format to byte array. Returns - // empty if input string is not of correct format. - // 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); }; } // namespace nearby diff --git a/internal/platform/bluetooth_utils_test.cc b/internal/platform/bluetooth_utils_test.cc deleted file mode 100644 index 80dabc35..00000000 --- a/internal/platform/bluetooth_utils_test.cc +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2020 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/bluetooth_utils.h" - -#include "gtest/gtest.h" - -namespace nearby { - -constexpr absl::string_view kBluetoothMacAddress{"00:00:E6:88:64:13"}; -constexpr char kBluetoothMacAddressBytes[] = {0x00, 0x00, 0xe6, - 0x88, 0x64, 0x13}; - -TEST(BluetoothUtilsTest, ToStringWorks) { - ByteArray bt_mac_address_bytes{kBluetoothMacAddressBytes, - sizeof(kBluetoothMacAddressBytes)}; - - auto bt_mac_address = BluetoothUtils::ToString(bt_mac_address_bytes); - - EXPECT_EQ(kBluetoothMacAddress, bt_mac_address); -} - -TEST(BluetoothUtilsTest, FromStringWorks) { - ByteArray bt_mac_address_bytes{kBluetoothMacAddressBytes, - sizeof(kBluetoothMacAddressBytes)}; - - auto bt_mac_address_bytes_result = - BluetoothUtils::FromString(kBluetoothMacAddress); - - EXPECT_EQ(bt_mac_address_bytes, bt_mac_address_bytes_result); -} - -TEST(BluetoothUtilsTest, InvalidBytesReturnsEmptyString) { - std::string string_result; - - char bad_bt_mac_address_1[] = {0x02, 0x20, 0x00}; - ByteArray bad_bt_mac_address_bytes_1{bad_bt_mac_address_1, - sizeof(bad_bt_mac_address_1)}; - string_result = BluetoothUtils::ToString(bad_bt_mac_address_bytes_1); - EXPECT_TRUE(string_result.empty()); - - char bad_bt_mac_address_2[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - ByteArray bad_bt_mac_address_bytes_2{bad_bt_mac_address_2, - sizeof(bad_bt_mac_address_2)}; - string_result = BluetoothUtils::ToString(bad_bt_mac_address_bytes_2); - EXPECT_TRUE(string_result.empty()); - - char bad_bt_mac_address_3[] = {0x11, 0x22, 0x33, 0x44, 0x55, - 0x66, 0x77, 0x88, 0x99}; - ByteArray bad_bt_mac_address_bytes_3{bad_bt_mac_address_3, - sizeof(bad_bt_mac_address_3)}; - string_result = BluetoothUtils::ToString(bad_bt_mac_address_bytes_3); - EXPECT_TRUE(string_result.empty()); -} - -TEST(BluetoothUtilsTest, InvalidStringReturnsEmptyByteArray) { - ByteArray bytes_result; - - std::string bad_bt_mac_address_1 = "022:00"; - bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_1); - EXPECT_TRUE(bytes_result.Empty()); - - std::string bad_bt_mac_address_2 = "22:00:11:33:77:aa::bb::99"; - bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_2); - EXPECT_TRUE(bytes_result.Empty()); - - std::string bad_bt_mac_address_3 = "00:00:00:00:00:00"; - bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_3); - EXPECT_TRUE(bytes_result.Empty()); - - std::string bad_bt_mac_address_4 = "BLUETOOTHCHIP"; - bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_4); - EXPECT_TRUE(bytes_result.Empty()); - - std::string bad_bt_mac_address_5 = "G1:F2:F3:F4:F5:F6"; - bytes_result = BluetoothUtils::FromString(bad_bt_mac_address_5); - EXPECT_TRUE(bytes_result.Empty()); -} - -} // namespace nearby diff --git a/internal/platform/byte_array.h b/internal/platform/byte_array.h index ae7cefcf..2951c04f 100644 --- a/internal/platform/byte_array.h +++ b/internal/platform/byte_array.h @@ -81,22 +81,6 @@ 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 (size_t 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 973b24fc..62a31339 100644 --- a/internal/platform/byte_array_test.cc +++ b/internal/platform/byte_array_test.cc @@ -104,27 +104,6 @@ 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>);