mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
[BLE Refactor] Uses Uuid class for those used in Uuid string-form in /medium layer.
PiperOrigin-RevId: 450910238
This commit is contained in:
committed by
Copybara-Service
parent
cb8fef7875
commit
8e048f7be0
@@ -72,6 +72,7 @@ cc_library(
|
||||
"//connections/implementation/proto:offline_wire_formats_cc_proto",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:uuid",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/container:flat_hash_set",
|
||||
"@com_google_absl//absl/strings",
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/cancellation_flag.h"
|
||||
#include "internal/platform/exception.h"
|
||||
@@ -75,7 +76,7 @@ struct BleAdvertisementData {
|
||||
// iOS : 16 bit service UUID (type=0x03) + LocalName data (type=0x08)
|
||||
// Windows: Service data (type=0x16)
|
||||
// Android: 16 bit service UUID (type=0x03) + Service data (type=0x16)
|
||||
absl::flat_hash_map<std::string, location::nearby::ByteArray> service_data;
|
||||
absl::flat_hash_map<Uuid, location::nearby::ByteArray> service_data;
|
||||
};
|
||||
|
||||
// Opaque wrapper over a BLE peripheral. Must be able to uniquely identify a
|
||||
@@ -110,8 +111,8 @@ struct GattCharacteristic {
|
||||
kLast,
|
||||
};
|
||||
|
||||
std::string uuid;
|
||||
std::string service_uuid;
|
||||
Uuid uuid;
|
||||
Uuid service_uuid;
|
||||
|
||||
// Hashable
|
||||
template <typename H>
|
||||
@@ -136,7 +137,7 @@ class GattClient {
|
||||
// Returns whether or not discovery finished successfully.
|
||||
//
|
||||
// This function should block until discovery has finished.
|
||||
virtual bool DiscoverService(const std::string& service_uuid) = 0;
|
||||
virtual bool DiscoverService(const Uuid& service_uuid) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#getService(java.util.UUID)
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattService.html#getCharacteristic(java.util.UUID)
|
||||
@@ -148,12 +149,13 @@ class GattClient {
|
||||
//
|
||||
// It is okay for duplicate services to exist, as long as the specified
|
||||
// characteristic UUID is unique among all services of the same UUID.
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
virtual absl::optional<GattCharacteristic> GetCharacteristic(
|
||||
absl::string_view service_uuid,
|
||||
absl::string_view characteristic_uuid) = 0;
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#readCharacteristic(android.bluetooth.BluetoothGattCharacteristic)
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#getValue()
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
virtual absl::optional<ByteArray> ReadCharacteristic(
|
||||
const GattCharacteristic& characteristic) = 0;
|
||||
|
||||
@@ -188,8 +190,9 @@ class GattServer {
|
||||
// write to this descriptor and subscribe for characteristic changes. For
|
||||
// more information about this descriptor, please go to:
|
||||
// https://www.bluetooth.com/specifications/Gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.Gatt.client_characteristic_configuration.xml
|
||||
// NOLINTNEXTLINE(google3-legacy-absl-backports)
|
||||
virtual absl::optional<GattCharacteristic> CreateCharacteristic(
|
||||
absl::string_view service_uuid, absl::string_view characteristic_uuid,
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid,
|
||||
const std::vector<GattCharacteristic::Permission>& permissions,
|
||||
const std::vector<GattCharacteristic::Property>& properties) = 0;
|
||||
|
||||
@@ -317,7 +320,7 @@ class BleMedium {
|
||||
// HIGH:
|
||||
// - Scan window = ~4096ms
|
||||
// - Scan interval = ~4096ms
|
||||
virtual bool StartScanning(const std::string& service_uuid,
|
||||
virtual bool StartScanning(const Uuid& service_uuid,
|
||||
TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) = 0;
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ cc_library(
|
||||
"//internal/platform:cancellation_flag",
|
||||
"//internal/platform:logging",
|
||||
"//internal/platform:test_util",
|
||||
"//internal/platform:uuid",
|
||||
"//internal/platform/implementation:comm",
|
||||
"//internal/platform/implementation/shared:count_down_latch",
|
||||
"@com_google_absl//absl/base:core_headers",
|
||||
|
||||
@@ -242,7 +242,7 @@ bool BleV2Medium::StopAdvertising() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartScanning(const std::string& service_uuid,
|
||||
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
@@ -280,12 +280,11 @@ bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
|
||||
std::optional<api::ble_v2::GattCharacteristic>
|
||||
BleV2Medium::GattServer::CreateCharacteristic(
|
||||
absl::string_view service_uuid, absl::string_view characteristic_uuid,
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid,
|
||||
const std::vector<api::ble_v2::GattCharacteristic::Permission>& permissions,
|
||||
const std::vector<api::ble_v2::GattCharacteristic::Property>& properties) {
|
||||
api::ble_v2::GattCharacteristic characteristic = {
|
||||
.uuid = std::string(characteristic_uuid),
|
||||
.service_uuid = std::string(service_uuid)};
|
||||
.uuid = characteristic_uuid, .service_uuid = service_uuid};
|
||||
return characteristic;
|
||||
}
|
||||
|
||||
@@ -294,7 +293,8 @@ bool BleV2Medium::GattServer::UpdateCharacteristic(
|
||||
const location::nearby::ByteArray& value) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 Ble GattServer UpdateCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid << "," << characteristic.uuid
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value.data());
|
||||
MediumEnvironment::Instance().InsertBleV2MediumGattCharacteristics(
|
||||
characteristic, value);
|
||||
@@ -306,41 +306,43 @@ void BleV2Medium::GattServer::Stop() {
|
||||
MediumEnvironment::Instance().ClearBleV2MediumGattCharacteristics();
|
||||
}
|
||||
|
||||
bool BleV2Medium::GattClient::DiscoverService(const std::string& service_uuid) {
|
||||
bool BleV2Medium::GattClient::DiscoverService(const Uuid& service_uuid) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattClient DiscoverService, service_uuid"
|
||||
<< service_uuid;
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattClient DiscoverService, service_uuid="
|
||||
<< service_uuid.Get16BitAsString();
|
||||
if (!is_connection_alive_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Search if the service exists.
|
||||
return MediumEnvironment::Instance().ContainsBleV2MediumGattCharacteristics(
|
||||
service_uuid, "");
|
||||
service_uuid, /*characteristic_uuid=*/{});
|
||||
}
|
||||
|
||||
std::optional<api::ble_v2::GattCharacteristic>
|
||||
BleV2Medium::GattClient::GetCharacteristic(
|
||||
absl::string_view service_uuid, absl::string_view characteristic_uuid) {
|
||||
BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid,
|
||||
const Uuid& characteristic_uuid) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble GattClient GetCharacteristic, service_uuid="
|
||||
<< service_uuid
|
||||
<< ", characteristic_uuid=" << characteristic_uuid;
|
||||
<< service_uuid.Get16BitAsString()
|
||||
<< ", characteristic_uuid="
|
||||
<< std::string(characteristic_uuid);
|
||||
if (!is_connection_alive_) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Search gatt_characteristic by uuid and if found return the
|
||||
// gatt_characteristic.
|
||||
api::ble_v2::GattCharacteristic characteristic = {};
|
||||
api::ble_v2::GattCharacteristic characteristic;
|
||||
if (MediumEnvironment::Instance().ContainsBleV2MediumGattCharacteristics(
|
||||
service_uuid, characteristic_uuid)) {
|
||||
characteristic = {.uuid = std::string(characteristic_uuid),
|
||||
.service_uuid = std::string(service_uuid)};
|
||||
characteristic = {.uuid = characteristic_uuid,
|
||||
.service_uuid = service_uuid};
|
||||
}
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "G3 Ble GattClient GetCharacteristic, found characteristic=("
|
||||
<< characteristic.service_uuid << "," << characteristic.uuid << ")";
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid) << ")";
|
||||
|
||||
return characteristic;
|
||||
}
|
||||
@@ -356,7 +358,8 @@ std::optional<ByteArray> BleV2Medium::GattClient::ReadCharacteristic(
|
||||
MediumEnvironment::Instance().ReadBleV2MediumGattCharacteristics(
|
||||
characteristic);
|
||||
NEARBY_LOGS(INFO) << "G3 Ble ReadCharacteristic, characteristic=("
|
||||
<< characteristic.service_uuid << "," << characteristic.uuid
|
||||
<< characteristic.service_uuid.Get16BitAsString() << ","
|
||||
<< std::string(characteristic.uuid)
|
||||
<< "), value = " << absl::BytesToHexString(value.data());
|
||||
return std::move(value);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/g3/pipe.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -160,7 +161,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool StartScanning(const std::string& service_uuid,
|
||||
bool StartScanning(const Uuid& service_uuid,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -199,7 +200,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
class GattServer : public api::ble_v2::GattServer {
|
||||
public:
|
||||
std::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
|
||||
absl::string_view service_uuid, absl::string_view characteristic_uuid,
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid,
|
||||
const std::vector<api::ble_v2::GattCharacteristic::Permission>&
|
||||
permissions,
|
||||
const std::vector<api::ble_v2::GattCharacteristic::Property>&
|
||||
@@ -215,11 +216,10 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
// A concrete implemenation for GattClient.
|
||||
class GattClient : public api::ble_v2::GattClient {
|
||||
public:
|
||||
bool DiscoverService(const std::string& service_uuid) override;
|
||||
bool DiscoverService(const Uuid& service_uuid) override;
|
||||
|
||||
std::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(
|
||||
absl::string_view service_uuid,
|
||||
absl::string_view characteristic_uuid) override;
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid) override;
|
||||
|
||||
std::optional<ByteArray> ReadCharacteristic(
|
||||
const api::ble_v2::GattCharacteristic& characteristic) override;
|
||||
|
||||
@@ -106,7 +106,7 @@ bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data,
|
||||
// (AD type 0x16) Service Data
|
||||
DataWriter data_writer;
|
||||
auto it = advertising_data.service_data.begin();
|
||||
const std::string& service_uuid = it->first;
|
||||
const std::string& service_uuid = it->first.Get16BitAsString();
|
||||
const ByteArray& service_bytes = it->second;
|
||||
|
||||
if (service_uuid.size() < 2) {
|
||||
@@ -184,7 +184,7 @@ bool BleV2Medium::StopAdvertising() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartScanning(const std::string& service_uuid,
|
||||
bool BleV2Medium::StartScanning(const Uuid& service_uuid,
|
||||
TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "Windows Ble StartScanning";
|
||||
|
||||
@@ -50,7 +50,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool StartScanning(const std::string& service_uuid,
|
||||
bool StartScanning(const Uuid& service_uuid,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -56,7 +56,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
|
||||
BluetoothAdapter bluetoothAdapter;
|
||||
BleV2Medium blev2_medium(bluetoothAdapter);
|
||||
std::string service_uuid;
|
||||
Uuid service_uuid;
|
||||
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
@@ -78,7 +78,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
TEST(BleV2Medium, DISABLED_StopScanning) {
|
||||
BluetoothAdapter bluetoothAdapter;
|
||||
BleV2Medium blev2_medium(bluetoothAdapter);
|
||||
std::string service_uuid;
|
||||
Uuid service_uuid;
|
||||
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
|
||||
Reference in New Issue
Block a user