sensord: add/change enums and types for avoiding build-break
[platform/core/system/sensord.git] / src / shared / sensor_base.cpp
1 /*
2  * libsensord-share
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 <sensor_base.h>
21
22 #include <algorithm>
23
24 #define UNKNOWN_NAME "UNKNOWN_SENSOR"
25
26 sensor_base::sensor_base()
27 : m_privilege(SENSOR_PRIVILEGE_PUBLIC)
28 , m_permission(SENSOR_PERMISSION_STANDARD)
29 , m_client(0)
30 , m_started(false)
31 {
32
33 }
34
35 sensor_base::~sensor_base()
36 {
37
38 }
39
40 bool sensor_base::init()
41 {
42         return true;
43 }
44
45 bool sensor_base::is_virtual()
46 {
47         return false;
48 }
49
50 void sensor_base::set_id(sensor_id_t id)
51 {
52         m_id = id;
53 }
54
55 sensor_id_t sensor_base::get_id(void)
56 {
57         return m_id;
58 }
59
60 sensor_privilege_t sensor_base::get_privilege(void)
61 {
62         return m_privilege;
63 }
64
65 int sensor_base::get_permission(void)
66 {
67         return m_permission;
68 }
69
70
71 void sensor_base::set_privilege(sensor_privilege_t privilege)
72 {
73         m_privilege = privilege;
74 }
75
76 void sensor_base::set_permission(int permission)
77 {
78         m_permission = permission;
79 }
80
81
82 sensor_type_t sensor_base::get_type()
83 {
84         return UNKNOWN_SENSOR;
85 }
86
87 const char* sensor_base::get_name()
88 {
89         if (m_name.empty())
90                 return UNKNOWN_NAME;
91
92         return m_name.c_str();
93 }
94
95 bool sensor_base::on_start()
96 {
97         return true;
98 }
99
100 bool sensor_base::on_stop()
101 {
102         return true;
103 }
104
105 bool sensor_base::start()
106 {
107         AUTOLOCK(m_mutex);
108         AUTOLOCK(m_client_mutex);
109
110         ++m_client;
111
112         if (m_client == 1) {
113                 if (!on_start()) {
114                         ERR("[%s] sensor failed to start", get_name());
115                         return false;
116                 }
117
118                 m_started = true;
119         }
120
121         INFO("[%s] sensor started, #client = %d", get_name(), m_client);
122
123         return true;
124 }
125
126 bool sensor_base::stop(void)
127 {
128         AUTOLOCK(m_mutex);
129         AUTOLOCK(m_client_mutex);
130
131         --m_client;
132
133         if (m_client == 0) {
134                 if (!on_stop()) {
135                         ERR("[%s] sensor faild to stop", get_name());
136                         return false;
137                 }
138
139                 m_started = false;
140         }
141
142         INFO("[%s] sensor stopped, #client = %d", get_name(), m_client);
143
144         return true;
145 }
146
147
148 bool sensor_base::is_started(void)
149 {
150         AUTOLOCK(m_mutex);
151         AUTOLOCK(m_client_mutex);
152
153         return m_started;
154 }
155
156 bool sensor_base::add_client(unsigned int event_type)
157 {
158         if (!is_supported(event_type)) {
159                 ERR("Invaild event type: 0x%x", event_type);
160                 return false;
161         }
162
163         AUTOLOCK(m_client_info_mutex);
164
165         ++(m_client_info[event_type]);
166         return true;
167 }
168
169 bool sensor_base::delete_client(unsigned int event_type)
170 {
171         if (!is_supported(event_type)) {
172                 ERR("Invaild event type: 0x%x", event_type);
173                 return false;
174         }
175
176         AUTOLOCK(m_client_info_mutex);
177
178         auto iter = m_client_info.find(event_type);
179
180         if (iter == m_client_info.end())
181                 return false;
182
183         if (iter->second == 0)
184                 return false;
185
186         --(iter->second);
187
188         return true;
189 }
190
191 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
192 {
193         unsigned int prev_min, cur_min;
194
195         AUTOLOCK(m_interval_info_list_mutex);
196
197         prev_min = m_interval_info_list.get_min();
198
199         if (!m_interval_info_list.add_interval(client_id, interval, is_processor))
200                 return false;
201
202         cur_min = m_interval_info_list.get_min();
203
204         if (cur_min != prev_min) {
205                 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
206                         " by%sclient[%d] adding interval",
207                         get_type(), prev_min, cur_min,
208                         is_processor ? " processor " : " ", client_id);
209                 set_interval(cur_min);
210         }
211
212         return true;
213 }
214
215 bool sensor_base::delete_interval(int client_id, bool is_processor)
216 {
217         unsigned int prev_min, cur_min;
218         AUTOLOCK(m_interval_info_list_mutex);
219
220         prev_min = m_interval_info_list.get_min();
221
222         if (!m_interval_info_list.delete_interval(client_id, is_processor))
223                 return false;
224
225         cur_min = m_interval_info_list.get_min();
226
227         if (!cur_min) {
228                 INFO("No interval for sensor[0x%x] by%sclient[%d] deleting interval, "
229                          "so set to default %dms",
230                          get_type(), is_processor ? " processor " : " ",
231                          client_id, POLL_1HZ_MS);
232
233                 set_interval(POLL_1HZ_MS);
234         } else if (cur_min != prev_min) {
235                 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
236                         " by%sclient[%d] deleting interval",
237                         get_type(), prev_min, cur_min,
238                         is_processor ? " processor " : " ", client_id);
239
240                 set_interval(cur_min);
241         }
242
243         return true;
244 }
245
246 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
247 {
248         AUTOLOCK(m_interval_info_list_mutex);
249
250         return m_interval_info_list.get_interval(client_id, is_processor);
251 }
252
253 void sensor_base::get_sensor_info(sensor_info &info)
254 {
255         sensor_properties_s properties;
256         get_properties(properties);
257
258         info.set_type(get_type());
259         info.set_id(get_id());
260         info.set_privilege(m_privilege);
261         info.set_name(properties.name.c_str());
262         info.set_vendor(properties.vendor.c_str());
263         info.set_min_range(properties.min_range);
264         info.set_max_range(properties.max_range);
265         info.set_resolution(properties.resolution);
266         info.set_min_interval(properties.min_interval);
267         info.set_fifo_count(properties.fifo_count);
268         info.set_max_batch_count(properties.max_batch_count);
269         info.set_supported_events(m_supported_event_info);
270
271         return;
272 }
273
274 bool sensor_base::get_properties(sensor_properties_s &properties)
275 {
276         return true;
277 }
278
279 bool sensor_base::is_supported(unsigned int event_type)
280 {
281         auto iter = find(m_supported_event_info.begin(), m_supported_event_info.end(), event_type);
282
283         if (iter == m_supported_event_info.end())
284                 return false;
285
286         return true;
287 }
288
289 long sensor_base::set_command(unsigned int cmd, long value)
290 {
291         return -1;
292 }
293
294 int sensor_base::send_sensorhub_data(const char* data, int data_len)
295 {
296         return -1;
297 }
298
299 int sensor_base::get_sensor_data(unsigned int type, sensor_data_t &data)
300 {
301         return -1;
302 }
303
304 void sensor_base::register_supported_event(unsigned int event_type)
305 {
306         m_supported_event_info.push_back(event_type);
307 }
308
309 unsigned int sensor_base::get_client_cnt(unsigned int event_type)
310 {
311         AUTOLOCK(m_client_info_mutex);
312
313         auto iter = m_client_info.find(event_type);
314
315         if (iter == m_client_info.end())
316                 return 0;
317
318         return iter->second;
319 }
320
321 bool sensor_base::set_interval(unsigned long val)
322 {
323         return true;
324 }
325
326 unsigned long long sensor_base::get_timestamp(void)
327 {
328         struct timespec t;
329         clock_gettime(CLOCK_MONOTONIC, &t);
330         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
331 }
332
333 unsigned long long sensor_base::get_timestamp(timeval *t)
334 {
335         if (!t) {
336                 ERR("t is NULL");
337                 return 0;
338         }
339
340         return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
341 }