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.
24 #include <sensor_hal.h>
25 #include <sensor_event_queue.h>
26 #include <sensor_base.h>
31 sensor_base::sensor_base()
33 , m_privilege(SENSOR_PRIVILEGE_PUBLIC)
34 , m_permission(SENSOR_PERMISSION_STANDARD)
40 sensor_base::~sensor_base()
44 sensor_type_t sensor_base::get_type(void)
46 return UNKNOWN_SENSOR;
49 bool sensor_base::start()
51 AUTOLOCK(m_client_mutex);
57 ERR("[%s] sensor failed to start", get_name());
64 INFO("[%s] sensor started, #client = %d", get_name(), m_client);
69 bool sensor_base::stop(void)
71 AUTOLOCK(m_client_mutex);
77 ERR("[%s] sensor faild to stop", get_name());
84 INFO("[%s] sensor stopped, #client = %d", get_name(), m_client);
89 bool sensor_base::on_start()
94 bool sensor_base::on_stop()
99 long sensor_base::set_command(unsigned int cmd, long value)
104 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
106 unsigned int prev_min, cur_min;
108 AUTOLOCK(m_plugin_info_list_mutex);
110 prev_min = m_plugin_info_list.get_min_interval();
112 if (!m_plugin_info_list.add_interval(client_id, interval, is_processor))
115 cur_min = m_plugin_info_list.get_min_interval();
117 if (cur_min != prev_min) {
118 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
119 " by%sclient[%d] adding interval",
120 get_id(), prev_min, cur_min,
121 is_processor ? " processor " : " ", client_id);
123 set_interval(cur_min);
129 bool sensor_base::delete_interval(int client_id, bool is_processor)
131 unsigned int prev_min, cur_min;
132 AUTOLOCK(m_plugin_info_list_mutex);
134 prev_min = m_plugin_info_list.get_min_interval();
136 if (!m_plugin_info_list.delete_interval(client_id, is_processor))
139 cur_min = m_plugin_info_list.get_min_interval();
142 INFO("No interval for sensor[0x%x] by%sclient[%d] deleting interval, "
143 "so set to default %dms",
144 get_id(), is_processor ? " processor " : " ",
145 client_id, POLL_1HZ_MS);
147 set_interval(POLL_1HZ_MS);
148 } else if (cur_min != prev_min) {
149 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
150 " by%sclient[%d] deleting interval",
151 get_id(), prev_min, cur_min,
152 is_processor ? " processor " : " ", client_id);
154 set_interval(cur_min);
160 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
162 AUTOLOCK(m_plugin_info_list_mutex);
164 return m_plugin_info_list.get_interval(client_id, is_processor);
167 bool sensor_base::add_batch(int client_id, unsigned int latency)
169 unsigned int prev_max, cur_max;
171 AUTOLOCK(m_plugin_info_list_mutex);
173 prev_max = m_plugin_info_list.get_max_batch();
175 if (!m_plugin_info_list.add_batch(client_id, latency))
178 cur_max = m_plugin_info_list.get_max_batch();
180 if (cur_max != prev_max) {
181 INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] adding latency",
182 get_id(), prev_max, cur_max, client_id);
189 bool sensor_base::delete_batch(int client_id)
191 unsigned int prev_max, cur_max;
192 AUTOLOCK(m_plugin_info_list_mutex);
194 prev_max = m_plugin_info_list.get_max_batch();
196 if (!m_plugin_info_list.delete_batch(client_id))
199 cur_max = m_plugin_info_list.get_max_batch();
202 INFO("No latency for sensor[0x%x] by client[%d] deleting latency, so set to default 0 ms",
203 get_id(), client_id);
206 } else if (cur_max != prev_max) {
207 INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] deleting latency",
208 get_id(), prev_max, cur_max, client_id);
216 unsigned int sensor_base::get_batch(int client_id)
218 AUTOLOCK(m_plugin_info_list_mutex);
220 return m_plugin_info_list.get_batch(client_id);
223 bool sensor_base::add_wakeup(int client_id, int wakeup)
225 int prev_wakeup, cur_wakeup;
227 AUTOLOCK(m_plugin_info_list_mutex);
229 prev_wakeup = m_plugin_info_list.is_wakeup_on();
231 if (!m_plugin_info_list.add_wakeup(client_id, wakeup))
234 cur_wakeup = m_plugin_info_list.is_wakeup_on();
236 if ((cur_wakeup == SENSOR_WAKEUP_ON) && (prev_wakeup < SENSOR_WAKEUP_ON)) {
237 INFO("Wakeup for sensor[0x%x] is changed from %d to %d by client[%d] adding wakeup",
238 get_id(), prev_wakeup, cur_wakeup, client_id);
239 set_wakeup(SENSOR_WAKEUP_ON);
245 bool sensor_base::delete_wakeup(int client_id)
247 int prev_wakeup, cur_wakeup;
248 AUTOLOCK(m_plugin_info_list_mutex);
250 prev_wakeup = m_plugin_info_list.is_wakeup_on();
252 if (!m_plugin_info_list.delete_wakeup(client_id))
255 cur_wakeup = m_plugin_info_list.is_wakeup_on();
257 if ((cur_wakeup < SENSOR_WAKEUP_ON) && (prev_wakeup == SENSOR_WAKEUP_ON)) {
258 INFO("Wakeup for sensor[0x%x] is changed from %d to %d by client[%d] deleting wakeup",
259 get_id(), prev_wakeup, cur_wakeup, client_id);
260 set_wakeup(SENSOR_WAKEUP_OFF);
266 int sensor_base::get_wakeup(int client_id)
268 AUTOLOCK(m_plugin_info_list_mutex);
270 return m_plugin_info_list.is_wakeup_on();
273 int sensor_base::get_sensor_data(sensor_data_t &data)
278 int sensor_base::get_sensor_event(sensor_event_t **event)
283 bool sensor_base::get_properties(sensor_properties_s &properties)
288 const char* sensor_base::get_name()
293 void sensor_base::set_id(sensor_id_t id)
298 sensor_id_t sensor_base::get_id(void)
300 if (m_unique_id == -1)
301 return UNKNOWN_SENSOR;
306 unsigned int sensor_base::get_event_type(void)
311 sensor_privilege_t sensor_base::get_privilege(void)
316 int sensor_base::get_permission(void)
321 bool sensor_base::is_started(void)
323 AUTOLOCK(m_client_mutex);
328 bool sensor_base::is_virtual()
333 void sensor_base::get_sensor_info(sensor_info &info)
335 sensor_properties_s properties;
336 properties.wakeup_supported = false;
338 get_properties(properties);
340 info.set_type(get_type());
341 info.set_id(get_id());
342 info.set_privilege(m_privilege);
343 info.set_name(properties.name.c_str());
344 info.set_vendor(properties.vendor.c_str());
345 info.set_min_range(properties.min_range);
346 info.set_max_range(properties.max_range);
347 info.set_resolution(properties.resolution);
348 info.set_min_interval(properties.min_interval);
349 info.set_fifo_count(properties.fifo_count);
350 info.set_max_batch_count(properties.max_batch_count);
351 info.set_supported_event(get_event_type());
352 info.set_wakeup_supported(properties.wakeup_supported);
357 bool sensor_base::is_wakeup_supported(void)
362 bool sensor_base::set_interval(unsigned long interval)
367 bool sensor_base::set_wakeup(int wakeup)
372 bool sensor_base::set_batch(unsigned long latency)
377 void sensor_base::set_privilege(sensor_privilege_t privilege)
379 m_privilege = privilege;
382 void sensor_base::set_permission(int permission)
384 m_permission = permission;
387 int sensor_base::send_sensorhub_data(const char* data, int data_len)
392 bool sensor_base::push(sensor_event_t *event, int event_length)
394 AUTOLOCK(m_client_mutex);
399 sensor_event_queue::get_instance().push(event, event_length);
403 unsigned long long sensor_base::get_timestamp(void)
406 clock_gettime(CLOCK_MONOTONIC, &t);
407 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
410 unsigned long long sensor_base::get_timestamp(timeval *t)
417 return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);