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