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