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:
~EventMessageQueueWrapperBase()38     virtual ~EventMessageQueueWrapperBase() {}
39 
40     virtual std::atomic<uint32_t>* getEventFlagWord() = 0;
41     virtual size_t availableToRead() = 0;
42     virtual size_t availableToWrite() = 0;
43     virtual bool read(V2_1::Event* events, size_t numToRead) = 0;
44     virtual bool write(const V2_1::Event* events, size_t numToWrite) = 0;
45     virtual bool write(const std::vector<V2_1::Event>& events) = 0;
46     virtual bool writeBlocking(const V2_1::Event* events, size_t count, uint32_t readNotification,
47                                uint32_t writeNotification, int64_t timeOutNanos,
48                                android::hardware::EventFlag* evFlag) = 0;
49     virtual size_t getQuantumCount() = 0;
50 };
51 
52 class EventMessageQueueWrapperV1_0 : public EventMessageQueueWrapperBase {
53   public:
54     using EventMessageQueue = MessageQueue<V1_0::Event, kSynchronizedReadWrite>;
55 
EventMessageQueueWrapperV1_0(std::unique_ptr<EventMessageQueue> & queue)56     EventMessageQueueWrapperV1_0(std::unique_ptr<EventMessageQueue>& queue)
57         : mQueue(std::move(queue)) {}
58 
getDesc()59     const ::android::hardware::MQDescriptorSync<V1_0::Event>* getDesc() {
60         return mQueue->getDesc();
61     }
62 
getEventFlagWord()63     virtual std::atomic<uint32_t>* getEventFlagWord() override {
64         return mQueue->getEventFlagWord();
65     }
66 
availableToRead()67     virtual size_t availableToRead() override { return mQueue->availableToRead(); }
68 
availableToWrite()69     size_t availableToWrite() override { return mQueue->availableToWrite(); }
70 
read(V2_1::Event * events,size_t numToRead)71     virtual bool read(V2_1::Event* events, size_t numToRead) override {
72         return mQueue->read(reinterpret_cast<V1_0::Event*>(events), numToRead);
73     }
74 
write(const V2_1::Event * events,size_t numToWrite)75     bool write(const V2_1::Event* events, size_t numToWrite) override {
76         return mQueue->write(reinterpret_cast<const V1_0::Event*>(events), numToWrite);
77     }
78 
write(const std::vector<V2_1::Event> & events)79     virtual bool write(const std::vector<V2_1::Event>& events) override {
80         const std::vector<V1_0::Event>& oldEvents = convertToOldEvents(events);
81         return mQueue->write(oldEvents.data(), oldEvents.size());
82     }
83 
writeBlocking(const V2_1::Event * events,size_t count,uint32_t readNotification,uint32_t writeNotification,int64_t timeOutNanos,android::hardware::EventFlag * evFlag)84     bool writeBlocking(const V2_1::Event* events, size_t count, uint32_t readNotification,
85                        uint32_t writeNotification, int64_t timeOutNanos,
86                        android::hardware::EventFlag* evFlag) override {
87         return mQueue->writeBlocking(reinterpret_cast<const V1_0::Event*>(events), count,
88                                      readNotification, writeNotification, timeOutNanos, evFlag);
89     }
90 
getQuantumCount()91     size_t getQuantumCount() override { return mQueue->getQuantumCount(); }
92 
93   private:
94     std::unique_ptr<EventMessageQueue> mQueue;
95 };
96 
97 class EventMessageQueueWrapperV2_1 : public EventMessageQueueWrapperBase {
98   public:
99     using EventMessageQueue = MessageQueue<V2_1::Event, kSynchronizedReadWrite>;
100 
EventMessageQueueWrapperV2_1(std::unique_ptr<EventMessageQueue> & queue)101     EventMessageQueueWrapperV2_1(std::unique_ptr<EventMessageQueue>& queue)
102         : mQueue(std::move(queue)) {}
103 
getDesc()104     const ::android::hardware::MQDescriptorSync<V2_1::Event>* getDesc() {
105         return mQueue->getDesc();
106     }
107 
getEventFlagWord()108     std::atomic<uint32_t>* getEventFlagWord() override { return mQueue->getEventFlagWord(); }
109 
availableToRead()110     virtual size_t availableToRead() override { return mQueue->availableToRead(); }
111 
availableToWrite()112     size_t availableToWrite() override { return mQueue->availableToWrite(); }
113 
read(V2_1::Event * events,size_t numToRead)114     virtual bool read(V2_1::Event* events, size_t numToRead) override {
115         return mQueue->read(events, numToRead);
116     }
117 
write(const V2_1::Event * events,size_t numToWrite)118     bool write(const V2_1::Event* events, size_t numToWrite) override {
119         return mQueue->write(events, numToWrite);
120     }
121 
write(const std::vector<V2_1::Event> & events)122     bool write(const std::vector<V2_1::Event>& events) override {
123         return mQueue->write(events.data(), events.size());
124     }
125 
writeBlocking(const V2_1::Event * events,size_t count,uint32_t readNotification,uint32_t writeNotification,int64_t timeOutNanos,android::hardware::EventFlag * evFlag)126     bool writeBlocking(const V2_1::Event* events, size_t count, uint32_t readNotification,
127                        uint32_t writeNotification, int64_t timeOutNanos,
128                        android::hardware::EventFlag* evFlag) override {
129         return mQueue->writeBlocking(events, count, readNotification, writeNotification,
130                                      timeOutNanos, evFlag);
131     }
132 
getQuantumCount()133     size_t getQuantumCount() override { return mQueue->getQuantumCount(); }
134 
135   private:
136     std::unique_ptr<EventMessageQueue> mQueue;
137 };
138 
139 }  // namespace implementation
140 }  // namespace V2_1
141 }  // namespace sensors
142 }  // namespace hardware
143 }  // namespace android
144 
145 #endif  // ANDROID_HARDWARE_SENSORS_V2_1_EVENTMESSAGEQUEUEWRAPPER_H