Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #ifndef ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H |
| 19 | |
| 20 | #include "EventMessageQueueWrapper.h" |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 21 | #include "ISensorsWrapper.h" |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 22 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 23 | #include "android/hardware/sensors/1.0/ISensors.h" |
| 24 | #include "android/hardware/sensors/1.0/types.h" |
| 25 | #include "android/hardware/sensors/2.0/ISensors.h" |
| 26 | #include "android/hardware/sensors/2.0/ISensorsCallback.h" |
| 27 | #include "android/hardware/sensors/2.1/ISensors.h" |
| 28 | #include "android/hardware/sensors/2.1/ISensorsCallback.h" |
| 29 | #include "android/hardware/sensors/2.1/types.h" |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 30 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 31 | #include <utils/LightRefBase.h> |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 32 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 33 | #include <cassert> |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | namespace sensors { |
| 38 | namespace V2_1 { |
| 39 | namespace implementation { |
| 40 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 41 | using ::android::hardware::MessageQueue; |
| 42 | using ::android::hardware::MQDescriptorSync; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 43 | using ::android::hardware::Return; |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 44 | using ::android::hardware::sensors::V1_0::ISensors; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 45 | using ::android::hardware::sensors::V1_0::OperationMode; |
| 46 | using ::android::hardware::sensors::V1_0::RateLevel; |
| 47 | using ::android::hardware::sensors::V1_0::Result; |
| 48 | using ::android::hardware::sensors::V1_0::SharedMemInfo; |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 49 | using ::android::hardware::sensors::V2_1::Event; |
| 50 | using ::android::hardware::sensors::V2_1::ISensorsCallback; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 51 | |
| 52 | // TODO: Look into providing this as a param if it needs to be a different value |
| 53 | // than the framework. |
| 54 | static constexpr size_t MAX_RECEIVE_BUFFER_EVENT_COUNT = 256; |
| 55 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 56 | /* |
| 57 | * The ISensorsWrapper interface includes all function from supported Sensors HAL versions. This |
| 58 | * allows for the SensorDevice to use the ISensorsWrapper interface to interact with the Sensors |
| 59 | * HAL regardless of the current version of the Sensors HAL that is loaded. Each concrete |
| 60 | * instantiation of ISensorsWrapper must correspond to a specific Sensors HAL version. This design |
| 61 | * is beneficial because only the functions that change between Sensors HAL versions must be newly |
| 62 | * implemented, any previously implemented function that does not change may remain the same. |
| 63 | * |
| 64 | * Functions that exist across all versions of the Sensors HAL should be implemented as pure |
| 65 | * virtual functions which forces the concrete instantiations to implement the functions. |
| 66 | * |
| 67 | * Functions that do not exist across all versions of the Sensors HAL should include a default |
| 68 | * implementation that generates an error if called. The default implementation should never |
| 69 | * be called and must be overridden by Sensors HAL versions that support the function. |
| 70 | */ |
| 71 | class ISensorsWrapperBase : public VirtualLightRefBase { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 72 | public: |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 73 | virtual bool supportsPolling() const = 0; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 74 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 75 | virtual bool supportsMessageQueues() const = 0; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 76 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 77 | virtual void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient, |
| 78 | uint64_t cookie) = 0; |
| 79 | |
| 80 | virtual Return<void> getSensorsList( |
| 81 | ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) = 0; |
| 82 | |
| 83 | virtual Return<Result> setOperationMode(OperationMode mode) = 0; |
| 84 | |
| 85 | virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0; |
| 86 | |
| 87 | virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 88 | int64_t maxReportLatencyNs) = 0; |
| 89 | |
| 90 | virtual Return<Result> flush(int32_t sensorHandle) = 0; |
| 91 | |
| 92 | virtual Return<Result> injectSensorData(const Event& event) = 0; |
| 93 | |
| 94 | virtual Return<void> registerDirectChannel(const SharedMemInfo& mem, |
| 95 | ISensors::registerDirectChannel_cb _hidl_cb) = 0; |
| 96 | |
| 97 | virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0; |
| 98 | |
| 99 | virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, |
| 100 | RateLevel rate, |
| 101 | ISensors::configDirectReport_cb _hidl_cb) = 0; |
| 102 | |
| 103 | virtual Return<void> poll(int32_t /* maxCount */, ISensors::poll_cb /* _hidl_cb */) { |
| 104 | // Enforce this method is never invoked as it should be overridden if it's meant to be used. |
| 105 | assert(false); |
| 106 | return Return<void>(); |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 109 | virtual EventMessageQueueWrapperBase* getEventQueue() { return nullptr; } |
| 110 | |
| 111 | virtual Return<Result> initialize(const MQDescriptorSync<uint32_t>& /* wakeLockDesc */, |
| 112 | const ::android::sp<ISensorsCallback>& /* callback */) { |
| 113 | // Enforce this method is never invoked as it should be overridden if it's meant to be used. |
| 114 | assert(false); |
| 115 | return Result::INVALID_OPERATION; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 116 | } |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 117 | }; |
| 118 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 119 | template <typename T> |
| 120 | class SensorsWrapperBase : public ISensorsWrapperBase { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 121 | public: |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 122 | SensorsWrapperBase(sp<T> sensors) : mSensors(sensors){}; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 123 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 124 | void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient, |
| 125 | uint64_t cookie) override { |
| 126 | mSensors->linkToDeath(deathRecipient, cookie); |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 129 | virtual Return<void> getSensorsList( |
| 130 | ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override { |
| 131 | return mSensors->getSensorsList( |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 132 | [&](const auto& list) { _hidl_cb(convertToNewSensorInfos(list)); }); |
| 133 | } |
| 134 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 135 | Return<Result> setOperationMode(OperationMode mode) override { |
| 136 | return mSensors->setOperationMode(mode); |
| 137 | } |
| 138 | |
| 139 | Return<Result> activate(int32_t sensorHandle, bool enabled) override { |
| 140 | return mSensors->activate(sensorHandle, enabled); |
| 141 | } |
| 142 | |
| 143 | Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 144 | int64_t maxReportLatencyNs) override { |
| 145 | return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs); |
| 146 | } |
| 147 | |
| 148 | Return<Result> flush(int32_t sensorHandle) override { return mSensors->flush(sensorHandle); } |
| 149 | |
| 150 | virtual Return<Result> injectSensorData(const Event& event) override { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 151 | return mSensors->injectSensorData(convertToOldEvent(event)); |
| 152 | } |
| 153 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 154 | Return<void> registerDirectChannel(const SharedMemInfo& mem, |
| 155 | ISensors::registerDirectChannel_cb _hidl_cb) override { |
| 156 | return mSensors->registerDirectChannel(mem, _hidl_cb); |
| 157 | } |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 158 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 159 | Return<Result> unregisterDirectChannel(int32_t channelHandle) override { |
| 160 | return mSensors->unregisterDirectChannel(channelHandle); |
| 161 | } |
| 162 | |
| 163 | Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, |
| 164 | ISensors::configDirectReport_cb _hidl_cb) override { |
| 165 | return mSensors->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb); |
| 166 | } |
| 167 | |
| 168 | protected: |
| 169 | sp<T> mSensors; |
| 170 | }; |
| 171 | |
| 172 | class ISensorsWrapperV1_0 : public SensorsWrapperBase<hardware::sensors::V1_0::ISensors> { |
| 173 | public: |
| 174 | ISensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors) |
| 175 | : SensorsWrapperBase(sensors){}; |
| 176 | |
| 177 | bool supportsPolling() const override { return true; } |
| 178 | |
| 179 | bool supportsMessageQueues() const override { return false; } |
| 180 | |
| 181 | Return<void> poll(int32_t maxCount, |
| 182 | hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb) override { |
| 183 | return mSensors->poll(maxCount, _hidl_cb); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | class ISensorsWrapperV2_0 : public SensorsWrapperBase<hardware::sensors::V2_0::ISensors> { |
| 188 | public: |
| 189 | typedef MessageQueue<::android::hardware::sensors::V1_0::Event, |
| 190 | ::android::hardware::kSynchronizedReadWrite> |
| 191 | EventMessageQueue; |
| 192 | |
| 193 | ISensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors) |
| 194 | : SensorsWrapperBase(sensors) { |
| 195 | auto eventQueue = std::make_unique<EventMessageQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT, |
| 196 | true /* configureEventFlagWord */); |
| 197 | mEventQueue = std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue); |
| 198 | }; |
| 199 | |
| 200 | bool supportsPolling() const override { return false; } |
| 201 | |
| 202 | bool supportsMessageQueues() const override { return true; } |
| 203 | |
| 204 | EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); } |
| 205 | |
| 206 | Return<Result> initialize(const MQDescriptorSync<uint32_t>& wakeLockDesc, |
| 207 | const ::android::sp<ISensorsCallback>& callback) override { |
| 208 | return mSensors->initialize(*mEventQueue->getDesc(), wakeLockDesc, callback); |
| 209 | } |
| 210 | |
| 211 | private: |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 212 | std::unique_ptr<EventMessageQueueWrapperV1_0> mEventQueue; |
| 213 | }; |
| 214 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 215 | class ISensorsWrapperV2_1 : public SensorsWrapperBase<hardware::sensors::V2_1::ISensors> { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 216 | public: |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 217 | typedef MessageQueue<Event, ::android::hardware::kSynchronizedReadWrite> EventMessageQueueV2_1; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 218 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 219 | ISensorsWrapperV2_1(sp<hardware::sensors::V2_1::ISensors> sensors) |
| 220 | : SensorsWrapperBase(sensors) { |
| 221 | auto eventQueue = std::make_unique<EventMessageQueueV2_1>( |
| 222 | MAX_RECEIVE_BUFFER_EVENT_COUNT, true /* configureEventFlagWord */); |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 223 | mEventQueue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue); |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 224 | }; |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 225 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 226 | bool supportsPolling() const override { return false; } |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 227 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 228 | bool supportsMessageQueues() const override { return true; } |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 229 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 230 | EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); } |
| 231 | |
| 232 | Return<void> getSensorsList( |
| 233 | ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 234 | return mSensors->getSensorsList_2_1(_hidl_cb); |
| 235 | } |
| 236 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 237 | Return<Result> injectSensorData(const Event& event) override { |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 238 | return mSensors->injectSensorData_2_1(event); |
| 239 | } |
| 240 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 241 | Return<Result> initialize(const MQDescriptorSync<uint32_t>& wakeLockDesc, |
| 242 | const ::android::sp<ISensorsCallback>& callback) override { |
| 243 | return mSensors->initialize_2_1(*mEventQueue->getDesc(), wakeLockDesc, callback); |
| 244 | } |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 245 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 246 | private: |
Anthony Stange | c002dd9 | 2020-02-12 14:41:41 -0500 | [diff] [blame] | 247 | std::unique_ptr<EventMessageQueueWrapperV2_1> mEventQueue; |
| 248 | }; |
| 249 | |
| 250 | inline sp<ISensorsWrapperV2_0> wrapISensors(sp<V2_0::ISensors> sensors) { |
| 251 | return new ISensorsWrapperV2_0(sensors); |
| 252 | } |
| 253 | |
| 254 | inline sp<ISensorsWrapperV2_1> wrapISensors(sp<V2_1::ISensors> sensors) { |
| 255 | return new ISensorsWrapperV2_1(sensors); |
| 256 | } |
| 257 | |
| 258 | class NoOpSensorsCallback : public ISensorsCallback { |
| 259 | public: |
| 260 | Return<void> onDynamicSensorsConnected( |
| 261 | const hidl_vec<V1_0::SensorInfo>& /* sensorInfos */) override { |
| 262 | return Return<void>(); |
| 263 | } |
| 264 | |
| 265 | Return<void> onDynamicSensorsDisconnected( |
| 266 | const hidl_vec<int32_t>& /* sensorHandles */) override { |
| 267 | return Return<void>(); |
| 268 | } |
| 269 | |
| 270 | Return<void> onDynamicSensorsConnected_2_1( |
| 271 | const hidl_vec<SensorInfo>& /* sensorInfos */) override { |
| 272 | return Return<void>(); |
| 273 | } |
| 274 | }; |
| 275 | |
| 276 | } // namespace implementation |
| 277 | } // namespace V2_1 |
| 278 | } // namespace sensors |
| 279 | } // namespace hardware |
| 280 | } // namespace android |
| 281 | |
Anthony Stange | 3289590 | 2020-02-14 08:56:07 -0500 | [diff] [blame] | 282 | #endif // ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H |