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