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