blob: 80845a287c6747593915d12846fce5f651acaf0a [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyndb458612014-06-10 14:50:02 -070017#include <inttypes.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mark Salyzyndb458612014-06-10 14:50:02 -070019#include <stdint.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080020#include <sys/types.h>
21
22#include <utils/Atomic.h>
23#include <utils/Errors.h>
24#include <utils/Singleton.h>
25
26#include <binder/BinderService.h>
27#include <binder/Parcel.h>
28#include <binder/IServiceManager.h>
29
30#include <hardware/sensors.h>
31
32#include "SensorDevice.h"
Mathias Agopiana1b7db92011-05-27 16:23:58 -070033#include "SensorService.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080034
35namespace android {
36// ---------------------------------------------------------------------------
Mathias Agopianf001c922010-11-11 17:58:51 -080037
38ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)
39
40SensorDevice::SensorDevice()
41 : mSensorDevice(0),
42 mSensorModule(0)
43{
44 status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
45 (hw_module_t const**)&mSensorModule);
46
Steve Blockf5a12302012-01-06 19:20:56 +000047 ALOGE_IF(err, "couldn't load %s module (%s)",
Mathias Agopianf001c922010-11-11 17:58:51 -080048 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
49
50 if (mSensorModule) {
Aravind Akella724d91d2013-06-27 12:04:23 -070051 err = sensors_open_1(&mSensorModule->common, &mSensorDevice);
Mathias Agopianf001c922010-11-11 17:58:51 -080052
Steve Blockf5a12302012-01-06 19:20:56 +000053 ALOGE_IF(err, "couldn't open device for module %s (%s)",
Mathias Agopianf001c922010-11-11 17:58:51 -080054 SENSORS_HARDWARE_MODULE_ID, strerror(-err));
55
56 if (mSensorDevice) {
Aravind Akella6c2664a2014-08-13 12:24:50 -070057 if (mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_1 ||
58 mSensorDevice->common.version == SENSORS_DEVICE_API_VERSION_1_2) {
59 ALOGE(">>>> WARNING <<< Upgrade sensor HAL to version 1_3");
60 }
61
Mathias Agopianf001c922010-11-11 17:58:51 -080062 sensor_t const* list;
63 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
64 mActivationCount.setCapacity(count);
65 Info model;
66 for (size_t i=0 ; i<size_t(count) ; i++) {
67 mActivationCount.add(list[i].handle, model);
Aravind Akella724d91d2013-06-27 12:04:23 -070068 mSensorDevice->activate(
69 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
70 list[i].handle, 0);
Mathias Agopianf001c922010-11-11 17:58:51 -080071 }
72 }
73 }
74}
75
Mathias Agopianba02cd22013-07-03 16:20:57 -070076void SensorDevice::dump(String8& result)
Mathias Agopianf001c922010-11-11 17:58:51 -080077{
78 if (!mSensorModule) return;
79 sensor_t const* list;
80 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
81
Aravind Akella6c2664a2014-08-13 12:24:50 -070082 result.appendFormat("halVersion %d\n", getHalDeviceVersion());
Mathias Agopianba02cd22013-07-03 16:20:57 -070083 result.appendFormat("%d h/w sensors:\n", int(count));
Mathias Agopianf001c922010-11-11 17:58:51 -080084
85 Mutex::Autolock _l(mLock);
86 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopian667102f2011-09-14 16:43:34 -070087 const Info& info = mActivationCount.valueFor(list[i].handle);
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070088 result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle,
Aravind Akella724d91d2013-06-27 12:04:23 -070089 info.batchParams.size());
90 for (size_t j = 0; j < info.batchParams.size(); j++) {
91 BatchParams params = info.batchParams.valueAt(j);
92 result.appendFormat("%4.1f%s", params.batchDelay / 1e6f,
93 j < info.batchParams.size() - 1 ? ", " : "");
Mathias Agopian667102f2011-09-14 16:43:34 -070094 }
Aravind Akella724d91d2013-06-27 12:04:23 -070095 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f);
96
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070097 result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle,
Aravind Akella724d91d2013-06-27 12:04:23 -070098 info.batchParams.size());
99 for (size_t j = 0; j < info.batchParams.size(); j++) {
100 BatchParams params = info.batchParams.valueAt(j);
101 result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f,
102 j < info.batchParams.size() - 1 ? ", " : "");
103 }
104 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f);
Mathias Agopianf001c922010-11-11 17:58:51 -0800105 }
106}
107
108ssize_t SensorDevice::getSensorList(sensor_t const** list) {
109 if (!mSensorModule) return NO_INIT;
110 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
111 return count;
112}
113
114status_t SensorDevice::initCheck() const {
115 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
116}
117
118ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
119 if (!mSensorDevice) return NO_INIT;
Mathias Agopian1a623012011-11-09 17:50:15 -0800120 ssize_t c;
121 do {
Aravind Akella724d91d2013-06-27 12:04:23 -0700122 c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),
123 buffer, count);
Mathias Agopian1a623012011-11-09 17:50:15 -0800124 } while (c == -EINTR);
125 return c;
Mathias Agopianf001c922010-11-11 17:58:51 -0800126}
127
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700128void SensorDevice::autoDisable(void *ident, int handle) {
129 Info& info( mActivationCount.editValueFor(handle) );
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700130 Mutex::Autolock _l(mLock);
Aravind Akella724d91d2013-06-27 12:04:23 -0700131 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700132}
133
Mathias Agopianf001c922010-11-11 17:58:51 -0800134status_t SensorDevice::activate(void* ident, int handle, int enabled)
135{
136 if (!mSensorDevice) return NO_INIT;
137 status_t err(NO_ERROR);
138 bool actuateHardware = false;
139
Aravind Akella724d91d2013-06-27 12:04:23 -0700140 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800141 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700142
Steve Blocka5512372011-12-20 16:23:08 +0000143 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700144 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700145 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700146
Mathias Agopianf001c922010-11-11 17:58:51 -0800147 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700148 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700149
Aravind Akella724d91d2013-06-27 12:04:23 -0700150 if (info.batchParams.indexOfKey(ident) >= 0) {
151 if (info.batchParams.size() == 1) {
152 // This is the first connection, we need to activate the underlying h/w sensor.
153 actuateHardware = true;
154 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800155 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700156 // Log error. Every activate call should be preceded by a batch() call.
157 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800158 }
159 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700160 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700161
Aravind Akella724d91d2013-06-27 12:04:23 -0700162 if (info.removeBatchParamsForIdent(ident) >= 0) {
163 if (info.batchParams.size() == 0) {
164 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800165 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700166 } else {
167 const int halVersion = getHalDeviceVersion();
168 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
169 // Call batch for this sensor with the previously calculated best effort
170 // batch_rate and timeout. One of the apps has unregistered for sensor
171 // events, and the best effort batch parameters might have changed.
172 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700173 "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700174 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
175 info.bestBatchParams.batchTimeout);
176 mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags,
177 info.bestBatchParams.batchDelay,
178 info.bestBatchParams.batchTimeout);
179 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800180 }
181 } else {
182 // sensor wasn't enabled for this ident
183 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800184 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800185
Mathias Agopianf001c922010-11-11 17:58:51 -0800186 if (actuateHardware) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700187 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, enabled);
188 err = mSensorDevice->activate(
189 reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled);
190 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
191 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700192
Aravind Akella724d91d2013-06-27 12:04:23 -0700193 if (err != NO_ERROR && enabled) {
194 // Failure when enabling the sensor. Clean up on failure.
195 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700196 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800197 }
198
Aravind Akella724d91d2013-06-27 12:04:23 -0700199 // On older devices which do not support batch, call setDelay().
200 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700201 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700202 info.bestBatchParams.batchDelay);
203 mSensorDevice->setDelay(
204 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
205 handle, info.bestBatchParams.batchDelay);
Mathias Agopianf001c922010-11-11 17:58:51 -0800206 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800207 return err;
208}
209
Aravind Akella724d91d2013-06-27 12:04:23 -0700210status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
211 int64_t maxBatchReportLatencyNs) {
212 if (!mSensorDevice) return NO_INIT;
213
214 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
215 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
216 }
217
218 const int halVersion = getHalDeviceVersion();
Aravind Akella6c2664a2014-08-13 12:24:50 -0700219 if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) {
220 // Batch is not supported on older devices return invalid operation.
Aravind Akella724d91d2013-06-27 12:04:23 -0700221 return INVALID_OPERATION;
222 }
223
224 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700225 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700226 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
227
228 Mutex::Autolock _l(mLock);
229 Info& info(mActivationCount.editValueFor(handle));
230
231 if (info.batchParams.indexOfKey(ident) < 0) {
232 BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs);
233 info.batchParams.add(ident, params);
234 } else {
235 // A batch has already been called with this ident. Update the batch parameters.
236 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
237 }
238
239 BatchParams prevBestBatchParams = info.bestBatchParams;
240 // Find the minimum of all timeouts and batch_rates for this sensor.
241 info.selectBatchParams();
242
243 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700244 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
245 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700246 prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay,
247 prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout);
248
249 status_t err(NO_ERROR);
250 // If the min period or min timeout has changed since the last batch call, call batch.
251 if (prevBestBatchParams != info.bestBatchParams) {
252 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700253 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700254 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
255 info.bestBatchParams.batchTimeout);
256 err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags,
257 info.bestBatchParams.batchDelay,
258 info.bestBatchParams.batchTimeout);
259 } else {
260 // For older devices which do not support batch, call setDelay() after activate() is
261 // called. Some older devices may not support calling setDelay before activate(), so
262 // call setDelay in SensorDevice::activate() method.
263 }
264 if (err != NO_ERROR) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700265 ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
266 mSensorDevice, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700267 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
268 info.bestBatchParams.batchTimeout, strerror(-err));
269 info.removeBatchParamsForIdent(ident);
270 }
271 }
272 return err;
273}
274
275status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs)
Mathias Agopianf001c922010-11-11 17:58:51 -0800276{
277 if (!mSensorDevice) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700278 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
279 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
280 }
Mathias Agopian667102f2011-09-14 16:43:34 -0700281 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800282 Info& info( mActivationCount.editValueFor(handle) );
Aravind Akella724d91d2013-06-27 12:04:23 -0700283 // If the underlying sensor is NOT in continuous mode, setDelay() should return an error.
284 // Calling setDelay() in batch mode is an invalid operation.
285 if (info.bestBatchParams.batchTimeout != 0) {
286 return INVALID_OPERATION;
287 }
288 ssize_t index = info.batchParams.indexOfKey(ident);
289 if (index < 0) {
290 return BAD_INDEX;
291 }
292 BatchParams& params = info.batchParams.editValueAt(index);
293 params.batchDelay = samplingPeriodNs;
294 info.selectBatchParams();
295 return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
296 handle, info.bestBatchParams.batchDelay);
Mathias Agopian667102f2011-09-14 16:43:34 -0700297}
298
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700299int SensorDevice::getHalDeviceVersion() const {
300 if (!mSensorDevice) return -1;
301
302 return mSensorDevice->common.version;
303}
304
Aravind Akella9a844cf2014-02-11 18:58:52 -0800305status_t SensorDevice::flush(void* ident, int handle) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700306 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) {
307 return INVALID_OPERATION;
308 }
309 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
310 return mSensorDevice->flush(mSensorDevice, handle);
311}
312
Mathias Agopian667102f2011-09-14 16:43:34 -0700313// ---------------------------------------------------------------------------
314
Aravind Akella724d91d2013-06-27 12:04:23 -0700315status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags,
316 int64_t samplingPeriodNs,
317 int64_t maxBatchReportLatencyNs) {
318 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700319 if (index < 0) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700320 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700321 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700322 return BAD_INDEX;
323 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700324 BatchParams& params = batchParams.editValueAt(index);
325 params.flags = flags;
326 params.batchDelay = samplingPeriodNs;
327 params.batchTimeout = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700328 return NO_ERROR;
329}
330
Aravind Akella724d91d2013-06-27 12:04:23 -0700331void SensorDevice::Info::selectBatchParams() {
332 BatchParams bestParams(-1, -1, -1);
333
334 if (batchParams.size() > 0) {
335 BatchParams params = batchParams.valueAt(0);
336 bestParams = params;
337 }
338
339 for (size_t i = 1; i < batchParams.size(); ++i) {
340 BatchParams params = batchParams.valueAt(i);
341 if (params.batchDelay < bestParams.batchDelay) {
342 bestParams.batchDelay = params.batchDelay;
343 }
344 if (params.batchTimeout < bestParams.batchTimeout) {
345 bestParams.batchTimeout = params.batchTimeout;
Mathias Agopianf001c922010-11-11 17:58:51 -0800346 }
347 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700348 bestBatchParams = bestParams;
349}
350
351ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
352 ssize_t idx = batchParams.removeItem(ident);
353 if (idx >= 0) {
354 selectBatchParams();
355 }
356 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800357}
358
359// ---------------------------------------------------------------------------
360}; // namespace android
361