mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Change ConnectToGattServer to take BlePeripheral::UniqueId.
PiperOrigin-RevId: 756498100
This commit is contained in:
committed by
Copybara-Service
parent
f7e96e518c
commit
99458be820
+39
-40
@@ -15,6 +15,7 @@
|
||||
#include "internal/platform/ble_v2.h"
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -62,7 +63,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
ScanCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (scanning_enabled_) {
|
||||
NEARBY_LOGS(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
return false;
|
||||
}
|
||||
bool success = impl_->StartScanning(
|
||||
@@ -73,8 +74,8 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
BleAdvertisementData advertisement_data) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!peripherals_.contains(&peripheral)) {
|
||||
NEARBY_LOGS(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
LOG(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
peripherals_.insert(&peripheral);
|
||||
}
|
||||
|
||||
@@ -93,7 +94,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
// prevent the stale data in cache.
|
||||
peripherals_.clear();
|
||||
scanning_enabled_ = true;
|
||||
NEARBY_LOGS(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
@@ -103,7 +104,7 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (scanning_enabled_) {
|
||||
NEARBY_LOGS(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning already enabled; impl=" << GetImpl();
|
||||
return false;
|
||||
}
|
||||
bool success = impl_->StartMultipleServicesScanning(
|
||||
@@ -114,8 +115,8 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
BleAdvertisementData advertisement_data) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (!peripherals_.contains(&peripheral)) {
|
||||
NEARBY_LOGS(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
LOG(INFO) << "Peripheral impl=" << &peripheral
|
||||
<< " does not exist; add it to the map.";
|
||||
peripherals_.insert(&peripheral);
|
||||
}
|
||||
|
||||
@@ -129,7 +130,7 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
scan_callback_ = std::move(callback);
|
||||
peripherals_.clear();
|
||||
scanning_enabled_ = true;
|
||||
NEARBY_LOGS(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning enabled; impl=" << GetImpl();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
@@ -144,7 +145,7 @@ bool BleV2Medium::StopScanning() {
|
||||
scanning_enabled_ = false;
|
||||
peripherals_.clear();
|
||||
scan_callback_ = {};
|
||||
NEARBY_LOGS(INFO) << "Ble Scanning disabled: impl=" << GetImpl();
|
||||
LOG(INFO) << "Ble Scanning disabled: impl=" << GetImpl();
|
||||
return impl_->StopScanning();
|
||||
}
|
||||
bool BleV2Medium::PauseMediumScanning() {
|
||||
@@ -152,8 +153,7 @@ bool BleV2Medium::PauseMediumScanning() {
|
||||
if (!scanning_enabled_) {
|
||||
return true;
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "Pause Medium level BLE_V2 Scanning: impl="
|
||||
<< GetImpl();
|
||||
LOG(INFO) << "Pause Medium level BLE_V2 Scanning: impl=" << GetImpl();
|
||||
return impl_->PauseMediumScanning();
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ std::unique_ptr<api::ble_v2::BleMedium::ScanningSession>
|
||||
BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BleMedium::ScanningCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "platform mutex: " << &mutex_;
|
||||
LOG(INFO) << "platform mutex: " << &mutex_;
|
||||
return impl_->StartScanning(
|
||||
service_uuid, tx_power_level,
|
||||
api::ble_v2::BleMedium::ScanningCallback{
|
||||
@@ -209,14 +209,15 @@ std::unique_ptr<GattServer> BleV2Medium::StartGattServer(
|
||||
.characteristic_unsubscription_cb(characteristic);
|
||||
},
|
||||
.on_characteristic_read_cb =
|
||||
[this](const api::ble_v2::BlePeripheral& remote_device,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
ReadValueCallback callback) {
|
||||
[this](
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
ReadValueCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (server_gatt_connection_callback_
|
||||
.on_characteristic_read_cb) {
|
||||
server_gatt_connection_callback_.on_characteristic_read_cb(
|
||||
remote_device, characteristic, offset,
|
||||
remote_device_id, characteristic, offset,
|
||||
std::move(callback));
|
||||
} else {
|
||||
callback(absl::FailedPreconditionError(
|
||||
@@ -224,14 +225,15 @@ std::unique_ptr<GattServer> BleV2Medium::StartGattServer(
|
||||
}
|
||||
},
|
||||
.on_characteristic_write_cb =
|
||||
[this](const api::ble_v2::BlePeripheral& remote_device,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data, WriteValueCallback callback) {
|
||||
[this](
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data, WriteValueCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (server_gatt_connection_callback_
|
||||
.on_characteristic_write_cb) {
|
||||
server_gatt_connection_callback_.on_characteristic_write_cb(
|
||||
remote_device, characteristic, offset, data,
|
||||
remote_device_id, characteristic, offset, data,
|
||||
std::move(callback));
|
||||
} else {
|
||||
callback(absl::FailedPreconditionError(
|
||||
@@ -239,26 +241,26 @@ std::unique_ptr<GattServer> BleV2Medium::StartGattServer(
|
||||
}
|
||||
},
|
||||
});
|
||||
return std::make_unique<GattServer>(*this, std::move(api_gatt_server));
|
||||
return std::make_unique<GattServer>(std::move(api_gatt_server));
|
||||
}
|
||||
|
||||
std::unique_ptr<GattClient> BleV2Medium::ConnectToGattServer(
|
||||
BleV2Peripheral peripheral, TxPowerLevel tx_power_level,
|
||||
ClientGattConnectionCallback callback) {
|
||||
std::unique_ptr<api::ble_v2::GattClient> api_gatt_client;
|
||||
api::ble_v2::BlePeripheral* device = peripheral.GetImpl();
|
||||
if (device != nullptr) {
|
||||
api_gatt_client = impl_->ConnectToGattServer(
|
||||
*device, tx_power_level,
|
||||
{
|
||||
.disconnected_cb =
|
||||
[callback = std::move(callback)]() mutable {
|
||||
callback.disconnected_cb();
|
||||
},
|
||||
});
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId> id =
|
||||
peripheral.GetUniqueId();
|
||||
if (!id.has_value()) {
|
||||
LOG(ERROR) << "Failed to connect to GattServer, invalid peripheral";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<GattClient>(std::move(api_gatt_client));
|
||||
return std::make_unique<GattClient>(impl_->ConnectToGattServer(
|
||||
*id, tx_power_level,
|
||||
{
|
||||
.disconnected_cb =
|
||||
[callback = std::move(callback)]() mutable {
|
||||
callback.disconnected_cb();
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
BleV2ServerSocket BleV2Medium::OpenServerSocket(const std::string& service_id) {
|
||||
@@ -302,17 +304,14 @@ bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
return IsValid() && impl_->IsExtendedAdvertisementsAvailable();
|
||||
}
|
||||
|
||||
bool BleV2Peripheral::IsValid() const {
|
||||
return GetImpl() != nullptr;
|
||||
}
|
||||
bool BleV2Peripheral::IsValid() const { return GetImpl() != nullptr; }
|
||||
|
||||
api::ble_v2::BlePeripheral* BleV2Peripheral::GetImpl() const {
|
||||
if (!unique_id_.has_value()) return nullptr;
|
||||
api::ble_v2::BlePeripheral* result = nullptr;
|
||||
if (!medium_->GetImpl()->GetRemotePeripheral(
|
||||
unique_id_.value(), [&](api::ble_v2::BlePeripheral& device) {
|
||||
result = &device;
|
||||
})) {
|
||||
unique_id_.value(),
|
||||
[&](api::ble_v2::BlePeripheral& device) { result = &device; })) {
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -70,6 +70,10 @@ class BleV2Peripheral final {
|
||||
bool IsValid() const;
|
||||
explicit operator bool() const { return IsValid(); }
|
||||
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId> GetUniqueId() const {
|
||||
return unique_id_;
|
||||
}
|
||||
|
||||
api::ble_v2::BlePeripheral* GetImpl() const;
|
||||
std::string ToReadableString() const {
|
||||
if (!IsValid()) {
|
||||
@@ -222,9 +226,8 @@ class BleV2ServerSocket final {
|
||||
// of std::optional, because iOS platform is still in C++14.
|
||||
class GattServer final {
|
||||
public:
|
||||
GattServer(BleV2Medium& medium,
|
||||
std::unique_ptr<api::ble_v2::GattServer> gatt_server)
|
||||
: medium_(medium), impl_(std::move(gatt_server)) {}
|
||||
explicit GattServer(std::unique_ptr<api::ble_v2::GattServer> gatt_server)
|
||||
: impl_(std::move(gatt_server)) {}
|
||||
~GattServer() { Stop(); }
|
||||
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
@@ -263,7 +266,6 @@ class GattServer final {
|
||||
api::ble_v2::GattServer* GetImpl() { return impl_.get(); }
|
||||
|
||||
private:
|
||||
BleV2Medium& medium_;
|
||||
std::unique_ptr<api::ble_v2::GattServer> impl_;
|
||||
};
|
||||
|
||||
@@ -479,11 +481,11 @@ class BleV2Medium final {
|
||||
absl::AnyInvocable<void(const GattCharacteristic& characteristic)>
|
||||
characteristic_unsubscription_cb =
|
||||
nearby::DefaultCallback<const GattCharacteristic&>();
|
||||
absl::AnyInvocable<void(const BlePeripheral& remote_device,
|
||||
absl::AnyInvocable<void(const BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic,
|
||||
int offset, ReadValueCallback callback)>
|
||||
on_characteristic_read_cb;
|
||||
absl::AnyInvocable<void(const BlePeripheral& remote_device,
|
||||
absl::AnyInvocable<void(const BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic,
|
||||
int offset, absl::string_view data,
|
||||
WriteValueCallback callback)>
|
||||
|
||||
@@ -63,7 +63,6 @@ constexpr absl::string_view kAdvertisementHeaderString = "\x0x\x0y\x0z";
|
||||
constexpr TxPowerLevel kTxPowerLevel(TxPowerLevel::kHigh);
|
||||
constexpr absl::string_view kServiceIDA{
|
||||
"com.google.location.nearby.apps.test.a"};
|
||||
constexpr absl::string_view kId = "AB12";
|
||||
|
||||
class BleV2MediumTest : public ::testing::TestWithParam<FeatureFlags> {
|
||||
protected:
|
||||
@@ -713,7 +712,7 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) {
|
||||
std::unique_ptr<GattServer> gatt_server =
|
||||
ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{
|
||||
.on_characteristic_write_cb =
|
||||
[&](const api::ble_v2::BlePeripheral& remote_device,
|
||||
[&](const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
int offset, absl::string_view data,
|
||||
BleV2Medium::ServerGattConnectionCallback::WriteValueCallback
|
||||
|
||||
@@ -119,7 +119,7 @@ class BleMedium : public api::ble_v2::BleMedium {
|
||||
// The peripheral must outlive the GATT client or undefined behavior will occur. The peripheral
|
||||
// should not be modified by this method.
|
||||
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) override;
|
||||
|
||||
// Opens a BLE server socket based on service ID.
|
||||
|
||||
@@ -306,20 +306,24 @@ std::unique_ptr<api::ble_v2::GattServer> BleMedium::StartGattServer(
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> BleMedium::ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) {
|
||||
// Check that the @c api::ble_v2::BlePeripheral is a @c nearby::apple::BlePeripheral and not a
|
||||
// @c nearby::apple::EmptyBlePeripheral instance, so we can retreive the CBPeripheral object.
|
||||
BlePeripheral *non_empty_peripheral = dynamic_cast<BlePeripheral *>(&peripheral);
|
||||
if (non_empty_peripheral == nullptr) {
|
||||
return nullptr;
|
||||
BlePeripheral *peripheral = nullptr;
|
||||
{
|
||||
absl::MutexLock lock(&peripherals_mutex_);
|
||||
const auto& it = peripherals_.find(peripheral_id);
|
||||
if (it == peripherals_.end()) {
|
||||
GTMLoggerError(@"[NEARBY] Failed to connect to Gatt server: peripheral is not found.");
|
||||
return nullptr;
|
||||
}
|
||||
peripheral = it->second.get();
|
||||
}
|
||||
|
||||
__block api::ble_v2::ClientGattConnectionCallback blockCallback = std::move(callback);
|
||||
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
__block GNCBLEGATTClient *blockClient = nil;
|
||||
[medium_ connectToGATTServerForPeripheral:non_empty_peripheral->GetPeripheral()
|
||||
[medium_ connectToGATTServerForPeripheral:peripheral->GetPeripheral()
|
||||
disconnectionHandler:^(void) {
|
||||
blockCallback.disconnected_cb();
|
||||
}
|
||||
|
||||
@@ -340,14 +340,14 @@ struct ServerGattConnectionCallback {
|
||||
// `GattServer::UpdateCharacteristic()`, then reading from the characteristic
|
||||
// yields that static value. The read callback is not called.
|
||||
// Otherwise, the gatt server calls the read callback to get the value.
|
||||
absl::AnyInvocable<void(const BlePeripheral& remote_device,
|
||||
absl::AnyInvocable<void(const BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
ReadValueCallback callback)>
|
||||
on_characteristic_read_cb;
|
||||
|
||||
// Called when a gatt client is writing to the characteristic.
|
||||
// Must call `callback` with the write result.
|
||||
absl::AnyInvocable<void(const BlePeripheral& remote_device,
|
||||
absl::AnyInvocable<void(const BlePeripheral::UniqueId remote_device_id,
|
||||
const GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data, WriteValueCallback callback)>
|
||||
on_characteristic_write_cb;
|
||||
@@ -581,7 +581,7 @@ class BleMedium {
|
||||
// LOW:
|
||||
// - Connection interval = ~100ms - 125ms
|
||||
virtual std::unique_ptr<GattClient> ConnectToGattServer(
|
||||
BlePeripheral& peripheral, TxPowerLevel tx_power_level,
|
||||
BlePeripheral::UniqueId peripheral_id, TxPowerLevel tx_power_level,
|
||||
ClientGattConnectionCallback callback) = 0;
|
||||
|
||||
// Opens a BLE server socket based on service ID.
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "internal/platform/implementation/g3/ble_v2.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -101,8 +100,7 @@ bool BleV2ServerSocket::Connect(BleV2Socket& socket) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (closed_) return false;
|
||||
if (socket.IsConnected()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "Failed to connect to Ble server socket: already connected";
|
||||
LOG(WARNING) << "Failed to connect to Ble server socket: already connected";
|
||||
return true; // already connected.
|
||||
}
|
||||
// add client socket to the pending list
|
||||
@@ -165,7 +163,7 @@ BleV2Medium::~BleV2Medium() {
|
||||
bool BleV2Medium::StartAdvertising(
|
||||
const BleAdvertisementData& advertising_data,
|
||||
api::ble_v2::AdvertiseParameters advertise_parameters) {
|
||||
NEARBY_LOGS(INFO)
|
||||
LOG(INFO)
|
||||
<< "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement="
|
||||
<< advertising_data.is_extended_advertisement
|
||||
<< ", advertising_data.service_data size="
|
||||
@@ -174,7 +172,7 @@ bool BleV2Medium::StartAdvertising(
|
||||
<< ", is_connectable=" << advertise_parameters.is_connectable;
|
||||
if (advertising_data.is_extended_advertisement &&
|
||||
!IsExtendedAdvertisementsAvailable()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
LOG(INFO)
|
||||
<< "G3 Ble StartAdvertising does not support extended advertisement";
|
||||
return false;
|
||||
}
|
||||
@@ -186,7 +184,7 @@ bool BleV2Medium::StartAdvertising(
|
||||
}
|
||||
|
||||
bool BleV2Medium::StopAdvertising() {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StopAdvertising";
|
||||
LOG(INFO) << "G3 Ble StopAdvertising";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
BleAdvertisementData empty_advertisement_data = {};
|
||||
@@ -200,7 +198,7 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
|
||||
const api::ble_v2::BleAdvertisementData& advertising_data,
|
||||
api::ble_v2::AdvertiseParameters advertise_parameters,
|
||||
BleV2Medium::AdvertisingCallback callback) {
|
||||
NEARBY_LOGS(INFO)
|
||||
LOG(INFO)
|
||||
<< "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement="
|
||||
<< advertising_data.is_extended_advertisement
|
||||
<< ", advertising_data.service_data size="
|
||||
@@ -209,7 +207,7 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
|
||||
<< ", is_connectable=" << advertise_parameters.is_connectable;
|
||||
if (advertising_data.is_extended_advertisement &&
|
||||
!IsExtendedAdvertisementsAvailable()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
LOG(INFO)
|
||||
<< "G3 Ble StartAdvertising does not support extended advertisement";
|
||||
return nullptr;
|
||||
}
|
||||
@@ -231,7 +229,7 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
|
||||
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
LOG(INFO) << "G3 Ble StartScanning";
|
||||
auto internal_session_id = Prng().NextUint32();
|
||||
absl::MutexLock lock(&mutex_);
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
@@ -245,7 +243,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
bool BleV2Medium::StartMultipleServicesScanning(
|
||||
const std::vector<Uuid>& service_uuids,
|
||||
api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartMultipleServicesScanning";
|
||||
LOG(INFO) << "G3 Ble StartMultipleServicesScanning";
|
||||
|
||||
absl::MutexLock lock(&mutex_);
|
||||
scan_callback_ = std::move(callback);
|
||||
@@ -270,7 +268,7 @@ bool BleV2Medium::StartMultipleServicesScanning(
|
||||
}
|
||||
|
||||
bool BleV2Medium::StopScanning() {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StopScanning";
|
||||
LOG(INFO) << "G3 Ble StopScanning";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
for (auto element : scanning_internal_session_ids_) {
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
@@ -285,7 +283,7 @@ bool BleV2Medium::StopScanning() {
|
||||
std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
|
||||
const Uuid& service_uuid, TxPowerLevel tx_power_level,
|
||||
BleV2Medium::ScanningCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
LOG(INFO) << "G3 Ble StartScanning";
|
||||
auto internal_session_id = Prng().NextUint32();
|
||||
|
||||
{
|
||||
@@ -333,16 +331,17 @@ bool BleV2Medium::IsStopped(Borrowable<api::ble_v2::GattServer*> server) {
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral, TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) {
|
||||
Borrowable<api::ble_v2::GattServer*> server =
|
||||
MediumEnvironment::Instance().GetGattServer(peripheral);
|
||||
MediumEnvironment::Instance().GetGattServer(peripheral_id);
|
||||
if (IsStopped(server)) {
|
||||
NEARBY_LOGS(WARNING) << "No GATT server found for "
|
||||
<< peripheral.GetAddress();
|
||||
LOG(WARNING) << "No GATT server found for " << peripheral_id;
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_unique<GattClient>(peripheral, server, std::move(callback));
|
||||
return std::make_unique<GattClient>(peripheral_id, server,
|
||||
std::move(callback));
|
||||
}
|
||||
|
||||
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
@@ -361,7 +360,7 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
|
||||
MediumEnvironment::Instance().FindBleV2Medium(id));
|
||||
if (remote_medium == nullptr) {
|
||||
NEARBY_LOGS(INFO) << "Peripheral not found, id= " << id;
|
||||
LOG(INFO) << "Peripheral not found, id= " << id;
|
||||
return false;
|
||||
}
|
||||
BluetoothAdapter& adapter = remote_medium->GetAdapter();
|
||||
@@ -380,8 +379,7 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
|
||||
BleV2Medium::GattServer::GattServer(
|
||||
BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback)
|
||||
: medium_(medium),
|
||||
callback_(std::move(callback)) {
|
||||
: medium_(medium), callback_(std::move(callback)) {
|
||||
BluetoothAdapter& adapter = medium.GetAdapter();
|
||||
MacAddress address;
|
||||
MacAddress::FromString(adapter.GetMacAddress(), address);
|
||||
@@ -429,11 +427,10 @@ bool BleV2Medium::GattServer::UpdateCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
const nearby::ByteArray& value) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 Ble GattServer UpdateCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value.data());
|
||||
LOG(INFO) << "G3 Ble GattServer UpdateCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value.data());
|
||||
characteristics_[characteristic] = value;
|
||||
return true;
|
||||
}
|
||||
@@ -442,11 +439,10 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
|
||||
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
|
||||
const ByteArray& new_value) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 Ble GattServer NotifyCharacteristicChanged, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), new_value = " << absl::BytesToHexString(new_value.data());
|
||||
LOG(INFO) << "G3 Ble GattServer NotifyCharacteristicChanged, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), new_value = " << absl::BytesToHexString(new_value.data());
|
||||
absl::Status status = absl::NotFoundError(
|
||||
"Characteristic not subscribed to receive notification.");
|
||||
for (auto& it : subscribers_) {
|
||||
@@ -460,7 +456,7 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
|
||||
}
|
||||
|
||||
absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -475,7 +471,7 @@ absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
|
||||
absl::StatusOr<ByteArray> result;
|
||||
CountDownLatch latch(1);
|
||||
callback_.on_characteristic_read_cb(
|
||||
remote_device, characteristic, offset,
|
||||
remote_device_id, characteristic, offset,
|
||||
[&](absl::StatusOr<absl::string_view> data) {
|
||||
if (data.ok()) {
|
||||
result = ByteArray(std::string(*data));
|
||||
@@ -489,14 +485,15 @@ absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
|
||||
}
|
||||
|
||||
absl::Status BleV2Medium::GattServer::WriteCharacteristic(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data) {
|
||||
if (HasCharacteristic(characteristic)) {
|
||||
absl::Status result;
|
||||
CountDownLatch latch(1);
|
||||
callback_.on_characteristic_write_cb(remote_device, characteristic, offset,
|
||||
data, [&](absl::Status status) {
|
||||
callback_.on_characteristic_write_cb(remote_device_id, characteristic,
|
||||
offset, data,
|
||||
[&](absl::Status status) {
|
||||
result = status;
|
||||
latch.CountDown();
|
||||
});
|
||||
@@ -508,13 +505,13 @@ absl::Status BleV2Medium::GattServer::WriteCharacteristic(
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattServer::AddCharacteristicSubscription(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
absl::AnyInvocable<void(absl::string_view value)> callback) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
const auto it = characteristics_.find(characteristic);
|
||||
if (it != characteristics_.end()) {
|
||||
subscribers_[SubscriberKey(&remote_device, characteristic)] =
|
||||
subscribers_[SubscriberKey(remote_device_id, characteristic)] =
|
||||
std::move(callback);
|
||||
return true;
|
||||
}
|
||||
@@ -522,12 +519,12 @@ bool BleV2Medium::GattServer::AddCharacteristicSubscription(
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattServer::RemoveCharacteristicSubscription(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
const auto it = characteristics_.find(characteristic);
|
||||
if (it != characteristics_.end()) {
|
||||
subscribers_.erase(SubscriberKey(&remote_device, characteristic));
|
||||
subscribers_.erase(SubscriberKey(remote_device_id, characteristic));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -562,10 +559,10 @@ void BleV2Medium::GattServer::Stop() {
|
||||
}
|
||||
|
||||
BleV2Medium::GattClient::GattClient(
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server,
|
||||
api::ble_v2::ClientGattConnectionCallback callback)
|
||||
: peripheral_(peripheral),
|
||||
: peripheral_id_(peripheral_id),
|
||||
gatt_server_(gatt_server),
|
||||
callback_(std::move(callback)) {
|
||||
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
@@ -587,7 +584,7 @@ BleV2Medium::GattClient::~GattClient() {
|
||||
|
||||
bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics(
|
||||
const Uuid& service_uuid, const std::vector<Uuid>& characteristic_uuids) {
|
||||
NEARBY_LOGS(INFO)
|
||||
LOG(INFO)
|
||||
<< "G3 Ble GattClient DiscoverServiceAndCharacteristics, service_uuid="
|
||||
<< service_uuid.Get16BitAsString();
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -608,10 +605,9 @@ std::optional<api::ble_v2::GattCharacteristic>
|
||||
BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
|
||||
const Uuid& characteristic_uuid) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattClient GetCharacteristic, service_uuid="
|
||||
<< service_uuid.Get16BitAsString()
|
||||
<< ", characteristic_uuid="
|
||||
<< std::string(characteristic_uuid);
|
||||
LOG(INFO) << "G3 Ble GattClient GetCharacteristic, service_uuid="
|
||||
<< service_uuid.Get16BitAsString()
|
||||
<< ", characteristic_uuid=" << std::string(characteristic_uuid);
|
||||
if (!is_connection_alive_) {
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -627,17 +623,15 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*borrowed);
|
||||
if (!gatt_server->HasCharacteristic(characteristic)) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "G3 Ble GattClient GetCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< ") not registered on GATT server";
|
||||
LOG(WARNING) << "G3 Ble GattClient GetCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< ") not registered on GATT server";
|
||||
return std::nullopt;
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 Ble GattClient GetCharacteristic, found characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << ")";
|
||||
LOG(INFO) << "G3 Ble GattClient GetCharacteristic, found characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << ")";
|
||||
|
||||
return characteristic;
|
||||
}
|
||||
@@ -655,19 +649,18 @@ std::optional<std::string> BleV2Medium::GattClient::ReadCharacteristic(
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*borrowed);
|
||||
absl::StatusOr<ByteArray> value =
|
||||
gatt_server->ReadCharacteristic(peripheral_, characteristic,
|
||||
gatt_server->ReadCharacteristic(peripheral_id_, characteristic,
|
||||
/*offset=*/0);
|
||||
if (!value.ok()) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble ReadCharacteristic failed, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << "), "
|
||||
<< value.status();
|
||||
LOG(INFO) << "G3 Ble ReadCharacteristic failed, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << "), " << value.status();
|
||||
return std::nullopt;
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "G3 Ble ReadCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value->data());
|
||||
LOG(INFO) << "G3 Ble ReadCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value->data());
|
||||
return value->string_data();
|
||||
}
|
||||
|
||||
@@ -684,15 +677,15 @@ bool BleV2Medium::GattClient::WriteCharacteristic(
|
||||
}
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*borrowed);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble WriteCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value);
|
||||
LOG(INFO) << "G3 Ble WriteCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value);
|
||||
absl::Status status =
|
||||
gatt_server->WriteCharacteristic(peripheral_, characteristic,
|
||||
gatt_server->WriteCharacteristic(peripheral_id_, characteristic,
|
||||
/*offset=*/0, value);
|
||||
if (!status.ok()) {
|
||||
NEARBY_LOGS(WARNING) << "WriteCharacteristic failed with " << status;
|
||||
LOG(WARNING) << "WriteCharacteristic failed with " << status;
|
||||
}
|
||||
return status.ok();
|
||||
}
|
||||
@@ -711,15 +704,15 @@ bool BleV2Medium::GattClient::SetCharacteristicSubscription(
|
||||
}
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
static_cast<BleV2Medium::GattServer*>(*borrowed);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble SetCharacteristicSubscription, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), enable = " << enable;
|
||||
LOG(INFO) << "G3 Ble SetCharacteristicSubscription, characteristic=("
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << "), enable = " << enable;
|
||||
if (enable) {
|
||||
return gatt_server->AddCharacteristicSubscription(
|
||||
peripheral_, characteristic, std::move(on_characteristic_changed_cb));
|
||||
peripheral_id_, characteristic,
|
||||
std::move(on_characteristic_changed_cb));
|
||||
} else {
|
||||
return gatt_server->RemoveCharacteristicSubscription(peripheral_,
|
||||
return gatt_server->RemoveCharacteristicSubscription(peripheral_id_,
|
||||
characteristic);
|
||||
}
|
||||
}
|
||||
@@ -727,7 +720,7 @@ bool BleV2Medium::GattClient::SetCharacteristicSubscription(
|
||||
void BleV2Medium::GattClient::Disconnect() {
|
||||
bool was_alive = is_connection_alive_.exchange(false);
|
||||
if (!was_alive) return;
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattClient Disconnect";
|
||||
LOG(INFO) << "G3 Ble GattClient Disconnect";
|
||||
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
if (borrowed) {
|
||||
BleV2Medium::GattServer* gatt_server =
|
||||
@@ -739,7 +732,7 @@ void BleV2Medium::GattClient::Disconnect() {
|
||||
void BleV2Medium::GattClient::OnServerDisconnected() {
|
||||
bool was_alive = is_connection_alive_.exchange(false);
|
||||
if (!was_alive) return;
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattServer disconnected";
|
||||
LOG(INFO) << "G3 Ble GattServer disconnected";
|
||||
if (callback_.disconnected_cb != nullptr) {
|
||||
callback_.disconnected_cb();
|
||||
}
|
||||
@@ -752,8 +745,8 @@ std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.erase(service_id);
|
||||
});
|
||||
NEARBY_LOGS(INFO) << "G3 Ble Adding server socket: medium=" << this
|
||||
<< ", service_id=" << service_id;
|
||||
LOG(INFO) << "G3 Ble Adding server socket: medium=" << this
|
||||
<< ", service_id=" << service_id;
|
||||
absl::MutexLock lock(&mutex_);
|
||||
server_sockets_.insert({service_id, server_socket.get()});
|
||||
return server_socket;
|
||||
@@ -769,10 +762,10 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
|
||||
const std::string& service_id, TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral& remote_peripheral,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble Connect [self]: medium=" << this
|
||||
<< ", adapter=" << &GetAdapter()
|
||||
<< ", peripheral=" << &GetPeripheral()
|
||||
<< ", service_id=" << service_id;
|
||||
LOG(INFO) << "G3 Ble Connect [self]: medium=" << this
|
||||
<< ", adapter=" << &GetAdapter()
|
||||
<< ", peripheral=" << &GetPeripheral()
|
||||
<< ", service_id=" << service_id;
|
||||
// First, find an instance of remote medium, that exposed this peripheral.
|
||||
BluetoothAdapter* remote_adapter =
|
||||
static_cast<BluetoothAdapter*>(remote_peripheral.GetPlatformData());
|
||||
@@ -783,10 +776,10 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
|
||||
}
|
||||
|
||||
BleV2ServerSocket* remote_server_socket = nullptr;
|
||||
NEARBY_LOGS(INFO) << "G3 Ble Connect [peer]: medium=" << remote_medium
|
||||
<< ", adapter=" << &remote_adapter
|
||||
<< ", peripheral=" << &remote_peripheral
|
||||
<< ", service_id=" << service_id;
|
||||
LOG(INFO) << "G3 Ble Connect [peer]: medium=" << remote_medium
|
||||
<< ", adapter=" << &remote_adapter
|
||||
<< ", peripheral=" << &remote_peripheral
|
||||
<< ", service_id=" << service_id;
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&remote_medium->mutex_);
|
||||
@@ -794,37 +787,36 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
|
||||
remote_server_socket =
|
||||
item != remote_medium->server_sockets_.end() ? item->second : nullptr;
|
||||
if (remote_server_socket == nullptr) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "G3 Ble Failed to find Ble Server socket: service_id="
|
||||
<< service_id;
|
||||
LOG(ERROR) << "G3 Ble Failed to find Ble Server socket: service_id="
|
||||
<< service_id;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (cancellation_flag->Cancelled()) {
|
||||
NEARBY_LOGS(ERROR) << "G3 BLE Connect: Has been cancelled: "
|
||||
"service_id="
|
||||
<< service_id;
|
||||
LOG(ERROR) << "G3 BLE Connect: Has been cancelled: "
|
||||
"service_id="
|
||||
<< service_id;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CancellationFlagListener listener(
|
||||
cancellation_flag, [&remote_server_socket]() {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble Cancel Connect.";
|
||||
if (remote_server_socket != nullptr) {
|
||||
remote_server_socket->Close();
|
||||
}
|
||||
});
|
||||
CancellationFlagListener listener(cancellation_flag,
|
||||
[&remote_server_socket]() {
|
||||
LOG(INFO) << "G3 Ble Cancel Connect.";
|
||||
if (remote_server_socket != nullptr) {
|
||||
remote_server_socket->Close();
|
||||
}
|
||||
});
|
||||
|
||||
auto socket = std::make_unique<BleV2Socket>(&GetAdapter());
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!remote_server_socket->Connect(*socket)) {
|
||||
NEARBY_LOGS(ERROR) << "G3 Ble Failed to connect to existing Ble "
|
||||
"Server socket: service_id="
|
||||
<< service_id;
|
||||
LOG(ERROR) << "G3 Ble Failed to connect to existing Ble "
|
||||
"Server socket: service_id="
|
||||
<< service_id;
|
||||
return nullptr;
|
||||
}
|
||||
NEARBY_LOGS(INFO) << "G3 Ble Connect to socket=" << socket.get();
|
||||
LOG(INFO) << "G3 Ble Connect to socket=" << socket.get();
|
||||
return socket;
|
||||
}
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::ServerGattConnectionCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -226,20 +226,20 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
const std::vector<Uuid>& characteristic_uuids);
|
||||
|
||||
absl::StatusOr<ByteArray> ReadCharacteristic(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset);
|
||||
|
||||
absl::Status WriteCharacteristic(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data);
|
||||
|
||||
bool AddCharacteristicSubscription(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
absl::AnyInvocable<void(absl::string_view value)>);
|
||||
bool RemoveCharacteristicSubscription(
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
|
||||
const api::ble_v2::GattCharacteristic& characteristic);
|
||||
|
||||
bool HasCharacteristic(
|
||||
@@ -249,7 +249,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
void Disconnect(GattClient* client);
|
||||
|
||||
private:
|
||||
using SubscriberKey = std::pair<const api::ble_v2::BlePeripheral*,
|
||||
using SubscriberKey = std::pair<const api::ble_v2::BlePeripheral::UniqueId,
|
||||
api::ble_v2::GattCharacteristic>;
|
||||
using SubscriberCallback =
|
||||
absl::AnyInvocable<void(absl::string_view value)>;
|
||||
@@ -270,7 +270,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
// A concrete implementation for GattClient.
|
||||
class GattClient : public api::ble_v2::GattClient {
|
||||
public:
|
||||
GattClient(api::ble_v2::BlePeripheral& peripheral,
|
||||
GattClient(api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server,
|
||||
api::ble_v2::ClientGattConnectionCallback callback);
|
||||
~GattClient() override;
|
||||
@@ -305,7 +305,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
// disconnected/*false*/, the instance needs to be created again to bring
|
||||
// it alive.
|
||||
std::atomic_bool is_connection_alive_ = true;
|
||||
api::ble_v2::BlePeripheral& peripheral_;
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id_;
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server_;
|
||||
api::ble_v2::ClientGattConnectionCallback callback_;
|
||||
};
|
||||
|
||||
@@ -587,17 +587,18 @@ std::unique_ptr<api::ble_v2::GattServer> BleV2Medium::StartGattServer(
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral, TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
LOG(INFO) << "ConnectToGattServer is called, address: "
|
||||
<< peripheral.GetAddress()
|
||||
LOG(INFO) << "ConnectToGattServer is called with peripheral id: "
|
||||
<< peripheral_id
|
||||
<< ", power:" << TxPowerLevelToName(tx_power_level);
|
||||
|
||||
try {
|
||||
// In windows, peripheral unique id is the same as the bluetooth address.
|
||||
BluetoothLEDevice ble_device =
|
||||
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral.GetUniqueId())
|
||||
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral_id)
|
||||
.get();
|
||||
|
||||
return std::make_unique<BleGattClient>(ble_device);
|
||||
|
||||
@@ -68,7 +68,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::ServerGattConnectionCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -59,7 +59,7 @@ MediumEnvironment& MediumEnvironment::Instance() {
|
||||
|
||||
void MediumEnvironment::Start(EnvironmentConfig config) {
|
||||
if (!enabled_.exchange(true)) {
|
||||
NEARBY_LOGS(INFO) << "MediumEnvironment::Start()";
|
||||
LOG(INFO) << "MediumEnvironment::Start()";
|
||||
config_ = std::move(config);
|
||||
if (config_.use_simulated_clock) {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -71,7 +71,7 @@ void MediumEnvironment::Start(EnvironmentConfig config) {
|
||||
|
||||
void MediumEnvironment::Stop() {
|
||||
if (enabled_.exchange(false)) {
|
||||
NEARBY_LOGS(INFO) << "MediumEnvironment::Stop()";
|
||||
LOG(INFO) << "MediumEnvironment::Stop()";
|
||||
Sync(false);
|
||||
if (config_.use_simulated_clock) {
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -83,7 +83,7 @@ void MediumEnvironment::Stop() {
|
||||
|
||||
void MediumEnvironment::Reset() {
|
||||
RunOnMediumEnvironmentThread([this]() {
|
||||
NEARBY_LOGS(INFO) << "MediumEnvironment::Reset()";
|
||||
LOG(INFO) << "MediumEnvironment::Reset()";
|
||||
bluetooth_adapters_.clear();
|
||||
bluetooth_mediums_.clear();
|
||||
ble_mediums_.clear();
|
||||
@@ -107,7 +107,7 @@ void MediumEnvironment::Reset() {
|
||||
|
||||
void MediumEnvironment::Sync(bool enable_notifications) {
|
||||
enable_notifications_ = enable_notifications;
|
||||
NEARBY_LOGS(INFO) << "MediumEnvironment::sync=" << enable_notifications;
|
||||
LOG(INFO) << "MediumEnvironment::sync=" << enable_notifications;
|
||||
int count = 0;
|
||||
do {
|
||||
CountDownLatch latch(1);
|
||||
@@ -121,8 +121,7 @@ void MediumEnvironment::Sync(bool enable_notifications) {
|
||||
RunOnMediumEnvironmentThread([&latch]() { latch.CountDown(); });
|
||||
latch.Await();
|
||||
} while (count < job_count_);
|
||||
NEARBY_LOGS(INFO) << "MediumEnvironment::Sync(): done [count=" << count
|
||||
<< "]";
|
||||
LOG(INFO) << "MediumEnvironment::Sync(): done [count=" << count << "]";
|
||||
}
|
||||
|
||||
const EnvironmentConfig& MediumEnvironment::GetEnvironmentConfig() {
|
||||
@@ -137,17 +136,15 @@ void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
RunOnMediumEnvironmentThread([this, &adapter, &adapter_device,
|
||||
name = std::move(name), enabled, mode,
|
||||
&latch]() {
|
||||
NEARBY_LOGS(INFO) << "[adapter=" << &adapter
|
||||
<< ", device=" << &adapter_device
|
||||
<< "] update: name=" << ", enabled=" << enabled
|
||||
<< ", mode=" << int32_t(mode);
|
||||
LOG(INFO) << "[adapter=" << &adapter << ", device=" << &adapter_device
|
||||
<< "] update: name=" << ", enabled=" << enabled
|
||||
<< ", mode=" << int32_t(mode);
|
||||
for (auto& medium_info : bluetooth_mediums_) {
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to medium that owns this adapter.
|
||||
if (info.adapter == &adapter) continue;
|
||||
NEARBY_LOGS(INFO) << "[adapter=" << &adapter
|
||||
<< ", device=" << &adapter_device
|
||||
<< "] notify: adapter=" << info.adapter;
|
||||
LOG(INFO) << "[adapter=" << &adapter << ", device=" << &adapter_device
|
||||
<< "] notify: adapter=" << info.adapter;
|
||||
OnBluetoothDeviceStateChanged(info, adapter_device, name, mode, enabled);
|
||||
}
|
||||
// We don't care if there is an adapter already since all we store is a
|
||||
@@ -171,9 +168,8 @@ void MediumEnvironment::OnBluetoothDeviceStateChanged(
|
||||
if (!enabled_) return;
|
||||
auto item = info.devices.find(&device);
|
||||
if (item == info.devices.end()) {
|
||||
NEARBY_LOGS(INFO) << "OnBluetoothDeviceStateChanged [device impl="
|
||||
<< &device << "]: new device; notify="
|
||||
<< enable_notifications_.load();
|
||||
LOG(INFO) << "OnBluetoothDeviceStateChanged [device impl=" << &device
|
||||
<< "]: new device; notify=" << enable_notifications_.load();
|
||||
if (mode == api::BluetoothAdapter::ScanMode::kConnectableDiscoverable &&
|
||||
enabled) {
|
||||
// New device is turned on, and is in discoverable state.
|
||||
@@ -188,9 +184,8 @@ void MediumEnvironment::OnBluetoothDeviceStateChanged(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "OnBluetoothDeviceStateChanged [device impl="
|
||||
<< &device << "]: existing device; notify="
|
||||
<< enable_notifications_.load();
|
||||
LOG(INFO) << "OnBluetoothDeviceStateChanged [device impl=" << &device
|
||||
<< "]: existing device; notify=" << enable_notifications_.load();
|
||||
auto& discovered_name = item->second;
|
||||
if (mode == api::BluetoothAdapter::ScanMode::kConnectableDiscoverable &&
|
||||
enabled) {
|
||||
@@ -232,11 +227,11 @@ api::BluetoothDevice* MediumEnvironment::FindBluetoothDevice(
|
||||
api::BluetoothDevice* device = nullptr;
|
||||
CountDownLatch latch(1);
|
||||
RunOnMediumEnvironmentThread([this, &device, &latch, &mac_address]() {
|
||||
NEARBY_LOGS(INFO) << " Looking for: " << mac_address;
|
||||
LOG(INFO) << " Looking for: " << mac_address;
|
||||
for (auto& item : bluetooth_mediums_) {
|
||||
auto* adapter = item.second.adapter;
|
||||
if (!adapter) continue;
|
||||
NEARBY_LOGS(INFO) << " Adapter: " << adapter->GetMacAddress();
|
||||
LOG(INFO) << " Adapter: " << adapter->GetMacAddress();
|
||||
if (adapter->GetMacAddress() == mac_address) {
|
||||
device = bluetooth_adapters_[adapter];
|
||||
break;
|
||||
@@ -252,7 +247,7 @@ api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(
|
||||
absl::string_view address) {
|
||||
api::ble_v2::BleMedium* device = nullptr;
|
||||
CountDownLatch latch(1);
|
||||
NEARBY_LOGS(INFO) << "FindBleV2Medium " << address;
|
||||
LOG(INFO) << "FindBleV2Medium " << address;
|
||||
RunOnMediumEnvironmentThread([&]() {
|
||||
for (auto& item : ble_v2_mediums_) {
|
||||
auto* medium = item.first;
|
||||
@@ -266,7 +261,7 @@ api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(
|
||||
});
|
||||
latch.Await();
|
||||
if (device == nullptr) {
|
||||
NEARBY_LOGS(INFO) << "FindBleV2Medium, not found: " << address;
|
||||
LOG(INFO) << "FindBleV2Medium, not found: " << address;
|
||||
}
|
||||
return device;
|
||||
}
|
||||
@@ -274,7 +269,7 @@ api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(
|
||||
api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(uint64_t id) {
|
||||
api::ble_v2::BleMedium* device = nullptr;
|
||||
CountDownLatch latch(1);
|
||||
NEARBY_LOGS(INFO) << "FindBleV2Medium " << id;
|
||||
LOG(INFO) << "FindBleV2Medium " << id;
|
||||
RunOnMediumEnvironmentThread([&]() {
|
||||
for (auto& item : ble_v2_mediums_) {
|
||||
auto* medium = item.first;
|
||||
@@ -288,7 +283,7 @@ api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium(uint64_t id) {
|
||||
});
|
||||
latch.Await();
|
||||
if (device == nullptr) {
|
||||
NEARBY_LOGS(INFO) << "FindBleV2Medium, not found: " << id;
|
||||
LOG(INFO) << "FindBleV2Medium, not found: " << id;
|
||||
}
|
||||
return device;
|
||||
}
|
||||
@@ -297,20 +292,19 @@ void MediumEnvironment::OnBlePeripheralStateChanged(
|
||||
BleMediumContext& info, api::BlePeripheral& peripheral,
|
||||
const std::string& service_id, bool fast_advertisement, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
NEARBY_LOGS(INFO) << "OnBleServiceStateChanged [peripheral impl="
|
||||
<< &peripheral << "]; context=" << &info
|
||||
<< "; service_id=" << service_id
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnBleServiceStateChanged [peripheral impl=" << &peripheral
|
||||
<< "]; context=" << &info << "; service_id=" << service_id
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (!enable_notifications_) return;
|
||||
if (enabled) {
|
||||
RunOnMediumEnvironmentThread([&info, &peripheral, service_id,
|
||||
fast_advertisement]() {
|
||||
NEARBY_LOGS(INFO) << "[Run] OnBleServiceStateChanged [peripheral impl="
|
||||
<< &peripheral << "]; context=" << &info
|
||||
<< "; service_id=" << service_id;
|
||||
info.discovery_callback.peripheral_discovered_cb(peripheral, service_id,
|
||||
fast_advertisement);
|
||||
});
|
||||
RunOnMediumEnvironmentThread(
|
||||
[&info, &peripheral, service_id, fast_advertisement]() {
|
||||
LOG(INFO) << "[Run] OnBleServiceStateChanged [peripheral impl="
|
||||
<< &peripheral << "]; context=" << &info
|
||||
<< "; service_id=" << service_id;
|
||||
info.discovery_callback.peripheral_discovered_cb(
|
||||
peripheral, service_id, fast_advertisement);
|
||||
});
|
||||
} else {
|
||||
info.discovery_callback.peripheral_lost_cb(peripheral, service_id);
|
||||
}
|
||||
@@ -321,13 +315,12 @@ void MediumEnvironment::OnBleV2PeripheralStateChanged(
|
||||
const api::ble_v2::BleAdvertisementData& ble_advertisement_data,
|
||||
api::ble_v2::BlePeripheral& peripheral) {
|
||||
if (!enabled_) return;
|
||||
NEARBY_LOGS(INFO) << "OnBleServiceStateChanged [peripheral impl="
|
||||
<< &peripheral << "]; medium_context=" << &context
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnBleServiceStateChanged [peripheral impl=" << &peripheral
|
||||
<< "]; medium_context=" << &context
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (!enable_notifications_) return;
|
||||
NEARBY_LOGS(INFO) << "[Run] OnBleServiceStateChanged [peripheral impl="
|
||||
<< &peripheral << "]; context=" << &context
|
||||
<< "; notify=" << enabled;
|
||||
LOG(INFO) << "[Run] OnBleServiceStateChanged [peripheral impl=" << &peripheral
|
||||
<< "]; context=" << &context << "; notify=" << enabled;
|
||||
|
||||
for (auto& element : context.scan_callback_map) {
|
||||
if (element.first.first == service_id) {
|
||||
@@ -349,10 +342,9 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = info.discovered_services.find(service_name);
|
||||
if (item == info.discovered_services.end()) {
|
||||
NEARBY_LOGS(INFO) << "OnWifiLanServiceStateChanged; context=" << &info
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnWifiLanServiceStateChanged; context=" << &info
|
||||
<< "; service_type=" << service_type << "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
// Find advertising service with matched service_type. Report it as
|
||||
// discovered.
|
||||
@@ -369,10 +361,10 @@ void MediumEnvironment::OnWifiLanServiceStateChanged(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "OnWifiLanServiceStateChanged: exisitng service; context=" << &info
|
||||
<< "; service_type=" << service_type << "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnWifiLanServiceStateChanged: exisitng service; context="
|
||||
<< &info << "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
if (enable_notifications_) {
|
||||
RunOnMediumEnvironmentThread(
|
||||
@@ -407,10 +399,9 @@ void MediumEnvironment::OnAwdlServiceStateChanged(
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
auto item = info.discovered_services.find(service_name);
|
||||
if (item == info.discovered_services.end()) {
|
||||
NEARBY_LOGS(INFO) << "OnAwdlServiceStateChanged; context=" << &info
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnAwdlServiceStateChanged; context=" << &info
|
||||
<< "; service_type=" << service_type << "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
// Find advertising service with matched service_type. Report it as
|
||||
// discovered.
|
||||
@@ -427,10 +418,10 @@ void MediumEnvironment::OnAwdlServiceStateChanged(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "OnAwdlServiceStateChanged: exisitng service; context="
|
||||
<< &info << "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
LOG(INFO) << "OnAwdlServiceStateChanged: exisitng service; context="
|
||||
<< &info << "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled
|
||||
<< "; notify=" << enable_notifications_.load();
|
||||
if (enabled) {
|
||||
if (enable_notifications_) {
|
||||
RunOnMediumEnvironmentThread(
|
||||
@@ -475,8 +466,8 @@ void MediumEnvironment::RegisterBluetoothMedium(
|
||||
}})
|
||||
.first->second;
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOGS(INFO) << "Registered: Bluetooth medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter;
|
||||
LOG(INFO) << "Registered: Bluetooth medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter;
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
@@ -498,11 +489,11 @@ void MediumEnvironment::UpdateBluetoothMedium(
|
||||
auto& context = item->second;
|
||||
context.callback = std::move(callback);
|
||||
auto* owned_adapter = context.adapter;
|
||||
NEARBY_LOGS(INFO) << "Updated: this=" << this << "; medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter
|
||||
<< "; name=" << owned_adapter->GetName()
|
||||
<< "; enabled=" << owned_adapter->IsEnabled()
|
||||
<< "; mode=" << int32_t(owned_adapter->GetScanMode());
|
||||
LOG(INFO) << "Updated: this=" << this << "; medium=" << &medium
|
||||
<< "; adapter=" << owned_adapter
|
||||
<< "; name=" << owned_adapter->GetName()
|
||||
<< "; enabled=" << owned_adapter->IsEnabled()
|
||||
<< "; mode=" << int32_t(owned_adapter->GetScanMode());
|
||||
for (auto& adapter_device : bluetooth_adapters_) {
|
||||
auto& adapter = adapter_device.first;
|
||||
auto& device = adapter_device.second;
|
||||
@@ -522,7 +513,7 @@ void MediumEnvironment::UnregisterBluetoothMedium(
|
||||
auto item = bluetooth_mediums_.extract(&medium);
|
||||
latch.CountDown();
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered Bluetooth medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered Bluetooth medium:" << &medium;
|
||||
});
|
||||
latch.Await();
|
||||
}
|
||||
@@ -531,7 +522,7 @@ void MediumEnvironment::RegisterBleMedium(api::BleMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
ble_mediums_.insert({&medium, BleMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: BLE medium:" << &medium;
|
||||
LOG(INFO) << "Registered: BLE medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -539,32 +530,32 @@ void MediumEnvironment::UpdateBleMediumForAdvertising(
|
||||
api::BleMedium& medium, api::BlePeripheral& peripheral,
|
||||
const std::string& service_id, bool fast_advertisement, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &peripheral, service_id,
|
||||
fast_advertisement, enabled]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO) << "UpdateBleMediumForAdvertising failed. There is no "
|
||||
"medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.ble_peripheral = &peripheral;
|
||||
context.advertising = enabled;
|
||||
context.fast_advertisement = fast_advertisement;
|
||||
NEARBY_LOGS(INFO) << "Update Ble medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id
|
||||
<< "; name=" << peripheral.GetName()
|
||||
<< "; fast_advertisement=" << fast_advertisement
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : ble_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
OnBlePeripheralStateChanged(info, peripheral, service_id,
|
||||
fast_advertisement, enabled);
|
||||
}
|
||||
});
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &peripheral, service_id, fast_advertisement, enabled]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
LOG(INFO) << "UpdateBleMediumForAdvertising failed. There is no "
|
||||
"medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.ble_peripheral = &peripheral;
|
||||
context.advertising = enabled;
|
||||
context.fast_advertisement = fast_advertisement;
|
||||
LOG(INFO) << "Update Ble medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id
|
||||
<< "; name=" << peripheral.GetName()
|
||||
<< "; fast_advertisement=" << fast_advertisement
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : ble_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium.
|
||||
if (local_medium == &medium) continue;
|
||||
OnBlePeripheralStateChanged(info, peripheral, service_id,
|
||||
fast_advertisement, enabled);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateBleMediumForScanning(
|
||||
@@ -577,20 +568,17 @@ void MediumEnvironment::UpdateBleMediumForScanning(
|
||||
callback = std::move(callback), enabled]() mutable {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleMediumFoScanning failed. There is no medium "
|
||||
"registered.";
|
||||
LOG(INFO) << "UpdateBleMediumFoScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovery_callback = std::move(callback);
|
||||
NEARBY_LOGS(INFO) << "Update Ble medium for scanning: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_id=" << service_id
|
||||
<< "; fast_advertisement_service_uuid="
|
||||
<< absl::BytesToHexString(
|
||||
fast_advertisement_service_uuid)
|
||||
<< "; enabled=" << enabled;
|
||||
LOG(INFO) << "Update Ble medium for scanning: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id
|
||||
<< "; fast_advertisement_service_uuid="
|
||||
<< absl::BytesToHexString(fast_advertisement_service_uuid)
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : ble_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
@@ -610,20 +598,18 @@ void MediumEnvironment::UpdateBleMediumForAcceptedConnection(
|
||||
api::BleMedium& medium, const std::string& service_id,
|
||||
BleAcceptedConnectionCallback callback) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, service_id, callback = std::move(callback)]() mutable {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Update Ble medium failed. There is no medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.accepted_connection_callback = std::move(callback);
|
||||
NEARBY_LOGS(INFO) << "Update Ble medium for accepted callback: this="
|
||||
<< this << "; medium=" << &medium
|
||||
<< "; service_id=" << service_id;
|
||||
});
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_id,
|
||||
callback = std::move(callback)]() mutable {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
LOG(INFO) << "Update Ble medium failed. There is no medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.accepted_connection_callback = std::move(callback);
|
||||
LOG(INFO) << "Update Ble medium for accepted callback: this=" << this
|
||||
<< "; medium=" << &medium << "; service_id=" << service_id;
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UnregisterBleMedium(api::BleMedium& medium) {
|
||||
@@ -633,7 +619,7 @@ void MediumEnvironment::UnregisterBleMedium(api::BleMedium& medium) {
|
||||
auto item = ble_mediums_.extract(&medium);
|
||||
latch.CountDown();
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered BLE medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered BLE medium:" << &medium;
|
||||
});
|
||||
latch.Await();
|
||||
}
|
||||
@@ -642,20 +628,18 @@ void MediumEnvironment::CallBleAcceptedConnectionCallback(
|
||||
api::BleMedium& medium, api::BleSocket& socket,
|
||||
const std::string& service_id) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &socket, service_id]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Call AcceptedConnectionCallback failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
if (info.accepted_connection_callback) {
|
||||
info.accepted_connection_callback(socket, service_id);
|
||||
}
|
||||
});
|
||||
RunOnMediumEnvironmentThread([this, &medium, &socket, service_id]() {
|
||||
auto item = ble_mediums_.find(&medium);
|
||||
if (item == ble_mediums_.end()) {
|
||||
LOG(INFO) << "Call AcceptedConnectionCallback failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& info = item->second;
|
||||
if (info.accepted_connection_callback) {
|
||||
info.accepted_connection_callback(socket, service_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::RegisterBleV2Medium(
|
||||
@@ -664,7 +648,7 @@ void MediumEnvironment::RegisterBleV2Medium(
|
||||
RunOnMediumEnvironmentThread([this, &medium, peripheral]() {
|
||||
ble_v2_mediums_.insert(
|
||||
{&medium, BleV2MediumContext{.ble_peripheral = peripheral}});
|
||||
NEARBY_LOGS(INFO) << "Registered: BLE V2 medium:" << &medium;
|
||||
LOG(INFO) << "Registered: BLE V2 medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -673,65 +657,60 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising(
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, &peripheral, advertisement_data = advertisement_data,
|
||||
enabled]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleV2MediumForAdvertising failed. There is no "
|
||||
"medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = it->second;
|
||||
context.ble_peripheral = &peripheral;
|
||||
context.advertising = enabled;
|
||||
context.advertisement_data = advertisement_data;
|
||||
RunOnMediumEnvironmentThread([this, &medium, &peripheral,
|
||||
advertisement_data = advertisement_data,
|
||||
enabled]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
LOG(INFO) << "UpdateBleV2MediumForAdvertising failed. There is no "
|
||||
"medium registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = it->second;
|
||||
context.ble_peripheral = &peripheral;
|
||||
context.advertising = enabled;
|
||||
context.advertisement_data = advertisement_data;
|
||||
|
||||
NEARBY_LOGS(INFO) << "UpdateBleV2MediumForAdvertising: this=" << this
|
||||
<< ", medium=" << &medium
|
||||
<< ", medium_context=" << &context
|
||||
<< ", peripheral=" << &peripheral
|
||||
<< ", enabled=" << enabled;
|
||||
LOG(INFO) << "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;
|
||||
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 scanning.
|
||||
if (!remote_context.scanning) continue;
|
||||
// Do not send notification to the same medium.
|
||||
if (remote_medium == &medium) continue;
|
||||
// Do not send notification to the medium that is not scanning.
|
||||
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);
|
||||
}
|
||||
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);
|
||||
for (auto& remote_scanning_service_uuid : remote_scanning_service_uuids) {
|
||||
auto const it = context.advertisement_data.service_data.find(
|
||||
remote_scanning_service_uuid);
|
||||
|
||||
// Only skip when service data is not found and the medium is
|
||||
// enabled. Mediums that stop advertising (disabled) pass in empty
|
||||
// advertisement data but should still be processed.
|
||||
if (it == context.advertisement_data.service_data.end() && enabled)
|
||||
continue;
|
||||
// Only skip when service data is not found and the medium is
|
||||
// enabled. Mediums that stop advertising (disabled) pass in empty
|
||||
// advertisement data but should still be processed.
|
||||
if (it == context.advertisement_data.service_data.end() && enabled)
|
||||
continue;
|
||||
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "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);
|
||||
}
|
||||
}
|
||||
});
|
||||
LOG(INFO) << "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(
|
||||
@@ -746,16 +725,14 @@ void MediumEnvironment::UpdateBleV2MediumForScanning(
|
||||
enabled]() mutable {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleV2MediumForScanning failed. There is no medium "
|
||||
"registered.";
|
||||
LOG(INFO) << "UpdateBleV2MediumForScanning failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
BleV2MediumContext& context = it->second;
|
||||
NEARBY_LOGS(INFO) << "UpdateBleV2MediumForScanning: this=" << this
|
||||
<< ", medium=" << &medium
|
||||
<< ", medium_context=" << &context
|
||||
<< ", enabled=" << enabled;
|
||||
LOG(INFO) << "UpdateBleV2MediumForScanning: this=" << this
|
||||
<< ", medium=" << &medium << ", medium_context=" << &context
|
||||
<< ", enabled=" << enabled;
|
||||
if (enabled) {
|
||||
context.scanning = true;
|
||||
callback.start_scanning_result(absl::OkStatus());
|
||||
@@ -776,12 +753,12 @@ void MediumEnvironment::UpdateBleV2MediumForScanning(
|
||||
scanning_service_uuid);
|
||||
if (it == remote_context.advertisement_data.service_data.end())
|
||||
continue;
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateBleV2MediumForScanning, found other medium="
|
||||
<< remote_medium << ", remote_medium_context=" << &remote_context
|
||||
<< ", scanning_service_uuid="
|
||||
<< scanning_service_uuid.Get16BitAsString()
|
||||
<< ". Ready to call OnBleV2PeripheralStateChanged.";
|
||||
LOG(INFO) << "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);
|
||||
@@ -802,7 +779,7 @@ void MediumEnvironment::UnregisterBleV2Medium(api::ble_v2::BleMedium& medium) {
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = ble_v2_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered BLE V2 medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered BLE V2 medium:" << &medium;
|
||||
});
|
||||
}
|
||||
std::optional<MediumEnvironment::BleV2MediumStatus>
|
||||
@@ -840,8 +817,8 @@ void MediumEnvironment::RegisterWebRtcSignalingMessenger(
|
||||
std::move(complete_callback)}]() mutable {
|
||||
webrtc_signaling_message_callback_[self_id] = std::move(message_callback);
|
||||
webrtc_signaling_complete_callback_[self_id] = std::move(complete_callback);
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Registered: WebRTC signaling message callback for id = " << self_id;
|
||||
LOG(INFO) << "Registered: WebRTC signaling message callback for id = "
|
||||
<< self_id;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -853,9 +830,8 @@ void MediumEnvironment::UnregisterWebRtcSignalingMessenger(
|
||||
webrtc_signaling_message_callback_.extract(self_id);
|
||||
auto complete_callback_item =
|
||||
webrtc_signaling_complete_callback_.extract(self_id);
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Unregistered WebRTC signaling message callback for id = "
|
||||
<< self_id;
|
||||
LOG(INFO) << "Unregistered WebRTC signaling message callback for id = "
|
||||
<< self_id;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -866,8 +842,7 @@ void MediumEnvironment::SendWebRtcSignalingMessage(absl::string_view peer_id,
|
||||
[this, peer_id{std::string(peer_id)}, message]() {
|
||||
auto item = webrtc_signaling_message_callback_.find(peer_id);
|
||||
if (item == webrtc_signaling_message_callback_.end()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "No callback registered for peer id = " << peer_id;
|
||||
LOG(WARNING) << "No callback registered for peer id = " << peer_id;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -882,8 +857,7 @@ void MediumEnvironment::SendWebRtcSignalingComplete(absl::string_view peer_id,
|
||||
[this, peer_id{std::string(peer_id)}, success]() {
|
||||
auto item = webrtc_signaling_complete_callback_.find(peer_id);
|
||||
if (item == webrtc_signaling_complete_callback_.end()) {
|
||||
NEARBY_LOGS(WARNING)
|
||||
<< "No callback registered for peer id = " << peer_id;
|
||||
LOG(WARNING) << "No callback registered for peer id = " << peer_id;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -931,7 +905,7 @@ void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
wifi_lan_mediums_.insert({&medium, WifiLanMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: WifiLan medium:" << &medium;
|
||||
LOG(INFO) << "Registered: WifiLan medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -939,7 +913,7 @@ void MediumEnvironment::RegisterAwdlMedium(api::AwdlMedium& medium) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
awdl_mediums_.insert({&medium, AwdlMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: Awdl medium:" << &medium;
|
||||
LOG(INFO) << "Registered: Awdl medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -951,11 +925,9 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
enabled]() {
|
||||
std::string service_name = service_info.GetServiceName();
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_name=" << service_name
|
||||
<< "; service_type=" << service_type
|
||||
<< ", enabled=" << enabled;
|
||||
LOG(INFO) << "Update WifiLan medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium << "; service_name=" << service_name
|
||||
<< "; service_type=" << service_type << ", enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
@@ -977,31 +949,29 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising(
|
||||
void MediumEnvironment::UpdateAwdlMediumForAdvertising(
|
||||
api::AwdlMedium& medium, const NsdServiceInfo& service_info, bool enabled) {
|
||||
if (!enabled_) return;
|
||||
RunOnMediumEnvironmentThread(
|
||||
[this, &medium, service_info = service_info, enabled]() {
|
||||
std::string service_name = service_info.GetServiceName();
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
NEARBY_LOGS(INFO) << "Update Awdl medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_name=" << service_name
|
||||
<< "; service_type=" << service_type
|
||||
<< ", enabled=" << enabled;
|
||||
for (auto& medium_info : awdl_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium but update
|
||||
// service info map.
|
||||
if (local_medium == &medium) {
|
||||
if (enabled) {
|
||||
info.advertising_services.insert({service_name, service_info});
|
||||
} else {
|
||||
info.advertising_services.erase(service_name);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
OnAwdlServiceStateChanged(info, service_info, enabled);
|
||||
RunOnMediumEnvironmentThread([this, &medium, service_info = service_info,
|
||||
enabled]() {
|
||||
std::string service_name = service_info.GetServiceName();
|
||||
std::string service_type = service_info.GetServiceType();
|
||||
LOG(INFO) << "Update Awdl medium for advertising: this=" << this
|
||||
<< "; medium=" << &medium << "; service_name=" << service_name
|
||||
<< "; service_type=" << service_type << ", enabled=" << enabled;
|
||||
for (auto& medium_info : awdl_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
// Do not send notification to the same medium but update
|
||||
// service info map.
|
||||
if (local_medium == &medium) {
|
||||
if (enabled) {
|
||||
info.advertising_services.insert({service_name, service_info});
|
||||
} else {
|
||||
info.advertising_services.erase(service_name);
|
||||
}
|
||||
});
|
||||
continue;
|
||||
}
|
||||
OnAwdlServiceStateChanged(info, service_info, enabled);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
@@ -1012,17 +982,15 @@ void MediumEnvironment::UpdateWifiLanMediumForDiscovery(
|
||||
service_type, enabled]() mutable {
|
||||
auto item = wifi_lan_mediums_.find(&medium);
|
||||
if (item == wifi_lan_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
LOG(INFO) << "UpdateWifiLanMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovered_callbacks.insert({service_type, std::move(callback)});
|
||||
NEARBY_LOGS(INFO) << "Update WifiLan medium for discovery: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
LOG(INFO) << "Update WifiLan medium for discovery: this=" << this
|
||||
<< "; medium=" << &medium << "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : wifi_lan_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
@@ -1045,17 +1013,15 @@ void MediumEnvironment::UpdateAwdlMediumForDiscovery(
|
||||
service_type, enabled]() mutable {
|
||||
auto item = awdl_mediums_.find(&medium);
|
||||
if (item == awdl_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "UpdateAwdlMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
LOG(INFO) << "UpdateAwdlMediumForDiscovery failed. There is no medium "
|
||||
"registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = item->second;
|
||||
context.discovered_callbacks.insert({service_type, std::move(callback)});
|
||||
NEARBY_LOGS(INFO) << "Update Awdl medium for discovery: this=" << this
|
||||
<< "; medium=" << &medium
|
||||
<< "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
LOG(INFO) << "Update Awdl medium for discovery: this=" << this
|
||||
<< "; medium=" << &medium << "; service_type=" << service_type
|
||||
<< "; enabled=" << enabled;
|
||||
for (auto& medium_info : awdl_mediums_) {
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
@@ -1075,7 +1041,7 @@ void MediumEnvironment::UnregisterWifiLanMedium(api::WifiLanMedium& medium) {
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = wifi_lan_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiLan medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered WifiLan medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1084,7 +1050,7 @@ void MediumEnvironment::UnregisterAwdlMedium(api::AwdlMedium& medium) {
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
auto item = awdl_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered Awdl medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered Awdl medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1142,7 +1108,7 @@ void MediumEnvironment::RegisterWifiDirectMedium(
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
MutexLock lock(&mutex_);
|
||||
wifi_direct_mediums_.insert({&medium, WifiDirectMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: WifiDirect medium:" << &medium;
|
||||
LOG(INFO) << "Registered: WifiDirect medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1156,13 +1122,13 @@ api::WifiDirectMedium* MediumEnvironment::GetWifiDirectMedium(
|
||||
if ((info.wifi_direct_credentials->GetSSID() == ssid) ||
|
||||
(!ip_address.empty() &&
|
||||
(info.wifi_direct_credentials->GetIPAddress() == ip_address))) {
|
||||
NEARBY_LOGS(INFO) << "Found Remote WifiDirect medium=" << medium_found;
|
||||
LOG(INFO) << "Found Remote WifiDirect medium=" << medium_found;
|
||||
return medium_found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << "Can't find WifiDirect medium!";
|
||||
LOG(INFO) << "Can't find WifiDirect medium!";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1182,14 +1148,13 @@ void MediumEnvironment::UpdateWifiDirectMediumForStartOrConnect(
|
||||
: (enabled ? "Connected" : "Disconneced"));
|
||||
|
||||
if (wifi_direct_credentials) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Update WifiDirect medium for GO: this=" << this
|
||||
<< "; medium=" << &medium << role_status
|
||||
<< "; ssid=" << wifi_direct_credentials->GetSSID()
|
||||
<< "; password=" << wifi_direct_credentials->GetPassword();
|
||||
LOG(INFO) << "Update WifiDirect medium for GO: this=" << this
|
||||
<< "; medium=" << &medium << role_status
|
||||
<< "; ssid=" << wifi_direct_credentials->GetSSID()
|
||||
<< "; password=" << wifi_direct_credentials->GetPassword();
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "Reset WifiDirect medium for GO: this=" << this
|
||||
<< "; medium=" << &medium << role_status;
|
||||
LOG(INFO) << "Reset WifiDirect medium for GO: this=" << this
|
||||
<< "; medium=" << &medium << role_status;
|
||||
}
|
||||
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -1197,7 +1162,7 @@ void MediumEnvironment::UpdateWifiDirectMediumForStartOrConnect(
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
if (local_medium == &medium) {
|
||||
NEARBY_LOGS(INFO) << "Found WifiDirect medium=" << &medium;
|
||||
LOG(INFO) << "Found WifiDirect medium=" << &medium;
|
||||
info.is_active = enabled;
|
||||
info.is_go = is_go;
|
||||
if (enabled) {
|
||||
@@ -1222,7 +1187,7 @@ void MediumEnvironment::UnregisterWifiDirectMedium(
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
MutexLock lock(&mutex_);
|
||||
wifi_direct_mediums_.extract(&medium);
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiDirect medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered WifiDirect medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1232,7 +1197,7 @@ void MediumEnvironment::RegisterWifiHotspotMedium(
|
||||
RunOnMediumEnvironmentThread([this, &medium]() {
|
||||
MutexLock lock(&mutex_);
|
||||
wifi_hotspot_mediums_.insert({&medium, WifiHotspotMediumContext{}});
|
||||
NEARBY_LOGS(INFO) << "Registered: WifiHotspot medium:" << &medium;
|
||||
LOG(INFO) << "Registered: WifiHotspot medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1246,7 +1211,7 @@ api::WifiHotspotMedium* MediumEnvironment::GetWifiHotspotMedium(
|
||||
if ((info.hotspot_credentials->GetSSID() == ssid) ||
|
||||
(!ip_address.empty() &&
|
||||
(info.hotspot_credentials->GetIPAddress() == ip_address))) {
|
||||
NEARBY_LOGS(INFO) << "Found Remote WifiHotspot medium=" << medium_found;
|
||||
LOG(INFO) << "Found Remote WifiHotspot medium=" << medium_found;
|
||||
return medium_found;
|
||||
}
|
||||
}
|
||||
@@ -1270,13 +1235,13 @@ void MediumEnvironment::UpdateWifiHotspotMediumForStartOrConnect(
|
||||
: (enabled ? "Connected" : "Disconneced"));
|
||||
|
||||
if (hotspot_credentials) {
|
||||
NEARBY_LOGS(INFO) << "Update WifiHotspot medium for Hotspot: this="
|
||||
<< this << "; medium=" << &medium << role_status
|
||||
<< "; ssid=" << hotspot_credentials->GetSSID()
|
||||
<< "; password=" << hotspot_credentials->GetPassword();
|
||||
LOG(INFO) << "Update WifiHotspot medium for Hotspot: this=" << this
|
||||
<< "; medium=" << &medium << role_status
|
||||
<< "; ssid=" << hotspot_credentials->GetSSID()
|
||||
<< "; password=" << hotspot_credentials->GetPassword();
|
||||
} else {
|
||||
NEARBY_LOGS(INFO) << "Reset WifiHotspot medium for Hotspot: this=" << this
|
||||
<< "; medium=" << &medium << role_status;
|
||||
LOG(INFO) << "Reset WifiHotspot medium for Hotspot: this=" << this
|
||||
<< "; medium=" << &medium << role_status;
|
||||
}
|
||||
|
||||
MutexLock lock(&mutex_);
|
||||
@@ -1284,7 +1249,7 @@ void MediumEnvironment::UpdateWifiHotspotMediumForStartOrConnect(
|
||||
auto& local_medium = medium_info.first;
|
||||
auto& info = medium_info.second;
|
||||
if (local_medium == &medium) {
|
||||
NEARBY_LOGS(INFO) << "Found WifiHotspot medium=" << &medium;
|
||||
LOG(INFO) << "Found WifiHotspot medium=" << &medium;
|
||||
info.is_active = enabled;
|
||||
info.is_ap = is_ap;
|
||||
if (enabled) {
|
||||
@@ -1307,7 +1272,7 @@ void MediumEnvironment::UnregisterWifiHotspotMedium(
|
||||
MutexLock lock(&mutex_);
|
||||
auto item = wifi_hotspot_mediums_.extract(&medium);
|
||||
if (item.empty()) return;
|
||||
NEARBY_LOGS(INFO) << "Unregistered WifiHotspot medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered WifiHotspot medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1330,8 +1295,8 @@ void MediumEnvironment::RegisterGattServer(
|
||||
RunOnMediumEnvironmentThread([this, &medium, peripheral, gatt_server]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(WARNING) << "Register GattServer failed. There is no medium"
|
||||
" registered.";
|
||||
LOG(WARNING) << "Register GattServer failed. There is no medium"
|
||||
" registered.";
|
||||
return;
|
||||
}
|
||||
auto& context = it->second;
|
||||
@@ -1339,8 +1304,8 @@ void MediumEnvironment::RegisterGattServer(
|
||||
context.gatt_server =
|
||||
std::make_unique<Borrowable<api::ble_v2::GattServer*>>(gatt_server);
|
||||
context.ble_peripheral = peripheral;
|
||||
NEARBY_LOGS(INFO) << "Registered: GattServer for "
|
||||
<< peripheral->GetAddress() << " on medium:" << &medium;
|
||||
LOG(INFO) << "Registered: GattServer for " << peripheral->GetAddress()
|
||||
<< " on medium:" << &medium;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1350,16 +1315,16 @@ void MediumEnvironment::UnregisterGattServer(api::ble_v2::BleMedium& medium) {
|
||||
RunOnMediumEnvironmentThread([&]() {
|
||||
auto it = ble_v2_mediums_.find(&medium);
|
||||
if (it == ble_v2_mediums_.end()) {
|
||||
NEARBY_LOGS(INFO) << "Unregister GattServer failed. There is no "
|
||||
"medium registered on medium:"
|
||||
<< &medium;
|
||||
LOG(INFO) << "Unregister GattServer failed. There is no "
|
||||
"medium registered on medium:"
|
||||
<< &medium;
|
||||
latch.CountDown();
|
||||
return;
|
||||
}
|
||||
auto& context = it->second;
|
||||
NEARBY_LOGS(INFO) << "Unregistered GattServer for "
|
||||
<< context.ble_peripheral->GetAddress()
|
||||
<< " on medium:" << &medium;
|
||||
LOG(INFO) << "Unregistered GattServer for "
|
||||
<< context.ble_peripheral->GetAddress()
|
||||
<< " on medium:" << &medium;
|
||||
context.gatt_server = nullptr;
|
||||
context.ble_peripheral = nullptr;
|
||||
latch.CountDown();
|
||||
@@ -1368,7 +1333,7 @@ void MediumEnvironment::UnregisterGattServer(api::ble_v2::BleMedium& medium) {
|
||||
}
|
||||
|
||||
Borrowable<api::ble_v2::GattServer*> MediumEnvironment::GetGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral) {
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id) {
|
||||
Borrowable<api::ble_v2::GattServer*> result;
|
||||
bool found_server = false;
|
||||
CountDownLatch latch(1);
|
||||
@@ -1378,7 +1343,7 @@ Borrowable<api::ble_v2::GattServer*> MediumEnvironment::GetGattServer(
|
||||
const api::ble_v2::BlePeripheral* ble_peripheral =
|
||||
remote_context.ble_peripheral;
|
||||
if (remote_context.gatt_server != nullptr && ble_peripheral != nullptr &&
|
||||
(ble_peripheral->GetAddress() == peripheral.GetAddress())) {
|
||||
(ble_peripheral->GetUniqueId() == peripheral_id)) {
|
||||
if (remote_context.gatt_server == nullptr) {
|
||||
break;
|
||||
}
|
||||
@@ -1390,8 +1355,7 @@ Borrowable<api::ble_v2::GattServer*> MediumEnvironment::GetGattServer(
|
||||
});
|
||||
latch.Await();
|
||||
if (!found_server) {
|
||||
NEARBY_LOGS(INFO) << "GetGattServer failed. No GATT server for "
|
||||
<< peripheral.GetAddress();
|
||||
LOG(INFO) << "GetGattServer failed. No GATT server for " << peripheral_id;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -388,7 +388,7 @@ class MediumEnvironment {
|
||||
void UnregisterGattServer(api::ble_v2::BleMedium& medium);
|
||||
|
||||
Borrowable<api::ble_v2::GattServer*> GetGattServer(
|
||||
api::ble_v2::BlePeripheral& peripheral);
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id);
|
||||
|
||||
// Configures the BluetoothPairingContext for remote BluetoothDevice.
|
||||
void ConfigBluetoothPairingContext(api::BluetoothDevice* device,
|
||||
|
||||
Reference in New Issue
Block a user