4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <sensor_logs.h>
31 poller::poller(int fd)
43 void poller::init_poll_fd(void)
45 m_epfd = epoll_create(EPOLL_MAX);
48 bool poller::add_fd(int fd)
50 struct epoll_event event;
53 event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
55 if (epoll_ctl(m_epfd, EPOLL_CTL_ADD, fd, &event)) {
56 ERR("errno : %d , errstr : %s", errno, strerror(errno));
63 bool poller::fill_event_queue(void)
65 const int EPOLL_MAX_EVENT = 1;
67 struct epoll_event event_items[EPOLL_MAX_EVENT];
68 int nr_events = epoll_wait(m_epfd, event_items, EPOLL_MAX_EVENT, -1);
74 ERR("Epoll failed errrno : %d , errstr : %s", errno, strerror(errno));
79 ERR("Epoll timeout!");
83 for (int i = 0; i < nr_events; i++)
84 m_event_queue.push(event_items[i]);
90 bool poller::poll(struct epoll_event &event)
93 if (m_event_queue.empty()) {
94 if (!fill_event_queue())
98 if (!m_event_queue.empty()) {
99 event = m_event_queue.front();
102 if (event.events & EPOLLERR) {
107 if (event.events & EPOLLHUP) {
108 INFO("Poll: Connetion is closed from the other side");