Merge branch 'devel/tizen' into tizen
[platform/core/system/sensord.git] / src / server / fusion_sensor_handler.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "fusion_sensor_handler.h"
21
22 #include <message.h>
23 #include <sensor_log.h>
24 #include <algorithm>
25
26 using namespace sensor;
27
28 fusion_sensor_handler::fusion_sensor_handler(const sensor_info &info,
29                 fusion_sensor *sensor)
30 : sensor_handler(info)
31 , m_sensor(sensor)
32 {
33 }
34
35 fusion_sensor_handler::~fusion_sensor_handler()
36 {
37         m_required_sensors.clear();
38 }
39
40 void fusion_sensor_handler::add_required_sensor(uint32_t id, sensor_handler *sensor)
41 {
42         sensor_info info = sensor->get_sensor_info();
43         m_required_sensors.emplace(info.get_uri(), required_sensor(id, sensor));
44 }
45
46 int fusion_sensor_handler::update(const char *uri, ipc::message *msg)
47 {
48         retv_if(!m_sensor, -EINVAL);
49
50         auto it = m_required_sensors.find(uri);
51         retv_if(it == m_required_sensors.end(), OP_SUCCESS);
52
53         if (m_sensor->update(it->second.id, (sensor_data_t *)msg->body(), msg->size()) < 0)
54                 return OP_SUCCESS;
55
56         sensor_data_t *data;
57         int len;
58
59         if (m_sensor->get_data(&data, &len) < 0)
60                 return OP_ERROR;
61
62         return notify(m_info.get_uri().c_str(), data, len);
63 }
64
65 const sensor_info &fusion_sensor_handler::get_sensor_info(void)
66 {
67         return m_info;
68 }
69
70 int fusion_sensor_handler::start(sensor_observer *ob)
71 {
72         retv_if(!m_sensor, -EINVAL);
73
74         int policy = OP_DEFAULT;
75
76         policy = m_sensor->start(ob);
77         retv_if(policy <= OP_ERROR, policy);
78
79         add_observer(ob);
80
81         if (policy == OP_DEFAULT) {
82                 if (observer_count() > 1)
83                         return OP_SUCCESS;
84         }
85
86         _I("Started[%s]", m_info.get_uri().c_str());
87
88         return start_internal();
89 }
90
91 int fusion_sensor_handler::stop(sensor_observer *ob)
92 {
93         retv_if(!m_sensor, -EINVAL);
94
95         int policy = OP_DEFAULT;
96
97         policy = m_sensor->stop(ob);
98         retv_if(policy <= OP_ERROR, policy);
99
100         remove_observer(ob);
101
102         if (policy == OP_DEFAULT) {
103                 if (observer_count() >= 1)
104                         return OP_SUCCESS; /* already started */
105         }
106
107         _I("Stopped[%s]", m_info.get_uri().c_str());
108
109         return stop_internal();
110 }
111
112 int fusion_sensor_handler::get_min_interval(void)
113 {
114         int interval;
115         std::vector<int> temp;
116
117         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
118                 if (it->second > 0)
119                     temp.push_back(it->second);
120
121         if (temp.empty())
122                 return m_info.get_min_interval();
123
124         interval = *std::min_element(temp.begin(), temp.end());
125
126         if (interval < m_info.get_min_interval())
127                 return m_info.get_min_interval();
128
129         return interval;
130 }
131
132 int fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
133 {
134         retv_if(!m_sensor, -EINVAL);
135
136         int _interval = interval;
137         int policy = OP_DEFAULT;
138
139         policy = m_sensor->set_interval(ob, _interval);
140         retv_if(policy <= OP_ERROR, policy);
141
142         m_interval_map[ob] = interval;
143
144         if (policy == OP_DEFAULT)
145                 _interval = get_min_interval();
146
147         _I("Set interval[%d] to sensor[%s]", _interval, m_info.get_uri().c_str());
148
149         return set_interval_internal(_interval);
150 }
151
152 int fusion_sensor_handler::get_min_batch_latency(void)
153 {
154         int batch_latency;
155         std::vector<int> temp;
156
157         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
158                 if (it->second > 0)
159                     temp.push_back(it->second);
160
161         if (temp.empty())
162                 return 0;
163
164         batch_latency = *std::min_element(temp.begin(), temp.end());
165
166         return batch_latency;
167 }
168
169 int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
170 {
171         retv_if(!m_sensor, -EINVAL);
172
173         int _latency = latency;
174         int policy = OP_DEFAULT;
175
176         if (m_sensor) {
177                 policy = m_sensor->set_batch_latency(ob, _latency);
178                 retv_if(policy <= OP_ERROR, policy);
179         }
180
181         m_batch_latency_map[ob] = _latency;
182
183         if (policy == OP_DEFAULT)
184                 _latency = get_min_batch_latency();
185
186         return set_batch_latency_internal(_latency);
187 }
188
189 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
190 {
191         retv_if(!m_sensor, -EINVAL);
192
193         int policy = OP_DEFAULT;
194
195         policy = m_sensor->set_attribute(ob, attr, value);
196         retv_if(policy <= OP_ERROR, policy);
197
198         if (policy == OP_DEFAULT) {
199                 /* default logic */
200         }
201
202         return set_attribute_internal(attr, value);
203 }
204
205 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
206 {
207         retv_if(!m_sensor, -EINVAL);
208
209         int policy = OP_DEFAULT;
210
211         policy = m_sensor->set_attribute(ob, attr, value, len);
212         retv_if(policy <= OP_ERROR, policy);
213
214         if (policy == OP_DEFAULT) {
215                 /* default logic */
216         }
217
218         return set_attribute_internal(attr, value, len);
219 }
220
221 int fusion_sensor_handler::get_data(sensor_data_t **data, int *len)
222 {
223         retv_if(!m_sensor, -EINVAL);
224
225         return m_sensor->get_data(data, len);
226 }
227
228 int fusion_sensor_handler::flush(sensor_observer *ob)
229 {
230         retv_if(!m_sensor, -EINVAL);
231
232         m_sensor->flush(this);
233
234         return OP_SUCCESS;
235 }
236
237 int fusion_sensor_handler::start_internal(void)
238 {
239         auto it = m_required_sensors.begin();
240         for (; it != m_required_sensors.end(); ++it) {
241                 if (it->second.sensor->start(this) < 0)
242                         return OP_ERROR;
243         }
244
245         return OP_SUCCESS;
246 }
247
248 int fusion_sensor_handler::stop_internal(void)
249 {
250         auto it = m_required_sensors.begin();
251         for (; it != m_required_sensors.end(); ++it) {
252                 if (it->second.sensor->stop(this) < 0)
253                         return OP_ERROR;
254         }
255
256         return OP_SUCCESS;
257 }
258
259 int fusion_sensor_handler::set_interval_internal(int32_t interval)
260 {
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)
264                         return OP_ERROR;
265         }
266
267         return OP_SUCCESS;
268 }
269
270 int fusion_sensor_handler::set_batch_latency_internal(int32_t latency)
271 {
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)
275                         return OP_ERROR;
276         }
277
278         return OP_SUCCESS;
279 }
280
281 int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value)
282 {
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)
286                         return OP_ERROR;
287         }
288
289         return OP_SUCCESS;
290 }
291
292 int fusion_sensor_handler::set_attribute_internal(int32_t attr, const char *value, int len)
293 {
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)
297                         return OP_ERROR;
298         }
299
300         return OP_SUCCESS;
301 }