mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
internal change
PiperOrigin-RevId: 474390888
This commit is contained in:
committed by
Copybara-Service
parent
0b57cd5c5f
commit
bbe77d8397
@@ -219,9 +219,11 @@ cc_library(
|
||||
deps = [
|
||||
":base",
|
||||
":logging",
|
||||
":uuid",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform/implementation:comm",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/container:flat_hash_set",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/types:optional",
|
||||
],
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "internal/platform/implementation/g3/ble_v2.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@@ -271,42 +272,58 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
auto internal_session_id = prng_.NextUint32();
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuid, std::move(callback), *this);
|
||||
/*enabled=*/true, service_uuid, internal_session_id,
|
||||
{.advertisement_found_cb = callback.advertisement_found_cb}, *this);
|
||||
scanning_internal_session_ids_.insert({service_uuid, internal_session_id});
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StopScanning() {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StopScanning";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/false,
|
||||
/*service_uuid=*/{}, /*callback=*/{}, *this);
|
||||
for (auto element : scanning_internal_session_ids_) {
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/false,
|
||||
/*service_uuid=*/element.first, /*internal_session_id*/ element.second,
|
||||
/*callback=*/{}, *this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
|
||||
const Uuid& service_uuid, TxPowerLevel tx_power_level,
|
||||
BleV2Medium::ScanningCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
auto internal_session_id = prng_.NextUint32();
|
||||
|
||||
{
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuid,
|
||||
{.advertisement_found_cb = callback.advertisement_found_cb}, *this);
|
||||
/*enabled=*/true, service_uuid, internal_session_id, callback, *this);
|
||||
scanning_internal_session_ids_.insert({service_uuid, internal_session_id});
|
||||
}
|
||||
callback.start_scanning_result(api::ble_v2::BleOperationStatus::kSucceeded);
|
||||
return std::make_unique<ScanningSession>(ScanningSession{
|
||||
.stop_scanning =
|
||||
[this]() {
|
||||
if (StopScanning())
|
||||
return BleOperationStatus::kSucceeded;
|
||||
else
|
||||
[this, service_uuid = service_uuid,
|
||||
internal_session_id = internal_session_id]() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (scanning_internal_session_ids_.find(
|
||||
{service_uuid, internal_session_id}) ==
|
||||
scanning_internal_session_ids_.end()) {
|
||||
// can't find the provided internal session.
|
||||
return BleOperationStatus::kFailed;
|
||||
}
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/false, service_uuid, internal_session_id,
|
||||
/*callback=*/{}, *this);
|
||||
scanning_internal_session_ids_.erase(
|
||||
{service_uuid, internal_session_id});
|
||||
return BleOperationStatus::kSucceeded;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,18 +15,21 @@
|
||||
#ifndef PLATFORM_IMPL_G3_BLE_V2_H_
|
||||
#define PLATFORM_IMPL_G3_BLE_V2_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/container/flat_hash_set.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/pipe.h"
|
||||
#include "internal/platform/prng.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
@@ -250,9 +253,12 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
Prng prng_;
|
||||
BluetoothAdapter* adapter_; // Our device adapter; read-only.
|
||||
absl::flat_hash_map<std::string, BleV2ServerSocket*> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_set<std::pair<Uuid, std::uint32_t>>
|
||||
scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_);
|
||||
// TODO(edwinwu): Adds extended advertisement for testing.
|
||||
bool is_support_extended_advertisement_ = false;
|
||||
};
|
||||
|
||||
@@ -17,12 +17,14 @@
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cinttypes>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
#include "internal/platform/feature_flags.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
@@ -236,7 +238,7 @@ void MediumEnvironment::OnBlePeripheralStateChanged(
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnBleV2PeripheralStateChanged(
|
||||
bool enabled, BleV2MediumContext& context,
|
||||
bool enabled, BleV2MediumContext& context, const Uuid& service_id,
|
||||
const api::ble_v2::BleAdvertisementData& ble_advertisement_data,
|
||||
api::ble_v2::BlePeripheral& peripheral) {
|
||||
if (!enabled_) return;
|
||||
@@ -248,8 +250,11 @@ void MediumEnvironment::OnBleV2PeripheralStateChanged(
|
||||
<< &peripheral << "]; context=" << &context
|
||||
<< "; notify=" << enabled;
|
||||
if (enabled) {
|
||||
context.scan_callback.advertisement_found_cb(peripheral,
|
||||
ble_advertisement_data);
|
||||
for (auto& element : context.scan_callback_map) {
|
||||
if (element.first.first == service_id)
|
||||
element.second.advertisement_found_cb(peripheral,
|
||||
ble_advertisement_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -529,81 +534,105 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising(
|
||||
auto& context = it->second;
|
||||
context.ble_peripheral = &peripheral;
|
||||
context.advertising = enabled;
|
||||
context.advertisement_data = advertisement_data;
|
||||
NEARBY_LOGS(INFO) << "G3 UpdateBleV2MediumForAdvertising: this=" << this
|
||||
<< ", medium=" << &medium
|
||||
<< ", medium_context=" << &context
|
||||
<< ", peripheral=" << &peripheral
|
||||
<< ", enabled=" << enabled;
|
||||
for (auto& medium_info : ble_v2_mediums_) {
|
||||
const api::ble_v2::BleMedium* remote_medium = medium_info.first;
|
||||
BleV2MediumContext& remote_context = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (remote_medium == &medium) continue;
|
||||
// Do not send notification to the medium that is not scannig.
|
||||
if (!remote_context.scanning) continue;
|
||||
auto const it = context.advertisement_data.service_data.find(
|
||||
remote_context.scanning_service_uuid);
|
||||
if (it == context.advertisement_data.service_data.end()) continue;
|
||||
if (enabled) {
|
||||
context.advertisement_data = advertisement_data;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 UpdateBleV2MediumForAdvertising, found other medium="
|
||||
<< remote_medium << ", remote_medium_context=" << &remote_context
|
||||
<< ", remote_context.peripheral=" << remote_context.ble_peripheral
|
||||
<< ". Ready to call OnBleV2PeripheralStateChanged.";
|
||||
OnBleV2PeripheralStateChanged(enabled, remote_context,
|
||||
context.advertisement_data,
|
||||
*context.ble_peripheral);
|
||||
<< "G3 UpdateBleV2MediumForAdvertising: this=" << this
|
||||
<< ", medium=" << &medium << ", medium_context=" << &context
|
||||
<< ", peripheral=" << &peripheral << ", enabled=" << enabled;
|
||||
for (auto& medium_info : ble_v2_mediums_) {
|
||||
const api::ble_v2::BleMedium* remote_medium = medium_info.first;
|
||||
BleV2MediumContext& remote_context = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (remote_medium == &medium) continue;
|
||||
// Do not send notification to the medium that is not scannig.
|
||||
if (!remote_context.scanning) continue;
|
||||
absl::flat_hash_set<Uuid> remote_scanning_service_uuids;
|
||||
for (auto& element : remote_context.scan_callback_map) {
|
||||
remote_scanning_service_uuids.insert(element.first.first);
|
||||
}
|
||||
for (auto& remote_scanning_service_uuid :
|
||||
remote_scanning_service_uuids) {
|
||||
auto const it = context.advertisement_data.service_data.find(
|
||||
remote_scanning_service_uuid);
|
||||
if (it == context.advertisement_data.service_data.end()) continue;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 UpdateBleV2MediumForAdvertising, found other medium="
|
||||
<< remote_medium
|
||||
<< ", remote_medium_context=" << &remote_context
|
||||
<< ", remote_context.peripheral="
|
||||
<< remote_context.ble_peripheral
|
||||
<< ". Ready to call OnBleV2PeripheralStateChanged.";
|
||||
OnBleV2PeripheralStateChanged(
|
||||
enabled, remote_context, remote_scanning_service_uuid,
|
||||
context.advertisement_data, *context.ble_peripheral);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateBleV2MediumForScanning(
|
||||
bool enabled, const Uuid& scanning_service_uuid, BleScanCallback callback,
|
||||
bool enabled, const Uuid& scanning_service_uuid,
|
||||
std::uint32_t internal_session_id, BleScanCallback callback,
|
||||
api::ble_v2::BleMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, scanning_service_uuid = scanning_service_uuid,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
RunOnMediumEnvironmentThread([this, &medium,
|
||||
scanning_service_uuid = scanning_service_uuid,
|
||||
internal_session_id = internal_session_id,
|
||||
callback = std::move(callback), enabled]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 UpdateBleV2MediumForScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
BleV2MediumContext& context = it->second;
|
||||
NEARBY_LOGS(INFO) << "G3 UpdateBleV2MediumForScanning: this=" << this
|
||||
<< ", medium=" << &medium
|
||||
<< ", medium_context=" << &context
|
||||
<< ", enabled=" << enabled;
|
||||
if (enabled) {
|
||||
context.scanning = true;
|
||||
callback.start_scanning_result(
|
||||
api::ble_v2::BleOperationStatus::kSucceeded);
|
||||
context.scan_callback_map.insert(
|
||||
{{scanning_service_uuid, internal_session_id}, std::move(callback)});
|
||||
absl::flat_hash_set<Uuid> scanning_service_uuids;
|
||||
for (auto& element : context.scan_callback_map) {
|
||||
scanning_service_uuids.insert(element.first.first);
|
||||
}
|
||||
for (const auto& medium_info : ble_v2_mediums_) {
|
||||
const api::ble_v2::BleMedium* remote_medium = medium_info.first;
|
||||
const BleV2MediumContext& remote_context = medium_info.second;
|
||||
// Do not send notification to the same or the non-advertising
|
||||
// medium.
|
||||
if (remote_medium == &medium || !remote_context.advertising) continue;
|
||||
for (auto& scanning_service_uuid : scanning_service_uuids) {
|
||||
auto const it = remote_context.advertisement_data.service_data.find(
|
||||
scanning_service_uuid);
|
||||
if (it == remote_context.advertisement_data.service_data.end())
|
||||
continue;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 UpdateBleV2MediumForScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
<< "G3 UpdateBleV2MediumForScanning, found other medium="
|
||||
<< remote_medium << ", remote_medium_context=" << &remote_context
|
||||
<< ", scanning_service_uuid="
|
||||
<< scanning_service_uuid.Get16BitAsString()
|
||||
<< ". Ready to call OnBleV2PeripheralStateChanged.";
|
||||
OnBleV2PeripheralStateChanged(enabled, context, scanning_service_uuid,
|
||||
remote_context.advertisement_data,
|
||||
*remote_context.ble_peripheral);
|
||||
}
|
||||
BleV2MediumContext& context = it->second;
|
||||
context.scan_callback = std::move(callback);
|
||||
context.scanning_service_uuid = scanning_service_uuid;
|
||||
NEARBY_LOGS(INFO) << "G3 UpdateBleV2MediumForScanning: this=" << this
|
||||
<< ", medium=" << &medium
|
||||
<< ", medium_context=" << &context
|
||||
<< ", enabled=" << enabled;
|
||||
context.scanning = enabled;
|
||||
if (enabled) {
|
||||
for (const auto& medium_info : ble_v2_mediums_) {
|
||||
const api::ble_v2::BleMedium* remote_medium = medium_info.first;
|
||||
const BleV2MediumContext& remote_context = medium_info.second;
|
||||
// Do not send notification to the same or the non-advertising
|
||||
// medium.
|
||||
if (remote_medium == &medium || !remote_context.advertising)
|
||||
continue;
|
||||
auto const it = remote_context.advertisement_data.service_data.find(
|
||||
context.scanning_service_uuid);
|
||||
if (it == remote_context.advertisement_data.service_data.end())
|
||||
continue;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 UpdateBleV2MediumForScanning, found other medium="
|
||||
<< remote_medium
|
||||
<< ", remote_medium_context=" << &remote_context
|
||||
<< ", scanning_service_uuid="
|
||||
<< scanning_service_uuid.Get16BitAsString()
|
||||
<< ". Ready to call OnBleV2PeripheralStateChanged.";
|
||||
OnBleV2PeripheralStateChanged(enabled, context,
|
||||
remote_context.advertisement_data,
|
||||
*remote_context.ble_peripheral);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
context.scan_callback_map.erase(
|
||||
{scanning_service_uuid, internal_session_id});
|
||||
if (context.scan_callback_map.empty()) {
|
||||
context.scanning = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::InsertBleV2MediumGattCharacteristics(
|
||||
|
||||
@@ -19,14 +19,17 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "internal/platform/implementation/ble.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
#ifndef NO_WEBRTC
|
||||
#include "internal/platform/implementation/webrtc.h"
|
||||
#endif
|
||||
@@ -64,7 +67,7 @@ class MediumEnvironment {
|
||||
api::BleMedium::DiscoveredPeripheralCallback;
|
||||
using BleAcceptedConnectionCallback =
|
||||
api::BleMedium::AcceptedConnectionCallback;
|
||||
using BleScanCallback = api::ble_v2::BleMedium::ScanCallback;
|
||||
using BleScanCallback = api::ble_v2::BleMedium::ScanningCallback;
|
||||
#ifndef NO_WEBRTC
|
||||
using OnSignalingMessageCallback =
|
||||
api::WebRtcSignalingMessenger::OnSignalingMessageCallback;
|
||||
@@ -238,6 +241,7 @@ class MediumEnvironment {
|
||||
// if `enabled` is false.
|
||||
void UpdateBleV2MediumForScanning(bool enabled,
|
||||
const Uuid& scanning_service_uuid,
|
||||
std::uint32_t internal_session_id,
|
||||
BleScanCallback callback,
|
||||
api::ble_v2::BleMedium& medium);
|
||||
|
||||
@@ -344,10 +348,11 @@ class MediumEnvironment {
|
||||
};
|
||||
|
||||
struct BleV2MediumContext {
|
||||
BleScanCallback scan_callback = {};
|
||||
api::ble_v2::BlePeripheral* ble_peripheral = nullptr;
|
||||
absl::flat_hash_map<std::pair<Uuid, std::uint32_t>, BleScanCallback>
|
||||
scan_callback_map;
|
||||
// using the same ble peripheral for different advertisement.
|
||||
api::ble_v2::BlePeripheral* ble_peripheral;
|
||||
api::ble_v2::BleAdvertisementData advertisement_data;
|
||||
Uuid scanning_service_uuid;
|
||||
bool advertising = false;
|
||||
bool scanning = false;
|
||||
};
|
||||
@@ -389,7 +394,7 @@ class MediumEnvironment {
|
||||
bool fast_advertisement, bool enabled);
|
||||
|
||||
void OnBleV2PeripheralStateChanged(
|
||||
bool enabled, BleV2MediumContext& context,
|
||||
bool enabled, BleV2MediumContext& context, const Uuid& service_id,
|
||||
const api::ble_v2::BleAdvertisementData& ble_advertisement_data,
|
||||
api::ble_v2::BlePeripheral& peripheral);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user