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 #pragma once
18 
19 #include "V2_0/SubHal.h"
20 #include "V2_1/SubHal.h"
21 #include "convertV2_1.h"
22 
23 namespace android {
24 namespace hardware {
25 namespace sensors {
26 namespace V2_1 {
27 namespace subhal {
28 namespace implementation {
29 
30 /**
31  * The following callback wrapper classes abstract away common functionality across V2.0 and V2.1
32  * interfaces. Much of the logic is common between the two versions and this allows users of the
33  * classes to only care about the type used at initialization and then interact with either version
34  * of the callback interface without worrying about the type.
35  */
36 class IHalProxyCallbackWrapperBase {
37   protected:
38     using ScopedWakelock = V2_0::implementation::ScopedWakelock;
39 
40   public:
~IHalProxyCallbackWrapperBase()41     virtual ~IHalProxyCallbackWrapperBase() {}
42 
43     virtual Return<void> onDynamicSensorsConnected(
44             const hidl_vec<V2_1::SensorInfo>& sensorInfos) = 0;
45 
46     virtual Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& sensorHandles) = 0;
47 
48     virtual void postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) = 0;
49 
50     virtual ScopedWakelock createScopedWakelock(bool lock) = 0;
51 };
52 
53 template <typename T>
54 class HalProxyCallbackWrapperBase : public IHalProxyCallbackWrapperBase {
55   public:
HalProxyCallbackWrapperBase(sp<T> callback)56     HalProxyCallbackWrapperBase(sp<T> callback) : mCallback(callback){};
57 
onDynamicSensorsDisconnected(const hidl_vec<int32_t> & sensorHandles)58     Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& sensorHandles) override {
59         return mCallback->onDynamicSensorsDisconnected(sensorHandles);
60     }
61 
createScopedWakelock(bool lock)62     ScopedWakelock createScopedWakelock(bool lock) override {
63         return mCallback->createScopedWakelock(lock);
64     }
65 
66   protected:
67     sp<T> mCallback;
68 };
69 
70 class HalProxyCallbackWrapperV2_0
71     : public HalProxyCallbackWrapperBase<V2_0::implementation::IHalProxyCallback> {
72   public:
HalProxyCallbackWrapperV2_0(sp<V2_0::implementation::IHalProxyCallback> callback)73     HalProxyCallbackWrapperV2_0(sp<V2_0::implementation::IHalProxyCallback> callback)
74         : HalProxyCallbackWrapperBase(callback){};
75 
onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo> & sensorInfos)76     Return<void> onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo>& sensorInfos) override {
77         return mCallback->onDynamicSensorsConnected(
78                 V2_1::implementation::convertToOldSensorInfos(sensorInfos));
79     }
80 
postEvents(const std::vector<V2_1::Event> & events,ScopedWakelock wakelock)81     void postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) override {
82         return mCallback->postEvents(V2_1::implementation::convertToOldEvents(events),
83                                      std::move(wakelock));
84     }
85 };
86 
87 class HalProxyCallbackWrapperV2_1
88     : public HalProxyCallbackWrapperBase<V2_1::implementation::IHalProxyCallback> {
89   public:
HalProxyCallbackWrapperV2_1(sp<V2_1::implementation::IHalProxyCallback> callback)90     HalProxyCallbackWrapperV2_1(sp<V2_1::implementation::IHalProxyCallback> callback)
91         : HalProxyCallbackWrapperBase(callback){};
92 
onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo> & sensorInfos)93     Return<void> onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo>& sensorInfos) override {
94         return mCallback->onDynamicSensorsConnected_2_1(sensorInfos);
95     }
96 
postEvents(const std::vector<V2_1::Event> & events,ScopedWakelock wakelock)97     void postEvents(const std::vector<V2_1::Event>& events, ScopedWakelock wakelock) {
98         return mCallback->postEvents(events, std::move(wakelock));
99     }
100 };
101 
102 }  // namespace implementation
103 }  // namespace subhal
104 }  // namespace V2_1
105 }  // namespace sensors
106 }  // namespace hardware
107 }  // namespace android
108