Modify PollFd 67/304367/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 16 Jan 2024 11:25:44 +0000 (20:25 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 16 Jan 2024 11:27:50 +0000 (11:27 +0000)
The following APIs are added for adding poll fd to the source:
 - tizen_core_poll_fd_create()
 - tizen_core_poll_fd_destroy()
 - tizen_core_poll_fd_set_fd()
 - tizen_core_poll_fd_get_fd()
 - tizen_core_poll_fd_set_events()
 - tizen_core_poll_fd_get_events()
 - tizen_core_poll_fd_set_revents()
 - tizen_core_poll_fd_get_revents()

Change-Id: Ifedb72ef90d316c1f4203daa1c48dcbb8db75c12
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/tizen_core.h
tests/tizen-core_unittests/tizen_core_test.cc
tizen_base/poll_fd.cc
tizen_base/poll_fd.h
tizen_base/source.cc
tizen_base/source.h
tizen_base/stub.cc

index 876656c8857b1f49984832a2f484c8ba48a4f0d4..dd74f4b3fd3c981fa4927c7dd310771ca831183d 100644 (file)
@@ -18,7 +18,9 @@
 #define __TIZEN_BASE_TIZEN_CORE_H__
 
 #include <stdbool.h>
+#include <stdint.h>
 #include <tizen_error.h>
+#include <poll.h>
 
 #include <tizen_core_channel.h>
 #include <tizen_core_event.h>
@@ -69,6 +71,12 @@ typedef void *tizen_core_task_h;
  */
 typedef void *tizen_core_source_h;
 
+/**
+ * @brief The tizen core poll fd handle.
+ * @since_tizen 9.0
+ */
+typedef void *tizen_core_poll_fd_h;
+
 /**
  * @brief Called when the tizen core idle or timer event is emitted.
  * @since_tizen 9.0
@@ -122,7 +130,7 @@ typedef bool (*tizen_core_source_check_cb)(tizen_core_source_h source,
  * @see tizen_core_source_set_dispatch_callback()
  */
 typedef bool (*tizen_core_source_dispatch_cb)(tizen_core_source_h source,
-                                         void *user_data);
+                                              void *user_data);
 
 /**
  * @brief Called when the source is finalizing.
@@ -133,19 +141,7 @@ typedef bool (*tizen_core_source_dispatch_cb)(tizen_core_source_h source,
  * @see tizen_core_source_set_finalize_callback()
  */
 typedef void (*tizen_core_source_finalize_cb)(tizen_core_source_h source,
-                                         void *user_data);
-
-/**
- * @brief The structure type containing the set of variables for setting poll events.
- * @since_tizen 9.0
- *
- * @see tizen_core_source_add_poll()
- */
-typedef struct {
-  int fd;  /**< The file descriptor */
-  short events;  /**< The requested events */
-  short revents;  /**< The returned events */
-} tizen_core_poll_fd_s;
+                                              void *user_data);
 
 /**
  * @brief Initializes the tizen core.
@@ -432,7 +428,7 @@ int tizen_core_source_destroy(tizen_core_source_h h);
  * @details The tizen core source must be created using the tizen_core_source_create().
  *
  * @param[in] h The tizen core source handle
- * @param[in] poll_fd The polling file descriptor
+ * @param[in] poll_fd The poll fd handle
  * @return @c 0 on success,
  *         otherwise a negative error value
  * @retval #TIZEN_CORE_ERROR_NONE Successful
@@ -446,7 +442,7 @@ int tizen_core_source_destroy(tizen_core_source_h h);
  * @see tizen_core_source_remove_poll()
  */
 int tizen_core_source_add_poll(tizen_core_source_h h,
-                               tizen_core_poll_fd_s poll_fd);
+                               tizen_core_poll_fd_h poll_fd);
 
 /**
  * @brief Removes to waits for some events on the file descriptor.
@@ -454,7 +450,7 @@ int tizen_core_source_add_poll(tizen_core_source_h h,
  * @details The tizen core source must be created using the tizen_core_source_create().
  *
  * @param[in] h The tizen core source handle
- * @param[in] poll_fd The polling file descriptor
+ * @param[in] poll_fd The poll fd handle
  * @return @c 0 on success,
  *         otherwise a negative error value
  * @retval #TIZEN_CORE_ERROR_NONE Successful
@@ -463,7 +459,7 @@ int tizen_core_source_add_poll(tizen_core_source_h h,
  * @see tizen_core_source_add_poll()
  */
 int tizen_core_source_remove_poll(tizen_core_source_h h,
-                                  tizen_core_poll_fd_s poll_fd);
+                                  tizen_core_poll_fd_h poll_fd);
 
 /**
  * @brief Sets the prepare callback function to the tizen core source.
@@ -551,7 +547,6 @@ int tizen_core_source_set_priority(tizen_core_source_h h,
 /**
  * @brief Gets the poll fds from the tizen core source.
  * @since_tizen 9.0
- * @details The @c poll_fds should be released using free().
  *
  * @param[in] h The tizen core source handle
  * @param[out] poll_fds The polling file descriptors
@@ -564,9 +559,120 @@ int tizen_core_source_set_priority(tizen_core_source_h h,
  * @see tizen_core_source_add_poll()
  */
 int tizen_core_source_get_poll_fds(tizen_core_source_h h,
-                                   tizen_core_poll_fd_s **poll_fds,
+                                   tizen_core_poll_fd_*poll_fds,
                                    unsigned int *length);
 
