Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[platform/core/system/sensord.git] / src / server / sensor_event_poller.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2016 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
20 #include <signal.h>
21 #include <sys/signalfd.h>
22 #include <sensor_base.h>
23 #include <physical_sensor.h>
24 #include <sensor_event_poller.h>
25 #include <sensor_loader.h>
26 #include <algorithm>
27 #include <vector>
28
29 sensor_event_poller::sensor_event_poller()
30 {
31         init_sensor_map();
32         init_fd();
33         init_signal_fd();
34 }
35
36 sensor_event_poller::~sensor_event_poller()
37 {
38         fd_sensors_t::iterator it;
39         for (it = m_fd_sensors.begin(); it != m_fd_sensors.end(); it = m_fd_sensors.upper_bound(it->first))
40                 m_poller.del_fd(it->first);
41 }
42
43 void sensor_event_poller::init_sensor_map(void)
44 {
45         int fd;
46         physical_sensor *sensor;
47
48         std::vector<sensor_base *> sensors;
49         sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
50
51         std::vector<sensor_base *>::iterator it;
52
53         for (it = sensors.begin(); it != sensors.end(); ++it) {
54                 sensor = dynamic_cast<physical_sensor *>(*it);
55                 if (sensor == NULL)
56                         continue;
57
58                 fd = sensor->get_poll_fd();
59
60                 if (fd < 0)
61                         continue;
62
63                 m_fd_sensors.insert(std::make_pair(fd, sensor));
64         }
65 }
66
67 void sensor_event_poller::init_fd(void)
68 {
69         fd_sensors_t::iterator it;
70         for (it = m_fd_sensors.begin(); it != m_fd_sensors.end(); it = m_fd_sensors.upper_bound(it->first)) {
71                 /* if fd is not valid, it is not added to poller */
72                 add_poll_fd(it->first);
73         }
74 }
75
76 void sensor_event_poller::init_signal_fd(void)
77 {
78         int sfd;
79         sigset_t mask;
80
81         sigemptyset(&mask);
82         sigaddset(&mask, SIGTERM);
83         sigaddset(&mask, SIGABRT);
84         sigaddset(&mask, SIGINT);
85
86         sfd = signalfd(-1, &mask, 0);
87         m_poller.add_signal_fd(sfd);
88 }
89
90 bool sensor_event_poller::add_poll_fd(int fd)
91 {
92         return m_poller.add_fd(fd);
93 }
94
95 bool sensor_event_poller::poll(void)
96 {
97         std::vector<uint32_t> ids;
98         while (true) {
99                 int fd;
100                 struct epoll_event poll_event;
101
102                 if (!m_poller.poll(poll_event))
103                         return false;
104
105                 fd = poll_event.data.fd;
106                 ids.clear();
107
108                 if (!read_fd(fd, ids))
109                         continue;
110
111                 if (!process_event(fd, ids))
112                         continue;
113         }
114 }
115
116 bool sensor_event_poller::read_fd(int fd, std::vector<uint32_t> &ids)
117 {
118         fd_sensors_t::iterator it;
119         physical_sensor *sensor;
120
121         it = m_fd_sensors.find(fd);
122         sensor = dynamic_cast<physical_sensor *>(it->second);
123
124         if (!sensor) {
125                 _E("Failed to get sensor");
126                 return false;
127         }
128
129         if (!sensor->read_fd(ids))
130                 return false;
131
132         return true;
133 }
134
135 bool sensor_event_poller::process_event(int fd, const std::vector<uint32_t> &ids)
136 {
137         physical_sensor *sensor;
138         std::pair<fd_sensors_t::iterator, fd_sensors_t::iterator> ret;
139
140         /* find sensors which is based on same device(fd) */
141         ret = m_fd_sensors.equal_range(fd);
142
143         for (auto it_sensor = ret.first; it_sensor != ret.second; ++it_sensor) {
144                 sensor_event_t *event;
145                 sensor_data_t *data;
146                 int data_length;
147                 int remains = 1;
148
149                 sensor = it_sensor->second;
150
151                 /* check whether the id of this sensor is in id list(parameter) or not */
152                 auto result = std::find(std::begin(ids), std::end(ids), sensor->get_hal_id());
153
154                 if (result == std::end(ids))
155                         continue;
156
157                 while (remains > 0) {
158                         remains = sensor->get_data(&data, &data_length);
159                         if (remains < 0) {
160                                 _E("Failed to get sensor data");
161                                 break;
162                         }
163
164                         if (!sensor->on_event(data, data_length, remains)) {
165                                 free(data);
166                                 continue;
167                         }
168
169                         event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
170                         if (!event) {
171                                 _E("Memory allocation failed");
172                                 break;
173                         }
174
175                         event->sensor_id = sensor->get_id();
176                         event->event_type = sensor->get_event_type();
177                         event->data_length = data_length;
178                         event->data = data;
179
180                         if (!sensor->push(event)) {
181                                 free(event);
182                                 free(data);
183                         }
184                 }
185         }
186
187         return true;
188 }