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