+/**
+ * @brief Creates a tizen core poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[out] h The tizen core poll fd handle
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #TIZEN_CORE_ERROR_OUT_OF_MEMORY Out of memory
+ * @see tizen_core_poll_fd_destroy()
+ */
+int tizen_core_poll_fd_create(tizen_core_poll_fd_h *h);
+
+/**
+ * @brief Destroys the tizen core poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #TIZEN_CORE_ERROR_OUT_OF_MEMORY Out of memory
+ */
+int tizen_core_poll_fd_destroy(tizen_core_poll_fd_h h);
+
+/**
+ * @brief Sets the file descriptor to the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[in] fd The file descriptor
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_RRROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_get_fd()
+ */
+int tizen_core_poll_fd_set_fd(tizen_core_poll_fd_h h, int fd);
+
+/**
+ * @brief Gets the file descriptor from the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[out] fd The file descriptor
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_RRROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_set_fd()
+ */
+int tizen_core_poll_fd_get_fd(tizen_core_poll_fd_h h, int *fd);
+
+/**
+ * @brief Sets the requested events to the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[in] events The requested events
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_get_events()
+ */
+int tizen_core_poll_fd_set_events(tizen_core_poll_fd_h h, uint16_t events);
+
+/**
+ * @brief Gets the requested events from the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[out] events The requested events
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_set_events()
+ */
+int tizen_core_poll_fd_get_events(tizen_core_poll_fd_h h, uint16_t *events);
+
+/**
+ * @brief Sets the returned events to the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[in] revents The returned events
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_get_events()
+ */
+int tizen_core_poll_fd_set_revents(tizen_core_poll_fd_h h, uint16_t revents);
+
+/**
+ * @brief Gets the returned events from the poll fd handle.
+ * @since_tizen 9.0
+ *
+ * @param[in] h The tizen core poll fd handle
+ * @param[out] revents The returned events
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #TIZEN_CORE_ERROR_NONE Successful
+ * @retval #TIZEN_CORE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see tizen_core_poll_fd_set_events()
+ */
+int tizen_core_poll_fd_get_revents(tizen_core_poll_fd_h h, uint16_t *revents);
+
 #ifdef __cplusplus
 }
 #endif
