blob: bdcc1ff7c11401bcea7499974ab983ba716ef7f2 [file] [log] [blame]
Anthony Stangea689f8a2019-07-30 11:35:48 -04001/*
2 * Copyright (C) 2019 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#pragma once
18
19#include "SubHal.h"
20
21#include <android/hardware/sensors/2.0/ISensors.h>
22#include <fmq/MessageQueue.h>
23#include <hardware_legacy/power.h>
24#include <hidl/MQDescriptor.h>
25#include <hidl/Status.h>
26
Stan Rokita537c0272019-09-13 10:36:07 -070027#include <map>
28
Anthony Stangea689f8a2019-07-30 11:35:48 -040029namespace android {
30namespace hardware {
31namespace sensors {
32namespace V2_0 {
33namespace implementation {
34
35using ::android::sp;
36using ::android::hardware::EventFlag;
37using ::android::hardware::hidl_string;
38using ::android::hardware::hidl_vec;
39using ::android::hardware::MessageQueue;
40using ::android::hardware::MQDescriptor;
41using ::android::hardware::Return;
42using ::android::hardware::Void;
43
Stan Rokitad0cd57d2019-09-17 15:52:51 -070044class HalProxy : public ISensors, public IScopedWakelockRefCounter {
Stan Rokitadc7a8e72019-08-23 12:35:40 -070045 public:
Anthony Stangea689f8a2019-07-30 11:35:48 -040046 using Event = ::android::hardware::sensors::V1_0::Event;
47 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
48 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
49 using Result = ::android::hardware::sensors::V1_0::Result;
Stan Rokita537c0272019-09-13 10:36:07 -070050 using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
Anthony Stangea689f8a2019-07-30 11:35:48 -040051 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
Stan Rokita537c0272019-09-13 10:36:07 -070052 using ISensorsSubHal = ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal;
Anthony Stangea689f8a2019-07-30 11:35:48 -040053
Anthony Stangeaacbf942019-08-30 15:21:34 -040054 explicit HalProxy();
55 // Test only constructor.
56 explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList);
Anthony Stangea689f8a2019-07-30 11:35:48 -040057 ~HalProxy();
58
59 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
60 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
61
62 Return<Result> setOperationMode(OperationMode mode) override;
63
64 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
65
66 Return<Result> initialize(
67 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
68 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
69 const sp<ISensorsCallback>& sensorsCallback) override;
70
71 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
72 int64_t maxReportLatencyNs) override;
73
74 Return<Result> flush(int32_t sensorHandle) override;
75
76 Return<Result> injectSensorData(const Event& event) override;
77
78 Return<void> registerDirectChannel(const SharedMemInfo& mem,
79 registerDirectChannel_cb _hidl_cb) override;
80
81 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
82
83 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
84 configDirectReport_cb _hidl_cb) override;
85
86 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
87
Anthony Stangeaacbf942019-08-30 15:21:34 -040088 // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
Anthony Stangea689f8a2019-07-30 11:35:48 -040089 // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
90 // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
91 // same process as the HalProxy, but potentially running on different threads.
92 Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded,
93 int32_t subHalIndex);
94
95 Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
96 int32_t subHalIndex);
97
Stan Rokitad0cd57d2019-09-17 15:52:51 -070098 // Below methods follow IScopedWakelockRefCounter
99
100 /**
101 * Increment ref count and maybe acquire wakelock.
102 */
103 void incrementRefCountAndMaybeAcquireWakelock() override;
104
105 /**
106 * Decrement ref count and maybe release wakelock.
107 */
108 void decrementRefCountAndMaybeReleaseWakelock() override;
109
Stan Rokita537c0272019-09-13 10:36:07 -0700110 // Below methods are for HalProxyCallback
111
112 /**
113 * Post events to the event message queue if there is room to write them. Otherwise post the
114 * remaining events to a background thread for a blocking write with a 5 second timeout.
115 *
116 * @param events The list of events to post to the message queue.
117 */
118 void postEventsToMessageQueue(const std::vector<Event>& events);
119
120 /**
121 * Get the SensorInfo object associated with the sensorHandle.
122 *
123 * @param sensorHandle The sensorHandle for the sensor.
124 *
125 * @return The sensor info for the sensor.
126 */
127 const SensorInfo& getSensorInfo(uint32_t sensorHandle) const {
128 return mSensors.at(sensorHandle);
129 }
130
Anthony Stangea689f8a2019-07-30 11:35:48 -0400131 private:
132 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
133 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
134
Stan Rokitad0cd57d2019-09-17 15:52:51 -0700135 const char* kWakeLockName = "SensorsHAL_WAKEUP";
136
Anthony Stangea689f8a2019-07-30 11:35:48 -0400137 /**
138 * The Event FMQ where sensor events are written
139 */
140 std::unique_ptr<EventMessageQueue> mEventQueue;
141
142 /**
143 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
144 */
145 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
146
147 /**
148 * Event Flag to signal to the framework when sensor events are available to be read
149 */
150 EventFlag* mEventQueueFlag;
151
152 /**
153 * Callback to the sensors framework to inform it that new sensors have been added or removed.
154 */
155 sp<ISensorsCallback> mDynamicSensorsCallback;
Stan Rokita28790672019-08-20 14:32:15 -0700156
157 /**
158 * SubHal object pointers that have been saved from vendor dynamic libraries.
159 */
160 std::vector<ISensorsSubHal*> mSubHalList;
Stan Rokita16385312019-09-10 14:54:36 -0700161
Stan Rokita537c0272019-09-13 10:36:07 -0700162 std::vector<const sp<IHalProxyCallback>> mSubHalCallbacks;
163
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700164 /**
Stan Rokita537c0272019-09-13 10:36:07 -0700165 * Map of sensor handles to SensorInfo objects that contains the sensor info from subhals as
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700166 * well as the modified sensor handle for the framework.
167 *
Stan Rokita537c0272019-09-13 10:36:07 -0700168 * The subhal index is encoded in the first byte of the sensor handle and the remaining
169 * bytes are generated by the subhal to identify the sensor.
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700170 */
Stan Rokita537c0272019-09-13 10:36:07 -0700171 std::map<uint32_t, SensorInfo> mSensors;
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700172
Stan Rokita7a723542019-08-29 15:47:50 -0700173 //! The current operation mode for all subhals.
174 OperationMode mCurrentOperationMode = OperationMode::NORMAL;
175
176 //! The single subHal that supports directChannel reporting.
177 ISensorsSubHal* mDirectChannelSubHal = nullptr;
178
Stan Rokita537c0272019-09-13 10:36:07 -0700179 //! The mutex for the event queue.
180 std::mutex mEventQueueMutex;
181
Stan Rokitad0cd57d2019-09-17 15:52:51 -0700182 //! The scoped wakelock ref count.
183 size_t mWakelockRefCount = 0;
184
185 //! The mutex guarding the mWakelockRefCount variable
186 std::mutex mWakelockRefCountMutex;
187
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700188 //! The bit mask used to get the subhal index from a sensor handle.
189 static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
190
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700191 /**
192 * Initialize the list of SubHal objects in mSubHalList by reading from dynamic libraries
193 * listed in a config file.
194 */
195 void initializeSubHalListFromConfigFile(const char* configFileName);
196
197 /**
Stan Rokita537c0272019-09-13 10:36:07 -0700198 * Initialize the HalProxyCallback vector using the list of subhals.
199 */
200 void initializeSubHalCallbacks();
201
202 /**
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700203 * Initialize the list of SensorInfo objects in mSensorList by getting sensors from each
204 * subhal.
205 */
206 void initializeSensorList();
207
Stan Rokita7a723542019-08-29 15:47:50 -0700208 /**
Stan Rokita537c0272019-09-13 10:36:07 -0700209 * Calls the above two helper methods which are shared in both ctors.
210 */
211 void initializeSubHalCallbacksAndSensorList();
212
213 /**
Stan Rokita7a723542019-08-29 15:47:50 -0700214 * Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel
215 * subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first
216 * direct channel enabled sensor seen.
217 *
218 * @param sensorInfo The SensorInfo object that may be altered to have direct channel support
219 * disabled.
220 * @param subHal The subhal pointer that the current sensorInfo object came from.
221 */
222 void setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal);
223
Stan Rokita16385312019-09-10 14:54:36 -0700224 /*
225 * Get the subhal pointer which can be found by indexing into the mSubHalList vector
226 * using the index from the first byte of sensorHandle.
227 *
228 * @param sensorHandle The handle used to identify a sensor in one of the subhals.
229 */
230 ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle);
231
232 /*
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700233 * Clear out the subhal index bytes from a sensorHandle.
Stan Rokita16385312019-09-10 14:54:36 -0700234 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700235 * @param sensorHandle The sensor handle to modify.
Stan Rokita16385312019-09-10 14:54:36 -0700236 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700237 * @return The modified version of the sensor handle.
Stan Rokita16385312019-09-10 14:54:36 -0700238 */
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700239 static uint32_t clearSubHalIndex(uint32_t sensorHandle);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400240};
241
Stan Rokita537c0272019-09-13 10:36:07 -0700242/**
243 * Callback class used to provide the HalProxy with the index of which subHal is invoking
244 */
245class HalProxyCallback : public IHalProxyCallback {
246 using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
247
248 public:
249 HalProxyCallback(HalProxy* halProxy, int32_t subHalIndex)
250 : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {}
251
252 Return<void> onDynamicSensorsConnected(
253 const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
254 return mHalProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
255 }
256
257 Return<void> onDynamicSensorsDisconnected(
258 const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
259 return mHalProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex);
260 }
261
262 void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock);
263
264 ScopedWakelock createScopedWakelock(bool lock);
265
266 private:
267 HalProxy* mHalProxy;
268 int32_t mSubHalIndex;
269
270 std::vector<Event> processEvents(const std::vector<Event>& events,
271 size_t* numWakeupEvents) const;
272
273 uint32_t setSubHalIndex(uint32_t sensorHandle) const;
274};
275
Anthony Stangea689f8a2019-07-30 11:35:48 -0400276} // namespace implementation
277} // namespace V2_0
278} // namespace sensors
279} // namespace hardware
280} // namespace android