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 "fusion_sensor_handler.h"
23 #include <sensor_log.h>
26 using namespace sensor;
28 fusion_sensor_handler::fusion_sensor_handler(const sensor_info &info,
29 fusion_sensor *sensor)
30 : sensor_handler(info)
35 fusion_sensor_handler::~fusion_sensor_handler()
37 m_required_sensors.clear();
40 void fusion_sensor_handler::add_required_sensor(uint32_t id, sensor_handler *sensor)
42 sensor_info info = sensor->get_sensor_info();
43 m_required_sensors.emplace(info.get_uri(), required_sensor(id, sensor));
46 int fusion_sensor_handler::update(const char *uri, ipc::message *msg)
48 retv_if(!m_sensor, -EINVAL);
50 auto it = m_required_sensors.find(uri);
51 retv_if(it == m_required_sensors.end(), OP_SUCCESS);
53 if (m_sensor->update(it->second.id, (sensor_data_t *)msg->body(), msg->size()) < 0)
59 if (m_sensor->get_data(&data, &len) < 0)
62 return notify(m_info.get_uri().c_str(), data, len);
65 const sensor_info &fusion_sensor_handler::get_sensor_info(void)
70 int fusion_sensor_handler::start(sensor_observer *ob)
72 retv_if(!m_sensor, -EINVAL);
74 int policy = OP_DEFAULT;
76 policy = m_sensor->start(ob);
77 retv_if(policy <= OP_ERROR, policy);
81 if (policy == OP_DEFAULT) {
82 if (observer_count() > 1)
86 return start_internal();
89 int fusion_sensor_handler::stop(sensor_observer *ob)
91 retv_if(!m_sensor, -EINVAL);
93 int policy = OP_DEFAULT;
95 policy = m_sensor->stop(ob);
96 retv_if(policy <= OP_ERROR, policy);
100 if (policy == OP_DEFAULT) {
101 if (observer_count() >= 1)
102 return OP_SUCCESS; /* already started */
105 return stop_internal();
108 int fusion_sensor_handler::get_min_interval(void)
111 std::vector<int> temp;
113 for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
115 temp.push_back(it->second);
118 return m_info.get_min_interval();
120 interval = *std::min_element(temp.begin(), temp.end());
122 if (interval < m_info.get_min_interval())
123 return m_info.get_min_interval();
128 int fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
130 retv_if(!m_sensor, -EINVAL);
132 int _interval = interval;
133 int policy = OP_DEFAULT;
135 policy = m_sensor->set_interval(ob, _interval);
136 retv_if(policy <= OP_ERROR, policy);
138 m_interval_map[ob] = interval;
140 if (policy == OP_DEFAULT)
141 _interval = get_min_interval();
143 return set_interval_internal(_interval);
146 int fusion_sensor_handler::get_min_batch_latency(void)
149 std::vector<int> temp;
151 for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
153 temp.push_back(it->second);
158 batch_latency = *std::min_element(temp.begin(), temp.end());
160 return batch_latency;
163 int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
165 retv_if(!m_sensor, -EINVAL);
167 int _latency = latency;
168 int policy = OP_DEFAULT;
171 policy = m_sensor->set_batch_latency(ob, _latency);
172 retv_if(policy <= OP_ERROR, policy);
175 m_batch_latency_map[ob] = _latency;
177 if (policy == OP_DEFAULT)
178 _latency = get_min_batch_latency();
180 return set_batch_latency_internal(_latency);
183 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
185 retv_if(!m_sensor, -EINVAL);
187 int policy = OP_DEFAULT;
189 policy = m_sensor->set_attribute(ob, attr, value);
190 retv_if(policy <= OP_ERROR, policy);
192 if (policy == OP_DEFAULT) {
196 return set_attribute_internal(attr, value);
199 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
201 retv_if(!m_sensor, -EINVAL);
203 int policy = OP_DEFAULT;
205 policy = m_sensor->set_attribute(ob, attr, value, len);
206 retv_if(policy <= OP_ERROR, policy);
208 if (policy == OP_DEFAULT) {
212 return set_attribute_internal(attr, value, len);
215 int fusion_sensor_handler::get_data(sensor_data_t **data, int *len)
217 retv_if(!m_sensor, -EINVAL);
219 return m_sensor->get_data(data, len);
222 int fusion_sensor_handler::flush(sensor_observer *ob)
224 retv_if(!m_sensor, -EINVAL);
226 m_sensor->flush(this);
231 int fusion_sensor_handler::start_internal(void)
233 auto it = m_required_sensors.begin();
234 for (; it != m_required_sensors.end(); ++it) {
235 if (it->second.sensor->start(this) < 0)
242 int fusion_sensor_handler::stop_internal(void)
244 auto it = m_required_sensors.begin();
245 for (; it != m_required_sensors.end(); ++it) {
246 if (it->second.sensor->stop(this) < 0)
253 int fusion_sensor_handler::set_interval_internal(int32_t interval)
255 auto it = m_required_sensors.begin();
256 for (; it != m_required_sensors.end(); ++it) {
257 if (it->second.sensor->set_interval(this, interval) < 0)
264 int fusion_sensor_handler::set_batch_latency_internal(int32_t latency)
266 auto it = m_required_sensors.begin();
267 for (; it != m_required_sensors.end(); ++it) {
268 if (it->second.sensor->set_batch_latency(this, latency) < 0)
275 int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value)
277 auto it = m_required_sensors.begin();
278 for (; it != m_required_sensors.end(); ++it) {
279 if (it->second.sensor->set_attribute(this, attr, value) < 0)
286 int fusion_sensor_handler::set_attribute_internal(int32_t attr, const char *value, int len)
288 auto it = m_required_sensors.begin();
289 for (; it != m_required_sensors.end(); ++it) {
290 if (it->second.sensor->set_attribute(this, attr, value, len) < 0)