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