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