blob: 6592afef967e52cf6b687ea8dcb49017d466116e [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
Stan Rokita75cc7bf2019-09-26 13:17:01 -070019#include "ScopedWakelock.h"
Anthony Stangea689f8a2019-07-30 11:35:48 -040020#include "SubHal.h"
21
22#include <android/hardware/sensors/2.0/ISensors.h>
Stan Rokita75cc7bf2019-09-26 13:17:01 -070023#include <android/hardware/sensors/2.0/types.h>
Anthony Stangea689f8a2019-07-30 11:35:48 -040024#include <fmq/MessageQueue.h>
25#include <hardware_legacy/power.h>
26#include <hidl/MQDescriptor.h>
27#include <hidl/Status.h>
28
Stan Rokita59714262019-09-20 10:55:30 -070029#include <atomic>
30#include <condition_variable>
Stan Rokita537c0272019-09-13 10:36:07 -070031#include <map>
Stan Rokita59714262019-09-20 10:55:30 -070032#include <mutex>
33#include <queue>
34#include <thread>
Stan Rokita75cc7bf2019-09-26 13:17:01 -070035#include <utility>
Stan Rokita537c0272019-09-13 10:36:07 -070036
Anthony Stangea689f8a2019-07-30 11:35:48 -040037namespace android {
38namespace hardware {
39namespace sensors {
40namespace V2_0 {
41namespace implementation {
42
43using ::android::sp;
44using ::android::hardware::EventFlag;
45using ::android::hardware::hidl_string;
46using ::android::hardware::hidl_vec;
47using ::android::hardware::MessageQueue;
48using ::android::hardware::MQDescriptor;
49using ::android::hardware::Return;
50using ::android::hardware::Void;
51
Stan Rokitad0cd57d2019-09-17 15:52:51 -070052class HalProxy : public ISensors, public IScopedWakelockRefCounter {
Stan Rokitadc7a8e72019-08-23 12:35:40 -070053 public:
Anthony Stangea689f8a2019-07-30 11:35:48 -040054 using Event = ::android::hardware::sensors::V1_0::Event;
55 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
56 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
57 using Result = ::android::hardware::sensors::V1_0::Result;
Stan Rokita537c0272019-09-13 10:36:07 -070058 using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
Anthony Stangea689f8a2019-07-30 11:35:48 -040059 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
Stan Rokita537c0272019-09-13 10:36:07 -070060 using ISensorsSubHal = ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal;
Anthony Stangea689f8a2019-07-30 11:35:48 -040061
Anthony Stangeaacbf942019-08-30 15:21:34 -040062 explicit HalProxy();
63 // Test only constructor.
64 explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList);
Anthony Stangea689f8a2019-07-30 11:35:48 -040065 ~HalProxy();
66
67 // Methods from ::android::hardware::sensors::V2_0::ISensors follow.
68 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override;
69
70 Return<Result> setOperationMode(OperationMode mode) override;
71
72 Return<Result> activate(int32_t sensorHandle, bool enabled) override;
73
74 Return<Result> initialize(
75 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
76 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
77 const sp<ISensorsCallback>& sensorsCallback) override;
78
79 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs,
80 int64_t maxReportLatencyNs) override;
81
82 Return<Result> flush(int32_t sensorHandle) override;
83
84 Return<Result> injectSensorData(const Event& event) override;
85
86 Return<void> registerDirectChannel(const SharedMemInfo& mem,
87 registerDirectChannel_cb _hidl_cb) override;
88
89 Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
90
91 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
92 configDirectReport_cb _hidl_cb) override;
93
94 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
95
Anthony Stangeaacbf942019-08-30 15:21:34 -040096 // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change
Anthony Stangea689f8a2019-07-30 11:35:48 -040097 // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework
98 // via the binder, these methods are invoked from a callback provided to sub-HALs inside the
99 // same process as the HalProxy, but potentially running on different threads.
100 Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded,
101 int32_t subHalIndex);
102
103 Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved,
104 int32_t subHalIndex);
105
Stan Rokita537c0272019-09-13 10:36:07 -0700106 // Below methods are for HalProxyCallback
107
108 /**
109 * Post events to the event message queue if there is room to write them. Otherwise post the
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700110 * remaining events to a background thread for a blocking write with a kPendingWriteTimeoutNs
111 * timeout.
Stan Rokita537c0272019-09-13 10:36:07 -0700112 *
113 * @param events The list of events to post to the message queue.
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700114 * @param numWakeupEvents The number of wakeup events in events.
115 * @param wakelock The wakelock associated with this post of events.
Stan Rokita537c0272019-09-13 10:36:07 -0700116 */
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700117 void postEventsToMessageQueue(const std::vector<Event>& events, size_t numWakeupEvents,
118 ScopedWakelock wakelock);
Stan Rokita537c0272019-09-13 10:36:07 -0700119
120 /**
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700121 * Get the sensor info associated with that sensorHandle.
Stan Rokita537c0272019-09-13 10:36:07 -0700122 *
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700123 * @param sensorHandle The sensor handle.
Stan Rokita537c0272019-09-13 10:36:07 -0700124 *
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700125 * @return The sensor info object in the mapping.
Stan Rokita537c0272019-09-13 10:36:07 -0700126 */
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700127 const SensorInfo& getSensorInfo(uint32_t sensorHandle) { return mSensors[sensorHandle]; }
128
129 bool areThreadsRunning() { return mThreadsRun.load(); }
130
131 // Below methods are from IScopedWakelockRefCounter interface
132 bool incrementRefCountAndMaybeAcquireWakelock(size_t delta,
133 int64_t* timeoutStart = nullptr) override;
134
135 void decrementRefCountAndMaybeReleaseWakelock(size_t delta, int64_t timeoutStart = -1) override;
Stan Rokita537c0272019-09-13 10:36:07 -0700136
Anthony Stangea689f8a2019-07-30 11:35:48 -0400137 private:
138 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
139 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
140
141 /**
142 * The Event FMQ where sensor events are written
143 */
144 std::unique_ptr<EventMessageQueue> mEventQueue;
145
146 /**
147 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
148 */
149 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
150
151 /**
152 * Event Flag to signal to the framework when sensor events are available to be read
153 */
154 EventFlag* mEventQueueFlag;
155
156 /**
157 * Callback to the sensors framework to inform it that new sensors have been added or removed.
158 */
159 sp<ISensorsCallback> mDynamicSensorsCallback;
Stan Rokita28790672019-08-20 14:32:15 -0700160
161 /**
162 * SubHal object pointers that have been saved from vendor dynamic libraries.
163 */
164 std::vector<ISensorsSubHal*> mSubHalList;
Stan Rokita16385312019-09-10 14:54:36 -0700165
Stan Rokita59714262019-09-20 10:55:30 -0700166 //! The list of subhal callbacks for each subhal where the indices correlate with mSubHalList
Stan Rokita537c0272019-09-13 10:36:07 -0700167 std::vector<const sp<IHalProxyCallback>> mSubHalCallbacks;
168
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700169 /**
Stan Rokita537c0272019-09-13 10:36:07 -0700170 * Map of sensor handles to SensorInfo objects that contains the sensor info from subhals as
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700171 * well as the modified sensor handle for the framework.
172 *
Stan Rokita537c0272019-09-13 10:36:07 -0700173 * The subhal index is encoded in the first byte of the sensor handle and the remaining
174 * bytes are generated by the subhal to identify the sensor.
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700175 */
Stan Rokita537c0272019-09-13 10:36:07 -0700176 std::map<uint32_t, SensorInfo> mSensors;
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700177
Stan Rokitae93fdf92019-09-24 11:58:51 -0700178 //! Map of the dynamic sensors that have been added to halproxy.
179 std::map<uint32_t, SensorInfo> mDynamicSensors;
180
Stan Rokita7a723542019-08-29 15:47:50 -0700181 //! The current operation mode for all subhals.
182 OperationMode mCurrentOperationMode = OperationMode::NORMAL;
183
184 //! The single subHal that supports directChannel reporting.
185 ISensorsSubHal* mDirectChannelSubHal = nullptr;
186
Stan Rokita59714262019-09-20 10:55:30 -0700187 //! The timeout for each pending write on background thread for events.
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700188 static const int64_t kPendingWriteTimeoutNs = 5 * INT64_C(1000000000) /* 5 seconds */;
Stan Rokitad0cd57d2019-09-17 15:52:51 -0700189
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700190 //! The bit mask used to get the subhal index from a sensor handle.
191 static constexpr uint32_t kSensorHandleSubHalIndexMask = 0xFF000000;
192
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700193 /**
194 * A FIFO queue of pairs of vector of events and the number of wakeup events in that vector
195 * which are waiting to be written to the events fmq in the background thread.
196 */
197 std::queue<std::pair<std::vector<Event>, size_t>> mPendingWriteEventsQueue;
Stan Rokita59714262019-09-20 10:55:30 -0700198
199 //! The mutex protecting writing to the fmq and the pending events queue
200 std::mutex mEventQueueWriteMutex;
201
202 //! The condition variable waiting on pending write events to stack up
203 std::condition_variable mEventQueueWriteCV;
204
205 //! The thread object ptr that handles pending writes
206 std::thread mPendingWritesThread;
207
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700208 //! The thread object that handles wakelocks
209 std::thread mWakelockThread;
210
211 //! The bool indicating whether to end the threads started in initialize
212 std::atomic_bool mThreadsRun = true;
Stan Rokita59714262019-09-20 10:55:30 -0700213
Stan Rokitae93fdf92019-09-24 11:58:51 -0700214 //! The mutex protecting access to the dynamic sensors added and removed methods.
215 std::mutex mDynamicSensorsMutex;
216
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700217 // WakelockRefCount membar vars below
218
219 //! The mutex protecting the wakelock refcount and subsequent wakelock releases and
220 //! acquisitions
221 std::recursive_mutex mWakelockMutex;
222
223 std::condition_variable_any mWakelockCV;
224
225 //! The refcount of how many ScopedWakelocks and pending wakeup events are active
226 size_t mWakelockRefCount = 0;
227
228 int64_t mWakelockTimeoutStartTime = getTimeNow();
229
230 int64_t mWakelockTimeoutResetTime = getTimeNow();
231
232 const char* kWakelockName = "SensorsMultiHal";
233
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700234 /**
235 * Initialize the list of SubHal objects in mSubHalList by reading from dynamic libraries
236 * listed in a config file.
237 */
238 void initializeSubHalListFromConfigFile(const char* configFileName);
239
240 /**
Stan Rokita537c0272019-09-13 10:36:07 -0700241 * Initialize the HalProxyCallback vector using the list of subhals.
242 */
243 void initializeSubHalCallbacks();
244
245 /**
Stan Rokitadc7a8e72019-08-23 12:35:40 -0700246 * Initialize the list of SensorInfo objects in mSensorList by getting sensors from each
247 * subhal.
248 */
249 void initializeSensorList();
250
Stan Rokita7a723542019-08-29 15:47:50 -0700251 /**
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700252 * Calls the helper methods that all ctors use.
Stan Rokita537c0272019-09-13 10:36:07 -0700253 */
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700254 void init();
Stan Rokita537c0272019-09-13 10:36:07 -0700255
256 /**
Stan Rokita59714262019-09-20 10:55:30 -0700257 * Starts the thread that handles pending writes to event fmq.
258 *
259 * @param halProxy The HalProxy object pointer.
260 */
261 static void startPendingWritesThread(HalProxy* halProxy);
262
263 //! Handles the pending writes on events to eventqueue.
264 void handlePendingWrites();
265
266 /**
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700267 * Starts the thread that handles decrementing the ref count on wakeup events processed by the
268 * framework and timing out wakelocks.
269 *
270 * @param halProxy The HalProxy object pointer.
271 */
272 static void startWakelockThread(HalProxy* halProxy);
273
274 //! Handles the wakelocks.
275 void handleWakelocks();
276
277 /**
278 * @param timeLeft The variable that should be set to the timeleft before timeout will occur or
279 * unmodified if timeout occurred.
280 *
281 * @return true if the shared wakelock has been held passed the timeout and should be released
282 */
283 bool sharedWakelockDidTimeout(int64_t* timeLeft);
284
285 /**
286 * Reset all the member variables associated with the wakelock ref count and maybe release
287 * the shared wakelock.
288 */
289 void resetSharedWakelock();
290
291 /**
Stan Rokita7a723542019-08-29 15:47:50 -0700292 * Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel
293 * subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first
294 * direct channel enabled sensor seen.
295 *
296 * @param sensorInfo The SensorInfo object that may be altered to have direct channel support
297 * disabled.
298 * @param subHal The subhal pointer that the current sensorInfo object came from.
299 */
300 void setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal);
301
Stan Rokita16385312019-09-10 14:54:36 -0700302 /*
303 * Get the subhal pointer which can be found by indexing into the mSubHalList vector
304 * using the index from the first byte of sensorHandle.
305 *
306 * @param sensorHandle The handle used to identify a sensor in one of the subhals.
307 */
308 ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle);
309
Stan Rokita75cc7bf2019-09-26 13:17:01 -0700310 /**
311 * Count the number of wakeup events in the first n events of the vector.
312 *
313 * @param events The vector of Event objects.
314 * @param n The end index not inclusive of events to consider.
315 *
316 * @return The number of wakeup events of the considered events.
317 */
318 size_t countNumWakeupEvents(const std::vector<Event>& events, size_t n);
319
Stan Rokita16385312019-09-10 14:54:36 -0700320 /*
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700321 * Clear out the subhal index bytes from a sensorHandle.
Stan Rokita16385312019-09-10 14:54:36 -0700322 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700323 * @param sensorHandle The sensor handle to modify.
Stan Rokita16385312019-09-10 14:54:36 -0700324 *
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700325 * @return The modified version of the sensor handle.
Stan Rokita16385312019-09-10 14:54:36 -0700326 */
Stan Rokitaf97a3f32019-09-13 09:58:47 -0700327 static uint32_t clearSubHalIndex(uint32_t sensorHandle);
Stan Rokitae93fdf92019-09-24 11:58:51 -0700328
329 /**
330 * @param sensorHandle The sensor handle to modify.
331 *
332 * @return true if subHalIndex byte of sensorHandle is zeroed.
333 */
334 static bool subHalIndexIsClear(uint32_t sensorHandle);
Anthony Stangea689f8a2019-07-30 11:35:48 -0400335};
336
Stan Rokita537c0272019-09-13 10:36:07 -0700337/**
338 * Callback class used to provide the HalProxy with the index of which subHal is invoking
339 */
340class HalProxyCallback : public IHalProxyCallback {
341 using SensorInfo = ::android::hardware::sensors::V1_0::SensorInfo;
342
343 public:
344 HalProxyCallback(HalProxy* halProxy, int32_t subHalIndex)
345 : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {}
346
347 Return<void> onDynamicSensorsConnected(
348 const hidl_vec<SensorInfo>& dynamicSensorsAdded) override {
349 return mHalProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex);
350 }
351
352 Return<void> onDynamicSensorsDisconnected(
353 const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override {
354 return mHalProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, mSubHalIndex);
355 }
356
357 void postEvents(const std::vector<Event>& events, ScopedWakelock wakelock);
358
359 ScopedWakelock createScopedWakelock(bool lock);
360
361 private:
362 HalProxy* mHalProxy;
363 int32_t mSubHalIndex;
364
365 std::vector<Event> processEvents(const std::vector<Event>& events,
366 size_t* numWakeupEvents) const;
Stan Rokita537c0272019-09-13 10:36:07 -0700367};
368
Anthony Stangea689f8a2019-07-30 11:35:48 -0400369} // namespace implementation
370} // namespace V2_0
371} // namespace sensors
372} // namespace hardware
373} // namespace android