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