blob: d06dd78dad418d3f727218d32f2a13d0fbdbdc2c [file] [log] [blame]
Brian Stack60fcdcf2018-10-16 14:51:34 -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_HARDWARE_SENSORS_V2_0_SENSORS_H
18#define ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H
19
Brian Stack897528d2018-10-23 10:38:03 -070020#include "Sensor.h"
21
Brian Stack60fcdcf2018-10-16 14:51:34 -070022#include <android/hardware/sensors/2.0/ISensors.h>
Brian Stack475d4d42018-10-19 15:58:09 -070023#include <fmq/MessageQueue.h>
Brian Stackf2aca3b2018-11-05 09:37:15 -080024#include <hardware_legacy/power.h>
Brian Stack60fcdcf2018-10-16 14:51:34 -070025#include <hidl/MQDescriptor.h>
26#include <hidl/Status.h>
27
Brian Stackf2aca3b2018-11-05 09:37:15 -080028#include <atomic>
Brian Stack475d4d42018-10-19 15:58:09 -070029#include <memory>
Brian Stackf2aca3b2018-11-05 09:37:15 -080030#include <thread>
Brian Stack475d4d42018-10-19 15:58:09 -070031
Brian Stack60fcdcf2018-10-16 14:51:34 -070032namespace android {
33namespace hardware {
34namespace sensors {
35namespace V2_0 {
36namespace implementation {
37
38using ::android::sp;
Brian Stack475d4d42018-10-19 15:58:09 -070039using ::android::hardware::EventFlag;
Brian Stack60fcdcf2018-10-16 14:51:34 -070040using ::android::hardware::hidl_array;
41using ::android::hardware::hidl_memory;
42using ::android::hardware::hidl_string;
43using ::android::hardware::hidl_vec;
Brian Stack475d4d42018-10-19 15:58:09 -070044using ::android::hardware::MessageQueue;
45using ::android::hardware::MQDescriptor;
Brian Stack60fcdcf2018-10-16 14:51:34 -070046using ::android::hardware::Return;
47using ::android::hardware::Void;
48
Brian Stack237abc62018-10-23 11:09:59 -070049struct Sensors : public ISensors, public ISensorsEventCallback {
Brian Stack60fcdcf2018-10-16 14:51:34 -070050 using Event = ::android::hardware::sensors::V1_0::Event;
51 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode;
52 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel;
53 using Result = ::android::hardware::sensors::V1_0::Result;
54 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo;
55
Brian Stack475d4d42018-10-19 15:58:09 -070056 Sensors();
57 virtual ~Sensors();
58
Brian Stack60fcdcf2018-10-16 14:51:34 -070059 // 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;
Brian Stack475d4d42018-10-19 15:58:09 -070085
Brian Stackf2aca3b2018-11-05 09:37:15 -080086 void postEvents(const std::vector<Event>& events, bool wakeup) override;
Brian Stack237abc62018-10-23 11:09:59 -070087
Brian Stack475d4d42018-10-19 15:58:09 -070088 private:
89 /**
Brian Stacka2d16bb2019-01-25 11:22:04 -080090 * Add a new sensor
91 */
92 template <class SensorType>
93 void AddSensor() {
94 std::shared_ptr<SensorType> sensor =
95 std::make_shared<SensorType>(mNextHandle++ /* sensorHandle */, this /* callback */);
96 mSensors[sensor->getSensorInfo().sensorHandle] = sensor;
97 }
98
99 /**
Brian Stack475d4d42018-10-19 15:58:09 -0700100 * Utility function to delete the Event Flag
101 */
102 void deleteEventFlag();
103
Brian Stackf2aca3b2018-11-05 09:37:15 -0800104 /**
105 * Function to read the Wake Lock FMQ and release the wake lock when appropriate
106 */
107 void readWakeLockFMQ();
108
109 static void startReadWakeLockThread(Sensors* sensors);
110
111 /**
112 * Responsible for acquiring and releasing a wake lock when there are unhandled WAKE_UP events
113 */
114 void updateWakeLock(int32_t eventsWritten, int32_t eventsHandled);
115
Brian Stack475d4d42018-10-19 15:58:09 -0700116 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
117 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
118
119 /**
120 * The Event FMQ where sensor events are written
121 */
122 std::unique_ptr<EventMessageQueue> mEventQueue;
123
124 /**
125 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
126 */
127 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
128
129 /**
130 * Event Flag to signal to the framework when sensor events are available to be read
131 */
132 EventFlag* mEventQueueFlag;
133
134 /**
135 * Callback for asynchronous events, such as dynamic sensor connections.
136 */
137 sp<ISensorsCallback> mCallback;
Brian Stack897528d2018-10-23 10:38:03 -0700138
139 /**
140 * A map of the available sensors
141 */
142 std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
Brian Stack237abc62018-10-23 11:09:59 -0700143
144 /**
Brian Stacka2d16bb2019-01-25 11:22:04 -0800145 * The next available sensor handle
146 */
147 int32_t mNextHandle;
148
149 /**
Brian Stackf2aca3b2018-11-05 09:37:15 -0800150 * Lock to protect writes to the FMQs
Brian Stack237abc62018-10-23 11:09:59 -0700151 */
Brian Stackf2aca3b2018-11-05 09:37:15 -0800152 std::mutex mWriteLock;
153
154 /**
155 * Lock to protect acquiring and releasing the wake lock
156 */
157 std::mutex mWakeLockLock;
158
159 /**
160 * Track the number of WAKE_UP events that have not been handled by the framework
161 */
162 uint32_t mOutstandingWakeUpEvents;
163
164 /**
165 * A thread to read the Wake Lock FMQ
166 */
167 std::thread mWakeLockThread;
168
169 /**
170 * Flag to indicate that the Wake Lock Thread should continue to run
171 */
172 std::atomic_bool mReadWakeLockQueueRun;
173
174 /**
175 * Track the time when the wake lock should automatically be released
176 */
177 int64_t mAutoReleaseWakeLockTime;
178
179 /**
180 * Flag to indicate if a wake lock has been acquired
181 */
182 bool mHasWakeLock;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700183};
184
185} // namespace implementation
186} // namespace V2_0
187} // namespace sensors
188} // namespace hardware
189} // namespace android
190
191#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H