blob: 816b225806b0ef66be9cac028fb6bda146b1057b [file] [log] [blame]
Anthony Stange33acf9f2020-03-03 15:32:39 -05001/*
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
34namespace android {
35namespace hardware {
36namespace sensors {
37namespace V2_1 {
38namespace 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 */
46class 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
54template <typename T>
55class SensorsCallbackWrapperBase : public ISensorsCallbackWrapperBase {
56 public:
57 SensorsCallbackWrapperBase(sp<T> sensorsCallback) : mSensorsCallback(sensorsCallback){};
58
59 virtual Return<void> onDynamicSensorsConnected(
60 const hidl_vec<V2_1::SensorInfo>& sensorInfos) override {
61 return mSensorsCallback->onDynamicSensorsConnected(convertToOldSensorInfos(sensorInfos));
62 }
63
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
72class ISensorsCallbackWrapperV2_0
73 : public SensorsCallbackWrapperBase<hardware::sensors::V2_0::ISensorsCallback> {
74 public:
75 ISensorsCallbackWrapperV2_0(sp<hardware::sensors::V2_0::ISensorsCallback> sensorsCallback)
76 : SensorsCallbackWrapperBase(sensorsCallback){};
77};
78
79class ISensorsCallbackWrapperV2_1
80 : public SensorsCallbackWrapperBase<hardware::sensors::V2_1::ISensorsCallback> {
81 public:
82 ISensorsCallbackWrapperV2_1(sp<hardware::sensors::V2_1::ISensorsCallback> sensorsCallback)
83 : SensorsCallbackWrapperBase(sensorsCallback) {}
84
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