blob: 3934509e22aeb048454a0d4161cfa355ec3cb1fd [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) {
57 sensor_t const* list;
58 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
59 mActivationCount.setCapacity(count);
60 Info model;
61 for (size_t i=0 ; i<size_t(count) ; i++) {
62 mActivationCount.add(list[i].handle, model);
Aravind Akella724d91d2013-06-27 12:04:23 -070063 mSensorDevice->activate(
64 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
65 list[i].handle, 0);
Mathias Agopianf001c922010-11-11 17:58:51 -080066 }
67 }
68 }
69}
70
Mathias Agopianba02cd22013-07-03 16:20:57 -070071void SensorDevice::dump(String8& result)
Mathias Agopianf001c922010-11-11 17:58:51 -080072{
73 if (!mSensorModule) return;
74 sensor_t const* list;
75 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
76
Mathias Agopianba02cd22013-07-03 16:20:57 -070077 result.appendFormat("%d h/w sensors:\n", int(count));
Mathias Agopianf001c922010-11-11 17:58:51 -080078
79 Mutex::Autolock _l(mLock);
80 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopian667102f2011-09-14 16:43:34 -070081 const Info& info = mActivationCount.valueFor(list[i].handle);
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070082 result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle,
Aravind Akella724d91d2013-06-27 12:04:23 -070083 info.batchParams.size());
84 for (size_t j = 0; j < info.batchParams.size(); j++) {
85 BatchParams params = info.batchParams.valueAt(j);
86 result.appendFormat("%4.1f%s", params.batchDelay / 1e6f,
87 j < info.batchParams.size() - 1 ? ", " : "");
Mathias Agopian667102f2011-09-14 16:43:34 -070088 }
Aravind Akella724d91d2013-06-27 12:04:23 -070089 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f);
90
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070091 result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle,
Aravind Akella724d91d2013-06-27 12:04:23 -070092 info.batchParams.size());
93 for (size_t j = 0; j < info.batchParams.size(); j++) {
94 BatchParams params = info.batchParams.valueAt(j);
95 result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f,
96 j < info.batchParams.size() - 1 ? ", " : "");
97 }
98 result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f);
Mathias Agopianf001c922010-11-11 17:58:51 -080099 }
100}
101
102ssize_t SensorDevice::getSensorList(sensor_t const** list) {
103 if (!mSensorModule) return NO_INIT;
104 ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
105 return count;
106}
107
108status_t SensorDevice::initCheck() const {
109 return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT;
110}
111
112ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) {
113 if (!mSensorDevice) return NO_INIT;
Mathias Agopian1a623012011-11-09 17:50:15 -0800114 ssize_t c;
115 do {
Aravind Akella724d91d2013-06-27 12:04:23 -0700116 c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice),
117 buffer, count);
Mathias Agopian1a623012011-11-09 17:50:15 -0800118 } while (c == -EINTR);
119 return c;
Mathias Agopianf001c922010-11-11 17:58:51 -0800120}
121
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700122void SensorDevice::autoDisable(void *ident, int handle) {
123 Info& info( mActivationCount.editValueFor(handle) );
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700124 Mutex::Autolock _l(mLock);
Aravind Akella724d91d2013-06-27 12:04:23 -0700125 info.removeBatchParamsForIdent(ident);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700126}
127
Mathias Agopianf001c922010-11-11 17:58:51 -0800128status_t SensorDevice::activate(void* ident, int handle, int enabled)
129{
130 if (!mSensorDevice) return NO_INIT;
131 status_t err(NO_ERROR);
132 bool actuateHardware = false;
133
Aravind Akella724d91d2013-06-27 12:04:23 -0700134 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800135 Info& info( mActivationCount.editValueFor(handle) );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700136
Steve Blocka5512372011-12-20 16:23:08 +0000137 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700138 "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu",
Aravind Akella724d91d2013-06-27 12:04:23 -0700139 ident, handle, enabled, info.batchParams.size());
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700140
Mathias Agopianf001c922010-11-11 17:58:51 -0800141 if (enabled) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700142 ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700143
Aravind Akella724d91d2013-06-27 12:04:23 -0700144 if (info.batchParams.indexOfKey(ident) >= 0) {
145 if (info.batchParams.size() == 1) {
146 // This is the first connection, we need to activate the underlying h/w sensor.
147 actuateHardware = true;
148 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800149 } else {
Aravind Akella724d91d2013-06-27 12:04:23 -0700150 // Log error. Every activate call should be preceded by a batch() call.
151 ALOGE("\t >>>ERROR: activate called without batch");
Mathias Agopianf001c922010-11-11 17:58:51 -0800152 }
153 } else {
Mark Salyzyndb458612014-06-10 14:50:02 -0700154 ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700155
Aravind Akella724d91d2013-06-27 12:04:23 -0700156 if (info.removeBatchParamsForIdent(ident) >= 0) {
157 if (info.batchParams.size() == 0) {
158 // This is the last connection, we need to de-activate the underlying h/w sensor.
Mathias Agopian50b66762010-11-29 17:26:51 -0800159 actuateHardware = true;
Aravind Akella724d91d2013-06-27 12:04:23 -0700160 } else {
161 const int halVersion = getHalDeviceVersion();
162 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
163 // Call batch for this sensor with the previously calculated best effort
164 // batch_rate and timeout. One of the apps has unregistered for sensor
165 // events, and the best effort batch parameters might have changed.
166 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700167 "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700168 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
169 info.bestBatchParams.batchTimeout);
170 mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags,
171 info.bestBatchParams.batchDelay,
172 info.bestBatchParams.batchTimeout);
173 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800174 }
175 } else {
176 // sensor wasn't enabled for this ident
177 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800178 }
Mathias Agopian50b66762010-11-29 17:26:51 -0800179
Mathias Agopianf001c922010-11-11 17:58:51 -0800180 if (actuateHardware) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700181 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, enabled);
182 err = mSensorDevice->activate(
183 reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled);
184 ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle,
185 strerror(-err));
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700186
Aravind Akella724d91d2013-06-27 12:04:23 -0700187 if (err != NO_ERROR && enabled) {
188 // Failure when enabling the sensor. Clean up on failure.
189 info.removeBatchParamsForIdent(ident);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700190 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800191 }
192
Aravind Akella724d91d2013-06-27 12:04:23 -0700193 // On older devices which do not support batch, call setDelay().
194 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700195 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700196 info.bestBatchParams.batchDelay);
197 mSensorDevice->setDelay(
198 reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
199 handle, info.bestBatchParams.batchDelay);
Mathias Agopianf001c922010-11-11 17:58:51 -0800200 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800201 return err;
202}
203
Aravind Akella724d91d2013-06-27 12:04:23 -0700204status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
205 int64_t maxBatchReportLatencyNs) {
206 if (!mSensorDevice) return NO_INIT;
207
208 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
209 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
210 }
211
212 const int halVersion = getHalDeviceVersion();
213 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
214 if (flags & SENSORS_BATCH_DRY_RUN) {
215 return mSensorDevice->batch(mSensorDevice, handle, flags, samplingPeriodNs,
216 maxBatchReportLatencyNs);
217 } else {
218 // Call h/w with dry run to see if the given parameters are feasible or not. Return if
219 // there is an error.
220 status_t errDryRun(NO_ERROR);
221 errDryRun = mSensorDevice->batch(mSensorDevice, handle, flags | SENSORS_BATCH_DRY_RUN,
222 samplingPeriodNs, maxBatchReportLatencyNs);
223 if (errDryRun != NO_ERROR) {
224 ALOGD_IF(DEBUG_CONNECTIONS, "SensorDevice::batch dry run error %s",
225 strerror(-errDryRun));
226 return errDryRun;
227 }
228 }
229 } else if (maxBatchReportLatencyNs != 0) {
230 // Batch is not supported on older devices.
231 return INVALID_OPERATION;
232 }
233
234 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700235 "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700236 ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs);
237
238 Mutex::Autolock _l(mLock);
239 Info& info(mActivationCount.editValueFor(handle));
240
241 if (info.batchParams.indexOfKey(ident) < 0) {
242 BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs);
243 info.batchParams.add(ident, params);
244 } else {
245 // A batch has already been called with this ident. Update the batch parameters.
246 info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs);
247 }
248
249 BatchParams prevBestBatchParams = info.bestBatchParams;
250 // Find the minimum of all timeouts and batch_rates for this sensor.
251 info.selectBatchParams();
252
253 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700254 "\t>>> curr_period=%" PRId64 " min_period=%" PRId64
255 " curr_timeout=%" PRId64 " min_timeout=%" PRId64,
Aravind Akella724d91d2013-06-27 12:04:23 -0700256 prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay,
257 prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout);
258
259 status_t err(NO_ERROR);
260 // If the min period or min timeout has changed since the last batch call, call batch.
261 if (prevBestBatchParams != info.bestBatchParams) {
262 if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700263 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700264 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
265 info.bestBatchParams.batchTimeout);
266 err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags,
267 info.bestBatchParams.batchDelay,
268 info.bestBatchParams.batchTimeout);
269 } else {
270 // For older devices which do not support batch, call setDelay() after activate() is
271 // called. Some older devices may not support calling setDelay before activate(), so
272 // call setDelay in SensorDevice::activate() method.
273 }
274 if (err != NO_ERROR) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700275 ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s",
276 mSensorDevice, handle,
Aravind Akella724d91d2013-06-27 12:04:23 -0700277 info.bestBatchParams.flags, info.bestBatchParams.batchDelay,
278 info.bestBatchParams.batchTimeout, strerror(-err));
279 info.removeBatchParamsForIdent(ident);
280 }
281 }
282 return err;
283}
284
285status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs)
Mathias Agopianf001c922010-11-11 17:58:51 -0800286{
287 if (!mSensorDevice) return NO_INIT;
Aravind Akella724d91d2013-06-27 12:04:23 -0700288 if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) {
289 samplingPeriodNs = MINIMUM_EVENTS_PERIOD;
290 }
Mathias Agopian667102f2011-09-14 16:43:34 -0700291 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800292 Info& info( mActivationCount.editValueFor(handle) );
Aravind Akella724d91d2013-06-27 12:04:23 -0700293 // If the underlying sensor is NOT in continuous mode, setDelay() should return an error.
294 // Calling setDelay() in batch mode is an invalid operation.
295 if (info.bestBatchParams.batchTimeout != 0) {
296 return INVALID_OPERATION;
297 }
298 ssize_t index = info.batchParams.indexOfKey(ident);
299 if (index < 0) {
300 return BAD_INDEX;
301 }
302 BatchParams& params = info.batchParams.editValueAt(index);
303 params.batchDelay = samplingPeriodNs;
304 info.selectBatchParams();
305 return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice),
306 handle, info.bestBatchParams.batchDelay);
Mathias Agopian667102f2011-09-14 16:43:34 -0700307}
308
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700309int SensorDevice::getHalDeviceVersion() const {
310 if (!mSensorDevice) return -1;
311
312 return mSensorDevice->common.version;
313}
314
Aravind Akella9a844cf2014-02-11 18:58:52 -0800315status_t SensorDevice::flush(void* ident, int handle) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700316 if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) {
317 return INVALID_OPERATION;
318 }
319 ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle);
320 return mSensorDevice->flush(mSensorDevice, handle);
321}
322
Mathias Agopian667102f2011-09-14 16:43:34 -0700323// ---------------------------------------------------------------------------
324
Aravind Akella724d91d2013-06-27 12:04:23 -0700325status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags,
326 int64_t samplingPeriodNs,
327 int64_t maxBatchReportLatencyNs) {
328 ssize_t index = batchParams.indexOfKey(ident);
Mathias Agopian667102f2011-09-14 16:43:34 -0700329 if (index < 0) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700330 ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)",
Aravind Akella724d91d2013-06-27 12:04:23 -0700331 ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index));
Mathias Agopian667102f2011-09-14 16:43:34 -0700332 return BAD_INDEX;
333 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700334 BatchParams& params = batchParams.editValueAt(index);
335 params.flags = flags;
336 params.batchDelay = samplingPeriodNs;
337 params.batchTimeout = maxBatchReportLatencyNs;
Mathias Agopian667102f2011-09-14 16:43:34 -0700338 return NO_ERROR;
339}
340
Aravind Akella724d91d2013-06-27 12:04:23 -0700341void SensorDevice::Info::selectBatchParams() {
342 BatchParams bestParams(-1, -1, -1);
343
344 if (batchParams.size() > 0) {
345 BatchParams params = batchParams.valueAt(0);
346 bestParams = params;
347 }
348
349 for (size_t i = 1; i < batchParams.size(); ++i) {
350 BatchParams params = batchParams.valueAt(i);
351 if (params.batchDelay < bestParams.batchDelay) {
352 bestParams.batchDelay = params.batchDelay;
353 }
354 if (params.batchTimeout < bestParams.batchTimeout) {
355 bestParams.batchTimeout = params.batchTimeout;
Mathias Agopianf001c922010-11-11 17:58:51 -0800356 }
357 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700358 bestBatchParams = bestParams;
359}
360
361ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) {
362 ssize_t idx = batchParams.removeItem(ident);
363 if (idx >= 0) {
364 selectBatchParams();
365 }
366 return idx;
Mathias Agopianf001c922010-11-11 17:58:51 -0800367}
368
369// ---------------------------------------------------------------------------
370}; // namespace android
371