4 * Copyright (c) 2017 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.
20 #include "physical_sensor_handler.h"
22 #include <sensor_log.h>
23 #include <command_types.h>
27 using namespace sensor;
29 physical_sensor_handler::physical_sensor_handler(const sensor_info &info,
30 sensor_device *device, int hal_id,
31 physical_sensor *sensor)
32 : sensor_handler(info)
39 physical_sensor_handler::~physical_sensor_handler()
43 const sensor_info &physical_sensor_handler::get_sensor_info(void)
48 int physical_sensor_handler::get_hal_id(void)
53 int physical_sensor_handler::get_poll_fd(void)
55 retv_if(!m_device, -EINVAL);
57 return m_device->get_poll_fd();
60 int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
62 retv_if(!m_device, -EINVAL);
67 size = m_device->read_fd(&_ids);
68 retv_if(size == 0, -ENODATA);
70 for (int i = 0; i < size; ++i)
71 ids.push_back(_ids[i]);
76 int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
78 retv_if(!m_device, -EINVAL);
81 int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
82 retv_if(ret <= OP_ERROR, ret);
87 int physical_sensor_handler::start(sensor_observer *ob)
89 retv_if(!m_device, -EINVAL);
92 int policy = OP_DEFAULT;
94 ret = add_observer(ob);
95 retvm_if(!ret, OP_SUCCESS, "Listener is already added");
98 policy = m_sensor->start(ob);
99 if (policy <= OP_ERROR) {
105 if (policy == OP_DEFAULT) {
106 if (observer_count() > 1)
107 return OP_SUCCESS; /* already started */
110 _I("Started[%s]", m_info.get_uri().c_str());
112 return m_device->enable(m_hal_id);
115 int physical_sensor_handler::stop(sensor_observer *ob)
117 retv_if(!m_device, -EINVAL);
119 int policy = OP_DEFAULT;
122 policy = m_sensor->stop(ob);
123 retv_if(policy <= OP_ERROR, policy);
128 if (policy == OP_DEFAULT) {
129 if (observer_count() >= 1)
130 return OP_SUCCESS; /* already stopped */
133 _I("Stopped[%s]", m_info.get_uri().c_str());
135 return m_device->disable(m_hal_id);
138 int physical_sensor_handler::get_min_interval(void)
141 std::vector<int> temp;
143 for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
145 temp.push_back(it->second);
148 return m_info.get_min_interval();
150 interval = *std::min_element(temp.begin(), temp.end());
152 if (interval < m_info.get_min_interval())
153 return m_info.get_min_interval();
158 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
160 retv_if(!m_device, -EINVAL);
163 int32_t cur_interval = interval;
164 int policy = OP_DEFAULT;
167 policy = m_sensor->set_interval(ob, cur_interval);
168 retv_if(policy <= OP_ERROR, policy);
171 m_interval_map[ob] = cur_interval;
173 if (policy == OP_DEFAULT)
174 cur_interval = get_min_interval();
176 retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
178 ret = m_device->set_interval(m_hal_id, cur_interval);
180 update_prev_interval(cur_interval);
182 return (ret ? OP_SUCCESS : OP_ERROR);
185 int physical_sensor_handler::get_interval(sensor_observer *ob, int32_t& interval)
187 retv_if(!m_device, -EINVAL);
188 interval = m_prev_interval;
192 int physical_sensor_handler::get_min_batch_latency(void)
195 std::vector<int> temp;
197 for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
198 temp.push_back(it->second);
203 batch_latency = *std::min_element(temp.begin(), temp.end());
205 return batch_latency;
208 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
210 retv_if(!m_device, -EINVAL);
213 int32_t cur_latency = latency;
214 int policy = OP_DEFAULT;
217 policy = m_sensor->set_batch_latency(ob, latency);
218 retv_if(policy <= OP_ERROR, policy);
221 m_batch_latency_map[ob] = cur_latency;
223 if (policy == OP_DEFAULT)
224 cur_latency = get_min_batch_latency();
226 retv_if(m_prev_latency == cur_latency, OP_SUCCESS);
228 ret = m_device->set_batch_latency(m_hal_id, cur_latency);
230 update_prev_latency(cur_latency);
232 return (ret ? OP_SUCCESS : OP_ERROR);
235 int physical_sensor_handler::get_batch_latency(sensor_observer *ob, int32_t &latency)
237 retv_if(!m_device, -EINVAL);
238 latency = m_prev_latency;
242 int physical_sensor_handler::delete_batch_latency(sensor_observer *ob)
245 int policy = OP_DEFAULT;
248 m_batch_latency_map.erase(ob);
250 latency = get_min_batch_latency();
251 retv_if(m_prev_latency == latency, OP_SUCCESS);
253 ret = m_device->set_batch_latency(m_hal_id, latency);
255 m_prev_latency = latency;
257 return (ret ? OP_SUCCESS : OP_ERROR);
260 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
262 retv_if(!m_device, -EINVAL);
265 int policy = OP_DEFAULT;
268 policy = m_sensor->set_attribute(ob, attr, value);
269 retv_if(policy <= OP_ERROR, policy);
273 * TODO: default policy for attribute?
274 if (policy == OP_DEFAULT) {
275 if (observer_count() > 1)
280 ret = m_device->set_attribute_int(m_hal_id, attr, value);
283 update_attribute(attr, value);
285 return (ret ? OP_SUCCESS : OP_ERROR);
288 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
290 retv_if(!m_device, -EINVAL);
293 int policy = OP_DEFAULT;
296 policy = m_sensor->set_attribute(ob, attr, value, len);
297 retv_if(policy <= OP_ERROR, policy);
301 * TODO: default policy for attribute?
302 if (policy == OP_DEFAULT) {
303 if (observer_count() > 1)
308 ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
311 update_attribute(attr, value, len);
314 return (ret ? OP_SUCCESS : OP_ERROR);
317 int physical_sensor_handler::get_attribute(int32_t attr, int32_t *value)
319 retv_if(!m_device, -EINVAL);
322 int policy = OP_DEFAULT;
324 ret = m_device->get_attribute_int(m_hal_id, attr, value);
325 return (ret ? OP_SUCCESS : OP_ERROR);
328 int physical_sensor_handler::get_attribute(int32_t attr, char **value, int *len)
330 retv_if(!m_device, -EINVAL);
333 int policy = OP_DEFAULT;
335 ret = m_device->get_attribute_str(m_hal_id, attr, value, len);
336 return (ret ? OP_SUCCESS : OP_ERROR);
339 int physical_sensor_handler::flush(sensor_observer *ob)
341 retv_if(!m_device, -EINVAL);
345 ret = m_sensor->flush(ob);
346 retv_if(ret <= OP_ERROR, ret);
349 ret = m_device->flush(m_hal_id);
351 return (ret ? OP_SUCCESS : OP_ERROR);
354 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
356 retv_if(!m_device, -EINVAL);
358 int remains = m_device->get_data(m_hal_id, data, len);
359 retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");