| 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 |  | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 19 | #include "android/hardware/sensors/2.0/ISensorsCallback.h" | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 20 | #include "android/hardware/sensors/2.0/types.h" | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 21 | #include "SensorService.h" | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 22 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 24 | #include <sensors/convert.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 25 | #include <cutils/atomic.h> | 
| Ashutosh Joshi | 5cafc1e | 2017-02-09 21:44:04 +0000 | [diff] [blame] | 26 | #include <utils/Errors.h> | 
|  | 27 | #include <utils/Singleton.h> | 
| Steven Moreland | d333511 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 28 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 29 | #include <chrono> | 
|  | 30 | #include <cinttypes> | 
|  | 31 | #include <thread> | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 32 |  | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 33 | using namespace android::hardware::sensors; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 34 | using namespace android::hardware::sensors::V1_0; | 
|  | 35 | using namespace android::hardware::sensors::V1_0::implementation; | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 36 | using android::hardware::sensors::V2_0::ISensorsCallback; | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 37 | using android::hardware::sensors::V2_0::EventQueueFlagBits; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 38 | using android::hardware::hidl_vec; | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 39 | using android::hardware::Return; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 40 | using android::SensorDeviceUtils::HidlServiceRegistrationWaiter; | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 41 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 42 | namespace android { | 
|  | 43 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice) | 
|  | 46 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 47 | static status_t StatusFromResult(Result result) { | 
|  | 48 | switch (result) { | 
|  | 49 | case Result::OK: | 
|  | 50 | return OK; | 
|  | 51 | case Result::BAD_VALUE: | 
|  | 52 | return BAD_VALUE; | 
|  | 53 | case Result::PERMISSION_DENIED: | 
|  | 54 | return PERMISSION_DENIED; | 
|  | 55 | case Result::INVALID_OPERATION: | 
|  | 56 | return INVALID_OPERATION; | 
|  | 57 | case Result::NO_MEMORY: | 
|  | 58 | return NO_MEMORY; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 62 | template<typename EnumType> | 
|  | 63 | constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) { | 
|  | 64 | return static_cast<typename std::underlying_type<EnumType>::type>(value); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // Used internally by the framework to wake the Event FMQ. These values must start after | 
|  | 68 | // the last value of EventQueueFlagBits | 
|  | 69 | enum EventQueueFlagBitsInternal : uint32_t { | 
|  | 70 | INTERNAL_WAKE =  1 << 16, | 
|  | 71 | }; | 
|  | 72 |  | 
| Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 73 | void SensorsHalDeathReceivier::serviceDied( | 
|  | 74 | uint64_t /* cookie */, | 
|  | 75 | const wp<::android::hidl::base::V1_0::IBase>& /* service */) { | 
|  | 76 | ALOGW("Sensors HAL died, attempting to reconnect."); | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 77 | SensorDevice::getInstance().prepareForReconnect(); | 
| Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 80 | struct SensorsCallback : public ISensorsCallback { | 
|  | 81 | using Result = ::android::hardware::sensors::V1_0::Result; | 
|  | 82 | Return<void> onDynamicSensorsConnected( | 
|  | 83 | const hidl_vec<SensorInfo> &dynamicSensorsAdded) override { | 
|  | 84 | return SensorDevice::getInstance().onDynamicSensorsConnected(dynamicSensorsAdded); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | Return<void> onDynamicSensorsDisconnected( | 
|  | 88 | const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override { | 
|  | 89 | return SensorDevice::getInstance().onDynamicSensorsDisconnected( | 
|  | 90 | dynamicSensorHandlesRemoved); | 
|  | 91 | } | 
|  | 92 | }; | 
|  | 93 |  | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 94 | SensorDevice::SensorDevice() | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 95 | : mHidlTransportErrors(20), | 
|  | 96 | mRestartWaiter(new HidlServiceRegistrationWaiter()), | 
|  | 97 | mReconnecting(false) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 98 | if (!connectHidlService()) { | 
|  | 99 | return; | 
|  | 100 | } | 
| Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 101 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 102 | initializeSensorList(); | 
|  | 103 |  | 
|  | 104 | mIsDirectReportSupported = | 
|  | 105 | (checkReturn(mSensors->unregisterDirectChannel(-1)) != Result::INVALID_OPERATION); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | void SensorDevice::initializeSensorList() { | 
| Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 109 | float minPowerMa = 0.001; // 1 microAmp | 
|  | 110 |  | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 111 | checkReturn(mSensors->getSensorsList( | 
|  | 112 | [&](const auto &list) { | 
|  | 113 | const size_t count = list.size(); | 
|  | 114 |  | 
|  | 115 | mActivationCount.setCapacity(count); | 
|  | 116 | Info model; | 
|  | 117 | for (size_t i=0 ; i < count; i++) { | 
|  | 118 | sensor_t sensor; | 
|  | 119 | convertToSensor(list[i], &sensor); | 
| Ashutosh Joshi | fea2d26 | 2017-04-19 23:27:49 -0700 | [diff] [blame] | 120 | // Sanity check and clamp power if it is 0 (or close) | 
|  | 121 | if (sensor.power < minPowerMa) { | 
|  | 122 | ALOGE("Reported power %f not deemed sane, clamping to %f", | 
|  | 123 | sensor.power, minPowerMa); | 
|  | 124 | sensor.power = minPowerMa; | 
|  | 125 | } | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 126 | mSensorList.push_back(sensor); | 
|  | 127 |  | 
|  | 128 | mActivationCount.add(list[i].sensorHandle, model); | 
|  | 129 |  | 
|  | 130 | checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */)); | 
|  | 131 | } | 
|  | 132 | })); | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 135 | SensorDevice::~SensorDevice() { | 
|  | 136 | if (mEventQueueFlag != nullptr) { | 
|  | 137 | hardware::EventFlag::deleteEventFlag(&mEventQueueFlag); | 
|  | 138 | mEventQueueFlag = nullptr; | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 142 | bool SensorDevice::connectHidlService() { | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 143 | HalConnectionStatus status = connectHidlServiceV2_0(); | 
|  | 144 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { | 
|  | 145 | status = connectHidlServiceV1_0(); | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 146 | } | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 147 | return (status == HalConnectionStatus::CONNECTED); | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 150 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV1_0() { | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 151 | // 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] | 152 | size_t retry = 10; | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 153 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 154 |  | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 155 | while (retry-- > 0) { | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 156 | sp<V1_0::ISensors> sensors = V1_0::ISensors::getService(); | 
|  | 157 | if (sensors == nullptr) { | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 158 | // no sensor hidl service found | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 159 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 160 | break; | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 161 | } | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 162 |  | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 163 | mSensors = new SensorServiceUtil::SensorsWrapperV1_0(sensors); | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 164 | mRestartWaiter->reset(); | 
|  | 165 | // Poke ISensor service. If it has lingering connection from previous generation of | 
|  | 166 | // system server, it will kill itself. There is no intention to handle the poll result, | 
|  | 167 | // which will be done since the size is 0. | 
|  | 168 | if(mSensors->poll(0, [](auto, const auto &, const auto &) {}).isOk()) { | 
|  | 169 | // ok to continue | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 170 | connectionStatus = HalConnectionStatus::CONNECTED; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 171 | break; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | // hidl service is restarting, pointer is invalid. | 
|  | 175 | mSensors = nullptr; | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 176 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 177 | ALOGI("%s unsuccessful, remaining retry %zu.", __FUNCTION__, retry); | 
|  | 178 | mRestartWaiter->wait(); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 179 | } | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 180 |  | 
|  | 181 | return connectionStatus; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 184 | SensorDevice::HalConnectionStatus SensorDevice::connectHidlServiceV2_0() { | 
|  | 185 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 186 | sp<V2_0::ISensors> sensors = V2_0::ISensors::getService(); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 187 |  | 
|  | 188 | if (sensors == nullptr) { | 
|  | 189 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; | 
|  | 190 | } else { | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 191 | mSensors = new SensorServiceUtil::SensorsWrapperV2_0(sensors); | 
|  | 192 |  | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 193 | mEventQueue = std::make_unique<EventMessageQueue>( | 
|  | 194 | SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, | 
|  | 195 | true /* configureEventFlagWord */); | 
|  | 196 |  | 
|  | 197 | mWakeLockQueue = std::make_unique<WakeLockQueue>( | 
|  | 198 | SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, | 
|  | 199 | true /* configureEventFlagWord */); | 
|  | 200 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 201 | hardware::EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag); | 
|  | 202 |  | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 203 | CHECK(mSensors != nullptr && mEventQueue != nullptr && | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 204 | mWakeLockQueue != nullptr && mEventQueueFlag != nullptr); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 205 |  | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 206 | status_t status = StatusFromResult(checkReturn(mSensors->initialize( | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 207 | *mEventQueue->getDesc(), | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 208 | *mWakeLockQueue->getDesc(), | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 209 | new SensorsCallback()))); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 210 |  | 
|  | 211 | if (status != NO_ERROR) { | 
|  | 212 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 213 | ALOGE("Failed to initialize Sensors HAL (%s)", strerror(-status)); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 214 | } else { | 
|  | 215 | connectionStatus = HalConnectionStatus::CONNECTED; | 
| Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 216 | mSensorsHalDeathReceiver = new SensorsHalDeathReceivier(); | 
|  | 217 | sensors->linkToDeath(mSensorsHalDeathReceiver, 0 /* cookie */); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 218 | } | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 219 | } | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 220 |  | 
|  | 221 | return connectionStatus; | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 224 | void SensorDevice::prepareForReconnect() { | 
|  | 225 | mReconnecting = true; | 
|  | 226 |  | 
|  | 227 | // Wake up the polling thread so it returns and allows the SensorService to initiate | 
|  | 228 | // a reconnect. | 
|  | 229 | mEventQueueFlag->wake(asBaseType(INTERNAL_WAKE)); | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | void SensorDevice::reconnect() { | 
|  | 233 | Mutex::Autolock _l(mLock); | 
|  | 234 | mSensors = nullptr; | 
|  | 235 |  | 
|  | 236 | auto previousActivations = mActivationCount; | 
|  | 237 | auto previousSensorList = mSensorList; | 
|  | 238 |  | 
|  | 239 | mActivationCount.clear(); | 
|  | 240 | mSensorList.clear(); | 
|  | 241 |  | 
|  | 242 | if (connectHidlServiceV2_0() == HalConnectionStatus::CONNECTED) { | 
|  | 243 | initializeSensorList(); | 
|  | 244 |  | 
|  | 245 | if (sensorHandlesChanged(previousSensorList, mSensorList)) { | 
|  | 246 | LOG_ALWAYS_FATAL("Sensor handles changed, cannot re-enable sensors."); | 
|  | 247 | } else { | 
|  | 248 | reactivateSensors(previousActivations); | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | mReconnecting = false; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | bool SensorDevice::sensorHandlesChanged(const Vector<sensor_t>& oldSensorList, | 
|  | 255 | const Vector<sensor_t>& newSensorList) { | 
|  | 256 | bool didChange = false; | 
|  | 257 |  | 
|  | 258 | if (oldSensorList.size() != newSensorList.size()) { | 
|  | 259 | didChange = true; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | for (size_t i = 0; i < newSensorList.size() && !didChange; i++) { | 
|  | 263 | bool found = false; | 
|  | 264 | const sensor_t& newSensor = newSensorList[i]; | 
|  | 265 | for (size_t j = 0; j < oldSensorList.size() && !found; j++) { | 
|  | 266 | const sensor_t& prevSensor = oldSensorList[j]; | 
|  | 267 | if (prevSensor.handle == newSensor.handle) { | 
|  | 268 | found = true; | 
|  | 269 | if (!sensorIsEquivalent(prevSensor, newSensor)) { | 
|  | 270 | didChange = true; | 
|  | 271 | } | 
|  | 272 | } | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | if (!found) { | 
|  | 276 | // Could not find the new sensor in the old list of sensors, the lists must | 
|  | 277 | // have changed. | 
|  | 278 | didChange = true; | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 | return didChange; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | bool SensorDevice::sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor) { | 
|  | 285 | bool equivalent = true; | 
|  | 286 | if (prevSensor.handle != newSensor.handle || | 
|  | 287 | (strcmp(prevSensor.vendor, newSensor.vendor) != 0) || | 
|  | 288 | (strcmp(prevSensor.stringType, newSensor.stringType) != 0) || | 
|  | 289 | (strcmp(prevSensor.requiredPermission, newSensor.requiredPermission) != 0) || | 
|  | 290 | (prevSensor.version != newSensor.version) || | 
|  | 291 | (prevSensor.type != newSensor.type) || | 
|  | 292 | (std::abs(prevSensor.maxRange - newSensor.maxRange) > 0.001f) || | 
|  | 293 | (std::abs(prevSensor.resolution - newSensor.resolution) > 0.001f) || | 
|  | 294 | (std::abs(prevSensor.power - newSensor.power) > 0.001f) || | 
|  | 295 | (prevSensor.minDelay != newSensor.minDelay) || | 
|  | 296 | (prevSensor.fifoReservedEventCount != newSensor.fifoReservedEventCount) || | 
|  | 297 | (prevSensor.fifoMaxEventCount != newSensor.fifoMaxEventCount) || | 
|  | 298 | (prevSensor.maxDelay != newSensor.maxDelay) || | 
|  | 299 | (prevSensor.flags != newSensor.flags)) { | 
|  | 300 | equivalent = false; | 
|  | 301 | } | 
|  | 302 | return equivalent; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | void SensorDevice::reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations) { | 
|  | 306 | for (size_t i = 0; i < mSensorList.size(); i++) { | 
|  | 307 | int handle = mSensorList[i].handle; | 
|  | 308 | ssize_t activationIndex = previousActivations.indexOfKey(handle); | 
|  | 309 | if (activationIndex < 0 || previousActivations[activationIndex].numActiveClients() <= 0) { | 
|  | 310 | continue; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | const Info& info = previousActivations[activationIndex]; | 
|  | 314 | for (size_t j = 0; j < info.batchParams.size(); j++) { | 
|  | 315 | const BatchParams& batchParams = info.batchParams[j]; | 
|  | 316 | status_t res = batchLocked(info.batchParams.keyAt(j), handle, 0 /* flags */, | 
|  | 317 | batchParams.mTSample, batchParams.mTBatch); | 
|  | 318 |  | 
|  | 319 | if (res == NO_ERROR) { | 
|  | 320 | activateLocked(info.batchParams.keyAt(j), handle, true /* enabled */); | 
|  | 321 | } | 
|  | 322 | } | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 326 | void SensorDevice::handleDynamicSensorConnection(int handle, bool connected) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 327 | // 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] | 328 | if (connected) { | 
|  | 329 | Info model; | 
|  | 330 | mActivationCount.add(handle, model); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 331 | checkReturn(mSensors->activate(handle, 0 /* enabled */)); | 
| Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 332 | } else { | 
|  | 333 | mActivationCount.removeItem(handle); | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 337 | std::string SensorDevice::dump() const { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 338 | if (mSensors == nullptr) return "HAL not initialized\n"; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 339 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 340 | String8 result; | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 341 | result.appendFormat("Total %zu h/w sensors, %zu running:\n", | 
|  | 342 | mSensorList.size(), mActivationCount.size()); | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 343 |  | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 344 | Mutex::Autolock _l(mLock); | 
|  | 345 | for (const auto & s : mSensorList) { | 
|  | 346 | int32_t handle = s.handle; | 
|  | 347 | const Info& info = mActivationCount.valueFor(handle); | 
|  | 348 | if (info.batchParams.isEmpty()) continue; | 
|  | 349 |  | 
|  | 350 | result.appendFormat("0x%08x) active-count = %zu; ", handle, info.batchParams.size()); | 
|  | 351 |  | 
|  | 352 | result.append("sampling_period(ms) = {"); | 
|  | 353 | for (size_t j = 0; j < info.batchParams.size(); j++) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 354 | const BatchParams& params = info.batchParams[j]; | 
|  | 355 | result.appendFormat("%.1f%s", params.mTSample / 1e6f, | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 356 | j < info.batchParams.size() - 1 ? ", " : ""); | 
|  | 357 | } | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 358 | result.appendFormat("}, selected = %.2f ms; ", info.bestBatchParams.mTSample / 1e6f); | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 359 |  | 
|  | 360 | result.append("batching_period(ms) = {"); | 
|  | 361 | for (size_t j = 0; j < info.batchParams.size(); j++) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 362 | const BatchParams& params = info.batchParams[j]; | 
|  | 363 | result.appendFormat("%.1f%s", params.mTBatch / 1e6f, | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 364 | j < info.batchParams.size() - 1 ? ", " : ""); | 
|  | 365 | } | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 366 | result.appendFormat("}, selected = %.2f ms\n", info.bestBatchParams.mTBatch / 1e6f); | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 369 | return result.string(); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
|  | 372 | ssize_t SensorDevice::getSensorList(sensor_t const** list) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 373 | *list = &mSensorList[0]; | 
|  | 374 |  | 
|  | 375 | return mSensorList.size(); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
|  | 378 | status_t SensorDevice::initCheck() const { | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 379 | return mSensors != nullptr ? NO_ERROR : NO_INIT; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
|  | 382 | ssize_t SensorDevice::poll(sensors_event_t* buffer, size_t count) { | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 383 | if (mSensors == nullptr) return NO_INIT; | 
|  | 384 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 385 | ssize_t eventsRead = 0; | 
|  | 386 | if (mSensors->supportsMessageQueues()) { | 
|  | 387 | eventsRead = pollFmq(buffer, count); | 
|  | 388 | } else if (mSensors->supportsPolling()) { | 
|  | 389 | eventsRead = pollHal(buffer, count); | 
|  | 390 | } else { | 
|  | 391 | ALOGE("Must support polling or FMQ"); | 
|  | 392 | eventsRead = -1; | 
|  | 393 | } | 
|  | 394 | return eventsRead; | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | ssize_t SensorDevice::pollHal(sensors_event_t* buffer, size_t count) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 398 | ssize_t err; | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 399 | int numHidlTransportErrors = 0; | 
|  | 400 | bool hidlTransportError = false; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 401 |  | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 402 | do { | 
|  | 403 | auto ret = mSensors->poll( | 
|  | 404 | count, | 
|  | 405 | [&](auto result, | 
|  | 406 | const auto &events, | 
|  | 407 | const auto &dynamicSensorsAdded) { | 
|  | 408 | if (result == Result::OK) { | 
|  | 409 | convertToSensorEvents(events, dynamicSensorsAdded, buffer); | 
|  | 410 | err = (ssize_t)events.size(); | 
|  | 411 | } else { | 
|  | 412 | err = StatusFromResult(result); | 
|  | 413 | } | 
|  | 414 | }); | 
|  | 415 |  | 
|  | 416 | if (ret.isOk())  { | 
|  | 417 | hidlTransportError = false; | 
|  | 418 | } else { | 
|  | 419 | hidlTransportError = true; | 
|  | 420 | numHidlTransportErrors++; | 
|  | 421 | if (numHidlTransportErrors > 50) { | 
|  | 422 | // Log error and bail | 
|  | 423 | ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors); | 
|  | 424 | handleHidlDeath(ret.description()); | 
|  | 425 | } else { | 
|  | 426 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 | } while (hidlTransportError); | 
|  | 430 |  | 
|  | 431 | if(numHidlTransportErrors > 0) { | 
|  | 432 | ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors); | 
| Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 433 | HidlTransportErrorLog errLog(time(nullptr), numHidlTransportErrors); | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 434 | mHidlTransportErrors.add(errLog); | 
|  | 435 | mTotalHidlTransportErrors++; | 
|  | 436 | } | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 437 |  | 
|  | 438 | return err; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 439 | } | 
|  | 440 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 441 | ssize_t SensorDevice::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead) { | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 442 | ssize_t eventsRead = 0; | 
|  | 443 | size_t availableEvents = mEventQueue->availableToRead(); | 
|  | 444 |  | 
|  | 445 | if (availableEvents == 0) { | 
|  | 446 | uint32_t eventFlagState = 0; | 
|  | 447 |  | 
|  | 448 | // Wait for events to become available. This is necessary so that the Event FMQ's read() is | 
|  | 449 | // able to be called with the correct number of events to read. If the specified number of | 
|  | 450 | // events is not available, then read() would return no events, possibly introducing | 
|  | 451 | // additional latency in delivering events to applications. | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 452 | mEventQueueFlag->wait(asBaseType(EventQueueFlagBits::READ_AND_PROCESS) | | 
|  | 453 | asBaseType(INTERNAL_WAKE), &eventFlagState); | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 454 | availableEvents = mEventQueue->availableToRead(); | 
|  | 455 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 456 | if ((eventFlagState & asBaseType(EventQueueFlagBits::READ_AND_PROCESS)) && | 
|  | 457 | availableEvents == 0) { | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 458 | ALOGW("Event FMQ wake without any events"); | 
|  | 459 | } | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 460 |  | 
|  | 461 | if ((eventFlagState & asBaseType(INTERNAL_WAKE)) && mReconnecting) { | 
|  | 462 | ALOGD("Event FMQ internal wake, returning from poll with no events"); | 
|  | 463 | return DEAD_OBJECT; | 
|  | 464 | } | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
|  | 467 | size_t eventsToRead = std::min({availableEvents, maxNumEventsToRead, mEventBuffer.size()}); | 
|  | 468 | if (eventsToRead > 0) { | 
|  | 469 | if (mEventQueue->read(mEventBuffer.data(), eventsToRead)) { | 
|  | 470 | for (size_t i = 0; i < eventsToRead; i++) { | 
|  | 471 | convertToSensorEvent(mEventBuffer[i], &buffer[i]); | 
|  | 472 | } | 
|  | 473 | eventsRead = eventsToRead; | 
|  | 474 | } else { | 
|  | 475 | ALOGW("Failed to read %zu events, currently %zu events available", | 
|  | 476 | eventsToRead, availableEvents); | 
|  | 477 | } | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | return eventsRead; | 
|  | 481 | } | 
|  | 482 |  | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 483 | Return<void> SensorDevice::onDynamicSensorsConnected( | 
|  | 484 | const hidl_vec<SensorInfo> &dynamicSensorsAdded) { | 
|  | 485 | // Allocate a sensor_t structure for each dynamic sensor added and insert | 
|  | 486 | // it into the dictionary of connected dynamic sensors keyed by handle. | 
|  | 487 | for (size_t i = 0; i < dynamicSensorsAdded.size(); ++i) { | 
|  | 488 | const SensorInfo &info = dynamicSensorsAdded[i]; | 
|  | 489 |  | 
|  | 490 | auto it = mConnectedDynamicSensors.find(info.sensorHandle); | 
|  | 491 | CHECK(it == mConnectedDynamicSensors.end()); | 
|  | 492 |  | 
|  | 493 | sensor_t *sensor = new sensor_t(); | 
|  | 494 | convertToSensor(info, sensor); | 
|  | 495 |  | 
|  | 496 | mConnectedDynamicSensors.insert( | 
|  | 497 | std::make_pair(sensor->handle, sensor)); | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | return Return<void>(); | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | Return<void> SensorDevice::onDynamicSensorsDisconnected( | 
|  | 504 | const hidl_vec<int32_t> &dynamicSensorHandlesRemoved) { | 
|  | 505 | (void) dynamicSensorHandlesRemoved; | 
|  | 506 | // TODO: Currently dynamic sensors do not seem to be removed | 
|  | 507 | return Return<void>(); | 
|  | 508 | } | 
|  | 509 |  | 
| Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 510 | void SensorDevice::writeWakeLockHandled(uint32_t count) { | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 511 | if (mSensors != nullptr && mSensors->supportsMessageQueues() && | 
|  | 512 | !mWakeLockQueue->write(&count)) { | 
| Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 513 | ALOGW("Failed to write wake lock handled"); | 
|  | 514 | } | 
|  | 515 | } | 
|  | 516 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 517 | void SensorDevice::autoDisable(void *ident, int handle) { | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 518 | Mutex::Autolock _l(mLock); | 
| Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 519 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); | 
|  | 520 | if (activationIndex < 0) { | 
|  | 521 | ALOGW("Handle %d cannot be found in activation record", handle); | 
|  | 522 | return; | 
|  | 523 | } | 
|  | 524 | Info& info(mActivationCount.editValueAt(activationIndex)); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 525 | info.removeBatchParamsForIdent(ident); | 
| Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 526 | } | 
|  | 527 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 528 | status_t SensorDevice::activate(void* ident, int handle, int enabled) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 529 | if (mSensors == nullptr) return NO_INIT; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 530 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 531 | Mutex::Autolock _l(mLock); | 
|  | 532 | return activateLocked(ident, handle, enabled); | 
|  | 533 | } | 
|  | 534 |  | 
|  | 535 | status_t SensorDevice::activateLocked(void* ident, int handle, int enabled) { | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 536 | bool actuateHardware = false; | 
|  | 537 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 538 | status_t err(NO_ERROR); | 
|  | 539 |  | 
| Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 540 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); | 
|  | 541 | if (activationIndex < 0) { | 
|  | 542 | ALOGW("Handle %d cannot be found in activation record", handle); | 
|  | 543 | return BAD_VALUE; | 
|  | 544 | } | 
|  | 545 | Info& info(mActivationCount.editValueAt(activationIndex)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 546 |  | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 547 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 548 | "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%zu", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 549 | ident, handle, enabled, info.batchParams.size()); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 550 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 551 | if (enabled) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 552 | ALOGD_IF(DEBUG_CONNECTIONS, "enable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 553 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 554 | if (isClientDisabledLocked(ident)) { | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 555 | ALOGE("SensorDevice::activate, isClientDisabledLocked(%p):true, handle:%d", | 
|  | 556 | ident, handle); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 557 | return INVALID_OPERATION; | 
|  | 558 | } | 
|  | 559 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 560 | if (info.batchParams.indexOfKey(ident) >= 0) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 561 | if (info.numActiveClients() == 1) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 562 | // This is the first connection, we need to activate the underlying h/w sensor. | 
|  | 563 | actuateHardware = true; | 
|  | 564 | } | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 565 | } else { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 566 | // Log error. Every activate call should be preceded by a batch() call. | 
|  | 567 | ALOGE("\t >>>ERROR: activate called without batch"); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 568 | } | 
|  | 569 | } else { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 570 | ALOGD_IF(DEBUG_CONNECTIONS, "disable index=%zd", info.batchParams.indexOfKey(ident)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 571 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 572 | // If a connected dynamic sensor is deactivated, remove it from the | 
|  | 573 | // dictionary. | 
|  | 574 | auto it = mConnectedDynamicSensors.find(handle); | 
|  | 575 | if (it != mConnectedDynamicSensors.end()) { | 
|  | 576 | delete it->second; | 
|  | 577 | mConnectedDynamicSensors.erase(it); | 
|  | 578 | } | 
|  | 579 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 580 | if (info.removeBatchParamsForIdent(ident) >= 0) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 581 | if (info.numActiveClients() == 0) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 582 | // This is the last connection, we need to de-activate the underlying h/w sensor. | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 583 | actuateHardware = true; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 584 | } else { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 585 | // Call batch for this sensor with the previously calculated best effort | 
|  | 586 | // batch_rate and timeout. One of the apps has unregistered for sensor | 
|  | 587 | // events, and the best effort batch parameters might have changed. | 
|  | 588 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 589 | "\t>>> actuating h/w batch 0x%08x %" PRId64 " %" PRId64, handle, | 
|  | 590 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 591 | checkReturn(mSensors->batch( | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 592 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch)); | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 593 | } | 
|  | 594 | } else { | 
|  | 595 | // sensor wasn't enabled for this ident | 
|  | 596 | } | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 597 |  | 
|  | 598 | if (isClientDisabledLocked(ident)) { | 
|  | 599 | return NO_ERROR; | 
|  | 600 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 601 | } | 
| Mathias Agopian | 50b6676 | 2010-11-29 17:26:51 -0800 | [diff] [blame] | 602 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 603 | if (actuateHardware) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 604 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w activate handle=%d enabled=%d", handle, | 
|  | 605 | enabled); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 606 | err = StatusFromResult(checkReturn(mSensors->activate(handle, enabled))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 607 | ALOGE_IF(err, "Error %s sensor %d (%s)", enabled ? "activating" : "disabling", handle, | 
|  | 608 | strerror(-err)); | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 609 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 610 | if (err != NO_ERROR && enabled) { | 
|  | 611 | // Failure when enabling the sensor. Clean up on failure. | 
|  | 612 | info.removeBatchParamsForIdent(ident); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 613 | } | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 614 | } | 
|  | 615 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 616 | return err; | 
|  | 617 | } | 
|  | 618 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 619 | status_t SensorDevice::batch( | 
|  | 620 | void* ident, | 
|  | 621 | int handle, | 
|  | 622 | int flags, | 
|  | 623 | int64_t samplingPeriodNs, | 
|  | 624 | int64_t maxBatchReportLatencyNs) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 625 | if (mSensors == nullptr) return NO_INIT; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 626 |  | 
|  | 627 | if (samplingPeriodNs < MINIMUM_EVENTS_PERIOD) { | 
|  | 628 | samplingPeriodNs = MINIMUM_EVENTS_PERIOD; | 
|  | 629 | } | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 630 | if (maxBatchReportLatencyNs < 0) { | 
|  | 631 | maxBatchReportLatencyNs = 0; | 
|  | 632 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 633 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 634 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 635 | "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] | 636 | ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 637 |  | 
|  | 638 | Mutex::Autolock _l(mLock); | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 639 | return batchLocked(ident, handle, flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | status_t SensorDevice::batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, | 
|  | 643 | int64_t maxBatchReportLatencyNs) { | 
| Peng Xu | 042baec | 2017-08-09 19:28:27 -0700 | [diff] [blame] | 644 | ssize_t activationIndex = mActivationCount.indexOfKey(handle); | 
|  | 645 | if (activationIndex < 0) { | 
|  | 646 | ALOGW("Handle %d cannot be found in activation record", handle); | 
|  | 647 | return BAD_VALUE; | 
|  | 648 | } | 
|  | 649 | Info& info(mActivationCount.editValueAt(activationIndex)); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 650 |  | 
|  | 651 | if (info.batchParams.indexOfKey(ident) < 0) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 652 | BatchParams params(samplingPeriodNs, maxBatchReportLatencyNs); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 653 | info.batchParams.add(ident, params); | 
|  | 654 | } else { | 
|  | 655 | // A batch has already been called with this ident. Update the batch parameters. | 
|  | 656 | info.setBatchParamsForIdent(ident, flags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 657 | } | 
|  | 658 |  | 
|  | 659 | BatchParams prevBestBatchParams = info.bestBatchParams; | 
|  | 660 | // Find the minimum of all timeouts and batch_rates for this sensor. | 
|  | 661 | info.selectBatchParams(); | 
|  | 662 |  | 
|  | 663 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 664 | "\t>>> curr_period=%" PRId64 " min_period=%" PRId64 | 
|  | 665 | " curr_timeout=%" PRId64 " min_timeout=%" PRId64, | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 666 | prevBestBatchParams.mTSample, info.bestBatchParams.mTSample, | 
|  | 667 | prevBestBatchParams.mTBatch, info.bestBatchParams.mTBatch); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 668 |  | 
|  | 669 | status_t err(NO_ERROR); | 
|  | 670 | // If the min period or min timeout has changed since the last batch call, call batch. | 
|  | 671 | if (prevBestBatchParams != info.bestBatchParams) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 672 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w BATCH 0x%08x %" PRId64 " %" PRId64, handle, | 
|  | 673 | info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 674 | err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 675 | checkReturn(mSensors->batch( | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 676 | handle, info.bestBatchParams.mTSample, info.bestBatchParams.mTBatch))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 677 | if (err != NO_ERROR) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 678 | ALOGE("sensor batch failed %p 0x%08x %" PRId64 " %" PRId64 " err=%s", | 
|  | 679 | mSensors.get(), handle, info.bestBatchParams.mTSample, | 
|  | 680 | info.bestBatchParams.mTBatch, strerror(-err)); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 681 | info.removeBatchParamsForIdent(ident); | 
|  | 682 | } | 
|  | 683 | } | 
|  | 684 | return err; | 
|  | 685 | } | 
|  | 686 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 687 | status_t SensorDevice::setDelay(void* ident, int handle, int64_t samplingPeriodNs) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 688 | return batch(ident, handle, 0, samplingPeriodNs, 0); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 689 | } | 
|  | 690 |  | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 691 | int SensorDevice::getHalDeviceVersion() const { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 692 | if (mSensors == nullptr) return -1; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 693 | return SENSORS_DEVICE_API_VERSION_1_4; | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 694 | } | 
|  | 695 |  | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 696 | status_t SensorDevice::flush(void* ident, int handle) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 697 | if (mSensors == nullptr) return NO_INIT; | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 698 | if (isClientDisabled(ident)) return INVALID_OPERATION; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 699 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w flush %d", handle); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 700 | return StatusFromResult(checkReturn(mSensors->flush(handle))); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 701 | } | 
|  | 702 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 703 | bool SensorDevice::isClientDisabled(void* ident) { | 
|  | 704 | Mutex::Autolock _l(mLock); | 
|  | 705 | return isClientDisabledLocked(ident); | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | bool SensorDevice::isClientDisabledLocked(void* ident) { | 
|  | 709 | return mDisabledClients.indexOf(ident) >= 0; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | void SensorDevice::enableAllSensors() { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 713 | if (mSensors == nullptr) return; | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 714 | Mutex::Autolock _l(mLock); | 
|  | 715 | mDisabledClients.clear(); | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 716 | ALOGI("cleared mDisabledClients"); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 717 | for (size_t i = 0; i< mActivationCount.size(); ++i) { | 
|  | 718 | Info& info = mActivationCount.editValueAt(i); | 
|  | 719 | if (info.batchParams.isEmpty()) continue; | 
|  | 720 | info.selectBatchParams(); | 
|  | 721 | const int sensor_handle = mActivationCount.keyAt(i); | 
|  | 722 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> reenable actuating h/w sensor enable handle=%d ", | 
|  | 723 | sensor_handle); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 724 | status_t err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 725 | checkReturn(mSensors->batch( | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 726 | sensor_handle, | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 727 | info.bestBatchParams.mTSample, | 
|  | 728 | info.bestBatchParams.mTBatch))); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 729 | 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] | 730 |  | 
|  | 731 | if (err == NO_ERROR) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 732 | err = StatusFromResult( | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 733 | checkReturn(mSensors->activate(sensor_handle, 1 /* enabled */))); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 734 | ALOGE_IF(err, "Error activating sensor %d (%s)", sensor_handle, strerror(-err)); | 
|  | 735 | } | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 736 | } | 
|  | 737 | } | 
|  | 738 |  | 
|  | 739 | void SensorDevice::disableAllSensors() { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 740 | if (mSensors == nullptr) return; | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 741 | Mutex::Autolock _l(mLock); | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 742 | for (size_t i = 0; i< mActivationCount.size(); ++i) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 743 | const Info& info = mActivationCount.valueAt(i); | 
|  | 744 | // Check if this sensor has been activated previously and disable it. | 
|  | 745 | if (info.batchParams.size() > 0) { | 
|  | 746 | const int sensor_handle = mActivationCount.keyAt(i); | 
|  | 747 | ALOGD_IF(DEBUG_CONNECTIONS, "\t>> actuating h/w sensor disable handle=%d ", | 
|  | 748 | sensor_handle); | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 749 | checkReturn(mSensors->activate(sensor_handle, 0 /* enabled */)); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 750 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 751 | // Add all the connections that were registered for this sensor to the disabled | 
|  | 752 | // clients list. | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 753 | for (size_t j = 0; j < info.batchParams.size(); ++j) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 754 | mDisabledClients.add(info.batchParams.keyAt(j)); | 
| Peng Xu | 966fa88 | 2016-09-01 16:13:15 -0700 | [diff] [blame] | 755 | ALOGI("added %p to mDisabledClients", info.batchParams.keyAt(j)); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 756 | } | 
|  | 757 | } | 
|  | 758 | } | 
|  | 759 | } | 
|  | 760 |  | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 761 | status_t SensorDevice::injectSensorData( | 
|  | 762 | const sensors_event_t *injected_sensor_event) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 763 | if (mSensors == nullptr) return NO_INIT; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 764 | ALOGD_IF(DEBUG_CONNECTIONS, | 
|  | 765 | "sensor_event handle=%d ts=%" PRId64 " data=%.2f, %.2f, %.2f %.2f %.2f %.2f", | 
|  | 766 | injected_sensor_event->sensor, | 
|  | 767 | injected_sensor_event->timestamp, injected_sensor_event->data[0], | 
|  | 768 | injected_sensor_event->data[1], injected_sensor_event->data[2], | 
|  | 769 | injected_sensor_event->data[3], injected_sensor_event->data[4], | 
|  | 770 | injected_sensor_event->data[5]); | 
|  | 771 |  | 
|  | 772 | Event ev; | 
|  | 773 | convertFromSensorEvent(*injected_sensor_event, &ev); | 
|  | 774 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 775 | return StatusFromResult(checkReturn(mSensors->injectSensorData(ev))); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 776 | } | 
|  | 777 |  | 
|  | 778 | status_t SensorDevice::setMode(uint32_t mode) { | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 779 | if (mSensors == nullptr) return NO_INIT; | 
|  | 780 | return StatusFromResult( | 
|  | 781 | checkReturn(mSensors->setOperationMode( | 
|  | 782 | static_cast<hardware::sensors::V1_0::OperationMode>(mode)))); | 
|  | 783 | } | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 784 |  | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 785 | int32_t SensorDevice::registerDirectChannel(const sensors_direct_mem_t* memory) { | 
|  | 786 | if (mSensors == nullptr) return NO_INIT; | 
|  | 787 | Mutex::Autolock _l(mLock); | 
|  | 788 |  | 
|  | 789 | SharedMemType type; | 
|  | 790 | switch (memory->type) { | 
|  | 791 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: | 
|  | 792 | type = SharedMemType::ASHMEM; | 
|  | 793 | break; | 
|  | 794 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: | 
|  | 795 | type = SharedMemType::GRALLOC; | 
|  | 796 | break; | 
|  | 797 | default: | 
|  | 798 | return BAD_VALUE; | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | SharedMemFormat format; | 
|  | 802 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { | 
|  | 803 | return BAD_VALUE; | 
|  | 804 | } | 
|  | 805 | format = SharedMemFormat::SENSORS_EVENT; | 
|  | 806 |  | 
|  | 807 | SharedMemInfo mem = { | 
|  | 808 | .type = type, | 
|  | 809 | .format = format, | 
|  | 810 | .size = static_cast<uint32_t>(memory->size), | 
|  | 811 | .memoryHandle = memory->handle, | 
|  | 812 | }; | 
|  | 813 |  | 
|  | 814 | int32_t ret; | 
|  | 815 | checkReturn(mSensors->registerDirectChannel(mem, | 
|  | 816 | [&ret](auto result, auto channelHandle) { | 
|  | 817 | if (result == Result::OK) { | 
|  | 818 | ret = channelHandle; | 
|  | 819 | } else { | 
|  | 820 | ret = StatusFromResult(result); | 
|  | 821 | } | 
|  | 822 | })); | 
|  | 823 | return ret; | 
|  | 824 | } | 
|  | 825 |  | 
|  | 826 | void SensorDevice::unregisterDirectChannel(int32_t channelHandle) { | 
|  | 827 | if (mSensors == nullptr) return; | 
|  | 828 | Mutex::Autolock _l(mLock); | 
|  | 829 | checkReturn(mSensors->unregisterDirectChannel(channelHandle)); | 
|  | 830 | } | 
|  | 831 |  | 
|  | 832 | int32_t SensorDevice::configureDirectChannel(int32_t sensorHandle, | 
|  | 833 | int32_t channelHandle, const struct sensors_direct_cfg_t *config) { | 
|  | 834 | if (mSensors == nullptr) return NO_INIT; | 
|  | 835 | Mutex::Autolock _l(mLock); | 
|  | 836 |  | 
|  | 837 | RateLevel rate; | 
|  | 838 | switch(config->rate_level) { | 
|  | 839 | case SENSOR_DIRECT_RATE_STOP: | 
|  | 840 | rate = RateLevel::STOP; | 
|  | 841 | break; | 
|  | 842 | case SENSOR_DIRECT_RATE_NORMAL: | 
|  | 843 | rate = RateLevel::NORMAL; | 
|  | 844 | break; | 
|  | 845 | case SENSOR_DIRECT_RATE_FAST: | 
|  | 846 | rate = RateLevel::FAST; | 
|  | 847 | break; | 
|  | 848 | case SENSOR_DIRECT_RATE_VERY_FAST: | 
|  | 849 | rate = RateLevel::VERY_FAST; | 
|  | 850 | break; | 
|  | 851 | default: | 
|  | 852 | return BAD_VALUE; | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | int32_t ret; | 
|  | 856 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, | 
|  | 857 | [&ret, rate] (auto result, auto token) { | 
|  | 858 | if (rate == RateLevel::STOP) { | 
|  | 859 | ret = StatusFromResult(result); | 
|  | 860 | } else { | 
|  | 861 | if (result == Result::OK) { | 
|  | 862 | ret = token; | 
|  | 863 | } else { | 
|  | 864 | ret = StatusFromResult(result); | 
|  | 865 | } | 
|  | 866 | } | 
|  | 867 | })); | 
|  | 868 |  | 
|  | 869 | return ret; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 870 | } | 
|  | 871 |  | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 872 | // --------------------------------------------------------------------------- | 
|  | 873 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 874 | int SensorDevice::Info::numActiveClients() const { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 875 | SensorDevice& device(SensorDevice::getInstance()); | 
|  | 876 | int num = 0; | 
|  | 877 | for (size_t i = 0; i < batchParams.size(); ++i) { | 
|  | 878 | if (!device.isClientDisabledLocked(batchParams.keyAt(i))) { | 
|  | 879 | ++num; | 
|  | 880 | } | 
|  | 881 | } | 
|  | 882 | return num; | 
|  | 883 | } | 
|  | 884 |  | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 885 | status_t SensorDevice::Info::setBatchParamsForIdent(void* ident, int, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 886 | int64_t samplingPeriodNs, | 
|  | 887 | int64_t maxBatchReportLatencyNs) { | 
|  | 888 | ssize_t index = batchParams.indexOfKey(ident); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 889 | if (index < 0) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 890 | ALOGE("Info::setBatchParamsForIdent(ident=%p, period_ns=%" PRId64 | 
|  | 891 | " timeout=%" PRId64 ") failed (%s)", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 892 | ident, samplingPeriodNs, maxBatchReportLatencyNs, strerror(-index)); | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 893 | return BAD_INDEX; | 
|  | 894 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 895 | BatchParams& params = batchParams.editValueAt(index); | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 896 | params.mTSample = samplingPeriodNs; | 
|  | 897 | params.mTBatch = maxBatchReportLatencyNs; | 
| Mathias Agopian | 667102f | 2011-09-14 16:43:34 -0700 | [diff] [blame] | 898 | return NO_ERROR; | 
|  | 899 | } | 
|  | 900 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 901 | void SensorDevice::Info::selectBatchParams() { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 902 | BatchParams bestParams; // default to max Tsample and max Tbatch | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 903 | SensorDevice& device(SensorDevice::getInstance()); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 904 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 905 | for (size_t i = 0; i < batchParams.size(); ++i) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 906 | if (device.isClientDisabledLocked(batchParams.keyAt(i))) { | 
|  | 907 | continue; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 908 | } | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 909 | bestParams.merge(batchParams[i]); | 
|  | 910 | } | 
|  | 911 | // if mTBatch <= mTSample, it is in streaming mode. set mTbatch to 0 to demand this explicitly. | 
|  | 912 | if (bestParams.mTBatch <= bestParams.mTSample) { | 
|  | 913 | bestParams.mTBatch = 0; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 914 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 915 | bestBatchParams = bestParams; | 
|  | 916 | } | 
|  | 917 |  | 
|  | 918 | ssize_t SensorDevice::Info::removeBatchParamsForIdent(void* ident) { | 
|  | 919 | ssize_t idx = batchParams.removeItem(ident); | 
|  | 920 | if (idx >= 0) { | 
|  | 921 | selectBatchParams(); | 
|  | 922 | } | 
|  | 923 | return idx; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 924 | } | 
|  | 925 |  | 
| Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 926 | void SensorDevice::notifyConnectionDestroyed(void* ident) { | 
|  | 927 | Mutex::Autolock _l(mLock); | 
|  | 928 | mDisabledClients.remove(ident); | 
|  | 929 | } | 
|  | 930 |  | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 931 | bool SensorDevice::isDirectReportSupported() const { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 932 | return mIsDirectReportSupported; | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 933 | } | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 934 |  | 
|  | 935 | void SensorDevice::convertToSensorEvent( | 
|  | 936 | const Event &src, sensors_event_t *dst) { | 
|  | 937 | ::android::hardware::sensors::V1_0::implementation::convertToSensorEvent( | 
|  | 938 | src, dst); | 
|  | 939 |  | 
|  | 940 | if (src.sensorType == SensorType::DYNAMIC_SENSOR_META) { | 
|  | 941 | const DynamicSensorInfo &dyn = src.u.dynamic; | 
|  | 942 |  | 
|  | 943 | dst->dynamic_sensor_meta.connected = dyn.connected; | 
|  | 944 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; | 
|  | 945 | if (dyn.connected) { | 
|  | 946 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); | 
|  | 947 | CHECK(it != mConnectedDynamicSensors.end()); | 
|  | 948 |  | 
|  | 949 | dst->dynamic_sensor_meta.sensor = it->second; | 
|  | 950 |  | 
|  | 951 | memcpy(dst->dynamic_sensor_meta.uuid, | 
|  | 952 | dyn.uuid.data(), | 
|  | 953 | sizeof(dst->dynamic_sensor_meta.uuid)); | 
|  | 954 | } | 
|  | 955 | } | 
|  | 956 | } | 
|  | 957 |  | 
|  | 958 | void SensorDevice::convertToSensorEvents( | 
|  | 959 | const hidl_vec<Event> &src, | 
|  | 960 | const hidl_vec<SensorInfo> &dynamicSensorsAdded, | 
|  | 961 | sensors_event_t *dst) { | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 962 |  | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 963 | if (dynamicSensorsAdded.size() > 0) { | 
|  | 964 | onDynamicSensorsConnected(dynamicSensorsAdded); | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 965 | } | 
|  | 966 |  | 
|  | 967 | for (size_t i = 0; i < src.size(); ++i) { | 
|  | 968 | convertToSensorEvent(src[i], &dst[i]); | 
|  | 969 | } | 
|  | 970 | } | 
|  | 971 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 972 | void SensorDevice::handleHidlDeath(const std::string & detail) { | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 973 | if (!SensorDevice::getInstance().mSensors->supportsMessageQueues()) { | 
|  | 974 | // restart is the only option at present. | 
|  | 975 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); | 
|  | 976 | } else { | 
|  | 977 | ALOGD("ISensors HAL died, death recipient will attempt reconnect"); | 
|  | 978 | } | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 979 | } | 
|  | 980 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 981 | // --------------------------------------------------------------------------- | 
|  | 982 | }; // namespace android |