blob: 15fe86f7886cdbf24acdaf739e161e04ea00776c [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#include "Sensors.h"
18
Brian Stack897528d2018-10-23 10:38:03 -070019#include <android/hardware/sensors/2.0/types.h>
Brian Stack475d4d42018-10-19 15:58:09 -070020#include <log/log.h>
21
Brian Stack60fcdcf2018-10-16 14:51:34 -070022namespace android {
23namespace hardware {
24namespace sensors {
25namespace V2_0 {
26namespace implementation {
27
28using ::android::hardware::sensors::V1_0::Event;
29using ::android::hardware::sensors::V1_0::OperationMode;
30using ::android::hardware::sensors::V1_0::RateLevel;
31using ::android::hardware::sensors::V1_0::Result;
32using ::android::hardware::sensors::V1_0::SharedMemInfo;
Brian Stackf2aca3b2018-11-05 09:37:15 -080033using ::android::hardware::sensors::V2_0::SensorTimeout;
Brian Stack2c313362019-01-08 13:09:49 -080034using ::android::hardware::sensors::V2_0::WakeLockQueueFlagBits;
Brian Stack60fcdcf2018-10-16 14:51:34 -070035
Brian Stackf2aca3b2018-11-05 09:37:15 -080036constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP";
37
38Sensors::Sensors()
39 : mEventQueueFlag(nullptr),
40 mOutstandingWakeUpEvents(0),
41 mReadWakeLockQueueRun(false),
42 mAutoReleaseWakeLockTime(0),
43 mHasWakeLock(false) {
Brian Stack2927ab72018-10-23 11:22:55 -070044 std::shared_ptr<AccelSensor> accel =
45 std::make_shared<AccelSensor>(1 /* sensorHandle */, this /* callback */);
46 mSensors[accel->getSensorInfo().sensorHandle] = accel;
47}
Brian Stack475d4d42018-10-19 15:58:09 -070048
49Sensors::~Sensors() {
50 deleteEventFlag();
Brian Stackf2aca3b2018-11-05 09:37:15 -080051 mReadWakeLockQueueRun = false;
52 mWakeLockThread.join();
Brian Stack475d4d42018-10-19 15:58:09 -070053}
54
Brian Stack60fcdcf2018-10-16 14:51:34 -070055// Methods from ::android::hardware::sensors::V2_0::ISensors follow.
Brian Stack897528d2018-10-23 10:38:03 -070056Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
57 std::vector<SensorInfo> sensors;
58 for (const auto& sensor : mSensors) {
59 sensors.push_back(sensor.second->getSensorInfo());
60 }
61
62 // Call the HIDL callback with the SensorInfo
63 _hidl_cb(sensors);
64
Brian Stack60fcdcf2018-10-16 14:51:34 -070065 return Void();
66}
67
Brian Stackd23f2002018-11-05 13:48:16 -080068Return<Result> Sensors::setOperationMode(OperationMode mode) {
69 for (auto sensor : mSensors) {
70 sensor.second->setOperationMode(mode);
71 }
72 return Result::OK;
Brian Stack60fcdcf2018-10-16 14:51:34 -070073}
74
Brian Stack897528d2018-10-23 10:38:03 -070075Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
76 auto sensor = mSensors.find(sensorHandle);
77 if (sensor != mSensors.end()) {
78 sensor->second->activate(enabled);
79 return Result::OK;
80 }
81 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -070082}
83
84Return<Result> Sensors::initialize(
Brian Stack475d4d42018-10-19 15:58:09 -070085 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
86 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
87 const sp<ISensorsCallback>& sensorsCallback) {
88 Result result = Result::OK;
89
Brian Stackd3849e12019-01-07 12:57:12 -080090 // Ensure that all sensors are disabled
91 for (auto sensor : mSensors) {
92 sensor.second->activate(false /* enable */);
93 }
94
95 // Stop the Wake Lock thread if it is currently running
96 if (mReadWakeLockQueueRun.load()) {
97 mReadWakeLockQueueRun = false;
98 mWakeLockThread.join();
99 }
100
Brian Stack475d4d42018-10-19 15:58:09 -0700101 // Save a reference to the callback
102 mCallback = sensorsCallback;
103
104 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
105 mEventQueue =
106 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
107
108 // Ensure that any existing EventFlag is properly deleted
109 deleteEventFlag();
110
111 // Create the EventFlag that is used to signal to the framework that sensor events have been
112 // written to the Event FMQ
113 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
114 result = Result::BAD_VALUE;
115 }
116
117 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
118 // events have been successfully read and handled by the framework.
119 mWakeLockQueue =
120 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
121
122 if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
123 result = Result::BAD_VALUE;
124 }
125
Brian Stackf2aca3b2018-11-05 09:37:15 -0800126 // Start the thread to read events from the Wake Lock FMQ
127 mReadWakeLockQueueRun = true;
128 mWakeLockThread = std::thread(startReadWakeLockThread, this);
129
Brian Stack475d4d42018-10-19 15:58:09 -0700130 return result;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700131}
132
Brian Stack897528d2018-10-23 10:38:03 -0700133Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
Brian Stack60fcdcf2018-10-16 14:51:34 -0700134 int64_t /* maxReportLatencyNs */) {
Brian Stack897528d2018-10-23 10:38:03 -0700135 auto sensor = mSensors.find(sensorHandle);
Brian Stack237abc62018-10-23 11:09:59 -0700136 if (sensor != mSensors.end()) {
137 sensor->second->batch(samplingPeriodNs);
138 return Result::OK;
Brian Stack897528d2018-10-23 10:38:03 -0700139 }
Brian Stack237abc62018-10-23 11:09:59 -0700140 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700141}
142
Brian Stacke4f74c72018-10-29 11:51:46 -0700143Return<Result> Sensors::flush(int32_t sensorHandle) {
144 auto sensor = mSensors.find(sensorHandle);
145 if (sensor != mSensors.end()) {
146 return sensor->second->flush();
147 }
148 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700149}
150
Brian Stackd23f2002018-11-05 13:48:16 -0800151Return<Result> Sensors::injectSensorData(const Event& event) {
152 auto sensor = mSensors.find(event.sensorHandle);
153 if (sensor != mSensors.end()) {
154 return sensor->second->injectEvent(event);
155 }
156
157 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700158}
159
160Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */,
Brian Stack9a407f22018-10-19 16:04:11 -0700161 registerDirectChannel_cb _hidl_cb) {
Brian Stack56358ca2018-10-29 17:20:43 -0700162 _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
Brian Stack9a407f22018-10-19 16:04:11 -0700163 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700164}
165
166Return<Result> Sensors::unregisterDirectChannel(int32_t /* channelHandle */) {
Brian Stack9a407f22018-10-19 16:04:11 -0700167 return Result::INVALID_OPERATION;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700168}
169
170Return<void> Sensors::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
Brian Stack9a407f22018-10-19 16:04:11 -0700171 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
172 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
173 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700174}
175
Brian Stackf2aca3b2018-11-05 09:37:15 -0800176void Sensors::postEvents(const std::vector<Event>& events, bool wakeup) {
177 std::lock_guard<std::mutex> lock(mWriteLock);
178 if (mEventQueue->write(events.data(), events.size())) {
179 mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
Brian Stack237abc62018-10-23 11:09:59 -0700180
Brian Stackf2aca3b2018-11-05 09:37:15 -0800181 if (wakeup) {
182 // Keep track of the number of outstanding WAKE_UP events in order to properly hold
183 // a wake lock until the framework has secured a wake lock
184 updateWakeLock(events.size(), 0 /* eventsHandled */);
185 }
186 }
187}
Brian Stack237abc62018-10-23 11:09:59 -0700188
Brian Stackf2aca3b2018-11-05 09:37:15 -0800189void Sensors::updateWakeLock(int32_t eventsWritten, int32_t eventsHandled) {
190 std::lock_guard<std::mutex> lock(mWakeLockLock);
191 int32_t newVal = mOutstandingWakeUpEvents + eventsWritten - eventsHandled;
192 if (newVal < 0) {
193 mOutstandingWakeUpEvents = 0;
194 } else {
195 mOutstandingWakeUpEvents = newVal;
196 }
197
198 if (eventsWritten > 0) {
199 // Update the time at which the last WAKE_UP event was sent
200 mAutoReleaseWakeLockTime = ::android::uptimeMillis() +
201 static_cast<uint32_t>(SensorTimeout::WAKE_LOCK_SECONDS) * 1000;
202 }
203
204 if (!mHasWakeLock && mOutstandingWakeUpEvents > 0 &&
205 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLockName) == 0) {
206 mHasWakeLock = true;
207 } else if (mHasWakeLock) {
208 // Check if the wake lock should be released automatically if
209 // SensorTimeout::WAKE_LOCK_SECONDS has elapsed since the last WAKE_UP event was written to
210 // the Wake Lock FMQ.
211 if (::android::uptimeMillis() > mAutoReleaseWakeLockTime) {
212 ALOGD("No events read from wake lock FMQ for %d seconds, auto releasing wake lock",
213 SensorTimeout::WAKE_LOCK_SECONDS);
214 mOutstandingWakeUpEvents = 0;
215 }
216
217 if (mOutstandingWakeUpEvents == 0 && release_wake_lock(kWakeLockName) == 0) {
218 mHasWakeLock = false;
219 }
220 }
221}
222
223void Sensors::readWakeLockFMQ() {
224 while (mReadWakeLockQueueRun.load()) {
225 constexpr int64_t kReadTimeoutNs = 500 * 1000 * 1000; // 500 ms
226 uint32_t eventsHandled = 0;
227
228 // Read events from the Wake Lock FMQ. Timeout after a reasonable amount of time to ensure
229 // that any held wake lock is able to be released if it is held for too long.
Brian Stack2c313362019-01-08 13:09:49 -0800230 mWakeLockQueue->readBlocking(&eventsHandled, 1 /* count */, 0 /* readNotification */,
231 static_cast<uint32_t>(WakeLockQueueFlagBits::DATA_WRITTEN),
232 kReadTimeoutNs);
Brian Stackf2aca3b2018-11-05 09:37:15 -0800233 updateWakeLock(0 /* eventsWritten */, eventsHandled);
234 }
235}
236
237void Sensors::startReadWakeLockThread(Sensors* sensors) {
238 sensors->readWakeLockFMQ();
Brian Stack237abc62018-10-23 11:09:59 -0700239}
240
Brian Stack475d4d42018-10-19 15:58:09 -0700241void Sensors::deleteEventFlag() {
242 status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag);
243 if (status != OK) {
244 ALOGI("Failed to delete event flag: %d", status);
245 }
246}
247
Brian Stack60fcdcf2018-10-16 14:51:34 -0700248} // namespace implementation
249} // namespace V2_0
250} // namespace sensors
251} // namespace hardware
252} // namespace android