Add new parcel functions 01/263201/6
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 27 Aug 2021 11:24:37 +0000 (20:24 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 9 Sep 2021 00:14:34 +0000 (09:14 +0900)
Adds:
 - rpc_port_parcel_get_raw()
 - rpc_port_parcel_create_from_raw()

Change-Id: I1568112a40405e5fbaef3c47a46fc2a23bf68fa6
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/rpc-port-parcel.h
src/rpc-port-parcel.cc
test/unit_tests/rpc_port_parcel_test.cc

index 7f1193d..73fdbfe 100644 (file)
@@ -512,6 +512,36 @@ int rpc_port_parcel_header_get_seq_num(rpc_port_parcel_header_h header, int *seq
 int rpc_port_parcel_header_get_timestamp(rpc_port_parcel_header_h header, struct timespec *timestamp);
 
 /**
+ * @brief Gets the raw data of the rpc port parcel handle.
+ * @since_tizen 6.5
+ * @remarks You MUST NOT release @a raw using free(). It's managed by platform.
+ * @param[in] h The rpc port parcel handle
+ * @param[out] raw The raw data
+ * @param[out] size The size of the raw data
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
+ * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
+ */
+int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void **raw, unsigned int *size);
+
+/**
+ * @brief Creates the rpc port parcel handle with given the raw data.
+ * @since_tizen 6.5
+ * @remarks You must release @a h using rpc_port_parcel_destroy().
+ * @param[out] h The rpc port parcel handle
+ * @param[in] raw The raw data
+ * @param[in] size The size of the raw data
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
+ * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #RPC_PORT_ERROR_OUT_OF_MEMORY Out of memory
+ * @see rpc_port_parcel_destroy()
+ */
+int rpc_port_parcel_create_from_raw(rpc_port_parcel_h *h, const void *raw, unsigned int size);
+
+/**
  * @}
  */
 
index 4c33ab5..35361db 100644 (file)
@@ -512,3 +512,38 @@ RPC_API int rpc_port_parcel_header_get_timestamp(
   *timestamp = parcel_header->GetTimeStamp();
   return RPC_PORT_ERROR_NONE;
 }
+
+RPC_API int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void** raw,
+    unsigned int* size) {
+  if (h == nullptr || raw == nullptr || size == nullptr)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  auto* parcel = static_cast<internal::Parcel*>(h);
+  void* raw_data = nullptr;
+  uint32_t raw_size = 0;
+  parcel_get_raw(parcel->GetHandle(), &raw_data, &raw_size);
+
+  *raw = raw_data;
+  *size = static_cast<unsigned int>(raw_size);
+  return RPC_PORT_ERROR_NONE;
+}
+
+RPC_API int rpc_port_parcel_create_from_raw(rpc_port_parcel_h* h,
+    const void* raw, unsigned int size) {
+  if (h == nullptr || raw == nullptr || size == 0)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  rpc_port_parcel_h parcel;
+  int ret = rpc_port_parcel_create(&parcel);
+  if (ret != RPC_PORT_ERROR_NONE)
+    return ret;
+
+  ret = rpc_port_parcel_from_array(parcel, raw, size);
+  if (ret != RPC_PORT_ERROR_NONE) {
+    rpc_port_parcel_destroy(parcel);
+    return ret;
+  }
+
+  *h = parcel;
+  return RPC_PORT_ERROR_NONE;
+}
index ffe8975..63038ca 100644 (file)
@@ -465,3 +465,70 @@ TEST_F(ParcelTest, rpc_port_parcel_header_get_timestamp_N) {
   int ret = rpc_port_parcel_header_get_timestamp(nullptr, nullptr);
   ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
 }
+
+/*
+ * @testcase rpc_port_parcel_get_raw_P
+ * @description Gets the raw data from the rpc port parcel handle
+ * @apicovered rpc_port_parcel_get_raw
+ */
+TEST_F(ParcelTest, rpc_port_parcel_get_raw_P) {
+  int ret = rpc_port_parcel_write_string(handle_, "test");
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  void* raw = nullptr;
+  unsigned int raw_size = 0;
+  ret = rpc_port_parcel_get_raw(handle_, &raw, &raw_size);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+  ASSERT_NE(raw, nullptr);
+  ASSERT_NE(raw_size, 0);
+}
+
+/*
+ * @testcase rpc_port_parcel_get_raw_N
+ * @description Gets the raw data from the rpc port parcel handle
+ * @apicovered rpc_port_parcel_get_raw
+ */
+TEST_F(ParcelTest, rpc_port_parcel_get_raw_N) {
+  int ret = rpc_port_parcel_get_raw(nullptr, nullptr, nullptr);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+}
+
+/*
+ * @testcase rpc_port_parcel_create_from_raw_P
+ * @description Creates the rpc parcel handle with given the raw data.
+ * @apicovered rpc_port_parcel_create_from_raw
+ */
+TEST_F(ParcelTest, rpc_port_parcel_create_from_raw_P) {
+  int ret = rpc_port_parcel_write_string(handle_, "test");
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  void* raw = nullptr;
+  unsigned int raw_size = 0;
+  ret = rpc_port_parcel_get_raw(handle_, &raw, &raw_size);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  rpc_port_parcel_h parcel = nullptr;
+  ret = rpc_port_parcel_create_from_raw(&parcel, raw, raw_size);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  std::unique_ptr<std::remove_pointer<rpc_port_parcel_h>::type,
+      decltype(rpc_port_parcel_destroy)*> parcel_auto(
+          parcel, rpc_port_parcel_destroy);
+
+  char* str = nullptr;
+  ret = rpc_port_parcel_read_string(parcel, &str);
+  std::unique_ptr<char, decltype(std::free)*> str_auto(str, std::free);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+  ASSERT_NE(str, nullptr);
+  ASSERT_EQ(std::string(str), "test");
+}
+
+/*
+ * @testcase rpc_port_parcel_create_from_raw_B
+ * @description Creates the rpc parcel handle with given the raw data.
+ * @apicovered rpc_port_parcel_create_from_raw
+ */
+TEST_F(ParcelTest, rpc_port_parcel_create_from_raw_N) {
+  int ret = rpc_port_parcel_create_from_raw(nullptr, nullptr, 0);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+}