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_ISENSORSCALLBACKWRAPPER_H
18 #define ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSCALLBACKWRAPPER_H
19 
20 #include "convertV2_1.h"
21 
22 #include "android/hardware/sensors/1.0/ISensors.h"
23 #include "android/hardware/sensors/1.0/types.h"
24 #include "android/hardware/sensors/2.0/ISensors.h"
25 #include "android/hardware/sensors/2.0/ISensorsCallback.h"
26 #include "android/hardware/sensors/2.1/ISensors.h"
27 #include "android/hardware/sensors/2.1/ISensorsCallback.h"
28 #include "android/hardware/sensors/2.1/types.h"
29 
30 #include <utils/LightRefBase.h>
31 
32 #include <cassert>
33 
34 namespace android {
35 namespace hardware {
36 namespace sensors {
37 namespace V2_1 {
38 namespace implementation {
39 
40 /**
41  * The ISensorsCallbackWrapper classes below abstract away the common logic between both the V2.0
42  * and V2.1 versions of the Sensors HAL interface. This allows users of these classes to only care
43  * about the HAL version at init time and then interact with either version of the callback without
44  * worrying about the class type by utilizing the base class.
45  */
46 class ISensorsCallbackWrapperBase : public VirtualLightRefBase {
47   public:
48     virtual Return<void> onDynamicSensorsConnected(
49             const hidl_vec<V2_1::SensorInfo>& sensorInfos) = 0;
50 
51     virtual Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& sensorHandles) = 0;
52 };
53 
54 template <typename T>
55 class SensorsCallbackWrapperBase : public ISensorsCallbackWrapperBase {
56   public:
SensorsCallbackWrapperBase(sp<T> sensorsCallback)57     SensorsCallbackWrapperBase(sp<T> sensorsCallback) : mSensorsCallback(sensorsCallback){};
58 
onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo> & sensorInfos)59     virtual Return<void> onDynamicSensorsConnected(
60             const hidl_vec<V2_1::SensorInfo>& sensorInfos) override {
61         return mSensorsCallback->onDynamicSensorsConnected(convertToOldSensorInfos(sensorInfos));
62     }
63 
onDynamicSensorsDisconnected(const hidl_vec<int32_t> & sensorHandles)64     Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& sensorHandles) {
65         return mSensorsCallback->onDynamicSensorsDisconnected(sensorHandles);
66     }
67 
68   protected:
69     sp<T> mSensorsCallback;
70 };
71 
72 class ISensorsCallbackWrapperV2_0
73     : public SensorsCallbackWrapperBase<hardware::sensors::V2_0::ISensorsCallback> {
74   public:
ISensorsCallbackWrapperV2_0(sp<hardware::sensors::V2_0::ISensorsCallback> sensorsCallback)75     ISensorsCallbackWrapperV2_0(sp<hardware::sensors::V2_0::ISensorsCallback> sensorsCallback)
76         : SensorsCallbackWrapperBase(sensorsCallback){};
77 };
78 
79 class ISensorsCallbackWrapperV2_1
80     : public SensorsCallbackWrapperBase<hardware::sensors::V2_1::ISensorsCallback> {
81   public:
ISensorsCallbackWrapperV2_1(sp<hardware::sensors::V2_1::ISensorsCallback> sensorsCallback)82     ISensorsCallbackWrapperV2_1(sp<hardware::sensors::V2_1::ISensorsCallback> sensorsCallback)
83         : SensorsCallbackWrapperBase(sensorsCallback) {}
84 
onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo> & sensorInfos)85     Return<void> onDynamicSensorsConnected(const hidl_vec<V2_1::SensorInfo>& sensorInfos) override {
86         return mSensorsCallback->onDynamicSensorsConnected_2_1(sensorInfos);
87     }
88 };
89 
90 }  // namespace implementation
91 }  // namespace V2_1
92 }  // namespace sensors
93 }  // namespace hardware
94 }  // namespace android
95 
96 #endif  // ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSCALLBACKWRAPPER_H