mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
[BLE Refactor] Refactor BlePeripheral class.
PiperOrigin-RevId: 447638179
This commit is contained in:
committed by
Copybara-Service
parent
6347f80a11
commit
12cef58420
@@ -333,12 +333,12 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
std::move(peripheral), advertisement_data,
|
||||
{
|
||||
.fetch_advertisements =
|
||||
[&](int num_slots, int psm,
|
||||
[&](BleV2Peripheral peripheral,
|
||||
int num_slots, int psm,
|
||||
const std::vector<std::string>&
|
||||
interesting_service_ids,
|
||||
mediums::AdvertisementReadResult&
|
||||
advertisement_read_result,
|
||||
BleV2Peripheral& peripheral) {
|
||||
advertisement_read_result) {
|
||||
// Th`mutex_` is already held here. Use
|
||||
// `AssumeHeld` tell the thread
|
||||
// annotation static analysis that
|
||||
@@ -346,10 +346,9 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
// locked.
|
||||
AssumeHeld(mutex_);
|
||||
ProcessFetchGattAdvertisementsRequest(
|
||||
num_slots, psm,
|
||||
interesting_service_ids,
|
||||
advertisement_read_result,
|
||||
peripheral);
|
||||
std::move(peripheral), num_slots,
|
||||
psm, interesting_service_ids,
|
||||
advertisement_read_result);
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -488,10 +487,9 @@ bool BleV2::GenerateAdvertisementCharacteristic(
|
||||
}
|
||||
|
||||
void BleV2::ProcessFetchGattAdvertisementsRequest(
|
||||
int num_slots, int psm,
|
||||
BleV2Peripheral peripheral, int num_slots, int psm,
|
||||
const std::vector<std::string>& interesting_service_ids,
|
||||
mediums::AdvertisementReadResult& advertisement_read_result,
|
||||
BleV2Peripheral& peripheral) {
|
||||
mediums::AdvertisementReadResult& advertisement_read_result) {
|
||||
if (!peripheral.IsValid()) {
|
||||
NEARBY_LOGS(INFO) << "Can't read from an advertisement GATT server because "
|
||||
"ble peripheral is null.";
|
||||
|
||||
@@ -131,10 +131,10 @@ class BleV2 final {
|
||||
GattServer& gatt_server)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
void ProcessFetchGattAdvertisementsRequest(
|
||||
int num_slots, int psm,
|
||||
BleV2Peripheral peripheral, int num_slots, int psm,
|
||||
const std::vector<std::string>& interesting_service_ids,
|
||||
mediums::AdvertisementReadResult& advertisement_read_result,
|
||||
BleV2Peripheral& peripheral) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
mediums::AdvertisementReadResult& advertisement_read_result)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
bool StopAdvertisementGattServerLocked()
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ cc_library(
|
||||
"ble_advertisement.h",
|
||||
"ble_advertisement_header.h",
|
||||
"ble_packet.h",
|
||||
"ble_peripheral.h",
|
||||
"ble_utils.h",
|
||||
"bloom_filter.h",
|
||||
"discovered_peripheral_callback.h",
|
||||
@@ -64,7 +63,6 @@ cc_test(
|
||||
"ble_advertisement_header_test.cc",
|
||||
"ble_advertisement_test.cc",
|
||||
"ble_packet_test.cc",
|
||||
"ble_peripheral_test.cc",
|
||||
"ble_utils_test.cc",
|
||||
"bloom_filter_test.cc",
|
||||
"discovered_peripheral_tracker_test.cc",
|
||||
|
||||
@@ -1,63 +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.
|
||||
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PERIPHERAL_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PERIPHERAL_H_
|
||||
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
// TODO(b/213835576): The peripheral class is for NearbyConnections to transmit
|
||||
// "advertisement byte array" when peripheral discovered in
|
||||
// 'discovered_peripheral_track' class, which is not the same as the one in
|
||||
// BluetoothAdapter. We need to see how to differentiate between this
|
||||
// BlePeripheral and BleV2Peripheral in BluetoothAdapter.
|
||||
class BlePeripheral {
|
||||
public:
|
||||
BlePeripheral() = default;
|
||||
explicit BlePeripheral(const ByteArray& id)
|
||||
: BlePeripheral(id, BleAdvertisementHeader::kDefaultPsmValue) {}
|
||||
BlePeripheral(const ByteArray& id, int psm) : id_(id), psm_(psm) {}
|
||||
BlePeripheral(const BlePeripheral&) = default;
|
||||
BlePeripheral& operator=(const BlePeripheral&) = default;
|
||||
BlePeripheral(BlePeripheral&&) = default;
|
||||
BlePeripheral& operator=(BlePeripheral&&) = default;
|
||||
~BlePeripheral() = default;
|
||||
|
||||
bool IsValid() const { return !id_.Empty(); }
|
||||
ByteArray GetId() const { return id_; }
|
||||
int GetPsm() const { return psm_; }
|
||||
|
||||
private:
|
||||
// A unique identifier for this peripheral. It is the BLE advertisement it
|
||||
// was found on, or even simply the BLE MAC address.
|
||||
ByteArray id_;
|
||||
|
||||
// The psm (protocol service multiplexer) value is used for create data
|
||||
// connection on L2CAP socket. It only exists when remote device supports
|
||||
// L2CAP socket feature.
|
||||
int psm_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PERIPHERAL_H_
|
||||
@@ -1,60 +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 "connections/implementation/mediums/ble_v2/ble_peripheral.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
namespace {
|
||||
|
||||
constexpr absl::string_view kId{"AB12"};
|
||||
constexpr int kDefaultPsm = 0;
|
||||
|
||||
TEST(BlePeripheralTest, ConstructionWorks) {
|
||||
ByteArray id{std::string(kId)};
|
||||
|
||||
BlePeripheral ble_peripheral(id);
|
||||
|
||||
EXPECT_TRUE(ble_peripheral.IsValid());
|
||||
EXPECT_EQ(id, ble_peripheral.GetId());
|
||||
EXPECT_EQ(kDefaultPsm, ble_peripheral.GetPsm());
|
||||
}
|
||||
|
||||
TEST(BlePeripheralTest, ConstructionWorksWithPsm) {
|
||||
ByteArray id{std::string(kId)};
|
||||
int psm = 1;
|
||||
|
||||
BlePeripheral ble_peripheral(id, psm);
|
||||
|
||||
EXPECT_TRUE(ble_peripheral.IsValid());
|
||||
EXPECT_EQ(id, ble_peripheral.GetId());
|
||||
EXPECT_EQ(psm, ble_peripheral.GetPsm());
|
||||
}
|
||||
|
||||
TEST(BlePeripheralTest, ConstructionEmptyFails) {
|
||||
BlePeripheral ble_peripheral;
|
||||
|
||||
EXPECT_FALSE(ble_peripheral.IsValid());
|
||||
EXPECT_TRUE(ble_peripheral.GetId().Empty());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -15,8 +15,11 @@
|
||||
#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_
|
||||
#define CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_
|
||||
|
||||
#include "connections/implementation/mediums/ble_v2/ble_peripheral.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "connections/listeners.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
|
||||
namespace location {
|
||||
@@ -26,15 +29,15 @@ namespace mediums {
|
||||
|
||||
/** Callback that is invoked when a {@link BlePeripheral} is discovered. */
|
||||
struct DiscoveredPeripheralCallback {
|
||||
std::function<void(BlePeripheral& peripheral, const std::string& service_id,
|
||||
std::function<void(BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_byts,
|
||||
bool fast_advertisement)>
|
||||
peripheral_discovered_cb =
|
||||
DefaultCallback<BlePeripheral&, const std::string&, const ByteArray&,
|
||||
DefaultCallback<BleV2Peripheral, const std::string&, const ByteArray&,
|
||||
bool>();
|
||||
std::function<void(BlePeripheral& peripheral, const std::string& service_id)>
|
||||
std::function<void(BleV2Peripheral peripheral, const std::string& service_id)>
|
||||
peripheral_lost_cb =
|
||||
DefaultCallback<BlePeripheral&, const std::string&>();
|
||||
DefaultCallback<BleV2Peripheral, const std::string&>();
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
|
||||
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
#include "internal/platform/mutex_lock.h"
|
||||
|
||||
namespace location {
|
||||
@@ -84,7 +85,7 @@ void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement(
|
||||
}
|
||||
|
||||
HandleAdvertisement(peripheral, advertisement_data);
|
||||
HandleAdvertisementHeader(advertisement_data, /*mutated=*/peripheral,
|
||||
HandleAdvertisementHeader(peripheral, advertisement_data,
|
||||
std::move(advertisement_fetcher));
|
||||
}
|
||||
|
||||
@@ -102,13 +103,16 @@ void DiscoveredPeripheralTracker::ProcessLostGattAdvertisements() {
|
||||
// Clear the map state for each lost GATT advertisement and report it to the
|
||||
// client.
|
||||
for (const auto& gatt_advertisement : lost_gatt_advertisements) {
|
||||
ClearGattAdvertisement(gatt_advertisement);
|
||||
|
||||
BlePeripheral peripheral = GenerateBlePeripheral(gatt_advertisement);
|
||||
if (peripheral.IsValid()) {
|
||||
discovered_peripheral_callback.peripheral_lost_cb(peripheral,
|
||||
service_id);
|
||||
const auto it = gatt_advertisement_infos_.find(gatt_advertisement);
|
||||
if (it != gatt_advertisement_infos_.end()) {
|
||||
BleV2Peripheral lost_peripheral = it->second.peripheral;
|
||||
if (lost_peripheral.IsValid()) {
|
||||
lost_peripheral.SetId(ByteArray(gatt_advertisement));
|
||||
discovered_peripheral_callback.peripheral_lost_cb(
|
||||
std::move(lost_peripheral), service_id);
|
||||
}
|
||||
}
|
||||
ClearGattAdvertisement(gatt_advertisement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,7 +158,7 @@ void DiscoveredPeripheralTracker::ClearGattAdvertisement(
|
||||
}
|
||||
|
||||
void DiscoveredPeripheralTracker::HandleAdvertisement(
|
||||
const BleV2Peripheral& peripheral,
|
||||
BleV2Peripheral peripheral,
|
||||
const location::nearby::api::ble_v2::BleAdvertisementData&
|
||||
advertisement_data) {
|
||||
ByteArray advertisement_bytes =
|
||||
@@ -197,9 +201,8 @@ void DiscoveredPeripheralTracker::HandleAdvertisement(
|
||||
{advertisement_header, std::make_unique<AdvertisementReadResult>()});
|
||||
|
||||
BleAdvertisementHeader new_advertisement_header = HandleRawGattAdvertisements(
|
||||
advertisement_header, {&advertisement_bytes}, service_uuid);
|
||||
UpdateCommonStateForFoundBleAdvertisement(new_advertisement_header,
|
||||
/*mac_address=*/peripheral.GetId());
|
||||
peripheral, advertisement_header, {&advertisement_bytes}, service_uuid);
|
||||
UpdateCommonStateForFoundBleAdvertisement(new_advertisement_header);
|
||||
}
|
||||
|
||||
ByteArray DiscoveredPeripheralTracker::ExtractInterestingAdvertisementBytes(
|
||||
@@ -237,6 +240,7 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::CreateAdvertisementHeader(
|
||||
}
|
||||
|
||||
BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements(
|
||||
BleV2Peripheral peripheral,
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
const std::vector<const ByteArray*>& gatt_advertisement_bytes_list,
|
||||
const std::string& service_uuid) {
|
||||
@@ -282,11 +286,13 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements(
|
||||
<< service_id;
|
||||
continue;
|
||||
}
|
||||
BlePeripheral discovered_ble_peripheral =
|
||||
GenerateBlePeripheral(gatt_advertisement, new_psm);
|
||||
if (discovered_ble_peripheral.IsValid()) {
|
||||
if (peripheral.IsValid()) {
|
||||
peripheral.SetPsm(new_psm);
|
||||
BleV2Peripheral discovered_peripheral = peripheral;
|
||||
discovered_peripheral.SetId(ByteArray(gatt_advertisement));
|
||||
sii_it->second.discovered_peripheral_callback.peripheral_discovered_cb(
|
||||
discovered_ble_peripheral, service_id, gatt_advertisement.GetData(),
|
||||
std::move(discovered_peripheral), service_id,
|
||||
gatt_advertisement.GetData(),
|
||||
gatt_advertisement.IsFastAdvertisement());
|
||||
}
|
||||
} else if (old_advertisement_header.GetPsm() !=
|
||||
@@ -309,7 +315,8 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements(
|
||||
GattAdvertisementInfo gatt_advertisement_info = {
|
||||
.service_id = service_id,
|
||||
.advertisement_header = new_advertisement_header,
|
||||
.mac_address = {}};
|
||||
.mac_address = peripheral.GetAddress(),
|
||||
.peripheral = peripheral};
|
||||
gatt_advertisement_infos_.insert_or_assign(
|
||||
gatt_advertisement, std::move(gatt_advertisement_info));
|
||||
}
|
||||
@@ -431,9 +438,10 @@ bool DiscoveredPeripheralTracker::IsDummyAdvertisementHeader(
|
||||
}
|
||||
|
||||
void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
|
||||
BleV2Peripheral peripheral,
|
||||
const location::nearby::api::ble_v2::BleAdvertisementData&
|
||||
advertisement_data,
|
||||
BleV2Peripheral& peripheral, AdvertisementFetcher advertisement_fetcher) {
|
||||
AdvertisementFetcher advertisement_fetcher) {
|
||||
// Attempt to parse the advertisement header.
|
||||
BleAdvertisementHeader advertisement_header(
|
||||
ExtractAdvertisementHeaderBytes(advertisement_data));
|
||||
@@ -457,20 +465,19 @@ void DiscoveredPeripheralTracker::HandleAdvertisementHeader(
|
||||
if (ShouldReadRawAdvertisementFromServer(advertisement_header)) {
|
||||
// Determine whether or not we need to read a fresh GATT advertisement.
|
||||
std::vector<const ByteArray*> gatt_advertisement_bytes_list =
|
||||
FetchRawAdvertisements(advertisement_header,
|
||||
/*mutated=*/peripheral,
|
||||
FetchRawAdvertisements(peripheral, advertisement_header,
|
||||
std::move(advertisement_fetcher));
|
||||
if (!gatt_advertisement_bytes_list.empty()) {
|
||||
HandleRawGattAdvertisements(advertisement_header,
|
||||
gatt_advertisement_bytes_list, "");
|
||||
HandleRawGattAdvertisements(peripheral, advertisement_header,
|
||||
gatt_advertisement_bytes_list,
|
||||
/*service_uuid=*/"");
|
||||
}
|
||||
}
|
||||
|
||||
// Regardless of whether or not we read a new GATT advertisement, the maps
|
||||
// should now be up-to-date. With this information, do some general
|
||||
// housekeeping.
|
||||
UpdateCommonStateForFoundBleAdvertisement(advertisement_header,
|
||||
/*mac_address=*/peripheral.GetId());
|
||||
UpdateCommonStateForFoundBleAdvertisement(advertisement_header);
|
||||
}
|
||||
|
||||
ByteArray DiscoveredPeripheralTracker::ExtractAdvertisementHeaderBytes(
|
||||
@@ -556,8 +563,9 @@ bool DiscoveredPeripheralTracker::ShouldReadRawAdvertisementFromServer(
|
||||
|
||||
std::vector<const ByteArray*>
|
||||
DiscoveredPeripheralTracker::FetchRawAdvertisements(
|
||||
BleV2Peripheral peripheral,
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
BleV2Peripheral& peripheral, AdvertisementFetcher advertisement_fetcher) {
|
||||
AdvertisementFetcher advertisement_fetcher) {
|
||||
// Fetch the raw GATT advertisements and store the results.
|
||||
auto& result = advertisement_read_results_[advertisement_header];
|
||||
if (result == nullptr) {
|
||||
@@ -569,8 +577,8 @@ DiscoveredPeripheralTracker::FetchRawAdvertisements(
|
||||
std::back_inserter(service_ids),
|
||||
[](auto& kv) { return kv.first; });
|
||||
advertisement_fetcher.fetch_advertisements(
|
||||
advertisement_header.GetNumSlots(), advertisement_header.GetPsm(),
|
||||
service_ids, *result, /*mutated=*/peripheral);
|
||||
std::move(peripheral), advertisement_header.GetNumSlots(),
|
||||
advertisement_header.GetPsm(), service_ids, *result);
|
||||
|
||||
// Take those results and return all the advertisements we were able to
|
||||
// read.
|
||||
@@ -578,8 +586,7 @@ DiscoveredPeripheralTracker::FetchRawAdvertisements(
|
||||
}
|
||||
|
||||
void DiscoveredPeripheralTracker::UpdateCommonStateForFoundBleAdvertisement(
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
const std::string& mac_address) {
|
||||
const BleAdvertisementHeader& advertisement_header) {
|
||||
const auto ga_it = gatt_advertisements_.find(advertisement_header);
|
||||
if (ga_it == gatt_advertisements_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
@@ -594,7 +601,7 @@ void DiscoveredPeripheralTracker::UpdateCommonStateForFoundBleAdvertisement(
|
||||
if (gai_it == gatt_advertisement_infos_.end()) {
|
||||
continue;
|
||||
}
|
||||
GattAdvertisementInfo& gatt_advertisement_info = gai_it->second;
|
||||
const GattAdvertisementInfo& gatt_advertisement_info = gai_it->second;
|
||||
const auto sii_it =
|
||||
service_id_infos_.find(gatt_advertisement_info.service_id);
|
||||
if (sii_it != service_id_infos_.end()) {
|
||||
@@ -607,18 +614,10 @@ void DiscoveredPeripheralTracker::UpdateCommonStateForFoundBleAdvertisement(
|
||||
continue;
|
||||
}
|
||||
lost_entity_tracker->RecordFoundEntity(gatt_advertisement);
|
||||
|
||||
gatt_advertisement_info.mac_address = mac_address;
|
||||
gatt_advertisement_infos_[gatt_advertisement] = gatt_advertisement_info;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BlePeripheral DiscoveredPeripheralTracker::GenerateBlePeripheral(
|
||||
const BleAdvertisement& gatt_advertisement, int psm) {
|
||||
return BlePeripheral(ByteArray(gatt_advertisement), psm);
|
||||
}
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -50,14 +50,13 @@ class DiscoveredPeripheralTracker {
|
||||
// `advertisement_read_result` is in/out mutable reference that the caller
|
||||
// should take of its life cycle and pass a valid reference.
|
||||
std::function<void(
|
||||
int num_slots, int psm,
|
||||
BleV2Peripheral peripheral, int num_slots, int psm,
|
||||
const std::vector<std::string>& interesting_service_ids,
|
||||
mediums::AdvertisementReadResult& advertisement_read_result,
|
||||
BleV2Peripheral& peripheral)>
|
||||
mediums::AdvertisementReadResult& advertisement_read_result)>
|
||||
fetch_advertisements =
|
||||
DefaultCallback<int, int, const std::vector<std::string>&,
|
||||
mediums::AdvertisementReadResult&,
|
||||
BleV2Peripheral&>();
|
||||
DefaultCallback<BleV2Peripheral, int, int,
|
||||
const std::vector<std::string>&,
|
||||
mediums::AdvertisementReadResult&>();
|
||||
};
|
||||
|
||||
// Starts tracking discoveries for a particular service Id.
|
||||
@@ -130,6 +129,9 @@ class DiscoveredPeripheralTracker {
|
||||
// advertisement alone. Entries are modified every time a GATT
|
||||
// advertisement's advertisement header is seen.
|
||||
std::string mac_address;
|
||||
|
||||
// A proxy BlePeripheral for found/lost disovery callback.
|
||||
BleV2Peripheral peripheral;
|
||||
};
|
||||
|
||||
// Clears stale data from any previous sessions.
|
||||
@@ -151,7 +153,7 @@ class DiscoveredPeripheralTracker {
|
||||
// Handles the legacy fast advertisement or the extended fast/regular
|
||||
// advertisement.
|
||||
void HandleAdvertisement(
|
||||
const BleV2Peripheral& peripheral,
|
||||
BleV2Peripheral peripheral,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
@@ -168,6 +170,7 @@ class DiscoveredPeripheralTracker {
|
||||
// Returns BleAdvertisementHeader, it may be replaced if the header is mock
|
||||
// and there's a psm value in advertisement.
|
||||
BleAdvertisementHeader HandleRawGattAdvertisements(
|
||||
BleV2Peripheral peripheral,
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
const std::vector<const ByteArray*>& gatt_advertisement_bytes_list,
|
||||
const std::string& service_uuid) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
@@ -194,8 +197,9 @@ class DiscoveredPeripheralTracker {
|
||||
|
||||
// Handles the advertisement header for regular advertisement.
|
||||
void HandleAdvertisementHeader(
|
||||
BleV2Peripheral peripheral,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data,
|
||||
BleV2Peripheral& peripheral, AdvertisementFetcher advertisement_fetcher)
|
||||
AdvertisementFetcher advertisement_fetcher)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Extracts the advertisement header byte array from `AdvertisementData`.
|
||||
@@ -221,20 +225,16 @@ class DiscoveredPeripheralTracker {
|
||||
// advertisement_fetcher : a fetcher passed from BLE medium to read the
|
||||
// advertisemeent from BLE characteristics by GATT server.
|
||||
std::vector<const ByteArray*> FetchRawAdvertisements(
|
||||
BleV2Peripheral peripheral,
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
BleV2Peripheral& peripheral, AdvertisementFetcher advertisement_fetcher)
|
||||
AdvertisementFetcher advertisement_fetcher)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Updates `gatt_advertisement_infos_` map no matter whether we read a new
|
||||
// GATT advertisement by the input `advertisement_header` and 'mac_address`.
|
||||
void UpdateCommonStateForFoundBleAdvertisement(
|
||||
const BleAdvertisementHeader& advertisement_header,
|
||||
const std::string& mac_address) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Creates BlePeripheral based on the input of advertisement and psm value.
|
||||
BlePeripheral GenerateBlePeripheral(
|
||||
const BleAdvertisement& gatt_advertisement,
|
||||
int psm = BleAdvertisementHeader::kDefaultPsmValue);
|
||||
const BleAdvertisementHeader& advertisement_header)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
Mutex mutex_;
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "connections/implementation/mediums/ble_v2/ble_utils.h"
|
||||
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
|
||||
@@ -114,7 +112,7 @@ class BlePeripheralStub : public api::ble_v2::BlePeripheral {
|
||||
mac_address_ = mac_address;
|
||||
}
|
||||
|
||||
std::string GetId() const override { return mac_address_; }
|
||||
std::string GetAddress() const override { return mac_address_; }
|
||||
|
||||
private:
|
||||
std::string mac_address_;
|
||||
@@ -166,10 +164,9 @@ class DiscoveredPeripheralTrackerTest : public testing::Test {
|
||||
return {
|
||||
.fetch_advertisements =
|
||||
[this, &fetch_latch, &advertisement_bytes_list](
|
||||
int num_slots, int psm,
|
||||
BleV2Peripheral peripheral, int num_slots, int psm,
|
||||
const std::vector<std::string>& interesting_service_ids,
|
||||
mediums::AdvertisementReadResult& advertisement_read_result,
|
||||
BleV2Peripheral& peripheral) {
|
||||
mediums::AdvertisementReadResult& advertisement_read_result) {
|
||||
MutexLock lock(&mutex_);
|
||||
fetch_count_++;
|
||||
int slot = 0;
|
||||
@@ -201,7 +198,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -242,7 +239,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&callback_times, &found_latch](
|
||||
BlePeripheral& peripheral, const std::string& service_id,
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
callback_times++;
|
||||
@@ -288,7 +285,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -333,7 +330,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch_a](BlePeripheral& peripheral,
|
||||
[&found_latch_a](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -347,7 +344,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdB),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch_b](BlePeripheral& peripheral,
|
||||
[&found_latch_b](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -396,7 +393,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -439,7 +436,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](
|
||||
BlePeripheral& peripheral, const std::string& service_id,
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) { found_latch.CountDown(); },
|
||||
},
|
||||
@@ -482,7 +479,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&callback_times, &found_latch](
|
||||
BlePeripheral& peripheral, const std::string& service_id,
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
callback_times++;
|
||||
@@ -530,7 +527,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&callback_times, &found_latch](
|
||||
BlePeripheral& peripheral, const std::string& service_id,
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
callback_times++;
|
||||
@@ -580,7 +577,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](
|
||||
BlePeripheral& peripheral, const std::string& service_id,
|
||||
BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) { found_latch.CountDown(); },
|
||||
},
|
||||
@@ -618,7 +615,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -628,7 +625,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch, &lost_callback_times](
|
||||
BlePeripheral& peripheral, const std::string& service_id) {
|
||||
BleV2Peripheral peripheral, const std::string& service_id) {
|
||||
lost_callback_times++;
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
@@ -683,7 +680,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -692,7 +689,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](BlePeripheral& peripheral,
|
||||
[&lost_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
@@ -744,7 +741,7 @@ TEST_F(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -753,7 +750,7 @@ TEST_F(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](BlePeripheral& peripheral,
|
||||
[&lost_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
@@ -805,7 +802,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch_a](BlePeripheral& peripheral,
|
||||
[&found_latch_a](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -814,7 +811,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
found_latch_a.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch_a](BlePeripheral& peripheral,
|
||||
[&lost_latch_a](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch_a.CountDown();
|
||||
},
|
||||
@@ -824,7 +821,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdB),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch_b](BlePeripheral& peripheral,
|
||||
[&found_latch_b](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -833,7 +830,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
found_latch_b.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch_b](BlePeripheral& peripheral,
|
||||
[&lost_latch_b](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch_b.CountDown();
|
||||
},
|
||||
@@ -890,7 +887,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
std::string(kServiceIdA),
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -899,7 +896,7 @@ TEST_F(DiscoveredPeripheralTrackerTest,
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](BlePeripheral& peripheral,
|
||||
[&lost_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
|
||||
@@ -82,23 +82,21 @@ TEST_F(BleV2Test, CanStartScanning) {
|
||||
BleV2 ble{radio};
|
||||
radio.Enable();
|
||||
|
||||
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[](mediums::BlePeripheral& peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
// nothing to do for now
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[](mediums::BlePeripheral& peripheral,
|
||||
const std::string& service_id) {
|
||||
// nothing to do for now
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/""));
|
||||
EXPECT_TRUE(ble.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[](BleV2Peripheral peripheral, const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
// nothing to do for now
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[](BleV2Peripheral peripheral, const std::string& service_id) {
|
||||
// nothing to do for now
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/""));
|
||||
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA)));
|
||||
env_.Stop();
|
||||
}
|
||||
@@ -118,7 +116,7 @@ TEST_F(BleV2Test, CanStartFastAdvertising) {
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](mediums::BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -126,8 +124,7 @@ TEST_F(BleV2Test, CanStartFastAdvertising) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[](mediums::BlePeripheral& peripheral,
|
||||
const std::string& service_id) {
|
||||
[](BleV2Peripheral peripheral, const std::string& service_id) {
|
||||
// nothing to do for now
|
||||
},
|
||||
},
|
||||
@@ -161,7 +158,7 @@ TEST_F(BleV2Test, CanStartFastScanning) {
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](mediums::BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -169,8 +166,7 @@ TEST_F(BleV2Test, CanStartFastScanning) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[](mediums::BlePeripheral& peripheral,
|
||||
const std::string& service_id) {
|
||||
[](BleV2Peripheral peripheral, const std::string& service_id) {
|
||||
// nothing to do for now
|
||||
},
|
||||
},
|
||||
@@ -258,7 +254,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](mediums::BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -266,7 +262,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](mediums::BlePeripheral& peripheral,
|
||||
[&lost_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
@@ -307,7 +303,7 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) {
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](mediums::BlePeripheral& peripheral,
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
@@ -315,7 +311,7 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[&lost_latch](mediums::BlePeripheral& peripheral,
|
||||
[&lost_latch](BleV2Peripheral peripheral,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_PUBLIC_BLUETOOTH_ADAPTER_H_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
@@ -33,7 +34,6 @@ class BlePeripheral final {
|
||||
BlePeripheral(const BlePeripheral&) = default;
|
||||
BlePeripheral& operator=(const BlePeripheral&) = default;
|
||||
explicit BlePeripheral(api::BlePeripheral* peripheral) : impl_(peripheral) {}
|
||||
~BlePeripheral() = default;
|
||||
|
||||
std::string GetName() const { return impl_->GetName(); }
|
||||
|
||||
@@ -48,16 +48,42 @@ class BlePeripheral final {
|
||||
api::BlePeripheral* impl_;
|
||||
};
|
||||
|
||||
// Opaque wrapper over a BLE peripheral.
|
||||
// Opaque wrapper over a BLE peripheral. Must contain enough data about a
|
||||
// particular BLE peripheral to connect to its GATT server.
|
||||
class BleV2Peripheral final {
|
||||
public:
|
||||
BleV2Peripheral() = default;
|
||||
BleV2Peripheral(const BleV2Peripheral&) = default;
|
||||
BleV2Peripheral& operator=(const BleV2Peripheral&) = default;
|
||||
explicit BleV2Peripheral(api::ble_v2::BlePeripheral* peripheral)
|
||||
: impl_(peripheral) {}
|
||||
BleV2Peripheral(const BleV2Peripheral&) = default;
|
||||
BleV2Peripheral& operator=(const BleV2Peripheral&) = default;
|
||||
BleV2Peripheral(BleV2Peripheral&& other) {
|
||||
impl_ = other.impl_;
|
||||
id_ = std::move(other.id_);
|
||||
psm_ = other.psm_;
|
||||
|
||||
std::string GetId() const { return impl_->GetId(); }
|
||||
other.impl_ = nullptr;
|
||||
other.psm_ = 0;
|
||||
}
|
||||
|
||||
BleV2Peripheral& operator=(BleV2Peripheral&& other) {
|
||||
if (this != &other) {
|
||||
impl_ = other.impl_;
|
||||
id_ = std::move(other.id_);
|
||||
psm_ = other.psm_;
|
||||
|
||||
other.impl_ = nullptr;
|
||||
other.psm_ = 0;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string GetAddress() const { return impl_->GetAddress(); }
|
||||
|
||||
ByteArray GetId() const { return id_; }
|
||||
void SetId(const ByteArray& id) { id_ = id; }
|
||||
|
||||
int GetPsm() const { return psm_; }
|
||||
void SetPsm(int psm) { psm_ = psm; }
|
||||
|
||||
// Returns reference to platform implementation.
|
||||
// This is used to communicate with platform code, and for debugging purposes.
|
||||
@@ -65,7 +91,18 @@ class BleV2Peripheral final {
|
||||
bool IsValid() const { return impl_ != nullptr; }
|
||||
|
||||
private:
|
||||
api::ble_v2::BlePeripheral* impl_;
|
||||
// Does not take ownership. It refers to a valid `api::ble_v2::BlePeripheral`
|
||||
// that outlives this object.
|
||||
api::ble_v2::BlePeripheral* impl_ = nullptr;
|
||||
|
||||
// A unique identifier for this peripheral. It is the BLE advertisement bytes
|
||||
// it was found on.
|
||||
ByteArray id_ = {};
|
||||
|
||||
// The psm (protocol service multiplexer) value is used for create data
|
||||
// connection on L2CAP socket. It only exists when remote device supports
|
||||
// L2CAP socket feature.
|
||||
int psm_ = 0;
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html.
|
||||
@@ -96,7 +133,6 @@ class BluetoothAdapter final {
|
||||
|
||||
BluetoothAdapter()
|
||||
: impl_(api::ImplementationPlatform::CreateBluetoothAdapter()) {}
|
||||
~BluetoothAdapter() = default;
|
||||
BluetoothAdapter(BluetoothAdapter&&) = default;
|
||||
BluetoothAdapter& operator=(BluetoothAdapter&&) = default;
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -24,6 +27,102 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
constexpr absl::string_view kMacAddress = "4C:8B:1D:CE:BA:D1";
|
||||
constexpr absl::string_view kId = "AB12";
|
||||
|
||||
class BlePeripheralStub : public api::ble_v2::BlePeripheral {
|
||||
public:
|
||||
explicit BlePeripheralStub(absl::string_view mac_address) {
|
||||
mac_address_ = mac_address;
|
||||
}
|
||||
|
||||
std::string GetAddress() const override { return mac_address_; }
|
||||
|
||||
private:
|
||||
std::string mac_address_;
|
||||
};
|
||||
|
||||
TEST(BleV2PeripheralTest, ConstructionWorks) {
|
||||
auto api_peripheral = std::make_unique<BlePeripheralStub>(kMacAddress);
|
||||
|
||||
BleV2Peripheral peripheral(api_peripheral.get());
|
||||
|
||||
ASSERT_TRUE(peripheral.IsValid());
|
||||
EXPECT_EQ(peripheral.GetAddress(), kMacAddress);
|
||||
}
|
||||
|
||||
TEST(BleV2PeripheralTest, SetIdAndPsmWorks) {
|
||||
auto api_peripheral = std::make_unique<BlePeripheralStub>(kMacAddress);
|
||||
ByteArray id((std::string(kId)));
|
||||
int psm = 2;
|
||||
|
||||
BleV2Peripheral peripheral(api_peripheral.get());
|
||||
peripheral.SetId(id);
|
||||
peripheral.SetPsm(psm);
|
||||
|
||||
ASSERT_TRUE(peripheral.IsValid());
|
||||
EXPECT_EQ(peripheral.GetId(), id);
|
||||
EXPECT_EQ(peripheral.GetPsm(), 2);
|
||||
}
|
||||
|
||||
TEST(BleV2PeripheralTest, CopyConstructorAndAssignmentSuccess) {
|
||||
auto api_peripheral = std::make_unique<BlePeripheralStub>(kMacAddress);
|
||||
ByteArray id((std::string(kId)));
|
||||
int psm = 2;
|
||||
|
||||
BleV2Peripheral peripheral(api_peripheral.get());
|
||||
peripheral.SetId(id);
|
||||
peripheral.SetPsm(psm);
|
||||
|
||||
BleV2Peripheral copy_peripheral_1(peripheral);
|
||||
|
||||
ASSERT_TRUE(copy_peripheral_1.IsValid());
|
||||
EXPECT_EQ(copy_peripheral_1.GetAddress(), kMacAddress);
|
||||
EXPECT_EQ(copy_peripheral_1.GetId(), id);
|
||||
EXPECT_EQ(copy_peripheral_1.GetPsm(), 2);
|
||||
|
||||
BleV2Peripheral copy_periphera1_2 = peripheral;
|
||||
|
||||
ASSERT_TRUE(copy_periphera1_2.IsValid());
|
||||
EXPECT_EQ(copy_periphera1_2.GetAddress(), kMacAddress);
|
||||
EXPECT_EQ(copy_periphera1_2.GetId(), id);
|
||||
EXPECT_EQ(copy_periphera1_2.GetPsm(), 2);
|
||||
}
|
||||
|
||||
TEST(BleV2PeripheralTest, MoveConstructorSuccess) {
|
||||
auto api_peripheral = std::make_unique<BlePeripheralStub>(kMacAddress);
|
||||
ByteArray id((std::string(kId)));
|
||||
int psm = 2;
|
||||
|
||||
BleV2Peripheral peripheral(api_peripheral.get());
|
||||
peripheral.SetId(id);
|
||||
peripheral.SetPsm(psm);
|
||||
|
||||
BleV2Peripheral move_peripheral(std::move(peripheral));
|
||||
|
||||
ASSERT_TRUE(move_peripheral.IsValid());
|
||||
EXPECT_EQ(move_peripheral.GetAddress(), kMacAddress);
|
||||
EXPECT_EQ(move_peripheral.GetId(), id);
|
||||
EXPECT_EQ(move_peripheral.GetPsm(), 2);
|
||||
}
|
||||
|
||||
TEST(BleV2PeripheralTest, MoveAssignmentSuccess) {
|
||||
auto api_peripheral = std::make_unique<BlePeripheralStub>(kMacAddress);
|
||||
ByteArray id((std::string(kId)));
|
||||
int psm = 2;
|
||||
|
||||
BleV2Peripheral peripheral(api_peripheral.get());
|
||||
peripheral.SetId(id);
|
||||
peripheral.SetPsm(psm);
|
||||
|
||||
BleV2Peripheral move_peripheral = std::move(peripheral);
|
||||
|
||||
ASSERT_TRUE(move_peripheral.IsValid());
|
||||
EXPECT_EQ(move_peripheral.GetAddress(), kMacAddress);
|
||||
EXPECT_EQ(move_peripheral.GetId(), id);
|
||||
EXPECT_EQ(move_peripheral.GetPsm(), 2);
|
||||
}
|
||||
|
||||
TEST(BluetoothAdapterTest, ConstructorDestructorWorks) {
|
||||
BluetoothAdapter adapter;
|
||||
EXPECT_TRUE(adapter.IsValid());
|
||||
|
||||
@@ -78,10 +78,8 @@ struct BleAdvertisementData {
|
||||
absl::flat_hash_map<std::string, location::nearby::ByteArray> service_data;
|
||||
};
|
||||
|
||||
// TODO(b/213835576): Refactor BlePeripheral. The one in BluetoothAdapter
|
||||
// should be considered, too. Opaque wrapper over a BLE peripheral. Must be
|
||||
// able to uniquely identify a peripheral so that we can connect to its GATT
|
||||
// server.
|
||||
// Opaque wrapper over a BLE peripheral. Must be able to uniquely identify a
|
||||
// peripheral so that we can connect to its GATT server.
|
||||
class BlePeripheral {
|
||||
public:
|
||||
virtual ~BlePeripheral() = default;
|
||||
@@ -90,7 +88,7 @@ class BlePeripheral {
|
||||
//
|
||||
// This should be the MAC address when possible. If the implementation is
|
||||
// unable to retrieve that, any unique identifier should suffice.
|
||||
virtual std::string GetId() const = 0;
|
||||
virtual std::string GetAddress() const = 0;
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic
|
||||
|
||||
@@ -16,20 +16,13 @@
|
||||
#define PLATFORM_IMPL_G3_BLE_V2_H_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/g3/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/g3/multi_thread_executor.h"
|
||||
#include "internal/platform/implementation/g3/pipe.h"
|
||||
#include "internal/platform/input_stream.h"
|
||||
#include "internal/platform/output_stream.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -77,7 +70,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
private:
|
||||
class GattServer : public api::ble_v2::GattServer {
|
||||
public:
|
||||
absl::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
|
||||
std::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
|
||||
absl::string_view service_uuid, absl::string_view characteristic_uuid,
|
||||
const std::vector<api::ble_v2::GattCharacteristic::Permission>&
|
||||
permissions,
|
||||
|
||||
@@ -41,7 +41,9 @@ void BlePeripheral::SetAdvertisementBytes(
|
||||
BleV2Peripheral::BleV2Peripheral(BluetoothAdapter* adapter)
|
||||
: adapter_(*adapter) {}
|
||||
|
||||
std::string BleV2Peripheral::GetId() const { return adapter_.GetMacAddress(); }
|
||||
std::string BleV2Peripheral::GetAddress() const {
|
||||
return adapter_.GetMacAddress();
|
||||
}
|
||||
|
||||
BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter)
|
||||
: adapter_(*adapter) {}
|
||||
|
||||
@@ -58,7 +58,7 @@ class BlePeripheral : public api::BlePeripheral {
|
||||
// BlePeripheral implementation.
|
||||
class BleV2Peripheral : public api::ble_v2::BlePeripheral {
|
||||
public:
|
||||
std::string GetId() const override;
|
||||
std::string GetAddress() const override;
|
||||
BluetoothAdapter& GetAdapter() { return adapter_; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -83,7 +83,7 @@ std::string PowerModeToName(PowerMode power_mode) {
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string BleV2Peripheral::GetId() const { return ""; }
|
||||
std::string BleV2Peripheral::GetAddress() const { return ""; }
|
||||
|
||||
BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter)
|
||||
: adapter_(dynamic_cast<BluetoothAdapter*>(&adapter)) {}
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace windows {
|
||||
|
||||
class BleV2Peripheral : public api::ble_v2::BlePeripheral {
|
||||
public:
|
||||
std::string GetId() const override;
|
||||
std::string GetAddress() const override;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the BLE medium.
|
||||
|
||||
Reference in New Issue
Block a user