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 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame^] | 76 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { |
| 77 | if (connected) { |
| 78 | Info model; |
| 79 | mActivationCount.add(handle, model); |
| 80 | mSensorDevice->activate( |
| 81 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), handle, 0); |
| 82 | } else { |
| 83 | mActivationCount.removeItem(handle); |
| 84 | } |
| 85 | } |
| 86 | |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 87 | void SensorDevice::dump(String8& result) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 88 | { |
| 89 | if (!mSensorModule) return; |
| 90 | sensor_t const* list; |
| 91 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list); |
| 92 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 93 | result.appendFormat("halVersion 0x%08x\n", getHalDeviceVersion()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 94 | result.appendFormat("%d h/w sensors:\n", int(count)); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 95 | |
| 96 | Mutex::Autolock _l(mLock); |
| 97 | for (size_t i=0 ; i<size_t(count) ; i++) { |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 98 | const Info& info = mActivationCount.valueFor(list[i].handle); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 99 | if (info.batchParams.isEmpty()) continue; |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 100 | 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] | 101 | info.batchParams.size()); |
| 102 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 103 | const BatchParams& params = info.batchParams.valueAt(j); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 104 | result.appendFormat("%4.1f%s", params.batchDelay / 1e6f, |
| 105 | j < info.batchParams.size() - 1 ? ", " : ""); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 106 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 107 | result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchDelay / 1e6f); |
| 108 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 109 | 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] | 110 | info.batchParams.size()); |
| 111 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
| 112 | BatchParams params = info.batchParams.valueAt(j); |
| 113 | result.appendFormat("%4.1f%s", params.batchTimeout / 1e6f, |
| 114 | j < info.batchParams.size() - 1 ? ", " : ""); |
| 115 | } |
| 116 | result.appendFormat(" }, selected=%4.1f ms\n", info.bestBatchParams.batchTimeout / 1e6f); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { |
| 121 | if (!mSensorModule) return NO_INIT; |
| 122 | ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list); |
| 123 | return count; |
| 124 | } |
| 125 | |
| 126 | status_t SensorDevice::initCheck() const { |
| 127 | return mSensorDevice && mSensorModule ? NO_ERROR : NO_INIT; |
| 128 | } |
| 129 | |
| 130 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
| 131 | if (!mSensorDevice) return NO_INIT; |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 132 | ssize_t c; |
| 133 | do { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 134 | c = mSensorDevice->poll(reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), |
| 135 | buffer, count); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 136 | } while (c == -EINTR); |
| 137 | return c; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 140 | void SensorDevice::autoDisable(void *ident, int handle) { |
| 141 | Info& info( mActivationCount.editValueFor(handle) ); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 142 | Mutex::Autolock _l(mLock); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 143 | info.removeBatchParamsForIdent(ident); |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 146 | status_t SensorDevice::activate(void* ident, int handle, int enabled) |
| 147 | { |
| 148 | if (!mSensorDevice) return NO_INIT; |
| 149 | status_t err(NO_ERROR); |
| 150 | bool actuateHardware = false; |
| 151 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 152 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 153 | Info& info( mActivationCount.editValueFor(handle) ); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 154 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 155 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 156 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 157 | ident, handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 158 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 159 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 160 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 161 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 162 | if (isClientDisabledLocked(ident)) { |
| 163 | return INVALID_OPERATION; |
| 164 | } |
| 165 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 166 | if (info.batchParams.indexOfKey(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 167 | if (info.numActiveClients() == 1) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 168 | // This is the first connection, we need to activate the underlying h/w sensor. |
| 169 | actuateHardware = true; |
| 170 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 171 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 172 | // Log error. Every activate call should be preceded by a batch() call. |
| 173 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 174 | } |
| 175 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 176 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 177 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 178 | if (info.removeBatchParamsForIdent(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 179 | if (info.numActiveClients() == 0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 180 | // 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] | 181 | actuateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 182 | } else { |
| 183 | const int halVersion = getHalDeviceVersion(); |
| 184 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { |
| 185 | // Call batch for this sensor with the previously calculated best effort |
| 186 | // batch_rate and timeout. One of the apps has unregistered for sensor |
| 187 | // events, and the best effort batch parameters might have changed. |
| 188 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 189 | "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 190 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, |
| 191 | info.bestBatchParams.batchTimeout); |
| 192 | mSensorDevice->batch(mSensorDevice, handle,info.bestBatchParams.flags, |
| 193 | info.bestBatchParams.batchDelay, |
| 194 | info.bestBatchParams.batchTimeout); |
| 195 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 196 | } |
| 197 | } else { |
| 198 | // sensor wasn't enabled for this ident |
| 199 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 200 | |
| 201 | if (isClientDisabledLocked(ident)) { |
| 202 | return NO_ERROR; |
| 203 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 204 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 205 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 206 | if (actuateHardware) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 207 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, |
| 208 | enabled); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 209 | err = mSensorDevice->activate( |
| 210 | reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), handle, enabled); |
| 211 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, |
| 212 | strerror(-err)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 213 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 214 | if (err != NO_ERROR && enabled) { |
| 215 | // Failure when enabling the sensor. Clean up on failure. |
| 216 | info.removeBatchParamsForIdent(ident); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 217 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 220 | // On older devices which do not support batch, call setDelay(). |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 221 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1 && info.numActiveClients() > 0) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 222 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w setDelay %d %" PRId64, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 223 | info.bestBatchParams.batchDelay); |
| 224 | mSensorDevice->setDelay( |
| 225 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 226 | handle, info.bestBatchParams.batchDelay); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 227 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 228 | return err; |
| 229 | } |
| 230 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 231 | status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 232 | int64_t maxBatchReportLatencyNs) { |
| 233 | if (!mSensorDevice) return NO_INIT; |
| 234 | |
| 235 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 236 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 237 | } |
| 238 | |
| 239 | const int halVersion = getHalDeviceVersion(); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 240 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 && maxBatchReportLatencyNs != 0) { |
| 241 | // Batch is not supported on older devices return invalid operation. |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | |
| 245 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 246 | "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] | 247 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 248 | |
| 249 | Mutex::Autolock _l(mLock); |
| 250 | Info& info(mActivationCount.editValueFor(handle)); |
| 251 | |
| 252 | if (info.batchParams.indexOfKey(ident) < 0) { |
| 253 | BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 254 | info.batchParams.add(ident, params); |
| 255 | } else { |
| 256 | // A batch has already been called with this ident. Update the batch parameters. |
| 257 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 258 | } |
| 259 | |
| 260 | BatchParams prevBestBatchParams = info.bestBatchParams; |
| 261 | // Find the minimum of all timeouts and batch_rates for this sensor. |
| 262 | info.selectBatchParams(); |
| 263 | |
| 264 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 265 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 |
| 266 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 267 | prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay, |
| 268 | prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout); |
| 269 | |
| 270 | status_t err(NO_ERROR); |
| 271 | // If the min period or min timeout has changed since the last batch call, call batch. |
| 272 | if (prevBestBatchParams != info.bestBatchParams) { |
| 273 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_1) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 274 | 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] | 275 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, |
| 276 | info.bestBatchParams.batchTimeout); |
| 277 | err = mSensorDevice->batch(mSensorDevice, handle, info.bestBatchParams.flags, |
| 278 | info.bestBatchParams.batchDelay, |
| 279 | info.bestBatchParams.batchTimeout); |
| 280 | } else { |
| 281 | // For older devices which do not support batch, call setDelay() after activate() is |
| 282 | // called. Some older devices may not support calling setDelay before activate(), so |
| 283 | // call setDelay in SensorDevice::activate() method. |
| 284 | } |
| 285 | if (err != NO_ERROR) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 286 | ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", |
| 287 | mSensorDevice, handle, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 288 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, |
| 289 | info.bestBatchParams.batchTimeout, strerror(-err)); |
| 290 | info.removeBatchParamsForIdent(ident); |
| 291 | } |
| 292 | } |
| 293 | return err; |
| 294 | } |
| 295 | |
| 296 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 297 | { |
| 298 | if (!mSensorDevice) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 299 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 300 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 301 | } |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 302 | Mutex::Autolock _l(mLock); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 303 | if (isClientDisabledLocked(ident)) return INVALID_OPERATION; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 304 | Info& info( mActivationCount.editValueFor(handle) ); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 305 | // If the underlying sensor is NOT in continuous mode, setDelay() should return an error. |
| 306 | // Calling setDelay() in batch mode is an invalid operation. |
| 307 | if (info.bestBatchParams.batchTimeout != 0) { |
| 308 | return INVALID_OPERATION; |
| 309 | } |
| 310 | ssize_t index = info.batchParams.indexOfKey(ident); |
| 311 | if (index < 0) { |
| 312 | return BAD_INDEX; |
| 313 | } |
| 314 | BatchParams& params = info.batchParams.editValueAt(index); |
| 315 | params.batchDelay = samplingPeriodNs; |
| 316 | info.selectBatchParams(); |
| 317 | return mSensorDevice->setDelay(reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 318 | handle, info.bestBatchParams.batchDelay); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 321 | int SensorDevice::getHalDeviceVersion() const { |
| 322 | if (!mSensorDevice) return -1; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 323 | return mSensorDevice->common.version; |
| 324 | } |
| 325 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 326 | status_t SensorDevice::flush(void* ident, int handle) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 327 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) { |
| 328 | return INVALID_OPERATION; |
| 329 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 330 | if (isClientDisabled(ident)) return INVALID_OPERATION; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 331 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); |
| 332 | return mSensorDevice->flush(mSensorDevice, handle); |
| 333 | } |
| 334 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 335 | bool SensorDevice::isClientDisabled(void* ident) { |
| 336 | Mutex::Autolock _l(mLock); |
| 337 | return isClientDisabledLocked(ident); |
| 338 | } |
| 339 | |
| 340 | bool SensorDevice::isClientDisabledLocked(void* ident) { |
| 341 | return mDisabledClients.indexOf(ident) >= 0; |
| 342 | } |
| 343 | |
| 344 | void SensorDevice::enableAllSensors() { |
| 345 | Mutex::Autolock _l(mLock); |
| 346 | mDisabledClients.clear(); |
| 347 | const int halVersion = getHalDeviceVersion(); |
| 348 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 349 | Info& info = mActivationCount.editValueAt(i); |
| 350 | if (info.batchParams.isEmpty()) continue; |
| 351 | info.selectBatchParams(); |
| 352 | const int sensor_handle = mActivationCount.keyAt(i); |
| 353 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", |
| 354 | sensor_handle); |
| 355 | status_t err(NO_ERROR); |
| 356 | if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) { |
| 357 | err = mSensorDevice->batch(mSensorDevice, sensor_handle, |
| 358 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, |
| 359 | info.bestBatchParams.batchTimeout); |
| 360 | ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err)); |
| 361 | } |
| 362 | |
| 363 | if (err == NO_ERROR) { |
| 364 | err = mSensorDevice->activate( |
| 365 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 366 | sensor_handle, 1); |
| 367 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); |
| 368 | } |
| 369 | |
| 370 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0) { |
| 371 | err = mSensorDevice->setDelay( |
| 372 | reinterpret_cast<struct sensors_poll_device_t *>(mSensorDevice), |
| 373 | sensor_handle, info.bestBatchParams.batchDelay); |
| 374 | ALOGE_IF(err, "Error calling setDelay sensor %d (%s)", sensor_handle, strerror(-err)); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void SensorDevice::disableAllSensors() { |
| 380 | Mutex::Autolock _l(mLock); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 381 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 382 | const Info& info = mActivationCount.valueAt(i); |
| 383 | // Check if this sensor has been activated previously and disable it. |
| 384 | if (info.batchParams.size() > 0) { |
| 385 | const int sensor_handle = mActivationCount.keyAt(i); |
| 386 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", |
| 387 | sensor_handle); |
| 388 | mSensorDevice->activate( |
| 389 | reinterpret_cast<struct sensors_poll_device_t *> (mSensorDevice), |
| 390 | sensor_handle, 0); |
| 391 | // Add all the connections that were registered for this sensor to the disabled |
| 392 | // clients list. |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 393 | for (size_t j = 0; j < info.batchParams.size(); ++j) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 394 | mDisabledClients.add(info.batchParams.keyAt(j)); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 400 | status_t SensorDevice::injectSensorData(const sensors_event_t *injected_sensor_event) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 401 | ALOGD_IF(DEBUG_CONNECTIONS, |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 402 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 403 | injected_sensor_event->sensor, |
| 404 | injected_sensor_event->timestamp, injected_sensor_event->data[0], |
| 405 | injected_sensor_event->data[1], injected_sensor_event->data[2], |
| 406 | injected_sensor_event->data[3], injected_sensor_event->data[4], |
| 407 | injected_sensor_event->data[5]); |
| 408 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { |
| 409 | return INVALID_OPERATION; |
| 410 | } |
| 411 | return mSensorDevice->inject_sensor_data(mSensorDevice, injected_sensor_event); |
| 412 | } |
| 413 | |
| 414 | status_t SensorDevice::setMode(uint32_t mode) { |
| 415 | if (getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_4) { |
| 416 | return INVALID_OPERATION; |
| 417 | } |
| 418 | return mSensorModule->set_operation_mode(mode); |
| 419 | } |
| 420 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 421 | // --------------------------------------------------------------------------- |
| 422 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 423 | int SensorDevice::Info::numActiveClients() { |
| 424 | SensorDevice& device(SensorDevice::getInstance()); |
| 425 | int num = 0; |
| 426 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 427 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 428 | ++num; |
| 429 | } |
| 430 | } |
| 431 | return num; |
| 432 | } |
| 433 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 434 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, |
| 435 | int64_t samplingPeriodNs, |
| 436 | int64_t maxBatchReportLatencyNs) { |
| 437 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 438 | if (index < 0) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 439 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 440 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 441 | return BAD_INDEX; |
| 442 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 443 | BatchParams& params = batchParams.editValueAt(index); |
| 444 | params.flags = flags; |
| 445 | params.batchDelay = samplingPeriodNs; |
| 446 | params.batchTimeout = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 447 | return NO_ERROR; |
| 448 | } |
| 449 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 450 | void SensorDevice::Info::selectBatchParams() { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 451 | BatchParams bestParams(0, -1, -1); |
| 452 | SensorDevice& device(SensorDevice::getInstance()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 453 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 454 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 455 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) continue; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 456 | BatchParams params = batchParams.valueAt(i); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 457 | if (bestParams.batchDelay == -1 || params.batchDelay < bestParams.batchDelay) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 458 | bestParams.batchDelay = params.batchDelay; |
| 459 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 460 | if (bestParams.batchTimeout == -1 || params.batchTimeout < bestParams.batchTimeout) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 461 | bestParams.batchTimeout = params.batchTimeout; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 462 | } |
| 463 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 464 | bestBatchParams = bestParams; |
| 465 | } |
| 466 | |
| 467 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 468 | ssize_t idx = batchParams.removeItem(ident); |
| 469 | if (idx >= 0) { |
| 470 | selectBatchParams(); |
| 471 | } |
| 472 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // --------------------------------------------------------------------------- |
| 476 | }; // namespace android |
| 477 | |