4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sensor_hal_types.h>
23 #include <sensor_event_queue.h>
24 #include <sensor_base.h>
25 #include <sensor_common.h>
34 sensor_base::sensor_base()
36 , m_id(SENSOR_ID_INVALID)
37 , m_permission(SENSOR_PERMISSION_STANDARD)
43 sensor_base::~sensor_base()
47 void sensor_base::set_id(sensor_id_t id)
52 sensor_id_t sensor_base::get_id(void)
54 if (m_id == SENSOR_ID_INVALID)
55 return UNKNOWN_SENSOR;
60 sensor_type_t sensor_base::get_type(void)
62 return UNKNOWN_SENSOR;
65 unsigned int sensor_base::get_event_type(void)
70 const char* sensor_base::get_name(void)
75 bool sensor_base::get_sensor_info(sensor_info &info)
80 bool sensor_base::is_virtual(void)
85 int sensor_base::get_data(sensor_data_t **data, int *length)
90 bool sensor_base::flush(void)
95 int sensor_base::add_attribute(int client_id, int32_t attribute, int32_t value)
97 return set_attribute(attribute, value);
100 int sensor_base::add_attribute(int client_id, int32_t attribute, char *value, int value_size)
102 return set_attribute(attribute, value, value_size);
105 bool sensor_base::delete_attribute(int client_id)
110 int sensor_base::set_attribute(int32_t attribute, int32_t value)
115 int sensor_base::set_attribute(int32_t attribute, char *value, int value_size)
120 bool sensor_base::start(void)
122 AUTOLOCK(m_client_mutex);
128 _E("[%s] sensor failed to start", get_name());
135 _I("[%s] sensor started, #client = %d", get_name(), m_client);
140 bool sensor_base::stop(void)
142 AUTOLOCK(m_client_mutex);
148 _E("[%s] sensor faild to stop", get_name());
158 _I("[%s] sensor stopped, #client = %d", get_name(), m_client);
163 bool sensor_base::is_started(void)
165 AUTOLOCK(m_client_mutex);
170 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
172 unsigned int prev_min, cur_min;
174 AUTOLOCK(m_sensor_info_list_mutex);
176 prev_min = m_sensor_info_list.get_min_interval();
178 if (!m_sensor_info_list.add_interval(client_id, interval, is_processor))
181 cur_min = m_sensor_info_list.get_min_interval();
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);
189 set_interval(cur_min);
195 bool sensor_base::delete_interval(int client_id, bool is_processor)
197 unsigned int prev_min, cur_min;
198 AUTOLOCK(m_sensor_info_list_mutex);
200 prev_min = m_sensor_info_list.get_min_interval();
202 if (!m_sensor_info_list.delete_interval(client_id, is_processor))
205 cur_min = m_sensor_info_list.get_min_interval();
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);
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);
220 set_interval(cur_min);
226 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
228 AUTOLOCK(m_sensor_info_list_mutex);
230 return m_sensor_info_list.get_interval(client_id, is_processor);
233 bool sensor_base::add_batch(int client_id, unsigned int latency)
235 unsigned int prev_max, cur_max;
237 AUTOLOCK(m_sensor_info_list_mutex);
239 prev_max = m_sensor_info_list.get_max_batch();
241 if (!m_sensor_info_list.add_batch(client_id, latency))
244 cur_max = m_sensor_info_list.get_max_batch();
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);
255 bool sensor_base::delete_batch(int client_id)
257 unsigned int prev_max, cur_max;
258 AUTOLOCK(m_sensor_info_list_mutex);
260 prev_max = m_sensor_info_list.get_max_batch();
262 if (!m_sensor_info_list.delete_batch(client_id))
265 cur_max = m_sensor_info_list.get_max_batch();
268 _I("No latency for sensor[%#llx] by client[%d] deleting latency, so set to default count",
269 get_id(), client_id);
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);
276 set_batch_latency(cur_max);
282 unsigned int sensor_base::get_batch(int client_id)
284 AUTOLOCK(m_sensor_info_list_mutex);
286 return m_sensor_info_list.get_batch(client_id);
289 int sensor_base::get_permission(void)
294 void sensor_base::set_permission(int permission)
296 m_permission = permission;
299 bool sensor_base::push(sensor_event_t *event)
301 if (!event || !(event->data))
304 set_cache(event->data);
306 AUTOLOCK(m_client_mutex);
311 sensor_event_queue::get_instance().push(event);
315 void sensor_base::set_cache(sensor_data_t *data)
317 AUTOLOCK(m_data_cache_mutex);
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");
325 memcpy(m_last_data, data, sizeof(sensor_data_t));
328 int sensor_base::get_cache(sensor_data_t **data)
330 retv_if(m_last_data == NULL, -ENODATA);
332 *data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
333 retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
335 AUTOLOCK(m_data_cache_mutex);
337 memcpy(*data, m_last_data, sizeof(sensor_data_t));
341 bool sensor_base::set_interval(unsigned long interval)
346 bool sensor_base::set_batch_latency(unsigned long latency)
351 bool sensor_base::on_start(void)
356 bool sensor_base::on_stop(void)
361 unsigned long long sensor_base::get_timestamp(void)
364 clock_gettime(CLOCK_MONOTONIC, &t);
365 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
368 unsigned long long sensor_base::get_timestamp(timeval *t)
375 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);