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