From: Hwankyu Jhun Date: Tue, 16 Jan 2024 11:25:44 +0000 (+0900) Subject: Modify PollFd X-Git-Tag: accepted/tizen/unified/20240202.020810~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f885e4fde1da45b1479c349c1d08603b497b71c7;p=platform%2Fcore%2Fappfw%2Ftizen-core.git Modify PollFd 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 --- diff --git a/include/tizen_core.h b/include/tizen_core.h index 876656c..dd74f4b 100644 --- a/include/tizen_core.h +++ b/include/tizen_core.h @@ -18,7 +18,9 @@ #define __TIZEN_BASE_TIZEN_CORE_H__ #include +#include #include +#include #include #include @@ -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_h *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 diff --git a/tests/tizen-core_unittests/tizen_core_test.cc b/tests/tizen-core_unittests/tizen_core_test.cc index dfbefc2..6928556 100644 --- a/tests/tizen-core_unittests/tizen_core_test.cc +++ b/tests/tizen-core_unittests/tizen_core_test.cc @@ -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); +} diff --git a/tizen_base/poll_fd.cc b/tizen_base/poll_fd.cc index 24ebaf0..3bbf0ad 100644 --- a/tizen_base/poll_fd.cc +++ b/tizen_base/poll_fd.cc @@ -16,23 +16,32 @@ #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 diff --git a/tizen_base/poll_fd.h b/tizen_base/poll_fd.h index 46a826e..bc28d6e 100644 --- a/tizen_base/poll_fd.h +++ b/tizen_base/poll_fd.h @@ -17,24 +17,38 @@ #ifndef TIZEN_CORE_TIZEN_BASE_POLL_FD_H_ #define TIZEN_CORE_TIZEN_BASE_POLL_FD_H_ +#include + +#include + #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 { 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 self_; }; } // namespace tizen_core diff --git a/tizen_base/source.cc b/tizen_base/source.cc index b85bfad..1dfe879 100644 --- a/tizen_base/source.cc +++ b/tizen_base/source.cc @@ -92,34 +92,20 @@ Source::~Source() { } } -void Source::AddPoll(const PollFd& pollfd) { - auto* gpollfd = static_cast(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(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(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(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) { @@ -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& Source::GetPollFds() const { + return poll_fds_; } bool Source::OnSourcePrepare(int* timeout) { return false; } diff --git a/tizen_base/source.h b/tizen_base/source.h index bd5c569..dde9d7c 100644 --- a/tizen_base/source.h +++ b/tizen_base/source.h @@ -20,6 +20,7 @@ #include #include +#include #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) override; bool IsAttached() const; GSource* GetHandle() const; void RefSelf() override; void UnrefSelf() override; void SetPriority(int priority); - GSList* GetPollFds() const; + const std::vector& 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 self_; + std::vector poll_fds_; }; } // namespace tizen_core diff --git a/tizen_base/stub.cc b/tizen_base/stub.cc index bedae6c..6386f05 100644 --- a/tizen_base/stub.cc +++ b/tizen_base/stub.cc @@ -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(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(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(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(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(h); - guint len = g_slist_length(source->GetPollFds()); - auto* fds = static_cast( - calloc(len, sizeof(tizen_core_poll_fd_s))); - if (fds == nullptr) { + auto& fds = source->GetPollFds(); + *poll_fds = static_cast( + const_cast(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(); + 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(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(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(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(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(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(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(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(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(h); + *revents = poll_fd->GetRevents(); return TIZEN_CORE_ERROR_NONE; }