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 <sensor_base.h>
22 #include <physical_sensor.h>
23 #include <sensor_event_poller.h>
24 #include <sensor_loader.h>
27 #define EPOLL_MAX_FD 32
29 sensor_event_poller::sensor_event_poller()
35 sensor_event_poller::~sensor_event_poller()
39 void sensor_event_poller::init_sensor_map()
42 physical_sensor *sensor;
44 std::vector<sensor_base *> sensors;
45 sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
47 auto it_sensor = sensors.begin();
49 while (it_sensor != sensors.end()) {
50 if ((*it_sensor)->is_virtual()) {
55 sensor = dynamic_cast<physical_sensor *>(*it_sensor);
56 fd = sensor->get_poll_fd();
58 m_fd_sensors.insert(std::make_pair(fd, sensor));
63 void sensor_event_poller::init_fd()
65 fd_sensors_t::iterator it;
66 for (it = m_fd_sensors.begin(); it != m_fd_sensors.end(); it = m_fd_sensors.upper_bound(it->first)) {
67 if (!add_poll_fd(it->first))
72 bool sensor_event_poller::add_poll_fd(int fd)
74 return m_poller.add_fd(fd);
77 bool sensor_event_poller::poll()
79 std::vector<uint32_t> ids;
82 struct epoll_event poll_event;
84 if (!m_poller.poll(poll_event))
87 fd = poll_event.data.fd;
90 if (!read_fd(fd, ids))
93 process_event(fd, ids);
99 bool sensor_event_poller::read_fd(int fd, std::vector<uint32_t> &ids)
101 fd_sensors_t::iterator it;
102 physical_sensor *sensor;
104 it = m_fd_sensors.find(fd);
105 sensor = dynamic_cast<physical_sensor *>(it->second);
108 _E("Failed to get sensor");
112 if (!sensor->read_fd(ids))
118 bool sensor_event_poller::process_event(int fd, const std::vector<uint32_t> &ids)
120 physical_sensor *sensor;
121 std::pair<fd_sensors_t::iterator, fd_sensors_t::iterator> ret;
123 ret = m_fd_sensors.equal_range(fd);
125 for (auto it_sensor = ret.first; it_sensor != ret.second; ++it_sensor) {
126 sensor_event_t *event;
131 sensor = it_sensor->second;
133 auto result = std::find(std::begin(ids), std::end(ids), sensor->get_hal_id());
135 if (result == std::end(ids))
138 while (remains > 0) {
139 event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
141 remains = sensor->get_data(&data, &data_length);
143 event->sensor_id = sensor->get_id();
144 event->event_type = sensor->get_event_type();
145 event->data_length = data_length;