Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[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         ++m_client;
125
126         if (m_client == 1) {
127                 if (!on_start()) {
128                         _E("[%s] sensor failed to start", get_name());
129                         return false;
130                 }
131
132                 m_started = true;
133         }
134
135         _I("[%s] sensor started, #client = %d", get_name(), m_client);
136
137         return true;
138 }
139
140 bool sensor_base::stop(void)
141 {
142         AUTOLOCK(m_client_mutex);
143
144         --m_client;
145
146         if (m_client == 0) {
147                 if (!on_stop()) {
148                         _E("[%s] sensor faild to stop", get_name());
149                         return false;
150                 }
151
152                 m_started = false;
153
154                 free(m_last_data);
155                 m_last_data = NULL;
156         }
157
158         _I("[%s] sensor stopped, #client = %d", get_name(), m_client);
159
160         return true;
161 }
162
163 bool sensor_base::is_started(void)
164 {
165         AUTOLOCK(m_client_mutex);
166
167         return m_started;
168 }
169
170 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
171 {
172         unsigned int prev_min, cur_min;
173
174         AUTOLOCK(m_sensor_info_list_mutex);
175
176         prev_min = m_sensor_info_list.get_min_interval();
177
178         if (!m_sensor_info_list.add_interval(client_id, interval, is_processor))
179                 return false;
180
181         cur_min = m_sensor_info_list.get_min_interval();
182
183         if (cur_min != prev_min) {
184                 _I("Min interval for sensor[%#llx] is changed from %dms to %dms"
185                         " by%sclient[%d] adding interval",
186                         get_id(), prev_min, cur_min,
187                         is_processor ? " processor " : " ", client_id);
188
189                 set_interval(cur_min);
190         }
191
192         return true;
193 }
194
195 bool sensor_base::delete_interval(int client_id, bool is_processor)
196 {
197         unsigned int prev_min, cur_min;
198         AUTOLOCK(m_sensor_info_list_mutex);
199
200         prev_min = m_sensor_info_list.get_min_interval();
201
202         if (!m_sensor_info_list.delete_interval(client_id, is_processor))
203                 return false;
204
205         cur_min = m_sensor_info_list.get_min_interval();
206
207         if (!cur_min) {
208                 _I("No interval for sensor[%#llx] by%sclient[%d] deleting interval, "
209                          "so set to default %dms",
210                          get_id(), is_processor ? " processor " : " ",
211                          client_id, POLL_1HZ_MS);
212
213                 set_interval(POLL_1HZ_MS);
214         } else if (cur_min != prev_min) {
215                 _I("Min interval for sensor[%#llx] is changed from %dms to %dms"
216                         " by%sclient[%d] deleting interval",
217                         get_id(), prev_min, cur_min,
218                         is_processor ? " processor " : " ", client_id);
219
220                 set_interval(cur_min);
221         }
222
223         return true;
224 }
225
226 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
227 {
228         AUTOLOCK(m_sensor_info_list_mutex);
229
230         return m_sensor_info_list.get_interval(client_id, is_processor);
231 }
232
233 bool sensor_base::add_batch(int client_id, unsigned int latency)
234 {
235         unsigned int prev_max, cur_max;
236
237         AUTOLOCK(m_sensor_info_list_mutex);
238
239         prev_max = m_sensor_info_list.get_max_batch();
240
241         if (!m_sensor_info_list.add_batch(client_id, latency))
242                 return false;
243
244         cur_max = m_sensor_info_list.get_max_batch();
245
246         if (cur_max != prev_max) {
247                 _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] adding latency",
248                         get_id(), prev_max, cur_max, client_id);
249                 set_batch_latency(cur_max);
250         }
251
252         return true;
253 }
254
255 bool sensor_base::delete_batch(int client_id)
256 {
257         unsigned int prev_max, cur_max;
258         AUTOLOCK(m_sensor_info_list_mutex);
259
260         prev_max = m_sensor_info_list.get_max_batch();
261
262         if (!m_sensor_info_list.delete_batch(client_id))
263                 return false;
264
265         cur_max = m_sensor_info_list.get_max_batch();
266
267         if (!cur_max) {
268                 _I("No latency for sensor[%#llx] by client[%d] deleting latency, so set to default count",
269                          get_id(), client_id);
270
271                 set_batch_latency(UINT_MAX);
272         } else if (cur_max != prev_max) {
273                 _I("Max latency for sensor[%#llx] is changed from %dms to %dms by client[%d] deleting latency",
274                         get_id(), prev_max, cur_max, client_id);
275
276                 set_batch_latency(cur_max);
277         }
278
279         return true;
280 }
281
282 unsigned int sensor_base::get_batch(int client_id)
283 {
284         AUTOLOCK(m_sensor_info_list_mutex);
285
286         return m_sensor_info_list.get_batch(client_id);
287 }
288
289 int sensor_base::get_permission(void)
290 {
291         return m_permission;
292 }
293
294 void sensor_base::set_permission(int permission)
295 {
296         m_permission = permission;
297 }
298
299 bool sensor_base::push(sensor_event_t *event)
300 {
301         if (!event || !(event->data))
302                 return false;
303
304         set_cache(event->data);
305
306         AUTOLOCK(m_client_mutex);
307
308         if (m_client <= 0)
309                 return false;
310
311         sensor_event_queue::get_instance().push(event);
312         return true;
313 }
314
315 void sensor_base::set_cache(sensor_data_t *data)
316 {
317         AUTOLOCK(m_data_cache_mutex);
318
319         /* Caching the last known data for sync-read support */
320         if (m_last_data == NULL) {
321                 m_last_data = (sensor_data_t*)malloc(sizeof(sensor_data_t));
322                 retm_if(m_last_data == NULL, "Memory allocation failed");
323         }
324
325         memcpy(m_last_data, data, sizeof(sensor_data_t));
326 }
327
328 int sensor_base::get_cache(sensor_data_t **data)
329 {
330         retv_if(m_last_data == NULL, -ENODATA);
331
332         *data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
333         retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
334
335         AUTOLOCK(m_data_cache_mutex);
336
337         memcpy(*data, m_last_data, sizeof(sensor_data_t));
338         return 0;
339 }
340
341 bool sensor_base::set_interval(unsigned long interval)
342 {
343         return true;
344 }
345
346 bool sensor_base::set_batch_latency(unsigned long latency)
347 {
348         return true;
349 }
350
351 bool sensor_base::on_start(void)
352 {
353         return true;
354 }
355
356 bool sensor_base::on_stop(void)
357 {
358         return true;
359 }
360
361 unsigned long long sensor_base::get_timestamp(void)
362 {
363         struct timespec t;
364         clock_gettime(CLOCK_MONOTONIC, &t);
365         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
366 }
367
368 unsigned long long sensor_base::get_timestamp(timeval *t)
369 {
370         if (!t) {
371                 _E("t is NULL");
372                 return 0;
373         }
374
375         return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
376 }