sensord: compare previous interval with current interval
[platform/core/system/sensord.git] / src / server / physical_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 "physical_sensor_handler.h"
21
22 #include <sensor_log.h>
23 #include <command_types.h>
24 #include <message.h>
25 #include <algorithm>
26
27 using namespace sensor;
28
29 physical_sensor_handler::physical_sensor_handler(const sensor_info &info,
30                 sensor_device *device, int hal_id,
31                 physical_sensor *sensor)
32 : m_info(info)
33 , m_device(device)
34 , m_sensor(sensor)
35 , m_hal_id(hal_id)
36 , m_prev_interval(0)
37 {
38         /* TODO: temporary walkaround */
39         switch (m_info.get_type()) {
40         case HRM_SENSOR:
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");
49                 break;
50         default:
51                 break;
52         };
53 }
54
55 physical_sensor_handler::~physical_sensor_handler()
56 {
57 }
58
59 const sensor_info &physical_sensor_handler::get_sensor_info(void)
60 {
61         return m_info;
62 }
63
64 int physical_sensor_handler::get_hal_id(void)
65 {
66         return m_hal_id;
67 }
68
69 int physical_sensor_handler::get_poll_fd(void)
70 {
71         retv_if(!m_device, -EINVAL);
72
73         return m_device->get_poll_fd();
74 }
75
76 int physical_sensor_handler::read_fd(std::vector<uint32_t> &ids)
77 {
78         int size;
79         uint32_t *_ids;
80
81         retv_if(!m_device, -EINVAL);
82
83         size = m_device->read_fd(&_ids);
84         retv_if(size == 0, -ENODATA);
85
86         for (int i = 0; i < size; ++i)
87                 ids.push_back(_ids[i]);
88
89         return OP_SUCCESS;
90 }
91
92 int physical_sensor_handler::on_event(const sensor_data_t *data, int32_t len, int32_t remains)
93 {
94         retv_if(!m_device, -EINVAL);
95
96         if (m_sensor) {
97                 int ret = m_sensor->on_event(const_cast<sensor_data_t *>(data), len, remains);
98                 retv_if(ret <= OP_ERROR, ret);
99         }
100
101         return OP_SUCCESS;
102 }
103 int physical_sensor_handler::start(sensor_observer *ob)
104 {
105         retv_if(!m_device, -EINVAL);
106
107         int policy = OP_DEFAULT;
108
109         if (m_sensor) {
110                 policy = m_sensor->start(ob);
111                 retv_if(policy <= OP_ERROR, policy);
112         }
113
114         add_observer(ob);
115
116         if (policy == OP_DEFAULT) {
117                 if (observer_count() > 1)
118                         return OP_SUCCESS; /* already started */
119         }
120
121         return m_device->enable(m_hal_id);
122 }
123
124 int physical_sensor_handler::stop(sensor_observer *ob)
125 {
126         retv_if(!m_device, -EINVAL);
127
128         int policy = OP_DEFAULT;
129
130         if (m_sensor) {
131                 policy = m_sensor->stop(ob);
132                 retv_if(policy <= OP_ERROR, policy);
133         }
134
135         remove_observer(ob);
136
137         if (policy == OP_DEFAULT) {
138                 if (observer_count() >= 1)
139                         return OP_SUCCESS; /* already stopped */
140         }
141
142         return m_device->disable(m_hal_id);
143 }
144
145 int physical_sensor_handler::get_min_interval(void)
146 {
147         int interval;
148         std::vector<int> temp;
149
150         for (auto it = m_interval_map.begin(); it != m_interval_map.end(); ++it)
151                 if (it->second > 0)
152                     temp.push_back(it->second);
153
154         if (temp.empty())
155                 return m_info.get_min_interval();
156
157         interval = *std::min_element(temp.begin(), temp.end());
158
159         if (interval < m_info.get_min_interval())
160                 return m_info.get_min_interval();
161
162         return interval;
163 }
164
165 int physical_sensor_handler::set_interval(sensor_observer *ob, int32_t interval)
166 {
167         retv_if(!m_device, -EINVAL);
168
169         bool ret = false;
170         int32_t cur_interval = interval;
171         int policy = OP_DEFAULT;
172
173         if (m_sensor) {
174                 policy = m_sensor->set_interval(ob, cur_interval);
175                 retv_if(policy <= OP_ERROR, policy);
176         }
177
178         m_interval_map[ob] = cur_interval;
179
180         if (policy == OP_DEFAULT)
181                 cur_interval = get_min_interval();
182
183         retv_if(m_prev_interval == cur_interval, OP_SUCCESS);
184
185         ret = m_device->set_interval(m_hal_id, cur_interval);
186
187         m_prev_interval = cur_interval;
188
189         return (ret ? OP_SUCCESS : OP_ERROR);
190 }
191
192 int physical_sensor_handler::get_min_batch_latency(void)
193 {
194         int batch_latency;
195         std::vector<int> temp;
196
197         for (auto it = m_batch_latency_map.begin(); it != m_batch_latency_map.end(); ++it)
198                 if (it->second > 0)
199                     temp.push_back(it->second);
200
201         if (temp.empty())
202                 return 0;
203
204         batch_latency = *std::min_element(temp.begin(), temp.end());
205
206         return batch_latency;
207 }
208
209 int physical_sensor_handler::set_batch_latency(sensor_observer *ob, int32_t latency)
210 {
211         retv_if(!m_device, -EINVAL);
212
213         bool ret = false;
214         int _latency = latency;
215         int policy = OP_DEFAULT;
216
217         if (m_sensor) {
218                 policy = m_sensor->set_batch_latency(ob, latency);
219                 retv_if(policy <= OP_ERROR, policy);
220         }
221
222         m_batch_latency_map[ob] = _latency;
223
224         if (_latency <= latency)
225                 return OP_SUCCESS;
226
227         ret = m_device->set_batch_latency(m_hal_id, _latency);
228
229         return (ret ? OP_SUCCESS : OP_ERROR);
230 }
231
232 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, int32_t value)
233 {
234         retv_if(!m_device, -EINVAL);
235
236         bool ret = false;
237         int policy = OP_DEFAULT;
238
239         if (m_sensor) {
240                 policy = m_sensor->set_attribute(ob, attr, value);
241                 retv_if(policy <= OP_ERROR, policy);
242         }
243
244         /*
245          * TODO: default policy for attribute?
246         if (policy == OP_DEFAULT) {
247                 if (observer_count() > 1)
248                         return OP_SUCCESS;
249         }
250         */
251
252         ret = m_device->set_attribute_int(m_hal_id, attr, value);
253
254         return (ret ? OP_SUCCESS : OP_ERROR);
255 }
256
257 int physical_sensor_handler::set_attribute(sensor_observer *ob, int32_t attr, const char *value, int len)
258 {
259         retv_if(!m_device, -EINVAL);
260
261         bool ret = false;
262         int policy = OP_DEFAULT;
263
264         if (m_sensor) {
265                 policy = m_sensor->set_attribute(ob, attr, value, len);
266                 retv_if(policy <= OP_ERROR, policy);
267         }
268
269         /*
270          * TODO: default policy for attribute?
271         if (policy == OP_DEFAULT) {
272                 if (observer_count() > 1)
273                         return OP_SUCCESS;
274         }
275         */
276
277         ret = m_device->set_attribute_str(m_hal_id, attr, const_cast<char *>(value), len);
278
279         return (ret ? OP_SUCCESS : OP_ERROR);
280 }
281
282 int physical_sensor_handler::flush(sensor_observer *ob)
283 {
284         retv_if(!m_device, -EINVAL);
285         int ret = false;
286
287         if (m_sensor) {
288                 ret = m_sensor->flush(ob);
289                 retv_if(ret <= OP_ERROR, ret);
290         }
291
292         ret = m_device->flush(m_hal_id);
293
294         return (ret ? OP_SUCCESS : OP_ERROR);
295 }
296
297 int physical_sensor_handler::get_data(sensor_data_t **data, int *len)
298 {
299         retv_if(!m_device, -EINVAL);
300
301         int remains = m_device->get_data(m_hal_id, data, len);
302         retvm_if(*len <= 0, OP_ERROR, "Failed to get sensor event");
303
304         return remains;
305 }
306