Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include "SubHal.h" |
| 20 | |
| 21 | #include <android/hardware/sensors/2.0/ISensors.h> |
| 22 | #include <fmq/MessageQueue.h> |
| 23 | #include <hardware_legacy/power.h> |
| 24 | #include <hidl/MQDescriptor.h> |
| 25 | #include <hidl/Status.h> |
| 26 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 27 | #include <map> |
| 28 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace sensors { |
| 32 | namespace V2_0 { |
| 33 | namespace implementation { |
| 34 | |
| 35 | using ::android::sp; |
| 36 | using ::android::hardware::EventFlag; |
| 37 | using ::android::hardware::hidl_string; |
| 38 | using ::android::hardware::hidl_vec; |
| 39 | using ::android::hardware::MessageQueue; |
| 40 | using ::android::hardware::MQDescriptor; |
| 41 | using ::android::hardware::Return; |
| 42 | using ::android::hardware::Void; |
| 43 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame^] | 44 | class HalProxy : public ISensors, public IScopedWakelockRefCounter { |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 45 | public: |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 46 | using Event = ::android::hardware::sensors::V1_0::Event; |
| 47 | using OperationMode = ::android::hardware::sensors::V1_0::OperationMode; |
| 48 | using RateLevel = ::android::hardware::sensors::V1_0::RateLevel; |
| 49 | using Result = ::android::hardware::sensors::V1_0::Result; |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 50 | using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 51 | using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo; |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 52 | using ISensorsSubHal = ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 53 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 54 | explicit HalProxy(); |
| 55 | // Test only constructor. |
| 56 | explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 57 | ~HalProxy(); |
| 58 | |
| 59 | // Methods from ::android::hardware::sensors::V2_0::ISensors follow. |
| 60 | Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override; |
| 61 | |
| 62 | Return<Result> setOperationMode(OperationMode mode) override; |
| 63 | |
| 64 | Return<Result> activate(int32_t sensorHandle, bool enabled) override; |
| 65 | |
| 66 | Return<Result> initialize( |
| 67 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 68 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 69 | const sp<ISensorsCallback>& sensorsCallback) override; |
| 70 | |
| 71 | Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 72 | int64_t maxReportLatencyNs) override; |
| 73 | |
| 74 | Return<Result> flush(int32_t sensorHandle) override; |
| 75 | |
| 76 | Return<Result> injectSensorData(const Event& event) override; |
| 77 | |
| 78 | Return<void> registerDirectChannel(const SharedMemInfo& mem, |
| 79 | registerDirectChannel_cb _hidl_cb) override; |
| 80 | |
| 81 | Return<Result> unregisterDirectChannel(int32_t channelHandle) override; |
| 82 | |
| 83 | Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, |
| 84 | configDirectReport_cb _hidl_cb) override; |
| 85 | |
| 86 | Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override; |
| 87 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 88 | // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 89 | // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework |
| 90 | // via the binder, these methods are invoked from a callback provided to sub-HALs inside the |
| 91 | // same process as the HalProxy, but potentially running on different threads. |
| 92 | Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded, |
| 93 | int32_t subHalIndex); |
| 94 | |
| 95 | Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved, |
| 96 | int32_t subHalIndex); |
| 97 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame^] | 98 | // Below methods follow IScopedWakelockRefCounter |
| 99 | |
| 100 | /** |
| 101 | * Increment ref count and maybe acquire wakelock. |
| 102 | */ |
| 103 | void incrementRefCountAndMaybeAcquireWakelock() override; |
| 104 | |
| 105 | /** |
| 106 | * Decrement ref count and maybe release wakelock. |
| 107 | */ |
| 108 | void decrementRefCountAndMaybeReleaseWakelock() override; |
| 109 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 110 | // Below methods are for HalProxyCallback |
| 111 | |
| 112 | /** |
| 113 | * Post events to the event message queue if there is room to write them. Otherwise post the |
| 114 | * remaining events to a background thread for a blocking write with a 5 second timeout. |
| 115 | * |
| 116 | * @param events The list of events to post to the message queue. |
| 117 | */ |
| 118 | void postEventsToMessageQueue(const std::vector<Event>& events); |
| 119 | |
| 120 | /** |
| 121 | * Get the SensorInfo object associated with the sensorHandle. |
| 122 | * |
| 123 | * @param sensorHandle The sensorHandle for the sensor. |
| 124 | * |
| 125 | * @return The sensor info for the sensor. |
| 126 | */ |
| 127 | const SensorInfo& getSensorInfo(uint32_t sensorHandle) const { |
| 128 | return mSensors.at(sensorHandle); |
| 129 | } |
| 130 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 131 | private: |
| 132 | using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>; |
| 133 | using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>; |
| 134 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame^] | 135 | const char* kWakeLockName = "SensorsHAL_WAKEUP"; |
| 136 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 137 | /** |
| 138 | * The Event FMQ where sensor events are written |
| 139 | */ |
| 140 | std::unique_ptr<EventMessageQueue> mEventQueue; |
| 141 | |
| 142 | /** |
| 143 | * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events |
| 144 | */ |
| 145 | std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue; |
| 146 | |
| 147 | /** |
| 148 | * Event Flag to signal to the framework when sensor events are available to be read |
| 149 | */ |
| 150 | EventFlag* mEventQueueFlag; |
| 151 | |
| 152 | /** |
| 153 | * Callback to the sensors framework to inform it that new sensors have been added or removed. |
| 154 | */ |
| 155 | sp<ISensorsCallback> mDynamicSensorsCallback; |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * SubHal object pointers that have been saved from vendor dynamic libraries. |
| 159 | */ |
| 160 | std::vector<ISensorsSubHal*> mSubHalList; |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 161 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 162 | std::vector<const sp<IHalProxyCallback>> mSubHalCallbacks; |
| 163 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 164 | /** |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 165 | * Map of sensor handles to SensorInfo objects that contains the sensor info from subhals as |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 166 | * well as the modified sensor handle for the framework. |
| 167 | * |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 168 | * The subhal index is encoded in the first byte of the sensor handle and the remaining |
| 169 | * bytes are generated by the subhal to identify the sensor. |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 170 | */ |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 171 | std::map<uint32_t, SensorInfo> mSensors; |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 172 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 173 | //! The current operation mode for all subhals. |
| 174 | OperationMode mCurrentOperationMode = OperationMode::NORMAL; |
| 175 | |
| 176 | //! The single subHal that supports directChannel reporting. |
| 177 | ISensorsSubHal* mDirectChannelSubHal = nullptr; |
| 178 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 179 | //! The mutex for the event queue. |
| 180 | std::mutex mEventQueueMutex; |
| 181 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame^] | 182 | //! The scoped wakelock ref count. |
| 183 | size_t mWakelockRefCount = 0; |
| 184 | |
| 185 | //! The mutex guarding the mWakelockRefCount variable |
| 186 | std::mutex mWakelockRefCountMutex; |
| 187 | |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 188 | //! The bit mask used to get the subhal index from a sensor handle. |
| 189 | static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000; |
| 190 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Initialize the list of SubHal objects in mSubHalList by reading from dynamic libraries |
| 193 | * listed in a config file. |
| 194 | */ |
| 195 | void initializeSubHalListFromConfigFile(const char* configFileName); |
| 196 | |
| 197 | /** |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 198 | * Initialize the HalProxyCallback vector using the list of subhals. |
| 199 | */ |
| 200 | void initializeSubHalCallbacks(); |
| 201 | |
| 202 | /** |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 203 | * Initialize the list of SensorInfo objects in mSensorList by getting sensors from each |
| 204 | * subhal. |
| 205 | */ |
| 206 | void initializeSensorList(); |
| 207 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 208 | /** |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 209 | * Calls the above two helper methods which are shared in both ctors. |
| 210 | */ |
| 211 | void initializeSubHalCallbacksAndSensorList(); |
| 212 | |
| 213 | /** |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 214 | * Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel |
| 215 | * subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first |
| 216 | * direct channel enabled sensor seen. |
| 217 | * |
| 218 | * @param sensorInfo The SensorInfo object that may be altered to have direct channel support |
| 219 | * disabled. |
| 220 | * @param subHal The subhal pointer that the current sensorInfo object came from. |
| 221 | */ |
| 222 | void setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal); |
| 223 | |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * Get the subhal pointer which can be found by indexing into the mSubHalList vector |
| 226 | * using the index from the first byte of sensorHandle. |
| 227 | * |
| 228 | * @param sensorHandle The handle used to identify a sensor in one of the subhals. |
| 229 | */ |
| 230 | ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle); |
| 231 | |
| 232 | /* |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 233 | * Clear out the subhal index bytes from a sensorHandle. |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 234 | * |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 235 | * @param sensorHandle The sensor handle to modify. |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 236 | * |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 237 | * @return The modified version of the sensor handle. |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 238 | */ |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 239 | static uint32_t clearSubHalIndex(uint32_t sensorHandle); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 240 | }; |
| 241 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Callback class used to provide the HalProxy with the index of which subHal is invoking |
| 244 | */ |
| 245 | class HalProxyCallback : public IHalProxyCallback { |
| 246 | using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo; |
| 247 | |
| 248 | public: |
| 249 | HalProxyCallback(HalProxy* halProxy, int32_t subHalIndex) |
| 250 | : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {} |
| 251 | |
| 252 | Return<void> onDynamicSensorsConnected( |
| 253 | const hidl_vec<SensorInfo>& dynamicSensorsAdded) override { |
| 254 | return mHalProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex); |
| 255 | } |
| 256 | |
| 257 | Return<void> onDynamicSensorsDisconnected( |
| 258 | const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override { |
| 259 | return mHalProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex); |
| 260 | } |
| 261 | |
| 262 | void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock); |
| 263 | |
| 264 | ScopedWakelock createScopedWakelock(bool lock); |
| 265 | |
| 266 | private: |
| 267 | HalProxy* mHalProxy; |
| 268 | int32_t mSubHalIndex; |
| 269 | |
| 270 | std::vector<Event> processEvents(const std::vector<Event>& events, |
| 271 | size_t* numWakeupEvents) const; |
| 272 | |
| 273 | uint32_t setSubHalIndex(uint32_t sensorHandle) const; |
| 274 | }; |
| 275 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 276 | } // namespace implementation |
| 277 | } // namespace V2_0 |
| 278 | } // namespace sensors |
| 279 | } // namespace hardware |
| 280 | } // namespace android |