sensord: compare previous interval with current interval
[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 : m_info(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         return start_internal();
87 }
88
89 int fusion_sensor_handler::stop(sensor_observer *ob)
90 {
91         retv_if(!m_sensor, -EINVAL);
92
93         int policy = OP_DEFAULT;
94
95         policy = m_sensor->stop(ob);
96         retv_if(policy <= OP_ERROR, policy);
97
98         remove_observer(ob);
99
100         if (policy == OP_DEFAULT) {
101                 if (observer_count() >= 1)
102                         return OP_SUCCESS; /* already started */
103         }
104
105         return stop_internal();
106 }
107
108 int fusion_sensor_handler::get_min_interval(void)
109 {
110         int interval;
111         std::vector<int> temp;
112
113         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
114                 if (it->second > 0)
115                     temp.push_back(it->second);
116
117         if (temp.empty())
118                 return m_info.get_min_interval();
119
120         interval = *std::min_element(temp.begin(), temp.end());
121
122         if (interval < m_info.get_min_interval())
123                 return m_info.get_min_interval();
124
125         return interval;
126 }
127
128 int fusion_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
129 {
130         retv_if(!m_sensor, -EINVAL);
131
132         int _interval = interval;
133         int policy = OP_DEFAULT;
134
135         policy = m_sensor->set_interval(ob, _interval);
136         retv_if(policy <= OP_ERROR, policy);
137
138         m_interval_map[ob] = interval;
139
140         if (policy == OP_DEFAULT)
141                 _interval = get_min_interval();
142
143         return set_interval_internal(_interval);
144 }
145
146 int fusion_sensor_handler::get_min_batch_latency(void)
147 {
148         int batch_latency;
149         std::vector<int> temp;
150
151         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
152                 if (it->second > 0)
153                     temp.push_back(it->second);
154
155         if (temp.empty())
156                 return 0;
157
158         batch_latency = *std::min_element(temp.begin(), temp.end());
159
160         return batch_latency;
161 }
162
163 int fusion_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
164 {
165         retv_if(!m_sensor, -EINVAL);
166
167         int _latency = latency;
168         int policy = OP_DEFAULT;
169
170         if (m_sensor) {
171                 policy = m_sensor->set_batch_latency(ob, _latency);
172                 retv_if(policy <= OP_ERROR, policy);
173         }
174
175         m_batch_latency_map[ob] = _latency;
176
177         if (policy == OP_DEFAULT)
178                 _latency = get_min_batch_latency();
179
180         return set_batch_latency_internal(_latency);
181 }
182
183 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
184 {
185         retv_if(!m_sensor, -EINVAL);
186
187         int policy = OP_DEFAULT;
188
189         policy = m_sensor->set_attribute(ob, attr, value);
190         retv_if(policy <= OP_ERROR, policy);
191
192         if (policy == OP_DEFAULT) {
193                 /* default logic */
194         }
195
196         return set_attribute_internal(attr, value);
197 }
198
199 int fusion_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
200 {
201         retv_if(!m_sensor, -EINVAL);
202
203         int policy = OP_DEFAULT;
204
205         policy = m_sensor->set_attribute(ob, attr, value, len);
206         retv_if(policy <= OP_ERROR, policy);
207
208         if (policy == OP_DEFAULT) {
209                 /* default logic */
210         }
211
212         return set_attribute_internal(attr, value, len);
213 }
214
215 int fusion_sensor_handler::get_data(sensor_data_t **data, int *len)
216 {
217         /* TODO */
218         return OP_SUCCESS;
219 }
220
221 int fusion_sensor_handler::flush(sensor_observer *ob)
222 {
223         retv_if(!m_sensor, -EINVAL);
224
225         m_sensor->flush(this);
226
227         return OP_SUCCESS;
228 }
229
230 int fusion_sensor_handler::start_internal(void)
231 {
232         auto it = m_required_sensors.begin();
233         for (; it != m_required_sensors.end(); ++it) {
234                 if (it->second.sensor->start(this) < 0)
235                         return OP_ERROR;
236         }
237
238         return OP_SUCCESS;
239 }
240
241 int fusion_sensor_handler::stop_internal(void)
242 {
243         auto it = m_required_sensors.begin();
244         for (; it != m_required_sensors.end(); ++it) {
245                 if (it->second.sensor->stop(this) < 0)
246                         return OP_ERROR;
247         }
248
249         return OP_SUCCESS;
250 }
251
252 int fusion_sensor_handler::set_interval_internal(int32_t interval)
253 {
254         auto it = m_required_sensors.begin();
255         for (; it != m_required_sensors.end(); ++it) {
256                 if (it->second.sensor->set_interval(this, interval) < 0)
257                         return OP_ERROR;
258         }
259
260         return OP_SUCCESS;
261 }
262
263 int fusion_sensor_handler::set_batch_latency_internal(int32_t latency)
264 {
265         auto it = m_required_sensors.begin();
266         for (; it != m_required_sensors.end(); ++it) {
267                 if (it->second.sensor->set_batch_latency(this, latency) < 0)
268                         return OP_ERROR;
269         }
270
271         return OP_SUCCESS;
272 }
273
274 int fusion_sensor_handler::set_attribute_internal(int32_t attr, int32_t value)
275 {
276         auto it = m_required_sensors.begin();
277         for (; it != m_required_sensors.end(); ++it) {
278                 if (it->second.sensor->set_attribute(this, attr, value) < 0)
279                         return OP_ERROR;
280         }
281
282         return OP_SUCCESS;
283 }
284
285 int fusion_sensor_handler::set_attribute_internal(int32_t attr, const char *value, int len)
286 {
287         auto it = m_required_sensors.begin();
288         for (; it != m_required_sensors.end(); ++it) {
289                 if (it->second.sensor->set_attribute(this, attr, value, len) < 0)
290                         return OP_ERROR;
291         }
292
293         return OP_SUCCESS;
294 }