blob: d1a72345f7b7b893397512ba24a63ee0b256a6bf [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"
Brian Stack6c49e6f2018-09-24 15:44:32 -070022#include "android/hardware/sensors/2.0/ISensorsCallback.h"
Brian Stackc9ef4832018-09-14 11:59:18 -070023
24#include <utils/LightRefBase.h>
25
26namespace android {
27namespace SensorServiceUtil {
28
29using ::android::hardware::MQDescriptorSync;
30using ::android::hardware::Return;
31using ::android::hardware::sensors::V1_0::Event;
32using ::android::hardware::sensors::V1_0::ISensors;
33using ::android::hardware::sensors::V1_0::OperationMode;
34using ::android::hardware::sensors::V1_0::RateLevel;
35using ::android::hardware::sensors::V1_0::Result;
36using ::android::hardware::sensors::V1_0::SharedMemInfo;
Brian Stack6c49e6f2018-09-24 15:44:32 -070037using ::android::hardware::sensors::V2_0::ISensorsCallback;
Brian Stackc9ef4832018-09-14 11:59:18 -070038
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 */
54class ISensorsWrapper : public VirtualLightRefBase {
55public:
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
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
Brian Stack6c49e6f2018-09-24 15:44:32 -070090 virtual Return<Result> initialize(const MQDescriptorSync<Event>& eventQueueDesc,
91 const MQDescriptorSync<uint32_t>& wakeLockDesc,
92 const ::android::sp<ISensorsCallback>& callback) {
Brian Stackc9ef4832018-09-14 11:59:18 -070093 (void)eventQueueDesc;
94 (void)wakeLockDesc;
Brian Stack6c49e6f2018-09-24 15:44:32 -070095 (void)callback;
Brian Stackc9ef4832018-09-14 11:59:18 -070096 // TODO (b/111070257): Generate an assert-level error since this should never be called
97 // directly
98 return Result::INVALID_OPERATION;
99 }
100};
101
Brian Stack087ed292018-09-14 15:45:01 -0700102template<typename T>
103class SensorsWrapperBase : public ISensorsWrapper {
104public:
105 SensorsWrapperBase(sp<T> sensors) :
106 mSensors(sensors) { };
107
108 Return<void> getSensorsList(ISensors::getSensorsList_cb _hidl_cb) override {
109 return mSensors->getSensorsList(_hidl_cb);
110 }
111
112 Return<Result> setOperationMode(OperationMode mode) override {
113 return mSensors->setOperationMode(mode);
114 }
115
116 Return<Result> activate(int32_t sensorHandle, bool enabled) override {
117 return mSensors->activate(sensorHandle, enabled);
118 }
119
120 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
121 int64_t maxReportLatencyNs) override {
122 return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
123 }
124
125 Return<Result> flush(int32_t sensorHandle) override {
126 return mSensors->flush(sensorHandle);
127 }
128
129 Return<Result> injectSensorData(const Event& event) override {
130 return mSensors->injectSensorData(event);
131 }
132
133 Return<void> registerDirectChannel(const SharedMemInfo& mem,
134 ISensors::registerDirectChannel_cb _hidl_cb) override {
135 return mSensors->registerDirectChannel(mem, _hidl_cb);
136 }
137
138 Return<Result> unregisterDirectChannel(int32_t channelHandle) override {
139 return mSensors->unregisterDirectChannel(channelHandle);
140 }
141
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
148protected:
149 sp<T> mSensors;
150};
151
152class SensorsWrapperV1_0 : public SensorsWrapperBase<hardware::sensors::V1_0::ISensors> {
153public:
154 SensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors) :
155 SensorsWrapperBase(sensors) { };
156
157 bool supportsPolling() const override {
158 return true;
159 }
160
161 bool supportsMessageQueues() const override {
162 return false;
163 }
164
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
171class SensorsWrapperV2_0 : public SensorsWrapperBase<hardware::sensors::V2_0::ISensors> {
172public:
173 SensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)
174 : SensorsWrapperBase(sensors) { };
175
176 bool supportsPolling() const override {
177 return false;
178 }
179
180 bool supportsMessageQueues() const override {
181 return true;
182 }
183
Brian Stack6c49e6f2018-09-24 15:44:32 -0700184 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);
Brian Stack087ed292018-09-14 15:45:01 -0700188 }
189};
190
Brian Stackc9ef4832018-09-14 11:59:18 -0700191}; // namespace SensorServiceUtil
192}; // namespace android
193
194#endif // ANDROID_SENSORS_WRAPPER_H