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_EVENTMESSAGEQUEUEWRAPPER_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_1_EVENTMESSAGEQUEUEWRAPPER_H |
| 19 | |
| 20 | #include "convertV2_1.h" |
| 21 | |
| 22 | #include <android/hardware/sensors/2.1/types.h> |
| 23 | #include <fmq/MessageQueue.h> |
| 24 | #include <hidl/MQDescriptor.h> |
| 25 | #include <hidl/Status.h> |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | #include <atomic> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace sensors { |
| 33 | namespace V2_1 { |
| 34 | namespace implementation { |
| 35 | |
| 36 | class EventMessageQueueWrapperBase : public RefBase { |
| 37 | public: |
| 38 | virtual ~EventMessageQueueWrapperBase() {} |
| 39 | |
| 40 | virtual std::atomic<uint32_t>* getEventFlagWord() = 0; |
| 41 | virtual size_t availableToRead() = 0; |
| 42 | virtual bool read(V2_1::Event* events, size_t numToRead) = 0; |
| 43 | virtual bool write(const std::vector<V2_1::Event>& events) = 0; |
| 44 | }; |
| 45 | |
| 46 | class EventMessageQueueWrapperV1_0 : public EventMessageQueueWrapperBase { |
| 47 | public: |
| 48 | using EventMessageQueue = MessageQueue<V1_0::Event, kSynchronizedReadWrite>; |
| 49 | |
| 50 | EventMessageQueueWrapperV1_0(std::unique_ptr<EventMessageQueue>& queue) |
| 51 | : mQueue(std::move(queue)) {} |
| 52 | |
| 53 | const ::android::hardware::MQDescriptorSync<V1_0::Event>* getDesc() { |
| 54 | return mQueue->getDesc(); |
| 55 | } |
| 56 | |
| 57 | virtual std::atomic<uint32_t>* getEventFlagWord() override { |
| 58 | return mQueue->getEventFlagWord(); |
| 59 | } |
| 60 | |
| 61 | virtual size_t availableToRead() override { return mQueue->availableToRead(); } |
| 62 | |
| 63 | virtual bool read(V2_1::Event* events, size_t numToRead) override { |
| 64 | return mQueue->read(reinterpret_cast<V1_0::Event*>(events), numToRead); |
| 65 | } |
| 66 | |
| 67 | virtual bool write(const std::vector<V2_1::Event>& events) override { |
| 68 | const std::vector<V1_0::Event>& oldEvents = convertToOldEvents(events); |
| 69 | return mQueue->write(oldEvents.data(), oldEvents.size()); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | std::unique_ptr<EventMessageQueue> mQueue; |
| 74 | }; |
| 75 | |
| 76 | class EventMessageQueueWrapperV2_1 : public EventMessageQueueWrapperBase { |
| 77 | public: |
| 78 | using EventMessageQueue = MessageQueue<V2_1::Event, kSynchronizedReadWrite>; |
| 79 | |
| 80 | EventMessageQueueWrapperV2_1(std::unique_ptr<EventMessageQueue>& queue) |
| 81 | : mQueue(std::move(queue)) {} |
| 82 | |
| 83 | const ::android::hardware::MQDescriptorSync<V2_1::Event>* getDesc() { |
| 84 | return mQueue->getDesc(); |
| 85 | } |
| 86 | |
| 87 | std::atomic<uint32_t>* getEventFlagWord() override { return mQueue->getEventFlagWord(); } |
| 88 | |
| 89 | virtual size_t availableToRead() override { return mQueue->availableToRead(); } |
| 90 | |
| 91 | virtual bool read(V2_1::Event* events, size_t numToRead) override { |
| 92 | return mQueue->read(events, numToRead); |
| 93 | } |
| 94 | |
| 95 | bool write(const std::vector<V2_1::Event>& events) override { |
| 96 | return mQueue->write(events.data(), events.size()); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | std::unique_ptr<EventMessageQueue> mQueue; |
| 101 | }; |
| 102 | |
| 103 | } // namespace implementation |
| 104 | } // namespace V2_1 |
| 105 | } // namespace sensors |
| 106 | } // namespace hardware |
| 107 | } // namespace android |
| 108 | |
| 109 | #endif // ANDROID_HARDWARE_SENSORS_V2_1_EVENTMESSAGEQUEUEWRAPPER_H |