blob: e9c22b15ac73ccfee297a77808cda93b98a8a4e9 [file] [log] [blame]
Anthony Stangec002dd92020-02-12 14:41:41 -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_ISENSORSWRAPPER_H
18#define ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H
19
20#include "EventMessageQueueWrapper.h"
Anthony Stange32895902020-02-14 08:56:07 -050021#include "ISensorsWrapper.h"
Anthony Stangec002dd92020-02-12 14:41:41 -050022
Anthony Stange32895902020-02-14 08:56:07 -050023#include "android/hardware/sensors/1.0/ISensors.h"
24#include "android/hardware/sensors/1.0/types.h"
25#include "android/hardware/sensors/2.0/ISensors.h"
26#include "android/hardware/sensors/2.0/ISensorsCallback.h"
27#include "android/hardware/sensors/2.1/ISensors.h"
28#include "android/hardware/sensors/2.1/ISensorsCallback.h"
29#include "android/hardware/sensors/2.1/types.h"
Anthony Stangec002dd92020-02-12 14:41:41 -050030
Anthony Stange32895902020-02-14 08:56:07 -050031#include <utils/LightRefBase.h>
Anthony Stangec002dd92020-02-12 14:41:41 -050032
Anthony Stange32895902020-02-14 08:56:07 -050033#include <cassert>
Anthony Stangec002dd92020-02-12 14:41:41 -050034
35namespace android {
36namespace hardware {
37namespace sensors {
38namespace V2_1 {
39namespace implementation {
40
Anthony Stange32895902020-02-14 08:56:07 -050041using ::android::hardware::MessageQueue;
42using ::android::hardware::MQDescriptorSync;
Anthony Stangec002dd92020-02-12 14:41:41 -050043using ::android::hardware::Return;
Anthony Stange32895902020-02-14 08:56:07 -050044using ::android::hardware::sensors::V1_0::ISensors;
Anthony Stangec002dd92020-02-12 14:41:41 -050045using ::android::hardware::sensors::V1_0::OperationMode;
46using ::android::hardware::sensors::V1_0::RateLevel;
47using ::android::hardware::sensors::V1_0::Result;
48using ::android::hardware::sensors::V1_0::SharedMemInfo;
Anthony Stange32895902020-02-14 08:56:07 -050049using ::android::hardware::sensors::V2_1::Event;
50using ::android::hardware::sensors::V2_1::ISensorsCallback;
Anthony Stangec002dd92020-02-12 14:41:41 -050051
52// TODO: Look into providing this as a param if it needs to be a different value
53// than the framework.
54static constexpr size_t MAX_RECEIVE_BUFFER_EVENT_COUNT = 256;
55
Anthony Stange32895902020-02-14 08:56:07 -050056/*
57 * The ISensorsWrapper interface includes all function from supported Sensors HAL versions. This
58 * allows for the SensorDevice to use the ISensorsWrapper interface to interact with the Sensors
59 * HAL regardless of the current version of the Sensors HAL that is loaded. Each concrete
60 * instantiation of ISensorsWrapper must correspond to a specific Sensors HAL version. This design
61 * is beneficial because only the functions that change between Sensors HAL versions must be newly
62 * implemented, any previously implemented function that does not change may remain the same.
63 *
64 * Functions that exist across all versions of the Sensors HAL should be implemented as pure
65 * virtual functions which forces the concrete instantiations to implement the functions.
66 *
67 * Functions that do not exist across all versions of the Sensors HAL should include a default
68 * implementation that generates an error if called. The default implementation should never
69 * be called and must be overridden by Sensors HAL versions that support the function.
70 */
71class ISensorsWrapperBase : public VirtualLightRefBase {
Anthony Stangec002dd92020-02-12 14:41:41 -050072 public:
Anthony Stange32895902020-02-14 08:56:07 -050073 virtual bool supportsPolling() const = 0;
Anthony Stangec002dd92020-02-12 14:41:41 -050074
Anthony Stange32895902020-02-14 08:56:07 -050075 virtual bool supportsMessageQueues() const = 0;
Anthony Stangec002dd92020-02-12 14:41:41 -050076
Anthony Stange32895902020-02-14 08:56:07 -050077 virtual void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient,
78 uint64_t cookie) = 0;
79
80 virtual Return<void> getSensorsList(
81 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) = 0;
82
83 virtual Return<Result> setOperationMode(OperationMode mode) = 0;
84
85 virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0;
86
87 virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
88 int64_t maxReportLatencyNs) = 0;
89
90 virtual Return<Result> flush(int32_t sensorHandle) = 0;
91
92 virtual Return<Result> injectSensorData(const Event& event) = 0;
93
94 virtual Return<void> registerDirectChannel(const SharedMemInfo& mem,
95 ISensors::registerDirectChannel_cb _hidl_cb) = 0;
96
97 virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0;
98
99 virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle,
100 RateLevel rate,
101 ISensors::configDirectReport_cb _hidl_cb) = 0;
102
103 virtual Return<void> poll(int32_t /* maxCount */, ISensors::poll_cb /* _hidl_cb */) {
104 // Enforce this method is never invoked as it should be overridden if it's meant to be used.
105 assert(false);
106 return Return<void>();
Anthony Stangec002dd92020-02-12 14:41:41 -0500107 }
108
Anthony Stange32895902020-02-14 08:56:07 -0500109 virtual EventMessageQueueWrapperBase* getEventQueue() { return nullptr; }
110
111 virtual Return<Result> initialize(const MQDescriptorSync<uint32_t>& /* wakeLockDesc */,
112 const ::android::sp<ISensorsCallback>& /* callback */) {
113 // Enforce this method is never invoked as it should be overridden if it's meant to be used.
114 assert(false);
115 return Result::INVALID_OPERATION;
Anthony Stangec002dd92020-02-12 14:41:41 -0500116 }
Anthony Stangec002dd92020-02-12 14:41:41 -0500117};
118
Anthony Stange32895902020-02-14 08:56:07 -0500119template <typename T>
120class SensorsWrapperBase : public ISensorsWrapperBase {
Anthony Stangec002dd92020-02-12 14:41:41 -0500121 public:
Anthony Stange32895902020-02-14 08:56:07 -0500122 SensorsWrapperBase(sp<T> sensors) : mSensors(sensors){};
Anthony Stangec002dd92020-02-12 14:41:41 -0500123
Anthony Stange32895902020-02-14 08:56:07 -0500124 void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient,
125 uint64_t cookie) override {
126 mSensors->linkToDeath(deathRecipient, cookie);
Anthony Stangec002dd92020-02-12 14:41:41 -0500127 }
128
Anthony Stange32895902020-02-14 08:56:07 -0500129 virtual Return<void> getSensorsList(
130 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
131 return mSensors->getSensorsList(
Anthony Stangec002dd92020-02-12 14:41:41 -0500132 [&](const auto& list) { _hidl_cb(convertToNewSensorInfos(list)); });
133 }
134
Anthony Stange32895902020-02-14 08:56:07 -0500135 Return<Result> setOperationMode(OperationMode mode) override {
136 return mSensors->setOperationMode(mode);
137 }
138
139 Return<Result> activate(int32_t sensorHandle, bool enabled) override {
140 return mSensors->activate(sensorHandle, enabled);
141 }
142
143 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
144 int64_t maxReportLatencyNs) override {
145 return mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
146 }
147
148 Return<Result> flush(int32_t sensorHandle) override { return mSensors->flush(sensorHandle); }
149
150 virtual Return<Result> injectSensorData(const Event& event) override {
Anthony Stangec002dd92020-02-12 14:41:41 -0500151 return mSensors->injectSensorData(convertToOldEvent(event));
152 }
153
Anthony Stange32895902020-02-14 08:56:07 -0500154 Return<void> registerDirectChannel(const SharedMemInfo& mem,
155 ISensors::registerDirectChannel_cb _hidl_cb) override {
156 return mSensors->registerDirectChannel(mem, _hidl_cb);
157 }
Anthony Stangec002dd92020-02-12 14:41:41 -0500158
Anthony Stange32895902020-02-14 08:56:07 -0500159 Return<Result> unregisterDirectChannel(int32_t channelHandle) override {
160 return mSensors->unregisterDirectChannel(channelHandle);
161 }
162
163 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
164 ISensors::configDirectReport_cb _hidl_cb) override {
165 return mSensors->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb);
166 }
167
168 protected:
169 sp<T> mSensors;
170};
171
172class ISensorsWrapperV1_0 : public SensorsWrapperBase<hardware::sensors::V1_0::ISensors> {
173 public:
174 ISensorsWrapperV1_0(sp<hardware::sensors::V1_0::ISensors> sensors)
175 : SensorsWrapperBase(sensors){};
176
177 bool supportsPolling() const override { return true; }
178
179 bool supportsMessageQueues() const override { return false; }
180
181 Return<void> poll(int32_t maxCount,
182 hardware::sensors::V1_0::ISensors::poll_cb _hidl_cb) override {
183 return mSensors->poll(maxCount, _hidl_cb);
184 }
185};
186
187class ISensorsWrapperV2_0 : public SensorsWrapperBase<hardware::sensors::V2_0::ISensors> {
188 public:
189 typedef MessageQueue<::android::hardware::sensors::V1_0::Event,
190 ::android::hardware::kSynchronizedReadWrite>
191 EventMessageQueue;
192
193 ISensorsWrapperV2_0(sp<hardware::sensors::V2_0::ISensors> sensors)
194 : SensorsWrapperBase(sensors) {
195 auto eventQueue = std::make_unique<EventMessageQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
196 true /* configureEventFlagWord */);
197 mEventQueue = std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue);
198 };
199
200 bool supportsPolling() const override { return false; }
201
202 bool supportsMessageQueues() const override { return true; }
203
204 EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); }
205
206 Return<Result> initialize(const MQDescriptorSync<uint32_t>& wakeLockDesc,
207 const ::android::sp<ISensorsCallback>& callback) override {
208 return mSensors->initialize(*mEventQueue->getDesc(), wakeLockDesc, callback);
209 }
210
211 private:
Anthony Stangec002dd92020-02-12 14:41:41 -0500212 std::unique_ptr<EventMessageQueueWrapperV1_0> mEventQueue;
213};
214
Anthony Stange32895902020-02-14 08:56:07 -0500215class ISensorsWrapperV2_1 : public SensorsWrapperBase<hardware::sensors::V2_1::ISensors> {
Anthony Stangec002dd92020-02-12 14:41:41 -0500216 public:
Anthony Stange32895902020-02-14 08:56:07 -0500217 typedef MessageQueue<Event, ::android::hardware::kSynchronizedReadWrite> EventMessageQueueV2_1;
Anthony Stangec002dd92020-02-12 14:41:41 -0500218
Anthony Stange32895902020-02-14 08:56:07 -0500219 ISensorsWrapperV2_1(sp<hardware::sensors::V2_1::ISensors> sensors)
220 : SensorsWrapperBase(sensors) {
221 auto eventQueue = std::make_unique<EventMessageQueueV2_1>(
222 MAX_RECEIVE_BUFFER_EVENT_COUNT, true /* configureEventFlagWord */);
Anthony Stangec002dd92020-02-12 14:41:41 -0500223 mEventQueue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue);
Anthony Stange32895902020-02-14 08:56:07 -0500224 };
Anthony Stangec002dd92020-02-12 14:41:41 -0500225
Anthony Stange32895902020-02-14 08:56:07 -0500226 bool supportsPolling() const override { return false; }
Anthony Stangec002dd92020-02-12 14:41:41 -0500227
Anthony Stange32895902020-02-14 08:56:07 -0500228 bool supportsMessageQueues() const override { return true; }
Anthony Stangec002dd92020-02-12 14:41:41 -0500229
Anthony Stange32895902020-02-14 08:56:07 -0500230 EventMessageQueueWrapperBase* getEventQueue() override { return mEventQueue.get(); }
231
232 Return<void> getSensorsList(
233 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
Anthony Stangec002dd92020-02-12 14:41:41 -0500234 return mSensors->getSensorsList_2_1(_hidl_cb);
235 }
236
Anthony Stange32895902020-02-14 08:56:07 -0500237 Return<Result> injectSensorData(const Event& event) override {
Anthony Stangec002dd92020-02-12 14:41:41 -0500238 return mSensors->injectSensorData_2_1(event);
239 }
240
Anthony Stange32895902020-02-14 08:56:07 -0500241 Return<Result> initialize(const MQDescriptorSync<uint32_t>& wakeLockDesc,
242 const ::android::sp<ISensorsCallback>& callback) override {
243 return mSensors->initialize_2_1(*mEventQueue->getDesc(), wakeLockDesc, callback);
244 }
Anthony Stangec002dd92020-02-12 14:41:41 -0500245
Anthony Stange32895902020-02-14 08:56:07 -0500246 private:
Anthony Stangec002dd92020-02-12 14:41:41 -0500247 std::unique_ptr<EventMessageQueueWrapperV2_1> mEventQueue;
248};
249
250inline sp<ISensorsWrapperV2_0> wrapISensors(sp<V2_0::ISensors> sensors) {
251 return new ISensorsWrapperV2_0(sensors);
252}
253
254inline sp<ISensorsWrapperV2_1> wrapISensors(sp<V2_1::ISensors> sensors) {
255 return new ISensorsWrapperV2_1(sensors);
256}
257
258class NoOpSensorsCallback : public ISensorsCallback {
259 public:
260 Return<void> onDynamicSensorsConnected(
261 const hidl_vec<V1_0::SensorInfo>& /* sensorInfos */) override {
262 return Return<void>();
263 }
264
265 Return<void> onDynamicSensorsDisconnected(
266 const hidl_vec<int32_t>& /* sensorHandles */) override {
267 return Return<void>();
268 }
269
270 Return<void> onDynamicSensorsConnected_2_1(
271 const hidl_vec<SensorInfo>& /* sensorInfos */) override {
272 return Return<void>();
273 }
274};
275
276} // namespace implementation
277} // namespace V2_1
278} // namespace sensors
279} // namespace hardware
280} // namespace android
281
Anthony Stange32895902020-02-14 08:56:07 -0500282#endif // ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H