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 | */ |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 16 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 17 | #include "SensorDevice.h" |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include "android/hardware/sensors/2.0/types.h" |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 20 | #include "android/hardware/sensors/2.1/types.h" |
| 21 | #include "convertV2_1.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 22 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 24 | #include <android/util/ProtoOutputStream.h> |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 25 | #include <cutils/atomic.h> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 26 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 27 | #include <sensors/convert.h> |
Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Singleton.h> |
Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 30 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 31 | #include <chrono> |
| 32 | #include <cinttypes> |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 33 | #include <cstddef> |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 34 | #include <thread> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 35 | |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 36 | using namespace android::hardware::sensors; |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 37 | using android::hardware::Return; |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 38 | using android::util::ProtoOutputStream; |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 39 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 42 | |
| 43 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) |
| 44 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 47 | template <typename EnumType> |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 48 | constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) { |
| 49 | return static_cast<typename std::underlying_type<EnumType>::type>(value); |
| 50 | } |
| 51 | |
| 52 | // Used internally by the framework to wake the Event FMQ. These values must start after |
| 53 | // the last value of EventQueueFlagBits |
| 54 | enum EventQueueFlagBitsInternal : uint32_t { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 55 | INTERNAL_WAKE = 1 << 16, |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 58 | enum DevicePrivateBase : int32_t { |
| 59 | DEVICE_PRIVATE_BASE = 65536, |
Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 62 | } // anonymous namespace |
| 63 | |
| 64 | SensorDevice::SensorDevice() { |
| 65 | if (!connectHalService()) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 66 | return; |
| 67 | } |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 68 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 69 | initializeSensorList(); |
| 70 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 71 | mIsDirectReportSupported = (mHalWrapper->unregisterDirectChannel(-1) != INVALID_OPERATION); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void SensorDevice::initializeSensorList() { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 75 | if (mHalWrapper == nullptr) { |
| 76 | return; |
| 77 | } |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 78 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 79 | auto list = mHalWrapper->getSensorsList(); |
| 80 | const size_t count = list.size(); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 81 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 82 | mActivationCount.setCapacity(count); |
| 83 | Info model; |
| 84 | for (size_t i = 0; i < count; i++) { |
| 85 | sensor_t sensor = list[i]; |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 86 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 87 | if (sensor.type < DEVICE_PRIVATE_BASE) { |
| 88 | sensor.resolution = SensorDeviceUtils::resolutionForSensor(sensor); |
| 89 | |
| 90 | // Some sensors don't have a default resolution and will be left at 0. |
| 91 | // Don't crash in this case since CTS will verify that devices don't go to |
| 92 | // production with a resolution of 0. |
| 93 | if (sensor.resolution != 0) { |
| 94 | float quantizedRange = sensor.maxRange; |
| 95 | SensorDeviceUtils::quantizeValue(&quantizedRange, sensor.resolution, |
| 96 | /*factor=*/1); |
| 97 | // Only rewrite maxRange if the requantization produced a "significant" |
| 98 | // change, which is fairly arbitrarily defined as resolution / 8. |
| 99 | // Smaller deltas are permitted, as they may simply be due to floating |
| 100 | // point representation error, etc. |
| 101 | if (fabsf(sensor.maxRange - quantizedRange) > sensor.resolution / 8) { |
| 102 | ALOGW("%s's max range %.12f is not a multiple of the resolution " |
| 103 | "%.12f - updated to %.12f", |
| 104 | sensor.name, sensor.maxRange, sensor.resolution, quantizedRange); |
| 105 | sensor.maxRange = quantizedRange; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 106 | } |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 107 | } else { |
| 108 | // Don't crash here or the device will go into a crashloop. |
| 109 | ALOGW("%s should have a non-zero resolution", sensor.name); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 112 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 113 | // Check and clamp power if it is 0 (or close) |
| 114 | constexpr float MIN_POWER_MA = 0.001; // 1 microAmp |
| 115 | if (sensor.power < MIN_POWER_MA) { |
| 116 | ALOGI("%s's reported power %f invalid, clamped to %f", sensor.name, sensor.power, |
| 117 | MIN_POWER_MA); |
| 118 | sensor.power = MIN_POWER_MA; |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 119 | } |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 120 | mSensorList.push_back(sensor); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 121 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 122 | mActivationCount.add(list[i].handle, model); |
| 123 | |
| 124 | // Only disable all sensors on HAL 1.0 since HAL 2.0 |
| 125 | // handles this in its initialize method |
| 126 | if (!mHalWrapper->supportsMessageQueues()) { |
| 127 | mHalWrapper->activate(list[i].handle, 0 /* enabled */); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 128 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 129 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 132 | SensorDevice::~SensorDevice() {} |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 133 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 134 | bool SensorDevice::connectHalService() { |
| 135 | std::unique_ptr<ISensorHalWrapper> hidl_wrapper = std::make_unique<HidlSensorHalWrapper>(); |
| 136 | if (hidl_wrapper->connect(this)) { |
| 137 | mHalWrapper = std::move(hidl_wrapper); |
| 138 | return true; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 139 | } |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 140 | // TODO: check aidl connection; |
| 141 | return false; |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 144 | void SensorDevice::prepareForReconnect() { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 145 | mHalWrapper->prepareForReconnect(); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void SensorDevice::reconnect() { |
| 149 | Mutex::Autolock _l(mLock); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 150 | |
| 151 | auto previousActivations = mActivationCount; |
| 152 | auto previousSensorList = mSensorList; |
| 153 | |
| 154 | mActivationCount.clear(); |
| 155 | mSensorList.clear(); |
| 156 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 157 | if (mHalWrapper->connect(this)) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 158 | initializeSensorList(); |
| 159 | |
| 160 | if (sensorHandlesChanged(previousSensorList, mSensorList)) { |
| 161 | LOG_ALWAYS_FATAL("Sensor handles changed, cannot re-enable sensors."); |
| 162 | } else { |
| 163 | reactivateSensors(previousActivations); |
| 164 | } |
| 165 | } |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 166 | mHalWrapper->mReconnecting = false; |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Arthur Ishiguro | 4f1dd8a | 2021-11-12 17:21:24 +0000 | [diff] [blame] | 169 | bool SensorDevice::sensorHandlesChanged(const std::vector<sensor_t>& oldSensorList, |
| 170 | const std::vector<sensor_t>& newSensorList) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 171 | bool didChange = false; |
| 172 | |
| 173 | if (oldSensorList.size() != newSensorList.size()) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 174 | ALOGI("Sensor list size changed from %zu to %zu", oldSensorList.size(), |
| 175 | newSensorList.size()); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 176 | didChange = true; |
| 177 | } |
| 178 | |
| 179 | for (size_t i = 0; i < newSensorList.size() && !didChange; i++) { |
| 180 | bool found = false; |
| 181 | const sensor_t& newSensor = newSensorList[i]; |
| 182 | for (size_t j = 0; j < oldSensorList.size() && !found; j++) { |
| 183 | const sensor_t& prevSensor = oldSensorList[j]; |
| 184 | if (prevSensor.handle == newSensor.handle) { |
| 185 | found = true; |
| 186 | if (!sensorIsEquivalent(prevSensor, newSensor)) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 187 | ALOGI("Sensor %s not equivalent to previous version", newSensor.name); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 188 | didChange = true; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (!found) { |
| 194 | // Could not find the new sensor in the old list of sensors, the lists must |
| 195 | // have changed. |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 196 | ALOGI("Sensor %s (handle %d) did not exist before", newSensor.name, newSensor.handle); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 197 | didChange = true; |
| 198 | } |
| 199 | } |
| 200 | return didChange; |
| 201 | } |
| 202 | |
| 203 | bool SensorDevice::sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor) { |
| 204 | bool equivalent = true; |
| 205 | if (prevSensor.handle != newSensor.handle || |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 206 | (strcmp(prevSensor.vendor, newSensor.vendor) != 0) || |
| 207 | (strcmp(prevSensor.stringType, newSensor.stringType) != 0) || |
| 208 | (strcmp(prevSensor.requiredPermission, newSensor.requiredPermission) != 0) || |
| 209 | (prevSensor.version != newSensor.version) || (prevSensor.type != newSensor.type) || |
| 210 | (std::abs(prevSensor.maxRange - newSensor.maxRange) > 0.001f) || |
| 211 | (std::abs(prevSensor.resolution - newSensor.resolution) > 0.001f) || |
| 212 | (std::abs(prevSensor.power - newSensor.power) > 0.001f) || |
| 213 | (prevSensor.minDelay != newSensor.minDelay) || |
| 214 | (prevSensor.fifoReservedEventCount != newSensor.fifoReservedEventCount) || |
| 215 | (prevSensor.fifoMaxEventCount != newSensor.fifoMaxEventCount) || |
| 216 | (prevSensor.maxDelay != newSensor.maxDelay) || (prevSensor.flags != newSensor.flags)) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 217 | equivalent = false; |
| 218 | } |
| 219 | return equivalent; |
| 220 | } |
| 221 | |
| 222 | void SensorDevice::reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations) { |
| 223 | for (size_t i = 0; i < mSensorList.size(); i++) { |
| 224 | int handle = mSensorList[i].handle; |
| 225 | ssize_t activationIndex = previousActivations.indexOfKey(handle); |
| 226 | if (activationIndex < 0 || previousActivations[activationIndex].numActiveClients() <= 0) { |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | const Info& info = previousActivations[activationIndex]; |
| 231 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
| 232 | const BatchParams& batchParams = info.batchParams[j]; |
| 233 | status_t res = batchLocked(info.batchParams.keyAt(j), handle, 0 /* flags */, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 234 | batchParams.mTSample, batchParams.mTBatch); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 235 | |
| 236 | if (res == NO_ERROR) { |
| 237 | activateLocked(info.batchParams.keyAt(j), handle, true /* enabled */); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 243 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 244 | // 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] | 245 | if (connected) { |
| 246 | Info model; |
| 247 | mActivationCount.add(handle, model); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 248 | mHalWrapper->activate(handle, 0 /* enabled */); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 249 | } else { |
| 250 | mActivationCount.removeItem(handle); |
| 251 | } |
| 252 | } |
| 253 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 254 | std::string SensorDevice::dump() const { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 255 | if (mHalWrapper == nullptr) return "HAL not initialized\n"; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 256 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 257 | String8 result; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 258 | result.appendFormat("Total %zu h/w sensors, %zu running %zu disabled clients:\n", |
| 259 | mSensorList.size(), mActivationCount.size(), mDisabledClients.size()); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 260 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 261 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 262 | for (const auto& s : mSensorList) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 263 | int32_t handle = s.handle; |
| 264 | const Info& info = mActivationCount.valueFor(handle); |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 265 | if (info.numActiveClients() == 0) continue; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 266 | |
| 267 | result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size()); |
| 268 | |
| 269 | result.append("sampling_period(ms) = {"); |
| 270 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 271 | const BatchParams& params = info.batchParams[j]; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 272 | result.appendFormat("%.1f%s%s", params.mTSample / 1e6f, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 273 | isClientDisabledLocked(info.batchParams.keyAt(j)) ? "(disabled)" |
| 274 | : "", |
| 275 | (j < info.batchParams.size() - 1) ? ", " : ""); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 276 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 277 | result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 278 | |
| 279 | result.append("batching_period(ms) = {"); |
| 280 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 281 | const BatchParams& params = info.batchParams[j]; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 282 | result.appendFormat("%.1f%s%s", params.mTBatch / 1e6f, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 283 | isClientDisabledLocked(info.batchParams.keyAt(j)) ? "(disabled)" |
| 284 | : "", |
| 285 | (j < info.batchParams.size() - 1) ? ", " : ""); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 286 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 287 | result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 290 | return result.string(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 293 | /** |
| 294 | * Dump debugging information as android.service.SensorDeviceProto protobuf message using |
| 295 | * ProtoOutputStream. |
| 296 | * |
| 297 | * See proto definition and some notes about ProtoOutputStream in |
| 298 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 299 | */ |
| 300 | void SensorDevice::dump(ProtoOutputStream* proto) const { |
| 301 | using namespace service::SensorDeviceProto; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 302 | if (mHalWrapper == nullptr) { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 303 | proto->write(INITIALIZED, false); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 304 | return; |
| 305 | } |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 306 | proto->write(INITIALIZED, true); |
| 307 | proto->write(TOTAL_SENSORS, int(mSensorList.size())); |
| 308 | proto->write(ACTIVE_SENSORS, int(mActivationCount.size())); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 309 | |
| 310 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 311 | for (const auto& s : mSensorList) { |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 312 | int32_t handle = s.handle; |
| 313 | const Info& info = mActivationCount.valueFor(handle); |
| 314 | if (info.numActiveClients() == 0) continue; |
| 315 | |
| 316 | uint64_t token = proto->start(SENSORS); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 317 | proto->write(SensorProto::HANDLE, handle); |
| 318 | proto->write(SensorProto::ACTIVE_COUNT, int(info.batchParams.size())); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 319 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
| 320 | const BatchParams& params = info.batchParams[j]; |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 321 | proto->write(SensorProto::SAMPLING_PERIOD_MS, params.mTSample / 1e6f); |
| 322 | proto->write(SensorProto::BATCHING_PERIOD_MS, params.mTBatch / 1e6f); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 323 | } |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 324 | proto->write(SensorProto::SAMPLING_PERIOD_SELECTED, info.bestBatchParams.mTSample / 1e6f); |
| 325 | proto->write(SensorProto::BATCHING_PERIOD_SELECTED, info.bestBatchParams.mTBatch / 1e6f); |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 326 | proto->end(token); |
| 327 | } |
| 328 | } |
| 329 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 330 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 331 | *list = &mSensorList[0]; |
| 332 | |
| 333 | return mSensorList.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | status_t SensorDevice::initCheck() const { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 337 | return mHalWrapper != nullptr ? NO_ERROR : NO_INIT; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 341 | if (mHalWrapper == nullptr) return NO_INIT; |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 342 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 343 | ssize_t eventsRead = 0; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 344 | if (mHalWrapper->supportsMessageQueues()) { |
| 345 | eventsRead = mHalWrapper->pollFmq(buffer, count); |
| 346 | } else if (mHalWrapper->supportsPolling()) { |
| 347 | eventsRead = mHalWrapper->poll(buffer, count); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 348 | } else { |
| 349 | ALOGE("Must support polling or FMQ"); |
| 350 | eventsRead = -1; |
| 351 | } |
Arthur Ishiguro | 39e04c6 | 2021-12-26 18:40:16 +0000 | [diff] [blame^] | 352 | |
| 353 | if (eventsRead > 0) { |
| 354 | for (ssize_t i = 0; i < eventsRead; i++) { |
| 355 | float resolution = getResolutionForSensor(buffer[i].sensor); |
| 356 | android::SensorDeviceUtils::quantizeSensorEventValues(&buffer[i], resolution); |
| 357 | } |
| 358 | } |
| 359 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 360 | return eventsRead; |
| 361 | } |
| 362 | |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 363 | void SensorDevice::onDynamicSensorsConnected(const std::vector<sensor_t>& dynamicSensorsAdded) { |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame] | 364 | std::unique_lock<std::mutex> lock(mDynamicSensorsMutex); |
| 365 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 366 | // Allocate a sensor_t structure for each dynamic sensor added and insert |
| 367 | // it into the dictionary of connected dynamic sensors keyed by handle. |
| 368 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 369 | const sensor_t& sensor = dynamicSensorsAdded[i]; |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 370 | |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 371 | auto it = mConnectedDynamicSensors.find(sensor.handle); |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 372 | CHECK(it == mConnectedDynamicSensors.end()); |
| 373 | |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 374 | mConnectedDynamicSensors.insert(std::make_pair(sensor.handle, sensor)); |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame] | 377 | mDynamicSensorsCv.notify_all(); |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 380 | void SensorDevice::onDynamicSensorsDisconnected( |
| 381 | const std::vector<int32_t>& /* dynamicSensorHandlesRemoved */) { |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 382 | // TODO: Currently dynamic sensors do not seem to be removed |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 385 | void SensorDevice::writeWakeLockHandled(uint32_t count) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 386 | if (mHalWrapper != nullptr && mHalWrapper->supportsMessageQueues()) { |
| 387 | mHalWrapper->writeWakeLockHandled(count); |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 391 | void SensorDevice::autoDisable(void* ident, int handle) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 392 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 393 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 394 | if (activationIndex < 0) { |
| 395 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 396 | return; |
| 397 | } |
| 398 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 399 | info.removeBatchParamsForIdent(ident); |
Brian Stack | aa6dc09 | 2019-05-10 13:36:40 -0700 | [diff] [blame] | 400 | if (info.numActiveClients() == 0) { |
| 401 | info.isActive = false; |
| 402 | } |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 405 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 406 | if (mHalWrapper == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 407 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 408 | Mutex::Autolock _l(mLock); |
| 409 | return activateLocked(ident, handle, enabled); |
| 410 | } |
| 411 | |
| 412 | status_t SensorDevice::activateLocked(void* ident, int handle, int enabled) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 413 | bool activateHardware = false; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 414 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 415 | status_t err(NO_ERROR); |
| 416 | |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 417 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 418 | if (activationIndex < 0) { |
| 419 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 420 | return BAD_VALUE; |
| 421 | } |
| 422 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 423 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 424 | ALOGD_IF(DEBUG_CONNECTIONS, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 425 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", ident, |
| 426 | handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 427 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 428 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 429 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 430 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 431 | if (isClientDisabledLocked(ident)) { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 432 | ALOGW("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", ident, |
| 433 | handle); |
xiamengsen | 94ae131 | 2021-02-05 15:35:34 +0800 | [diff] [blame] | 434 | return NO_ERROR; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 437 | if (info.batchParams.indexOfKey(ident) >= 0) { |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 438 | if (info.numActiveClients() > 0 && !info.isActive) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 439 | activateHardware = true; |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 440 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 441 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 442 | // Log error. Every activate call should be preceded by a batch() call. |
| 443 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 444 | } |
| 445 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 446 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 447 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 448 | // If a connected dynamic sensor is deactivated, remove it from the |
| 449 | // dictionary. |
| 450 | auto it = mConnectedDynamicSensors.find(handle); |
| 451 | if (it != mConnectedDynamicSensors.end()) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 452 | mConnectedDynamicSensors.erase(it); |
| 453 | } |
| 454 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 455 | if (info.removeBatchParamsForIdent(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 456 | if (info.numActiveClients() == 0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 457 | // This is the last connection, we need to de-activate the underlying h/w sensor. |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 458 | activateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 459 | } else { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 460 | // Call batch for this sensor with the previously calculated best effort |
| 461 | // batch_rate and timeout. One of the apps has unregistered for sensor |
| 462 | // events, and the best effort batch parameters might have changed. |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 463 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, |
| 464 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 465 | mHalWrapper->batch(handle, info.bestBatchParams.mTSample, |
| 466 | info.bestBatchParams.mTBatch); |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 467 | } |
| 468 | } else { |
| 469 | // sensor wasn't enabled for this ident |
| 470 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 471 | |
| 472 | if (isClientDisabledLocked(ident)) { |
| 473 | return NO_ERROR; |
| 474 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 475 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 476 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 477 | if (activateHardware) { |
| 478 | err = doActivateHardwareLocked(handle, enabled); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 479 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 480 | if (err != NO_ERROR && enabled) { |
| 481 | // Failure when enabling the sensor. Clean up on failure. |
| 482 | info.removeBatchParamsForIdent(ident); |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 483 | } else { |
| 484 | // Update the isActive flag if there is no error. If there is an error when disabling a |
| 485 | // sensor, still set the flag to false since the batch parameters have already been |
| 486 | // removed. This ensures that everything remains in-sync. |
| 487 | info.isActive = enabled; |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 488 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 491 | return err; |
| 492 | } |
| 493 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 494 | status_t SensorDevice::doActivateHardwareLocked(int handle, bool enabled) { |
| 495 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, |
| 496 | enabled); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 497 | status_t err = mHalWrapper->activate(handle, enabled); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 498 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, |
| 499 | strerror(-err)); |
| 500 | return err; |
| 501 | } |
| 502 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 503 | status_t SensorDevice::batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 504 | int64_t maxBatchReportLatencyNs) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 505 | if (mHalWrapper == nullptr) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 506 | |
| 507 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 508 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 509 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 510 | if (maxBatchReportLatencyNs < 0) { |
| 511 | maxBatchReportLatencyNs = 0; |
| 512 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 513 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 514 | ALOGD_IF(DEBUG_CONNECTIONS, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 515 | "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 |
| 516 | " timeout=%" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 517 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 518 | |
| 519 | Mutex::Autolock _l(mLock); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 520 | return batchLocked(ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 521 | } |
| 522 | |
| 523 | status_t SensorDevice::batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 524 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 525 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 526 | if (activationIndex < 0) { |
| 527 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 528 | return BAD_VALUE; |
| 529 | } |
| 530 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 531 | |
| 532 | if (info.batchParams.indexOfKey(ident) < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 533 | BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 534 | info.batchParams.add(ident, params); |
| 535 | } else { |
| 536 | // A batch has already been called with this ident. Update the batch parameters. |
| 537 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 538 | } |
| 539 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 540 | status_t err = updateBatchParamsLocked(handle, info); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 541 | if (err != NO_ERROR) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 542 | ALOGE("sensor batch failed 0x%08x %" PRId64 " %" PRId64 " err=%s", handle, |
| 543 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch, strerror(-err)); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 544 | info.removeBatchParamsForIdent(ident); |
| 545 | } |
| 546 | |
| 547 | return err; |
| 548 | } |
| 549 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 550 | status_t SensorDevice::updateBatchParamsLocked(int handle, Info& info) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 551 | BatchParams prevBestBatchParams = info.bestBatchParams; |
| 552 | // Find the minimum of all timeouts and batch_rates for this sensor. |
| 553 | info.selectBatchParams(); |
| 554 | |
| 555 | ALOGD_IF(DEBUG_CONNECTIONS, |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 556 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 " curr_timeout=%" PRId64 |
| 557 | " min_timeout=%" PRId64, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 558 | prevBestBatchParams.mTSample, info.bestBatchParams.mTSample, |
| 559 | prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 560 | |
| 561 | status_t err(NO_ERROR); |
| 562 | // If the min period or min timeout has changed since the last batch call, call batch. |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 563 | if (prevBestBatchParams != info.bestBatchParams && info.numActiveClients() > 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 564 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle, |
| 565 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 566 | err = mHalWrapper->batch(handle, info.bestBatchParams.mTSample, |
| 567 | info.bestBatchParams.mTBatch); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 568 | } |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 569 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 570 | return err; |
| 571 | } |
| 572 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 573 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 574 | return batch(ident, handle, 0, samplingPeriodNs, 0); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 577 | int SensorDevice::getHalDeviceVersion() const { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 578 | if (mHalWrapper == nullptr) return -1; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 579 | return SENSORS_DEVICE_API_VERSION_1_4; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 582 | status_t SensorDevice::flush(void* /*ident*/, int handle) { |
| 583 | return mHalWrapper->flush(handle); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 586 | bool SensorDevice::isClientDisabled(void* ident) const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 587 | Mutex::Autolock _l(mLock); |
| 588 | return isClientDisabledLocked(ident); |
| 589 | } |
| 590 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 591 | bool SensorDevice::isClientDisabledLocked(void* ident) const { |
| 592 | return mDisabledClients.count(ident) > 0; |
| 593 | } |
| 594 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 595 | std::vector<void*> SensorDevice::getDisabledClientsLocked() const { |
| 596 | std::vector<void*> vec; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 597 | for (const auto& it : mDisabledClients) { |
| 598 | vec.push_back(it.first); |
| 599 | } |
| 600 | |
| 601 | return vec; |
| 602 | } |
| 603 | |
| 604 | void SensorDevice::addDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 605 | mDisabledClients[ident] |= 1 << reason; |
| 606 | } |
| 607 | |
| 608 | void SensorDevice::removeDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 609 | if (isClientDisabledLocked(ident)) { |
| 610 | mDisabledClients[ident] &= ~(1 << reason); |
| 611 | if (mDisabledClients[ident] == 0) { |
| 612 | mDisabledClients.erase(ident); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | void SensorDevice::setUidStateForConnection(void* ident, SensorService::UidState state) { |
| 618 | Mutex::Autolock _l(mLock); |
| 619 | if (state == SensorService::UID_STATE_ACTIVE) { |
| 620 | removeDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 621 | } else { |
| 622 | addDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 623 | } |
| 624 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 625 | for (size_t i = 0; i < mActivationCount.size(); ++i) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 626 | int handle = mActivationCount.keyAt(i); |
| 627 | Info& info = mActivationCount.editValueAt(i); |
| 628 | |
| 629 | if (info.hasBatchParamsForIdent(ident)) { |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 630 | updateBatchParamsLocked(handle, info); |
| 631 | bool disable = info.numActiveClients() == 0 && info.isActive; |
| 632 | bool enable = info.numActiveClients() > 0 && !info.isActive; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 633 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 634 | if ((enable || disable) && doActivateHardwareLocked(handle, enable) == NO_ERROR) { |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 635 | info.isActive = enable; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 641 | bool SensorDevice::isSensorActive(int handle) const { |
| 642 | Mutex::Autolock _l(mLock); |
| 643 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 644 | if (activationIndex < 0) { |
| 645 | return false; |
| 646 | } |
Chris Kuiper | c63880a | 2021-08-04 15:06:11 -0700 | [diff] [blame] | 647 | return mActivationCount.valueAt(activationIndex).isActive; |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 650 | void SensorDevice::onMicSensorAccessChanged(void* ident, int handle, nsecs_t samplingPeriodNs) { |
| 651 | Mutex::Autolock _l(mLock); |
| 652 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 653 | if (activationIndex < 0) { |
| 654 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 655 | return; |
| 656 | } |
| 657 | Info& info(mActivationCount.editValueAt(activationIndex)); |
| 658 | if (info.hasBatchParamsForIdent(ident)) { |
| 659 | ssize_t index = info.batchParams.indexOfKey(ident); |
| 660 | BatchParams& params = info.batchParams.editValueAt(index); |
| 661 | params.mTSample = samplingPeriodNs; |
| 662 | } |
| 663 | } |
| 664 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 665 | void SensorDevice::enableAllSensors() { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 666 | if (mHalWrapper == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 667 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 668 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 669 | for (void* client : getDisabledClientsLocked()) { |
| 670 | removeDisabledReasonForIdentLocked(client, |
| 671 | DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 674 | for (size_t i = 0; i < mActivationCount.size(); ++i) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 675 | Info& info = mActivationCount.editValueAt(i); |
| 676 | if (info.batchParams.isEmpty()) continue; |
| 677 | info.selectBatchParams(); |
| 678 | const int sensor_handle = mActivationCount.keyAt(i); |
| 679 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 680 | sensor_handle); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 681 | status_t err = mHalWrapper->batch(sensor_handle, info.bestBatchParams.mTSample, |
| 682 | info.bestBatchParams.mTBatch); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 683 | 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] | 684 | |
| 685 | if (err == NO_ERROR) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 686 | err = mHalWrapper->activate(sensor_handle, 1 /* enabled */); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 687 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); |
| 688 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 689 | |
| 690 | if (err == NO_ERROR) { |
| 691 | info.isActive = true; |
| 692 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | void SensorDevice::disableAllSensors() { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 697 | if (mHalWrapper == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 698 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 699 | for (size_t i = 0; i < mActivationCount.size(); ++i) { |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 700 | Info& info = mActivationCount.editValueAt(i); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 701 | // Check if this sensor has been activated previously and disable it. |
| 702 | if (info.batchParams.size() > 0) { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 703 | const int sensor_handle = mActivationCount.keyAt(i); |
| 704 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", |
| 705 | sensor_handle); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 706 | mHalWrapper->activate(sensor_handle, 0 /* enabled */); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 707 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 708 | // Add all the connections that were registered for this sensor to the disabled |
| 709 | // clients list. |
| 710 | for (size_t j = 0; j < info.batchParams.size(); ++j) { |
| 711 | addDisabledReasonForIdentLocked(info.batchParams.keyAt(j), |
| 712 | DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
| 713 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); |
| 714 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 715 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 716 | info.isActive = false; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 721 | status_t SensorDevice::injectSensorData(const sensors_event_t* injected_sensor_event) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 722 | return mHalWrapper->injectSensorData(injected_sensor_event); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | status_t SensorDevice::setMode(uint32_t mode) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 726 | if (mHalWrapper == nullptr) return NO_INIT; |
| 727 | return mHalWrapper->setOperationMode(static_cast<SensorService::Mode>(mode)); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 728 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 729 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 730 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 731 | Mutex::Autolock _l(mLock); |
| 732 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 733 | return mHalWrapper->registerDirectChannel(memory, nullptr); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 737 | mHalWrapper->unregisterDirectChannel(channelHandle); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 740 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, int32_t channelHandle, |
| 741 | const struct sensors_direct_cfg_t* config) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 742 | Mutex::Autolock _l(mLock); |
| 743 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 744 | return mHalWrapper->configureDirectChannel(sensorHandle, channelHandle, config); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 747 | // --------------------------------------------------------------------------- |
| 748 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 749 | int SensorDevice::Info::numActiveClients() const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 750 | SensorDevice& device(SensorDevice::getInstance()); |
| 751 | int num = 0; |
| 752 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 753 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 754 | ++num; |
| 755 | } |
| 756 | } |
| 757 | return num; |
| 758 | } |
| 759 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 760 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int, int64_t samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 761 | int64_t maxBatchReportLatencyNs) { |
| 762 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 763 | if (index < 0) { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 764 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 " timeout=%" PRId64 |
| 765 | ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 766 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 767 | return BAD_INDEX; |
| 768 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 769 | BatchParams& params = batchParams.editValueAt(index); |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 770 | params.mTSample = samplingPeriodNs; |
| 771 | params.mTBatch = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 772 | return NO_ERROR; |
| 773 | } |
| 774 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 775 | void SensorDevice::Info::selectBatchParams() { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 776 | BatchParams bestParams; // default to max Tsample and max Tbatch |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 777 | SensorDevice& device(SensorDevice::getInstance()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 778 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 779 | for (size_t i = 0; i < batchParams.size(); ++i) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 780 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 781 | continue; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 782 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 783 | bestParams.merge(batchParams[i]); |
| 784 | } |
| 785 | // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly. |
| 786 | if (bestParams.mTBatch <= bestParams.mTSample) { |
| 787 | bestParams.mTBatch = 0; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 788 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 789 | bestBatchParams = bestParams; |
| 790 | } |
| 791 | |
| 792 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 793 | ssize_t idx = batchParams.removeItem(ident); |
| 794 | if (idx >= 0) { |
| 795 | selectBatchParams(); |
| 796 | } |
| 797 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 800 | void SensorDevice::notifyConnectionDestroyed(void* ident) { |
| 801 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 802 | mDisabledClients.erase(ident); |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 805 | bool SensorDevice::isDirectReportSupported() const { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 806 | return mIsDirectReportSupported; |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 807 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 808 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 809 | float SensorDevice::getResolutionForSensor(int sensorHandle) { |
| 810 | for (size_t i = 0; i < mSensorList.size(); i++) { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 811 | if (sensorHandle == mSensorList[i].handle) { |
| 812 | return mSensorList[i].resolution; |
| 813 | } |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | auto it = mConnectedDynamicSensors.find(sensorHandle); |
| 817 | if (it != mConnectedDynamicSensors.end()) { |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 818 | return it->second.resolution; |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 824 | // --------------------------------------------------------------------------- |
| 825 | }; // namespace android |