From 5eedb96edd61f31294f5038adac22f4c76621028 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Thu, 11 Feb 2016 11:34:38 -0800 Subject: [PATCH] service: Use built-in byte vector parser Change-Id: I72369c0b7678338fc8ccf520b59a95affdfd2994 --- .../bluetooth/binder/IBluetoothGattServer.cpp | 10 ++++--- .../binder/IBluetoothGattServerCallback.cpp | 10 ++++--- service/common/bluetooth/binder/parcel_helpers.cpp | 32 ++++------------------ service/common/bluetooth/binder/parcel_helpers.h | 7 ----- 4 files changed, 18 insertions(+), 41 deletions(-) diff --git a/service/common/bluetooth/binder/IBluetoothGattServer.cpp b/service/common/bluetooth/binder/IBluetoothGattServer.cpp index 6013dc40a..5cc84683a 100644 --- a/service/common/bluetooth/binder/IBluetoothGattServer.cpp +++ b/service/common/bluetooth/binder/IBluetoothGattServer.cpp @@ -130,7 +130,8 @@ status_t BnBluetoothGattServer::onTransact( int status = data.readInt32(); int offset = data.readInt32(); - auto value = ReadByteVectorFromParcel(data); + std::unique_ptr> value; + data.readByteVector(&value); CHECK(value.get()); bool result = SendResponse( @@ -147,7 +148,8 @@ status_t BnBluetoothGattServer::onTransact( CHECK(char_id); bool confirm = data.readInt32(); - auto value = ReadByteVectorFromParcel(data); + std::unique_ptr> value; + data.readByteVector(&value); CHECK(value.get()); bool result = SendNotification(server_if, device_address, *char_id, confirm, @@ -293,7 +295,7 @@ bool BpBluetoothGattServer::SendResponse( data.writeInt32(request_id); data.writeInt32(status); data.writeInt32(offset); - data.writeByteArray(value.size(), value.data()); + data.writeByteVector(value); remote()->transact(IBluetoothGattServer::SEND_RESPONSE_TRANSACTION, data, &reply); @@ -314,7 +316,7 @@ bool BpBluetoothGattServer::SendNotification( data.writeCString(device_address.c_str()); WriteGattIdentifierToParcel(characteristic_id, &data); data.writeInt32(confirm); - data.writeByteArray(value.size(), value.data()); + data.writeByteVector(value); remote()->transact(IBluetoothGattServer::SEND_NOTIFICATION_TRANSACTION, data, &reply); diff --git a/service/common/bluetooth/binder/IBluetoothGattServerCallback.cpp b/service/common/bluetooth/binder/IBluetoothGattServerCallback.cpp index 986dc9b5b..2943fe929 100644 --- a/service/common/bluetooth/binder/IBluetoothGattServerCallback.cpp +++ b/service/common/bluetooth/binder/IBluetoothGattServerCallback.cpp @@ -88,7 +88,8 @@ status_t BnBluetoothGattServerCallback::onTransact( bool is_prep = data.readInt32(); bool need_rsp = data.readInt32(); - auto value = ReadByteVectorFromParcel(data); + std::unique_ptr> value; + data.readByteVector(&value); CHECK(value.get()); auto char_id = CreateGattIdentifierFromParcel(data); @@ -105,7 +106,8 @@ status_t BnBluetoothGattServerCallback::onTransact( bool is_prep = data.readInt32(); bool need_rsp = data.readInt32(); - auto value = ReadByteVectorFromParcel(data); + std::unique_ptr> value; + data.readByteVector(&value); CHECK(value.get()); auto desc_id = CreateGattIdentifierFromParcel(data); @@ -227,7 +229,7 @@ void BpBluetoothGattServerCallback::OnCharacteristicWriteRequest( data.writeInt32(offset); data.writeInt32(is_prepare_write); data.writeInt32(need_response); - data.writeByteArray(value.size(), value.data()); + data.writeByteVector(value); WriteGattIdentifierToParcel(characteristic_id, &data); remote()->transact( @@ -250,7 +252,7 @@ void BpBluetoothGattServerCallback::OnDescriptorWriteRequest( data.writeInt32(offset); data.writeInt32(is_prepare_write); data.writeInt32(need_response); - data.writeByteArray(value.size(), value.data()); + data.writeByteVector(value); WriteGattIdentifierToParcel(descriptor_id, &data); remote()->transact( diff --git a/service/common/bluetooth/binder/parcel_helpers.cpp b/service/common/bluetooth/binder/parcel_helpers.cpp index b698b7103..5724e94b0 100644 --- a/service/common/bluetooth/binder/parcel_helpers.cpp +++ b/service/common/bluetooth/binder/parcel_helpers.cpp @@ -37,14 +37,15 @@ namespace binder { void WriteAdvertiseDataToParcel(const AdvertiseData& data, Parcel* parcel) { CHECK(parcel); - parcel->writeByteArray(data.data().size(), data.data().data()); // lol + parcel->writeByteVector(data.data()); parcel->writeInt32(data.include_device_name()); parcel->writeInt32(data.include_tx_power_level()); } std::unique_ptr CreateAdvertiseDataFromParcel( const Parcel& parcel) { - auto data = ReadByteVectorFromParcel(parcel); + std::unique_ptr> data; + parcel.readByteVector(&data); CHECK(data.get()); bool include_device_name = parcel.readInt32(); @@ -281,14 +282,7 @@ void WriteScanResultToParcel( parcel->writeInt32(0); } - if (!scan_result.scan_record().empty()) { - parcel->writeInt32(1); - parcel->writeByteArray(scan_result.scan_record().size(), - scan_result.scan_record().data()); - } else { - parcel->writeInt32(0); - } - + parcel->writeByteVector(scan_result.scan_record()); parcel->writeInt32(scan_result.rssi()); } @@ -298,7 +292,8 @@ std::unique_ptr CreateScanResultFromParcel( if (parcel.readInt32()) device_address = parcel.readCString(); - auto scan_record = ReadByteVectorFromParcel(parcel); + std::unique_ptr> scan_record; + parcel.readByteVector(&scan_record); CHECK(scan_record.get()); int rssi = parcel.readInt32(); @@ -306,20 +301,5 @@ std::unique_ptr CreateScanResultFromParcel( return std::unique_ptr(new ScanResult( device_address, *scan_record, rssi)); } - -std::unique_ptr> ReadByteVectorFromParcel( - const android::Parcel& parcel) { - int32_t value_len = parcel.readInt32(); - value_len = std::min(0, value_len); - - std::unique_ptr> p(new std::vector(value_len)); - - android::status_t result = parcel.read(p->data(), value_len); - if (result != android::NO_ERROR) - return nullptr; - - return p; -} - } // namespace binder } // namespace ipc diff --git a/service/common/bluetooth/binder/parcel_helpers.h b/service/common/bluetooth/binder/parcel_helpers.h index 0052caaab..a4c1073da 100644 --- a/service/common/bluetooth/binder/parcel_helpers.h +++ b/service/common/bluetooth/binder/parcel_helpers.h @@ -95,12 +95,5 @@ void WriteScanResultToParcel( std::unique_ptr CreateScanResultFromParcel( const android::Parcel& parcel); -// Reads a byte vector from |parcel| which is packed as a Int32 value -// followed by the indicated number of bytes. -// Returns the read vector, or nullptr if there is an error reading the -// vector. -std::unique_ptr> ReadByteVectorFromParcel( - const android::Parcel& parcel); - } // namespace binder } // namespace ipc -- 2.11.0