| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 17 | #include <inttypes.h> | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 | #include <math.h> | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 20 | #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 Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 33 | #include "SensorService.h" | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | namespace android { | 
|  | 36 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) | 
|  | 39 |  | 
|  | 40 | SensorDevice::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 Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 47 | ALOGE_IF(err, "couldn't load %s module (%s)", | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 48 | SENSORS_HARDWARE_MODULE_ID, strerror(-err)); | 
|  | 49 |  | 
|  | 50 | if (mSensorModule) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 51 | err = sensors_open_1(&mSensorModule->common, &mSensorDevice); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 52 |  | 
| Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 53 | ALOGE_IF(err, "couldn't open device for module %s (%s)", | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 54 | 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 Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 63 | mSensorDevice->activate( | 
|  | 64 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), | 
|  | 65 | list[i].handle, 0); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 66 | } | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 |  | 
| Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 71 | void SensorDevice::dump(String8& result) | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 72 | { | 
|  | 73 | if (!mSensorModule) return; | 
|  | 74 | sensor_t const* list; | 
|  | 75 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); | 
|  | 76 |  | 
| Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 77 | result.appendFormat("%d h/w sensors:\n", int(count)); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | Mutex::Autolock _l(mLock); | 
|  | 80 | for (size_t i=0 ; i<size_t(count) ; i++) { | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 81 | const Info& info = mActivationCount.valueFor(list[i].handle); | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 82 | result.appendFormat("handle=0x%08x, active-count=%zu, batch_period(ms)={ ", list[i].handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 83 | 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 88 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 89 | result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); | 
|  | 90 |  | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 91 | result.appendFormat("handle=0x%08x, active-count=%zu, batch_timeout(ms)={ ", list[i].handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 92 | 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | ssize_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 |  | 
|  | 108 | status_t SensorDevice::initCheck() const { | 
|  | 109 | return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { | 
|  | 113 | if (!mSensorDevice) return NO_INIT; | 
| Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 114 | ssize_t c; | 
|  | 115 | do { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 116 | c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), | 
|  | 117 | buffer, count); | 
| Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 118 | } while (c == -EINTR); | 
|  | 119 | return c; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 122 | void SensorDevice::autoDisable(void *ident, int handle) { | 
|  | 123 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 124 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 125 | info.removeBatchParamsForIdent(ident); | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 128 | status_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 Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 134 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 135 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 136 |  | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 137 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 138 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 139 | ident, handle, enabled, info.batchParams.size()); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 140 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 141 | if (enabled) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 142 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 143 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 144 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 149 | } else { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 150 | // Log error. Every activate call should be preceded by a batch() call. | 
|  | 151 | ALOGE("\t >>>ERROR: activate called without batch"); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 152 | } | 
|  | 153 | } else { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 154 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 155 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 156 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 159 | actuateHardware = true; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 160 | } 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 167 | "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 168 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 174 | } | 
|  | 175 | } else { | 
|  | 176 | // sensor wasn't enabled for this ident | 
|  | 177 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 178 | } | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 179 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 180 | if (actuateHardware) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 181 | 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 Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 186 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 187 | if (err != NO_ERROR && enabled) { | 
|  | 188 | // Failure when enabling the sensor. Clean up on failure. | 
|  | 189 | info.removeBatchParamsForIdent(ident); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 190 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 193 | // On older devices which do not support batch, call setDelay(). | 
|  | 194 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 195 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 196 | info.bestBatchParams.batchDelay); | 
|  | 197 | mSensorDevice->setDelay( | 
|  | 198 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), | 
|  | 199 | handle, info.bestBatchParams.batchDelay); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 200 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 201 | return err; | 
|  | 202 | } | 
|  | 203 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 204 | status_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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 235 | "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 236 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 254 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 | 
|  | 255 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 256 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 263 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 264 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 275 | ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", | 
|  | 276 | mSensorDevice, handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 277 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, | 
|  | 278 | info.bestBatchParams.batchTimeout, strerror(-err)); | 
|  | 279 | info.removeBatchParamsForIdent(ident); | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 | return err; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 286 | { | 
|  | 287 | if (!mSensorDevice) return NO_INIT; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 288 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { | 
|  | 289 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; | 
|  | 290 | } | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 291 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 292 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 293 | // 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 309 | int SensorDevice::getHalDeviceVersion() const { | 
|  | 310 | if (!mSensorDevice) return -1; | 
|  | 311 |  | 
|  | 312 | return mSensorDevice->common.version; | 
|  | 313 | } | 
|  | 314 |  | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 315 | status_t SensorDevice::flush(void* /*ident*/, int handle) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 316 | 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 323 | // --------------------------------------------------------------------------- | 
|  | 324 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 325 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, | 
|  | 326 | int64_t samplingPeriodNs, | 
|  | 327 | int64_t maxBatchReportLatencyNs) { | 
|  | 328 | ssize_t index = batchParams.indexOfKey(ident); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 329 | if (index < 0) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 330 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 331 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 332 | return BAD_INDEX; | 
|  | 333 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 334 | BatchParams& params = batchParams.editValueAt(index); | 
|  | 335 | params.flags = flags; | 
|  | 336 | params.batchDelay = samplingPeriodNs; | 
|  | 337 | params.batchTimeout = maxBatchReportLatencyNs; | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 338 | return NO_ERROR; | 
|  | 339 | } | 
|  | 340 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 341 | void 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 356 | } | 
|  | 357 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 358 | bestBatchParams = bestParams; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { | 
|  | 362 | ssize_t idx = batchParams.removeItem(ident); | 
|  | 363 | if (idx >= 0) { | 
|  | 364 | selectBatchParams(); | 
|  | 365 | } | 
|  | 366 | return idx; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
|  | 369 | // --------------------------------------------------------------------------- | 
|  | 370 | }; // namespace android | 
|  | 371 |  |