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