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);
131 _E("[%s] sensor failed to start", get_name());
138 _I("[%s] sensor started, #client = %d", get_name(), m_client);
143 bool sensor_base::stop(void)
145 AUTOLOCK(m_client_mutex);
151 _E("[%s] sensor faild to stop", get_name());
161 _I("[%s] sensor stopped, #client = %d", get_name(), m_client);
166 bool sensor_base::is_started(void)
168 AUTOLOCK(m_client_mutex);
173 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
175 unsigned int prev_min, cur_min;
177 AUTOLOCK(m_sensor_info_list_mutex);
179 prev_min = m_sensor_info_list.get_min_interval();
181 if (!m_sensor_info_list.add_interval(client_id, interval, is_processor))
184 cur_min = m_sensor_info_list.get_min_interval();
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);
192 set_interval(cur_min);
198 bool sensor_base::delete_interval(int client_id, bool is_processor)
200 unsigned int prev_min, cur_min;
201 AUTOLOCK(m_sensor_info_list_mutex);
203 prev_min = m_sensor_info_list.get_min_interval();
205 if (!m_sensor_info_list.delete_interval(client_id, is_processor))
208 cur_min = m_sensor_info_list.get_min_interval();
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);
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);
223 set_interval(cur_min);
229 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
231 AUTOLOCK(m_sensor_info_list_mutex);
233 return m_sensor_info_list.get_interval(client_id, is_processor);
236 bool sensor_base::add_batch(int client_id, unsigned int latency)
238 unsigned int prev_max, cur_max;
240 AUTOLOCK(m_sensor_info_list_mutex);
242 prev_max = m_sensor_info_list.get_max_batch();
244 if (!m_sensor_info_list.add_batch(client_id, latency))
247 cur_max = m_sensor_info_list.get_max_batch();
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);
258 bool sensor_base::delete_batch(int client_id)
260 unsigned int prev_max, cur_max;
261 AUTOLOCK(m_sensor_info_list_mutex);
263 prev_max = m_sensor_info_list.get_max_batch();
265 if (!m_sensor_info_list.delete_batch(client_id))
268 cur_max = m_sensor_info_list.get_max_batch();
271 _I("No latency for sensor[%#llx] by client[%d] deleting latency, so set to default count",
272 get_id(), client_id);
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);
279 set_batch_latency(cur_max);
285 unsigned int sensor_base::get_batch(int client_id)
287 AUTOLOCK(m_sensor_info_list_mutex);
289 return m_sensor_info_list.get_batch(client_id);
292 int sensor_base::get_permission(void)
297 void sensor_base::set_permission(int permission)
299 m_permission = permission;
302 bool sensor_base::push(sensor_event_t *event)
304 if (!event || !(event->data))
307 set_cache(event->data);
309 AUTOLOCK(m_client_mutex);
314 sensor_event_queue::get_instance().push(event);
318 void sensor_base::set_cache(sensor_data_t *data)
320 AUTOLOCK(m_data_cache_mutex);
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");
328 memcpy(m_last_data, data, sizeof(sensor_data_t));
331 int sensor_base::get_cache(sensor_data_t **data)
333 retv_if(m_last_data == NULL, -ENODATA);
335 *data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
336 retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
338 AUTOLOCK(m_data_cache_mutex);
340 memcpy(*data, m_last_data, sizeof(sensor_data_t));
344 bool sensor_base::set_interval(unsigned long interval)
349 bool sensor_base::set_batch_latency(unsigned long latency)
354 bool sensor_base::pre_start(void)
359 bool sensor_base::on_start(void)
364 bool sensor_base::on_stop(void)
369 unsigned long long sensor_base::get_timestamp(void)
372 clock_gettime(CLOCK_MONOTONIC, &t);
373 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
376 unsigned long long sensor_base::get_timestamp(timeval *t)
383 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);