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