mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Added BLE v2 peripheral
PiperOrigin-RevId: 509962831
This commit is contained in:
committed by
Copybara-Service
parent
fc1811f966
commit
d41c26dd24
@@ -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",
|
||||
|
||||
@@ -14,12 +14,14 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/ble_v2.h"
|
||||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#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<BluetoothAdapter*>(&adapter)) {}
|
||||
|
||||
BleV2Medium::~BleV2Medium() {}
|
||||
|
||||
// advertisement packet and populate accordingly
|
||||
bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data,
|
||||
AdvertiseParameters advertising_parameters) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 <string>
|
||||
|
||||
#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
|
||||
@@ -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 <string>
|
||||
|
||||
#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_
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user