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