index dfbefc2b0bdab53bcc5570ea1c9418134c468f36..692855651ece79f9095f356a23c52f26b58b2ef3 100644 (file)
@@ -41,9 +41,11 @@ class TizenCoreTest : public ::testing::Test  {
     TCoreSetUp();
     TCoreChannelSetUp();
     TCoreEventSetUp();
+    TCorePollFdSetUp();
   }
 
   void TearDown() override {
+    TCorePollFdTearDown();
     TCoreEventTearDown();
     TCoreChannelTearDown();
     TCoreTearDown();
@@ -62,6 +64,8 @@ class TizenCoreTest : public ::testing::Test  {
   tizen_core_event_object_h event_object_ = nullptr;
   int type_ = 0;
 
+  tizen_core_poll_fd_h poll_fd_ = nullptr;
+
  private:
   void TCoreSetUp() {
     tizen_core_init();
@@ -134,6 +138,20 @@ class TizenCoreTest : public ::testing::Test  {
       event_ = nullptr;
     }
   }
+
+  void TCorePollFdSetUp() {
+    tizen_core_poll_fd_create(&poll_fd_);
+    tizen_core_poll_fd_set_fd(poll_fd_, 0);
+    tizen_core_poll_fd_set_events(poll_fd_, POLL_IN);
+    tizen_core_poll_fd_set_revents(poll_fd_, POLL_IN);
+  }
+
+  void TCorePollFdTearDown() {
+    if (poll_fd_ != nullptr) {
+      tizen_core_poll_fd_destroy(poll_fd_);
+      poll_fd_ = nullptr;
+    }
+  }
 };
 
 TEST_F(TizenCoreTest, tizen_core_init_P) {
@@ -593,14 +611,21 @@ TEST_F(TizenCoreTest, tizen_core_source_destroy_N) {
 }
 
 TEST_F(TizenCoreTest, tizen_core_source_add_poll_P) {
-  tizen_core_poll_fd_s poll_fd = { 0, POLL_IN, };
+  tizen_core_poll_fd_h poll_fd = nullptr;
+  tizen_core_poll_fd_create(&poll_fd);
+  ASSERT_NE(poll_fd, nullptr);
+  tizen_core_poll_fd_set_fd(poll_fd, 0);
+  tizen_core_poll_fd_set_events(poll_fd, POLL_IN);
+
   int ret = tizen_core_source_add_poll(source_, poll_fd);
   ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+
+  tizen_core_source_remove_poll(source_, poll_fd);
+  tizen_core_poll_fd_destroy(poll_fd);
 }
 
 TEST_F(TizenCoreTest, tizen_core_source_add_poll_N) {
-  tizen_core_poll_fd_s poll_fd = { 0, };
-  int ret = tizen_core_source_add_poll(nullptr, poll_fd);
+  int ret = tizen_core_source_add_poll(nullptr, nullptr);
   ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
 }
 
@@ -668,3 +693,81 @@ TEST_F(TizenCoreTest, tizen_core_get_glib_context_N) {
   ASSERT_EQ(context, nullptr);
   ASSERT_EQ(get_last_result(), TIZEN_CORE_ERROR_INVALID_PARAMETER);
 }
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_create_P) {
+  tizen_core_poll_fd_h poll_fd = nullptr;
+  int ret = tizen_core_poll_fd_create(&poll_fd);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  ASSERT_NE(poll_fd, nullptr);
+  tizen_core_poll_fd_destroy(poll_fd);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_create_N) {
+  int ret = tizen_core_poll_fd_create(nullptr);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_destroy_P) {
+  int ret = tizen_core_poll_fd_destroy(poll_fd_);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  poll_fd_ = nullptr;
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_destroy_N) {
+  int ret = tizen_core_poll_fd_destroy(nullptr);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_fd_P) {
+  int ret = tizen_core_poll_fd_set_fd(poll_fd_, 1);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+
+  int fd = -1;
+  ret = tizen_core_poll_fd_get_fd(poll_fd_, &fd);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  ASSERT_EQ(fd, 1);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_fd_N) {
+  int ret = tizen_core_poll_fd_set_fd(nullptr, -1);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+
+  ret = tizen_core_poll_fd_get_fd(nullptr, nullptr);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_events_P) {
+  int ret = tizen_core_poll_fd_set_events(poll_fd_, POLLIN);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+
+  uint16_t events = 0;
+  ret = tizen_core_poll_fd_get_events(poll_fd_, &events);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  ASSERT_EQ(events, POLLIN);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_events_N) {
+  int ret = tizen_core_poll_fd_set_events(nullptr, 0);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+
+  ret = tizen_core_poll_fd_get_events(nullptr, nullptr);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_revents_P) {
+  int ret = tizen_core_poll_fd_set_revents(poll_fd_, POLLIN);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+
+  uint16_t revents = 0;
+  ret = tizen_core_poll_fd_get_revents(poll_fd_, &revents);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_NONE);
+  ASSERT_EQ(revents, POLLIN);
+}
+
+TEST_F(TizenCoreTest, tizen_core_poll_fd_set_and_get_revents_N) {
+  int ret = tizen_core_poll_fd_set_revents(nullptr, 0);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+
+  ret = tizen_core_poll_fd_get_revents(nullptr, nullptr);
+  ASSERT_EQ(ret, TIZEN_CORE_ERROR_INVALID_PARAMETER);
+}
index 24ebaf00b9165bf00eead926637af4317d7fb55f..3bbf0ad54b1759ddcd2974027786c4ff40f2de6f 100644 (file)
 
 #include "tizen_base/poll_fd.h"
 
