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) { |
| 617 | // Allocate a sensor_t structure for each dynamic sensor added and insert |
| 618 | // it into the dictionary of connected dynamic sensors keyed by handle. |
| 619 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { |
| 620 | const SensorInfo &info = dynamicSensorsAdded[i]; |
| 621 | |
| 622 | auto it = mConnectedDynamicSensors.find(info.sensorHandle); |
| 623 | CHECK(it == mConnectedDynamicSensors.end()); |
| 624 | |
| 625 | sensor_t *sensor = new sensor_t(); |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 626 | convertToSensor(convertToOldSensorInfo(info), sensor); |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 627 | |
| 628 | mConnectedDynamicSensors.insert( |
| 629 | std::make_pair(sensor->handle, sensor)); |
| 630 | } |
| 631 | |
| 632 | return Return<void>(); |
| 633 | } |
| 634 | |
| 635 | Return<void> SensorDevice::onDynamicSensorsDisconnected( |
| 636 | const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) { |
| 637 | (void) dynamicSensorHandlesRemoved; |
| 638 | // TODO: Currently dynamic sensors do not seem to be removed |
| 639 | return Return<void>(); |
| 640 | } |
| 641 | |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 642 | void SensorDevice::writeWakeLockHandled(uint32_t count) { |
Brian Stack | a24e7d4 | 2019-01-08 12:56:09 -0800 | [diff] [blame] | 643 | if (mSensors != nullptr && mSensors->supportsMessageQueues()) { |
| 644 | if (mWakeLockQueue->write(&count)) { |
| 645 | mWakeLockQueueFlag->wake(asBaseType(WakeLockQueueFlagBits::DATA_WRITTEN)); |
| 646 | } else { |
| 647 | ALOGW("Failed to write wake lock handled"); |
| 648 | } |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 652 | void SensorDevice::autoDisable(void *ident, int handle) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 653 | Mutex::Autolock _l(mLock); |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 654 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 655 | if (activationIndex < 0) { |
| 656 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 657 | return; |
| 658 | } |
| 659 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 660 | info.removeBatchParamsForIdent(ident); |
Brian Stack | aa6dc09 | 2019-05-10 13:36:40 -0700 | [diff] [blame] | 661 | if (info.numActiveClients() == 0) { |
| 662 | info.isActive = false; |
| 663 | } |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 666 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 667 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 668 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 669 | Mutex::Autolock _l(mLock); |
| 670 | return activateLocked(ident, handle, enabled); |
| 671 | } |
| 672 | |
| 673 | status_t SensorDevice::activateLocked(void* ident, int handle, int enabled) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 674 | bool activateHardware = false; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 675 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 676 | status_t err(NO_ERROR); |
| 677 | |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 678 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 679 | if (activationIndex < 0) { |
| 680 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 681 | return BAD_VALUE; |
| 682 | } |
| 683 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 684 | |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 685 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 686 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 687 | ident, handle, enabled, info.batchParams.size()); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 688 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 689 | if (enabled) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 690 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 691 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 692 | if (isClientDisabledLocked(ident)) { |
xiamengsen | 94ae131 | 2021-02-05 15:35:34 +0800 | [diff] [blame] | 693 | ALOGW("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 694 | ident, handle); |
xiamengsen | 94ae131 | 2021-02-05 15:35:34 +0800 | [diff] [blame] | 695 | return NO_ERROR; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 698 | if (info.batchParams.indexOfKey(ident) >= 0) { |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 699 | if (info.numActiveClients() > 0 && !info.isActive) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 700 | activateHardware = true; |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 701 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 702 | } else { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 703 | // Log error. Every activate call should be preceded by a batch() call. |
| 704 | ALOGE("\t >>>ERROR: activate called without batch"); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 705 | } |
| 706 | } else { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 707 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 708 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 709 | // If a connected dynamic sensor is deactivated, remove it from the |
| 710 | // dictionary. |
| 711 | auto it = mConnectedDynamicSensors.find(handle); |
| 712 | if (it != mConnectedDynamicSensors.end()) { |
| 713 | delete it->second; |
| 714 | mConnectedDynamicSensors.erase(it); |
| 715 | } |
| 716 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 717 | if (info.removeBatchParamsForIdent(ident) >= 0) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 718 | if (info.numActiveClients() == 0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 719 | // 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] | 720 | activateHardware = true; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 721 | } else { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 722 | // Call batch for this sensor with the previously calculated best effort |
| 723 | // batch_rate and timeout. One of the apps has unregistered for sensor |
| 724 | // events, and the best effort batch parameters might have changed. |
| 725 | ALOGD_IF(DEBUG_CONNECTIONS, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 726 | "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle, |
| 727 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 728 | checkReturn(mSensors->batch( |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 729 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 730 | } |
| 731 | } else { |
| 732 | // sensor wasn't enabled for this ident |
| 733 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 734 | |
| 735 | if (isClientDisabledLocked(ident)) { |
| 736 | return NO_ERROR; |
| 737 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 738 | } |
Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 739 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 740 | if (activateHardware) { |
| 741 | err = doActivateHardwareLocked(handle, enabled); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 742 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 743 | if (err != NO_ERROR && enabled) { |
| 744 | // Failure when enabling the sensor. Clean up on failure. |
| 745 | info.removeBatchParamsForIdent(ident); |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 746 | } else { |
| 747 | // Update the isActive flag if there is no error. If there is an error when disabling a |
| 748 | // sensor, still set the flag to false since the batch parameters have already been |
| 749 | // removed. This ensures that everything remains in-sync. |
| 750 | info.isActive = enabled; |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 751 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 754 | return err; |
| 755 | } |
| 756 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 757 | status_t SensorDevice::doActivateHardwareLocked(int handle, bool enabled) { |
| 758 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, |
| 759 | enabled); |
| 760 | status_t err = checkReturnAndGetStatus(mSensors->activate(handle, enabled)); |
| 761 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, |
| 762 | strerror(-err)); |
| 763 | return err; |
| 764 | } |
| 765 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 766 | status_t SensorDevice::batch( |
| 767 | void* ident, |
| 768 | int handle, |
| 769 | int flags, |
| 770 | int64_t samplingPeriodNs, |
| 771 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 772 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 773 | |
| 774 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { |
| 775 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; |
| 776 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 777 | if (maxBatchReportLatencyNs < 0) { |
| 778 | maxBatchReportLatencyNs = 0; |
| 779 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 780 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 781 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 782 | "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] | 783 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 784 | |
| 785 | Mutex::Autolock _l(mLock); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 786 | return batchLocked(ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 787 | } |
| 788 | |
| 789 | status_t SensorDevice::batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 790 | int64_t maxBatchReportLatencyNs) { |
Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 791 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 792 | if (activationIndex < 0) { |
| 793 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 794 | return BAD_VALUE; |
| 795 | } |
| 796 | Info& info(mActivationCount.editValueAt(activationIndex)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 797 | |
| 798 | if (info.batchParams.indexOfKey(ident) < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 799 | BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 800 | info.batchParams.add(ident, params); |
| 801 | } else { |
| 802 | // A batch has already been called with this ident. Update the batch parameters. |
| 803 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 804 | } |
| 805 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 806 | status_t err = updateBatchParamsLocked(handle, info); |
| 807 | if (err != NO_ERROR) { |
| 808 | ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s", |
| 809 | mSensors.get(), handle, info.bestBatchParams.mTSample, |
| 810 | info.bestBatchParams.mTBatch, strerror(-err)); |
| 811 | info.removeBatchParamsForIdent(ident); |
| 812 | } |
| 813 | |
| 814 | return err; |
| 815 | } |
| 816 | |
| 817 | status_t SensorDevice::updateBatchParamsLocked(int handle, Info &info) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 818 | BatchParams prevBestBatchParams = info.bestBatchParams; |
| 819 | // Find the minimum of all timeouts and batch_rates for this sensor. |
| 820 | info.selectBatchParams(); |
| 821 | |
| 822 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 823 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 |
| 824 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 825 | prevBestBatchParams.mTSample, info.bestBatchParams.mTSample, |
| 826 | prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 827 | |
| 828 | status_t err(NO_ERROR); |
| 829 | // 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] | 830 | if (prevBestBatchParams != info.bestBatchParams && info.numActiveClients() > 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 831 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle, |
| 832 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 833 | err = checkReturnAndGetStatus(mSensors->batch( |
| 834 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 835 | } |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 836 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 837 | return err; |
| 838 | } |
| 839 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 840 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 841 | return batch(ident, handle, 0, samplingPeriodNs, 0); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 844 | int SensorDevice::getHalDeviceVersion() const { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 845 | if (mSensors == nullptr) return -1; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 846 | return SENSORS_DEVICE_API_VERSION_1_4; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 849 | status_t SensorDevice::flush(void* ident, int handle) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 850 | if (mSensors == nullptr) return NO_INIT; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 851 | if (isClientDisabled(ident)) return INVALID_OPERATION; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 852 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 853 | return checkReturnAndGetStatus(mSensors->flush(handle)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 856 | bool SensorDevice::isClientDisabled(void* ident) const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 857 | Mutex::Autolock _l(mLock); |
| 858 | return isClientDisabledLocked(ident); |
| 859 | } |
| 860 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 861 | bool SensorDevice::isClientDisabledLocked(void* ident) const { |
| 862 | return mDisabledClients.count(ident) > 0; |
| 863 | } |
| 864 | |
| 865 | std::vector<void *> SensorDevice::getDisabledClientsLocked() const { |
| 866 | std::vector<void *> vec; |
| 867 | for (const auto& it : mDisabledClients) { |
| 868 | vec.push_back(it.first); |
| 869 | } |
| 870 | |
| 871 | return vec; |
| 872 | } |
| 873 | |
| 874 | void SensorDevice::addDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 875 | mDisabledClients[ident] |= 1 << reason; |
| 876 | } |
| 877 | |
| 878 | void SensorDevice::removeDisabledReasonForIdentLocked(void* ident, DisabledReason reason) { |
| 879 | if (isClientDisabledLocked(ident)) { |
| 880 | mDisabledClients[ident] &= ~(1 << reason); |
| 881 | if (mDisabledClients[ident] == 0) { |
| 882 | mDisabledClients.erase(ident); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void SensorDevice::setUidStateForConnection(void* ident, SensorService::UidState state) { |
| 888 | Mutex::Autolock _l(mLock); |
| 889 | if (state == SensorService::UID_STATE_ACTIVE) { |
| 890 | removeDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 891 | } else { |
| 892 | addDisabledReasonForIdentLocked(ident, DisabledReason::DISABLED_REASON_UID_IDLE); |
| 893 | } |
| 894 | |
| 895 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 896 | int handle = mActivationCount.keyAt(i); |
| 897 | Info& info = mActivationCount.editValueAt(i); |
| 898 | |
| 899 | if (info.hasBatchParamsForIdent(ident)) { |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 900 | updateBatchParamsLocked(handle, info); |
| 901 | bool disable = info.numActiveClients() == 0 && info.isActive; |
| 902 | bool enable = info.numActiveClients() > 0 && !info.isActive; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 903 | |
Arthur Ishiguro | 3cc2c35 | 2020-06-01 16:15:19 -0700 | [diff] [blame] | 904 | if ((enable || disable) && |
| 905 | doActivateHardwareLocked(handle, enable) == NO_ERROR) { |
| 906 | info.isActive = enable; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 912 | bool SensorDevice::isSensorActive(int handle) const { |
| 913 | Mutex::Autolock _l(mLock); |
| 914 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 915 | if (activationIndex < 0) { |
| 916 | return false; |
| 917 | } |
| 918 | return mActivationCount.valueAt(activationIndex).numActiveClients() > 0; |
| 919 | } |
| 920 | |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 921 | void SensorDevice::onMicSensorAccessChanged(void* ident, int handle, nsecs_t samplingPeriodNs) { |
| 922 | Mutex::Autolock _l(mLock); |
| 923 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); |
| 924 | if (activationIndex < 0) { |
| 925 | ALOGW("Handle %d cannot be found in activation record", handle); |
| 926 | return; |
| 927 | } |
| 928 | Info& info(mActivationCount.editValueAt(activationIndex)); |
| 929 | if (info.hasBatchParamsForIdent(ident)) { |
| 930 | ssize_t index = info.batchParams.indexOfKey(ident); |
| 931 | BatchParams& params = info.batchParams.editValueAt(index); |
| 932 | params.mTSample = samplingPeriodNs; |
| 933 | } |
| 934 | } |
| 935 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 936 | void SensorDevice::enableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 937 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 938 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 939 | |
| 940 | for (void *client : getDisabledClientsLocked()) { |
| 941 | removeDisabledReasonForIdentLocked( |
| 942 | client, DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
| 943 | } |
| 944 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 945 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
| 946 | Info& info = mActivationCount.editValueAt(i); |
| 947 | if (info.batchParams.isEmpty()) continue; |
| 948 | info.selectBatchParams(); |
| 949 | const int sensor_handle = mActivationCount.keyAt(i); |
| 950 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", |
| 951 | sensor_handle); |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 952 | status_t err = checkReturnAndGetStatus(mSensors->batch( |
| 953 | sensor_handle, |
| 954 | info.bestBatchParams.mTSample, |
| 955 | info.bestBatchParams.mTBatch)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 956 | 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] | 957 | |
| 958 | if (err == NO_ERROR) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 959 | err = checkReturnAndGetStatus(mSensors->activate(sensor_handle, 1 /* enabled */)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 960 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); |
| 961 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 962 | |
| 963 | if (err == NO_ERROR) { |
| 964 | info.isActive = true; |
| 965 | } |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | void SensorDevice::disableAllSensors() { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 970 | if (mSensors == nullptr) return; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 971 | Mutex::Autolock _l(mLock); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 972 | for (size_t i = 0; i< mActivationCount.size(); ++i) { |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 973 | Info& info = mActivationCount.editValueAt(i); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 974 | // Check if this sensor has been activated previously and disable it. |
| 975 | if (info.batchParams.size() > 0) { |
| 976 | const int sensor_handle = mActivationCount.keyAt(i); |
| 977 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", |
| 978 | sensor_handle); |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 979 | checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 980 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 981 | // Add all the connections that were registered for this sensor to the disabled |
| 982 | // clients list. |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 983 | for (size_t j = 0; j < info.batchParams.size(); ++j) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 984 | addDisabledReasonForIdentLocked( |
| 985 | info.batchParams.keyAt(j), DisabledReason::DISABLED_REASON_SERVICE_RESTRICTED); |
Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 986 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 987 | } |
Brian Stack | 4a11fed | 2019-04-22 15:07:50 -0700 | [diff] [blame] | 988 | |
| 989 | info.isActive = false; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 994 | status_t SensorDevice::injectSensorData( |
| 995 | const sensors_event_t *injected_sensor_event) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 996 | if (mSensors == nullptr) return NO_INIT; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 997 | ALOGD_IF(DEBUG_CONNECTIONS, |
| 998 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", |
| 999 | injected_sensor_event->sensor, |
| 1000 | injected_sensor_event->timestamp, injected_sensor_event->data[0], |
| 1001 | injected_sensor_event->data[1], injected_sensor_event->data[2], |
| 1002 | injected_sensor_event->data[3], injected_sensor_event->data[4], |
| 1003 | injected_sensor_event->data[5]); |
| 1004 | |
| 1005 | Event ev; |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1006 | V2_1::implementation::convertFromSensorEvent(*injected_sensor_event, &ev); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1007 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1008 | return checkReturnAndGetStatus(mSensors->injectSensorData(ev)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | status_t SensorDevice::setMode(uint32_t mode) { |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1012 | if (mSensors == nullptr) return NO_INIT; |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1013 | return checkReturnAndGetStatus(mSensors->setOperationMode( |
| 1014 | static_cast<hardware::sensors::V1_0::OperationMode>(mode))); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1015 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1016 | |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1017 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { |
| 1018 | if (mSensors == nullptr) return NO_INIT; |
| 1019 | Mutex::Autolock _l(mLock); |
| 1020 | |
| 1021 | SharedMemType type; |
| 1022 | switch (memory->type) { |
| 1023 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: |
| 1024 | type = SharedMemType::ASHMEM; |
| 1025 | break; |
| 1026 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
| 1027 | type = SharedMemType::GRALLOC; |
| 1028 | break; |
| 1029 | default: |
| 1030 | return BAD_VALUE; |
| 1031 | } |
| 1032 | |
| 1033 | SharedMemFormat format; |
| 1034 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { |
| 1035 | return BAD_VALUE; |
| 1036 | } |
| 1037 | format = SharedMemFormat::SENSORS_EVENT; |
| 1038 | |
| 1039 | SharedMemInfo mem = { |
| 1040 | .type = type, |
| 1041 | .format = format, |
| 1042 | .size = static_cast<uint32_t>(memory->size), |
| 1043 | .memoryHandle = memory->handle, |
| 1044 | }; |
| 1045 | |
| 1046 | int32_t ret; |
| 1047 | checkReturn(mSensors->registerDirectChannel(mem, |
| 1048 | [&ret](auto result, auto channelHandle) { |
| 1049 | if (result == Result::OK) { |
| 1050 | ret = channelHandle; |
| 1051 | } else { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1052 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1053 | } |
| 1054 | })); |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { |
| 1059 | if (mSensors == nullptr) return; |
| 1060 | Mutex::Autolock _l(mLock); |
| 1061 | checkReturn(mSensors->unregisterDirectChannel(channelHandle)); |
| 1062 | } |
| 1063 | |
| 1064 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, |
| 1065 | int32_t channelHandle, const struct sensors_direct_cfg_t *config) { |
| 1066 | if (mSensors == nullptr) return NO_INIT; |
| 1067 | Mutex::Autolock _l(mLock); |
| 1068 | |
| 1069 | RateLevel rate; |
| 1070 | switch(config->rate_level) { |
| 1071 | case SENSOR_DIRECT_RATE_STOP: |
| 1072 | rate = RateLevel::STOP; |
| 1073 | break; |
| 1074 | case SENSOR_DIRECT_RATE_NORMAL: |
| 1075 | rate = RateLevel::NORMAL; |
| 1076 | break; |
| 1077 | case SENSOR_DIRECT_RATE_FAST: |
| 1078 | rate = RateLevel::FAST; |
| 1079 | break; |
| 1080 | case SENSOR_DIRECT_RATE_VERY_FAST: |
| 1081 | rate = RateLevel::VERY_FAST; |
| 1082 | break; |
| 1083 | default: |
| 1084 | return BAD_VALUE; |
| 1085 | } |
| 1086 | |
| 1087 | int32_t ret; |
| 1088 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, |
| 1089 | [&ret, rate] (auto result, auto token) { |
| 1090 | if (rate == RateLevel::STOP) { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1091 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1092 | } else { |
| 1093 | if (result == Result::OK) { |
| 1094 | ret = token; |
| 1095 | } else { |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1096 | ret = statusFromResult(result); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | })); |
| 1100 | |
| 1101 | return ret; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1104 | // --------------------------------------------------------------------------- |
| 1105 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 1106 | int SensorDevice::Info::numActiveClients() const { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1107 | SensorDevice& device(SensorDevice::getInstance()); |
| 1108 | int num = 0; |
| 1109 | for (size_t i = 0; i < batchParams.size(); ++i) { |
| 1110 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 1111 | ++num; |
| 1112 | } |
| 1113 | } |
| 1114 | return num; |
| 1115 | } |
| 1116 | |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1117 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1118 | int64_t samplingPeriodNs, |
| 1119 | int64_t maxBatchReportLatencyNs) { |
| 1120 | ssize_t index = batchParams.indexOfKey(ident); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1121 | if (index < 0) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1122 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 |
| 1123 | " timeout=%" PRId64 ") failed (%s)", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1124 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1125 | return BAD_INDEX; |
| 1126 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1127 | BatchParams& params = batchParams.editValueAt(index); |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1128 | params.mTSample = samplingPeriodNs; |
| 1129 | params.mTBatch = maxBatchReportLatencyNs; |
Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 1130 | return NO_ERROR; |
| 1131 | } |
| 1132 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1133 | void SensorDevice::Info::selectBatchParams() { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1134 | BatchParams bestParams; // default to max Tsample and max Tbatch |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1135 | SensorDevice& device(SensorDevice::getInstance()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1136 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1137 | for (size_t i = 0; i < batchParams.size(); ++i) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1138 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) { |
| 1139 | continue; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1140 | } |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 1141 | bestParams.merge(batchParams[i]); |
| 1142 | } |
| 1143 | // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly. |
| 1144 | if (bestParams.mTBatch <= bestParams.mTSample) { |
| 1145 | bestParams.mTBatch = 0; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1146 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1147 | bestBatchParams = bestParams; |
| 1148 | } |
| 1149 | |
| 1150 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { |
| 1151 | ssize_t idx = batchParams.removeItem(ident); |
| 1152 | if (idx >= 0) { |
| 1153 | selectBatchParams(); |
| 1154 | } |
| 1155 | return idx; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1158 | void SensorDevice::notifyConnectionDestroyed(void* ident) { |
| 1159 | Mutex::Autolock _l(mLock); |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 1160 | mDisabledClients.erase(ident); |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 1163 | bool SensorDevice::isDirectReportSupported() const { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1164 | return mIsDirectReportSupported; |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 1165 | } |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1166 | |
| 1167 | void SensorDevice::convertToSensorEvent( |
| 1168 | const Event &src, sensors_event_t *dst) { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1169 | V2_1::implementation::convertToSensorEvent(src, dst); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1170 | |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1171 | if (src.sensorType == V2_1::SensorType::DYNAMIC_SENSOR_META) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1172 | const DynamicSensorInfo &dyn = src.u.dynamic; |
| 1173 | |
| 1174 | dst->dynamic_sensor_meta.connected = dyn.connected; |
| 1175 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; |
| 1176 | if (dyn.connected) { |
| 1177 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); |
| 1178 | CHECK(it != mConnectedDynamicSensors.end()); |
| 1179 | |
| 1180 | dst->dynamic_sensor_meta.sensor = it->second; |
| 1181 | |
| 1182 | memcpy(dst->dynamic_sensor_meta.uuid, |
| 1183 | dyn.uuid.data(), |
| 1184 | sizeof(dst->dynamic_sensor_meta.uuid)); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1189 | void SensorDevice::convertToSensorEventsAndQuantize( |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1190 | const hidl_vec<Event> &src, |
| 1191 | const hidl_vec<SensorInfo> &dynamicSensorsAdded, |
| 1192 | sensors_event_t *dst) { |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1193 | |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 1194 | if (dynamicSensorsAdded.size() > 0) { |
| 1195 | onDynamicSensorsConnected(dynamicSensorsAdded); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | for (size_t i = 0; i < src.size(); ++i) { |
Anthony Stange | e38a141 | 2020-02-13 21:28:37 -0500 | [diff] [blame] | 1199 | V2_1::implementation::convertToSensorEvent(src[i], &dst[i]); |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1200 | android::SensorDeviceUtils::quantizeSensorEventValues(&dst[i], |
| 1201 | getResolutionForSensor(dst[i].sensor)); |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 1205 | float SensorDevice::getResolutionForSensor(int sensorHandle) { |
| 1206 | for (size_t i = 0; i < mSensorList.size(); i++) { |
| 1207 | if (sensorHandle == mSensorList[i].handle) { |
| 1208 | return mSensorList[i].resolution; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | auto it = mConnectedDynamicSensors.find(sensorHandle); |
| 1213 | if (it != mConnectedDynamicSensors.end()) { |
| 1214 | return it->second->resolution; |
| 1215 | } |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 1220 | void SensorDevice::handleHidlDeath(const std::string & detail) { |
Brian Duddie | 6d2e375 | 2019-05-30 09:52:28 -0700 | [diff] [blame] | 1221 | if (!mSensors->supportsMessageQueues()) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 1222 | // restart is the only option at present. |
| 1223 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); |
| 1224 | } else { |
| 1225 | ALOGD("ISensors HAL died, death recipient will attempt reconnect"); |
| 1226 | } |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Brian Duddie | 8e6f31c | 2019-05-29 13:19:13 -0700 | [diff] [blame] | 1229 | status_t SensorDevice::checkReturnAndGetStatus(const Return<Result>& ret) { |
| 1230 | checkReturn(ret); |
| 1231 | return (!ret.isOk()) ? DEAD_OBJECT : statusFromResult(ret); |
| 1232 | } |
| 1233 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1234 | // --------------------------------------------------------------------------- |
| 1235 | }; // namespace android |