1 /*
2  * Copyright (C) 2018 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_SENSORS_WRAPPER_H
18 #define ANDROID_SENSORS_WRAPPER_H
19 
20 #include "android/hardware/sensors/1.0/ISensors.h"
21 #include "android/hardware/sensors/2.0/ISensors.h"
22 #include "android/hardware/sensors/2.0/ISensorsCallback.h"
23 
24 #include <utils/LightRefBase.h>
25 
26 namespace android {
27 namespace SensorServiceUtil {
28 
29 using ::android::hardware::MQDescriptorSync;
30 using ::android::hardware::Return;
31 using ::android::hardware::sensors::V1_0::Event;
32 using ::android::hardware::sensors::V1_0::ISensors;
33 using ::android::hardware::sensors::V1_0::OperationMode;
34 using ::android::hardware::sensors::V1_0::RateLevel;
35 using ::android::hardware::sensors::V1_0::Result;
36 using ::android::hardware::sensors::V1_0::SharedMemInfo;
37 using ::android::hardware::sensors::V2_0::ISensorsCallback;
38 
39 /*
40  * The ISensorsWrapper interface includes all function from supported Sensors HAL versions. This
41  * allows for the SensorDevice to use the ISensorsWrapper interface to interact with the Sensors
42  * HAL regardless of the current version of the Sensors HAL that is loaded. Each concrete
43  * instantiation of ISensorsWrapper must correspond to a specific Sensors HAL version. This design
44  * is beneficial because only the functions that change between Sensors HAL versions must be newly
45  * newly implemented, any previously implemented function that does not change may remain the same.
46  *
47  * Functions that exist across all versions of the Sensors HAL should be implemented as pure
48  * virtual functions which forces the concrete instantiations to implement the functions.
49  *
50  * Functions that do not exist across all versions of the Sensors HAL should include a default
51  * implementation that generates an error if called. The default implementation should never
52  * be called and must be overridden by Sensors HAL versions that support the function.
53  */
54 class ISensorsWrapper : public VirtualLightRefBase {
55 public:
56     virtual bool supportsPolling() const = 0;
57 
58     virtual bool supportsMessageQueues() const = 0;
59 
60     virtual Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) = 0;
61 
62     virtual Return<Result> setOperationMode(OperationMode mode) = 0;
63 
64     virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0;
65 
66     virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
67                                  int64_t maxReportLatencyNs) = 0;
68 
69     virtual Return<Result> flush(int32_t sensorHandle) = 0;
70 
71     virtual Return<Result> injectSensorData(const Event& event) = 0;
72 
73     virtual Return<void> registerDirectChannel(const SharedMemInfo& mem,
74                                                ISensors::registerDirectChannel_cb _hidl_cb) = 0;
75 
76     virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0;
77 
78     virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
79                                             RateLevel rate,
80                                             ISensors::configDirectReport_cb _hidl_cb) = 0;
81 
poll(int32_t maxCount,ISensors::poll_cb _hidl_cb)82     virtual Return<void> poll(int32_t maxCount, ISensors::poll_cb _hidl_cb) {
83         (void)maxCount;
84         (void)_hidl_cb;
85         // TODO (b/111070257): Generate an assert-level error since this should never be called
86         // directly
87         return Return<void>();
88     }
89 
initialize(const MQDescriptorSync<Event> & eventQueueDesc,const MQDescriptorSync<uint32_t> & wakeLockDesc,const::android::sp<ISensorsCallback> & callback)90     virtual Return<Result> initialize(const MQDescriptorSync<Event>& eventQueueDesc,
91                                       const MQDescriptorSync<uint32_t>& wakeLockDesc,
92                                       const ::android::sp<ISensorsCallback>& callback) {
93         (void)eventQueueDesc;
94         (void)wakeLockDesc;
95         (void)callback;
96         // TODO (b/111070257): Generate an assert-level error since this should never be called
97         // directly
98         return Result::INVALID_OPERATION;
99     }
100 };
101 
102 template<typename T>
103 class SensorsWrapperBase : public ISensorsWrapper {
104 public:
SensorsWrapperBase(sp<T> sensors)105     SensorsWrapperBase(sp<T> sensors) :
106         mSensors(sensors) { };
107 
getSensorsList(ISensors::getSensorsList_cb _hidl_cb)108     Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) override {
109         return mSensors->getSensorsList(_hidl_cb);
110     }
111 
setOperationMode(OperationMode mode)112     Return<Result> setOperationMode(OperationMode mode) override {
113         return mSensors->setOperationMode(mode);
114     }
115 
activate(int32_t sensorHandle,bool enabled)116     Return<Result> activate(int32_t sensorHandle, bool enabled) override {
117         return mSensors->activate(sensorHandle, enabled);
118     }
119 
batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t maxReportLatencyNs)120     Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
121                          int64_t maxReportLatencyNs) override {
122         return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
123     }
124 
flush(int32_t sensorHandle)125     Return<Result> flush(int32_t sensorHandle) override {
126         return mSensors->flush(sensorHandle);
127     }
128 
injectSensorData(const Event & event)129     Return<Result> injectSensorData(const Event& event) override {
130         return mSensors->injectSensorData(event);
131     }
132 
registerDirectChannel(const SharedMemInfo & mem,ISensors::registerDirectChannel_cb _hidl_cb)133     Return<void> registerDirectChannel(const SharedMemInfo& mem,
134                                        ISensors::registerDirectChannel_cb _hidl_cb) override {
135         return mSensors->registerDirectChannel(mem, _hidl_cb);
136     }
137 
unregisterDirectChannel(int32_t channelHandle)138     Return<Result> unregisterDirectChannel(int32_t channelHandle) override {
139         return mSensors->unregisterDirectChannel(channelHandle);
140     }
141 
configDirectReport(int32_t sensorHandle,int32_t channelHandle,RateLevel rate,ISensors::configDirectReport_cb _hidl_cb)142     Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
143                                     RateLevel rate,
144                                     ISensors::configDirectReport_cb _hidl_cb) override {
145         return mSensors->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb);
146     }
147 
148 protected:
149     sp<T> mSensors;
150 };
151 
152 class SensorsWrapperV1_0 : public SensorsWrapperBase<hardware::sensors::V1_0::ISensors> {
153 public:
SensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors)154     SensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors) :
155         SensorsWrapperBase(sensors) { };
156 
supportsPolling()157     bool supportsPolling() const override {
158         return true;
159     }
160 
supportsMessageQueues()161     bool supportsMessageQueues() const override {
162         return false;
163     }
164 
poll(int32_t maxCount,hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb)165     Return<void> poll(int32_t maxCount,
166                       hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb) override {
167         return mSensors->poll(maxCount, _hidl_cb);
168     }
169 };
170 
171 class SensorsWrapperV2_0 : public SensorsWrapperBase<hardware::sensors::V2_0::ISensors> {
172 public:
SensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)173     SensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)
174         : SensorsWrapperBase(sensors) { };
175 
supportsPolling()176     bool supportsPolling() const override {
177         return false;
178     }
179 
supportsMessageQueues()180     bool supportsMessageQueues() const override {
181         return true;
182     }
183 
initialize(const MQDescriptorSync<Event> & eventQueueDesc,const MQDescriptorSync<uint32_t> & wakeLockDesc,const::android::sp<ISensorsCallback> & callback)184     Return<Result> initialize(const MQDescriptorSync<Event>& eventQueueDesc,
185                               const MQDescriptorSync<uint32_t>& wakeLockDesc,
186                               const ::android::sp<ISensorsCallback>& callback) override {
187         return mSensors->initialize(eventQueueDesc, wakeLockDesc, callback);
188     }
189 };
190 
191 }; // namespace SensorServiceUtil
192 }; // namespace android
193 
194 #endif // ANDROID_SENSORS_WRAPPER_H
195