mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Cleanup unused code.
PiperOrigin-RevId: 800660305
This commit is contained in:
committed by
Copybara-Service
parent
000e12da8e
commit
a4ce4b1781
@@ -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",
|
||||
|
||||
@@ -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 <cstdint>
|
||||
#include <string>
|
||||
|
||||
#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<const uint8_t*>(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<uint8_t*>(address_bytes.data()), address_bytes.size()));
|
||||
return address_bytes;
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
@@ -15,30 +15,11 @@
|
||||
#ifndef PLATFORM_BASE_BLUETOOTH_UTILS_H_
|
||||
#define PLATFORM_BASE_BLUETOOTH_UTILS_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#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
|
||||
|
||||
@@ -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
|
||||
@@ -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<uint64_t> 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<uint64_t>(data()[i])) & 0xFF);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char* data() { return &data_[0]; }
|
||||
std::string string_data() const { return data_; }
|
||||
const char* data() const { return data_.data(); }
|
||||
|
||||
@@ -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<decltype(std::declval<ByteArray>().begin()),
|
||||
ByteArray::iterator>);
|
||||
|
||||
Reference in New Issue
Block a user