4 * Copyright (c) 2016 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 <sys/signalfd.h>
22 #include <sensor_base.h>
23 #include <physical_sensor.h>
24 #include <sensor_event_poller.h>
25 #include <sensor_loader.h>
29 sensor_event_poller::sensor_event_poller()
36 sensor_event_poller::~sensor_event_poller()
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);
43 void sensor_event_poller::init_sensor_map(void)
46 physical_sensor *sensor;
48 std::vector<sensor_base *> sensors;
49 sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
51 std::vector<sensor_base *>::iterator it;
53 for (it = sensors.begin(); it != sensors.end(); ++it) {
54 sensor = dynamic_cast<physical_sensor *>(*it);
58 fd = sensor->get_poll_fd();
63 m_fd_sensors.insert(std::make_pair(fd, sensor));
67 void sensor_event_poller::init_fd(void)
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);
76 void sensor_event_poller::init_signal_fd(void)
82 sigaddset(&mask, SIGTERM);
83 sigaddset(&mask, SIGABRT);
84 sigaddset(&mask, SIGINT);
86 sfd = signalfd(-1, &mask, 0);
87 m_poller.add_signal_fd(sfd);
90 bool sensor_event_poller::add_poll_fd(int fd)
92 return m_poller.add_fd(fd);
95 bool sensor_event_poller::poll(void)
97 std::vector<uint32_t> ids;
100 struct epoll_event poll_event;
102 if (!m_poller.poll(poll_event))
105 fd = poll_event.data.fd;
108 if (!read_fd(fd, ids))
111 if (!process_event(fd, ids))
116 bool sensor_event_poller::read_fd(int fd, std::vector<uint32_t> &ids)
118 fd_sensors_t::iterator it;
119 physical_sensor *sensor;
121 it = m_fd_sensors.find(fd);
122 sensor = dynamic_cast<physical_sensor *>(it->second);
125 _E("Failed to get sensor");
129 if (!sensor->read_fd(ids))
135 bool sensor_event_poller::process_event(int fd, const std::vector<uint32_t> &ids)
137 physical_sensor *sensor;
138 std::pair<fd_sensors_t::iterator, fd_sensors_t::iterator> ret;
140 /* find sensors which is based on same device(fd) */
141 ret = m_fd_sensors.equal_range(fd);
143 for (auto it_sensor = ret.first; it_sensor != ret.second; ++it_sensor) {
144 sensor_event_t *event;
149 sensor = it_sensor->second;
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());
154 if (result == std::end(ids))
157 while (remains > 0) {
158 remains = sensor->get_data(&data, &data_length);
160 _E("Failed to get sensor data");
164 if (!sensor->on_event(data, data_length, remains)) {
169 event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
171 _E("Memory allocation failed");
175 event->sensor_id = sensor->get_id();
176 event->event_type = sensor->get_event_type();
177 event->data_length = data_length;
180 if (!sensor->push(event)) {