Add new parcel header APIs 90/262690/14
authorChanggyu Choi <changyu.choi@samsung.com>
Wed, 18 Aug 2021 02:17:00 +0000 (11:17 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Thu, 19 Aug 2021 09:56:18 +0000 (09:56 +0000)
Adds:
 - rpc_port_parcel_header_set_tag()
 - rpc_port_parcel_header_get_tag()

Change-Id: I9f8c3d906ba38a66433f49fe32190db7d5d7757b
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
include/rpc-port-parcel.h
src/parcel-header-internal.cc
src/parcel-header-internal.hh
src/rpc-port-parcel.cc
test/unit_tests/rpc_port_parcel_test.cc

index 005148c..7f1193d 100644 (file)
@@ -444,6 +444,34 @@ int rpc_port_parcel_burst_write(rpc_port_parcel_h h, const unsigned char *buf, u
 int rpc_port_parcel_get_header(rpc_port_parcel_h h, rpc_port_parcel_header_h *header);
 
 /**
+ * @brief Sets the tag to the header handle of the rpc port parcel.
+ * @since_tizen 6.5
+ * @param[in] header The header handle of the rpc port parcel
+ * @param[in] tag The tag
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
+ * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see rpc_port_parcel_header_get_tag()
+ */
+int rpc_port_parcel_header_set_tag(rpc_port_parcel_header_h header, const char *tag);
+
+/**
+ * @brief Gets the tag from the header handle of the rpc port parcel.
+ * @since_tizen 6.5
+ * @remarks The @a tag should be released using free().
+ * @param[in] header The header handle of the rpc port parcel
+ * @param[out] tag The tag
+ * @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_header_set_tag()
+ */
+int rpc_port_parcel_header_get_tag(rpc_port_parcel_header_h header, char **tag);
+
+/**
  * @brief Sets the sequence number to the header handle of the rpc port parcel.
  * @since_tizen 6.5
  * @param[in] header The header handle of the rpc port parcel
index f508bda..f4413dd 100644 (file)
@@ -17,6 +17,7 @@
 #include <atomic>
 #include <climits>
 #include <limits>
+#include <utility>
 
 #include "parcel-header-internal.hh"
 
@@ -28,12 +29,14 @@ ParcelHeader::ParcelHeader() : seq_num_(GenerateSeqNum()) {
 }
 
 void ParcelHeader::WriteToParcel(tizen_base::Parcel* parcel) const {
+  parcel->WriteString(tag_);
   parcel->WriteInt32(seq_num_);
   parcel->WriteInt64(static_cast<int64_t>(time_stamp_.tv_sec));
   parcel->WriteInt64(static_cast<int64_t>(time_stamp_.tv_nsec));
 }
 
 void ParcelHeader::ReadFromParcel(tizen_base::Parcel* parcel) {
+  tag_ = std::move(parcel->ReadString());
   parcel->ReadInt32(&seq_num_);
 
   int64_t tv_sec = 0;
@@ -45,6 +48,14 @@ void ParcelHeader::ReadFromParcel(tizen_base::Parcel* parcel) {
   time_stamp_.tv_nsec = tv_nsec & std::numeric_limits<long>::max();
 }
 
+void ParcelHeader::SetTag(std::string tag) {
+  tag_ = std::move(tag);
+}
+
+const std::string& ParcelHeader::GetTag() const {
+  return tag_;
+}
+
 void ParcelHeader::SetSeqNum(int seq_num) {
   seq_num_ = seq_num;
 }
index 3519e2d..32a9007 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <time.h>
 
+#include <string>
+
 #include <parcel.hh>
 
 namespace rpc_port {
@@ -31,6 +33,8 @@ class ParcelHeader : public tizen_base::Parcelable {
   void WriteToParcel(tizen_base::Parcel* parcel) const override;
   void ReadFromParcel(tizen_base::Parcel* parcel) override;
 
+  void SetTag(std::string tag);
+  const std::string& GetTag() const;
   void SetSeqNum(int seq_num);
   int GetSeqNum() const;
   struct timespec GetTimeStamp() const;
@@ -38,6 +42,7 @@ class ParcelHeader : public tizen_base::Parcelable {
   static int GenerateSeqNum();
 
  private:
+  std::string tag_;
   int seq_num_;
   struct timespec time_stamp_;
 };
index ded14b2..4c33ab5 100644 (file)
@@ -458,6 +458,31 @@ RPC_API int rpc_port_parcel_get_header(rpc_port_parcel_h h,
   return RPC_PORT_ERROR_NONE;
 }
 
+RPC_API int rpc_port_parcel_header_set_tag(rpc_port_parcel_header_h header,
+    const char* tag) {
+  if (header == nullptr || tag == nullptr)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  auto* parcel_header = static_cast<internal::ParcelHeader*>(header);
+  parcel_header->SetTag(tag);
+  return RPC_PORT_ERROR_NONE;
+}
+
+RPC_API int rpc_port_parcel_header_get_tag(rpc_port_parcel_header_h header,
+    char** tag) {
+  if (header == nullptr || tag == nullptr)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  auto* parcel_header = static_cast<internal::ParcelHeader*>(header);
+  const std::string& raw_tag = parcel_header->GetTag();
+
+  *tag = strdup(raw_tag.c_str());
+  if (*tag == nullptr)
+    return RPC_PORT_ERROR_OUT_OF_MEMORY;
+
+  return RPC_PORT_ERROR_NONE;
+}
+
 RPC_API int rpc_port_parcel_header_set_seq_num(rpc_port_parcel_header_h header,
     int seq_num) {
   if (header == nullptr)
index c982844..4cc326a 100644 (file)
@@ -344,6 +344,69 @@ TEST_F(ParcelTest, rpc_port_parcel_get_header_N) {
 }
 
 /*
+ * @testcase rpc_port_parcel_header_set_get_tag_P
+ * @description Sets and gets the tag
+ * @apicovered rpc_port_parcel_header_set_tag, rpc_port_parcel_header_get_tag
+ */
+TEST_F(ParcelTest, rpc_port_parcel_header_set_get_tag_P) {
+  rpc_port_parcel_header_h header = nullptr;
+  int ret = rpc_port_parcel_get_header(handle_, &header);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  const char tag[] = "1.5.0";
+  ret = rpc_port_parcel_header_set_tag(header, tag);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  char* out_tag = nullptr;
+  ret = rpc_port_parcel_header_get_tag(header, &out_tag);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+  ASSERT_STREQ(tag, out_tag);
+  std::free(out_tag);
+}
+
+/*
+ * @testcase rpc_port_parcel_header_set_get_tag_without_set_tag_P
+ * @description gets the tag without set tag.
+ * @apicovered rpc_port_parcel_header_get_tag
+ */
+TEST_F(ParcelTest, rpc_port_parcel_header_set_get_tag_without_set_tag_P) {
+  rpc_port_parcel_header_h header = nullptr;
+  int ret = rpc_port_parcel_get_header(handle_, &header);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  char* out_tag = nullptr;
+  ret = rpc_port_parcel_header_get_tag(header, &out_tag);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+  ASSERT_STREQ(out_tag, "");
+  std::free(out_tag);
+}
+
+/*
+ * @testcase rpc_port_parcel_header_set_get_tag_N
+ * @description Sets and gets the tag
+ * @apicovered rpc_port_parcel_header_set_tag, rpc_port_parcel_header_get_tag
+ */
+TEST_F(ParcelTest, rpc_port_parcel_header_set_get_tag_N) {
+  rpc_port_parcel_header_h header = nullptr;
+  int ret = rpc_port_parcel_get_header(handle_, &header);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_NONE);
+
+  const char tag[] = "1.5.0";
+  ret = rpc_port_parcel_header_set_tag(nullptr, tag);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+
+  ret = rpc_port_parcel_header_set_tag(header, nullptr);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+
+  char* out_tag = nullptr;
+  ret = rpc_port_parcel_header_get_tag(nullptr, &out_tag);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+
+  ret = rpc_port_parcel_header_get_tag(header, nullptr);
+  ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER);
+}
+
+/*
  * @testcase rpc_port_parcel_header_set_get_seq_num_P
  * @description Sets and gets the sequence number
  * @apicovered rpc_port_parcel_header_set_seq_num, rpc_port_parcel_header_get_seq_num