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