mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Remove g3 BlePeripheral subclass.
PiperOrigin-RevId: 754087426
This commit is contained in:
committed by
Copybara-Service
parent
866b3fa9e1
commit
bda5444fcd
@@ -110,11 +110,19 @@ class BlePeripheral {
|
||||
// the BLE address is rotated.
|
||||
virtual UniqueId GetUniqueId() const { return unique_id_; };
|
||||
|
||||
// Sets platform specific data that can be retrieved by `GetPlatformData()`.
|
||||
void SetPlatformData(void* platform_data) {
|
||||
platform_data_ = platform_data;
|
||||
}
|
||||
|
||||
void* GetPlatformData() const { return platform_data_; }
|
||||
|
||||
bool IsSet() const { return unique_id_ != 0 || address_.IsSet(); }
|
||||
|
||||
private:
|
||||
UniqueId unique_id_ = 0;
|
||||
MacAddress address_;
|
||||
void* platform_data_ = nullptr;
|
||||
};
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic
|
||||
|
||||
@@ -90,6 +90,7 @@ cc_library(
|
||||
":types",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:mac_address",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:types",
|
||||
"//internal/platform:uuid",
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/prng.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
@@ -68,24 +69,13 @@ std::string TxPowerLevelToName(TxPowerLevel power_mode) {
|
||||
|
||||
} // namespace
|
||||
|
||||
BleV2Peripheral::BleV2Peripheral(BluetoothAdapter* adapter)
|
||||
: adapter_(*adapter) {}
|
||||
|
||||
std::string BleV2Peripheral::GetAddress() const {
|
||||
return adapter_.GetMacAddress();
|
||||
}
|
||||
|
||||
api::ble_v2::BlePeripheral::UniqueId BleV2Peripheral::GetUniqueId() const {
|
||||
return adapter_.GetUniqueId();
|
||||
}
|
||||
|
||||
BleV2Peripheral* BleV2Socket::GetRemotePeripheral() {
|
||||
api::ble_v2::BlePeripheral* BleV2Socket::GetRemotePeripheral() {
|
||||
BleV2Socket* remote_socket = GetRemoteSocket();
|
||||
if (remote_socket == nullptr || remote_socket->adapter_ == nullptr ||
|
||||
remote_socket->adapter_->GetBleV2Medium() == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return &(static_cast<BleV2Medium*>(remote_socket->adapter_->GetBleV2Medium())
|
||||
return &(dynamic_cast<BleV2Medium*>(remote_socket->adapter_->GetBleV2Medium())
|
||||
->GetPeripheral());
|
||||
}
|
||||
|
||||
@@ -158,7 +148,8 @@ Exception BleV2ServerSocket::DoClose() {
|
||||
}
|
||||
|
||||
BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter)
|
||||
: adapter_(static_cast<BluetoothAdapter*>(&adapter)) {
|
||||
: adapter_(static_cast<BluetoothAdapter*>(&adapter)),
|
||||
peripheral_(adapter_->GetUniqueId(), adapter_->mac_address()) {
|
||||
adapter_->SetBleV2Medium(this);
|
||||
is_extended_advertisements_available_ =
|
||||
MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable();
|
||||
@@ -369,14 +360,21 @@ bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address,
|
||||
return true;
|
||||
}
|
||||
}
|
||||
BleV2Medium* remote_medium = static_cast<BleV2Medium*>(
|
||||
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
|
||||
MediumEnvironment::Instance().FindBleV2Medium(mac_address));
|
||||
if (remote_medium == nullptr) {
|
||||
return false;
|
||||
}
|
||||
auto id = remote_medium->GetPeripheral().GetUniqueId();
|
||||
api::ble_v2::BlePeripheral::UniqueId id =
|
||||
remote_medium->GetPeripheral().GetUniqueId();
|
||||
BluetoothAdapter& adapter = remote_medium->GetAdapter();
|
||||
MacAddress address;
|
||||
if (!MacAddress::FromString(adapter.GetMacAddress(), address)) {
|
||||
return false;
|
||||
}
|
||||
remote_peripherals_[id] =
|
||||
std::make_unique<BleV2Peripheral>(&remote_medium->GetAdapter());
|
||||
std::make_unique<api::ble_v2::BlePeripheral>(id, address);
|
||||
remote_peripherals_[id]->SetPlatformData(&adapter);
|
||||
callback(*remote_peripherals_[id]);
|
||||
return true;
|
||||
}
|
||||
@@ -390,14 +388,22 @@ bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
return true;
|
||||
}
|
||||
|
||||
BleV2Medium* remote_medium = static_cast<BleV2Medium*>(
|
||||
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
|
||||
MediumEnvironment::Instance().FindBleV2Medium(id));
|
||||
if (remote_medium == nullptr) {
|
||||
NEARBY_LOGS(INFO) << "Peripheral not found, id= " << id;
|
||||
return false;
|
||||
}
|
||||
BluetoothAdapter& adapter = remote_medium->GetAdapter();
|
||||
MacAddress address;
|
||||
if (!MacAddress::FromString(adapter.GetMacAddress(), address)) {
|
||||
LOG(ERROR) << "Adapter has invalid mac address: "
|
||||
<< adapter.GetMacAddress();
|
||||
return false;
|
||||
}
|
||||
remote_peripherals_[id] =
|
||||
std::make_unique<BleV2Peripheral>(&remote_medium->GetAdapter());
|
||||
std::make_unique<api::ble_v2::BlePeripheral>(id, address);
|
||||
remote_peripherals_[id]->SetPlatformData(&adapter);
|
||||
callback(*remote_peripherals_[id]);
|
||||
return true;
|
||||
}
|
||||
@@ -405,8 +411,12 @@ 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)),
|
||||
ble_peripheral_(&medium.GetAdapter()) {
|
||||
callback_(std::move(callback)) {
|
||||
BluetoothAdapter& adapter = medium.GetAdapter();
|
||||
MacAddress address;
|
||||
MacAddress::FromString(adapter.GetMacAddress(), address);
|
||||
ble_peripheral_ = api::ble_v2::BlePeripheral(adapter.GetUniqueId(), address);
|
||||
ble_peripheral_.SetPlatformData(&adapter);
|
||||
MediumEnvironment::Instance().RegisterGattServer(medium_, &ble_peripheral_,
|
||||
lender_.GetBorrowable());
|
||||
}
|
||||
@@ -480,7 +490,7 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
|
||||
}
|
||||
|
||||
absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -509,7 +519,7 @@ absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
|
||||
}
|
||||
|
||||
absl::Status BleV2Medium::GattServer::WriteCharacteristic(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data) {
|
||||
if (HasCharacteristic(characteristic)) {
|
||||
@@ -528,7 +538,7 @@ absl::Status BleV2Medium::GattServer::WriteCharacteristic(
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattServer::AddCharacteristicSubscription(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
absl::AnyInvocable<void(absl::string_view value)> callback) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
@@ -542,7 +552,7 @@ bool BleV2Medium::GattServer::AddCharacteristicSubscription(
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattServer::RemoveCharacteristicSubscription(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
const auto it = characteristics_.find(characteristic);
|
||||
@@ -585,7 +595,7 @@ BleV2Medium::GattClient::GattClient(
|
||||
api::ble_v2::BlePeripheral& peripheral,
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server,
|
||||
api::ble_v2::ClientGattConnectionCallback callback)
|
||||
: peripheral_(static_cast<BleV2Peripheral&>(peripheral)),
|
||||
: peripheral_(peripheral),
|
||||
gatt_server_(gatt_server),
|
||||
callback_(std::move(callback)) {
|
||||
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
|
||||
@@ -794,10 +804,10 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
|
||||
<< ", peripheral=" << &GetPeripheral()
|
||||
<< ", service_id=" << service_id;
|
||||
// First, find an instance of remote medium, that exposed this peripheral.
|
||||
auto& remote_adapter =
|
||||
static_cast<BleV2Peripheral&>(remote_peripheral).GetAdapter();
|
||||
BluetoothAdapter* remote_adapter =
|
||||
static_cast<BluetoothAdapter*>(remote_peripheral.GetPlatformData());
|
||||
auto* remote_medium =
|
||||
static_cast<BleV2Medium*>(remote_adapter.GetBleV2Medium());
|
||||
dynamic_cast<BleV2Medium*>(remote_adapter->GetBleV2Medium());
|
||||
if (!remote_medium) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -46,19 +46,6 @@
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
// BlePeripheral implementation.
|
||||
class BleV2Peripheral : public api::ble_v2::BlePeripheral {
|
||||
public:
|
||||
explicit BleV2Peripheral(BluetoothAdapter* adapter);
|
||||
std::string GetAddress() const override;
|
||||
api::ble_v2::BlePeripheral::UniqueId GetUniqueId() const override;
|
||||
|
||||
BluetoothAdapter& GetAdapter() { return adapter_; }
|
||||
|
||||
private:
|
||||
BluetoothAdapter& adapter_;
|
||||
};
|
||||
|
||||
class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase {
|
||||
public:
|
||||
explicit BleV2Socket(BluetoothAdapter* adapter) : adapter_(adapter) {}
|
||||
@@ -84,7 +71,8 @@ class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase {
|
||||
|
||||
// Returns valid BlePeripheral pointer if there is a connection, and
|
||||
// nullptr otherwise.
|
||||
BleV2Peripheral* GetRemotePeripheral() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
api::ble_v2::BlePeripheral* GetRemotePeripheral() override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
|
||||
@@ -204,7 +192,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
|
||||
BluetoothAdapter& GetAdapter() { return *adapter_; }
|
||||
|
||||
BleV2Peripheral& GetPeripheral() { return peripheral_; }
|
||||
api::ble_v2::BlePeripheral& GetPeripheral() { return peripheral_; }
|
||||
|
||||
bool GetRemotePeripheral(const std::string& mac_address,
|
||||
GetRemotePeripheralCallback callback) override;
|
||||
@@ -244,20 +232,20 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
const std::vector<Uuid>& characteristic_uuids);
|
||||
|
||||
absl::StatusOr<ByteArray> ReadCharacteristic(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset);
|
||||
|
||||
absl::Status WriteCharacteristic(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic, int offset,
|
||||
absl::string_view data);
|
||||
|
||||
bool AddCharacteristicSubscription(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic,
|
||||
absl::AnyInvocable<void(absl::string_view value)>);
|
||||
bool RemoveCharacteristicSubscription(
|
||||
const BleV2Peripheral& remote_device,
|
||||
const api::ble_v2::BlePeripheral& remote_device,
|
||||
const api::ble_v2::GattCharacteristic& characteristic);
|
||||
|
||||
bool HasCharacteristic(
|
||||
@@ -267,14 +255,14 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
void Disconnect(GattClient* client);
|
||||
|
||||
private:
|
||||
using SubscriberKey =
|
||||
std::pair<const BleV2Peripheral*, api::ble_v2::GattCharacteristic>;
|
||||
using SubscriberKey = std::pair<const api::ble_v2::BlePeripheral*,
|
||||
api::ble_v2::GattCharacteristic>;
|
||||
using SubscriberCallback =
|
||||
absl::AnyInvocable<void(absl::string_view value)>;
|
||||
absl::Mutex mutex_;
|
||||
BleV2Medium& medium_;
|
||||
api::ble_v2::ServerGattConnectionCallback callback_;
|
||||
BleV2Peripheral ble_peripheral_;
|
||||
api::ble_v2::BlePeripheral ble_peripheral_;
|
||||
absl::flat_hash_map<api::ble_v2::GattCharacteristic,
|
||||
absl::StatusOr<ByteArray>>
|
||||
characteristics_ ABSL_GUARDED_BY(mutex_);
|
||||
@@ -323,7 +311,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;
|
||||
BleV2Peripheral& peripheral_;
|
||||
api::ble_v2::BlePeripheral& peripheral_;
|
||||
Borrowable<api::ble_v2::GattServer*> gatt_server_;
|
||||
api::ble_v2::ClientGattConnectionCallback callback_;
|
||||
};
|
||||
@@ -331,9 +319,9 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
bool IsStopped(Borrowable<api::ble_v2::GattServer*> server);
|
||||
absl::Mutex mutex_;
|
||||
BluetoothAdapter* adapter_; // Our device adapter; read-only.
|
||||
BleV2Peripheral peripheral_{adapter_};
|
||||
api::ble_v2::BlePeripheral peripheral_;
|
||||
absl::flat_hash_map<api::ble_v2::BlePeripheral::UniqueId,
|
||||
std::unique_ptr<BleV2Peripheral>>
|
||||
std::unique_ptr<api::ble_v2::BlePeripheral>>
|
||||
remote_peripherals_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::flat_hash_map<std::string, BleV2ServerSocket*> server_sockets_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef PLATFORM_IMPL_G3_BLUETOOTH_ADAPTER_H_
|
||||
#define PLATFORM_IMPL_G3_BLUETOOTH_ADAPTER_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -27,6 +28,7 @@
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/g3/single_thread_executor.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
@@ -111,7 +113,8 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Returns BT MAC address assigned to this adapter.
|
||||
std::string GetMacAddress() const override { return mac_address_; }
|
||||
std::string GetMacAddress() const override { return mac_address_.ToString(); }
|
||||
MacAddress mac_address() const { return mac_address_; }
|
||||
|
||||
BluetoothDevice& GetDevice() { return device_; }
|
||||
|
||||
@@ -129,7 +132,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
api::ble_v2::BleMedium* GetBleV2Medium() { return ble_v2_medium_; }
|
||||
|
||||
void SetMacAddress(absl::string_view mac_address) {
|
||||
mac_address_ = std::string(mac_address);
|
||||
MacAddress::FromString(mac_address, mac_address_);
|
||||
}
|
||||
|
||||
std::uint64_t GetUniqueId() { return unique_id_; }
|
||||
@@ -141,7 +144,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
api::BluetoothClassicMedium* bluetooth_classic_medium_ = nullptr;
|
||||
api::BleMedium* ble_medium_ = nullptr;
|
||||
api::ble_v2::BleMedium* ble_v2_medium_ = nullptr;
|
||||
std::string mac_address_;
|
||||
MacAddress mac_address_;
|
||||
ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone;
|
||||
std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device";
|
||||
bool enabled_ ABSL_GUARDED_BY(mutex_) = true;
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "internal/platform/borrowable.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/count_down_latch.h"
|
||||
|
||||
Reference in New Issue
Block a user