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