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.
21 #include <sensor_hal.h>
22 #include <sensor_event_queue.h>
23 #include <sensor_base.h>
24 #include <sensor_common.h>
33 sensor_base::sensor_base()
34 : m_id(SENSOR_ID_INVALID)
35 , m_permission(SENSOR_PERMISSION_STANDARD)
41 sensor_base::~sensor_base()
45 void sensor_base::set_id(sensor_id_t id)
50 sensor_id_t sensor_base::get_id(void)
52 if (m_id == SENSOR_ID_INVALID)
53 return UNKNOWN_SENSOR;
58 sensor_type_t sensor_base::get_type(void)
60 return UNKNOWN_SENSOR;
63 unsigned int sensor_base::get_event_type(void)
68 const char* sensor_base::get_name()
73 bool sensor_base::get_sensor_info(sensor_info &info)
78 bool sensor_base::is_virtual()
83 int sensor_base::get_data(sensor_data_t **data, int *length)
88 bool sensor_base::flush(void)
93 int sensor_base::set_attribute(int32_t cmd, int32_t value)
98 int sensor_base::set_attribute(int32_t attribute, char *value, int value_size)
103 bool sensor_base::start()
105 AUTOLOCK(m_client_mutex);
111 _E("[%s] sensor failed to start", get_name());
118 _I("[%s] sensor started, #client = %d", get_name(), m_client);
123 bool sensor_base::stop(void)
125 AUTOLOCK(m_client_mutex);
131 _E("[%s] sensor faild to stop", get_name());
138 _I("[%s] sensor stopped, #client = %d", get_name(), m_client);
143 bool sensor_base::is_started(void)
145 AUTOLOCK(m_client_mutex);
150 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
152 unsigned int prev_min, cur_min;
154 AUTOLOCK(m_sensor_info_list_mutex);
156 prev_min = m_sensor_info_list.get_min_interval();
158 if (!m_sensor_info_list.add_interval(client_id, interval, is_processor))
161 cur_min = m_sensor_info_list.get_min_interval();
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);
169 set_interval(cur_min);
175 bool sensor_base::delete_interval(int client_id, bool is_processor)
177 unsigned int prev_min, cur_min;
178 AUTOLOCK(m_sensor_info_list_mutex);
180 prev_min = m_sensor_info_list.get_min_interval();
182 if (!m_sensor_info_list.delete_interval(client_id, is_processor))
185 cur_min = m_sensor_info_list.get_min_interval();
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);
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);
200 set_interval(cur_min);
206 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
208 AUTOLOCK(m_sensor_info_list_mutex);
210 return m_sensor_info_list.get_interval(client_id, is_processor);
213 bool sensor_base::add_batch(int client_id, unsigned int latency)
215 unsigned int prev_max, cur_max;
217 AUTOLOCK(m_sensor_info_list_mutex);
219 prev_max = m_sensor_info_list.get_max_batch();
221 if (!m_sensor_info_list.add_batch(client_id, latency))
224 cur_max = m_sensor_info_list.get_max_batch();
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);
235 bool sensor_base::delete_batch(int client_id)
237 unsigned int prev_max, cur_max;
238 AUTOLOCK(m_sensor_info_list_mutex);
240 prev_max = m_sensor_info_list.get_max_batch();
242 if (!m_sensor_info_list.delete_batch(client_id))
245 cur_max = m_sensor_info_list.get_max_batch();
248 _I("No latency for sensor[0x%llx] by client[%d] deleting latency, so set to default 0 ms",
249 get_id(), client_id);
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);
256 set_batch_latency(cur_max);
262 unsigned int sensor_base::get_batch(int client_id)
264 AUTOLOCK(m_sensor_info_list_mutex);
266 return m_sensor_info_list.get_batch(client_id);
269 int sensor_base::get_permission(void)
274 void sensor_base::set_permission(int permission)
276 m_permission = permission;
279 bool sensor_base::push(sensor_event_t *event)
281 AUTOLOCK(m_client_mutex);
286 sensor_event_queue::get_instance().push(event);
290 bool sensor_base::set_interval(unsigned long interval)
295 bool sensor_base::set_batch_latency(unsigned long latency)
300 bool sensor_base::on_start()
305 bool sensor_base::on_stop()
310 unsigned long long sensor_base::get_timestamp(void)
313 clock_gettime(CLOCK_MONOTONIC, &t);
314 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
317 unsigned long long sensor_base::get_timestamp(timeval *t)
324 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);