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