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) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 57 | 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 62 | 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 Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 68 | mSensorDevice->activate( |
| 69 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 70 | list[i].handle, 0); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 76 | void SensorDevice::dump(String8& result) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 77 | { |
| 78 | if (!mSensorModule) return; |
| 79 | sensor_t const* list; |
| 80 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); |
| 81 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 82 | result.appendFormat("halVersion %d\n", getHalDeviceVersion()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 83 | result.appendFormat("%d h/w sensors:\n", int(count)); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 84 | |
| 85 | Mutex::Autolock _l(mLock); |
| 86 | for (size_t i=0 ; i<size_t(count) ; i++) { |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 87 | const Info& info = mActivationCount.valueFor(list[i].handle); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 88 | 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] | 89 | 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 94 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 95 | result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); |
| 96 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 97 | 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] | 98 | 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | ssize_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 | |
| 114 | status_t SensorDevice::initCheck() const { |
| 115 | return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; |
| 116 | } |
| 117 | |
| 118 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
| 119 | if (!mSensorDevice) return NO_INIT; |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 120 | ssize_t c; |
| 121 | do { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 122 | c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), |
| 123 | buffer, count); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 124 | } while (c == -EINTR); |
| 125 | return c; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 128 | void SensorDevice::autoDisable(void *ident, int handle) { |
| 129 | Info& info( mActivationCount.editValueFor(handle) ); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 130 | Mutex::Autolock _l(mLock); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 131 | info.removeBatchParamsForIdent(ident); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 134 | status_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 Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 140 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 141 | Info& info( mActivationCount.editValueFor(handle) ); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 142 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 143 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 144 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 145 | ident, handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 146 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 147 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 148 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 149 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 150 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 155 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 156 | // Log error. Every activate call should be preceded by a batch() call. |
| 157 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 158 | } |
| 159 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 160 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 161 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 162 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 165 | actuateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 166 | } 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 173 | "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 174 | 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 Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | // sensor wasn't enabled for this ident |
| 183 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 184 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 185 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 186 | if (actuateHardware) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 187 | 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 Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 192 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 193 | if (err != NO_ERROR && enabled) { |
| 194 | // Failure when enabling the sensor. Clean up on failure. |
| 195 | info.removeBatchParamsForIdent(ident); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 196 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 199 | // On older devices which do not support batch, call setDelay(). |
| 200 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.batchParams.size() > 0) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 201 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 202 | info.bestBatchParams.batchDelay); |
| 203 | mSensorDevice->setDelay( |
| 204 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 205 | handle, info.bestBatchParams.batchDelay); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 206 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 207 | return err; |
| 208 | } |
| 209 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 210 | status_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 Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 219 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) { |
| 220 | // Batch is not supported on older devices return invalid operation. |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 221 | return INVALID_OPERATION; |
| 222 | } |
| 223 | |
| 224 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 225 | "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] | 226 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 244 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 |
| 245 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 246 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 253 | 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] | 254 | 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 Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 265 | ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", |
| 266 | mSensorDevice, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 267 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, |
| 268 | info.bestBatchParams.batchTimeout, strerror(-err)); |
| 269 | info.removeBatchParamsForIdent(ident); |
| 270 | } |
| 271 | } |
| 272 | return err; |
| 273 | } |
| 274 | |
| 275 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 276 | { |
| 277 | if (!mSensorDevice) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 278 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 279 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 280 | } |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 281 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 282 | Info& info( mActivationCount.editValueFor(handle) ); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 283 | // 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 299 | int SensorDevice::getHalDeviceVersion() const { |
| 300 | if (!mSensorDevice) return -1; |
| 301 | |
| 302 | return mSensorDevice->common.version; |
| 303 | } |
| 304 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 305 | status_t SensorDevice::flush(void* ident, int handle) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 306 | 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 Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 313 | // --------------------------------------------------------------------------- |
| 314 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 315 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, |
| 316 | int64_t samplingPeriodNs, |
| 317 | int64_t maxBatchReportLatencyNs) { |
| 318 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 319 | if (index < 0) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 320 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 321 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 322 | return BAD_INDEX; |
| 323 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 324 | BatchParams& params = batchParams.editValueAt(index); |
| 325 | params.flags = flags; |
| 326 | params.batchDelay = samplingPeriodNs; |
| 327 | params.batchTimeout = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 328 | return NO_ERROR; |
| 329 | } |
| 330 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 331 | void 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 348 | bestBatchParams = bestParams; |
| 349 | } |
| 350 | |
| 351 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 352 | ssize_t idx = batchParams.removeItem(ident); |
| 353 | if (idx >= 0) { |
| 354 | selectBatchParams(); |
| 355 | } |
| 356 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // --------------------------------------------------------------------------- |
| 360 | }; // namespace android |
| 361 | |