+#include "tizen_base/log_private.h"
+
 namespace tizen_base {
 namespace tizen_core {
 
-PollFd::PollFd(int fd, short events, short revents)
-    : fd_(fd), events_(events), revents_(revents) {}
+PollFd::PollFd() { _D("PollFd=%p", this); }
+
+PollFd::~PollFd() { _D("PollFd=%p, fd=%d", this, gpollfd_.fd); }
+
+void PollFd::SetFd(int fd) { gpollfd_.fd = fd; }
+
+int PollFd::GetFd() const { return gpollfd_.fd; }
+
+void PollFd::SetEvents(uint16_t events) { gpollfd_.events = events; }
+
+uint16_t PollFd::GetEvents() const { return gpollfd_.events; }
+
+void PollFd::SetRevents(uint16_t revents) { gpollfd_.revents = revents; }
+
+uint16_t PollFd::GetRevents() const { return gpollfd_.revents; }
 
-int PollFd::GetFd() const {
-  return fd_;
-}
+const GPollFD* PollFd::GetGPollFD() const { return &gpollfd_; }
 
-short PollFd::GetEvents() const {
-  return events_;
-}
+void PollFd::RefSelf() { self_ = shared_from_this(); }
 
-short PollFd::GetRevents() const {
-  return revents_;
-}
+void PollFd::UnrefSelf() { self_.reset(); }
 
 }  // namespace tizen_core
 }  // namespace tizen_base
index 46a826ef21a606ab9bebc37082aff164d971a5b7..bc28d6e169a63d166f66f94e4f9db194bee5e4f8 100644 (file)
 #ifndef TIZEN_CORE_TIZEN_BASE_POLL_FD_H_
 #define TIZEN_CORE_TIZEN_BASE_POLL_FD_H_
 
