sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / shared / poller.cpp
1 /*
2  * libsensord
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <errno.h>
20 #include <string.h>
21 #include <sensor_logs.h>
22 #include <poller.h>
23
24 #define EPOLL_MAX 32
25
26 poller::poller()
27 {
28         init_poll_fd();
29 }
30
31 poller::poller(int fd)
32 {
33         init_poll_fd();
34         add_fd(fd);
35 }
36
37 poller::~poller()
38 {
39         if (m_epfd)
40                 close(m_epfd);
41 }
42
43 void poller::init_poll_fd(void)
44 {
45         m_epfd = epoll_create(EPOLL_MAX);
46 }
47
48 bool poller::add_fd(int fd)
49 {
50         struct epoll_event event;
51
52         event.data.fd = fd;
53         event.events = EPOLLIN | EPOLLERR | EPOLLHUP;
54
55         if (epoll_ctl(m_epfd, EPOLL_CTL_ADD, fd, &event)) {
56                 ERR("errno : %d , errstr : %s", errno, strerror(errno));
57                 return false;
58         }
59
60         return true;
61 }
62
63 bool poller::fill_event_queue(void)
64 {
65         const int EPOLL_MAX_EVENT = 1;
66
67         struct epoll_event event_items[EPOLL_MAX_EVENT];
68         int nr_events = epoll_wait(m_epfd, event_items, EPOLL_MAX_EVENT, -1);
69
70         if (nr_events < 0) {
71                 if (errno == EINTR)
72                         return true;
73
74                 ERR("Epoll failed errrno : %d , errstr : %s", errno, strerror(errno));
75                 return false;
76         }
77
78         if (nr_events == 0) {
79                 ERR("Epoll timeout!");
80                 return false;
81         }
82
83     for (int i = 0; i < nr_events; i++)
84                 m_event_queue.push(event_items[i]);
85
86         return true;
87 }
88
89
90 bool poller::poll(struct epoll_event &event)
91 {
92         while (true) {
93                 if (m_event_queue.empty()) {
94                         if (!fill_event_queue())
95                                 return false;
96                 }
97
98                 if (!m_event_queue.empty()) {
99                         event = m_event_queue.front();
100                         m_event_queue.pop();
101
102                         if (event.events & EPOLLERR) {
103                                 DBG("Poll error!");
104                                 return false;
105                         }
106
107                         if (event.events & EPOLLHUP) {
108                                 INFO("Poll: Connetion is closed from the other side");
109                                 return false;
110                         }
111
112                         return true;
113                 }
114         }
115 }