From 0c078b352834779d7eb8c0df2012766c75dbe224 Mon Sep 17 00:00:00 2001 From: Qin Wang Date: Tue, 7 Mar 2023 11:09:16 -0800 Subject: [PATCH] Convet ByteArray to std::string in ble_gatt_client PiperOrigin-RevId: 514786088 --- connections/implementation/mediums/ble_v2.cc | 3 ++- internal/platform/ble_v2.h | 7 ++++--- internal/platform/ble_v2_test.cc | 2 +- internal/platform/byte_array.h | 1 + internal/platform/byte_array_test.cc | 3 ++- internal/platform/implementation/apple/ble.h | 9 +++++---- internal/platform/implementation/apple/ble.mm | 11 ++++++----- internal/platform/implementation/ble_v2.h | 10 +++++----- internal/platform/implementation/g3/ble_v2.cc | 8 ++++---- internal/platform/implementation/g3/ble_v2.h | 6 +++--- .../implementation/windows/ble_gatt_client.cc | 14 +++++++------- .../implementation/windows/ble_gatt_client.h | 10 ++++++---- 12 files changed, 46 insertions(+), 38 deletions(-) diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index dd514c41..024479a5 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -665,7 +665,8 @@ void BleV2::ProcessFetchGattAdvertisementsRequest( auto characteristic_byte = gatt_client->ReadCharacteristic(gatt_characteristic.value()); if (characteristic_byte.has_value()) { - advertisement_read_result.AddAdvertisement(slot, *characteristic_byte); + advertisement_read_result.AddAdvertisement( + slot, ByteArray(characteristic_byte.value())); NEARBY_LOGS(VERBOSE) << "Successfully read advertisement at slot=" << slot; } else { diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index 41a42cf0..fd106161 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -21,6 +21,7 @@ #include #include "absl/functional/any_invocable.h" +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" @@ -219,7 +220,7 @@ class GattClient final { } // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional ReadCharacteristic( + absl::optional ReadCharacteristic( api::ble_v2::GattCharacteristic& characteristic) { return impl_->ReadCharacteristic(characteristic); } @@ -227,14 +228,14 @@ class GattClient final { // NOLINTNEXTLINE(google3-legacy-absl-backports) bool WriteCharacteristic( const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) { + absl::string_view value) { return impl_->WriteCharacteristic(characteristic, value); } // NOLINTNEXTLINE(google3-legacy-absl-backports) bool SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) { return impl_->SetCharacteristicSubscription( characteristic, enable, std::move(on_characteristic_changed_cb)); diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 58eb9498..0b3735fc 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -574,7 +574,7 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { // Can read the characteristic value. EXPECT_THAT(gatt_client->ReadCharacteristic(*client_characteristic), - Optional(server_value)); + Optional(server_value.string_data())); gatt_client->Disconnect(); gatt_server->Stop(); diff --git a/internal/platform/byte_array.h b/internal/platform/byte_array.h index 45b82d97..944e5258 100644 --- a/internal/platform/byte_array.h +++ b/internal/platform/byte_array.h @@ -78,6 +78,7 @@ class ByteArray { } char* data() { return &data_[0]; } + std::string string_data() const { return data_; } const char* data() const { return data_.data(); } size_t size() const { return data_.size(); } bool Empty() const { return data_.empty(); } diff --git a/internal/platform/byte_array_test.cc b/internal/platform/byte_array_test.cc index 832b1af1..2397ad3b 100644 --- a/internal/platform/byte_array_test.cc +++ b/internal/platform/byte_array_test.cc @@ -15,6 +15,7 @@ #include "internal/platform/byte_array.h" #include +#include #include "gtest/gtest.h" #include "absl/hash/hash_testing.h" @@ -60,7 +61,7 @@ TEST(ByteArrayTest, SetFromString) { std::string setup("setup_test"); ByteArray bytes{setup}; // array initialized with a copy of string. EXPECT_EQ(setup.size(), bytes.size()); - EXPECT_EQ(std::string(bytes), setup); + EXPECT_EQ(bytes.string_data(), setup); } TEST(ByteArrayTest, SetExplicitSize) { diff --git a/internal/platform/implementation/apple/ble.h b/internal/platform/implementation/apple/ble.h index 1d1064b2..b8cac83e 100644 --- a/internal/platform/implementation/apple/ble.h +++ b/internal/platform/implementation/apple/ble.h @@ -182,22 +182,23 @@ class BleMedium : public api::ble_v2::BleMedium { const Uuid &service_uuid, const Uuid &characteristic_uuid) override; // NOLINTNEXTLINE - absl::optional ReadCharacteristic( + absl::optional ReadCharacteristic( const api::ble_v2::GattCharacteristic &characteristic) override; bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, - const ByteArray &value) override; + absl::string_view value) override; bool SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic &characteristic, bool enable, - absl::AnyInvocable on_characteristic_changed_cb) override; + absl::AnyInvocable on_characteristic_changed_cb) override; void Disconnect() override; private: GNCMBleCentral *central_; std::string peripheral_id_; - absl::flat_hash_map gatt_characteristic_values_; + absl::flat_hash_map + gatt_characteristic_values_; }; absl::Mutex mutex_; diff --git a/internal/platform/implementation/apple/ble.mm b/internal/platform/implementation/apple/ble.mm index 6fbf4eb3..51c1712f 100644 --- a/internal/platform/implementation/apple/ble.mm +++ b/internal/platform/implementation/apple/ble.mm @@ -16,6 +16,7 @@ #include +#include #include #include @@ -539,7 +540,7 @@ bool BleMedium::GattClient::DiscoverServiceAndCharacteristics( api::ble_v2::GattCharacteristic characteristic = {.uuid = it->second, .service_uuid = service_uuid}; gatt_characteristic_values_.insert( - {characteristic, ByteArrayFromNSData(cb_characteristic.value)}); + {characteristic, ByteArrayFromNSData(cb_characteristic.value).string_data()}); } } @@ -567,24 +568,24 @@ absl::optional BleMedium::GattClient::GetCharac } // NOLINTNEXTLINE -absl::optional BleMedium::GattClient::ReadCharacteristic( +absl::optional BleMedium::GattClient::ReadCharacteristic( const api::ble_v2::GattCharacteristic& characteristic) { auto const it = gatt_characteristic_values_.find(characteristic); if (it == gatt_characteristic_values_.end()) { return absl::nullopt; // NOLINT } - return it->second; + return std::string(it->second); } bool BleMedium::GattClient::WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, const ByteArray& value) { + const api::ble_v2::GattCharacteristic& characteristic, absl::string_view value) { // No op. return false; } bool BleMedium::GattClient::SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable on_characteristic_changed_cb) { + absl::AnyInvocable on_characteristic_changed_cb) { // No op since we can't write characteristics. return false; } diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index fc662c1a..31c47194 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -197,7 +197,7 @@ class GattClient { // 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 ReadCharacteristic( + virtual absl::optional ReadCharacteristic( const GattCharacteristic& characteristic) = 0; // https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#setValue(byte[]) @@ -206,14 +206,14 @@ class GattClient { // Sends a remote characteristic write request to the server and returns // whether or not it was successful. virtual bool WriteCharacteristic(const GattCharacteristic& characteristic, - const ByteArray& value) = 0; + absl::string_view value) = 0; // https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic,%20boolean) // // Enable or disable notifications/indications for a given characteristic. virtual bool SetCharacteristicSubscription( const GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) = 0; // https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#disconnect() @@ -270,8 +270,8 @@ class GattServer { struct ClientGattConnectionCallback { public: // Called when the characteristic is changed - absl::AnyInvocable on_characteristic_changed_cb = - [](ByteArray&) {}; + absl::AnyInvocable + on_characteristic_changed_cb = [](absl::string_view) {}; // Called when the client is disconnected from the GATT server. absl::AnyInvocable disconnected_cb = []() {}; diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index f77de371..f301ddd0 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -437,7 +437,7 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid, return characteristic; } -std::optional BleV2Medium::GattClient::ReadCharacteristic( +std::optional BleV2Medium::GattClient::ReadCharacteristic( const api::ble_v2::GattCharacteristic& characteristic) { absl::MutexLock lock(&mutex_); if (!is_connection_alive_) { @@ -451,19 +451,19 @@ std::optional BleV2Medium::GattClient::ReadCharacteristic( << characteristic.service_uuid.Get16BitAsString() << "," << std::string(characteristic.uuid) << "), value = " << absl::BytesToHexString(value.data()); - return std::move(value); + return value.string_data(); } bool BleV2Medium::GattClient::WriteCharacteristic( const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) { + absl::string_view value) { // No op. return false; } bool BleV2Medium::GattClient::SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) { // No op since we can't write characteristics. return false; diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 10692eb5..b7593f27 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -235,16 +235,16 @@ class BleV2Medium : public api::ble_v2::BleMedium { std::optional GetCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid) override; - std::optional ReadCharacteristic( + std::optional ReadCharacteristic( const api::ble_v2::GattCharacteristic& characteristic) override; bool WriteCharacteristic( const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) override; + absl::string_view value) override; bool SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) override; void Disconnect() override; diff --git a/internal/platform/implementation/windows/ble_gatt_client.cc b/internal/platform/implementation/windows/ble_gatt_client.cc index 074361e5..bdf20344 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.cc +++ b/internal/platform/implementation/windows/ble_gatt_client.cc @@ -275,7 +275,7 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid, return absl::nullopt; } -absl::optional BleGattClient::ReadCharacteristic( +absl::optional BleGattClient::ReadCharacteristic( const api::ble_v2::GattCharacteristic& characteristic) { NEARBY_LOGS(VERBOSE) << __func__ << ": Read characteristic=" << std::string(characteristic.uuid); @@ -316,7 +316,7 @@ absl::optional BleGattClient::ReadCharacteristic( NEARBY_LOGS(VERBOSE) << __func__ << ": Got characteristic value length=" << data.size(); - return ByteArray(data); + return data; } catch (std::exception exception) { NEARBY_LOGS(ERROR) << __func__ << ": Failed to read GATT characteristic. exception: " @@ -332,7 +332,7 @@ absl::optional BleGattClient::ReadCharacteristic( bool BleGattClient::WriteCharacteristic( const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) { + absl::string_view value) { NEARBY_LOGS(VERBOSE) << __func__ << ": write characteristic: " << std::string(characteristic.uuid); absl::MutexLock lock(&mutex_); @@ -380,7 +380,7 @@ bool BleGattClient::WriteCharacteristic( bool BleGattClient::SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) { NEARBY_LOGS(VERBOSE) << __func__ << ": Started to set Characteristic Subscription."; @@ -577,7 +577,8 @@ bool BleGattClient::WriteCharacteristicConfigurationDescriptor( void BleGattClient::OnCharacteristicValueChanged( GattCharacteristic const& characteristic, GattValueChangedEventArgs args, - absl::AnyInvocable on_characteristic_changed_cb) { + absl::AnyInvocable + on_characteristic_changed_cb) { NEARBY_LOGS(VERBOSE) << __func__ << "Gatt Characteristic value changed."; IBuffer buffer = args.CharacteristicValue(); int size = buffer.Length(); @@ -589,8 +590,7 @@ void BleGattClient::OnCharacteristicValueChanged( } NEARBY_LOGS(VERBOSE) << __func__ << ": Got characteristic value length= " << data.size(); - ByteArray value = ByteArray(data); - on_characteristic_changed_cb(value); + on_characteristic_changed_cb(data); } } // namespace windows diff --git a/internal/platform/implementation/windows/ble_gatt_client.h b/internal/platform/implementation/windows/ble_gatt_client.h index 09b0eb0e..69281b3f 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.h +++ b/internal/platform/implementation/windows/ble_gatt_client.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "absl/container/flat_hash_map.h" @@ -46,16 +47,16 @@ class BleGattClient : public api::ble_v2::GattClient { absl::optional GetCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid) override; - absl::optional ReadCharacteristic( + absl::optional ReadCharacteristic( const api::ble_v2::GattCharacteristic& characteristic) override; bool WriteCharacteristic( const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) override; + absl::string_view value) override; bool SetCharacteristicSubscription( const api::ble_v2::GattCharacteristic& characteristic, bool enable, - absl::AnyInvocable + absl::AnyInvocable on_characteristic_changed_cb) override; void Disconnect() override; @@ -84,7 +85,8 @@ class BleGattClient : public api::ble_v2::GattClient { GattCharacteristic const& characteristic, ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile:: GattValueChangedEventArgs args, - absl::AnyInvocable on_characteristic_changed_cb); + absl::AnyInvocable + on_characteristic_changed_cb); absl::Mutex mutex_;