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/ISensorsCallback.h" |
| 21 | #include "android/hardware/sensors/2.1/types.h" |
| 22 | #include "convertV2_1.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 23 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 25 | #include <android/util/ProtoOutputStream.h> |
| 26 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 27 | #include <sensors/convert.h> |
Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 28 | #include <cutils/atomic.h> |
Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 29 | #include <utils/Errors.h> |
| 30 | #include <utils/Singleton.h> |
Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 31 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 32 | #include <cstddef> |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <cinttypes> |
| 35 | #include <thread> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 36 | |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 37 | using namespace android::hardware::sensors; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 38 | using namespace android::hardware::sensors::V1_0; |
| 39 | using namespace android::hardware::sensors::V1_0::implementation; |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 40 | using android::hardware::sensors::V2_0::EventQueueFlagBits; |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 41 | using android::hardware::sensors::V2_0::WakeLockQueueFlagBits; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 42 | using android::hardware::sensors::V2_1::ISensorsCallback; |
| 43 | using android::hardware::sensors::V2_1::implementation::convertToOldSensorInfo; |
| 44 | using android::hardware::sensors::V2_1::implementation::convertToNewSensorInfos; |
| 45 | using android::hardware::sensors::V2_1::implementation::convertToNewEvents; |
| 46 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV1_0; |
| 47 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_0; |
| 48 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_1; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 49 | using android::hardware::hidl_vec; |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 50 | using android::hardware::Return; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 51 | using android::SensorDeviceUtils::HidlServiceRegistrationWaiter; |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 52 | using android::util::ProtoOutputStream; |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 53 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 54 | namespace android { |
| 55 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 56 | |
| 57 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) |
| 58 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | status_t statusFromResult(Result result) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 62 | switch (result) { |
| 63 | case Result::OK: |
| 64 | return OK; |
| 65 | case Result::BAD_VALUE: |
| 66 | return BAD_VALUE; |
| 67 | case Result::PERMISSION_DENIED: |
| 68 | return PERMISSION_DENIED; |
| 69 | case Result::INVALID_OPERATION: |
| 70 | return INVALID_OPERATION; |
| 71 | case Result::NO_MEMORY: |
| 72 | return NO_MEMORY; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 76 | template<typename EnumType> |
| 77 | constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) { |
| 78 | return static_cast<typename std::underlying_type<EnumType>::type>(value); |
| 79 | } |
| 80 | |
| 81 | // Used internally by the framework to wake the Event FMQ. These values must start after |
| 82 | // the last value of EventQueueFlagBits |
| 83 | enum EventQueueFlagBitsInternal : uint32_t { |
| 84 | INTERNAL_WAKE = 1 << 16, |
| 85 | }; |
| 86 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 87 | } // anonymous namespace |
| 88 | |
Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 89 | void SensorsHalDeathReceivier::serviceDied( |
| 90 | uint64_t /* cookie */, |
| 91 | const wp<::android::hidl::base::V1_0::IBase>& /* service */) { |
| 92 | ALOGW("Sensors HAL died, attempting to reconnect."); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 93 | SensorDevice::getInstance().prepareForReconnect(); |
Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 96 | struct SensorsCallback : public ISensorsCallback { |
| 97 | using Result = ::android::hardware::sensors::V1_0::Result; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 98 | using SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo; |
| 99 | |
| 100 | Return<void> onDynamicSensorsConnected_2_1( |
Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 101 | const hidl_vec<SensorInfo> &dynamicSensorsAdded) override { |
| 102 | return SensorDevice::getInstance().onDynamicSensorsConnected(dynamicSensorsAdded); |
| 103 | } |
| 104 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 105 | Return<void> onDynamicSensorsConnected( |
| 106 | const hidl_vec<V1_0::SensorInfo> &dynamicSensorsAdded) override { |
| 107 | return SensorDevice::getInstance().onDynamicSensorsConnected( |
| 108 | convertToNewSensorInfos(dynamicSensorsAdded)); |
| 109 | } |
| 110 | |
Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 111 | Return<void> onDynamicSensorsDisconnected( |
| 112 | const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override { |
| 113 | return SensorDevice::getInstance().onDynamicSensorsDisconnected( |
| 114 | dynamicSensorHandlesRemoved); |
| 115 | } |
| 116 | }; |
| 117 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 118 | SensorDevice::SensorDevice() |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 119 | : mHidlTransportErrors(20), |
| 120 | mRestartWaiter(new HidlServiceRegistrationWaiter()), |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 121 | mEventQueueFlag(nullptr), |
| 122 | mWakeLockQueueFlag(nullptr), |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 123 | mReconnecting(false) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 124 | if (!connectHidlService()) { |
| 125 | return; |
| 126 | } |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 127 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 128 | initializeSensorList(); |
| 129 | |
| 130 | mIsDirectReportSupported = |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 131 | (checkReturnAndGetStatus(mSensors->unregisterDirectChannel(-1)) != INVALID_OPERATION); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void SensorDevice::initializeSensorList() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 135 | checkReturn(mSensors->getSensorsList( |
| 136 | [&](const auto &list) { |
| 137 | const size_t count = list.size(); |
| 138 | |
| 139 | mActivationCount.setCapacity(count); |
| 140 | Info model; |
| 141 | for (size_t i=0 ; i < count; i++) { |
| 142 | sensor_t sensor; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 143 | convertToSensor(convertToOldSensorInfo(list[i]), &sensor); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 144 | |
| 145 | if (sensor.type < static_cast<int>(SensorType::DEVICE_PRIVATE_BASE)) { |
Anthony Stange | 5945eb3 | 2020-08-06 12:45:16 -0400 | [diff] [blame] | 146 | sensor.resolution = SensorDeviceUtils::resolutionForSensor(sensor); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 147 | |
Anthony Stange | ec5b4b1 | 2020-07-09 09:35:29 -0400 | [diff] [blame] | 148 | // Some sensors don't have a default resolution and will be left at 0. |
| 149 | // Don't crash in this case since CTS will verify that devices don't go to |
| 150 | // production with a resolution of 0. |
| 151 | if (sensor.resolution != 0) { |
Brian Duddie | 3dd8502 | 2021-04-27 17:18:00 -0700 | [diff] [blame] | 152 | float quantizedRange = sensor.maxRange; |
| 153 | SensorDeviceUtils::quantizeValue( |
| 154 | &quantizedRange, sensor.resolution, /*factor=*/ 1); |
| 155 | // Only rewrite maxRange if the requantization produced a "significant" |
| 156 | // change, which is fairly arbitrarily defined as resolution / 8. |
| 157 | // Smaller deltas are permitted, as they may simply be due to floating |
| 158 | // point representation error, etc. |
| 159 | if (fabsf(sensor.maxRange - quantizedRange) > sensor.resolution / 8) { |
| 160 | ALOGW("%s's max range %.12f is not a multiple of the resolution " |
| 161 | "%.12f - updated to %.12f", sensor.name, sensor.maxRange, |
| 162 | sensor.resolution, quantizedRange); |
| 163 | sensor.maxRange = quantizedRange; |
Anthony Stange | ec5b4b1 | 2020-07-09 09:35:29 -0400 | [diff] [blame] | 164 | } |
Anthony Stange | 5945eb3 | 2020-08-06 12:45:16 -0400 | [diff] [blame] | 165 | } else { |
| 166 | // Don't crash here or the device will go into a crashloop. |
| 167 | ALOGW("%s should have a non-zero resolution", sensor.name); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 171 | // Sanity check and clamp power if it is 0 (or close) |
Brian Duddie | 3dd8502 | 2021-04-27 17:18:00 -0700 | [diff] [blame] | 172 | constexpr float MIN_POWER_MA = 0.001; // 1 microAmp |
| 173 | if (sensor.power < MIN_POWER_MA) { |
| 174 | ALOGI("%s's reported power %f invalid, clamped to %f", |
| 175 | sensor.name, sensor.power, MIN_POWER_MA); |
| 176 | sensor.power = MIN_POWER_MA; |
Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 177 | } |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 178 | mSensorList.push_back(sensor); |
| 179 | |
| 180 | mActivationCount.add(list[i].sensorHandle, model); |
| 181 | |
Stan Rokita | 7c9d895 | 2019-08-01 14:24:25 -0700 | [diff] [blame] | 182 | // Only disable all sensors on HAL 1.0 since HAL 2.0 |
| 183 | // handles this in its initialize method |
| 184 | if (!mSensors->supportsMessageQueues()) { |
| 185 | checkReturn(mSensors->activate(list[i].sensorHandle, |
| 186 | 0 /* enabled */)); |
| 187 | } |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 188 | } |
| 189 | })); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 192 | SensorDevice::~SensorDevice() { |
| 193 | if (mEventQueueFlag != nullptr) { |
| 194 | hardware::EventFlag::deleteEventFlag(&mEventQueueFlag); |
| 195 | mEventQueueFlag = nullptr; |
| 196 | } |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 197 | |
| 198 | if (mWakeLockQueueFlag != nullptr) { |
| 199 | hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag); |
| 200 | mWakeLockQueueFlag = nullptr; |
| 201 | } |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 204 | bool SensorDevice::connectHidlService() { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 205 | HalConnectionStatus status = connectHidlServiceV2_1(); |
| 206 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { |
| 207 | status = connectHidlServiceV2_0(); |
| 208 | } |
| 209 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 210 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { |
| 211 | status = connectHidlServiceV1_0(); |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 212 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 213 | return (status == HalConnectionStatus::CONNECTED); |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 216 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV1_0() { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 217 | // SensorDevice will wait for HAL service to start if HAL is declared in device manifest. |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 218 | size_t retry = 10; |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 219 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 220 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 221 | while (retry-- > 0) { |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 222 | sp<V1_0::ISensors> sensors = V1_0::ISensors::getService(); |
| 223 | if (sensors == nullptr) { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 224 | // no sensor hidl service found |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 225 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 226 | break; |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 227 | } |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 228 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 229 | mSensors = new ISensorsWrapperV1_0(sensors); |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 230 | mRestartWaiter->reset(); |
| 231 | // Poke ISensor service. If it has lingering connection from previous generation of |
| 232 | // system server, it will kill itself. There is no intention to handle the poll result, |
| 233 | // which will be done since the size is 0. |
| 234 | if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) { |
| 235 | // ok to continue |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 236 | connectionStatus = HalConnectionStatus::CONNECTED; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | |
| 240 | // hidl service is restarting, pointer is invalid. |
| 241 | mSensors = nullptr; |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 242 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 243 | ALOGI("%s unsuccessful, remaining retry %zu.", __FUNCTION__, retry); |
| 244 | mRestartWaiter->wait(); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 245 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 246 | |
| 247 | return connectionStatus; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 250 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() { |
| 251 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 252 | sp<V2_0::ISensors> sensors = V2_0::ISensors::getService(); |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 253 | |
| 254 | if (sensors == nullptr) { |
| 255 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; |
| 256 | } else { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 257 | mSensors = new ISensorsWrapperV2_0(sensors); |
| 258 | connectionStatus = initializeHidlServiceV2_X(); |
| 259 | } |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 260 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 261 | return connectionStatus; |
| 262 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 263 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 264 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_1() { |
| 265 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
| 266 | sp<V2_1::ISensors> sensors = V2_1::ISensors::getService(); |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 267 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 268 | if (sensors == nullptr) { |
| 269 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; |
| 270 | } else { |
| 271 | mSensors = new ISensorsWrapperV2_1(sensors); |
| 272 | connectionStatus = initializeHidlServiceV2_X(); |
| 273 | } |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 274 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 275 | return connectionStatus; |
| 276 | } |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 277 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 278 | SensorDevice::HalConnectionStatus SensorDevice::initializeHidlServiceV2_X() { |
| 279 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 280 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 281 | mWakeLockQueue = std::make_unique<WakeLockQueue>( |
| 282 | SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, |
| 283 | true /* configureEventFlagWord */); |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 284 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 285 | hardware::EventFlag::deleteEventFlag(&mEventQueueFlag); |
| 286 | hardware::EventFlag::createEventFlag(mSensors->getEventQueue()->getEventFlagWord(), &mEventQueueFlag); |
| 287 | |
| 288 | hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag); |
| 289 | hardware::EventFlag::createEventFlag(mWakeLockQueue->getEventFlagWord(), |
| 290 | &mWakeLockQueueFlag); |
| 291 | |
| 292 | CHECK(mSensors != nullptr && mWakeLockQueue != nullptr && |
| 293 | mEventQueueFlag != nullptr && mWakeLockQueueFlag != nullptr); |
| 294 | |
| 295 | status_t status = checkReturnAndGetStatus(mSensors->initialize( |
| 296 | *mWakeLockQueue->getDesc(), |
| 297 | new SensorsCallback())); |
| 298 | |
| 299 | if (status != NO_ERROR) { |
| 300 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; |
| 301 | ALOGE("Failed to initialize Sensors HAL (%s)", strerror(-status)); |
| 302 | } else { |
| 303 | connectionStatus = HalConnectionStatus::CONNECTED; |
| 304 | mSensorsHalDeathReceiver = new SensorsHalDeathReceivier(); |
| 305 | mSensors->linkToDeath(mSensorsHalDeathReceiver, 0 /* cookie */); |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 306 | } |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 307 | |
| 308 | return connectionStatus; |
Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 311 | void SensorDevice::prepareForReconnect() { |
| 312 | mReconnecting = true; |
| 313 | |
| 314 | // Wake up the polling thread so it returns and allows the SensorService to initiate |
| 315 | // a reconnect. |
| 316 | mEventQueueFlag->wake(asBaseType(INTERNAL_WAKE)); |
| 317 | } |
| 318 | |
| 319 | void SensorDevice::reconnect() { |
| 320 | Mutex::Autolock _l(mLock); |
| 321 | mSensors = nullptr; |
| 322 | |
| 323 | auto previousActivations = mActivationCount; |
| 324 | auto previousSensorList = mSensorList; |
| 325 | |
| 326 | mActivationCount.clear(); |
| 327 | mSensorList.clear(); |
| 328 | |
| 329 | if (connectHidlServiceV2_0() == HalConnectionStatus::CONNECTED) { |
| 330 | initializeSensorList(); |
| 331 | |
| 332 | if (sensorHandlesChanged(previousSensorList, mSensorList)) { |
| 333 | LOG_ALWAYS_FATAL("Sensor handles changed, cannot re-enable sensors."); |
| 334 | } else { |
| 335 | reactivateSensors(previousActivations); |
| 336 | } |
| 337 | } |
| 338 | mReconnecting = false; |
| 339 | } |
| 340 | |
| 341 | bool SensorDevice::sensorHandlesChanged(const Vector<sensor_t>& oldSensorList, |
| 342 | const Vector<sensor_t>& newSensorList) { |
| 343 | bool didChange = false; |
| 344 | |
| 345 | if (oldSensorList.size() != newSensorList.size()) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 346 | ALOGI("Sensor list size changed from %zu to %zu", oldSensorList.size(), |
| 347 | newSensorList.size()); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 348 | didChange = true; |
| 349 | } |
| 350 | |
| 351 | for (size_t i = 0; i < newSensorList.size() && !didChange; i++) { |
| 352 | bool found = false; |
| 353 | const sensor_t& newSensor = newSensorList[i]; |
| 354 | for (size_t j = 0; j < oldSensorList.size() && !found; j++) { |
| 355 | const sensor_t& prevSensor = oldSensorList[j]; |
| 356 | if (prevSensor.handle == newSensor.handle) { |
| 357 | found = true; |
| 358 | if (!sensorIsEquivalent(prevSensor, newSensor)) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 359 | ALOGI("Sensor %s not equivalent to previous version", newSensor.name); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 360 | didChange = true; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (!found) { |
| 366 | // Could not find the new sensor in the old list of sensors, the lists must |
| 367 | // have changed. |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 368 | 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] | 369 | didChange = true; |
| 370 | } |
| 371 | } |
| 372 | return didChange; |
| 373 | } |
| 374 | |
| 375 | bool SensorDevice::sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor) { |
| 376 | bool equivalent = true; |
| 377 | if (prevSensor.handle != newSensor.handle || |
| 378 | (strcmp(prevSensor.vendor, newSensor.vendor) != 0) || |
| 379 | (strcmp(prevSensor.stringType, newSensor.stringType) != 0) || |
| 380 | (strcmp(prevSensor.requiredPermission, newSensor.requiredPermission) != 0) || |
| 381 | (prevSensor.version != newSensor.version) || |
| 382 | (prevSensor.type != newSensor.type) || |
| 383 | (std::abs(prevSensor.maxRange - newSensor.maxRange) > 0.001f) || |
| 384 | (std::abs(prevSensor.resolution - newSensor.resolution) > 0.001f) || |
| 385 | (std::abs(prevSensor.power - newSensor.power) > 0.001f) || |
| 386 | (prevSensor.minDelay != newSensor.minDelay) || |
| 387 | (prevSensor.fifoReservedEventCount != newSensor.fifoReservedEventCount) || |
| 388 | (prevSensor.fifoMaxEventCount != newSensor.fifoMaxEventCount) || |
| 389 | (prevSensor.maxDelay != newSensor.maxDelay) || |
| 390 | (prevSensor.flags != newSensor.flags)) { |
| 391 | equivalent = false; |
| 392 | } |
| 393 | return equivalent; |
| 394 | } |
| 395 | |
| 396 | void SensorDevice::reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations) { |
| 397 | for (size_t i = 0; i < mSensorList.size(); i++) { |
| 398 | int handle = mSensorList[i].handle; |
| 399 | ssize_t activationIndex = previousActivations.indexOfKey(handle); |
| 400 | if (activationIndex < 0 || previousActivations[activationIndex].numActiveClients() <= 0) { |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | const Info& info = previousActivations[activationIndex]; |
| 405 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
| 406 | const BatchParams& batchParams = info.batchParams[j]; |
| 407 | status_t res = batchLocked(info.batchParams.keyAt(j), handle, 0 /* flags */, |
| 408 | batchParams.mTSample, batchParams.mTBatch); |
| 409 | |
| 410 | if (res == NO_ERROR) { |
| 411 | activateLocked(info.batchParams.keyAt(j), handle, true /* enabled */); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 417 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 418 | // 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] | 419 | if (connected) { |
| 420 | Info model; |
| 421 | mActivationCount.add(handle, model); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 422 | checkReturn(mSensors->activate(handle, 0 /* enabled */)); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 423 | } else { |
| 424 | mActivationCount.removeItem(handle); |
| 425 | } |
| 426 | } |
| 427 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 428 | std::string SensorDevice::dump() const { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 429 | if (mSensors == nullptr) return "HAL not initialized\n"; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 430 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 431 | String8 result; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 432 | result.appendFormat("Total %zu h/w sensors, %zu running %zu disabled clients:\n", |
| 433 | mSensorList.size(), mActivationCount.size(), mDisabledClients.size()); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 434 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 435 | Mutex::Autolock _l(mLock); |
| 436 | for (const auto & s : mSensorList) { |
| 437 | int32_t handle = s.handle; |
| 438 | const Info& info = mActivationCount.valueFor(handle); |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 439 | if (info.numActiveClients() == 0) continue; |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 440 | |
| 441 | result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size()); |
| 442 | |
| 443 | result.append("sampling_period(ms) = {"); |
| 444 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 445 | const BatchParams& params = info.batchParams[j]; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 446 | result.appendFormat("%.1f%s%s", params.mTSample / 1e6f, |
| 447 | isClientDisabledLocked(info.batchParams.keyAt(j)) ? "(disabled)" : "", |
| 448 | (j < info.batchParams.size() - 1) ? ", " : ""); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 449 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 450 | result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 451 | |
| 452 | result.append("batching_period(ms) = {"); |
| 453 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 454 | const BatchParams& params = info.batchParams[j]; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 455 | result.appendFormat("%.1f%s%s", params.mTBatch / 1e6f, |
| 456 | isClientDisabledLocked(info.batchParams.keyAt(j)) ? "(disabled)" : "", |
| 457 | (j < info.batchParams.size() - 1) ? ", " : ""); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 458 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 459 | result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 462 | return result.string(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 465 | /** |
| 466 | * Dump debugging information as android.service.SensorDeviceProto protobuf message using |
| 467 | * ProtoOutputStream. |
| 468 | * |
| 469 | * See proto definition and some notes about ProtoOutputStream in |
| 470 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 471 | */ |
| 472 | void SensorDevice::dump(ProtoOutputStream* proto) const { |
| 473 | using namespace service::SensorDeviceProto; |
| 474 | if (mSensors == nullptr) { |
| 475 | proto->write(INITIALIZED , false); |
| 476 | return; |
| 477 | } |
| 478 | proto->write(INITIALIZED , true); |
| 479 | proto->write(TOTAL_SENSORS , int(mSensorList.size())); |
| 480 | proto->write(ACTIVE_SENSORS , int(mActivationCount.size())); |
| 481 | |
| 482 | Mutex::Autolock _l(mLock); |
| 483 | for (const auto & s : mSensorList) { |
| 484 | int32_t handle = s.handle; |
| 485 | const Info& info = mActivationCount.valueFor(handle); |
| 486 | if (info.numActiveClients() == 0) continue; |
| 487 | |
| 488 | uint64_t token = proto->start(SENSORS); |
| 489 | proto->write(SensorProto::HANDLE , handle); |
| 490 | proto->write(SensorProto::ACTIVE_COUNT , int(info.batchParams.size())); |
| 491 | for (size_t j = 0; j < info.batchParams.size(); j++) { |
| 492 | const BatchParams& params = info.batchParams[j]; |
| 493 | proto->write(SensorProto::SAMPLING_PERIOD_MS , params.mTSample / 1e6f); |
| 494 | proto->write(SensorProto::BATCHING_PERIOD_MS , params.mTBatch / 1e6f); |
| 495 | } |
| 496 | proto->write(SensorProto::SAMPLING_PERIOD_SELECTED , info.bestBatchParams.mTSample / 1e6f); |
| 497 | proto->write(SensorProto::BATCHING_PERIOD_SELECTED , info.bestBatchParams.mTBatch / 1e6f); |
| 498 | proto->end(token); |
| 499 | } |
| 500 | } |
| 501 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 502 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 503 | *list = &mSensorList[0]; |
| 504 | |
| 505 | return mSensorList.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | status_t SensorDevice::initCheck() const { |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 509 | return mSensors != nullptr ? NO_ERROR : NO_INIT; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 513 | if (mSensors == nullptr) return NO_INIT; |
| 514 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 515 | ssize_t eventsRead = 0; |
| 516 | if (mSensors->supportsMessageQueues()) { |
| 517 | eventsRead = pollFmq(buffer, count); |
| 518 | } else if (mSensors->supportsPolling()) { |
| 519 | eventsRead = pollHal(buffer, count); |
| 520 | } else { |
| 521 | ALOGE("Must support polling or FMQ"); |
| 522 | eventsRead = -1; |
| 523 | } |
| 524 | return eventsRead; |
| 525 | } |
| 526 | |
| 527 | ssize_t SensorDevice::pollHal(sensors_event_t* buffer, size_t count) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 528 | ssize_t err; |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 529 | int numHidlTransportErrors = 0; |
| 530 | bool hidlTransportError = false; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 531 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 532 | do { |
| 533 | auto ret = mSensors->poll( |
| 534 | count, |
| 535 | [&](auto result, |
| 536 | const auto &events, |
| 537 | const auto &dynamicSensorsAdded) { |
| 538 | if (result == Result::OK) { |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 539 | convertToSensorEventsAndQuantize(convertToNewEvents(events), |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 540 | convertToNewSensorInfos(dynamicSensorsAdded), buffer); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 541 | err = (ssize_t)events.size(); |
| 542 | } else { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 543 | err = statusFromResult(result); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 544 | } |
| 545 | }); |
| 546 | |
| 547 | if (ret.isOk()) { |
| 548 | hidlTransportError = false; |
| 549 | } else { |
| 550 | hidlTransportError = true; |
| 551 | numHidlTransportErrors++; |
| 552 | if (numHidlTransportErrors > 50) { |
| 553 | // Log error and bail |
| 554 | ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors); |
| 555 | handleHidlDeath(ret.description()); |
| 556 | } else { |
| 557 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 558 | } |
| 559 | } |
| 560 | } while (hidlTransportError); |
| 561 | |
| 562 | if(numHidlTransportErrors > 0) { |
| 563 | ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors); |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 564 | HidlTransportErrorLog errLog(time(nullptr), numHidlTransportErrors); |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 565 | mHidlTransportErrors.add(errLog); |
| 566 | mTotalHidlTransportErrors++; |
| 567 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 568 | |
| 569 | return err; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 572 | ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead) { |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 573 | ssize_t eventsRead = 0; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 574 | size_t availableEvents = mSensors->getEventQueue()->availableToRead(); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 575 | |
| 576 | if (availableEvents == 0) { |
| 577 | uint32_t eventFlagState = 0; |
| 578 | |
| 579 | // Wait for events to become available. This is necessary so that the Event FMQ's read() is |
| 580 | // able to be called with the correct number of events to read. If the specified number of |
| 581 | // events is not available, then read() would return no events, possibly introducing |
| 582 | // additional latency in delivering events to applications. |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 583 | mEventQueueFlag->wait(asBaseType(EventQueueFlagBits::READ_AND_PROCESS) | |
| 584 | asBaseType(INTERNAL_WAKE), &eventFlagState); |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 585 | availableEvents = mSensors->getEventQueue()->availableToRead(); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 586 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 587 | if ((eventFlagState & asBaseType(INTERNAL_WAKE)) && mReconnecting) { |
| 588 | ALOGD("Event FMQ internal wake, returning from poll with no events"); |
| 589 | return DEAD_OBJECT; |
| 590 | } |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | size_t eventsToRead = std::min({availableEvents, maxNumEventsToRead, mEventBuffer.size()}); |
| 594 | if (eventsToRead > 0) { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 595 | if (mSensors->getEventQueue()->read(mEventBuffer.data(), eventsToRead)) { |
Brian Stack | af1b54c | 2018-11-09 10:20:01 -0800 | [diff] [blame] | 596 | // Notify the Sensors HAL that sensor events have been read. This is required to support |
| 597 | // the use of writeBlocking by the Sensors HAL. |
| 598 | mEventQueueFlag->wake(asBaseType(EventQueueFlagBits::EVENTS_READ)); |
| 599 | |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 600 | for (size_t i = 0; i < eventsToRead; i++) { |
| 601 | convertToSensorEvent(mEventBuffer[i], &buffer[i]); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 602 | android::SensorDeviceUtils::quantizeSensorEventValues(&buffer[i], |
| 603 | getResolutionForSensor(buffer[i].sensor)); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 604 | } |
| 605 | eventsRead = eventsToRead; |
| 606 | } else { |
| 607 | ALOGW("Failed to read %zu events, currently %zu events available", |
| 608 | eventsToRead, availableEvents); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | return eventsRead; |
| 613 | } |
| 614 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 615 | Return<void> SensorDevice::onDynamicSensorsConnected( |
| 616 | const hidl_vec<SensorInfo> &dynamicSensorsAdded) { |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame^] | 617 | std::unique_lock<std::mutex> lock(mDynamicSensorsMutex); |
| 618 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 619 | // Allocate a sensor_t structure for each dynamic sensor added and insert |
| 620 | // it into the dictionary of connected dynamic sensors keyed by handle. |
| 621 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { |
| 622 | const SensorInfo &info = dynamicSensorsAdded[i]; |
| 623 | |
| 624 | auto it = mConnectedDynamicSensors.find(info.sensorHandle); |
| 625 | CHECK(it == mConnectedDynamicSensors.end()); |
| 626 | |
| 627 | sensor_t *sensor = new sensor_t(); |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 628 | convertToSensor(convertToOldSensorInfo(info), sensor); |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 629 | |
| 630 | mConnectedDynamicSensors.insert( |
| 631 | std::make_pair(sensor->handle, sensor)); |
| 632 | } |
| 633 | |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame^] | 634 | mDynamicSensorsCv.notify_all(); |
| 635 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 636 | return Return<void>(); |
| 637 | } |
| 638 | |
| 639 | Return<void> SensorDevice::onDynamicSensorsDisconnected( |
| 640 | const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) { |
| 641 | (void) dynamicSensorHandlesRemoved; |
| 642 | // TODO: Currently dynamic sensors do not seem to be removed |
| 643 | return Return<void>(); |
| 644 | } |
| 645 | |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 646 | void SensorDevice::writeWakeLockHandled(uint32_t count) { |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 647 | if (mSensors != nullptr && mSensors->supportsMessageQueues()) { |
| 648 | if (mWakeLockQueue->write(&count)) { |
| 649 | mWakeLockQueueFlag->wake(asBaseType(WakeLockQueueFlagBits::DATA_WRITTEN)); |
| 650 | } else { |
| 651 | ALOGW("Failed to write wake lock handled"); |
| 652 | } |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 656 | void SensorDevice::autoDisable(void *ident, int handle) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 657 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 658 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 659 | if (activationIndex < 0) { |
| 660 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 661 | return; |
| 662 | } |
| 663 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 664 | info.removeBatchParamsForIdent(ident); |
Brian Stack | aa6dc09 | 2019-05-10 13:36:40 -0700 | [diff] [blame] | 665 | if (info.numActiveClients() == 0) { |
| 666 | info.isActive = false; |
| 667 | } |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 670 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 671 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 672 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 673 | Mutex::Autolock _l(mLock); |
| 674 | return activateLocked(ident, handle, enabled); |
| 675 | } |
| 676 | |
| 677 | status_t SensorDevice::activateLocked(void* ident, int handle, int enabled) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 678 | bool activateHardware = false; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 679 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 680 | status_t err(NO_ERROR); |
| 681 | |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 682 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 683 | if (activationIndex < 0) { |
| 684 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 685 | return BAD_VALUE; |
| 686 | } |
| 687 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 688 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 689 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 690 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 691 | ident, handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 692 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 693 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 694 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 695 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 696 | if (isClientDisabledLocked(ident)) { |
xiamengsen | 94ae131 | 2021-02-05 15:35:34 +0800 | [diff] [blame] | 697 | ALOGW("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 698 | ident, handle); |
xiamengsen | 94ae131 | 2021-02-05 15:35:34 +0800 | [diff] [blame] | 699 | return NO_ERROR; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 702 | if (info.batchParams.indexOfKey(ident) >= 0) { |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 703 | if (info.numActiveClients() > 0 && !info.isActive) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 704 | activateHardware = true; |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 705 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 706 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 707 | // Log error. Every activate call should be preceded by a batch() call. |
| 708 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 709 | } |
| 710 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 711 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 712 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 713 | // If a connected dynamic sensor is deactivated, remove it from the |
| 714 | // dictionary. |
| 715 | auto it = mConnectedDynamicSensors.find(handle); |
| 716 | if (it != mConnectedDynamicSensors.end()) { |
| 717 | delete it->second; |
| 718 | mConnectedDynamicSensors.erase(it); |
| 719 | } |
| 720 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 721 | if (info.removeBatchParamsForIdent(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 722 | if (info.numActiveClients() == 0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 723 | // 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] | 724 | activateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 725 | } else { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 726 | // Call batch for this sensor with the previously calculated best effort |
| 727 | // batch_rate and timeout. One of the apps has unregistered for sensor |
| 728 | // events, and the best effort batch parameters might have changed. |
| 729 | ALOGD_IF(DEBUG_CONNECTIONS, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 730 | "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle, |
| 731 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 732 | checkReturn(mSensors->batch( |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 733 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 734 | } |
| 735 | } else { |
| 736 | // sensor wasn't enabled for this ident |
| 737 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 738 | |
| 739 | if (isClientDisabledLocked(ident)) { |
| 740 | return NO_ERROR; |
| 741 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 742 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 743 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 744 | if (activateHardware) { |
| 745 | err = doActivateHardwareLocked(handle, enabled); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 746 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 747 | if (err != NO_ERROR && enabled) { |
| 748 | // Failure when enabling the sensor. Clean up on failure. |
| 749 | info.removeBatchParamsForIdent(ident); |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 750 | } else { |
| 751 | // Update the isActive flag if there is no error. If there is an error when disabling a |
| 752 | // sensor, still set the flag to false since the batch parameters have already been |
| 753 | // removed. This ensures that everything remains in-sync. |
| 754 | info.isActive = enabled; |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 755 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 758 | return err; |
| 759 | } |
| 760 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 761 | status_t SensorDevice::doActivateHardwareLocked(int handle, bool enabled) { |
| 762 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, |
| 763 | enabled); |
| 764 | status_t err = checkReturnAndGetStatus(mSensors->activate(handle, enabled)); |
| 765 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, |
| 766 | strerror(-err)); |
| 767 | return err; |
| 768 | } |
| 769 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 770 | status_t SensorDevice::batch( |
| 771 | void* ident, |
| 772 | int handle, |
| 773 | int flags, |
| 774 | int64_t samplingPeriodNs, |
| 775 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 776 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 777 | |
| 778 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 779 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 780 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 781 | if (maxBatchReportLatencyNs < 0) { |
| 782 | maxBatchReportLatencyNs = 0; |
| 783 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 784 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 785 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 786 | "SensorDevice::batch: ident=%p, handle=0x%08x, flags=%d, period_ns=%" PRId64 " timeout=%" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 787 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 788 | |
| 789 | Mutex::Autolock _l(mLock); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 790 | return batchLocked(ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 791 | } |
| 792 | |
| 793 | status_t SensorDevice::batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 794 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 795 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 796 | if (activationIndex < 0) { |
| 797 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 798 | return BAD_VALUE; |
| 799 | } |
| 800 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 801 | |
| 802 | if (info.batchParams.indexOfKey(ident) < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 803 | BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 804 | info.batchParams.add(ident, params); |
| 805 | } else { |
| 806 | // A batch has already been called with this ident. Update the batch parameters. |
| 807 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 808 | } |
| 809 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 810 | status_t err = updateBatchParamsLocked(handle, info); |
| 811 | if (err != NO_ERROR) { |
| 812 | ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s", |
| 813 | mSensors.get(), handle, info.bestBatchParams.mTSample, |
| 814 | info.bestBatchParams.mTBatch, strerror(-err)); |
| 815 | info.removeBatchParamsForIdent(ident); |
| 816 | } |
| 817 | |
| 818 | return err; |
| 819 | } |
| 820 | |
| 821 | status_t SensorDevice::updateBatchParamsLocked(int handle, Info &info) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 822 | BatchParams prevBestBatchParams = info.bestBatchParams; |
| 823 | // Find the minimum of all timeouts and batch_rates for this sensor. |
| 824 | info.selectBatchParams(); |
| 825 | |
| 826 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 827 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 |
| 828 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 829 | prevBestBatchParams.mTSample, info.bestBatchParams.mTSample, |
| 830 | prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 831 | |
| 832 | status_t err(NO_ERROR); |
| 833 | // 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] | 834 | if (prevBestBatchParams != info.bestBatchParams && info.numActiveClients() > 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 835 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle, |
| 836 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 837 | err = checkReturnAndGetStatus(mSensors->batch( |
| 838 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 839 | } |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 840 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 841 | return err; |
| 842 | } |
| 843 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 844 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 845 | return batch(ident, handle, 0, samplingPeriodNs, 0); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 848 | int SensorDevice::getHalDeviceVersion() const { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 849 | if (mSensors == nullptr) return -1; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 850 | return SENSORS_DEVICE_API_VERSION_1_4; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 853 | status_t SensorDevice::flush(void* ident, int handle) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 854 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 855 | if (isClientDisabled(ident)) return INVALID_OPERATION; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 856 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 857 | return checkReturnAndGetStatus(mSensors->flush(handle)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 860 | bool SensorDevice::isClientDisabled(void* ident) const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 861 | Mutex::Autolock _l(mLock); |
| 862 | return isClientDisabledLocked(ident); |
| 863 | } |
| 864 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 865 | bool SensorDevice::isClientDisabledLocked(void* ident) const { |
| 866 | return mDisabledClients.count(ident) > 0; |
| 867 | } |
| 868 | |
| 869 | std::vector<void *> SensorDevice::getDisabledClientsLocked() const { |
| 870 | std::vector<void *> vec; |
| 871 | for (const auto& it : mDisabledClients) { |
| 872 | vec.push_back(it.first); |
| 873 | } |
| 874 | |
| 875 | return vec; |
| 876 | } |
| 877 | |
| 878 | void SensorDevice::addDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 879 | mDisabledClients[ident] |= 1 << reason; |
| 880 | } |
| 881 | |
| 882 | void SensorDevice::removeDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 883 | if (isClientDisabledLocked(ident)) { |
| 884 | mDisabledClients[ident] &= ~(1 << reason); |
| 885 | if (mDisabledClients[ident] == 0) { |
| 886 | mDisabledClients.erase(ident); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | void SensorDevice::setUidStateForConnection(void* ident, SensorService::UidState state) { |
| 892 | Mutex::Autolock _l(mLock); |
| 893 | if (state == SensorService::UID_STATE_ACTIVE) { |
| 894 | removeDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 895 | } else { |
| 896 | addDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 897 | } |
| 898 | |
| 899 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 900 | int handle = mActivationCount.keyAt(i); |
| 901 | Info& info = mActivationCount.editValueAt(i); |
| 902 | |
| 903 | if (info.hasBatchParamsForIdent(ident)) { |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 904 | updateBatchParamsLocked(handle, info); |
| 905 | bool disable = info.numActiveClients() == 0 && info.isActive; |
| 906 | bool enable = info.numActiveClients() > 0 && !info.isActive; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 907 | |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 908 | if ((enable || disable) && |
| 909 | doActivateHardwareLocked(handle, enable) == NO_ERROR) { |
| 910 | info.isActive = enable; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 916 | bool SensorDevice::isSensorActive(int handle) const { |
| 917 | Mutex::Autolock _l(mLock); |
| 918 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 919 | if (activationIndex < 0) { |
| 920 | return false; |
| 921 | } |
Chris Kuiper | c63880a | 2021-08-04 15:06:11 -0700 | [diff] [blame] | 922 | return mActivationCount.valueAt(activationIndex).isActive; |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 925 | void SensorDevice::onMicSensorAccessChanged(void* ident, int handle, nsecs_t samplingPeriodNs) { |
| 926 | Mutex::Autolock _l(mLock); |
| 927 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 928 | if (activationIndex < 0) { |
| 929 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 930 | return; |
| 931 | } |
| 932 | Info& info(mActivationCount.editValueAt(activationIndex)); |
| 933 | if (info.hasBatchParamsForIdent(ident)) { |
| 934 | ssize_t index = info.batchParams.indexOfKey(ident); |
| 935 | BatchParams& params = info.batchParams.editValueAt(index); |
| 936 | params.mTSample = samplingPeriodNs; |
| 937 | } |
| 938 | } |
| 939 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 940 | void SensorDevice::enableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 941 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 942 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 943 | |
| 944 | for (void *client : getDisabledClientsLocked()) { |
| 945 | removeDisabledReasonForIdentLocked( |
| 946 | client, DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
| 947 | } |
| 948 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 949 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 950 | Info& info = mActivationCount.editValueAt(i); |
| 951 | if (info.batchParams.isEmpty()) continue; |
| 952 | info.selectBatchParams(); |
| 953 | const int sensor_handle = mActivationCount.keyAt(i); |
| 954 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", |
| 955 | sensor_handle); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 956 | status_t err = checkReturnAndGetStatus(mSensors->batch( |
| 957 | sensor_handle, |
| 958 | info.bestBatchParams.mTSample, |
| 959 | info.bestBatchParams.mTBatch)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 960 | 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] | 961 | |
| 962 | if (err == NO_ERROR) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 963 | err = checkReturnAndGetStatus(mSensors->activate(sensor_handle, 1 /* enabled */)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 964 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); |
| 965 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 966 | |
| 967 | if (err == NO_ERROR) { |
| 968 | info.isActive = true; |
| 969 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | void SensorDevice::disableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 974 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 975 | Mutex::Autolock _l(mLock); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 976 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 977 | Info& info = mActivationCount.editValueAt(i); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 978 | // Check if this sensor has been activated previously and disable it. |
| 979 | if (info.batchParams.size() > 0) { |
| 980 | const int sensor_handle = mActivationCount.keyAt(i); |
| 981 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", |
| 982 | sensor_handle); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 983 | checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 984 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 985 | // Add all the connections that were registered for this sensor to the disabled |
| 986 | // clients list. |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 987 | for (size_t j = 0; j < info.batchParams.size(); ++j) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 988 | addDisabledReasonForIdentLocked( |
| 989 | info.batchParams.keyAt(j), DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 990 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 991 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 992 | |
| 993 | info.isActive = false; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 998 | status_t SensorDevice::injectSensorData( |
| 999 | const sensors_event_t *injected_sensor_event) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1000 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1001 | ALOGD_IF(DEBUG_CONNECTIONS, |
| 1002 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", |
| 1003 | injected_sensor_event->sensor, |
| 1004 | injected_sensor_event->timestamp, injected_sensor_event->data[0], |
| 1005 | injected_sensor_event->data[1], injected_sensor_event->data[2], |
| 1006 | injected_sensor_event->data[3], injected_sensor_event->data[4], |
| 1007 | injected_sensor_event->data[5]); |
| 1008 | |
| 1009 | Event ev; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1010 | V2_1::implementation::convertFromSensorEvent(*injected_sensor_event, &ev); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1011 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1012 | return checkReturnAndGetStatus(mSensors->injectSensorData(ev)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | status_t SensorDevice::setMode(uint32_t mode) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1016 | if (mSensors == nullptr) return NO_INIT; |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1017 | return checkReturnAndGetStatus(mSensors->setOperationMode( |
| 1018 | static_cast<hardware::sensors::V1_0::OperationMode>(mode))); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1019 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1020 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1021 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { |
| 1022 | if (mSensors == nullptr) return NO_INIT; |
| 1023 | Mutex::Autolock _l(mLock); |
| 1024 | |
| 1025 | SharedMemType type; |
| 1026 | switch (memory->type) { |
| 1027 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: |
| 1028 | type = SharedMemType::ASHMEM; |
| 1029 | break; |
| 1030 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
| 1031 | type = SharedMemType::GRALLOC; |
| 1032 | break; |
| 1033 | default: |
| 1034 | return BAD_VALUE; |
| 1035 | } |
| 1036 | |
| 1037 | SharedMemFormat format; |
| 1038 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { |
| 1039 | return BAD_VALUE; |
| 1040 | } |
| 1041 | format = SharedMemFormat::SENSORS_EVENT; |
| 1042 | |
| 1043 | SharedMemInfo mem = { |
| 1044 | .type = type, |
| 1045 | .format = format, |
| 1046 | .size = static_cast<uint32_t>(memory->size), |
| 1047 | .memoryHandle = memory->handle, |
| 1048 | }; |
| 1049 | |
| 1050 | int32_t ret; |
| 1051 | checkReturn(mSensors->registerDirectChannel(mem, |
| 1052 | [&ret](auto result, auto channelHandle) { |
| 1053 | if (result == Result::OK) { |
| 1054 | ret = channelHandle; |
| 1055 | } else { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1056 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1057 | } |
| 1058 | })); |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { |
| 1063 | if (mSensors == nullptr) return; |
| 1064 | Mutex::Autolock _l(mLock); |
| 1065 | checkReturn(mSensors->unregisterDirectChannel(channelHandle)); |
| 1066 | } |
| 1067 | |
| 1068 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, |
| 1069 | int32_t channelHandle, const struct sensors_direct_cfg_t *config) { |
| 1070 | if (mSensors == nullptr) return NO_INIT; |
| 1071 | Mutex::Autolock _l(mLock); |
| 1072 | |
| 1073 | RateLevel rate; |
| 1074 | switch(config->rate_level) { |
| 1075 | case SENSOR_DIRECT_RATE_STOP: |
| 1076 | rate = RateLevel::STOP; |
| 1077 | break; |
| 1078 | case SENSOR_DIRECT_RATE_NORMAL: |
| 1079 | rate = RateLevel::NORMAL; |
| 1080 | break; |
| 1081 | case SENSOR_DIRECT_RATE_FAST: |
| 1082 | rate = RateLevel::FAST; |
| 1083 | break; |
| 1084 | case SENSOR_DIRECT_RATE_VERY_FAST: |
| 1085 | rate = RateLevel::VERY_FAST; |
| 1086 | break; |
| 1087 | default: |
| 1088 | return BAD_VALUE; |
| 1089 | } |
| 1090 | |
| 1091 | int32_t ret; |
| 1092 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, |
| 1093 | [&ret, rate] (auto result, auto token) { |
| 1094 | if (rate == RateLevel::STOP) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1095 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1096 | } else { |
| 1097 | if (result == Result::OK) { |
| 1098 | ret = token; |
| 1099 | } else { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1100 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | })); |
| 1104 | |
| 1105 | return ret; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1108 | // --------------------------------------------------------------------------- |
| 1109 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 1110 | int SensorDevice::Info::numActiveClients() const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1111 | SensorDevice& device(SensorDevice::getInstance()); |
| 1112 | int num = 0; |
| 1113 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 1114 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 1115 | ++num; |
| 1116 | } |
| 1117 | } |
| 1118 | return num; |
| 1119 | } |
| 1120 | |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1121 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1122 | int64_t samplingPeriodNs, |
| 1123 | int64_t maxBatchReportLatencyNs) { |
| 1124 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1125 | if (index < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1126 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 |
| 1127 | " timeout=%" PRId64 ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1128 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1129 | return BAD_INDEX; |
| 1130 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1131 | BatchParams& params = batchParams.editValueAt(index); |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1132 | params.mTSample = samplingPeriodNs; |
| 1133 | params.mTBatch = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1134 | return NO_ERROR; |
| 1135 | } |
| 1136 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1137 | void SensorDevice::Info::selectBatchParams() { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1138 | BatchParams bestParams; // default to max Tsample and max Tbatch |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1139 | SensorDevice& device(SensorDevice::getInstance()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1140 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1141 | for (size_t i = 0; i < batchParams.size(); ++i) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1142 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 1143 | continue; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1144 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1145 | bestParams.merge(batchParams[i]); |
| 1146 | } |
| 1147 | // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly. |
| 1148 | if (bestParams.mTBatch <= bestParams.mTSample) { |
| 1149 | bestParams.mTBatch = 0; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1150 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1151 | bestBatchParams = bestParams; |
| 1152 | } |
| 1153 | |
| 1154 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 1155 | ssize_t idx = batchParams.removeItem(ident); |
| 1156 | if (idx >= 0) { |
| 1157 | selectBatchParams(); |
| 1158 | } |
| 1159 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1162 | void SensorDevice::notifyConnectionDestroyed(void* ident) { |
| 1163 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 1164 | mDisabledClients.erase(ident); |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 1167 | bool SensorDevice::isDirectReportSupported() const { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1168 | return mIsDirectReportSupported; |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 1169 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1170 | |
| 1171 | void SensorDevice::convertToSensorEvent( |
| 1172 | const Event &src, sensors_event_t *dst) { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1173 | V2_1::implementation::convertToSensorEvent(src, dst); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1174 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1175 | if (src.sensorType == V2_1::SensorType::DYNAMIC_SENSOR_META) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1176 | const DynamicSensorInfo &dyn = src.u.dynamic; |
| 1177 | |
| 1178 | dst->dynamic_sensor_meta.connected = dyn.connected; |
| 1179 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; |
| 1180 | if (dyn.connected) { |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame^] | 1181 | std::unique_lock<std::mutex> lock(mDynamicSensorsMutex); |
| 1182 | // Give MAX_DYN_SENSOR_WAIT_SEC for onDynamicSensorsConnected to be invoked since it |
| 1183 | // can be received out of order from this event due to a bug in the HIDL spec that |
| 1184 | // marks it as oneway. |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1185 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame^] | 1186 | if (it == mConnectedDynamicSensors.end()) { |
| 1187 | mDynamicSensorsCv.wait_for(lock, MAX_DYN_SENSOR_WAIT, |
| 1188 | [&, dyn]{ |
| 1189 | return mConnectedDynamicSensors.find(dyn.sensorHandle) |
| 1190 | != mConnectedDynamicSensors.end(); |
| 1191 | }); |
| 1192 | it = mConnectedDynamicSensors.find(dyn.sensorHandle); |
| 1193 | CHECK(it != mConnectedDynamicSensors.end()); |
| 1194 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1195 | |
| 1196 | dst->dynamic_sensor_meta.sensor = it->second; |
| 1197 | |
| 1198 | memcpy(dst->dynamic_sensor_meta.uuid, |
| 1199 | dyn.uuid.data(), |
| 1200 | sizeof(dst->dynamic_sensor_meta.uuid)); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1205 | void SensorDevice::convertToSensorEventsAndQuantize( |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1206 | const hidl_vec<Event> &src, |
| 1207 | const hidl_vec<SensorInfo> &dynamicSensorsAdded, |
| 1208 | sensors_event_t *dst) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1209 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 1210 | if (dynamicSensorsAdded.size() > 0) { |
| 1211 | onDynamicSensorsConnected(dynamicSensorsAdded); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | for (size_t i = 0; i < src.size(); ++i) { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1215 | V2_1::implementation::convertToSensorEvent(src[i], &dst[i]); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1216 | android::SensorDeviceUtils::quantizeSensorEventValues(&dst[i], |
| 1217 | getResolutionForSensor(dst[i].sensor)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1221 | float SensorDevice::getResolutionForSensor(int sensorHandle) { |
| 1222 | for (size_t i = 0; i < mSensorList.size(); i++) { |
| 1223 | if (sensorHandle == mSensorList[i].handle) { |
| 1224 | return mSensorList[i].resolution; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | auto it = mConnectedDynamicSensors.find(sensorHandle); |
| 1229 | if (it != mConnectedDynamicSensors.end()) { |
| 1230 | return it->second->resolution; |
| 1231 | } |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 1236 | void SensorDevice::handleHidlDeath(const std::string & detail) { |
Brian Duddie | 6d2e375 | 2019-05-30 09:52:28 -0700 | [diff] [blame] | 1237 | if (!mSensors->supportsMessageQueues()) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 1238 | // restart is the only option at present. |
| 1239 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); |
| 1240 | } else { |
| 1241 | ALOGD("ISensors HAL died, death recipient will attempt reconnect"); |
| 1242 | } |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 1243 | } |
| 1244 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1245 | status_t SensorDevice::checkReturnAndGetStatus(const Return<Result>& ret) { |
| 1246 | checkReturn(ret); |
| 1247 | return (!ret.isOk()) ? DEAD_OBJECT : statusFromResult(ret); |
| 1248 | } |
| 1249 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1250 | // --------------------------------------------------------------------------- |
| 1251 | }; // namespace android |