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