From d41c26dd248804f95ad2a0ac2bb0077bc912ab99 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Wed, 15 Feb 2023 16:25:30 -0800 Subject: [PATCH] Added BLE v2 peripheral PiperOrigin-RevId: 509962831 --- .../platform/implementation/windows/BUILD | 3 + .../platform/implementation/windows/ble_v2.cc | 6 +- .../platform/implementation/windows/ble_v2.h | 7 +-- .../windows/ble_v2_peripheral.cc | 57 +++++++++++++++++++ .../windows/ble_v2_peripheral.h | 47 +++++++++++++++ .../windows/ble_v2_peripheral_test.cc | 40 +++++++++++++ 6 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 internal/platform/implementation/windows/ble_v2_peripheral.cc create mode 100644 internal/platform/implementation/windows/ble_v2_peripheral.h create mode 100644 internal/platform/implementation/windows/ble_v2_peripheral_test.cc diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 0842c745..a7d37086 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -60,6 +60,7 @@ cc_library( "ble_peripheral.h", "ble_socket.h", "ble_v2.h", + "ble_v2_peripheral.h", "bluetooth_adapter.h", "bluetooth_classic.h", "bluetooth_classic_device.h", @@ -133,6 +134,7 @@ cc_library( "ble_medium.cc", "ble_socket.cc", "ble_v2.cc", + "ble_v2_peripheral.cc", "bluetooth_adapter.cc", "bluetooth_classic_device.cc", "bluetooth_classic_medium.cc", @@ -221,6 +223,7 @@ cc_test( "atomic_boolean_test.cc", "atomic_reference_test.cc", "ble_medium_test.cc", + "ble_v2_peripheral_test.cc", "ble_v2_test.cc", "bluetooth_adapter_test.cc", "count_down_latch_test.cc", diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble_v2.cc index 61031de4..5c2b5883 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble_v2.cc @@ -14,12 +14,14 @@ #include "internal/platform/implementation/windows/ble_v2.h" +#include #include #include #include #include "absl/synchronization/mutex.h" #include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/windows/ble_v2_peripheral.h" #include "internal/platform/logging.h" #include "winrt/Windows.Devices.Bluetooth.Advertisement.h" #include "winrt/Windows.Devices.Bluetooth.h" @@ -83,13 +85,9 @@ std::string TxPowerLevelToName(TxPowerLevel tx_power_level) { } // namespace -std::string BleV2Peripheral::GetAddress() const { return ""; } - BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter) : adapter_(dynamic_cast(&adapter)) {} -BleV2Medium::~BleV2Medium() {} - // advertisement packet and populate accordingly bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data, AdvertiseParameters advertising_parameters) { diff --git a/internal/platform/implementation/windows/ble_v2.h b/internal/platform/implementation/windows/ble_v2.h index ad1352e3..1865793c 100644 --- a/internal/platform/implementation/windows/ble_v2.h +++ b/internal/platform/implementation/windows/ble_v2.h @@ -30,16 +30,11 @@ namespace nearby { namespace windows { -class BleV2Peripheral : public api::ble_v2::BlePeripheral { - public: - std::string GetAddress() const override; -}; - // Container of operations that can be performed over the BLE medium. class BleV2Medium : public api::ble_v2::BleMedium { public: explicit BleV2Medium(api::BluetoothAdapter& adapter); - ~BleV2Medium() override; + ~BleV2Medium() override = default; // Returns true once the Ble advertising has been initiated. bool StartAdvertising( diff --git a/internal/platform/implementation/windows/ble_v2_peripheral.cc b/internal/platform/implementation/windows/ble_v2_peripheral.cc new file mode 100644 index 00000000..b1e3597d --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_peripheral.cc @@ -0,0 +1,57 @@ +// Copyright 2023 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/implementation/windows/ble_v2_peripheral.h" + +#include + +#include "absl/strings/string_view.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace windows { +namespace { +constexpr int kMacAddressLength = 17; +} + +bool BleV2Peripheral::SetAddress(absl::string_view address) { + // The address must be in format "00:B0:D0:63:C2:26". + if (address.size() != kMacAddressLength) { + NEARBY_LOGS(ERROR) << ": Invalid MAC address length."; + return false; + } + + 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; + } + } + + NEARBY_LOGS(ERROR) << ": Invalid MAC address format."; + return false; + } + + address_ = std::string(address); + return true; +} + +} // namespace windows +} // namespace nearby diff --git a/internal/platform/implementation/windows/ble_v2_peripheral.h b/internal/platform/implementation/windows/ble_v2_peripheral.h new file mode 100644 index 00000000..984e3af8 --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_peripheral.h @@ -0,0 +1,47 @@ +// Copyright 2023 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. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_ + +#include + +#include "absl/strings/string_view.h" +#include "internal/platform/implementation/ble_v2.h" + +namespace nearby { +namespace windows { + +// BLE peripheral implementation for BLE version 2. Must contain enough data +// about a particular BLE device to connect to its GATT server. +class BleV2Peripheral : public api::ble_v2::BlePeripheral { + public: + ~BleV2Peripheral() override = default; + + // Returns the MAC address of the peripheral. The format is in + // "00:B0:D0:63:C2:26". + std::string GetAddress() const override { return address_; } + + // 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); + + private: + std::string address_; +}; + +} // namespace windows +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_PERIPHERAL_H_ diff --git a/internal/platform/implementation/windows/ble_v2_peripheral_test.cc b/internal/platform/implementation/windows/ble_v2_peripheral_test.cc new file mode 100644 index 00000000..a1ed3843 --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_peripheral_test.cc @@ -0,0 +1,40 @@ +// Copyright 2023 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/implementation/windows/ble_v2_peripheral.h" + +#include "gtest/gtest.h" + +namespace nearby { +namespace windows { +namespace { + +TEST(BleV2Peripheral, SetMacAddress) { + BleV2Peripheral ble_peripheral; + 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) { + BleV2Peripheral ble_peripheral; + EXPECT_TRUE(ble_peripheral.SetAddress("00:B0:D0:63:C2:26")); + EXPECT_EQ(ble_peripheral.GetAddress(), "00:B0:D0:63:C2:26"); +} + +} // namespace +} // namespace windows +} // namespace nearby