| 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 | */ | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 16 | #include "SensorDevice.h" | 
|  | 17 | #include "SensorService.h" | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 20 | #include <sensors/convert.h> | 
| Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 21 | #include <utils/Atomic.h> | 
|  | 22 | #include <utils/Errors.h> | 
|  | 23 | #include <utils/Singleton.h> | 
| Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 24 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 25 | #include <chrono> | 
|  | 26 | #include <cinttypes> | 
|  | 27 | #include <thread> | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | using android::hardware::hidl_vec; | 
|  | 30 |  | 
|  | 31 | using namespace android::hardware::sensors::V1_0; | 
|  | 32 | using namespace android::hardware::sensors::V1_0::implementation; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) | 
|  | 38 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 39 | static status_t StatusFromResult(Result result) { | 
|  | 40 | switch (result) { | 
|  | 41 | case Result::OK: | 
|  | 42 | return OK; | 
|  | 43 | case Result::BAD_VALUE: | 
|  | 44 | return BAD_VALUE; | 
|  | 45 | case Result::PERMISSION_DENIED: | 
|  | 46 | return PERMISSION_DENIED; | 
|  | 47 | case Result::INVALID_OPERATION: | 
|  | 48 | return INVALID_OPERATION; | 
|  | 49 | case Result::NO_MEMORY: | 
|  | 50 | return NO_MEMORY; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 54 | SensorDevice::SensorDevice() : mHidlTransportErrors(20) { | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 55 | // SensorDevice may wait upto 100ms * 10 = 1s for hidl service. | 
|  | 56 | constexpr auto RETRY_DELAY = std::chrono::milliseconds(100); | 
|  | 57 | size_t retry = 10; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 58 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 59 | while (true) { | 
|  | 60 | int initStep = 0; | 
|  | 61 | mSensors = ISensors::getService(); | 
|  | 62 | if (mSensors != nullptr) { | 
|  | 63 | ++initStep; | 
|  | 64 | // Poke ISensor service. If it has lingering connection from previous generation of | 
|  | 65 | // system server, it will kill itself. There is no intention to handle the poll result, | 
|  | 66 | // which will be done since the size is 0. | 
|  | 67 | if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) { | 
|  | 68 | // ok to continue | 
|  | 69 | break; | 
|  | 70 | } | 
|  | 71 | // hidl service is restarting, pointer is invalid. | 
|  | 72 | mSensors = nullptr; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | if (--retry <= 0) { | 
|  | 76 | ALOGE("Cannot connect to ISensors hidl service!"); | 
|  | 77 | return; | 
|  | 78 | } | 
|  | 79 | // Delay 100ms before retry, hidl service is expected to come up in short time after | 
|  | 80 | // crash. | 
|  | 81 | ALOGI("%s unsuccessful, try again soon (remaining retry %zu).", | 
|  | 82 | (initStep == 0) ? "getService()" : "poll() check", retry); | 
|  | 83 | std::this_thread::sleep_for(RETRY_DELAY); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 86 | checkReturn(mSensors->getSensorsList( | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 87 | [&](const auto &list) { | 
|  | 88 | const size_t count = list.size(); | 
|  | 89 |  | 
|  | 90 | mActivationCount.setCapacity(count); | 
|  | 91 | Info model; | 
|  | 92 | for (size_t i=0 ; i < count; i++) { | 
|  | 93 | sensor_t sensor; | 
|  | 94 | convertToSensor(list[i], &sensor); | 
|  | 95 | mSensorList.push_back(sensor); | 
|  | 96 |  | 
|  | 97 | mActivationCount.add(list[i].sensorHandle, model); | 
|  | 98 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 99 | checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */)); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 100 | } | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 101 | })); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 102 |  | 
|  | 103 | mIsDirectReportSupported = | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 104 | (checkReturn(mSensors->unregisterDirectChannel(-1)) != Result::INVALID_OPERATION); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 107 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { | 
|  | 108 | if (connected) { | 
|  | 109 | Info model; | 
|  | 110 | mActivationCount.add(handle, model); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 111 | checkReturn(mSensors->activate(handle, 0 /* enabled */)); | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 112 | } else { | 
|  | 113 | mActivationCount.removeItem(handle); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 117 | std::string SensorDevice::dump() const { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 118 | if (mSensors == NULL) return "HAL not initialized\n"; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 119 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 120 | String8 result; | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | result.appendFormat("Saw %d hidlTransport Errors\n", mTotalHidlTransportErrors); | 
|  | 123 | for (auto it = mHidlTransportErrors.begin() ; it != mHidlTransportErrors.end(); it++ ) { | 
|  | 124 | result += "\t"; | 
|  | 125 | result += it->toString(); | 
|  | 126 | result += "\n"; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | checkReturn(mSensors->getSensorsList([&](const auto &list){ | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 130 | const size_t count = list.size(); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 131 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 132 | result.appendFormat( | 
|  | 133 | "Total %zu h/w sensors, %zu running:\n", | 
|  | 134 | count, | 
|  | 135 | mActivationCount.size()); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 136 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 137 | Mutex::Autolock _l(mLock); | 
|  | 138 | for (size_t i = 0 ; i < count ; i++) { | 
|  | 139 | const Info& info = mActivationCount.valueFor( | 
|  | 140 | list[i].sensorHandle); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 141 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 142 | if (info.batchParams.isEmpty()) continue; | 
|  | 143 | result.appendFormat( | 
|  | 144 | "0x%08x) active-count = %zu; ", | 
|  | 145 | list[i].sensorHandle, | 
|  | 146 | info.batchParams.size()); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 147 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 148 | result.append("sampling_period(ms) = {"); | 
|  | 149 | for (size_t j = 0; j < info.batchParams.size(); j++) { | 
|  | 150 | const BatchParams& params = info.batchParams.valueAt(j); | 
|  | 151 | result.appendFormat( | 
|  | 152 | "%.1f%s", | 
|  | 153 | params.batchDelay / 1e6f, | 
|  | 154 | j < info.batchParams.size() - 1 ? ", " : ""); | 
|  | 155 | } | 
|  | 156 | result.appendFormat( | 
|  | 157 | "}, selected = %.1f ms; ", | 
|  | 158 | info.bestBatchParams.batchDelay / 1e6f); | 
|  | 159 |  | 
|  | 160 | result.append("batching_period(ms) = {"); | 
|  | 161 | for (size_t j = 0; j < info.batchParams.size(); j++) { | 
|  | 162 | BatchParams params = info.batchParams.valueAt(j); | 
|  | 163 |  | 
|  | 164 | result.appendFormat( | 
|  | 165 | "%.1f%s", | 
|  | 166 | params.batchTimeout / 1e6f, | 
|  | 167 | j < info.batchParams.size() - 1 ? ", " : ""); | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | result.appendFormat( | 
|  | 171 | "}, selected = %.1f ms\n", | 
|  | 172 | info.bestBatchParams.batchTimeout / 1e6f); | 
|  | 173 | } | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 174 | })); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 175 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 176 | return result.string(); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
|  | 179 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 180 | *list = &mSensorList[0]; | 
|  | 181 |  | 
|  | 182 | return mSensorList.size(); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
|  | 185 | status_t SensorDevice::initCheck() const { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 186 | return mSensors != NULL ? NO_ERROR : NO_INIT; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
|  | 189 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 190 | if (mSensors == NULL) return NO_INIT; | 
|  | 191 |  | 
|  | 192 | ssize_t err; | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 193 | int numHidlTransportErrors = 0; | 
|  | 194 | bool hidlTransportError = false; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 195 |  | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 196 | do { | 
|  | 197 | auto ret = mSensors->poll( | 
|  | 198 | count, | 
|  | 199 | [&](auto result, | 
|  | 200 | const auto &events, | 
|  | 201 | const auto &dynamicSensorsAdded) { | 
|  | 202 | if (result == Result::OK) { | 
|  | 203 | convertToSensorEvents(events, dynamicSensorsAdded, buffer); | 
|  | 204 | err = (ssize_t)events.size(); | 
|  | 205 | } else { | 
|  | 206 | err = StatusFromResult(result); | 
|  | 207 | } | 
|  | 208 | }); | 
|  | 209 |  | 
|  | 210 | if (ret.isOk())  { | 
|  | 211 | hidlTransportError = false; | 
|  | 212 | } else { | 
|  | 213 | hidlTransportError = true; | 
|  | 214 | numHidlTransportErrors++; | 
|  | 215 | if (numHidlTransportErrors > 50) { | 
|  | 216 | // Log error and bail | 
|  | 217 | ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors); | 
|  | 218 | handleHidlDeath(ret.description()); | 
|  | 219 | } else { | 
|  | 220 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 | } while (hidlTransportError); | 
|  | 224 |  | 
|  | 225 | if(numHidlTransportErrors > 0) { | 
|  | 226 | ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors); | 
|  | 227 | HidlTransportErrorLog errLog(time(NULL), numHidlTransportErrors); | 
|  | 228 | mHidlTransportErrors.add(errLog); | 
|  | 229 | mTotalHidlTransportErrors++; | 
|  | 230 | } | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 231 |  | 
|  | 232 | return err; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 235 | void SensorDevice::autoDisable(void *ident, int handle) { | 
|  | 236 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 237 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 238 | info.removeBatchParamsForIdent(ident); | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 241 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 242 | if (mSensors == NULL) return NO_INIT; | 
|  | 243 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 244 | status_t err(NO_ERROR); | 
|  | 245 | bool actuateHardware = false; | 
|  | 246 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 247 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 248 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 249 |  | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 250 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 251 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 252 | ident, handle, enabled, info.batchParams.size()); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 253 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 254 | if (enabled) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 255 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 256 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 257 | if (isClientDisabledLocked(ident)) { | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 258 | ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", | 
|  | 259 | ident, handle); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 260 | return INVALID_OPERATION; | 
|  | 261 | } | 
|  | 262 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 263 | if (info.batchParams.indexOfKey(ident) >= 0) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 264 | if (info.numActiveClients() == 1) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 265 | // This is the first connection, we need to activate the underlying h/w sensor. | 
|  | 266 | actuateHardware = true; | 
|  | 267 | } | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 268 | } else { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 269 | // Log error. Every activate call should be preceded by a batch() call. | 
|  | 270 | ALOGE("\t >>>ERROR: activate called without batch"); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 271 | } | 
|  | 272 | } else { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 273 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 274 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 275 | // If a connected dynamic sensor is deactivated, remove it from the | 
|  | 276 | // dictionary. | 
|  | 277 | auto it = mConnectedDynamicSensors.find(handle); | 
|  | 278 | if (it != mConnectedDynamicSensors.end()) { | 
|  | 279 | delete it->second; | 
|  | 280 | mConnectedDynamicSensors.erase(it); | 
|  | 281 | } | 
|  | 282 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 283 | if (info.removeBatchParamsForIdent(ident) >= 0) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 284 | if (info.numActiveClients() == 0) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 285 | // 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] | 286 | actuateHardware = true; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 287 | } else { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 288 | // Call batch for this sensor with the previously calculated best effort | 
|  | 289 | // batch_rate and timeout. One of the apps has unregistered for sensor | 
|  | 290 | // events, and the best effort batch parameters might have changed. | 
|  | 291 | ALOGD_IF(DEBUG_CONNECTIONS, | 
|  | 292 | "\t>>> actuating h/w batch %d %d %" PRId64 " %" PRId64, handle, | 
|  | 293 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, | 
|  | 294 | info.bestBatchParams.batchTimeout); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 295 | checkReturn(mSensors->batch( | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 296 | handle, | 
|  | 297 | info.bestBatchParams.batchDelay, | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 298 | info.bestBatchParams.batchTimeout)); | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 299 | } | 
|  | 300 | } else { | 
|  | 301 | // sensor wasn't enabled for this ident | 
|  | 302 | } | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 303 |  | 
|  | 304 | if (isClientDisabledLocked(ident)) { | 
|  | 305 | return NO_ERROR; | 
|  | 306 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 307 | } | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 308 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 309 | if (actuateHardware) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 310 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, | 
|  | 311 | enabled); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 312 | err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 313 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, | 
|  | 314 | strerror(-err)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 315 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 316 | if (err != NO_ERROR && enabled) { | 
|  | 317 | // Failure when enabling the sensor. Clean up on failure. | 
|  | 318 | info.removeBatchParamsForIdent(ident); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 319 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 322 | return err; | 
|  | 323 | } | 
|  | 324 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 325 | status_t SensorDevice::batch( | 
|  | 326 | void* ident, | 
|  | 327 | int handle, | 
|  | 328 | int flags, | 
|  | 329 | int64_t samplingPeriodNs, | 
|  | 330 | int64_t maxBatchReportLatencyNs) { | 
|  | 331 | if (mSensors == NULL) return NO_INIT; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 332 |  | 
|  | 333 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { | 
|  | 334 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; | 
|  | 335 | } | 
|  | 336 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 337 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 338 | "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] | 339 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 340 |  | 
|  | 341 | Mutex::Autolock _l(mLock); | 
|  | 342 | Info& info(mActivationCount.editValueFor(handle)); | 
|  | 343 |  | 
|  | 344 | if (info.batchParams.indexOfKey(ident) < 0) { | 
|  | 345 | BatchParams params(flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 346 | info.batchParams.add(ident, params); | 
|  | 347 | } else { | 
|  | 348 | // A batch has already been called with this ident. Update the batch parameters. | 
|  | 349 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | BatchParams prevBestBatchParams = info.bestBatchParams; | 
|  | 353 | // Find the minimum of all timeouts and batch_rates for this sensor. | 
|  | 354 | info.selectBatchParams(); | 
|  | 355 |  | 
|  | 356 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 357 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 | 
|  | 358 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 359 | prevBestBatchParams.batchDelay, info.bestBatchParams.batchDelay, | 
|  | 360 | prevBestBatchParams.batchTimeout, info.bestBatchParams.batchTimeout); | 
|  | 361 |  | 
|  | 362 | status_t err(NO_ERROR); | 
|  | 363 | // If the min period or min timeout has changed since the last batch call, call batch. | 
|  | 364 | if (prevBestBatchParams != info.bestBatchParams) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 365 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH %d %d %" PRId64 " %" PRId64, handle, | 
|  | 366 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, | 
|  | 367 | info.bestBatchParams.batchTimeout); | 
|  | 368 | err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 369 | checkReturn(mSensors->batch( | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 370 | handle, | 
|  | 371 | info.bestBatchParams.batchDelay, | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 372 | info.bestBatchParams.batchTimeout))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 373 | if (err != NO_ERROR) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 374 | ALOGE("sensor batch failed %p %d %d %" PRId64 " %" PRId64 " err=%s", | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 375 | mSensors.get(), handle, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 376 | info.bestBatchParams.flags, info.bestBatchParams.batchDelay, | 
|  | 377 | info.bestBatchParams.batchTimeout, strerror(-err)); | 
|  | 378 | info.removeBatchParamsForIdent(ident); | 
|  | 379 | } | 
|  | 380 | } | 
|  | 381 | return err; | 
|  | 382 | } | 
|  | 383 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 384 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 385 | if (mSensors == NULL) return NO_INIT; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 386 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { | 
|  | 387 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; | 
|  | 388 | } | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 389 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 390 | if (isClientDisabledLocked(ident)) return INVALID_OPERATION; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 391 | Info& info( mActivationCount.editValueFor(handle) ); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 392 | // If the underlying sensor is NOT in continuous mode, setDelay() should return an error. | 
|  | 393 | // Calling setDelay() in batch mode is an invalid operation. | 
|  | 394 | if (info.bestBatchParams.batchTimeout != 0) { | 
|  | 395 | return INVALID_OPERATION; | 
|  | 396 | } | 
|  | 397 | ssize_t index = info.batchParams.indexOfKey(ident); | 
|  | 398 | if (index < 0) { | 
|  | 399 | return BAD_INDEX; | 
|  | 400 | } | 
|  | 401 | BatchParams& params = info.batchParams.editValueAt(index); | 
|  | 402 | params.batchDelay = samplingPeriodNs; | 
|  | 403 | info.selectBatchParams(); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 404 |  | 
|  | 405 | return StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 406 | checkReturn(mSensors->batch(handle, info.bestBatchParams.batchDelay, 0))); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 409 | int SensorDevice::getHalDeviceVersion() const { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 410 | if (mSensors == NULL) return -1; | 
|  | 411 | return SENSORS_DEVICE_API_VERSION_1_4; | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 414 | status_t SensorDevice::flush(void* ident, int handle) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 415 | if (isClientDisabled(ident)) return INVALID_OPERATION; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 416 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 417 | return StatusFromResult(checkReturn(mSensors->flush(handle))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 420 | bool SensorDevice::isClientDisabled(void* ident) { | 
|  | 421 | Mutex::Autolock _l(mLock); | 
|  | 422 | return isClientDisabledLocked(ident); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | bool SensorDevice::isClientDisabledLocked(void* ident) { | 
|  | 426 | return mDisabledClients.indexOf(ident) >= 0; | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | void SensorDevice::enableAllSensors() { | 
|  | 430 | Mutex::Autolock _l(mLock); | 
|  | 431 | mDisabledClients.clear(); | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 432 | ALOGI("cleared mDisabledClients"); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 433 | for (size_t i = 0; i< mActivationCount.size(); ++i) { | 
|  | 434 | Info& info = mActivationCount.editValueAt(i); | 
|  | 435 | if (info.batchParams.isEmpty()) continue; | 
|  | 436 | info.selectBatchParams(); | 
|  | 437 | const int sensor_handle = mActivationCount.keyAt(i); | 
|  | 438 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", | 
|  | 439 | sensor_handle); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 440 | status_t err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 441 | checkReturn(mSensors->batch( | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 442 | sensor_handle, | 
|  | 443 | info.bestBatchParams.batchDelay, | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 444 | info.bestBatchParams.batchTimeout))); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 445 | ALOGE_IF(err, "Error calling batch on sensor %d (%s)", sensor_handle, strerror(-err)); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 446 |  | 
|  | 447 | if (err == NO_ERROR) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 448 | err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 449 | checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */))); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 450 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); | 
|  | 451 | } | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | void SensorDevice::disableAllSensors() { | 
|  | 456 | Mutex::Autolock _l(mLock); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 457 | for (size_t i = 0; i< mActivationCount.size(); ++i) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 458 | const Info& info = mActivationCount.valueAt(i); | 
|  | 459 | // Check if this sensor has been activated previously and disable it. | 
|  | 460 | if (info.batchParams.size() > 0) { | 
|  | 461 | const int sensor_handle = mActivationCount.keyAt(i); | 
|  | 462 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", | 
|  | 463 | sensor_handle); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 464 | checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */)); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 465 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 466 | // Add all the connections that were registered for this sensor to the disabled | 
|  | 467 | // clients list. | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 468 | for (size_t j = 0; j < info.batchParams.size(); ++j) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 469 | mDisabledClients.add(info.batchParams.keyAt(j)); | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 470 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 471 | } | 
|  | 472 | } | 
|  | 473 | } | 
|  | 474 | } | 
|  | 475 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 476 | status_t SensorDevice::injectSensorData( | 
|  | 477 | const sensors_event_t *injected_sensor_event) { | 
|  | 478 | ALOGD_IF(DEBUG_CONNECTIONS, | 
|  | 479 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", | 
|  | 480 | injected_sensor_event->sensor, | 
|  | 481 | injected_sensor_event->timestamp, injected_sensor_event->data[0], | 
|  | 482 | injected_sensor_event->data[1], injected_sensor_event->data[2], | 
|  | 483 | injected_sensor_event->data[3], injected_sensor_event->data[4], | 
|  | 484 | injected_sensor_event->data[5]); | 
|  | 485 |  | 
|  | 486 | Event ev; | 
|  | 487 | convertFromSensorEvent(*injected_sensor_event, &ev); | 
|  | 488 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 489 | return StatusFromResult(checkReturn(mSensors->injectSensorData(ev))); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 490 | } | 
|  | 491 |  | 
|  | 492 | status_t SensorDevice::setMode(uint32_t mode) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 493 |  | 
|  | 494 | return StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 495 | checkReturn(mSensors->setOperationMode( | 
|  | 496 | static_cast<hardware::sensors::V1_0::OperationMode>(mode)))); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 497 | } | 
|  | 498 |  | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 499 | // --------------------------------------------------------------------------- | 
|  | 500 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 501 | int SensorDevice::Info::numActiveClients() { | 
|  | 502 | SensorDevice& device(SensorDevice::getInstance()); | 
|  | 503 | int num = 0; | 
|  | 504 | for (size_t i = 0; i < batchParams.size(); ++i) { | 
|  | 505 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { | 
|  | 506 | ++num; | 
|  | 507 | } | 
|  | 508 | } | 
|  | 509 | return num; | 
|  | 510 | } | 
|  | 511 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 512 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int flags, | 
|  | 513 | int64_t samplingPeriodNs, | 
|  | 514 | int64_t maxBatchReportLatencyNs) { | 
|  | 515 | ssize_t index = batchParams.indexOfKey(ident); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 516 | if (index < 0) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 517 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 ") failed (%s)", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 518 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 519 | return BAD_INDEX; | 
|  | 520 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 521 | BatchParams& params = batchParams.editValueAt(index); | 
|  | 522 | params.flags = flags; | 
|  | 523 | params.batchDelay = samplingPeriodNs; | 
|  | 524 | params.batchTimeout = maxBatchReportLatencyNs; | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 525 | return NO_ERROR; | 
|  | 526 | } | 
|  | 527 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 528 | void SensorDevice::Info::selectBatchParams() { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 529 | BatchParams bestParams(0, -1, -1); | 
|  | 530 | SensorDevice& device(SensorDevice::getInstance()); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 531 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 532 | for (size_t i = 0; i < batchParams.size(); ++i) { | 
|  | 533 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) continue; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 534 | BatchParams params = batchParams.valueAt(i); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 535 | if (bestParams.batchDelay == -1 || params.batchDelay < bestParams.batchDelay) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 536 | bestParams.batchDelay = params.batchDelay; | 
|  | 537 | } | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 538 | if (bestParams.batchTimeout == -1 || params.batchTimeout < bestParams.batchTimeout) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 539 | bestParams.batchTimeout = params.batchTimeout; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 540 | } | 
|  | 541 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 542 | bestBatchParams = bestParams; | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { | 
|  | 546 | ssize_t idx = batchParams.removeItem(ident); | 
|  | 547 | if (idx >= 0) { | 
|  | 548 | selectBatchParams(); | 
|  | 549 | } | 
|  | 550 | return idx; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 551 | } | 
|  | 552 |  | 
| Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 553 | void SensorDevice::notifyConnectionDestroyed(void* ident) { | 
|  | 554 | Mutex::Autolock _l(mLock); | 
|  | 555 | mDisabledClients.remove(ident); | 
|  | 556 | } | 
|  | 557 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 558 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { | 
|  | 559 | Mutex::Autolock _l(mLock); | 
|  | 560 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 561 | SharedMemType type; | 
|  | 562 | switch (memory->type) { | 
|  | 563 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: | 
|  | 564 | type = SharedMemType::ASHMEM; | 
|  | 565 | break; | 
|  | 566 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: | 
|  | 567 | type = SharedMemType::GRALLOC; | 
|  | 568 | break; | 
|  | 569 | default: | 
|  | 570 | return BAD_VALUE; | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | SharedMemFormat format; | 
|  | 574 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { | 
|  | 575 | return BAD_VALUE; | 
|  | 576 | } | 
|  | 577 | format = SharedMemFormat::SENSORS_EVENT; | 
|  | 578 |  | 
|  | 579 | SharedMemInfo mem = { | 
|  | 580 | .type = type, | 
|  | 581 | .format = format, | 
|  | 582 | .size = static_cast<uint32_t>(memory->size), | 
|  | 583 | .memoryHandle = memory->handle, | 
|  | 584 | }; | 
|  | 585 |  | 
|  | 586 | int32_t ret; | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 587 | checkReturn(mSensors->registerDirectChannel(mem, | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 588 | [&ret](auto result, auto channelHandle) { | 
|  | 589 | if (result == Result::OK) { | 
|  | 590 | ret = channelHandle; | 
|  | 591 | } else { | 
|  | 592 | ret = StatusFromResult(result); | 
|  | 593 | } | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 594 | })); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 595 | return ret; | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 596 | } | 
|  | 597 |  | 
|  | 598 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { | 
|  | 599 | Mutex::Autolock _l(mLock); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 600 | checkReturn(mSensors->unregisterDirectChannel(channelHandle)); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 601 | } | 
|  | 602 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 603 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, | 
|  | 604 | int32_t channelHandle, const struct sensors_direct_cfg_t *config) { | 
| Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 605 | Mutex::Autolock _l(mLock); | 
| Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 606 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 607 | RateLevel rate; | 
|  | 608 | switch(config->rate_level) { | 
|  | 609 | case SENSOR_DIRECT_RATE_STOP: | 
|  | 610 | rate = RateLevel::STOP; | 
|  | 611 | break; | 
|  | 612 | case SENSOR_DIRECT_RATE_NORMAL: | 
|  | 613 | rate = RateLevel::NORMAL; | 
|  | 614 | break; | 
|  | 615 | case SENSOR_DIRECT_RATE_FAST: | 
|  | 616 | rate = RateLevel::FAST; | 
|  | 617 | break; | 
|  | 618 | case SENSOR_DIRECT_RATE_VERY_FAST: | 
|  | 619 | rate = RateLevel::VERY_FAST; | 
|  | 620 | break; | 
|  | 621 | default: | 
|  | 622 | return BAD_VALUE; | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | int32_t ret; | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 626 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 627 | [&ret, rate] (auto result, auto token) { | 
|  | 628 | if (rate == RateLevel::STOP) { | 
|  | 629 | ret = StatusFromResult(result); | 
|  | 630 | } else { | 
|  | 631 | if (result == Result::OK) { | 
|  | 632 | ret = token; | 
|  | 633 | } else { | 
|  | 634 | ret = StatusFromResult(result); | 
|  | 635 | } | 
|  | 636 | } | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 637 | })); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 638 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 639 | return ret; | 
|  | 640 | } | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 641 |  | 
|  | 642 | bool SensorDevice::isDirectReportSupported() const { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 643 | return mIsDirectReportSupported; | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 644 | } | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 645 |  | 
|  | 646 | void SensorDevice::convertToSensorEvent( | 
|  | 647 | const Event &src, sensors_event_t *dst) { | 
|  | 648 | ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent( | 
|  | 649 | src, dst); | 
|  | 650 |  | 
|  | 651 | if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) { | 
|  | 652 | const DynamicSensorInfo &dyn = src.u.dynamic; | 
|  | 653 |  | 
|  | 654 | dst->dynamic_sensor_meta.connected = dyn.connected; | 
|  | 655 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; | 
|  | 656 | if (dyn.connected) { | 
|  | 657 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); | 
|  | 658 | CHECK(it != mConnectedDynamicSensors.end()); | 
|  | 659 |  | 
|  | 660 | dst->dynamic_sensor_meta.sensor = it->second; | 
|  | 661 |  | 
|  | 662 | memcpy(dst->dynamic_sensor_meta.uuid, | 
|  | 663 | dyn.uuid.data(), | 
|  | 664 | sizeof(dst->dynamic_sensor_meta.uuid)); | 
|  | 665 | } | 
|  | 666 | } | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | void SensorDevice::convertToSensorEvents( | 
|  | 670 | const hidl_vec<Event> &src, | 
|  | 671 | const hidl_vec<SensorInfo> &dynamicSensorsAdded, | 
|  | 672 | sensors_event_t *dst) { | 
|  | 673 | // Allocate a sensor_t structure for each dynamic sensor added and insert | 
|  | 674 | // it into the dictionary of connected dynamic sensors keyed by handle. | 
|  | 675 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { | 
|  | 676 | const SensorInfo &info = dynamicSensorsAdded[i]; | 
|  | 677 |  | 
|  | 678 | auto it = mConnectedDynamicSensors.find(info.sensorHandle); | 
|  | 679 | CHECK(it == mConnectedDynamicSensors.end()); | 
|  | 680 |  | 
|  | 681 | sensor_t *sensor = new sensor_t; | 
|  | 682 | convertToSensor(info, sensor); | 
|  | 683 |  | 
|  | 684 | mConnectedDynamicSensors.insert( | 
|  | 685 | std::make_pair(sensor->handle, sensor)); | 
|  | 686 | } | 
|  | 687 |  | 
|  | 688 | for (size_t i = 0; i < src.size(); ++i) { | 
|  | 689 | convertToSensorEvent(src[i], &dst[i]); | 
|  | 690 | } | 
|  | 691 | } | 
|  | 692 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 693 | void SensorDevice::handleHidlDeath(const std::string & detail) { | 
|  | 694 | // restart is the only option at present. | 
|  | 695 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); | 
|  | 696 | } | 
|  | 697 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 698 | // --------------------------------------------------------------------------- | 
|  | 699 | }; // namespace android |