blob: eba3f97a089986fc74fd6395cf1feb23655df1e5 [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 /**
90 * Utility function to delete the Event Flag
91 */
92 void deleteEventFlag();
93
Brian Stackf2aca3b2018-11-05 09:37:15 -080094 /**
95 * Function to read the Wake Lock FMQ and release the wake lock when appropriate
96 */
97 void readWakeLockFMQ();
98
99 static void startReadWakeLockThread(Sensors* sensors);
100
101 /**
102 * Responsible for acquiring and releasing a wake lock when there are unhandled WAKE_UP events
103 */
104 void updateWakeLock(int32_t eventsWritten, int32_t eventsHandled);
105
Brian Stack475d4d42018-10-19 15:58:09 -0700106 using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>;
107 using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>;
108
109 /**
110 * The Event FMQ where sensor events are written
111 */
112 std::unique_ptr<EventMessageQueue> mEventQueue;
113
114 /**
115 * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events
116 */
117 std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue;
118
119 /**
120 * Event Flag to signal to the framework when sensor events are available to be read
121 */
122 EventFlag* mEventQueueFlag;
123
124 /**
125 * Callback for asynchronous events, such as dynamic sensor connections.
126 */
127 sp<ISensorsCallback> mCallback;
Brian Stack897528d2018-10-23 10:38:03 -0700128
129 /**
130 * A map of the available sensors
131 */
132 std::map<int32_t, std::shared_ptr<Sensor>> mSensors;
Brian Stack237abc62018-10-23 11:09:59 -0700133
134 /**
Brian Stackf2aca3b2018-11-05 09:37:15 -0800135 * Lock to protect writes to the FMQs
Brian Stack237abc62018-10-23 11:09:59 -0700136 */
Brian Stackf2aca3b2018-11-05 09:37:15 -0800137 std::mutex mWriteLock;
138
139 /**
140 * Lock to protect acquiring and releasing the wake lock
141 */
142 std::mutex mWakeLockLock;
143
144 /**
145 * Track the number of WAKE_UP events that have not been handled by the framework
146 */
147 uint32_t mOutstandingWakeUpEvents;
148
149 /**
150 * A thread to read the Wake Lock FMQ
151 */
152 std::thread mWakeLockThread;
153
154 /**
155 * Flag to indicate that the Wake Lock Thread should continue to run
156 */
157 std::atomic_bool mReadWakeLockQueueRun;
158
159 /**
160 * Track the time when the wake lock should automatically be released
161 */
162 int64_t mAutoReleaseWakeLockTime;
163
164 /**
165 * Flag to indicate if a wake lock has been acquired
166 */
167 bool mHasWakeLock;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700168};
169
170} // namespace implementation
171} // namespace V2_0
172} // namespace sensors
173} // namespace hardware
174} // namespace android
175
176#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSORS_H