sensord: fix incorrect return type
[platform/core/system/sensord.git] / src / server / sensor_base.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <stdint.h>
21 #include <limits.h>
22 #include <sensor_hal_types.h>
23 #include <sensor_event_queue.h>
24 #include <sensor_base.h>
25 #include <sensor_common.h>
26
27 #include <algorithm>
28 #include <utility>
29 #include <functional>
30
31 using std::make_pair;
32 using std::vector;
33
34 sensor_base::sensor_base()
35 : m_last_data(NULL)
36 , m_id(SENSOR_ID_INVALID)
37 , m_permission(SENSOR_PERMISSION_STANDARD)
38 , m_started(false)
39 , m_client(0)
40 {
41 }
42
43 sensor_base::~sensor_base()
44 {
45 }
46
47 void sensor_base::set_id(sensor_id_t id)
48 {
49         m_id = id;
50 }
51
52 sensor_id_t sensor_base::get_id(void)
53 {
54         if (m_id == SENSOR_ID_INVALID)
55                 return UNKNOWN_SENSOR;
56
57         return m_id;
58 }
59
60 sensor_type_t sensor_base::get_type(void)
61 {
62         return UNKNOWN_SENSOR;
63 }
64
65 unsigned int sensor_base::get_event_type(void)
66 {
67         return -1;
68 }
69
70 const char* sensor_base::get_name(void)
71 {
72         return NULL;
73 }
74
75 bool sensor_base::get_sensor_info(sensor_info &info)
76 {
77         return false;
78 }
79
80 bool sensor_base::is_virtual(void)
81 {
82         return false;
83 }
84
85 int sensor_base::get_data(sensor_data_t **data, int *length)
86 {
87         return OP_ERROR;
88 }
89
90 bool sensor_base::flush(void)
91 {
92         return true;
93 }
94
95 int sensor_base::add_attribute(int client_id, int32_t attribute, int32_t value)
96 {
97         return set_attribute(attribute, value);
98 }
99
100 int sensor_base::add_attribute(int client_id, int32_t attribute, char *value, int value_size)
101 {
102         return set_attribute(attribute, value, value_size);
103 }
104
105 bool sensor_base::delete_attribute(int client_id)
106 {
107         return true;
108 }
109
110 int sensor_base::set_attribute(int32_t attribute, int32_t value)
111 {
112         return OP_SUCCESS;
113 }
114
115 int sensor_base::set_attribute(int32_t attribute, char *value, int value_size)
116 {
117         return OP_SUCCESS;
118 }
119
120 bool sensor_base::start(void)
121 {
122         AUTOLOCK(m_client_mutex);
123
124         if (!pre_start())
125                 return false;
126
127         ++m_client;
128
129         if (m_client == 1) {
130                 if (!on_start()) {
131                         _E("[%s] sensor failed to start", get_name());
132                         return false;
133                 }
134
135                 m_started = true;
136         }
137
138         _I("[%s] sensor started, #client = %d", get_name(), m_client);
139
140         return true;
141 }
142
143 bool sensor_base::stop(void)
144 {
145         AUTOLOCK(m_client_mutex);
146
147         --m_client;
148
149         if (m_client == 0) {
150                 if (!on_stop()) {
151                         _E("[%s] sensor faild to stop", get_name());
152                         return false;
153                 }
154
155                 m_started = false;
156
157                 free(m_last_data);
158                 m_last_data = NULL;
159         }
160
161         _I("[%s] sensor stopped, #client = %d", get_name(), m_client);
162
163         return true;
164 }
165
166 bool sensor_base::is_started(void)
167 {
168         AUTOLOCK(m_client_mutex);
169
170         return m_started;
171 }
172
173 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
174 {
175         unsigned int prev_min, cur_min;
176
177         AUTOLOCK(m_sensor_info_list_mutex);
178
179         prev_min = m_sensor_info_list.get_min_interval();
180
181         if (!m_sensor_info_list.add_interval(client_id, interval, is_processor))
182                 return false;
183
184         cur_min = m_sensor_info_list.get_min_interval();
185
186         if (cur_min != prev_min) {
187                 _I("Min interval for sensor[%#llx] is changed from %dms to %dms"
188                         " by%sclient[%d] adding interval",
189                         get_id(), prev_min, cur_min,
190                         is_processor ? " processor " : " ", client_id);
191
192                 set_interval(cur_min);
193         }
194
195         return true;
196 }
197
198 bool sensor_base::delete_interval(int client_id, bool is_processor)
199 {
200         unsigned int prev_min, cur_min;
201         AUTOLOCK(m_sensor_info_list_mutex);
202
203         prev_min = m_sensor_info_list.get_min_interval();
204
205         if (!m_sensor_info_list.delete_interval(client_id, is_processor))
206                 return false;
207
208         cur_min = m_sensor_info_list.get_min_interval();
209
210         if (!cur_min) {
211                 _I("No interval for sensor[%#llx] by%sclient[%d] deleting interval, "
212                          "so set to default %dms",
213                          get_id(), is_processor ? " processor " : " ",
214                          client_id, POLL_1HZ_MS);
215
216                 set_interval(POLL_1HZ_MS);
217         } else if (cur_min != prev_min) {
218                 _I("Min interval for sensor[%#llx] is changed from %dms to %dms"
219                         " by%sclient[%d] deleting interval",
220                         get_id(), prev_min, cur_min,
221                         is_processor ? " processor " : " ", client_id);
222
223                 set_interval(cur_min);
224         }
225
226         return true;
227 }
228
229 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
230 {
231         AUTOLOCK(m_sensor_info_list_mutex);
232
233         return m_sensor_info_list.get_interval(client_id, is_processor);
234 }
235
236 bool sensor_base::add_batch(int client_id, unsigned int latency)
237 {
238         unsigned int prev_max, cur_max;
239
240         AUTOLOCK(m_sensor_info_list_mutex);
241
242         prev_max = m_sensor_info_list.get_max_batch();
243
244         if (!m_sensor_info_list.add_batch(client_id, latency))
245                 return false;
246
247         cur_max = m_sensor_info_list.get_max_batch();
248
249         if (cur_max != prev_max) {
250                 _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] adding latency",
251                         get_id(), prev_max, cur_max, client_id);
252                 set_batch_latency(cur_max);
253         }
254
255         return true;
256 }
257
258 bool sensor_base::delete_batch(int client_id)
259 {
260         unsigned int prev_max, cur_max;
261         AUTOLOCK(m_sensor_info_list_mutex);
262
263         prev_max = m_sensor_info_list.get_max_batch();
264
265         if (!m_sensor_info_list.delete_batch(client_id))
266                 return false;
267
268         cur_max = m_sensor_info_list.get_max_batch();
269
270         if (!cur_max) {
271                 _I("No latency for sensor[%#llx] by client[%d] deleting latency, so set to default count",
272                          get_id(), client_id);
273
274                 set_batch_latency(UINT_MAX);
275         } else if (cur_max != prev_max) {
276                 _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] deleting latency",
277                         get_id(), prev_max, cur_max, client_id);
278
279                 set_batch_latency(cur_max);
280         }
281
282         return true;
283 }
284
285 unsigned int sensor_base::get_batch(int client_id)
286 {
287         AUTOLOCK(m_sensor_info_list_mutex);
288
289         return m_sensor_info_list.get_batch(client_id);
290 }
291
292 int sensor_base::get_permission(void)
293 {
294         return m_permission;
295 }
296
297 void sensor_base::set_permission(int permission)
298 {
299         m_permission = permission;
300 }
301
302 bool sensor_base::push(sensor_event_t *event)
303 {
304         if (!event || !(event->data))
305                 return false;
306
307         set_cache(event->data);
308
309         AUTOLOCK(m_client_mutex);
310
311         if (m_client <= 0)
312                 return false;
313
314         sensor_event_queue::get_instance().push(event);
315         return true;
316 }
317
318 void sensor_base::set_cache(sensor_data_t *data)
319 {
320         AUTOLOCK(m_data_cache_mutex);
321
322         /* Caching the last known data for sync-read support */
323         if (m_last_data == NULL) {
324                 m_last_data = (sensor_data_t*)malloc(sizeof(sensor_data_t));
325                 retm_if(m_last_data == NULL, "Memory allocation failed");
326         }
327
328         memcpy(m_last_data, data, sizeof(sensor_data_t));
329 }
330
331 int sensor_base::get_cache(sensor_data_t **data)
332 {
333         retv_if(m_last_data == NULL, -ENODATA);
334
335         *data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
336         retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
337
338         AUTOLOCK(m_data_cache_mutex);
339
340         memcpy(*data, m_last_data, sizeof(sensor_data_t));
341         return 0;
342 }
343
344 bool sensor_base::set_interval(unsigned long interval)
345 {
346         return true;
347 }
348
349 bool sensor_base::set_batch_latency(unsigned long latency)
350 {
351         return true;
352 }
353
354 bool sensor_base::pre_start(void)
355 {
356         return true;
357 }
358
359 bool sensor_base::on_start(void)
360 {
361         return true;
362 }
363
364 bool sensor_base::on_stop(void)
365 {
366         return true;
367 }
368
369 unsigned long long sensor_base::get_timestamp(void)
370 {
371         struct timespec t;
372         clock_gettime(CLOCK_MONOTONIC, &t);
373         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
374 }
375
376 unsigned long long sensor_base::get_timestamp(timeval *t)
377 {
378         if (!t) {
379                 _E("t is NULL");
380                 return 0;
381         }
382
383         return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
384 }