+#include <glib.h>
+
+#include <memory>
+
 #undef EXPORT_API
 #define EXPORT_API __attribute__((visibility("default")))
 
 namespace tizen_base {
 namespace tizen_core {
 
-class EXPORT_API  PollFd {
+class EXPORT_API PollFd : public std::enable_shared_from_this<PollFd> {
  public:
-  PollFd(int fd, short events, short revents);
+  PollFd();
+  ~PollFd();
 
+  void SetFd(int fd);
   int GetFd() const;
-  short GetEvents() const;
-  short GetRevents() const;
+
+  void SetEvents(uint16_t events);
+  uint16_t GetEvents() const;
+
+  void SetRevents(uint16_t revents);
+  uint16_t GetRevents() const;
+
+  const GPollFD* GetGPollFD() const;
+
+  void RefSelf();
+  void UnrefSelf();
 
  private:
-  int fd_;
-  short events_;
-  short revents_;
+  GPollFD gpollfd_ = { -1, 0, 0 };
+  std::shared_ptr<PollFd> self_;
 };
 
 }  // namespace tizen_core
index b85bfad836b09b583724d551ee336f001a301d65..1dfe8796eeefa8de67c46651f95f6313aebbe834 100644 (file)
@@ -92,34 +92,20 @@ Source::~Source() {
   }
 }
 
-void Source::AddPoll(const PollFd& pollfd) {
-  auto* gpollfd = static_cast<GPollFD*>(g_malloc(sizeof(GPollFD)));
-  if (gpollfd == nullptr) {
-    _E("g_malloc() is failed");
-    throw std::runtime_error("g_malloc() is failed");
-  }
-
-  gpollfd->fd = pollfd.GetFd();
-  gpollfd->events = pollfd.GetEvents();
-  gpollfd->revents = pollfd.GetRevents();
-  g_source_add_poll(handle_, gpollfd);
+void Source::AddPoll(PollFd* poll_fd) {
+  _D("AddPoll(). PollFd=%p", poll_fd);
+  g_source_add_poll(handle_, const_cast<GPollFD*>(poll_fd->GetGPollFD()));
   g_source_set_callback(handle_, nullptr, this, nullptr);
   source_manager.Insert(this);
+  poll_fds_.push_back(poll_fd);
 }
 
-void Source::RemovePoll(const PollFd& pollfd) {
-  GSList* iter = handle_->poll_fds;
-  while (iter != nullptr) {
-    auto* gpollfd = static_cast<GPollFD*>(iter->data);
-    if (gpollfd->fd == pollfd.GetFd() &&
-        gpollfd->events == pollfd.GetEvents()) {
-      g_source_remove_poll(handle_, gpollfd);
-      g_free(gpollfd);
-      break;
-    }
-
-    iter = g_slist_next(iter);
-  }
+void Source::RemovePoll(PollFd* poll_fd) {
+  _D("RemovePoll(). PollFd=%p", poll_fd);
+  g_source_remove_poll(handle_, const_cast<GPollFD*>(poll_fd->GetGPollFD()));
+  auto found = std::find(poll_fds_.begin(), poll_fds_.end(), poll_fd);
+  if (found == poll_fds_.end()) return;
+  poll_fds_.erase(found);
 }
 
 void Source::Attach(const std::shared_ptr<Context>& context) {
@@ -146,10 +132,8 @@ void Source::SetPriority(int priority) {
   g_source_set_priority(handle_, priority);
 }
 
-GSList* Source::GetPollFds() const {
-  if (handle_ == nullptr) return nullptr;
-
-  return handle_->poll_fds;
+const std::vector<PollFd*>& Source::GetPollFds() const {
+  return poll_fds_;
 }
 
 bool Source::OnSourcePrepare(int* timeout) { return false; }
index bd5c569d2427d571bf38e1628d00a78b31ce38a7..dde9d7c654924f9c4838ee6c1814eddc0c299db0 100644 (file)
@@ -20,6 +20,7 @@
 #include <glib.h>
 
 #include <memory>
+#include <vector>
 
 #include "tizen_base/context.h"
 #include "tizen_base/interface_source.h"
@@ -38,15 +39,15 @@ class EXPORT_API Source : public ISource,
   explicit Source(GSource* handle);
   virtual ~Source();
 
-  void AddPoll(const PollFd& pollfd);
-  void RemovePoll(const PollFd& pollfd);
+  void AddPoll(PollFd* poll_fd);
+  void RemovePoll(PollFd* poll_fd);
   void Attach(const std::shared_ptr<Context>& context) override;
   bool IsAttached() const;
   GSource* GetHandle() const;
   void RefSelf() override;
   void UnrefSelf() override;
   void SetPriority(int priority);
-  GSList* GetPollFds() const;
+  const std::vector<PollFd*>& GetPollFds() const;
 
   virtual bool OnSourcePrepare(int* timeout);
   virtual bool OnSourceCheck();
@@ -64,6 +65,7 @@ class EXPORT_API Source : public ISource,
   GSource* handle_ = nullptr;
   bool attached_ = false;
   std::shared_ptr<Source> self_;
+  std::vector<PollFd*> poll_fds_;
 };
 
 }  // namespace tizen_core
index bedae6c824fd638fcc36fabf0c4da1c08d163e73..6386f05c3d1a77a7d032403b9590addd45ff04bf 100644 (file)
@@ -407,42 +407,26 @@ API int tizen_core_source_destroy(tizen_core_source_h h) {
 }
 
 API int tizen_core_source_add_poll(tizen_core_source_h h,
-                                   tizen_core_poll_fd_s poll_fd) {
-  if (h == nullptr) {
+                                   tizen_core_poll_fd_h poll_fd) {
+  if (h == nullptr || poll_fd == nullptr) {
     _E("Invalid parameter");
     return TIZEN_CORE_ERROR_INVALID_PARAMETER;
   }
 
   auto* source = static_cast<SourceExt*>(h);
-  tizen_base::tizen_core::PollFd pollfd(poll_fd.fd, poll_fd.events,
-                                        poll_fd.revents);
-  try {
-    source->AddPoll(pollfd);
-  } catch (const std::runtime_error& e) {
-    _E("runtime_error occurs. error(%s)", e.what());
-    return TIZEN_CORE_ERROR_INVALID_CONTEXT;
-  }
-
+  source->AddPoll(static_cast<tizen_base::tizen_core::PollFd*>(poll_fd));
   return TIZEN_CORE_ERROR_NONE;
 }
 
 API int tizen_core_source_remove_poll(tizen_core_source_h h,
-                                      tizen_core_poll_fd_s poll_fd) {
-  if (h == nullptr) {
+                                      tizen_core_poll_fd_h poll_fd) {
+  if (h == nullptr || poll_fd == nullptr) {
     _E("Invalid parameter");
     return TIZEN_CORE_ERROR_INVALID_PARAMETER;
   }
 
   auto* source = static_cast<SourceExt*>(h);
-  tizen_base::tizen_core::PollFd pollfd(poll_fd.fd, poll_fd.events,
-                                        poll_fd.revents);
-  try {
-    source->RemovePoll(pollfd);
-  } catch (const std::runtime_error& e) {
-    _E("runtime_error occurs. error(%s)", e.what());
-    return TIZEN_CORE_ERROR_INVALID_CONTEXT;
-  }
-
+  source->RemovePoll(static_cast<tizen_base::tizen_core::PollFd*>(poll_fd));
   return TIZEN_CORE_ERROR_NONE;
 }
 
@@ -523,7 +507,7 @@ API int tizen_core_source_set_priority(tizen_core_source_h h,
 }
 
 API int tizen_core_source_get_poll_fds(tizen_core_source_h h,
-                                       tizen_core_poll_fd_s** poll_fds,
+                                       tizen_core_poll_fd_h* poll_fds,
                                        unsigned int* length) {
   if (h == nullptr || poll_fds == nullptr || length == nullptr) {
     _E("Invalid parameter");
@@ -531,25 +515,107 @@ API int tizen_core_source_get_poll_fds(tizen_core_source_h h,
   }
 
   auto* source = static_cast<SourceExt*>(h);
-  guint len = g_slist_length(source->GetPollFds());
-  auto* fds = static_cast<tizen_core_poll_fd_s*>(
-      calloc(len, sizeof(tizen_core_poll_fd_s)));
-  if (fds == nullptr) {
+  auto& fds = source->GetPollFds();
+  *poll_fds = static_cast<tizen_core_poll_fd_h>(
+      const_cast<tizen_base::tizen_core::PollFd**>(fds.data()));
+  *length = fds.size();
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_create(tizen_core_poll_fd_h* h) {
+  if (h == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto poll_fd = std::make_shared<tizen_base::tizen_core::PollFd>();
+  if (poll_fd == nullptr) {
     _E("Out of memory");
     return TIZEN_CORE_ERROR_OUT_OF_MEMORY;
   }
 
-  int index = 0;
-  auto* iter = source->GetPollFds();
-  while (iter != nullptr) {
-    auto* gpoll_fd = static_cast<GPollFD*>(iter->data);
-    fds[index].fd = gpoll_fd->fd;
-    fds[index].events = gpoll_fd->events;
-    fds[index++].revents = gpoll_fd->revents;
-    iter = g_slist_next(iter);
+  poll_fd->RefSelf();
+  *h = static_cast<tizen_core_poll_fd_h>(poll_fd.get());
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_destroy(tizen_core_poll_fd_h h) {
+  if (h == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  poll_fd->UnrefSelf();
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_set_fd(tizen_core_poll_fd_h h, int fd) {
+  if (h == nullptr || fd < 0) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  poll_fd->SetFd(fd);
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_get_fd(tizen_core_poll_fd_h h, int* fd) {
+  if (h == nullptr || fd == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  *fd = poll_fd->GetFd();
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+
+API int tizen_core_poll_fd_set_events(tizen_core_poll_fd_h h, uint16_t events) {
+  if (h == nullptr || events == 0) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  poll_fd->SetEvents(events);
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_get_events(tizen_core_poll_fd_h h,
+                                      uint16_t* events) {
+  if (h == nullptr || events == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  *events = poll_fd->GetEvents();
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_set_revents(tizen_core_poll_fd_h h,
+                                       uint16_t revents) {
+  if (h == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
+  }
+
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  poll_fd->SetRevents(revents);
+  return TIZEN_CORE_ERROR_NONE;
+}
+
+API int tizen_core_poll_fd_get_revents(tizen_core_poll_fd_h h,
+                                       uint16_t* revents) {
+  if (h == nullptr || revents == nullptr) {
+    _E("Invalid parameter");
+    return TIZEN_CORE_ERROR_INVALID_PARAMETER;
   }
 
-  *poll_fds = fds;
-  *length = len;
+  auto* poll_fd = static_cast<tizen_base::tizen_core::PollFd*>(h);
+  *revents = poll_fd->GetRevents();
   return TIZEN_CORE_ERROR_NONE;
 }