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 _I("Started[%s]", m_info.get_uri().c_str());
88 return start_internal();
91 int fusion_sensor_handler::stop(sensor_observer *ob)
93 retv_if(!m_sensor, -EINVAL);
95 int policy = OP_DEFAULT;
97 policy = m_sensor->stop(ob);
98 retv_if(policy <= OP_ERROR, policy);
102 if (policy == OP_DEFAULT) {
103 if (observer_count() >= 1)
104 return OP_SUCCESS; /* already started */
107 _I("Stopped[%s]", m_info.get_uri().c_str());
109 return stop_internal();
112 int fusion_sensor_handler::get_min_interval(void)
115 std::vector<int> temp;
117 for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
119 temp.push_back(it->second);
122 return m_info.get_min_interval();
124 interval = *std::min_element(temp.begin(), temp.end());
126 if (interval < m_info.get_min_interval())
127 return m_info.get_min_interval();
132 int fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
134 retv_if(!m_sensor, -EINVAL);
136 int _interval = interval;
137 int policy = OP_DEFAULT;
139 policy = m_sensor->set_interval(ob, _interval);
140 retv_if(policy <= OP_ERROR, policy);
142 m_interval_map[ob] = interval;
144 if (policy == OP_DEFAULT)
145 _interval = get_min_interval();
147 _I("Set interval[%d] to sensor[%s]", _interval, m_info.get_uri().c_str());
149 return set_interval_internal(_interval);
152 int fusion_sensor_handler::get_min_batch_latency(void)
155 std::vector<int> temp;
157 for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
159 temp.push_back(it->second);
164 batch_latency = *std::min_element(temp.begin(), temp.end());
166 return batch_latency;
169 int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
171 retv_if(!m_sensor, -EINVAL);
173 int _latency = latency;
174 int policy = OP_DEFAULT;
177 policy = m_sensor->set_batch_latency(ob, _latency);
178 retv_if(policy <= OP_ERROR, policy);
181 m_batch_latency_map[ob] = _latency;
183 if (policy == OP_DEFAULT)
184 _latency = get_min_batch_latency();
186 return set_batch_latency_internal(_latency);
189 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
191 retv_if(!m_sensor, -EINVAL);
193 int policy = OP_DEFAULT;
195 policy = m_sensor->set_attribute(ob, attr, value);
196 retv_if(policy <= OP_ERROR, policy);
198 if (policy == OP_DEFAULT) {
202 return set_attribute_internal(attr, value);
205 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
207 retv_if(!m_sensor, -EINVAL);
209 int policy = OP_DEFAULT;
211 policy = m_sensor->set_attribute(ob, attr, value, len);
212 retv_if(policy <= OP_ERROR, policy);
214 if (policy == OP_DEFAULT) {
218 return set_attribute_internal(attr, value, len);
221 int fusion_sensor_handler::get_data(sensor_data_t **data, int *len)
223 retv_if(!m_sensor, -EINVAL);
225 return m_sensor->get_data(data, len);
228 int fusion_sensor_handler::flush(sensor_observer *ob)
230 retv_if(!m_sensor, -EINVAL);
232 m_sensor->flush(this);
237 int fusion_sensor_handler::start_internal(void)
239 auto it = m_required_sensors.begin();
240 for (; it != m_required_sensors.end(); ++it) {
241 if (it->second.sensor->start(this) < 0)
248 int fusion_sensor_handler::stop_internal(void)
250 auto it = m_required_sensors.begin();
251 for (; it != m_required_sensors.end(); ++it) {
252 if (it->second.sensor->stop(this) < 0)
259 int fusion_sensor_handler::set_interval_internal(int32_t interval)
261 auto it = m_required_sensors.begin();
262 for (; it != m_required_sensors.end(); ++it) {
263 if (it->second.sensor->set_interval(this, interval) < 0)
270 int fusion_sensor_handler::set_batch_latency_internal(int32_t latency)
272 auto it = m_required_sensors.begin();
273 for (; it != m_required_sensors.end(); ++it) {
274 if (it->second.sensor->set_batch_latency(this, latency) < 0)
281 int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value)
283 auto it = m_required_sensors.begin();
284 for (; it != m_required_sensors.end(); ++it) {
285 if (it->second.sensor->set_attribute(this, attr, value) < 0)
292 int fusion_sensor_handler::set_attribute_internal(int32_t attr, const char *value, int len)
294 auto it = m_required_sensors.begin();
295 for (; it != m_required_sensors.end(); ++it) {
296 if (it->second.sensor->set_attribute(this, attr, value, len) < 0)