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"
21 #include "ISensorsWrapper.h"
22 
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"
30 
31 #include <utils/LightRefBase.h>
32 
33 #include <cassert>
34 
35 namespace android {
36 namespace hardware {
37 namespace sensors {
38 namespace V2_1 {
39 namespace implementation {
40 
41 using ::android::hardware::MessageQueue;
42 using ::android::hardware::MQDescriptorSync;
43 using ::android::hardware::Return;
44 using ::android::hardware::sensors::V1_0::ISensors;
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;
49 using ::android::hardware::sensors::V2_1::Event;
50 using ::android::hardware::sensors::V2_1::ISensorsCallback;
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 
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 {
72   public:
73     virtual bool supportsPolling() const = 0;
74 
75     virtual bool supportsMessageQueues() const = 0;
76 
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 
poll(int32_t,ISensors::poll_cb)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>();
107     }
108 
getEventQueue()109     virtual EventMessageQueueWrapperBase* getEventQueue() { return nullptr; }
110 
initialize(const MQDescriptorSync<uint32_t> &,const::android::sp<ISensorsCallback> &)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;
116     }
117 };
118 
119 template <typename T>
120 class SensorsWrapperBase : public ISensorsWrapperBase {
121   public:
SensorsWrapperBase(sp<T> sensors)122     SensorsWrapperBase(sp<T> sensors) : mSensors(sensors){};
123 
linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient,uint64_t cookie)124     void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient,
125                      uint64_t cookie) override {
126         mSensors->linkToDeath(deathRecipient, cookie);
127     }
128 
getSensorsList(::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)129     virtual Return<void> getSensorsList(
130             ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
131         return mSensors->getSensorsList(
132                 [&](const auto& list) { _hidl_cb(convertToNewSensorInfos(list)); });
133     }
134 
setOperationMode(OperationMode mode)135     Return<Result> setOperationMode(OperationMode mode) override {
136         return mSensors->setOperationMode(mode);
137     }
138 
activate(int32_t sensorHandle,bool enabled)139     Return<Result> activate(int32_t sensorHandle, bool enabled) override {
140         return mSensors->activate(sensorHandle, enabled);
141     }
142 
batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t maxReportLatencyNs)143     Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
144                          int64_t maxReportLatencyNs) override {
145         return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
146     }
147 
flush(int32_t sensorHandle)148     Return<Result> flush(int32_t sensorHandle) override { return mSensors->flush(sensorHandle); }
149 
injectSensorData(const Event & event)150     virtual Return<Result> injectSensorData(const Event& event) override {
151         return mSensors->injectSensorData(convertToOldEvent(event));
152     }
153 
registerDirectChannel(const SharedMemInfo & mem,ISensors::registerDirectChannel_cb _hidl_cb)154     Return<void> registerDirectChannel(const SharedMemInfo& mem,
155                                        ISensors::registerDirectChannel_cb _hidl_cb) override {
156         return mSensors->registerDirectChannel(mem, _hidl_cb);
157     }
158 
unregisterDirectChannel(int32_t channelHandle)159     Return<Result> unregisterDirectChannel(int32_t channelHandle) override {
160         return mSensors->unregisterDirectChannel(channelHandle);
161     }
162 
configDirectReport(int32_t sensorHandle,int32_t channelHandle,RateLevel rate,ISensors::configDirectReport_cb _hidl_cb)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:
ISensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors)174     ISensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors)
175         : SensorsWrapperBase(sensors){};
176 
supportsPolling()177     bool supportsPolling() const override { return true; }
178 
supportsMessageQueues()179     bool supportsMessageQueues() const override { return false; }
180 
poll(int32_t maxCount,hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb)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 
ISensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)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 
supportsPolling()200     bool supportsPolling() const override { return false; }
201 
supportsMessageQueues()202     bool supportsMessageQueues() const override { return true; }
203 
getEventQueue()204     EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); }
205 
initialize(const MQDescriptorSync<uint32_t> & wakeLockDesc,const::android::sp<ISensorsCallback> & callback)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:
212     std::unique_ptr<EventMessageQueueWrapperV1_0> mEventQueue;
213 };
214 
215 class ISensorsWrapperV2_1 : public SensorsWrapperBase<hardware::sensors::V2_1::ISensors> {
216   public:
217     typedef MessageQueue<Event, ::android::hardware::kSynchronizedReadWrite> EventMessageQueueV2_1;
218 
ISensorsWrapperV2_1(sp<hardware::sensors::V2_1::ISensors> sensors)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 */);
223         mEventQueue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue);
224     };
225 
supportsPolling()226     bool supportsPolling() const override { return false; }
227 
supportsMessageQueues()228     bool supportsMessageQueues() const override { return true; }
229 
getEventQueue()230     EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); }
231 
getSensorsList(::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)232     Return<void> getSensorsList(
233             ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
234         return mSensors->getSensorsList_2_1(_hidl_cb);
235     }
236 
injectSensorData(const Event & event)237     Return<Result> injectSensorData(const Event& event) override {
238         return mSensors->injectSensorData_2_1(event);
239     }
240 
initialize(const MQDescriptorSync<uint32_t> & wakeLockDesc,const::android::sp<ISensorsCallback> & callback)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     }
245 
246   private:
247     std::unique_ptr<EventMessageQueueWrapperV2_1> mEventQueue;
248 };
249 
wrapISensors(sp<V2_0::ISensors> sensors)250 inline sp<ISensorsWrapperV2_0> wrapISensors(sp<V2_0::ISensors> sensors) {
251     return new ISensorsWrapperV2_0(sensors);
252 }
253 
wrapISensors(sp<V2_1::ISensors> sensors)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:
onDynamicSensorsConnected(const hidl_vec<V1_0::SensorInfo> &)260     Return<void> onDynamicSensorsConnected(
261             const hidl_vec<V1_0::SensorInfo>& /* sensorInfos */) override {
262         return Return<void>();
263     }
264 
onDynamicSensorsDisconnected(const hidl_vec<int32_t> &)265     Return<void> onDynamicSensorsDisconnected(
266             const hidl_vec<int32_t>& /* sensorHandles */) override {
267         return Return<void>();
268     }
269 
onDynamicSensorsConnected_2_1(const hidl_vec<SensorInfo> &)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 
282 #endif  // ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H