mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Add BluetoothUtils::FromNumber and ToNumber
The implementation is compatible with Windows interpretation of 64 bit MAC addresses. PiperOrigin-RevId: 531355253
This commit is contained in:
committed by
Copybara-Service
parent
9dff873424
commit
9c5b5d4993
@@ -49,7 +49,6 @@ ByteArray BluetoothUtils::FromString(absl::string_view bluetooth_mac_address) {
|
||||
if (bt_mac_address.length() != kBluetoothMacAddressLength * 2) {
|
||||
return ByteArray();
|
||||
}
|
||||
|
||||
// Convert to bytes. If MAC Address bytes are unset, return a null byte array.
|
||||
auto bt_mac_address_string(absl::HexStringToBytes(bt_mac_address));
|
||||
auto bt_mac_address_bytes =
|
||||
@@ -70,4 +69,27 @@ bool BluetoothUtils::IsBluetoothMacAddressUnset(
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string BluetoothUtils::FromNumber(std::uint64_t address) {
|
||||
// Gets byte `index` from `address`.
|
||||
auto b = [&](int index) {
|
||||
return (std::uint8_t)(address >> (56 - 8 * index));
|
||||
};
|
||||
return absl::StrFormat("%02X:%02X:%02X:%02X:%02X:%02X", b(2), b(3), b(4),
|
||||
b(5), b(6), b(7));
|
||||
}
|
||||
|
||||
std::uint64_t BluetoothUtils::ToNumber(std::string address) {
|
||||
ByteArray binary = FromString(address);
|
||||
if (binary.size() != kBluetoothMacAddressLength) {
|
||||
return 0;
|
||||
}
|
||||
std::uint64_t result = 0;
|
||||
for (char ch : binary.AsStringView()) {
|
||||
result <<= 8;
|
||||
result |= static_cast<uint8_t>(ch);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -34,6 +34,14 @@ class BluetoothUtils {
|
||||
// e.g. "AC:37:43:BC:A9:28" -> {-84, 55, 67, -68, -87, 40}.
|
||||
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"
|
||||
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
|
||||
static std::uint64_t ToNumber(std::string address);
|
||||
|
||||
// Checks if a Bluetooth MAC address is zero for every byte.
|
||||
static bool IsBluetoothMacAddressUnset(
|
||||
const ByteArray& bluetooth_mac_address);
|
||||
|
||||
@@ -84,4 +84,16 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user