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)
38 /* TODO: temporary walkaround */
39 switch (m_info.get_type()) {
41 case HRM_LED_GREEN_SENSOR:
42 case HRM_LED_IR_SENSOR:
43 case HRM_LED_RED_SENSOR:
44 case HUMAN_PEDOMETER_SENSOR:
45 case HUMAN_SLEEP_MONITOR_SENSOR:
46 case HUMAN_SLEEP_DETECTOR_SENSOR:
47 case HUMAN_STRESS_MONITOR_SENSOR:
48 m_info.set_privilege("http://tizen.org/privilege/healthinfo");
55 physical_sensor_handler::~physical_sensor_handler()
59 const sensor_info &physical_sensor_handler::get_sensor_info(void)
64 int physical_sensor_handler::get_hal_id(void)
69 int physical_sensor_handler::get_poll_fd(void)
71 retv_if(!m_device, -EINVAL);
73 return m_device->get_poll_fd();
76 int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
81 retv_if(!m_device, -EINVAL);
83 size = m_device->read_fd(&_ids);
84 retv_if(size == 0, -ENODATA);
86 for (int i = 0; i < size; ++i)
87 ids.push_back(_ids[i]);
92 int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
94 retv_if(!m_device, -EINVAL);
97 int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
98 retv_if(ret <= OP_ERROR, ret);
103 int physical_sensor_handler::start(sensor_observer *ob)
105 retv_if(!m_device, -EINVAL);
107 int policy = OP_DEFAULT;
110 policy = m_sensor->start(ob);
111 retv_if(policy <= OP_ERROR, policy);
116 if (policy == OP_DEFAULT) {
117 if (observer_count() > 1)
118 return OP_SUCCESS; /* already started */
121 return m_device->enable(m_hal_id);
124 int physical_sensor_handler::stop(sensor_observer *ob)
126 retv_if(!m_device, -EINVAL);
128 int policy = OP_DEFAULT;
131 policy = m_sensor->stop(ob);
132 retv_if(policy <= OP_ERROR, policy);
137 if (policy == OP_DEFAULT) {
138 if (observer_count() >= 1)
139 return OP_SUCCESS; /* already stopped */
142 return m_device->disable(m_hal_id);
145 int physical_sensor_handler::get_min_interval(void)
148 std::vector<int> temp;
150 for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
152 temp.push_back(it->second);
155 return m_info.get_min_interval();
157 interval = *std::min_element(temp.begin(), temp.end());
159 if (interval < m_info.get_min_interval())
160 return m_info.get_min_interval();
165 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
167 retv_if(!m_device, -EINVAL);
170 int32_t cur_interval = interval;
171 int policy = OP_DEFAULT;
174 policy = m_sensor->set_interval(ob, cur_interval);
175 retv_if(policy <= OP_ERROR, policy);
178 m_interval_map[ob] = cur_interval;
180 if (policy == OP_DEFAULT)
181 cur_interval = get_min_interval();
183 retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
185 ret = m_device->set_interval(m_hal_id, cur_interval);
187 m_prev_interval = cur_interval;
189 return (ret ? OP_SUCCESS : OP_ERROR);
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)
199 temp.push_back(it->second);
204 batch_latency = *std::min_element(temp.begin(), temp.end());
206 return batch_latency;
209 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
211 retv_if(!m_device, -EINVAL);
214 int _latency = latency;
215 int policy = OP_DEFAULT;
218 policy = m_sensor->set_batch_latency(ob, latency);
219 retv_if(policy <= OP_ERROR, policy);
222 m_batch_latency_map[ob] = _latency;
224 if (_latency <= latency)
227 ret = m_device->set_batch_latency(m_hal_id, _latency);
229 return (ret ? OP_SUCCESS : OP_ERROR);
232 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
234 retv_if(!m_device, -EINVAL);
237 int policy = OP_DEFAULT;
240 policy = m_sensor->set_attribute(ob, attr, value);
241 retv_if(policy <= OP_ERROR, policy);
245 * TODO: default policy for attribute?
246 if (policy == OP_DEFAULT) {
247 if (observer_count() > 1)
252 ret = m_device->set_attribute_int(m_hal_id, attr, value);
254 return (ret ? OP_SUCCESS : OP_ERROR);
257 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
259 retv_if(!m_device, -EINVAL);
262 int policy = OP_DEFAULT;
265 policy = m_sensor->set_attribute(ob, attr, value, len);
266 retv_if(policy <= OP_ERROR, policy);
270 * TODO: default policy for attribute?
271 if (policy == OP_DEFAULT) {
272 if (observer_count() > 1)
277 ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
279 return (ret ? OP_SUCCESS : OP_ERROR);
282 int physical_sensor_handler::flush(sensor_observer *ob)
284 retv_if(!m_device, -EINVAL);
288 ret = m_sensor->flush(ob);
289 retv_if(ret <= OP_ERROR, ret);
292 ret = m_device->flush(m_hal_id);
294 return (ret ? OP_SUCCESS : OP_ERROR);
297 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
299 retv_if(!m_device, -EINVAL);
301 int remains = m_device->get_data(m_hal_id, data, len);
302 retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");