blob: 18240dab4a88ce81035ec860f5580db980be0e19 [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 Stack60fcdcf2018-10-16 14:51:34 -070034
Brian Stackf2aca3b2018-11-05 09:37:15 -080035constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP";
36
37Sensors::Sensors()
38 : mEventQueueFlag(nullptr),
39 mOutstandingWakeUpEvents(0),
40 mReadWakeLockQueueRun(false),
41 mAutoReleaseWakeLockTime(0),
42 mHasWakeLock(false) {
Brian Stack2927ab72018-10-23 11:22:55 -070043 std::shared_ptr<AccelSensor> accel =
44 std::make_shared<AccelSensor>(1 /* sensorHandle */, this /* callback */);
45 mSensors[accel->getSensorInfo().sensorHandle] = accel;
46}
Brian Stack475d4d42018-10-19 15:58:09 -070047
48Sensors::~Sensors() {
49 deleteEventFlag();
Brian Stackf2aca3b2018-11-05 09:37:15 -080050 mReadWakeLockQueueRun = false;
51 mWakeLockThread.join();
Brian Stack475d4d42018-10-19 15:58:09 -070052}
53
Brian Stack60fcdcf2018-10-16 14:51:34 -070054// Methods from ::android::hardware::sensors::V2_0::ISensors follow.
Brian Stack897528d2018-10-23 10:38:03 -070055Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
56 std::vector<SensorInfo> sensors;
57 for (const auto& sensor : mSensors) {
58 sensors.push_back(sensor.second->getSensorInfo());
59 }
60
61 // Call the HIDL callback with the SensorInfo
62 _hidl_cb(sensors);
63
Brian Stack60fcdcf2018-10-16 14:51:34 -070064 return Void();
65}
66
Brian Stackd23f2002018-11-05 13:48:16 -080067Return<Result> Sensors::setOperationMode(OperationMode mode) {
68 for (auto sensor : mSensors) {
69 sensor.second->setOperationMode(mode);
70 }
71 return Result::OK;
Brian Stack60fcdcf2018-10-16 14:51:34 -070072}
73
Brian Stack897528d2018-10-23 10:38:03 -070074Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
75 auto sensor = mSensors.find(sensorHandle);
76 if (sensor != mSensors.end()) {
77 sensor->second->activate(enabled);
78 return Result::OK;
79 }
80 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -070081}
82
83Return<Result> Sensors::initialize(
Brian Stack475d4d42018-10-19 15:58:09 -070084 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
85 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
86 const sp<ISensorsCallback>& sensorsCallback) {
87 Result result = Result::OK;
88
89 // Save a reference to the callback
90 mCallback = sensorsCallback;
91
92 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
93 mEventQueue =
94 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
95
96 // Ensure that any existing EventFlag is properly deleted
97 deleteEventFlag();
98
99 // Create the EventFlag that is used to signal to the framework that sensor events have been
100 // written to the Event FMQ
101 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
102 result = Result::BAD_VALUE;
103 }
104
105 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
106 // events have been successfully read and handled by the framework.
107 mWakeLockQueue =
108 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
109
110 if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
111 result = Result::BAD_VALUE;
112 }
113
Brian Stackf2aca3b2018-11-05 09:37:15 -0800114 // Start the thread to read events from the Wake Lock FMQ
115 mReadWakeLockQueueRun = true;
116 mWakeLockThread = std::thread(startReadWakeLockThread, this);
117
Brian Stack475d4d42018-10-19 15:58:09 -0700118 return result;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700119}
120
Brian Stack897528d2018-10-23 10:38:03 -0700121Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
Brian Stack60fcdcf2018-10-16 14:51:34 -0700122 int64_t /* maxReportLatencyNs */) {
Brian Stack897528d2018-10-23 10:38:03 -0700123 auto sensor = mSensors.find(sensorHandle);
Brian Stack237abc62018-10-23 11:09:59 -0700124 if (sensor != mSensors.end()) {
125 sensor->second->batch(samplingPeriodNs);
126 return Result::OK;
Brian Stack897528d2018-10-23 10:38:03 -0700127 }
Brian Stack237abc62018-10-23 11:09:59 -0700128 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700129}
130
Brian Stacke4f74c72018-10-29 11:51:46 -0700131Return<Result> Sensors::flush(int32_t sensorHandle) {
132 auto sensor = mSensors.find(sensorHandle);
133 if (sensor != mSensors.end()) {
134 return sensor->second->flush();
135 }
136 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700137}
138
Brian Stackd23f2002018-11-05 13:48:16 -0800139Return<Result> Sensors::injectSensorData(const Event& event) {
140 auto sensor = mSensors.find(event.sensorHandle);
141 if (sensor != mSensors.end()) {
142 return sensor->second->injectEvent(event);
143 }
144
145 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700146}
147
148Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */,
Brian Stack9a407f22018-10-19 16:04:11 -0700149 registerDirectChannel_cb _hidl_cb) {
150 _hidl_cb(Result::INVALID_OPERATION, 0 /* channelHandle */);
151 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700152}
153
154Return<Result> Sensors::unregisterDirectChannel(int32_t /* channelHandle */) {
Brian Stack9a407f22018-10-19 16:04:11 -0700155 return Result::INVALID_OPERATION;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700156}
157
158Return<void> Sensors::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
Brian Stack9a407f22018-10-19 16:04:11 -0700159 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
160 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
161 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700162}
163
Brian Stackf2aca3b2018-11-05 09:37:15 -0800164void Sensors::postEvents(const std::vector<Event>& events, bool wakeup) {
165 std::lock_guard<std::mutex> lock(mWriteLock);
166 if (mEventQueue->write(events.data(), events.size())) {
167 mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
Brian Stack237abc62018-10-23 11:09:59 -0700168
Brian Stackf2aca3b2018-11-05 09:37:15 -0800169 if (wakeup) {
170 // Keep track of the number of outstanding WAKE_UP events in order to properly hold
171 // a wake lock until the framework has secured a wake lock
172 updateWakeLock(events.size(), 0 /* eventsHandled */);
173 }
174 }
175}
Brian Stack237abc62018-10-23 11:09:59 -0700176
Brian Stackf2aca3b2018-11-05 09:37:15 -0800177void Sensors::updateWakeLock(int32_t eventsWritten, int32_t eventsHandled) {
178 std::lock_guard<std::mutex> lock(mWakeLockLock);
179 int32_t newVal = mOutstandingWakeUpEvents + eventsWritten - eventsHandled;
180 if (newVal < 0) {
181 mOutstandingWakeUpEvents = 0;
182 } else {
183 mOutstandingWakeUpEvents = newVal;
184 }
185
186 if (eventsWritten > 0) {
187 // Update the time at which the last WAKE_UP event was sent
188 mAutoReleaseWakeLockTime = ::android::uptimeMillis() +
189 static_cast<uint32_t>(SensorTimeout::WAKE_LOCK_SECONDS) * 1000;
190 }
191
192 if (!mHasWakeLock && mOutstandingWakeUpEvents > 0 &&
193 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLockName) == 0) {
194 mHasWakeLock = true;
195 } else if (mHasWakeLock) {
196 // Check if the wake lock should be released automatically if
197 // SensorTimeout::WAKE_LOCK_SECONDS has elapsed since the last WAKE_UP event was written to
198 // the Wake Lock FMQ.
199 if (::android::uptimeMillis() > mAutoReleaseWakeLockTime) {
200 ALOGD("No events read from wake lock FMQ for %d seconds, auto releasing wake lock",
201 SensorTimeout::WAKE_LOCK_SECONDS);
202 mOutstandingWakeUpEvents = 0;
203 }
204
205 if (mOutstandingWakeUpEvents == 0 && release_wake_lock(kWakeLockName) == 0) {
206 mHasWakeLock = false;
207 }
208 }
209}
210
211void Sensors::readWakeLockFMQ() {
212 while (mReadWakeLockQueueRun.load()) {
213 constexpr int64_t kReadTimeoutNs = 500 * 1000 * 1000; // 500 ms
214 uint32_t eventsHandled = 0;
215
216 // Read events from the Wake Lock FMQ. Timeout after a reasonable amount of time to ensure
217 // that any held wake lock is able to be released if it is held for too long.
218 mWakeLockQueue->readBlocking(&eventsHandled, 1 /* count */, kReadTimeoutNs);
219 updateWakeLock(0 /* eventsWritten */, eventsHandled);
220 }
221}
222
223void Sensors::startReadWakeLockThread(Sensors* sensors) {
224 sensors->readWakeLockFMQ();
Brian Stack237abc62018-10-23 11:09:59 -0700225}
226
Brian Stack475d4d42018-10-19 15:58:09 -0700227void Sensors::deleteEventFlag() {
228 status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag);
229 if (status != OK) {
230 ALOGI("Failed to delete event flag: %d", status);
231 }
232}
233
Brian Stack60fcdcf2018-10-16 14:51:34 -0700234} // namespace implementation
235} // namespace V2_0
236} // namespace sensors
237} // namespace hardware
238} // namespace android