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
@@ -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_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user