mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Create MacAddress class for MAC address manipulation.
PiperOrigin-RevId: 752503841
This commit is contained in:
committed by
Copybara-Service
parent
422030f826
commit
83ec6bcece
@@ -251,6 +251,7 @@ cc_library(
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:mac_address",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/flags:platform_flags",
|
||||
|
||||
@@ -13,51 +13,26 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "internal/platform/implementation/windows/ble_v2_peripheral.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/bluetooth_utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
namespace {
|
||||
constexpr int kMacAddressLength = 17;
|
||||
}
|
||||
|
||||
BleV2Peripheral::BleV2Peripheral(absl::string_view address) {
|
||||
if (SetAddress(address)) {
|
||||
unique_id_ = static_cast<UniqueId>(BluetoothUtils::ToNumber(address));
|
||||
if (!MacAddress::FromString(address, mac_address_)) {
|
||||
LOG(WARNING) << "Create BleV2Peripheral with invalid MAC: " << address;
|
||||
}
|
||||
}
|
||||
|
||||
bool BleV2Peripheral::SetAddress(absl::string_view address) {
|
||||
// The address must be in format "00:B0:D0:63:C2:26".
|
||||
if (address.size() != kMacAddressLength) {
|
||||
LOG(ERROR) << ": Invalid MAC address length.";
|
||||
return false;
|
||||
std::string BleV2Peripheral::GetAddress() const {
|
||||
if (mac_address_.IsSet()) {
|
||||
return mac_address_.ToString();
|
||||
}
|
||||
|
||||
for (int i = 0; i < kMacAddressLength; ++i) {
|
||||
if ((i % 3) == 0 || (i % 3) == 1) {
|
||||
if ((address[i] >= '0' && address[i] <= '9') ||
|
||||
(address[i] >= 'a' && address[i] <= 'f') ||
|
||||
(address[i] >= 'A' && address[i] <= 'F')) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (address[i] == ':') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
LOG(ERROR) << ": Invalid MAC address format.";
|
||||
return false;
|
||||
}
|
||||
|
||||
address_ = std::string(address);
|
||||
return true;
|
||||
return "";
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -31,21 +32,17 @@ class BleV2Peripheral : public api::ble_v2::BlePeripheral {
|
||||
explicit BleV2Peripheral(absl::string_view address);
|
||||
~BleV2Peripheral() override = default;
|
||||
|
||||
// Returns the MAC address of the peripheral. The format is in
|
||||
// Returns the MAC address of the peripheral or empty string. The format is in
|
||||
// "00:B0:D0:63:C2:26".
|
||||
std::string GetAddress() const override { return address_; }
|
||||
std::string GetAddress() const override;
|
||||
|
||||
UniqueId GetUniqueId() const override { return unique_id_; }
|
||||
// Sets the MAC address of the peripheral. The address format must be in
|
||||
// pattern of "00:B0:D0:63:C2:26".
|
||||
bool SetAddress(absl::string_view address);
|
||||
UniqueId GetUniqueId() const override { return mac_address_.address(); }
|
||||
|
||||
bool Ok() const { return unique_id_ != 0; }
|
||||
bool Ok() const { return mac_address_.IsSet(); }
|
||||
explicit operator bool() const { return Ok(); }
|
||||
|
||||
private:
|
||||
std::string address_;
|
||||
UniqueId unique_id_ = 0;
|
||||
MacAddress mac_address_;
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "internal/platform/implementation/windows/ble_v2_peripheral.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -26,38 +27,10 @@ TEST(BleV2Peripheral, Constructor) {
|
||||
|
||||
EXPECT_TRUE(ble_peripheral);
|
||||
EXPECT_TRUE(ble_peripheral.Ok());
|
||||
EXPECT_NE(ble_peripheral.GetUniqueId(), 0);
|
||||
EXPECT_EQ(ble_peripheral.GetUniqueId(), 0xf1f2f3f4f5f6);
|
||||
EXPECT_EQ(ble_peripheral.GetAddress(), kAddress);
|
||||
}
|
||||
|
||||
TEST(BleV2Peripheral, SetMacAddress) {
|
||||
BleV2Peripheral ble_peripheral("F1:F2:F3:F4:F5:F6");
|
||||
EXPECT_TRUE(ble_peripheral.SetAddress("00:B0:D0:63:C2:26"));
|
||||
EXPECT_FALSE(ble_peripheral.SetAddress("00:B0:D0:6T:C2:26"));
|
||||
EXPECT_FALSE(ble_peripheral.SetAddress("00:B0:D0:63:C2:2"));
|
||||
EXPECT_FALSE(ble_peripheral.SetAddress("0:B0:D0:63:C2:203"));
|
||||
EXPECT_FALSE(ble_peripheral.SetAddress("0:B0:D0:6P:C2:203"));
|
||||
}
|
||||
|
||||
TEST(BleV2Peripheral, SetAndGetMacAddress) {
|
||||
constexpr absl::string_view kChangedAddress = "00:B0:D0:63:C2:26";
|
||||
BleV2Peripheral ble_peripheral("F1:F2:F3:F4:F5:F6");
|
||||
|
||||
EXPECT_TRUE(ble_peripheral.SetAddress(kChangedAddress));
|
||||
EXPECT_EQ(ble_peripheral.GetAddress(), kChangedAddress);
|
||||
}
|
||||
|
||||
TEST(BleV2Peripheral, SetAddressDoesNotChangeUniqueId) {
|
||||
constexpr absl::string_view kChangedAddress = "00:B0:D0:63:C2:26";
|
||||
BleV2Peripheral ble_peripheral("F1:F2:F3:F4:F5:F6");
|
||||
BleV2Peripheral::UniqueId unique_id = ble_peripheral.GetUniqueId();
|
||||
|
||||
EXPECT_NE(unique_id, 0);
|
||||
EXPECT_TRUE(ble_peripheral.SetAddress(kChangedAddress));
|
||||
EXPECT_EQ(ble_peripheral.GetAddress(), kChangedAddress);
|
||||
EXPECT_EQ(ble_peripheral.GetUniqueId(), unique_id);
|
||||
}
|
||||
|
||||
TEST(BleV2Peripheral, ConstructFromBadAddress) {
|
||||
BleV2Peripheral ble_peripheral("G1:F2:F3:F4:F5:F6");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user