blob: 4c8b442396071f443c0c39a00f5eca6b106a07bf [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"
21#include "convertV2_1.h"
22
23#include <android/hardware/sensors/2.1/ISensors.h>
24#include <android/hardware/sensors/2.1/types.h>
25#include <binder/IBinder.h>
26#include <fmq/MessageQueue.h>
27#include <hidl/MQDescriptor.h>
28#include <hidl/Status.h>
29#include <log/log.h>
30
31#include <atomic>
32
33/**
34 * ISensorsWrapperBase wraps around the V2_1::ISensors APIs to make any HAL 2.0/2.1 interface
35 * appear as a HAL 2.1 implementation. This ensures the maximum amount of code can be shared
36 * between VTS, default implementations, and the sensors framework.
37 */
38
39namespace android {
40namespace hardware {
41namespace sensors {
42namespace V2_1 {
43namespace implementation {
44
45using ::android::sp;
46using ::android::hardware::Return;
47using ::android::hardware::Void;
48using ::android::hardware::sensors::V1_0::OperationMode;
49using ::android::hardware::sensors::V1_0::RateLevel;
50using ::android::hardware::sensors::V1_0::Result;
51using ::android::hardware::sensors::V1_0::SharedMemInfo;
52
53// TODO: Look into providing this as a param if it needs to be a different value
54// than the framework.
55static constexpr size_t MAX_RECEIVE_BUFFER_EVENT_COUNT = 256;
56
57class ISensorsWrapperBase : public RefBase {
58 public:
59 virtual ~ISensorsWrapperBase() {}
60
61 virtual EventMessageQueueWrapperBase& getEventQueue() = 0;
62 virtual Return<void> getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) = 0;
63 virtual Return<Result> injectSensorData(const V2_1::Event& event) = 0;
64 virtual Return<Result> initialize(
65 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
66 const sp<V2_1::ISensorsCallback>& sensorsCallback) = 0;
67
68 // V2_0::ISensors implementation
69 void linkToDeath(android::sp<android::hardware::hidl_death_recipient> deathRecipient,
70 uint64_t cookie) {
71 getSensors()->linkToDeath(deathRecipient, cookie);
72 }
73
74 Return<Result> activate(int32_t sensorHandle, bool enabled) {
75 return getSensors()->activate(sensorHandle, enabled);
76 }
77
78 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
79 int64_t maxReportLatencyNs) {
80 return getSensors()->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs);
81 }
82
83 Return<Result> flush(int32_t sensorHandle) { return getSensors()->flush(sensorHandle); }
84
85 Return<void> registerDirectChannel(const SharedMemInfo& mem,
86 V2_0::ISensors::registerDirectChannel_cb _hidl_cb) {
87 return getSensors()->registerDirectChannel(mem, _hidl_cb);
88 }
89
90 Return<Result> unregisterDirectChannel(int32_t channelHandle) {
91 return getSensors()->unregisterDirectChannel(channelHandle);
92 }
93
94 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
95 V2_0::ISensors::configDirectReport_cb _hidl_cb) {
96 return getSensors()->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb);
97 }
98
99 Return<Result> setOperationMode(OperationMode mode) {
100 return getSensors()->setOperationMode(mode);
101 }
102
103 private:
104 virtual V2_0::ISensors* getSensors() = 0;
105};
106
107class ISensorsWrapperV2_0 : public ISensorsWrapperBase {
108 public:
109 typedef MessageQueue<V1_0::Event, ::android::hardware::kSynchronizedReadWrite>
110 EventMessageQueue;
111
112 ISensorsWrapperV2_0(sp<V2_0::ISensors>& sensors) : mSensors(sensors) {
113 auto eventQueue = std::make_unique<EventMessageQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
114 true /* configureEventFlagWord */);
115 mEventQueue = std::make_unique<EventMessageQueueWrapperV1_0>(eventQueue);
116 }
117
118 EventMessageQueueWrapperBase& getEventQueue() override { return *mEventQueue; }
119
120 Return<Result> initialize(
121 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
122 const sp<V2_1::ISensorsCallback>& sensorsCallback) override {
123 return mSensors->initialize(*mEventQueue->getDesc(), wakeLockDescriptor, sensorsCallback);
124 }
125
126 Return<void> getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
127 return getSensors()->getSensorsList(
128 [&](const auto& list) { _hidl_cb(convertToNewSensorInfos(list)); });
129 }
130
131 Return<Result> injectSensorData(const V2_1::Event& event) override {
132 return mSensors->injectSensorData(convertToOldEvent(event));
133 }
134
135 private:
136 V2_0::ISensors* getSensors() override { return mSensors.get(); }
137
138 sp<V2_0::ISensors> mSensors;
139 std::unique_ptr<EventMessageQueueWrapperV1_0> mEventQueue;
140};
141
142class ISensorsWrapperV2_1 : public ISensorsWrapperBase {
143 public:
144 typedef MessageQueue<V2_1::Event, ::android::hardware::kSynchronizedReadWrite>
145 EventMessageQueue;
146
147 ISensorsWrapperV2_1(sp<V2_1::ISensors>& sensors) : mSensors(sensors) {
148 auto eventQueue = std::make_unique<EventMessageQueue>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
149 true /* configureEventFlagWord */);
150 mEventQueue = std::make_unique<EventMessageQueueWrapperV2_1>(eventQueue);
151 }
152
153 EventMessageQueueWrapperBase& getEventQueue() override { return *mEventQueue; }
154
155 Return<Result> initialize(
156 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
157 const sp<V2_1::ISensorsCallback>& sensorsCallback) override {
158 return mSensors->initialize_2_1(*mEventQueue->getDesc(), wakeLockDescriptor,
159 sensorsCallback);
160 }
161
162 Return<void> getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override {
163 return mSensors->getSensorsList_2_1(_hidl_cb);
164 }
165
166 Return<Result> injectSensorData(const V2_1::Event& event) override {
167 return mSensors->injectSensorData_2_1(event);
168 }
169
170 private:
171 V2_0::ISensors* getSensors() override { return mSensors.get(); }
172
173 sp<V2_1::ISensors> mSensors;
174 std::unique_ptr<EventMessageQueueWrapperV2_1> mEventQueue;
175};
176
177inline sp<ISensorsWrapperV2_0> wrapISensors(sp<V2_0::ISensors> sensors) {
178 return new ISensorsWrapperV2_0(sensors);
179}
180
181inline sp<ISensorsWrapperV2_1> wrapISensors(sp<V2_1::ISensors> sensors) {
182 return new ISensorsWrapperV2_1(sensors);
183}
184
185class NoOpSensorsCallback : public ISensorsCallback {
186 public:
187 Return<void> onDynamicSensorsConnected(
188 const hidl_vec<V1_0::SensorInfo>& /* sensorInfos */) override {
189 return Return<void>();
190 }
191
192 Return<void> onDynamicSensorsDisconnected(
193 const hidl_vec<int32_t>& /* sensorHandles */) override {
194 return Return<void>();
195 }
196
197 Return<void> onDynamicSensorsConnected_2_1(
198 const hidl_vec<SensorInfo>& /* sensorInfos */) override {
199 return Return<void>();
200 }
201};
202
203} // namespace implementation
204} // namespace V2_1
205} // namespace sensors
206} // namespace hardware
207} // namespace android
208
209#endif // ANDROID_HARDWARE_SENSORS_V2_1_ISENSORSWRAPPER_H