Internal refactor

PiperOrigin-RevId: 815749899
This commit is contained in:
Guogang Li
2025-10-06 09:03:08 -07:00
committed by Copybara-Service
parent a065584164
commit cce45b745e
114 changed files with 2096 additions and 2141 deletions
+5 -4
View File
@@ -1,6 +1,3 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +11,10 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test")
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
licenses(["notice"])
cc_library(
@@ -152,7 +153,7 @@ cc_library(
name = "comm",
hdrs = [
"awdl.h",
"ble_v2.h",
"ble.h",
"bluetooth_adapter.h",
"bluetooth_classic.h",
"credential_callbacks.h",
@@ -19,12 +19,12 @@
#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTCharacteristic.h"
#import "internal/platform/implementation/apple/ble_utils.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
using GattCharacteristic = ::nearby::api::ble_v2::GattCharacteristic;
using Property = ::nearby::api::ble_v2::GattCharacteristic::Property;
using Permission = ::nearby::api::ble_v2::GattCharacteristic::Permission;
using WriteType = ::nearby::api::ble_v2::GattClient::WriteType;
using GattCharacteristic = ::nearby::api::ble::GattCharacteristic;
using Property = ::nearby::api::ble::GattCharacteristic::Property;
using Permission = ::nearby::api::ble::GattCharacteristic::Permission;
using WriteType = ::nearby::api::ble::GattClient::WriteType;
using Uuid = ::nearby::Uuid;
using ByteArray = ::nearby::ByteArray;
@@ -308,9 +308,9 @@ void GNCEnsureFileAtPath(std::string path) {
XCTAssertEqual(bluetooth_classic_medium.get(), nullptr);
}
- (void)testCreateBleV2Medium {
- (void)testCreateBleMedium {
auto bluetooth_adapter = nearby::api::ImplementationPlatform::CreateBluetoothAdapter();
auto ble_medium = nearby::api::ImplementationPlatform::CreateBleV2Medium(*bluetooth_adapter);
auto ble_medium = nearby::api::ImplementationPlatform::CreateBleMedium(*bluetooth_adapter);
XCTAssertNotEqual(ble_medium.get(), nullptr);
}
@@ -95,7 +95,7 @@
}
- (void)testDefaultBlePeripheral {
nearby::api::ble_v2::BlePeripheral &defaultPeripheral =
nearby::api::ble::BlePeripheral &defaultPeripheral =
nearby::apple::BlePeripheral::DefaultBlePeripheral();
// 0xffffffffffffffff is the default value from
// third_party/nearby/internal/platform/implementation/apple/ble_peripheral.mm when create static
@@ -24,7 +24,7 @@
#include <string>
#include <vector>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/uuid.h"
@class GNCBLEGATTClient;
@@ -32,7 +32,7 @@
namespace nearby {
namespace apple {
class GattClient : public api::ble_v2::GattClient {
class GattClient : public api::ble::GattClient {
public:
explicit GattClient(GNCBLEGATTClient *gatt_client_);
~GattClient() override = default;
@@ -51,21 +51,20 @@ class GattClient : public api::ble_v2::GattClient {
// services and characteristics first.
//
// On success, returns the characteristic. On error, returns nullptr.
std::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
std::optional<api::ble::GattCharacteristic> GetCharacteristic(
const Uuid &service_uuid, const Uuid &characteristic_uuid) override;
// Reads the requested characteristic from the associated remote device.
//
// On success, returns the characteristic's value. On error, returns nullptr.
std::optional<std::string> ReadCharacteristic(
const api::ble_v2::GattCharacteristic &characteristic) override;
const api::ble::GattCharacteristic &characteristic) override;
// Writes a given characteristic and its values to the associated remote device.
//
// Returns whether or not the write was successful.
bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
absl::string_view value,
api::ble_v2::GattClient::WriteType type) override;
bool WriteCharacteristic(const api::ble::GattCharacteristic &characteristic,
absl::string_view value, api::ble::GattClient::WriteType type) override;
// Enable or disable notifications/indications for a given characteristic.
//
@@ -74,7 +73,7 @@ class GattClient : public api::ble_v2::GattClient {
//
// Returns whether or not the subscription was successful.
bool SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic &characteristic, bool enable,
const api::ble::GattCharacteristic &characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb) override;
// Disconnects an established connection, or cancels a connection attempt currently in progress.
@@ -21,7 +21,7 @@
#include <utility>
#include <vector>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h"
#import "internal/platform/implementation/apple/Log/GNCLogger.h"
@@ -60,7 +60,7 @@ bool GattClient::DiscoverServiceAndCharacteristics(const Uuid &service_uuid,
return blockError == nil;
}
std::optional<api::ble_v2::GattCharacteristic> GattClient::GetCharacteristic(
std::optional<api::ble::GattCharacteristic> GattClient::GetCharacteristic(
const Uuid &service_uuid, const Uuid &characteristic_uuid) {
CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid);
CBUUID *characteristicUUID = CBUUID128FromCPP(characteristic_uuid);
@@ -90,7 +90,7 @@ std::optional<api::ble_v2::GattCharacteristic> GattClient::GetCharacteristic(
}
std::optional<std::string> GattClient::ReadCharacteristic(
const api::ble_v2::GattCharacteristic &characteristic) {
const api::ble::GattCharacteristic &characteristic) {
NSCondition *condition = [[NSCondition alloc] init];
[condition lock];
__block NSData *blockValue = nil;
@@ -115,15 +115,15 @@ std::optional<std::string> GattClient::ReadCharacteristic(
}
// TODO(b/290385712): Implement.
bool GattClient::WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
bool GattClient::WriteCharacteristic(const api::ble::GattCharacteristic &characteristic,
absl::string_view value,
api::ble_v2::GattClient::WriteType type) {
api::ble::GattClient::WriteType type) {
return false;
}
// TODO(b/290385712): Implement.
bool GattClient::SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic &characteristic, bool enable,
const api::ble::GattCharacteristic &characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb) {
return false;
}
@@ -21,7 +21,7 @@
#import <Foundation/Foundation.h>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/uuid.h"
#import "internal/platform/implementation/apple/ble_peripheral.h"
@@ -31,7 +31,7 @@
namespace nearby {
namespace apple {
class GattServer : public api::ble_v2::GattServer {
class GattServer : public api::ble::GattServer {
public:
explicit GattServer(GNCBLEGATTServer *gatt_server_);
~GattServer() override = default;
@@ -43,21 +43,21 @@ class GattServer : public api::ble_v2::GattServer {
// services with the same UUID.
//
// Returns no value upon error.
std::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
std::optional<api::ble::GattCharacteristic> CreateCharacteristic(
const Uuid &service_uuid, const Uuid &characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) override;
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) override;
// Updates a local characteristic with the provided value.
//
// Returns whether or not the update was successful.
bool UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
bool UpdateCharacteristic(const api::ble::GattCharacteristic &characteristic,
const nearby::ByteArray &value) override;
// Send a notification or indication that a local characteristic has been updated.
//
// Returns an absl::Status indicating success or what went wrong.
absl::Status NotifyCharacteristicChanged(const api::ble_v2::GattCharacteristic &characteristic,
absl::Status NotifyCharacteristicChanged(const api::ble::GattCharacteristic &characteristic,
bool confirm, const ByteArray &new_value) override;
// Stops a GATT server.
@@ -17,7 +17,7 @@
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/Log/GNCLogger.h"
#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTServer.h"
@@ -29,10 +29,10 @@ namespace apple {
GattServer::GattServer(GNCBLEGATTServer *gatt_server) : gatt_server_(gatt_server) {}
std::optional<api::ble_v2::GattCharacteristic> GattServer::CreateCharacteristic(
std::optional<api::ble::GattCharacteristic> GattServer::CreateCharacteristic(
const Uuid &service_uuid, const Uuid &characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) {
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) {
CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid);
CBUUID *characteristicUUID = CBUUID128FromCPP(characteristic_uuid);
CBAttributePermissions permissions = CBAttributePermissionsFromCPP(permission);
@@ -63,7 +63,7 @@ std::optional<api::ble_v2::GattCharacteristic> GattServer::CreateCharacteristic(
return CPPGATTCharacteristicFromObjC(blockCharacteristic);
}
bool GattServer::UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
bool GattServer::UpdateCharacteristic(const api::ble::GattCharacteristic &characteristic,
const nearby::ByteArray &value) {
NSCondition *condition = [[NSCondition alloc] init];
[condition lock];
@@ -86,8 +86,7 @@ bool GattServer::UpdateCharacteristic(const api::ble_v2::GattCharacteristic &cha
// TODO(b/290385712): Implement.
absl::Status GattServer::NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic &characteristic, bool confirm,
const ByteArray &new_value) {
const api::ble::GattCharacteristic &characteristic, bool confirm, const ByteArray &new_value) {
return absl::UnimplementedError("");
}
@@ -16,14 +16,14 @@
#include "absl/functional/any_invocable.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#import "internal/platform/implementation/apple/ble_l2cap_socket.h"
#include "internal/platform/implementation/ble.h"
namespace nearby {
namespace apple {
// A BLE L2CAP server socket for listening incoming L2CAP socket.
class BleL2capServerSocket : public api::ble_v2::BleL2capServerSocket {
class BleL2capServerSocket : public api::ble::BleL2capServerSocket {
public:
BleL2capServerSocket() = default;
~BleL2capServerSocket() override;
@@ -43,7 +43,7 @@ class BleL2capServerSocket : public api::ble_v2::BleL2capServerSocket {
// Returns nullptr on error.
// Once error is reported, it is permanent, and L2CAP ServerSocket has to be
// closed.
std::unique_ptr<api::ble_v2::BleL2capSocket> Accept() override
std::unique_ptr<api::ble::BleL2capSocket> Accept() override
ABSL_LOCKS_EXCLUDED(mutex_);
// Closes the L2CAP server socket.
@@ -35,7 +35,7 @@ int BleL2capServerSocket::GetPSM() const { return psm_; }
void BleL2capServerSocket::SetPSM(int psm) { psm_ = psm; }
// TODO: b/399815436 - Refactor Accept() and AddPendingSocket() for better readability.
std::unique_ptr<api::ble_v2::BleL2capSocket> BleL2capServerSocket::Accept() {
std::unique_ptr<api::ble::BleL2capSocket> BleL2capServerSocket::Accept() {
absl::MutexLock lock(&mutex_);
while (!closed_ && pending_sockets_.empty()) {
cond_.Wait(&mutex_);
@@ -14,7 +14,7 @@
#include <memory>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/ble_peripheral.h"
@@ -27,7 +27,7 @@ namespace apple {
/** A readable stream of bytes. */
class BleL2capInputStream : public InputStream {
public:
explicit BleL2capInputStream(GNCBLEL2CAPConnection* connection);
explicit BleL2capInputStream(GNCBLEL2CAPConnection *connection);
~BleL2capInputStream() override;
// Reads at most `size` bytes from the input stream.
@@ -42,7 +42,7 @@ class BleL2capInputStream : public InputStream {
private:
GNCMConnectionHandlers *connectionHandlers_;
GNCBLEL2CAPConnection* connection_;
GNCBLEL2CAPConnection *connection_;
NSMutableArray<NSData *> *newDataPackets_;
NSMutableData *accumulatedData_;
NSCondition *condition_;
@@ -51,7 +51,7 @@ class BleL2capInputStream : public InputStream {
/** A writable stream of bytes. */
class BleL2capOutputStream : public OutputStream {
public:
explicit BleL2capOutputStream(GNCBLEL2CAPConnection* connection)
explicit BleL2capOutputStream(GNCBLEL2CAPConnection *connection)
: connection_(connection), condition_([[NSCondition alloc] init]) {}
~BleL2capOutputStream() override;
@@ -71,21 +71,21 @@ class BleL2capOutputStream : public OutputStream {
Exception Close() override;
private:
GNCBLEL2CAPConnection* connection_;
GNCBLEL2CAPConnection *connection_;
NSCondition *condition_;
};
/**
* Concrete BleL2capSocket implementation.
*/
class BleL2capSocket : public api::ble_v2::BleL2capSocket {
class BleL2capSocket : public api::ble::BleL2capSocket {
public:
explicit BleL2capSocket(GNCBLEL2CAPConnection* connection);
explicit BleL2capSocket(GNCBLEL2CAPConnection *connection);
// The peripheral used to create the socket must outlive the socket or undefined behavior will
// occur.
BleL2capSocket(GNCBLEL2CAPConnection *connection,
api::ble_v2::BlePeripheral::UniqueId peripheral_id);
api::ble::BlePeripheral::UniqueId peripheral_id);
~BleL2capSocket() override;
// Returns the InputStream of the BleL2capSocket.
@@ -109,7 +109,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket {
// Returns valid BlePeripheral pointer if there is a connection, and
// nullptr otherwise.
api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; }
api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; }
bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_);
@@ -120,7 +120,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket {
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
std::unique_ptr<BleL2capInputStream> input_stream_;
std::unique_ptr<BleL2capOutputStream> output_stream_;
api::ble_v2::BlePeripheral::UniqueId peripheral_id_;
api::ble::BlePeripheral::UniqueId peripheral_id_;
};
} // namespace apple
@@ -17,7 +17,7 @@
#import "internal/platform/implementation/apple/Log/GNCLogger.h"
#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEL2CAPConnection.h"
#import "internal/platform/implementation/apple/utils.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
namespace nearby {
namespace apple {
@@ -166,7 +166,7 @@ BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection)
: BleL2capSocket(connection, BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {}
BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection,
api::ble_v2::BlePeripheral::UniqueId peripheral_id)
api::ble::BlePeripheral::UniqueId peripheral_id)
: input_stream_(std::make_unique<BleL2capInputStream>(connection)),
output_stream_(std::make_unique<BleL2capOutputStream>(connection)),
peripheral_id_(peripheral_id) {}
@@ -30,7 +30,7 @@
#import "internal/platform/implementation/apple/ble_server_socket.h"
#import "internal/platform/implementation/apple/bluetooth_adapter_v2.h"
#import "internal/platform/implementation/apple/single_thread_executor.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
@@ -44,7 +44,7 @@ namespace apple {
// The main BLE medium used inside of Nearby. This serves as the entry point for all BLE and GATT
// related operations.
class BleMedium : public api::ble_v2::BleMedium {
class BleMedium : public api::ble::BleMedium {
public:
BleMedium();
~BleMedium() override;
@@ -55,16 +55,16 @@ class BleMedium : public api::ble_v2::BleMedium {
// the stop_advertising callback in AdvertisingSession.
//
// Advertising must be stopped before attempting to start advertising again.
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession> StartAdvertising(
const api::ble_v2::BleAdvertisementData &advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters,
api::ble_v2::BleMedium::AdvertisingCallback callback) override;
std::unique_ptr<api::ble::BleMedium::AdvertisingSession> StartAdvertising(
const api::ble::BleAdvertisementData &advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters,
api::ble::BleMedium::AdvertisingCallback callback) override;
// Starts BLE advertising and returns whether or not it was successful.
//
// Advertising must be stopped before attempting to start advertising again.
bool StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters) override;
bool StartAdvertising(const api::ble::BleAdvertisementData &advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters) override;
// Stops advertising.
//
@@ -77,20 +77,20 @@ class BleMedium : public api::ble_v2::BleMedium {
// scanning, invoke the stop_scanning callback in ScanningSession.
//
// Scanning must be stopped before attempting to start scanning again.
std::unique_ptr<api::ble_v2::BleMedium::ScanningSession> StartScanning(
const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanningCallback callback) override;
std::unique_ptr<api::ble::BleMedium::ScanningSession> StartScanning(
const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanningCallback callback) override;
// Starts scanning and returns whether or not it was successful.
//
// Scanning must be stopped before attempting to start scanning again.
bool StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanCallback callback) override;
bool StartScanning(const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanCallback callback) override;
// Starts scanning on multiple services and returns whether or not it was successful.
bool StartMultipleServicesScanning(const std::vector<Uuid> &service_uuids,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanCallback callback) override;
api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanCallback callback) override;
// Stops scanning.
//
@@ -110,28 +110,28 @@ class BleMedium : public api::ble_v2::BleMedium {
// TODO(b/290385712): ServerGattConnectionCallback methods are not yet implemented.
//
// Starts a GATT server. Returns a nullptr upon error.
std::unique_ptr<api::ble_v2::GattServer> StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) override;
std::unique_ptr<api::ble::GattServer> StartGattServer(
api::ble::ServerGattConnectionCallback callback) override;
// Connects to a GATT server and negotiates the specified connection parameters. Returns nullptr
// upon error.
//
// 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::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) override;
std::unique_ptr<api::ble::GattClient> ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id, api::ble::TxPowerLevel tx_power_level,
api::ble::ClientGattConnectionCallback callback) override;
// Opens a BLE server socket based on service ID.
//
// On success, returns a new BleServerSocket. On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleServerSocket> OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> OpenServerSocket(
const std::string &service_id) override;
// Opens a L2CAP server socket based on service ID.
//
// On success, returns a new BleL2capServerSocket. On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleL2capServerSocket> OpenL2capServerSocket(
std::unique_ptr<api::ble::BleL2capServerSocket> OpenL2capServerSocket(
const std::string &service_id) override;
// TODO(b/290385712): cancellation_flag support is not yet implemented.
@@ -142,10 +142,10 @@ class BleMedium : public api::ble_v2::BleMedium {
// should not be modified by this method.
//
// On success, returns a new BleSocket. On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
CancellationFlag *cancellation_flag) override;
std::unique_ptr<api::ble::BleSocket> Connect(const std::string &service_id,
api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId peripheral_id,
CancellationFlag *cancellation_flag) override;
// TODO(b/290385712): cancellation_flag support is not yet implemented.
//
@@ -155,9 +155,9 @@ class BleMedium : public api::ble_v2::BleMedium {
// should not be modified by this method.
//
// On success, returns a new BleL2capSocket. On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleL2capSocket> ConnectOverL2cap(
int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
std::unique_ptr<api::ble::BleL2capSocket> ConnectOverL2cap(
int psm, const std::string &service_id, api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId peripheral_id,
CancellationFlag *cancellation_flag) override;
// Returns whether the hardware supports BOTH advertising extensions and extended scans.
@@ -171,7 +171,7 @@ class BleMedium : public api::ble_v2::BleMedium {
//
// Returns std::nullopt if cannot retrieve the BlePeripheral from the native
// BLE peripheral id.
std::optional<api::ble_v2::BlePeripheral::UniqueId> RetrieveBlePeripheralIdFromNativeId(
std::optional<api::ble::BlePeripheral::UniqueId> RetrieveBlePeripheralIdFromNativeId(
const std::string &ble_peripheral_native_id) override;
private:
@@ -181,15 +181,14 @@ class BleMedium : public api::ble_v2::BleMedium {
PeripheralsMap() = default;
~PeripheralsMap() = default;
api::ble_v2::BlePeripheral::UniqueId Add(id<GNCPeripheral> peripheral)
ABSL_LOCKS_EXCLUDED(mutex_);
id<GNCPeripheral> Get(api::ble_v2::BlePeripheral::UniqueId peripheral_id)
api::ble::BlePeripheral::UniqueId Add(id<GNCPeripheral> peripheral) ABSL_LOCKS_EXCLUDED(mutex_);
id<GNCPeripheral> Get(api::ble::BlePeripheral::UniqueId peripheral_id)
ABSL_LOCKS_EXCLUDED(mutex_);
void Clear() ABSL_LOCKS_EXCLUDED(mutex_);
private:
absl::Mutex mutex_;
absl::flat_hash_map<api::ble_v2::BlePeripheral::UniqueId, id<GNCPeripheral>> peripherals_
absl::flat_hash_map<api::ble::BlePeripheral::UniqueId, id<GNCPeripheral>> peripherals_
ABSL_GUARDED_BY(mutex_);
};
@@ -197,9 +196,9 @@ class BleMedium : public api::ble_v2::BleMedium {
NSDictionary<CBUUID *, NSData *> *serviceData);
void ClearAdvertisementPacketsMap();
void CleanUpExpiredAdvertisementPackets(NSDate *now);
bool ShouldReportAdvertisement(NSDate *now, api::ble_v2::BlePeripheral::UniqueId peripheral_id,
bool ShouldReportAdvertisement(NSDate *now, api::ble::BlePeripheral::UniqueId peripheral_id,
NSDictionary<CBUUID *, NSData *> *service_data);
void AddAdvertisementPacketInfo(api::ble_v2::BlePeripheral::UniqueId peripheral_id,
void AddAdvertisementPacketInfo(api::ble::BlePeripheral::UniqueId peripheral_id,
NSDictionary<CBUUID *, NSData *> *service_data);
NSDate *GetLastTimestampToCleanExpiredAdvertisementPackets();
@@ -216,7 +215,7 @@ class BleMedium : public api::ble_v2::BleMedium {
absl::Mutex advertisement_packets_mutex_;
// A map for maintaining the set of advertisement packets that should be tracked.
absl::flat_hash_map<api::ble_v2::BlePeripheral::UniqueId, AdvertisementPacketInfo>
absl::flat_hash_map<api::ble::BlePeripheral::UniqueId, AdvertisementPacketInfo>
advertisement_packets_map_ ABSL_GUARDED_BY(advertisement_packets_mutex_);
// The timestamp of the last time to clean up expired advertisement packets. This is used to
// prevent the map from growing indefinitely.
@@ -228,10 +227,10 @@ class BleMedium : public api::ble_v2::BleMedium {
GNSCentralManager *socketCentralManager_;
// Used for the blocking version of StartAdvertising and only has an advertisement found callback.
api::ble_v2::BleMedium::ScanCallback scan_cb_;
api::ble::BleMedium::ScanCallback scan_cb_;
// Used for the async version of StartAdvertising and has both an advertisement found and result
// callback.
api::ble_v2::BleMedium::ScanningCallback scanning_cb_;
api::ble::BleMedium::ScanningCallback scanning_cb_;
absl::Mutex l2cap_server_socket_mutex_;
BleL2capServerSocket *l2cap_server_socket_ptr_ = nullptr;
@@ -24,7 +24,7 @@
#include "internal/platform/implementation/apple/ble_utils.h"
#include "internal/platform/implementation/apple/utils.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTCharacteristic.h"
@@ -92,14 +92,14 @@ BleMedium::BleMedium() : medium_([[GNCBLEMedium alloc] init]) {
// ble_medium.mm
BleMedium::~BleMedium() { [medium_ stop]; }
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession> BleMedium::StartAdvertising(
const api::ble_v2::BleAdvertisementData &advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters,
api::ble_v2::BleMedium::AdvertisingCallback callback) {
std::unique_ptr<api::ble::BleMedium::AdvertisingSession> BleMedium::StartAdvertising(
const api::ble::BleAdvertisementData &advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters,
api::ble::BleMedium::AdvertisingCallback callback) {
NSMutableDictionary<CBUUID *, NSData *> *serviceData =
ObjCServiceDataFromCPP(advertising_data.service_data);
__block api::ble_v2::BleMedium::AdvertisingCallback blockCallback = std::move(callback);
__block api::ble::BleMedium::AdvertisingCallback blockCallback = std::move(callback);
[socketPeripheralManager_ start];
@@ -115,8 +115,8 @@ std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession> BleMedium::StartAdve
}});
}
bool BleMedium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters) {
bool BleMedium::StartAdvertising(const api::ble::BleAdvertisementData &advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters) {
NSMutableDictionary<CBUUID *, NSData *> *serviceData =
ObjCServiceDataFromCPP(advertising_data.service_data);
@@ -158,7 +158,7 @@ bool BleMedium::StopAdvertising() {
void BleMedium::HandleAdvertisementFound(id<GNCPeripheral> peripheral,
NSDictionary<CBUUID *, NSData *> *serviceData) {
api::ble_v2::BleAdvertisementData data;
api::ble::BleAdvertisementData data;
NSDate *now = [NSDate date];
if ([now timeIntervalSinceDate:GetLastTimestampToCleanExpiredAdvertisementPackets()] >
@@ -166,7 +166,7 @@ void BleMedium::HandleAdvertisementFound(id<GNCPeripheral> peripheral,
ClearAdvertisementPacketsMap();
}
api::ble_v2::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash;
api::ble::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash;
if (!ShouldReportAdvertisement(now, peripheral_id, serviceData)) {
return;
@@ -177,7 +177,7 @@ void BleMedium::HandleAdvertisementFound(id<GNCPeripheral> peripheral,
}
// Add the peripheral to the map if we haven't discovered it yet.
api::ble_v2::BlePeripheral::UniqueId unique_id = peripherals_.Add(peripheral);
api::ble::BlePeripheral::UniqueId unique_id = peripherals_.Add(peripheral);
AddAdvertisementPacketInfo(unique_id, serviceData);
#if DEBUG
@@ -196,9 +196,9 @@ void BleMedium::HandleAdvertisementFound(id<GNCPeripheral> peripheral,
}
}
std::unique_ptr<api::ble_v2::BleMedium::ScanningSession> BleMedium::StartScanning(
const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanningCallback callback) {
std::unique_ptr<api::ble::BleMedium::ScanningSession> BleMedium::StartScanning(
const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanningCallback callback) {
CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid);
scanning_cb_ = std::move(callback);
@@ -230,8 +230,8 @@ std::unique_ptr<api::ble_v2::BleMedium::ScanningSession> BleMedium::StartScannin
}});
}
bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanCallback callback) {
bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanCallback callback) {
CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid);
scan_cb_ = std::move(callback);
@@ -268,8 +268,8 @@ bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLeve
}
bool BleMedium::StartMultipleServicesScanning(const std::vector<Uuid> &service_uuids,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanCallback callback) {
api::ble::TxPowerLevel tx_power_level,
api::ble::BleMedium::ScanCallback callback) {
if (service_uuids.empty()) {
GNCLoggerError(@"No service UUIDs provided");
return false;
@@ -355,8 +355,8 @@ bool BleMedium::ResumeMediumScanning() {
}
// TODO(b/290385712): Add implementation that calls ServerGattConnectionCallback methods.
std::unique_ptr<api::ble_v2::GattServer> BleMedium::StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) {
std::unique_ptr<api::ble::GattServer> BleMedium::StartGattServer(
api::ble::ServerGattConnectionCallback callback) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block GNCBLEGATTServer *blockServer = nil;
__block NSError *blockError = nil;
@@ -381,16 +381,16 @@ std::unique_ptr<api::ble_v2::GattServer> BleMedium::StartGattServer(
return std::make_unique<GattServer>(blockServer);
}
std::unique_ptr<api::ble_v2::GattClient> BleMedium::ConnectToGattServer(
api::ble_v2::BlePeripheral::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) {
std::unique_ptr<api::ble::GattClient> BleMedium::ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id, api::ble::TxPowerLevel tx_power_level,
api::ble::ClientGattConnectionCallback callback) {
id<GNCPeripheral> peripheral = peripherals_.Get(peripheral_id);
if (!peripheral) {
GNCLoggerError(@"[NEARBY] Failed to connect to Gatt server: peripheral is not found.");
return nullptr;
}
api::ble_v2::ClientGattConnectionCallback thread_callback = {
api::ble::ClientGattConnectionCallback thread_callback = {
.on_characteristic_changed_cb =
[this,
call_on_characteristic_changed_cb = std::move(callback.on_characteristic_changed_cb)](
@@ -409,7 +409,7 @@ std::unique_ptr<api::ble_v2::GattClient> BleMedium::ConnectToGattServer(
});
}};
__block api::ble_v2::ClientGattConnectionCallback blockCallback = std::move(thread_callback);
__block api::ble::ClientGattConnectionCallback blockCallback = std::move(thread_callback);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block GNCBLEGATTClient *blockClient = nil;
@@ -440,7 +440,7 @@ std::unique_ptr<api::ble_v2::GattClient> BleMedium::ConnectToGattServer(
}
// TODO(b/293336684): Old Weave code that need to be deleted once shared Weave is complete.
std::unique_ptr<api::ble_v2::BleServerSocket> BleMedium::OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> BleMedium::OpenServerSocket(
const std::string &service_id) {
auto server_socket = std::make_unique<BleServerSocket>();
__block auto server_socket_ptr = server_socket.get();
@@ -512,7 +512,7 @@ std::unique_ptr<api::ble_v2::BleServerSocket> BleMedium::OpenServerSocket(
return std::move(server_socket);
}
std::unique_ptr<api::ble_v2::BleL2capServerSocket> BleMedium::OpenL2capServerSocket(
std::unique_ptr<api::ble::BleL2capServerSocket> BleMedium::OpenL2capServerSocket(
const std::string &service_id) {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSError *blockPSMPublishedError = nil;
@@ -575,9 +575,9 @@ std::unique_ptr<api::ble_v2::BleL2capServerSocket> BleMedium::OpenL2capServerSoc
// TODO(b/290385712): Add support for @c cancellation_flag.
// TODO(b/293336684): Old Weave code that need to be deleted once shared Weave is complete.
std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(
const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) {
std::unique_ptr<api::ble::BleSocket> BleMedium::Connect(
const std::string &service_id, api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) {
id<GNCPeripheral> peripheral = peripherals_.Get(peripheral_id);
if (!peripheral) {
GNCLoggerError(@"Failed to connect to Gatt server: peripheral is not found.");
@@ -630,9 +630,9 @@ std::unique_ptr<api::ble_v2::BleSocket> BleMedium::Connect(
return std::move(socket);
}
std::unique_ptr<api::ble_v2::BleL2capSocket> BleMedium::ConnectOverL2cap(
int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) {
std::unique_ptr<api::ble::BleL2capSocket> BleMedium::ConnectOverL2cap(
int psm, const std::string &service_id, api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) {
id<GNCPeripheral> peripheral = peripherals_.Get(peripheral_id);
if (!peripheral) {
GNCLoggerError(@"Failed to connect over L2CAP: peripheral is not found.");
@@ -686,7 +686,7 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() {
return [medium_ supportsExtendedAdvertisements];
}
std::optional<api::ble_v2::BlePeripheral::UniqueId> BleMedium::RetrieveBlePeripheralIdFromNativeId(
std::optional<api::ble::BlePeripheral::UniqueId> BleMedium::RetrieveBlePeripheralIdFromNativeId(
const std::string &ble_peripheral_native_id) {
NSString *uuidString = [[NSString alloc] initWithUTF8String:ble_peripheral_native_id.c_str()];
if (uuidString == nil) {
@@ -736,7 +736,7 @@ void BleMedium::CleanUpExpiredAdvertisementPackets(NSDate *now) {
}
bool BleMedium::ShouldReportAdvertisement(NSDate *now,
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
api::ble::BlePeripheral::UniqueId peripheral_id,
NSDictionary<CBUUID *, NSData *> *service_data) {
absl::MutexLock lock(&advertisement_packets_mutex_);
if (service_data == nil || service_data.count == 0) {
@@ -768,21 +768,20 @@ bool BleMedium::ShouldReportAdvertisement(NSDate *now,
return true;
}
void BleMedium::AddAdvertisementPacketInfo(api::ble_v2::BlePeripheral::UniqueId peripheral_id,
void BleMedium::AddAdvertisementPacketInfo(api::ble::BlePeripheral::UniqueId peripheral_id,
NSDictionary<CBUUID *, NSData *> *service_data) {
absl::MutexLock lock(&advertisement_packets_mutex_);
advertisement_packets_map_[peripheral_id] = {[NSDate date], service_data};
}
api::ble_v2::BlePeripheral::UniqueId BleMedium::PeripheralsMap::Add(id<GNCPeripheral> peripheral) {
api::ble::BlePeripheral::UniqueId BleMedium::PeripheralsMap::Add(id<GNCPeripheral> peripheral) {
absl::MutexLock lock(&mutex_);
api::ble_v2::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash;
api::ble::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash;
peripherals_.insert({peripheral_id, peripheral});
return peripheral_id;
}
id<GNCPeripheral> BleMedium::PeripheralsMap::Get(
api::ble_v2::BlePeripheral::UniqueId peripheral_id) {
id<GNCPeripheral> BleMedium::PeripheralsMap::Get(api::ble::BlePeripheral::UniqueId peripheral_id) {
absl::MutexLock lock(&mutex_);
auto peripheral_it = peripherals_.find(peripheral_id);
if (peripheral_it == peripherals_.end()) {
@@ -24,7 +24,7 @@
#include <string>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
@protocol GNCPeripheral;
@@ -37,10 +37,10 @@ namespace apple {
// Many times a CoreBluetooth peripheral is not available, namely, when the
// remote device is a central. For these cases, an EmptyBlePeripheral should be
// used instead.
class BlePeripheral : public api::ble_v2::BlePeripheral {
class BlePeripheral : public api::ble::BlePeripheral {
public:
// Returns a reference to a default BlePeripheral.
static api::ble_v2::BlePeripheral& DefaultBlePeripheral();
static api::ble::BlePeripheral& DefaultBlePeripheral();
explicit BlePeripheral(id<GNCPeripheral> peripheral);
~BlePeripheral() override = default;
@@ -19,7 +19,7 @@
#include <string>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/Mediums/BLE/GNCPeripheral.h"
@@ -29,11 +29,11 @@ namespace apple {
#pragma mark - BlePeripheral
BlePeripheral::BlePeripheral(id<GNCPeripheral> peripheral)
: api::ble_v2::BlePeripheral(peripheral.identifier.hash), peripheral_(peripheral) {}
: api::ble::BlePeripheral(peripheral.identifier.hash), peripheral_(peripheral) {}
api::ble_v2::BlePeripheral& BlePeripheral::DefaultBlePeripheral() {
static api::ble_v2::BlePeripheral* default_peripheral =
new api::ble_v2::BlePeripheral(0xffffffffffffffff);
api::ble::BlePeripheral& BlePeripheral::DefaultBlePeripheral() {
static api::ble::BlePeripheral* default_peripheral =
new api::ble::BlePeripheral(0xffffffffffffffff);
return *default_peripheral;
}
@@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Note: File language is detected using heuristics. Many Objective-C++ headers are incorrectly
// classified as C++ resulting in invalid linter errors. The use of "NSArray" and other Foundation
// classes like "NSData", "NSDictionary" and "NSUUID" are highly weighted for Objective-C and
// Objective-C++ scores. Oddly, "#import <Foundation/Foundation.h>" does not contribute any points.
// This comment alone should be enough to trick the IDE in to believing this is actually some sort
// of Objective-C file. See: cs/google3/devtools/search/lang/recognize_language_classifiers_data
// Note: File language is detected using heuristics. Many Objective-C++ headers
// are incorrectly classified as C++ resulting in invalid linter errors. The use
// of "NSArray" and other Foundation classes like "NSData", "NSDictionary" and
// "NSUUID" are highly weighted for Objective-C and Objective-C++ scores. Oddly,
// "#import <Foundation/Foundation.h>" does not contribute any points. This
// comment alone should be enough to trick the IDE in to believing this is
// actually some sort of Objective-C file. See:
// cs/google3/devtools/search/lang/recognize_language_classifiers_data
// TODO(b/293336684): Remove this file when shared Weave is complete.
@@ -27,15 +29,14 @@
#include "absl/functional/any_invocable.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#import "internal/platform/implementation/apple/ble_socket.h"
#include "internal/platform/implementation/ble.h"
namespace nearby {
namespace apple {
// A BLE server socket for listening for incoming Weave sockets.
class BleServerSocket : public api::ble_v2::BleServerSocket {
class BleServerSocket : public api::ble::BleServerSocket {
public:
BleServerSocket() = default;
~BleServerSocket() override;
@@ -49,7 +50,7 @@ class BleServerSocket : public api::ble_v2::BleServerSocket {
// On success, returns connected socket, ready to exchange data or nullptr on
// error. Once error is reported, it is permanent, and ServerSocket must be
// closed.
std::unique_ptr<api::ble_v2::BleSocket> Accept() override
std::unique_ptr<api::ble::BleSocket> Accept() override
ABSL_LOCKS_EXCLUDED(mutex_);
// Close the server socket.
@@ -29,7 +29,7 @@ BleServerSocket::~BleServerSocket() {
DoClose();
}
std::unique_ptr<api::ble_v2::BleSocket> BleServerSocket::Accept() {
std::unique_ptr<api::ble::BleSocket> BleServerSocket::Accept() {
absl::MutexLock lock(&mutex_);
while (!closed_ && pending_sockets_.empty()) {
cond_.Wait(&mutex_);
@@ -25,7 +25,7 @@
#include <memory>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/ble_peripheral.h"
@@ -88,13 +88,13 @@ class BleOutputStream : public OutputStream {
};
// A BLE Weave socket.
class BleSocket : public api::ble_v2::BleSocket {
class BleSocket : public api::ble::BleSocket {
public:
explicit BleSocket(id<GNCMConnection> connection);
// The peripheral used to create the socket must outlive the socket or undefined behavior will
// occur.
BleSocket(id<GNCMConnection> connection, api::ble_v2::BlePeripheral::UniqueId peripheral_id);
BleSocket(id<GNCMConnection> connection, api::ble::BlePeripheral::UniqueId peripheral_id);
~BleSocket() override;
// Returns the InputStream of the BleSocket.
@@ -116,7 +116,7 @@ class BleSocket : public api::ble_v2::BleSocket {
// Returns valid BlePeripheral pointer if there is a connection, and
// nullptr otherwise.
api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; }
api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; }
bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_);
@@ -130,7 +130,7 @@ class BleSocket : public api::ble_v2::BleSocket {
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
std::unique_ptr<BleInputStream> input_stream_;
std::unique_ptr<BleOutputStream> output_stream_;
api::ble_v2::BlePeripheral::UniqueId peripheral_id_;
api::ble::BlePeripheral::UniqueId peripheral_id_;
absl::AnyInvocable<void()> close_notifier_;
};
@@ -14,7 +14,7 @@
#import "internal/platform/implementation/apple/ble_socket.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#import "internal/platform/implementation/apple/Mediums/BLE/GNCMBleConnection.h"
#import "internal/platform/implementation/apple/ble_peripheral.h"
@@ -179,8 +179,7 @@ BleSocket::BleSocket(id<GNCMConnection> connection)
output_stream_(new BleOutputStream(connection)),
peripheral_id_(BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {}
BleSocket::BleSocket(id<GNCMConnection> connection,
api::ble_v2::BlePeripheral::UniqueId peripheral_id)
BleSocket::BleSocket(id<GNCMConnection> connection, api::ble::BlePeripheral::UniqueId peripheral_id)
: input_stream_(new BleInputStream()),
output_stream_(new BleOutputStream(connection)),
peripheral_id_(peripheral_id) {}
@@ -22,7 +22,7 @@
#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
@class GNCBLEGATTCharacteristic;
@@ -36,25 +36,25 @@ CBUUID *CBUUID16FromCPP(const Uuid &uuid);
CBUUID *CBUUID128FromCPP(const Uuid &uuid);
/** Converts a C++ write type enum to a CoreBluetooth write type. */
CBCharacteristicWriteType CBCharacteristicWriteTypeFromCPP(api::ble_v2::GattClient::WriteType wt);
CBCharacteristicWriteType CBCharacteristicWriteTypeFromCPP(api::ble::GattClient::WriteType wt);
/**
* Converts a C++ permission enum to CoreBluetooth permissions.
*
/**
* Converts a C++ permission enum to CoreBluetooth permissions.
*
* This conversion keeps multiple permission values intact.
*/
CBAttributePermissions CBAttributePermissionsFromCPP(api::ble_v2::GattCharacteristic::Permission p);
CBAttributePermissions CBAttributePermissionsFromCPP(api::ble::GattCharacteristic::Permission p);
/**
* Converts a C++ property enum to CoreBluetooth properties.
*
/**
* Converts a C++ property enum to CoreBluetooth properties.
*
* This conversion keeps multiple property values intact.
*/
CBCharacteristicProperties CBCharacteristicPropertiesFromCPP(
api::ble_v2::GattCharacteristic::Property p);
api::ble::GattCharacteristic::Property p);
/** Converts a C++ characteristic to an Objective-C characteristic. */
GNCBLEGATTCharacteristic *ObjCGATTCharacteristicFromCPP(const api::ble_v2::GattCharacteristic &c);
GNCBLEGATTCharacteristic *ObjCGATTCharacteristicFromCPP(const api::ble::GattCharacteristic &c);
NSMutableDictionary<CBUUID *, NSData *> *ObjCServiceDataFromCPP(
const absl::flat_hash_map<Uuid, nearby::ByteArray> &sd);
@@ -62,24 +62,24 @@ NSMutableDictionary<CBUUID *, NSData *> *ObjCServiceDataFromCPP(
/** Converts a 16 or 128 bit CoreBluetooth UUID to a C++ UUID. */
Uuid CPPUUIDFromObjC(CBUUID *uuid);
/**
* Converts CoreBluetooth permissions to a C++ permission enum.
*
/**
* Converts CoreBluetooth permissions to a C++ permission enum.
*
* This conversion keeps multiple permission values intact.
*/
api::ble_v2::GattCharacteristic::Permission CPPGATTCharacteristicPermissionFromObjC(
api::ble::GattCharacteristic::Permission CPPGATTCharacteristicPermissionFromObjC(
CBAttributePermissions p);
/**
* Converts CoreBluetooth properties to a C++ property enum.
*
/**
* Converts CoreBluetooth properties to a C++ property enum.
*
* This conversion keeps multiple property values intact.
*/
api::ble_v2::GattCharacteristic::Property CPPGATTCharacteristicPropertyFromObjC(
api::ble::GattCharacteristic::Property CPPGATTCharacteristicPropertyFromObjC(
CBCharacteristicProperties p);
/** Converts an Objective-C characteristic to a C++ characteristic. */
api::ble_v2::GattCharacteristic CPPGATTCharacteristicFromObjC(GNCBLEGATTCharacteristic *c);
api::ble::GattCharacteristic CPPGATTCharacteristicFromObjC(GNCBLEGATTCharacteristic *c);
} // namespace apple
} // namespace nearby
@@ -25,14 +25,14 @@
namespace nearby {
namespace apple {
using GattCharacteristic = api::ble_v2::GattCharacteristic;
using Permission = api::ble_v2::GattCharacteristic::Permission;
using Property = api::ble_v2::GattCharacteristic::Property;
using WriteType = api::ble_v2::GattClient::WriteType;
using GattCharacteristic = api::ble::GattCharacteristic;
using Permission = api::ble::GattCharacteristic::Permission;
using Property = api::ble::GattCharacteristic::Property;
using WriteType = api::ble::GattClient::WriteType;
/**
* Checks if enum `a` contains enum `b`
*
*
* For example, `Contains(Permission::kRead | Permission::kWrite, Permission::kRead)` should return
* `YES`.
*/
@@ -16,19 +16,19 @@
#import <Foundation/Foundation.h>
#include <string>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/base/files.h"
#include "internal/platform/implementation/apple/awdl.h"
#import "internal/platform/implementation/apple/Log/GNCLogger.h"
#include "internal/platform/implementation/apple/atomic_boolean.h"
#include "internal/platform/implementation/apple/atomic_uint32.h"
#include "internal/platform/implementation/apple/awdl.h"
#include "internal/platform/implementation/apple/ble.h"
#include "internal/platform/implementation/apple/condition_variable.h"
#include "internal/platform/implementation/apple/count_down_latch.h"
#include "internal/platform/implementation/apple/device_info.h"
#import "internal/platform/implementation/apple/Log/GNCLogger.h"
#import "internal/platform/implementation/apple/multi_thread_executor.h"
#include "internal/platform/implementation/apple/mutex.h"
#include "internal/platform/implementation/apple/preferences_manager.h"
@@ -172,7 +172,7 @@ std::unique_ptr<BluetoothClassicMedium> ImplementationPlatform::CreateBluetoothC
return nullptr;
}
std::unique_ptr<ble_v2::BleMedium> ImplementationPlatform::CreateBleV2Medium(
std::unique_ptr<ble::BleMedium> ImplementationPlatform::CreateBleMedium(
api::BluetoothAdapter& adapter) {
return std::make_unique<apple::BleMedium>();
}
@@ -256,7 +256,7 @@ absl::StatusOr<WebResponse> ImplementationPlatform::SendRequest(const WebRequest
}
if (blockData != nil) {
// Body is not a UTF-8 encoded string and is just using `std::string` as a container for data.
webResponse.body = std::string((char *)blockData.bytes, blockData.length);
webResponse.body = std::string((char*)blockData.bytes, blockData.length);
}
return webResponse;
}
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_API_BLE_V2_H_
#define PLATFORM_API_BLE_V2_H_
#ifndef PLATFORM_API_BLE_H_
#define PLATFORM_API_BLE_H_
#include <algorithm>
#include <cstdint>
@@ -40,7 +40,7 @@
namespace nearby {
namespace api {
namespace ble_v2 {
namespace ble {
// Coarse representation of power settings throughout all BLE operations.
enum class TxPowerLevel {
@@ -304,7 +304,7 @@ class GattServer {
// Stops a GATT server.
virtual void Stop() = 0;
};
// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/ble_v2.h)
// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/ble.h)
// Callback for asynchronous events on the client side of a GATT connection.
struct ClientGattConnectionCallback {
@@ -641,8 +641,8 @@ class BleMedium {
}
};
} // namespace ble_v2
} // namespace ble
} // namespace api
} // namespace nearby
#endif // PLATFORM_API_BLE_V2_H_
#endif // PLATFORM_API_BLE_H_
+3 -3
View File
@@ -80,7 +80,7 @@ cc_library(
testonly = True,
srcs = [
"awdl.cc",
"ble_v2.cc",
"ble.cc",
"bluetooth_adapter.cc",
"bluetooth_classic.cc",
"credential_storage_impl.cc",
@@ -90,7 +90,7 @@ cc_library(
],
hdrs = [
"awdl.h",
"ble_v2.h",
"ble.h",
"bluetooth_adapter.h",
"bluetooth_classic.h",
"credential_storage_impl.h",
@@ -133,7 +133,7 @@ cc_test(
name = "comm_test",
srcs = [
"awdl_test.cc",
"ble_v2_test.cc",
"ble_test.cc",
],
deps = [
":comm",
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/g3/ble_v2.h"
#include "internal/platform/implementation/g3/ble.h"
#include <algorithm>
#include <memory>
@@ -34,7 +34,7 @@
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/logging.h"
@@ -47,8 +47,8 @@ namespace g3 {
namespace {
using ::nearby::api::ble_v2::BleAdvertisementData;
using ::nearby::api::ble_v2::TxPowerLevel;
using ::nearby::api::ble::BleAdvertisementData;
using ::nearby::api::ble::TxPowerLevel;
std::string TxPowerLevelToName(TxPowerLevel power_mode) {
switch (power_mode) {
@@ -67,21 +67,21 @@ std::string TxPowerLevelToName(TxPowerLevel power_mode) {
} // namespace
api::ble_v2::BlePeripheral::UniqueId BleV2Socket::GetRemotePeripheralId() {
BleV2Socket* remote_socket = GetRemoteSocket();
api::ble::BlePeripheral::UniqueId BleSocket::GetRemotePeripheralId() {
BleSocket* remote_socket = GetRemoteSocket();
if (remote_socket == nullptr || remote_socket->adapter_ == nullptr ||
remote_socket->adapter_->GetBleV2Medium() == nullptr) {
remote_socket->adapter_->GetBleMedium() == nullptr) {
return 0LL;
}
BleV2Medium* medium =
dynamic_cast<BleV2Medium*>(remote_socket->adapter_->GetBleV2Medium());
BleMedium* medium =
dynamic_cast<BleMedium*>(remote_socket->adapter_->GetBleMedium());
if (medium == nullptr) {
return 0LL;
}
return medium->GetAdapter().GetUniqueId();
}
std::unique_ptr<api::ble_v2::BleSocket> BleV2ServerSocket::Accept() {
std::unique_ptr<api::ble::BleSocket> BleServerSocket::Accept() {
absl::MutexLock lock(mutex_);
while (!closed_ && pending_sockets_.empty()) {
cond_.Wait(&mutex_);
@@ -92,14 +92,14 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2ServerSocket::Accept() {
pending_sockets_.extract(pending_sockets_.begin()).value();
CHECK(remote_socket);
auto local_socket = std::make_unique<BleV2Socket>(adapter_);
auto local_socket = std::make_unique<BleSocket>(adapter_);
local_socket->Connect(*remote_socket);
remote_socket->Connect(*local_socket);
cond_.SignalAll();
return local_socket;
}
bool BleV2ServerSocket::Connect(BleV2Socket& socket) {
bool BleServerSocket::Connect(BleSocket& socket) {
absl::MutexLock lock(mutex_);
if (closed_) return false;
if (socket.IsConnected()) {
@@ -116,22 +116,22 @@ bool BleV2ServerSocket::Connect(BleV2Socket& socket) {
return true;
}
void BleV2ServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
void BleServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(mutex_);
close_notifier_ = std::move(notifier);
}
BleV2ServerSocket::~BleV2ServerSocket() {
BleServerSocket::~BleServerSocket() {
absl::MutexLock lock(mutex_);
DoClose();
}
Exception BleV2ServerSocket::Close() {
Exception BleServerSocket::Close() {
absl::MutexLock lock(mutex_);
return DoClose();
}
Exception BleV2ServerSocket::DoClose() {
Exception BleServerSocket::DoClose() {
bool should_notify = !closed_;
closed_ = true;
if (should_notify) {
@@ -148,24 +148,24 @@ Exception BleV2ServerSocket::DoClose() {
return {Exception::kSuccess};
}
BleV2Medium::BleV2Medium(BluetoothAdapter& adapter) : adapter_(adapter) {
BleMedium::BleMedium(BluetoothAdapter& adapter) : adapter_(adapter) {
CHECK(&adapter_);
adapter_.SetBleV2Medium(this);
adapter_.SetBleMedium(this);
is_extended_advertisements_available_ =
MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable();
MediumEnvironment::Instance().RegisterBleV2Medium(*this,
adapter_.GetUniqueId());
MediumEnvironment::Instance().RegisterBleMedium(*this,
adapter_.GetUniqueId());
}
BleV2Medium::~BleV2Medium() {
adapter_.SetBleV2Medium(nullptr);
MediumEnvironment::Instance().UnregisterBleV2Medium(*this);
BleMedium::~BleMedium() {
adapter_.SetBleMedium(nullptr);
MediumEnvironment::Instance().UnregisterBleMedium(*this);
}
bool BleV2Medium::StartAdvertising(
bool BleMedium::StartAdvertising(
const BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_parameters) {
api::ble::AdvertiseParameters advertise_parameters) {
LOG(INFO)
<< "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement="
<< advertising_data.is_extended_advertisement
@@ -181,26 +181,26 @@ bool BleV2Medium::StartAdvertising(
}
absl::MutexLock lock(mutex_);
MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising(
MediumEnvironment::Instance().UpdateBleMediumForAdvertising(
/*enabled=*/true, *this, adapter_.GetUniqueId(), advertising_data);
return true;
}
bool BleV2Medium::StopAdvertising() {
bool BleMedium::StopAdvertising() {
LOG(INFO) << "G3 Ble StopAdvertising";
absl::MutexLock lock(mutex_);
BleAdvertisementData empty_advertisement_data = {};
MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising(
MediumEnvironment::Instance().UpdateBleMediumForAdvertising(
/*enabled=*/false, *this, adapter_.GetUniqueId(),
empty_advertisement_data);
return true;
}
std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_parameters,
BleV2Medium::AdvertisingCallback callback) {
std::unique_ptr<BleMedium::AdvertisingSession> BleMedium::StartAdvertising(
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertise_parameters,
BleMedium::AdvertisingCallback callback) {
LOG(INFO)
<< "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement="
<< advertising_data.is_extended_advertisement
@@ -219,7 +219,7 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
callback.start_advertising_result(absl::OkStatus());
}
absl::MutexLock lock(mutex_);
MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising(
MediumEnvironment::Instance().UpdateBleMediumForAdvertising(
/*enabled=*/true, *this, adapter_.GetUniqueId(), advertising_data);
return std::make_unique<AdvertisingSession>(
AdvertisingSession{.stop_advertising = [this] {
@@ -229,13 +229,13 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
}});
}
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
TxPowerLevel tx_power_level,
ScanCallback callback) {
bool BleMedium::StartScanning(const Uuid& service_uuid,
TxPowerLevel tx_power_level,
ScanCallback callback) {
LOG(INFO) << "G3 Ble StartScanning";
auto internal_session_id = Prng().NextUint32();
absl::MutexLock lock(mutex_);
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
MediumEnvironment::Instance().UpdateBleMediumForScanning(
/*enabled=*/true, service_uuid, internal_session_id,
{.advertisement_found_cb = std::move(callback.advertisement_found_cb)},
*this);
@@ -243,9 +243,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
return true;
}
bool BleV2Medium::StartMultipleServicesScanning(
bool BleMedium::StartMultipleServicesScanning(
const std::vector<Uuid>& service_uuids,
api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) {
api::ble::TxPowerLevel tx_power_level, ScanCallback callback) {
LOG(INFO) << "G3 Ble StartMultipleServicesScanning";
absl::MutexLock lock(mutex_);
@@ -254,13 +254,13 @@ bool BleV2Medium::StartMultipleServicesScanning(
auto internal_session_id = Prng().NextUint32();
ScanCallback multiple_scan_callback = {
.advertisement_found_cb =
[this](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
[this](api::ble::BlePeripheral::UniqueId peripheral_id,
BleAdvertisementData advertisement_data) {
scan_callback_.advertisement_found_cb(peripheral_id,
advertisement_data);
}};
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
MediumEnvironment::Instance().UpdateBleMediumForScanning(
/*enabled=*/true, service_uuid, internal_session_id,
{.advertisement_found_cb =
std::move(multiple_scan_callback.advertisement_found_cb)},
@@ -271,11 +271,11 @@ bool BleV2Medium::StartMultipleServicesScanning(
return true;
}
bool BleV2Medium::StopScanning() {
bool BleMedium::StopScanning() {
LOG(INFO) << "G3 Ble StopScanning";
absl::MutexLock lock(mutex_);
for (auto element : scanning_internal_session_ids_) {
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
MediumEnvironment::Instance().UpdateBleMediumForScanning(
/*enabled=*/false,
/*service_uuid=*/element.first,
/*internal_session_id*/ element.second,
@@ -284,16 +284,16 @@ bool BleV2Medium::StopScanning() {
return true;
}
std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
std::unique_ptr<BleMedium::ScanningSession> BleMedium::StartScanning(
const Uuid& service_uuid, TxPowerLevel tx_power_level,
BleV2Medium::ScanningCallback callback) {
BleMedium::ScanningCallback callback) {
LOG(INFO) << "G3 Ble StartScanning";
auto internal_session_id = Prng().NextUint32();
{
absl::MutexLock lock(mutex_);
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
MediumEnvironment::Instance().UpdateBleMediumForScanning(
/*enabled=*/true, service_uuid, internal_session_id,
std::move(callback), *this);
scanning_internal_session_ids_.insert({service_uuid, internal_session_id});
@@ -310,7 +310,7 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
return absl::NotFoundError(
"can't find the provided internal session");
}
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
MediumEnvironment::Instance().UpdateBleMediumForScanning(
/*enabled=*/false, service_uuid, internal_session_id,
/*callback=*/{}, *this);
scanning_internal_session_ids_.erase(
@@ -320,25 +320,25 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
});
}
std::unique_ptr<api::ble_v2::GattServer> BleV2Medium::StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) {
std::unique_ptr<api::ble::GattServer> BleMedium::StartGattServer(
api::ble::ServerGattConnectionCallback callback) {
return std::make_unique<GattServer>(*this, std::move(callback));
}
bool BleV2Medium::IsStopped(Borrowable<api::ble_v2::GattServer*> server) {
bool BleMedium::IsStopped(Borrowable<api::ble::GattServer*> server) {
auto borrowed = server.Borrow();
if (!borrowed) {
return true;
}
BleV2Medium::GattServer* gatt_server = static_cast<GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server = static_cast<GattServer*>(*borrowed);
return gatt_server->IsStopped();
}
std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
std::unique_ptr<api::ble::GattClient> BleMedium::ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id,
TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) {
Borrowable<api::ble_v2::GattServer*> server =
api::ble::ClientGattConnectionCallback callback) {
Borrowable<api::ble::GattServer*> server =
MediumEnvironment::Instance().GetGattServer(peripheral_id);
if (IsStopped(server)) {
LOG(WARNING) << "No GATT server found for " << peripheral_id;
@@ -348,36 +348,36 @@ std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
std::move(callback));
}
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
bool BleMedium::IsExtendedAdvertisementsAvailable() {
return is_extended_advertisements_available_;
}
BleV2Medium::GattServer::GattServer(
BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback)
BleMedium::GattServer::GattServer(
BleMedium& medium, api::ble::ServerGattConnectionCallback callback)
: medium_(medium), callback_(std::move(callback)) {
BluetoothAdapter& adapter = medium.GetAdapter();
MediumEnvironment::Instance().RegisterGattServer(
medium_, adapter.GetUniqueId(), lender_.GetBorrowable());
}
BleV2Medium::GattServer::~GattServer() {
BleMedium::GattServer::~GattServer() {
Stop();
lender_.Release();
MediumEnvironment::Instance().UnregisterGattServer(medium_);
}
std::optional<api::ble_v2::GattCharacteristic>
BleV2Medium::GattServer::CreateCharacteristic(
std::optional<api::ble::GattCharacteristic>
BleMedium::GattServer::CreateCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) {
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) {
absl::MutexLock lock(mutex_);
api::ble_v2::GattCharacteristic characteristic = {
.uuid = characteristic_uuid, .service_uuid = service_uuid};
api::ble::GattCharacteristic characteristic = {.uuid = characteristic_uuid,
.service_uuid = service_uuid};
characteristics_[characteristic] = absl::NotFoundError("value not set");
return characteristic;
}
bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics(
bool BleMedium::GattServer::DiscoverBleMediumGattCharacteristics(
const Uuid& service_uuid, const std::vector<Uuid>& characteristic_uuids) {
absl::MutexLock lock(mutex_);
auto contains = [&](const Uuid& characteristic_uuid) {
@@ -385,7 +385,7 @@ bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics(
characteristic_uuid) != characteristic_uuids.end();
};
for (const auto& it : characteristics_) {
const api::ble_v2::GattCharacteristic& characteristic = it.first;
const api::ble::GattCharacteristic& characteristic = it.first;
if (characteristic.service_uuid == service_uuid &&
contains(characteristic.uuid)) {
return true;
@@ -394,8 +394,8 @@ bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics(
return false;
}
bool BleV2Medium::GattServer::UpdateCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
bool BleMedium::GattServer::UpdateCharacteristic(
const api::ble::GattCharacteristic& characteristic,
const nearby::ByteArray& value) {
absl::MutexLock lock(mutex_);
LOG(INFO) << "G3 Ble GattServer UpdateCharacteristic, characteristic=("
@@ -406,8 +406,8 @@ bool BleV2Medium::GattServer::UpdateCharacteristic(
return true;
}
absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
absl::Status BleMedium::GattServer::NotifyCharacteristicChanged(
const api::ble::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) {
absl::MutexLock lock(mutex_);
LOG(INFO) << "G3 Ble GattServer NotifyCharacteristicChanged, characteristic=("
@@ -426,9 +426,9 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
return status;
}
absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic, int offset) {
absl::StatusOr<ByteArray> BleMedium::GattServer::ReadCharacteristic(
const api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic, int offset) {
{
absl::MutexLock lock(mutex_);
const auto it = characteristics_.find(characteristic);
@@ -455,9 +455,9 @@ absl::StatusOr<ByteArray> BleV2Medium::GattServer::ReadCharacteristic(
return result;
}
absl::Status BleV2Medium::GattServer::WriteCharacteristic(
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic, int offset,
absl::Status BleMedium::GattServer::WriteCharacteristic(
const api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic, int offset,
absl::string_view data) {
if (HasCharacteristic(characteristic)) {
absl::Status result;
@@ -475,9 +475,9 @@ absl::Status BleV2Medium::GattServer::WriteCharacteristic(
absl::StrCat(characteristic, " not found"));
}
bool BleV2Medium::GattServer::AddCharacteristicSubscription(
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic,
bool BleMedium::GattServer::AddCharacteristicSubscription(
const api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic,
absl::AnyInvocable<void(absl::string_view value)> callback) {
absl::MutexLock lock(mutex_);
const auto it = characteristics_.find(characteristic);
@@ -489,9 +489,9 @@ bool BleV2Medium::GattServer::AddCharacteristicSubscription(
return false;
}
bool BleV2Medium::GattServer::RemoveCharacteristicSubscription(
const api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic) {
bool BleMedium::GattServer::RemoveCharacteristicSubscription(
const api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic) {
absl::MutexLock lock(mutex_);
const auto it = characteristics_.find(characteristic);
if (it != characteristics_.end()) {
@@ -501,25 +501,25 @@ bool BleV2Medium::GattServer::RemoveCharacteristicSubscription(
return false;
}
bool BleV2Medium::GattServer::HasCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic) {
bool BleMedium::GattServer::HasCharacteristic(
const api::ble::GattCharacteristic& characteristic) {
absl::MutexLock lock(mutex_);
return characteristics_.find(characteristic) != characteristics_.end();
}
void BleV2Medium::GattServer::Connect(GattClient* client) {
void BleMedium::GattServer::Connect(GattClient* client) {
absl::MutexLock lock(mutex_);
connected_clients_.push_back(client);
}
void BleV2Medium::GattServer::Disconnect(GattClient* client) {
void BleMedium::GattServer::Disconnect(GattClient* client) {
absl::MutexLock lock(mutex_);
connected_clients_.erase(
std::remove(connected_clients_.begin(), connected_clients_.end(), client),
connected_clients_.end());
}
void BleV2Medium::GattServer::Stop() {
void BleMedium::GattServer::Stop() {
if (stopped_) return;
absl::MutexLock lock(mutex_);
stopped_ = true;
@@ -529,31 +529,31 @@ void BleV2Medium::GattServer::Stop() {
characteristics_.clear();
}
BleV2Medium::GattClient::GattClient(
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
Borrowable<api::ble_v2::GattServer*> gatt_server,
api::ble_v2::ClientGattConnectionCallback callback)
BleMedium::GattClient::GattClient(
api::ble::BlePeripheral::UniqueId peripheral_id,
Borrowable<api::ble::GattServer*> gatt_server,
api::ble::ClientGattConnectionCallback callback)
: peripheral_id_(peripheral_id),
gatt_server_(gatt_server),
callback_(std::move(callback)) {
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (borrowed) {
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
gatt_server->Connect(this);
}
}
BleV2Medium::GattClient::~GattClient() {
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
BleMedium::GattClient::~GattClient() {
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (borrowed) {
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
gatt_server->Disconnect(this);
}
}
bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics(
bool BleMedium::GattClient::DiscoverServiceAndCharacteristics(
const Uuid& service_uuid, const std::vector<Uuid>& characteristic_uuids) {
LOG(INFO)
<< "G3 Ble GattClient DiscoverServiceAndCharacteristics, service_uuid="
@@ -562,19 +562,19 @@ bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics(
if (!is_connection_alive_) {
return false;
}
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (!borrowed) {
return false;
}
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
return gatt_server->DiscoverBleV2MediumGattCharacteristics(
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
return gatt_server->DiscoverBleMediumGattCharacteristics(
service_uuid, characteristic_uuids);
}
std::optional<api::ble_v2::GattCharacteristic>
BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
const Uuid& characteristic_uuid) {
std::optional<api::ble::GattCharacteristic>
BleMedium::GattClient::GetCharacteristic(const Uuid& service_uuid,
const Uuid& characteristic_uuid) {
absl::MutexLock lock(mutex_);
LOG(INFO) << "G3 Ble GattClient GetCharacteristic, service_uuid="
<< service_uuid.Get16BitAsString()
@@ -583,16 +583,16 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
return std::nullopt;
}
// clang-format off
api::ble_v2::GattCharacteristic characteristic = {
api::ble::GattCharacteristic characteristic = {
.uuid = characteristic_uuid,
.service_uuid = service_uuid};
// clang-format on
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (!borrowed) {
return std::nullopt;
}
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
if (!gatt_server->HasCharacteristic(characteristic)) {
LOG(WARNING) << "G3 Ble GattClient GetCharacteristic, characteristic=("
<< characteristic.service_uuid.Get16BitAsString() << ","
@@ -607,18 +607,18 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
return characteristic;
}
std::optional<std::string> BleV2Medium::GattClient::ReadCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic) {
std::optional<std::string> BleMedium::GattClient::ReadCharacteristic(
const api::ble::GattCharacteristic& characteristic) {
absl::MutexLock lock(mutex_);
if (!is_connection_alive_) {
return std::nullopt;
}
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (!borrowed) {
return std::nullopt;
}
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
absl::StatusOr<ByteArray> value =
gatt_server->ReadCharacteristic(peripheral_id_, characteristic,
/*offset=*/0);
@@ -635,19 +635,19 @@ std::optional<std::string> BleV2Medium::GattClient::ReadCharacteristic(
return value->string_data();
}
bool BleV2Medium::GattClient::WriteCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
absl::string_view value, api::ble_v2::GattClient::WriteType write_type) {
bool BleMedium::GattClient::WriteCharacteristic(
const api::ble::GattCharacteristic& characteristic, absl::string_view value,
api::ble::GattClient::WriteType write_type) {
absl::MutexLock lock(mutex_);
if (!is_connection_alive_) {
return false;
}
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (!borrowed) {
return false;
}
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
LOG(INFO) << "G3 Ble WriteCharacteristic, characteristic=("
<< characteristic.service_uuid.Get16BitAsString() << ","
<< std::string(characteristic.uuid)
@@ -661,20 +661,20 @@ bool BleV2Medium::GattClient::WriteCharacteristic(
return status.ok();
}
bool BleV2Medium::GattClient::SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
bool BleMedium::GattClient::SetCharacteristicSubscription(
const api::ble::GattCharacteristic& characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)>
on_characteristic_changed_cb) {
absl::MutexLock lock(mutex_);
if (!is_connection_alive_) {
return false;
}
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (!borrowed) {
return false;
}
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
LOG(INFO) << "G3 Ble SetCharacteristicSubscription, characteristic=("
<< characteristic.service_uuid.Get16BitAsString() << ","
<< std::string(characteristic.uuid) << "), enable = " << enable;
@@ -688,19 +688,19 @@ bool BleV2Medium::GattClient::SetCharacteristicSubscription(
}
}
void BleV2Medium::GattClient::Disconnect() {
void BleMedium::GattClient::Disconnect() {
bool was_alive = is_connection_alive_.exchange(false);
if (!was_alive) return;
LOG(INFO) << "G3 Ble GattClient Disconnect";
Borrowed<api::ble_v2::GattServer*> borrowed = gatt_server_.Borrow();
Borrowed<api::ble::GattServer*> borrowed = gatt_server_.Borrow();
if (borrowed) {
BleV2Medium::GattServer* gatt_server =
static_cast<BleV2Medium::GattServer*>(*borrowed);
BleMedium::GattServer* gatt_server =
static_cast<BleMedium::GattServer*>(*borrowed);
gatt_server->Disconnect(this);
}
}
void BleV2Medium::GattClient::OnServerDisconnected() {
void BleMedium::GattClient::OnServerDisconnected() {
bool was_alive = is_connection_alive_.exchange(false);
if (!was_alive) return;
LOG(INFO) << "G3 Ble GattServer disconnected";
@@ -709,9 +709,9 @@ void BleV2Medium::GattClient::OnServerDisconnected() {
}
}
std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> BleMedium::OpenServerSocket(
const std::string& service_id) {
auto server_socket = std::make_unique<BleV2ServerSocket>(&GetAdapter());
auto server_socket = std::make_unique<BleServerSocket>(&GetAdapter());
server_socket->SetCloseNotifier([this, service_id]() {
absl::MutexLock lock(mutex_);
server_sockets_.erase(service_id);
@@ -723,29 +723,29 @@ std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
return server_socket;
}
std::unique_ptr<api::ble_v2::BleL2capServerSocket>
BleV2Medium::OpenL2capServerSocket(const std::string& service_id) {
std::unique_ptr<api::ble::BleL2capServerSocket>
BleMedium::OpenL2capServerSocket(const std::string& service_id) {
// TODO(mingshiouwu): add more codes for g3 testing.
return nullptr;
}
std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
std::unique_ptr<api::ble::BleSocket> BleMedium::Connect(
const std::string& service_id, TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
api::ble::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) {
LOG(INFO) << "G3 Ble Connect [self]: medium=" << this
<< ", adapter=" << &GetAdapter()
<< ", peripheral id=" << adapter_.GetUniqueId()
<< ", service_id=" << service_id;
// First, find an instance of remote medium, that exposed this peripheral.
BleV2Medium* remote_medium = dynamic_cast<BleV2Medium*>(
MediumEnvironment::Instance().FindBleV2Medium(remote_peripheral_id));
BleMedium* remote_medium = dynamic_cast<BleMedium*>(
MediumEnvironment::Instance().FindBleMedium(remote_peripheral_id));
if (remote_medium == nullptr) {
LOG(INFO) << "Peripheral not found, id= " << remote_peripheral_id;
return nullptr;
}
BleV2ServerSocket* remote_server_socket = nullptr;
BleServerSocket* remote_server_socket = nullptr;
LOG(INFO) << "G3 Ble Connect [peer]: medium=" << remote_medium
<< ", peripheral=" << &remote_peripheral_id
<< ", service_id=" << service_id;
@@ -777,7 +777,7 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
}
});
auto socket = std::make_unique<BleV2Socket>(&GetAdapter());
auto socket = std::make_unique<BleSocket>(&GetAdapter());
// Finally, Request to connect to this socket.
if (!remote_server_socket->Connect(*socket)) {
LOG(ERROR) << "G3 Ble Failed to connect to existing Ble "
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_G3_BLE_V2_H_
#define PLATFORM_IMPL_G3_BLE_V2_H_
#ifndef PLATFORM_IMPL_G3_BLE_H_
#define PLATFORM_IMPL_G3_BLE_H_
#include <atomic>
#include <cstdint>
@@ -35,7 +35,7 @@
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/socket_base.h"
@@ -46,9 +46,9 @@
namespace nearby {
namespace g3 {
class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase {
class BleSocket : public api::ble::BleSocket, public SocketBase {
public:
explicit BleV2Socket(BluetoothAdapter* adapter) : adapter_(adapter) {}
explicit BleSocket(BluetoothAdapter* adapter) : adapter_(adapter) {}
// Returns the InputStream of this connected BleSocket.
InputStream& GetInputStream() override {
@@ -62,28 +62,28 @@ class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase {
}
// Returns address of a remote BleSocket or nullptr.
BleV2Socket* GetRemoteSocket() {
return static_cast<BleV2Socket*>(SocketBase::GetRemoteSocket());
BleSocket* GetRemoteSocket() {
return static_cast<BleSocket*>(SocketBase::GetRemoteSocket());
}
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override { return SocketBase::Close(); }
api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override
api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override
ABSL_LOCKS_EXCLUDED(mutex_);
private:
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
};
class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
class BleServerSocket : public api::ble::BleServerSocket {
public:
explicit BleV2ServerSocket(BluetoothAdapter* adapter) : adapter_(adapter) {}
BleV2ServerSocket(const BleV2ServerSocket&) = default;
BleV2ServerSocket& operator=(const BleV2ServerSocket&) = default;
BleV2ServerSocket(BleV2ServerSocket&&) = default;
BleV2ServerSocket& operator=(BleV2ServerSocket&&) = default;
~BleV2ServerSocket() override;
explicit BleServerSocket(BluetoothAdapter* adapter) : adapter_(adapter) {}
BleServerSocket(const BleServerSocket&) = default;
BleServerSocket& operator=(const BleServerSocket&) = default;
BleServerSocket(BleServerSocket&&) = default;
BleServerSocket& operator=(BleServerSocket&&) = default;
~BleServerSocket() override;
// Blocks until either:
// - at least one incoming connection request is available, or
@@ -95,7 +95,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
// Called by the server side of a connection.
// Returns BleSocket to the server side.
// If not null, returned socket is connected to its remote (client-side) peer.
std::unique_ptr<api::ble_v2::BleSocket> Accept() override
std::unique_ptr<api::ble::BleSocket> Accept() override
ABSL_LOCKS_EXCLUDED(mutex_);
// Blocks until either:
@@ -105,7 +105,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
//
// Called by the client side of a connection.
// Returns true, if socket is successfully connected.
bool Connect(BleV2Socket& socket) ABSL_LOCKS_EXCLUDED(mutex_);
bool Connect(BleSocket& socket) ABSL_LOCKS_EXCLUDED(mutex_);
// Called by the server side of a connection before passing ownership of
// BleServerSocker to user, to track validity of a pointer to this
@@ -123,67 +123,66 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
mutable absl::Mutex mutex_;
absl::CondVar cond_;
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
absl::flat_hash_set<BleV2Socket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
absl::flat_hash_set<BleSocket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
// Container of operations that can be performed over the BLE medium.
class BleV2Medium : public api::ble_v2::BleMedium {
class BleMedium : public api::ble::BleMedium {
public:
explicit BleV2Medium(BluetoothAdapter& adapter);
~BleV2Medium() override;
explicit BleMedium(BluetoothAdapter& adapter);
~BleMedium() override;
// Returns true once the Ble advertising has been initiated.
bool StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_parameters) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StartAdvertising(const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertise_parameters)
override ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<AdvertisingSession> StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_parameters,
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertise_parameters,
AdvertisingCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_);
bool StartScanning(const Uuid& service_uuid,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble::TxPowerLevel tx_power_level,
ScanCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StartMultipleServicesScanning(const std::vector<Uuid>& service_uuids,
api::ble_v2::TxPowerLevel tx_power_level,
ScanCallback callback)
override ABSL_LOCKS_EXCLUDED(mutex_);
api::ble::TxPowerLevel tx_power_level,
ScanCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopScanning() override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<ScanningSession> StartScanning(
const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level,
ScanningCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::GattServer> StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) override
std::unique_ptr<api::ble::GattServer> StartGattServer(
api::ble::ServerGattConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) override
std::unique_ptr<api::ble::GattClient> ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id,
api::ble::TxPowerLevel tx_power_level,
api::ble::ClientGattConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Open server socket to listen for incoming connection.
//
// On success, returns a new BleServerSocket.
// On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleServerSocket> OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> OpenServerSocket(
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::BleL2capServerSocket> OpenL2capServerSocket(
std::unique_ptr<api::ble::BleL2capServerSocket> OpenL2capServerSocket(
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
// Connects to existing remote Ble peripheral.
//
// On success, returns a new BleSocket.
// On error, returns nullptr.
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
std::unique_ptr<api::ble::BleSocket> Connect(
const std::string& service_id, api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
bool IsExtendedAdvertisementsAvailable() override;
@@ -193,96 +192,94 @@ class BleV2Medium : public api::ble_v2::BleMedium {
private:
class GattClient;
// A concrete implementation for GattServer.
class GattServer : public api::ble_v2::GattServer {
class GattServer : public api::ble::GattServer {
public:
GattServer(BleV2Medium& medium,
api::ble_v2::ServerGattConnectionCallback callback);
GattServer(BleMedium& medium,
api::ble::ServerGattConnectionCallback callback);
~GattServer() override;
std::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
std::optional<api::ble::GattCharacteristic> CreateCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) override;
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) override;
bool UpdateCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
const api::ble::GattCharacteristic& characteristic,
const nearby::ByteArray& value) override;
absl::Status NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const api::ble::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) override;
void Stop() override;
bool IsStopped() { return stopped_; }
bool DiscoverBleV2MediumGattCharacteristics(
bool DiscoverBleMediumGattCharacteristics(
const Uuid& service_uuid,
const std::vector<Uuid>& characteristic_uuids);
absl::StatusOr<ByteArray> ReadCharacteristic(
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic, int offset);
api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic, int offset);
absl::Status WriteCharacteristic(
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic, int offset,
api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic, int offset,
absl::string_view data);
bool AddCharacteristicSubscription(
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic,
api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic,
absl::AnyInvocable<void(absl::string_view value)>);
bool RemoveCharacteristicSubscription(
api::ble_v2::BlePeripheral::UniqueId remote_device_id,
const api::ble_v2::GattCharacteristic& characteristic);
api::ble::BlePeripheral::UniqueId remote_device_id,
const api::ble::GattCharacteristic& characteristic);
bool HasCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic);
bool HasCharacteristic(const api::ble::GattCharacteristic& characteristic);
void Connect(GattClient* client);
void Disconnect(GattClient* client);
private:
using SubscriberKey = std::pair<const api::ble_v2::BlePeripheral::UniqueId,
api::ble_v2::GattCharacteristic>;
using SubscriberKey = std::pair<const api::ble::BlePeripheral::UniqueId,
api::ble::GattCharacteristic>;
using SubscriberCallback =
absl::AnyInvocable<void(absl::string_view value)>;
absl::Mutex mutex_;
BleV2Medium& medium_;
api::ble_v2::ServerGattConnectionCallback callback_;
absl::flat_hash_map<api::ble_v2::GattCharacteristic,
absl::StatusOr<ByteArray>>
BleMedium& medium_;
api::ble::ServerGattConnectionCallback callback_;
absl::flat_hash_map<api::ble::GattCharacteristic, absl::StatusOr<ByteArray>>
characteristics_ ABSL_GUARDED_BY(mutex_);
absl::flat_hash_map<SubscriberKey, SubscriberCallback> subscribers_
ABSL_GUARDED_BY(mutex_);
std::vector<GattClient*> connected_clients_ ABSL_GUARDED_BY(mutex_);
std::atomic_bool stopped_ = false;
Lender<api::ble_v2::GattServer*> lender_{this};
Lender<api::ble::GattServer*> lender_{this};
};
// A concrete implementation for GattClient.
class GattClient : public api::ble_v2::GattClient {
class GattClient : public api::ble::GattClient {
public:
GattClient(api::ble_v2::BlePeripheral::UniqueId peripheral_id,
Borrowable<api::ble_v2::GattServer*> gatt_server,
api::ble_v2::ClientGattConnectionCallback callback);
GattClient(api::ble::BlePeripheral::UniqueId peripheral_id,
Borrowable<api::ble::GattServer*> gatt_server,
api::ble::ClientGattConnectionCallback callback);
~GattClient() override;
bool DiscoverServiceAndCharacteristics(
const Uuid& service_uuid,
const std::vector<Uuid>& characteristic_uuids) override;
std::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
std::optional<api::ble::GattCharacteristic> GetCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid) override;
std::optional<std::string> ReadCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic) override;
const api::ble::GattCharacteristic& characteristic) override;
bool WriteCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
const api::ble::GattCharacteristic& characteristic,
absl::string_view value,
api::ble_v2::GattClient::WriteType write_type) override;
api::ble::GattClient::WriteType write_type) override;
bool SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
const api::ble::GattCharacteristic& characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)>
on_characteristic_changed_cb) override;
@@ -297,15 +294,15 @@ 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::UniqueId peripheral_id_;
Borrowable<api::ble_v2::GattServer*> gatt_server_;
api::ble_v2::ClientGattConnectionCallback callback_;
api::ble::BlePeripheral::UniqueId peripheral_id_;
Borrowable<api::ble::GattServer*> gatt_server_;
api::ble::ClientGattConnectionCallback callback_;
};
bool IsStopped(Borrowable<api::ble_v2::GattServer*> server);
bool IsStopped(Borrowable<api::ble::GattServer*> server);
absl::Mutex mutex_;
BluetoothAdapter& adapter_; // Our device adapter; read-only.
absl::flat_hash_map<std::string, BleV2ServerSocket*> server_sockets_
absl::flat_hash_map<std::string, BleServerSocket*> server_sockets_
ABSL_GUARDED_BY(mutex_);
absl::flat_hash_set<std::pair<Uuid, std::uint32_t>>
scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_);
@@ -316,4 +313,4 @@ class BleV2Medium : public api::ble_v2::BleMedium {
} // namespace g3
} // namespace nearby
#endif // PLATFORM_IMPL_G3_BLE_V2_H_
#endif // PLATFORM_IMPL_G3_BLE_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/g3/ble_v2.h"
#include "internal/platform/implementation/g3/ble.h"
#include <string>
@@ -22,7 +22,7 @@
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
@@ -53,117 +53,115 @@ class MockBluetoothAdapter : public BluetoothAdapter {
MOCK_METHOD(api::BluetoothAdapter::ScanMode, GetScanMode, (),
(const, override));
MOCK_METHOD(int, GetNumSlots, (), (const));
MOCK_METHOD(api::ble_v2::BlePeripheral::UniqueId, GetUniqueId, (), (const));
MOCK_METHOD(void, SetBleV2Medium, (api::ble_v2::BleMedium * medium));
MOCK_METHOD(api::ble::BlePeripheral::UniqueId, GetUniqueId, (), (const));
MOCK_METHOD(void, SetBleMedium, (api::ble::BleMedium * medium));
};
TEST(BleV2MediumTest, StartAdvertising) {
TEST(BleMediumTest, StartAdvertising) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
api::ble_v2::BleAdvertisementData advertising_data;
BleMedium ble_medium(mock_bluetooth_adapter);
api::ble::BleAdvertisementData advertising_data;
advertising_data.is_extended_advertisement = false;
advertising_data.service_data.insert(
{Uuid(kTestUuid), ByteArray("test_service_data")});
auto advertising_session = ble_v2_medium.StartAdvertising(
auto advertising_session = ble_medium.StartAdvertising(
advertising_data,
{.tx_power_level = api::ble_v2::TxPowerLevel::kLow,
.is_connectable = true},
api::ble_v2::BleMedium::AdvertisingCallback{
{.tx_power_level = api::ble::TxPowerLevel::kLow, .is_connectable = true},
api::ble::BleMedium::AdvertisingCallback{
.start_advertising_result = [](absl::Status) {},
});
EXPECT_TRUE(advertising_session != nullptr);
}
TEST(BleV2MediumTest, StopAdvertising) {
TEST(BleMediumTest, StopAdvertising) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
EXPECT_TRUE(ble_v2_medium.StopAdvertising());
BleMedium ble_medium(mock_bluetooth_adapter);
EXPECT_TRUE(ble_medium.StopAdvertising());
}
TEST(BleV2MediumTest, StartScanning) {
TEST(BleMediumTest, StartScanning) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
auto scanning_session = ble_v2_medium.StartScanning(
Uuid(kTestUuid), api::ble_v2::TxPowerLevel::kLow,
api::ble_v2::BleMedium::ScanningCallback{
BleMedium ble_medium(mock_bluetooth_adapter);
auto scanning_session = ble_medium.StartScanning(
Uuid(kTestUuid), api::ble::TxPowerLevel::kLow,
api::ble::BleMedium::ScanningCallback{
.start_scanning_result = [](absl::Status) {},
.advertisement_found_cb = [](api::ble_v2::BlePeripheral::UniqueId,
api::ble_v2::BleAdvertisementData) {},
.advertisement_found_cb = [](api::ble::BlePeripheral::UniqueId,
api::ble::BleAdvertisementData) {},
});
EXPECT_TRUE(scanning_session != nullptr);
}
TEST(BleV2MediumTest, StopScanning) {
TEST(BleMediumTest, StopScanning) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
EXPECT_TRUE(ble_v2_medium.StopScanning());
BleMedium ble_medium(mock_bluetooth_adapter);
EXPECT_TRUE(ble_medium.StopScanning());
}
TEST(BleV2MediumTest, StartGattServer) {
TEST(BleMediumTest, StartGattServer) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
auto gatt_server = ble_v2_medium.StartGattServer({});
BleMedium ble_medium(mock_bluetooth_adapter);
auto gatt_server = ble_medium.StartGattServer({});
EXPECT_TRUE(gatt_server != nullptr);
}
TEST(BleV2MediumTest, ConnectToGattServer) {
TEST(BleMediumTest, ConnectToGattServer) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
auto gatt_client = ble_v2_medium.ConnectToGattServer(
/*peripheral_id=*/1234, api::ble_v2::TxPowerLevel::kLow, {});
BleMedium ble_medium(mock_bluetooth_adapter);
auto gatt_client = ble_medium.ConnectToGattServer(
/*peripheral_id=*/1234, api::ble::TxPowerLevel::kLow, {});
EXPECT_TRUE(gatt_client == nullptr);
}
TEST(BleV2MediumTest, OpenServerSocket) {
TEST(BleMediumTest, OpenServerSocket) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
auto server_socket =
ble_v2_medium.OpenServerSocket(std::string(kTestServiceId));
BleMedium ble_medium(mock_bluetooth_adapter);
auto server_socket = ble_medium.OpenServerSocket(std::string(kTestServiceId));
EXPECT_TRUE(server_socket != nullptr);
}
TEST(BleV2MediumTest, Connect) {
TEST(BleMediumTest, Connect) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
auto socket = ble_v2_medium.Connect(
std::string(kTestServiceId), api::ble_v2::TxPowerLevel::kLow,
BleMedium ble_medium(mock_bluetooth_adapter);
auto socket = ble_medium.Connect(
std::string(kTestServiceId), api::ble::TxPowerLevel::kLow,
/*remote_peripheral_id=*/1234, /*cancellation_flag=*/nullptr);
EXPECT_TRUE(socket == nullptr);
}
TEST(BleV2MediumTest, OpenL2capServerSocket) {
TEST(BleMediumTest, OpenL2capServerSocket) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
BleMedium ble_medium(mock_bluetooth_adapter);
auto server_socket =
ble_v2_medium.OpenL2capServerSocket(std::string(kTestServiceId));
ble_medium.OpenL2capServerSocket(std::string(kTestServiceId));
EXPECT_TRUE(server_socket == nullptr);
}
TEST(BleV2MediumTest, IsExtendedAdvertisementsAvailable) {
TEST(BleMediumTest, IsExtendedAdvertisementsAvailable) {
MockBluetoothAdapter mock_bluetooth_adapter;
EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true));
BleV2Medium ble_v2_medium(mock_bluetooth_adapter);
EXPECT_FALSE(ble_v2_medium.IsExtendedAdvertisementsAvailable());
BleMedium ble_medium(mock_bluetooth_adapter);
EXPECT_FALSE(ble_medium.IsExtendedAdvertisementsAvailable());
}
} // namespace
@@ -20,7 +20,7 @@
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/mac_address.h"
#include "internal/platform/medium_environment.h"
@@ -60,8 +60,8 @@ void BluetoothAdapter::SetBluetoothClassicMedium(
bluetooth_classic_medium_ = medium;
}
void BluetoothAdapter::SetBleV2Medium(api::ble_v2::BleMedium* medium) {
ble_v2_medium_ = medium;
void BluetoothAdapter::SetBleMedium(api::ble::BleMedium* medium) {
ble_medium_ = medium;
}
bool BluetoothAdapter::SetStatus(Status status) {
@@ -21,7 +21,7 @@
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/mac_address.h"
@@ -89,7 +89,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
// Returns BT MAC address assigned to this adapter.
std::string GetMacAddress() const override { return mac_address_.ToString(); }
MacAddress GetAddress() const override { return mac_address_; }
MacAddress GetAddress() const override { return mac_address_; }
BluetoothDevice& GetDevice() { return device_; }
@@ -98,12 +98,10 @@ class BluetoothAdapter : public api::BluetoothAdapter {
return bluetooth_classic_medium_;
}
void SetBleV2Medium(api::ble_v2::BleMedium* medium);
api::ble_v2::BleMedium* GetBleV2Medium() { return ble_v2_medium_; }
void SetBleMedium(api::ble::BleMedium* medium);
api::ble::BleMedium* GetBleMedium() { return ble_medium_; }
void SetMacAddress(MacAddress mac_address) {
mac_address_ = mac_address;
}
void SetMacAddress(MacAddress mac_address) { mac_address_ = mac_address; }
std::uint64_t GetUniqueId() { return unique_id_; }
@@ -111,7 +109,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
mutable absl::Mutex mutex_;
BluetoothDevice device_{this};
api::BluetoothClassicMedium* bluetooth_classic_medium_ = nullptr;
api::ble_v2::BleMedium* ble_v2_medium_ = nullptr;
api::ble::BleMedium* ble_medium_ = nullptr;
MacAddress mac_address_;
ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone;
std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device";
@@ -29,7 +29,7 @@
#include "internal/platform/implementation/atomic_boolean.h"
#include "internal/platform/implementation/atomic_reference.h"
#include "internal/platform/implementation/awdl.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/condition_variable.h"
@@ -61,7 +61,7 @@
#endif
#include "internal/platform/implementation/g3/atomic_boolean.h"
#include "internal/platform/implementation/g3/atomic_reference.h"
#include "internal/platform/implementation/g3/ble_v2.h"
#include "internal/platform/implementation/g3/ble.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/bluetooth_classic.h"
#include "internal/platform/implementation/g3/condition_variable.h"
@@ -185,9 +185,9 @@ ImplementationPlatform::CreateBluetoothClassicMedium(
return std::make_unique<g3::BluetoothClassicMedium>(adapter);
}
std::unique_ptr<api::ble_v2::BleMedium>
ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter& adapter) {
return std::make_unique<g3::BleV2Medium>(
std::unique_ptr<api::ble::BleMedium>
ImplementationPlatform::CreateBleMedium(api::BluetoothAdapter& adapter) {
return std::make_unique<g3::BleMedium>(
dynamic_cast<g3::BluetoothAdapter&>(adapter));
}
+2 -2
View File
@@ -24,7 +24,7 @@
#include "internal/platform/implementation/atomic_boolean.h"
#include "internal/platform/implementation/atomic_reference.h"
#include "internal/platform/implementation/awdl.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/condition_variable.h"
@@ -130,7 +130,7 @@ class ImplementationPlatform {
static std::unique_ptr<BluetoothAdapter> CreateBluetoothAdapter();
static std::unique_ptr<BluetoothClassicMedium> CreateBluetoothClassicMedium(
BluetoothAdapter&);
static std::unique_ptr<api::ble_v2::BleMedium> CreateBleV2Medium(
static std::unique_ptr<api::ble::BleMedium> CreateBleMedium(
api::BluetoothAdapter&);
static std::unique_ptr<api::CredentialStorage> CreateCredentialStorage();
static std::unique_ptr<ServerSyncMedium> CreateServerSyncMedium();
@@ -101,11 +101,11 @@ cc_library(
cc_library(
name = "comm",
hdrs = [
"ble.h",
"ble_gatt_client.h",
"ble_gatt_server.h",
"ble_v2.h",
"ble_v2_server_socket.h",
"ble_v2_socket.h",
"ble_server_socket.h",
"ble_socket.h",
"bluetooth_adapter.h",
"bluetooth_classic.h",
"bluetooth_classic_device.h",
@@ -202,11 +202,11 @@ cc_library(
cc_library(
name = "windows",
srcs = [
"ble.cc",
"ble_gatt_client.cc",
"ble_gatt_server.cc",
"ble_v2.cc",
"ble_v2_server_socket.cc",
"ble_v2_socket.cc",
"ble_server_socket.cc",
"ble_socket.cc",
"bluetooth_adapter.cc",
"bluetooth_classic_device.cc",
"bluetooth_classic_medium.cc",
@@ -271,8 +271,8 @@ cc_library(
":types",
"//connections/implementation/flags:connections_flags",
"//connections/implementation/mediums:utils",
"//connections/implementation/mediums/ble_v2:ble_advertisement_header",
"//connections/implementation/mediums/ble_v2:bloom_filter",
"//connections/implementation/mediums/ble:ble_advertisement_header",
"//connections/implementation/mediums/ble:bloom_filter",
"//internal/account",
"//internal/base:file_path",
"//internal/base:files",
@@ -336,7 +336,7 @@ cc_test(
"atomic_boolean_test.cc",
"atomic_reference_test.cc",
"ble_gatt_server_test.cc",
"ble_v2_test.cc",
"ble_test.cc",
"bluetooth_adapter_test.cc",
"count_down_latch_test.cc",
"crypto_test.cc",
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/ble_v2.h"
#include "internal/platform/implementation/windows/ble.h"
#include <algorithm>
#include <chrono> // NOLINT
@@ -34,20 +34,20 @@
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble_v2/bloom_filter.h"
#include "connections/implementation/mediums/ble/ble_advertisement_header.h"
#include "connections/implementation/mediums/ble/bloom_filter.h"
#include "connections/implementation/mediums/utils.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_gatt_client.h"
#include "internal/platform/implementation/windows/ble_gatt_server.h"
#include "internal/platform/implementation/windows/ble_v2_server_socket.h"
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include "internal/platform/implementation/windows/ble_server_socket.h"
#include "internal/platform/implementation/windows/ble_socket.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
@@ -67,13 +67,13 @@ namespace {
using ::absl::Milliseconds;
using ::absl::Minutes;
using ::nearby::api::ble_v2::AdvertiseParameters;
using ::nearby::api::ble_v2::BleAdvertisementData;
using ::nearby::api::ble_v2::BleServerSocket;
using ::nearby::api::ble_v2::BleSocket;
using ::nearby::api::ble_v2::GattClient;
using ::nearby::api::ble_v2::ServerGattConnectionCallback;
using ::nearby::api::ble_v2::TxPowerLevel;
using ::nearby::api::ble::AdvertiseParameters;
using ::nearby::api::ble::BleAdvertisementData;
using ::nearby::api::ble::BleServerSocket;
using ::nearby::api::ble::BleSocket;
using ::nearby::api::ble::GattClient;
using ::nearby::api::ble::ServerGattConnectionCallback;
using ::nearby::api::ble::TxPowerLevel;
using ::nearby::connections::mediums::BitSetImpl;
using ::nearby::connections::mediums::BleAdvertisementHeader;
using ::nearby::connections::mediums::BloomFilter;
@@ -133,8 +133,7 @@ std::string TxPowerLevelToName(TxPowerLevel tx_power_level) {
// Use the device MAC address as the advertisement hash so that we have a unique
// advertisement header for each device.
BleAdvertisementHeader CreateAdvertisementHeader(
MacAddress mac_address,
const std::vector<std::string>& service_ids) {
MacAddress mac_address, const std::vector<std::string>& service_ids) {
BloomFilter bloom_filter(
std::make_unique<BitSetImpl<
BleAdvertisementHeader::kServiceIdBloomFilterByteLength>>());
@@ -165,9 +164,9 @@ constexpr absl::Duration kMediumCheckInterval = Milliseconds(50);
constexpr uint8_t kUuid16ServiceDataType = 0x16;
} // namespace
BleV2Medium::AdvertisementWatcher::~AdvertisementWatcher() { Stop(); }
BleMedium::AdvertisementWatcher::~AdvertisementWatcher() { Stop(); }
void BleV2Medium::AdvertisementWatcher::Initialize(
void BleMedium::AdvertisementWatcher::Initialize(
const TypedEventHandler<BluetoothLEAdvertisementWatcher,
BluetoothLEAdvertisementReceivedEventArgs>&
received_handler,
@@ -199,7 +198,7 @@ void BleV2Medium::AdvertisementWatcher::Initialize(
}
}
void BleV2Medium::AdvertisementWatcher::InitializeWithServiceFilter(
void BleMedium::AdvertisementWatcher::InitializeWithServiceFilter(
uint16_t service_uuid,
const TypedEventHandler<BluetoothLEAdvertisementWatcher,
BluetoothLEAdvertisementReceivedEventArgs>&
@@ -240,7 +239,7 @@ void BleV2Medium::AdvertisementWatcher::InitializeWithServiceFilter(
}
}
bool BleV2Medium::AdvertisementWatcher::Start() {
bool BleMedium::AdvertisementWatcher::Start() {
absl::MutexLock lock(&mutex_);
if (!initialized_) {
LOG(ERROR) << __func__ << ": Failed to start BLE watcher: not initialized.";
@@ -277,7 +276,7 @@ bool BleV2Medium::AdvertisementWatcher::Start() {
return true;
}
void BleV2Medium::AdvertisementWatcher::Stop() {
void BleMedium::AdvertisementWatcher::Stop() {
absl::MutexLock lock(&mutex_);
try {
watcher_.Stop();
@@ -300,12 +299,12 @@ void BleV2Medium::AdvertisementWatcher::Stop() {
}
}
BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter)
BleMedium::BleMedium(api::BluetoothAdapter& adapter)
: adapter_(dynamic_cast<BluetoothAdapter*>(&adapter)) {}
// Advertisement packet and populate accordingly.
bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data,
AdvertiseParameters advertising_parameters) {
bool BleMedium::StartAdvertising(const BleAdvertisementData& advertising_data,
AdvertiseParameters advertising_parameters) {
absl::MutexLock lock(&mutex_);
std::string service_data_info;
for (const auto& it : advertising_data.service_data) {
@@ -321,14 +320,14 @@ bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data,
if (advertising_data.is_extended_advertisement ||
ble_gatt_server_ == nullptr) {
// In BLE v2, the flag is set when the Bluetooth adapter supports extended
// In BLE, the flag is set when the Bluetooth adapter supports extended
// advertising and GATT server is using.
return StartBleAdvertising(advertising_data, advertising_parameters);
}
return StartGattAdvertising(advertising_data, advertising_parameters);
}
bool BleV2Medium::StopAdvertising() {
bool BleMedium::StopAdvertising() {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__ << ": Stop advertising.";
bool result = true;
@@ -353,10 +352,10 @@ bool BleV2Medium::StopAdvertising() {
}
// TODO(hais) manually verify the new api before switching async flags.
std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters,
BleV2Medium::AdvertisingCallback callback) {
std::unique_ptr<BleMedium::AdvertisingSession> BleMedium::StartAdvertising(
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters,
BleMedium::AdvertisingCallback callback) {
LOG(INFO) << __func__ << ": advertising_data.is_extended_advertisement="
<< advertising_data.is_extended_advertisement
<< ", advertising_data.service_data size="
@@ -375,8 +374,8 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
return nullptr;
}
return std::make_unique<BleV2Medium::AdvertisingSession>(
BleV2Medium::AdvertisingSession{
return std::make_unique<BleMedium::AdvertisingSession>(
BleMedium::AdvertisingSession{
.stop_advertising =
[this]() {
if (StopAdvertising()) {
@@ -388,12 +387,12 @@ std::unique_ptr<BleV2Medium::AdvertisingSession> BleV2Medium::StartAdvertising(
});
}
std::unique_ptr<BleV2Medium::AdvertisementWatcher>
BleV2Medium::CreateBleWatcher(uint16_t service_uuid) {
std::unique_ptr<BleMedium::AdvertisementWatcher> BleMedium::CreateBleWatcher(
uint16_t service_uuid) {
auto watcher = std::make_unique<AdvertisementWatcher>();
watcher->InitializeWithServiceFilter(
service_uuid, {this, &BleV2Medium::AdvertisementReceivedHandler},
{this, &BleV2Medium::WatcherHandler});
service_uuid, {this, &BleMedium::AdvertisementReceivedHandler},
{this, &BleMedium::WatcherHandler});
if (!watcher->Start()) {
return nullptr;
@@ -403,9 +402,9 @@ BleV2Medium::CreateBleWatcher(uint16_t service_uuid) {
return watcher;
}
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
TxPowerLevel tx_power_level,
ScanCallback callback) {
bool BleMedium::StartScanning(const Uuid& service_uuid,
TxPowerLevel tx_power_level,
ScanCallback callback) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__ << ": service UUID: " << std::string(service_uuid)
<< ", TxPowerLevel: " << TxPowerLevelToName(tx_power_level);
@@ -463,9 +462,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
return true;
}
std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
std::unique_ptr<BleMedium::ScanningSession> BleMedium::StartScanning(
const Uuid& service_uuid, TxPowerLevel tx_power_level,
BleV2Medium::ScanningCallback callback) {
BleMedium::ScanningCallback callback) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__ << ": service UUID: " << std::string(service_uuid)
<< ", TxPowerLevel: " << TxPowerLevelToName(tx_power_level);
@@ -478,8 +477,8 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
if (watchers_.empty()) {
LOG(WARNING) << __func__ << ": Starting BLE Scanning.";
auto watcher = std::make_unique<AdvertisementWatcher>();
watcher->Initialize({this, &BleV2Medium::AdvertisementFoundHandler},
{this, &BleV2Medium::WatcherHandler});
watcher->Initialize({this, &BleMedium::AdvertisementFoundHandler},
{this, &BleMedium::WatcherHandler});
if (!watcher->Start()) {
return nullptr;
}
@@ -499,8 +498,8 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
}
// Generate and return ScanningSession.
return std::make_unique<BleV2Medium::ScanningSession>(
BleV2Medium::ScanningSession{
return std::make_unique<BleMedium::ScanningSession>(
BleMedium::ScanningSession{
.stop_scanning =
[this, session_id, service_uuid]() {
size_t num_erased_from_service_and_session_map = 0u;
@@ -552,8 +551,8 @@ std::unique_ptr<BleV2Medium::ScanningSession> BleV2Medium::StartScanning(
});
}
std::unique_ptr<api::ble_v2::GattServer> BleV2Medium::StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) {
std::unique_ptr<api::ble::GattServer> BleMedium::StartGattServer(
api::ble::ServerGattConnectionCallback callback) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__ << ": Start GATT server.";
@@ -573,10 +572,10 @@ std::unique_ptr<api::ble_v2::GattServer> BleV2Medium::StartGattServer(
return gatt_server;
}
std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
std::unique_ptr<api::ble::GattClient> BleMedium::ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id,
TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) {
api::ble::ClientGattConnectionCallback callback) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << "ConnectToGattServer is called with peripheral id: "
<< peripheral_id
@@ -585,8 +584,7 @@ std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
try {
// In windows, peripheral unique id is the same as the bluetooth address.
BluetoothLEDevice ble_device =
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral_id)
.get();
BluetoothLEDevice::FromBluetoothAddressAsync(peripheral_id).get();
return std::make_unique<BleGattClient>(ble_device);
} catch (std::exception exception) {
@@ -601,7 +599,7 @@ std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
return nullptr;
}
bool BleV2Medium::StopScanning() {
bool BleMedium::StopScanning() {
absl::MutexLock lock(&mutex_);
alternate_uuids_for_service_.clear();
@@ -631,11 +629,11 @@ bool BleV2Medium::StopScanning() {
return true;
}
std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> BleMedium::OpenServerSocket(
const std::string& service_id) {
VLOG(1) << "OpenServerSocket is called";
auto server_socket = std::make_unique<BleV2ServerSocket>(adapter_);
auto server_socket = std::make_unique<windows::BleServerSocket>(adapter_);
if (!server_socket->Bind()) {
LOG(ERROR) << __func__ << ": Failed to bing socket.";
@@ -645,9 +643,9 @@ std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
return server_socket;
}
std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
std::unique_ptr<api::ble::BleSocket> BleMedium::Connect(
const std::string& service_id, TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
api::ble::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) {
LOG(INFO) << __func__ << ": Connect to service_id=" << service_id;
@@ -662,7 +660,7 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
return nullptr;
}
auto ble_socket = std::make_unique<BleV2Socket>();
auto ble_socket = std::make_unique<windows::BleSocket>();
nearby::CancellationFlagListener cancellation_flag_listener(
cancellation_flag, [socket = ble_socket.get()]() { socket->Close(); });
@@ -676,13 +674,13 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(
return ble_socket;
}
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
bool BleMedium::IsExtendedAdvertisementsAvailable() {
return adapter_->IsExtendedAdvertisingSupported();
}
bool BleV2Medium::StartBleAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertising_parameters) {
bool BleMedium::StartBleAdvertising(
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertising_parameters) {
LOG(INFO) << __func__ << ": Start BLE advertising, extended:"
<< advertising_data.is_extended_advertisement;
try {
@@ -762,7 +760,7 @@ bool BleV2Medium::StartBleAdvertising(
publisher_.UseExtendedAdvertisement(false);
}
publisher_token_ =
publisher_.StatusChanged({this, &BleV2Medium::PublisherHandler});
publisher_.StatusChanged({this, &BleMedium::PublisherHandler});
publisher_.Start();
@@ -802,7 +800,7 @@ bool BleV2Medium::StartBleAdvertising(
}
}
bool BleV2Medium::StopBleAdvertising() {
bool BleMedium::StopBleAdvertising() {
LOG(INFO) << __func__ << ": Stop BLE advertising.";
try {
if (!is_ble_publisher_started_) {
@@ -863,9 +861,9 @@ bool BleV2Medium::StopBleAdvertising() {
return false;
}
bool BleV2Medium::StartGattAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertising_parameters) {
bool BleMedium::StartGattAdvertising(
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertising_parameters) {
LOG(INFO) << __func__ << ": Start GATT advertising.";
try {
if (!adapter_->IsEnabled()) {
@@ -927,7 +925,7 @@ bool BleV2Medium::StartGattAdvertising(
}
}
bool BleV2Medium::StopGattAdvertising() {
bool BleMedium::StopGattAdvertising() {
try {
LOG(INFO) << __func__ << ": Stop GATT advertising.";
@@ -968,7 +966,7 @@ bool BleV2Medium::StopGattAdvertising() {
return false;
}
void BleV2Medium::PublisherHandler(
void BleMedium::PublisherHandler(
BluetoothLEAdvertisementPublisher publisher,
BluetoothLEAdvertisementPublisherStatusChangedEventArgs args) {
// This method is called when publisher's status is changed.
@@ -1049,7 +1047,7 @@ void BleV2Medium::PublisherHandler(
}
}
void BleV2Medium::WatcherHandler(
void BleMedium::WatcherHandler(
BluetoothLEAdvertisementWatcher watcher,
BluetoothLEAdvertisementWatcherStoppedEventArgs args) {
// This method is called when watcher stopped. Args give more detailed
@@ -1096,7 +1094,7 @@ void BleV2Medium::WatcherHandler(
}
}
void BleV2Medium::AdvertisementReceivedHandler(
void BleMedium::AdvertisementReceivedHandler(
BluetoothLEAdvertisementWatcher watcher,
BluetoothLEAdvertisementReceivedEventArgs args) {
// Handle all BLE advertisements and determine whether the BLE Medium
@@ -1139,7 +1137,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
<< " peripheral address: " << bluetooth_address.ToString()
<< " unconsumed_buffer_length: " << unconsumed_buffer_length;
api::ble_v2::BleAdvertisementData ble_advertisement_data;
api::ble::BleAdvertisementData ble_advertisement_data;
if (unconsumed_buffer_length <= 27) {
ble_advertisement_data.is_extended_advertisement = false;
} else {
@@ -1166,7 +1164,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
<< bluetooth_address.ToString()
<< " for services: " << absl::StrJoin(alt_service_ids, ",");
// Create fake advertisement data.
api::ble_v2::BleAdvertisementData ble_advertisement_data;
api::ble::BleAdvertisementData ble_advertisement_data;
ble_advertisement_data.is_extended_advertisement = false;
BleAdvertisementHeader header =
CreateAdvertisementHeader(bluetooth_address, alt_service_ids);
@@ -1176,7 +1174,7 @@ void BleV2Medium::AdvertisementReceivedHandler(
}
}
void BleV2Medium::AdvertisementFoundHandler(
void BleMedium::AdvertisementFoundHandler(
BluetoothLEAdvertisementWatcher watcher,
BluetoothLEAdvertisementReceivedEventArgs args) {
BluetoothLEAdvertisement advertisement = args.Advertisement();
@@ -1199,7 +1197,7 @@ void BleV2Medium::AdvertisementFoundHandler(
}
// Construct BleAdvertisementData from the windows Advertisement.
api::ble_v2::BleAdvertisementData ble_advertisement_data;
api::ble::BleAdvertisementData ble_advertisement_data;
for (BluetoothLEAdvertisementDataSection service_data :
advertisement.GetSectionsByType(kUuid16ServiceDataType)) {
// Parse Advertisement Data for Section 0x16 (Service Data)
@@ -1260,7 +1258,7 @@ void BleV2Medium::AdvertisementFoundHandler(
}
}
uint64_t BleV2Medium::GenerateSessionId() {
uint64_t BleMedium::GenerateSessionId() {
for (int i = 0; i < kGenerateSessionIdRetryLimit; i++) {
uint64_t session_id = Prng().NextInt64();
if (session_id == kFailedGenerateSessionId) continue;
@@ -1272,8 +1270,8 @@ uint64_t BleV2Medium::GenerateSessionId() {
return kFailedGenerateSessionId;
}
void BleV2Medium::AddAlternateUuidForService(uint16_t uuid,
const std::string& service_id) {
void BleMedium::AddAlternateUuidForService(uint16_t uuid,
const std::string& service_id) {
absl::MutexLock lock(&mutex_);
alternate_uuids_for_service_[uuid] = service_id;
}
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_
#include <algorithm>
#include <cstdint>
@@ -25,7 +25,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_gatt_server.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
@@ -36,44 +36,43 @@ namespace nearby {
namespace windows {
// Container of operations that can be performed over the BLE medium.
class BleV2Medium : public api::ble_v2::BleMedium {
class BleMedium : public api::ble::BleMedium {
public:
explicit BleV2Medium(api::BluetoothAdapter& adapter);
~BleV2Medium() override = default;
explicit BleMedium(api::BluetoothAdapter& adapter);
~BleMedium() override = default;
// Returns true once the Ble advertising has been initiated.
bool StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertising_parameters) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StartAdvertising(const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertising_parameters)
override ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<AdvertisingSession> StartAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertise_set_parameters,
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertise_set_parameters,
AdvertisingCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_);
bool StartScanning(const Uuid& service_uuid,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble::TxPowerLevel tx_power_level,
ScanCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopScanning() override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<ScanningSession> StartScanning(
const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level,
ScanningCallback callback) override;
std::unique_ptr<api::ble_v2::GattServer> StartGattServer(
api::ble_v2::ServerGattConnectionCallback callback) override
std::unique_ptr<api::ble::GattServer> StartGattServer(
api::ble::ServerGattConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::ClientGattConnectionCallback callback) override
std::unique_ptr<api::ble::GattClient> ConnectToGattServer(
api::ble::BlePeripheral::UniqueId peripheral_id,
api::ble::TxPowerLevel tx_power_level,
api::ble::ClientGattConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::BleServerSocket> OpenServerSocket(
std::unique_ptr<api::ble::BleServerSocket> OpenServerSocket(
const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_);
std::unique_ptr<api::ble_v2::BleSocket> Connect(
const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id,
std::unique_ptr<api::ble::BleSocket> Connect(
const std::string& service_id, api::ble::TxPowerLevel tx_power_level,
api::ble::BlePeripheral::UniqueId remote_peripheral_id,
CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_);
bool IsExtendedAdvertisementsAvailable() override;
@@ -81,7 +80,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
const std::string& service_id) override;
private:
friend class BleV2MediumTest;
friend class BleMediumTest;
// A wrapper class for BluetoothLEAdvertisementWatcher.
class AdvertisementWatcher {
@@ -136,14 +135,14 @@ class BleV2Medium : public api::ble_v2::BleMedium {
};
bool StartBleAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertising_parameters)
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertising_parameters)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool StopBleAdvertising() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool StartGattAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
api::ble_v2::AdvertiseParameters advertising_parameters)
const api::ble::BleAdvertisementData& advertising_data,
api::ble::AdvertiseParameters advertising_parameters)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool StopGattAdvertising() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -172,7 +171,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
uint64_t GenerateSessionId() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
std::unique_ptr<BleV2Medium::AdvertisementWatcher> CreateBleWatcher(
std::unique_ptr<BleMedium::AdvertisementWatcher> CreateBleWatcher(
uint16_t service_uuid);
absl::Mutex mutex_;
@@ -180,7 +179,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
BluetoothAdapter* const adapter_;
Uuid service_uuid_;
uint16_t service_uuid16_ = 0;
api::ble_v2::TxPowerLevel tx_power_level_;
api::ble::TxPowerLevel tx_power_level_;
ScanCallback scan_callback_;
std::vector<std::unique_ptr<AdvertisementWatcher>> watchers_
ABSL_GUARDED_BY(mutex_);
@@ -214,4 +213,4 @@ class BleV2Medium : public api::ble_v2::BleMedium {
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_
@@ -36,7 +36,7 @@
#include "internal/flags/nearby_flags.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/flags/nearby_platform_feature_flags.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
@@ -79,9 +79,9 @@ using ::winrt::Windows::Foundation::Collections::IVectorView;
using ::winrt::Windows::Storage::Streams::Buffer;
using ::winrt::Windows::Storage::Streams::DataReader;
using ::winrt::Windows::Storage::Streams::IBuffer;
using Property = api::ble_v2::GattCharacteristic::Property;
using Permission = api::ble_v2::GattCharacteristic::Permission;
using WriteType = api::ble_v2::GattClient::WriteType;
using Property = api::ble::GattCharacteristic::Property;
using Permission = api::ble::GattCharacteristic::Permission;
using WriteType = api::ble::GattClient::WriteType;
constexpr int kGattTimeoutInSeconds = 5;
@@ -269,9 +269,8 @@ bool BleGattClient::DiscoverServiceAndCharacteristics(
return false;
}
absl::optional<api::ble_v2::GattCharacteristic>
BleGattClient::GetCharacteristic(const Uuid& service_uuid,
const Uuid& characteristic_uuid) {
absl::optional<api::ble::GattCharacteristic> BleGattClient::GetCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid) {
absl::MutexLock lock(&mutex_);
VLOG(1) << __func__ << ": Stared to get characteristic UUID="
<< std::string(characteristic_uuid)
@@ -285,12 +284,12 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid,
return absl::nullopt;
}
api::ble_v2::GattCharacteristic result;
api::ble::GattCharacteristic result;
result.service_uuid = service_uuid;
result.uuid = characteristic_uuid;
// Note: Windows has protection level on GattCharacteristic. cannot
// find a way to map it to api::ble_v2::GattCharacteristic.
// find a way to map it to api::ble::GattCharacteristic.
GattCharacteristicProperties properties =
gatt_characteristic->CharacteristicProperties();
result.permission = Permission::kNone;
@@ -336,7 +335,7 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid,
}
absl::optional<std::string> BleGattClient::ReadCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic) {
const api::ble::GattCharacteristic& characteristic) {
absl::MutexLock lock(&mutex_);
VLOG(1) << __func__
<< ": Read characteristic=" << std::string(characteristic.uuid);
@@ -390,8 +389,8 @@ absl::optional<std::string> BleGattClient::ReadCharacteristic(
}
bool BleGattClient::WriteCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
absl::string_view value, api::ble_v2::GattClient::WriteType write_type) {
const api::ble::GattCharacteristic& characteristic, absl::string_view value,
api::ble::GattClient::WriteType write_type) {
absl::MutexLock lock(&mutex_);
VLOG(1) << __func__
<< ": write characteristic: " << std::string(characteristic.uuid);
@@ -441,7 +440,7 @@ bool BleGattClient::WriteCharacteristic(
}
bool BleGattClient::SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
const api::ble::GattCharacteristic& characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)>
on_characteristic_changed_cb) {
absl::MutexLock lock(&mutex_);
@@ -629,7 +628,7 @@ bool BleGattClient::WriteCharacteristicConfigurationDescriptor(
}
void BleGattClient::OnCharacteristicValueChanged(
const api::ble_v2::GattCharacteristic& characteristic,
const api::ble::GattCharacteristic& characteristic,
GattValueChangedEventArgs args) {
VLOG(1) << __func__ << ": Gatt Characteristic value changed.";
IBuffer buffer = args.CharacteristicValue();
@@ -30,7 +30,7 @@
#include "absl/synchronization/mutex.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/uuid.h"
#include "winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h"
#include "winrt/Windows.Devices.Bluetooth.h"
@@ -39,7 +39,7 @@
namespace nearby {
namespace windows {
class BleGattClient : public api::ble_v2::GattClient {
class BleGattClient : public api::ble::GattClient {
public:
explicit BleGattClient(
::winrt::Windows::Devices::Bluetooth::BluetoothLEDevice ble_device);
@@ -50,22 +50,21 @@ class BleGattClient : public api::ble_v2::GattClient {
const std::vector<Uuid>& characteristic_uuids) override
ABSL_LOCKS_EXCLUDED(mutex_);
absl::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
absl::optional<api::ble::GattCharacteristic> GetCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid) override
ABSL_LOCKS_EXCLUDED(mutex_);
absl::optional<std::string> ReadCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic) override
const api::ble::GattCharacteristic& characteristic) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool WriteCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
absl::string_view value,
api::ble_v2::GattClient::WriteType write_type) override
bool WriteCharacteristic(const api::ble::GattCharacteristic& characteristic,
absl::string_view value,
api::ble::GattClient::WriteType write_type) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool SetCharacteristicSubscription(
const api::ble_v2::GattCharacteristic& characteristic, bool enable,
const api::ble::GattCharacteristic& characteristic, bool enable,
absl::AnyInvocable<void(absl::string_view value)>
on_characteristic_changed_cb) override ABSL_LOCKS_EXCLUDED(mutex_);
@@ -95,7 +94,7 @@ class BleGattClient : public api::ble_v2::GattClient {
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void OnCharacteristicValueChanged(
const api::ble_v2::GattCharacteristic& characteristic,
const api::ble::GattCharacteristic& characteristic,
::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
GattValueChangedEventArgs args);
@@ -107,7 +106,7 @@ class BleGattClient : public api::ble_v2::GattClient {
GattDeviceServicesResult gatt_devices_services_result_
ABSL_GUARDED_BY(mutex_) = nullptr;
absl::flat_hash_map<api::ble_v2::GattCharacteristic, GattCharacteristicData>
absl::flat_hash_map<api::ble::GattCharacteristic, GattCharacteristicData>
native_characteristic_map_ ABSL_GUARDED_BY(mutex_);
};
@@ -35,7 +35,7 @@
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/utils.h"
@@ -85,8 +85,8 @@ using ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
using ::winrt::Windows::Foundation::Collections::IVectorView;
using ::winrt::Windows::Storage::Streams::Buffer;
using ::winrt::Windows::Storage::Streams::DataWriter;
using Permission = api::ble_v2::GattCharacteristic::Permission;
using Property = api::ble_v2::GattCharacteristic::Property;
using Permission = api::ble::GattCharacteristic::Permission;
using Property = api::ble::GattCharacteristic::Property;
constexpr absl::Duration kGattServerTimeout = absl::Milliseconds(500);
constexpr int kGattServerCheckIntervalInMills = 50;
@@ -113,17 +113,17 @@ std::string ConvertGattStatusToString(
} // namespace
BleGattServer::BleGattServer(api::BluetoothAdapter* adapter,
api::ble_v2::ServerGattConnectionCallback callback)
api::ble::ServerGattConnectionCallback callback)
: adapter_(dynamic_cast<BluetoothAdapter*>(adapter)),
gatt_connection_callback_(std::move(callback)) {
DCHECK(adapter_ != nullptr);
}
absl::optional<api::ble_v2::GattCharacteristic>
absl::optional<api::ble::GattCharacteristic>
BleGattServer::CreateCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) {
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__ << ": create characteristic, service_uuid: "
<< std::string(service_uuid)
@@ -136,7 +136,7 @@ BleGattServer::CreateCharacteristic(
service_uuid_ = service_uuid;
api::ble_v2::GattCharacteristic gatt_characteristic;
api::ble::GattCharacteristic gatt_characteristic;
gatt_characteristic.uuid = characteristic_uuid;
gatt_characteristic.service_uuid = service_uuid;
gatt_characteristic.permission = permission;
@@ -151,7 +151,7 @@ BleGattServer::CreateCharacteristic(
}
bool BleGattServer::UpdateCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
const api::ble::GattCharacteristic& characteristic,
const nearby::ByteArray& value) {
absl::MutexLock lock(&mutex_);
LOG(INFO) << __func__
@@ -192,7 +192,7 @@ bool BleGattServer::UpdateCharacteristic(
}
absl::Status BleGattServer::NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const api::ble::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) {
absl::MutexLock lock(&mutex_);
// Currently, the method is not hooked up at platform layer.
@@ -598,9 +598,8 @@ void BleGattServer::Characteristic_SubscribedClientsChanged(
::winrt::to_hstring(gatt_local_characteristic.Uuid()));
try {
std::vector<api::ble_v2::GattCharacteristic>
added_subscribed_characteristics;
std::vector<api::ble_v2::GattCharacteristic>
std::vector<api::ble::GattCharacteristic> added_subscribed_characteristics;
std::vector<api::ble::GattCharacteristic>
removed_subscribed_characteristics;
GattCharacteristicData* characteristic_data =
@@ -685,7 +684,7 @@ void BleGattServer::ServiceProvider_AdvertisementStatusChanged(
}
void BleGattServer::NotifyValueChanged(
const api::ble_v2::GattCharacteristic& gatt_characteristic) {
const api::ble::GattCharacteristic& gatt_characteristic) {
try {
GattCharacteristicData* characteristic_data =
FindGattCharacteristicData(gatt_characteristic);
@@ -743,7 +742,7 @@ BleGattServer::FindGattCharacteristicData(
BleGattServer::GattCharacteristicData*
BleGattServer::FindGattCharacteristicData(
const api::ble_v2::GattCharacteristic& gatt_characteristic) {
const api::ble::GattCharacteristic& gatt_characteristic) {
for (auto& characteristic_data : gatt_characteristic_datas_) {
if (gatt_characteristic.uuid ==
characteristic_data.gatt_characteristic.uuid) {
@@ -29,7 +29,7 @@
#include "absl/synchronization/notification.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
@@ -41,24 +41,24 @@
namespace nearby {
namespace windows {
class BleGattServer : public api::ble_v2::GattServer {
class BleGattServer : public api::ble::GattServer {
public:
// Make sure the adapter parameter is not null.
BleGattServer(api::BluetoothAdapter* adapter,
api::ble_v2::ServerGattConnectionCallback callback);
api::ble::ServerGattConnectionCallback callback);
~BleGattServer() override = default;
absl::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
absl::optional<api::ble::GattCharacteristic> CreateCharacteristic(
const Uuid& service_uuid, const Uuid& characteristic_uuid,
api::ble_v2::GattCharacteristic::Permission permission,
api::ble_v2::GattCharacteristic::Property property) override
api::ble::GattCharacteristic::Permission permission,
api::ble::GattCharacteristic::Property property) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool UpdateCharacteristic(
const api::ble_v2::GattCharacteristic& characteristic,
const nearby::ByteArray& value) override ABSL_LOCKS_EXCLUDED(mutex_);
bool UpdateCharacteristic(const api::ble::GattCharacteristic& characteristic,
const nearby::ByteArray& value) override
ABSL_LOCKS_EXCLUDED(mutex_);
absl::Status NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const api::ble::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) override ABSL_LOCKS_EXCLUDED(mutex_);
void Stop() override ABSL_LOCKS_EXCLUDED(mutex_);
@@ -73,7 +73,7 @@ class BleGattServer : public api::ble_v2::GattServer {
private:
// Used to save native data related to the GATT characteristic.
struct GattCharacteristicData {
api::ble_v2::GattCharacteristic gatt_characteristic;
api::ble::GattCharacteristic gatt_characteristic;
ByteArray data;
::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
GattLocalCharacteristic local_characteristic = nullptr;
@@ -88,7 +88,7 @@ class BleGattServer : public api::ble_v2::GattServer {
bool InitializeGattServer() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void NotifyValueChanged(
const api::ble_v2::GattCharacteristic& gatt_characteristic)
const api::ble::GattCharacteristic& gatt_characteristic)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
GattCharacteristicData* FindGattCharacteristicData(
@@ -96,7 +96,7 @@ class BleGattServer : public api::ble_v2::GattServer {
GattLocalCharacteristic& gatt_local_characteristic)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
GattCharacteristicData* FindGattCharacteristicData(
const api::ble_v2::GattCharacteristic& gatt_characteristic)
const api::ble::GattCharacteristic& gatt_characteristic)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
::winrt::fire_and_forget Characteristic_ReadRequestedAsync(
@@ -127,7 +127,7 @@ class BleGattServer : public api::ble_v2::GattServer {
absl::Mutex mutex_;
BluetoothAdapter* const adapter_ = nullptr;
api::ble_v2::ServerGattConnectionCallback gatt_connection_callback_{};
api::ble::ServerGattConnectionCallback gatt_connection_callback_{};
::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
GattServiceProvider gatt_service_provider_ ABSL_GUARDED_BY(mutex_) =
@@ -21,7 +21,8 @@
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "absl/time/time.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
namespace nearby {
@@ -30,26 +31,25 @@ namespace {
TEST(BleV2GattServer, DISABLED_StartStopAdvertising) {
BluetoothAdapter bluetoothAdapter;
BleGattServer blev2_gatt_server(&bluetoothAdapter, {});
BleGattServer ble_gatt_server(&bluetoothAdapter, {});
ByteArray
service_data; // {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh}
ByteArray service_data; // {.tx_power_level = api::ble::TxPowerLevel::kHigh}
EXPECT_TRUE(blev2_gatt_server.StartAdvertisement(service_data, true));
EXPECT_TRUE(ble_gatt_server.StartAdvertisement(service_data, true));
EXPECT_TRUE(blev2_gatt_server.StopAdvertisement());
EXPECT_TRUE(ble_gatt_server.StopAdvertisement());
}
TEST(BleV2GattServer, DISABLED_Stop) {
BluetoothAdapter bluetoothAdapter;
BleGattServer blev2_gatt_server(&bluetoothAdapter, {});
BleGattServer ble_gatt_server(&bluetoothAdapter, {});
blev2_gatt_server.Stop();
ble_gatt_server.Stop();
}
TEST(BleV2GattServer, DISABLED_StopNotifierIsCalled) {
BluetoothAdapter bluetoothAdapter;
BleGattServer blev2_gatt_server(&bluetoothAdapter, {});
BleGattServer ble_gatt_server(&bluetoothAdapter, {});
bool is_close_notifier_called = false;
absl::Notification notification;
std::function<void()> notifier = [&is_close_notifier_called,
@@ -57,25 +57,25 @@ TEST(BleV2GattServer, DISABLED_StopNotifierIsCalled) {
is_close_notifier_called = true;
notification.Notify();
};
blev2_gatt_server.SetCloseNotifier(std::move(notifier));
ble_gatt_server.SetCloseNotifier(std::move(notifier));
blev2_gatt_server.Stop();
ble_gatt_server.Stop();
notification.WaitForNotificationWithTimeout(absl::Seconds(1));
EXPECT_TRUE(is_close_notifier_called);
}
TEST(BleV2GattServer, DISABLED_CreateCharacteristic) {
BluetoothAdapter bluetoothAdapter;
BleGattServer blev2_gatt_server(&bluetoothAdapter, {});
BleGattServer ble_gatt_server(&bluetoothAdapter, {});
Uuid service_uuid;
Uuid characteristic_uuid;
const api::ble_v2::GattCharacteristic::Permission permissions =
api::ble_v2::GattCharacteristic::Permission::kNone;
const api::ble_v2::GattCharacteristic::Property properties =
api::ble_v2::GattCharacteristic::Property::kNone;
const api::ble::GattCharacteristic::Permission permissions =
api::ble::GattCharacteristic::Permission::kNone;
const api::ble::GattCharacteristic::Property properties =
api::ble::GattCharacteristic::Property::kNone;
EXPECT_TRUE(blev2_gatt_server
EXPECT_TRUE(ble_gatt_server
.CreateCharacteristic(service_uuid, characteristic_uuid,
permissions, properties)
.has_value());
@@ -83,21 +83,21 @@ TEST(BleV2GattServer, DISABLED_CreateCharacteristic) {
TEST(BleV2GattServer, DISABLED_UpdateCharacteristic) {
BluetoothAdapter bluetoothAdapter;
BleGattServer blev2_gatt_server(&bluetoothAdapter, {});
BleGattServer ble_gatt_server(&bluetoothAdapter, {});
Uuid service_uuid;
Uuid characteristic_uuid;
const api::ble_v2::GattCharacteristic::Permission permissions =
api::ble_v2::GattCharacteristic::Permission::kNone;
const api::ble_v2::GattCharacteristic::Property properties =
api::ble_v2::GattCharacteristic::Property::kNone;
const api::ble::GattCharacteristic::Permission permissions =
api::ble::GattCharacteristic::Permission::kNone;
const api::ble::GattCharacteristic::Property properties =
api::ble::GattCharacteristic::Property::kNone;
auto gatt_characteristic = blev2_gatt_server.CreateCharacteristic(
auto gatt_characteristic = ble_gatt_server.CreateCharacteristic(
service_uuid, characteristic_uuid, permissions, properties);
ByteArray value;
EXPECT_TRUE(blev2_gatt_server.UpdateCharacteristic(
gatt_characteristic.value(), value));
EXPECT_TRUE(
ble_gatt_server.UpdateCharacteristic(gatt_characteristic.value(), value));
}
} // namespace
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/ble_v2_server_socket.h"
#include "internal/platform/implementation/windows/ble_server_socket.h"
#include <cstddef>
#include <exception>
@@ -21,9 +21,9 @@
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include "internal/platform/implementation/windows/ble_socket.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
@@ -31,12 +31,12 @@
namespace nearby {
namespace windows {
BleV2ServerSocket::BleV2ServerSocket(api::BluetoothAdapter* adapter)
BleServerSocket::BleServerSocket(api::BluetoothAdapter* adapter)
: adapter_(dynamic_cast<BluetoothAdapter*>(adapter)) {
DCHECK_NE(adapter_, nullptr);
}
std::unique_ptr<api::ble_v2::BleSocket> BleV2ServerSocket::Accept() {
std::unique_ptr<api::ble::BleSocket> BleServerSocket::Accept() {
absl::MutexLock lock(&mutex_);
VLOG(1) << __func__ << ": Accept is called.";
@@ -45,14 +45,14 @@ std::unique_ptr<api::ble_v2::BleSocket> BleV2ServerSocket::Accept() {
}
if (closed_) return nullptr;
BleV2Socket ble_socket = pending_sockets_.front();
BleSocket ble_socket = pending_sockets_.front();
pending_sockets_.pop_front();
LOG(INFO) << __func__ << ": Accepted a remote connection.";
return std::make_unique<BleV2Socket>(ble_socket);
return std::make_unique<BleSocket>(ble_socket);
}
Exception BleV2ServerSocket::Close() {
Exception BleServerSocket::Close() {
// TODO(b/271031645): implement BLE socket using weave
absl::MutexLock lock(&mutex_);
VLOG(1) << __func__ << ": Close is called.";
@@ -67,7 +67,7 @@ Exception BleV2ServerSocket::Close() {
return {Exception::kSuccess};
}
bool BleV2ServerSocket::Bind() {
bool BleServerSocket::Bind() {
// TODO(b/271031645): implement BLE socket using weave
LOG(ERROR) << __func__ << ": GATT socket started.";
return true;
@@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_
#include <deque>
#include <memory>
@@ -21,19 +21,19 @@
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/windows/ble_socket.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
namespace nearby {
namespace windows {
class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
class BleServerSocket : public api::ble::BleServerSocket {
public:
explicit BleV2ServerSocket(api::BluetoothAdapter* adapter);
~BleV2ServerSocket() override = default;
explicit BleServerSocket(api::BluetoothAdapter* adapter);
~BleServerSocket() override = default;
std::unique_ptr<::nearby::api::ble_v2::BleSocket> Accept() override;
std::unique_ptr<::nearby::api::ble::BleSocket> Accept() override;
Exception Close() override;
@@ -47,10 +47,10 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
absl::Mutex mutex_;
absl::CondVar cond_;
bool closed_ = false;
std::deque<BleV2Socket> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::deque<BleSocket> pending_sockets_ ABSL_GUARDED_BY(mutex_);
};
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/ble_v2_socket.h"
#include "internal/platform/implementation/windows/ble_socket.h"
#include <cstdint>
@@ -25,43 +25,43 @@
namespace nearby {
namespace windows {
InputStream& BleV2Socket::GetInputStream() { return input_stream_; }
InputStream& BleSocket::GetInputStream() { return input_stream_; }
OutputStream& BleV2Socket::GetOutputStream() { return output_stream_; }
OutputStream& BleSocket::GetOutputStream() { return output_stream_; }
Exception BleV2Socket::Close() { return {Exception::kSuccess}; }
Exception BleSocket::Close() { return {Exception::kSuccess}; }
bool BleV2Socket::Connect() {
bool BleSocket::Connect() {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Connect to BLE peripheral";
return false;
}
ExceptionOr<ByteArray> BleV2Socket::BleInputStream::Read(std::int64_t size) {
ExceptionOr<ByteArray> BleSocket::BleInputStream::Read(std::int64_t size) {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Read data size=" << size;
return ExceptionOr<ByteArray>(Exception::kIo);
}
Exception BleV2Socket::BleInputStream::Close() {
Exception BleSocket::BleInputStream::Close() {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Close BLE input stream.";
return {Exception::kSuccess};
}
Exception BleV2Socket::BleOutputStream::Write(const ByteArray& data) {
Exception BleSocket::BleOutputStream::Write(const ByteArray& data) {
// TODO(b/271031645): implement BLE socket using weave
VLOG(1) << __func__ << ": Write data size=" << data.size();
return {Exception::kIo};
}
Exception BleV2Socket::BleOutputStream::Flush() {
Exception BleSocket::BleOutputStream::Flush() {
// TODO(b/271031645): implement BLE socket using weave
LOG(INFO) << __func__ << ": Flush is called.";
return {Exception::kSuccess};
}
Exception BleV2Socket::BleOutputStream::Close() {
Exception BleSocket::BleOutputStream::Close() {
// TODO(b/271031645): implement BLE socket using weave
LOG(INFO) << __func__ << ": close is called.";
return {Exception::kSuccess};
@@ -12,24 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_
#include <cstdint>
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
namespace nearby {
namespace windows {
class BleV2Socket : public api::ble_v2::BleSocket {
class BleSocket : public api::ble::BleSocket {
public:
BleV2Socket() = default;
~BleV2Socket() override = default;
BleSocket() = default;
~BleSocket() override = default;
InputStream& GetInputStream() override;
@@ -37,7 +37,7 @@ class BleV2Socket : public api::ble_v2::BleSocket {
Exception Close() override;
api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override {
api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override {
return 0LL;
};
@@ -67,4 +67,4 @@ class BleV2Socket : public api::ble_v2::BleSocket {
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_
@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/ble_v2.h"
#include "internal/platform/implementation/windows/ble.h"
#include <cstddef>
#include <cstdint>
#include <string>
#include <memory>
#include <string>
#include <utility>
#include "gmock/gmock.h"
@@ -26,7 +26,7 @@
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/synchronization/notification.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
@@ -55,130 +55,132 @@ class MockBluetoothAdapter : public BluetoothAdapter {
} // namespace
class BleV2MediumTest : public ::testing::Test {
class BleMediumTest : public ::testing::Test {
protected:
BleV2MediumTest() {
blev2_medium_ = std::make_unique<BleV2Medium>(bluetooth_adapter_);
BleMediumTest() {
ble_medium_ = std::make_unique<BleMedium>(bluetooth_adapter_);
}
protected:
size_t GetWatchersCount() {
absl::MutexLock lock(&blev2_medium_->mutex_);
return blev2_medium_->watchers_.size();
absl::MutexLock lock(&ble_medium_->mutex_);
return ble_medium_->watchers_.size();
}
MockBluetoothAdapter bluetooth_adapter_;
std::unique_ptr<BleV2Medium> blev2_medium_;
std::unique_ptr<BleMedium> ble_medium_;
};
namespace {
TEST_F(BleV2MediumTest, StartScanningAdapterDisabled) {
TEST_F(BleMediumTest, StartScanningAdapterDisabled) {
EXPECT_CALL(bluetooth_adapter_, IsEnabled()).WillOnce(Return(false));
Uuid service_uuid = kTestServiceUuid;
api::ble_v2::BleMedium::ScanCallback callback;
api::ble::BleMedium::ScanCallback callback;
EXPECT_EQ(false, blev2_medium_->StartScanning(
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_EQ(false, ble_medium_->StartScanning(service_uuid,
api::ble::TxPowerLevel::kHigh,
std::move(callback)));
EXPECT_EQ(GetWatchersCount(), 0);
}
TEST_F(BleV2MediumTest, StartScanningAdapterNoAdapterOnForge) {
TEST_F(BleMediumTest, StartScanningAdapterNoAdapterOnForge) {
EXPECT_CALL(bluetooth_adapter_, IsEnabled()).WillOnce(Return(true));
Uuid service_uuid = kTestServiceUuid;
api::ble_v2::BleMedium::ScanCallback callback;
api::ble::BleMedium::ScanCallback callback;
EXPECT_EQ(false, blev2_medium_->StartScanning(
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_EQ(false, ble_medium_->StartScanning(service_uuid,
api::ble::TxPowerLevel::kHigh,
std::move(callback)));
EXPECT_EQ(GetWatchersCount(), 0);
}
TEST(BleV2Medium, DISABLED_StartAdvertising) {
TEST(BleMedium, DISABLED_StartAdvertising) {
BluetoothAdapter bluetoothAdapter;
BleV2Medium blev2_medium(bluetoothAdapter);
BleMedium ble_medium(bluetoothAdapter);
api::ble_v2::BleAdvertisementData advertising_data;
api::ble_v2::BleAdvertisementData scan_response_data;
api::ble::BleAdvertisementData advertising_data;
api::ble::BleAdvertisementData scan_response_data;
EXPECT_TRUE(blev2_medium.StartAdvertising(
advertising_data, {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh,
EXPECT_TRUE(ble_medium.StartAdvertising(
advertising_data, {.tx_power_level = api::ble::TxPowerLevel::kHigh,
.is_connectable = true}));
}
TEST(BleV2Medium, DISABLED_StopAdvertising) {
TEST(BleMedium, DISABLED_StopAdvertising) {
BluetoothAdapter bluetoothAdapter;
BleV2Medium blev2_medium(bluetoothAdapter);
BleMedium ble_medium(bluetoothAdapter);
api::ble_v2::BleAdvertisementData advertising_data;
api::ble::BleAdvertisementData advertising_data;
EXPECT_TRUE(blev2_medium.StartAdvertising(
advertising_data, {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh,
EXPECT_TRUE(ble_medium.StartAdvertising(
advertising_data, {.tx_power_level = api::ble::TxPowerLevel::kHigh,
.is_connectable = true}));
EXPECT_TRUE(blev2_medium.StopAdvertising());
EXPECT_TRUE(ble_medium.StopAdvertising());
}
TEST(BleV2Medium, DISABLED_StartScanning) {
TEST(BleMedium, DISABLED_StartScanning) {
bool scan_response_received = false;
absl::Notification scan_response_notification;
BluetoothAdapter bluetoothAdapter;
BleV2Medium blev2_medium(bluetoothAdapter);
BleMedium ble_medium(bluetoothAdapter);
Uuid service_uuid;
api::ble_v2::BleMedium::ScanCallback callback;
api::ble::BleMedium::ScanCallback callback;
callback.advertisement_found_cb =
[&scan_response_received, &scan_response_notification](
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
const api::ble_v2::BleAdvertisementData& advertisement_data) {
api::ble::BlePeripheral::UniqueId peripheral_id,
const api::ble::BleAdvertisementData& advertisement_data) {
scan_response_received = true;
scan_response_notification.Notify();
};
EXPECT_TRUE(blev2_medium.StartScanning(
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_TRUE(ble_medium.StartScanning(
service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_TRUE(scan_response_notification.WaitForNotificationWithTimeout(
absl::Seconds(5)));
EXPECT_TRUE(scan_response_received);
}
TEST(BleV2Medium, DISABLED_StopScanning) {
TEST(BleMedium, DISABLED_StopScanning) {
BluetoothAdapter bluetoothAdapter;
BleV2Medium blev2_medium(bluetoothAdapter);
BleMedium ble_medium(bluetoothAdapter);
Uuid service_uuid;
api::ble_v2::BleMedium::ScanCallback callback;
api::ble::BleMedium::ScanCallback callback;
callback.advertisement_found_cb =
[](api::ble_v2::BlePeripheral::UniqueId peripheral_id,
const api::ble_v2::BleAdvertisementData& advertisement_data) {};
[](api::ble::BlePeripheral::UniqueId peripheral_id,
const api::ble::BleAdvertisementData& advertisement_data) {};
EXPECT_TRUE(blev2_medium.StartScanning(
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_TRUE(ble_medium.StartScanning(
service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback)));
EXPECT_TRUE(blev2_medium.StopScanning());
EXPECT_TRUE(ble_medium.StopScanning());
}
TEST(BleV2Medium, DISABLED_StartThenStopScanning) {
TEST(BleMedium, DISABLED_StartThenStopScanning) {
bool scan_response_received = false;
absl::Notification scan_response_notification;
BluetoothAdapter bluetoothAdapter;
BleV2Medium blev2_medium(bluetoothAdapter);
BleMedium ble_medium(bluetoothAdapter);
Uuid service_uuid;
api::ble_v2::BleMedium::ScanningCallback callback;
api::ble::BleMedium::ScanningCallback callback;
callback.advertisement_found_cb =
[&scan_response_received, &scan_response_notification](
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
const api::ble_v2::BleAdvertisementData& advertisement_data) {
api::ble::BlePeripheral::UniqueId peripheral_id,
const api::ble::BleAdvertisementData& advertisement_data) {
scan_response_received = true;
scan_response_notification.Notify();
};
auto scanning_session = blev2_medium.StartScanning(
service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback));
auto scanning_session = ble_medium.StartScanning(
service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback));
EXPECT_NE(scanning_session, nullptr);
EXPECT_TRUE(scanning_session->stop_scanning().ok());
}
@@ -36,12 +36,12 @@
#undef StrCat // Remove the Windows macro definition
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "internal/base/files.h"
#include "internal/base/file_path.h"
#include "internal/base/files.h"
#include "internal/platform/implementation/atomic_boolean.h"
#include "internal/platform/implementation/atomic_reference.h"
#include "internal/platform/implementation/awdl.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/condition_variable.h"
@@ -59,7 +59,7 @@
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/implementation/windows/atomic_boolean.h"
#include "internal/platform/implementation/windows/atomic_reference.h"
#include "internal/platform/implementation/windows/ble_v2.h"
#include "internal/platform/implementation/windows/ble.h"
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/bluetooth_classic_medium.h"
#include "internal/platform/implementation/windows/condition_variable.h"
@@ -158,8 +158,7 @@ std::string ImplementationPlatform::GetAppDataPath(
std::string app_data_path_utf8 =
windows::string_utils::WideStringToString(app_data_path);
std::replace(app_data_path_utf8.begin(), app_data_path_utf8.end(), '\\', '/');
return absl::StrCat(app_data_path_utf8, "/", kNCRelativePath, "/",
file_name);
return absl::StrCat(app_data_path_utf8, "/", kNCRelativePath, "/", file_name);
}
OSName ImplementationPlatform::GetCurrentOS() { return OSName::kWindows; }
@@ -220,8 +219,7 @@ std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
// Verifies that a path is a valid directory.
if (!Files::DirectoryExists(folder_path)) {
if (!Files::CreateDirectories(folder_path)) {
LOG(ERROR) << "Failed to create directory: "
<< folder_path.ToString();
LOG(ERROR) << "Failed to create directory: " << folder_path.ToString();
return nullptr;
}
}
@@ -256,9 +254,9 @@ ImplementationPlatform::CreateBluetoothClassicMedium(
}
// TODO(b/184975123): replace with real implementation.
std::unique_ptr<api::ble_v2::BleMedium>
ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter& adapter) {
return std::make_unique<windows::BleV2Medium>(adapter);
std::unique_ptr<api::ble::BleMedium> ImplementationPlatform::CreateBleMedium(
api::BluetoothAdapter& adapter) {
return std::make_unique<windows::BleMedium>(adapter);
}
std::unique_ptr<api::CredentialStorage>