OSDN Git Service

service: Use built-in byte vector parser
authorJakub Pawlowski <jpawlowski@google.com>
Thu, 11 Feb 2016 19:34:38 +0000 (11:34 -0800)
committerAndre Eisenbach <eisenbach@google.com>
Wed, 17 Feb 2016 00:46:11 +0000 (00:46 +0000)
Change-Id: I72369c0b7678338fc8ccf520b59a95affdfd2994

service/common/bluetooth/binder/IBluetoothGattServer.cpp
service/common/bluetooth/binder/IBluetoothGattServerCallback.cpp
service/common/bluetooth/binder/parcel_helpers.cpp
service/common/bluetooth/binder/parcel_helpers.h

index 6013dc4..5cc8468 100644 (file)
@@ -130,7 +130,8 @@ status_t BnBluetoothGattServer::onTransact(
     int status = data.readInt32();
     int offset = data.readInt32();
 
-    auto value = ReadByteVectorFromParcel(data);
+    std::unique_ptr<std::vector<uint8_t>> 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<std::vector<uint8_t>> 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);
index 986dc9b..2943fe9 100644 (file)
@@ -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<std::vector<uint8_t>> 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<std::vector<uint8_t>> 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(
index b698b71..5724e94 100644 (file)
@@ -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<AdvertiseData> CreateAdvertiseDataFromParcel(
     const Parcel& parcel) {
-  auto data = ReadByteVectorFromParcel(parcel);
+  std::unique_ptr<std::vector<uint8_t>> 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<bluetooth::ScanResult> CreateScanResultFromParcel(
   if (parcel.readInt32())
     device_address = parcel.readCString();
 
-  auto scan_record = ReadByteVectorFromParcel(parcel);
+  std::unique_ptr<std::vector<uint8_t>> scan_record;
+  parcel.readByteVector(&scan_record);
   CHECK(scan_record.get());
 
   int rssi = parcel.readInt32();
@@ -306,20 +301,5 @@ std::unique_ptr<bluetooth::ScanResult> CreateScanResultFromParcel(
   return std::unique_ptr<ScanResult>(new ScanResult(
       device_address, *scan_record, rssi));
 }
-
-std::unique_ptr<std::vector<uint8_t>> ReadByteVectorFromParcel(
-    const android::Parcel& parcel) {
-  int32_t value_len = parcel.readInt32();
-  value_len = std::min(0, value_len);
-
-  std::unique_ptr<std::vector<uint8_t>> p(new std::vector<uint8_t>(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
index 0052caa..a4c1073 100644 (file)
@@ -95,12 +95,5 @@ void WriteScanResultToParcel(
 std::unique_ptr<bluetooth::ScanResult> 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<std::vector<uint8_t>> ReadByteVectorFromParcel(
-    const android::Parcel& parcel);
-
 }  // namespace binder
 }  // namespace ipc