blob: 23dd26bccbb597e7ed09fbea730480cdca7e5527 [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),
Brian Stacka2d16bb2019-01-25 11:22:04 -080040 mNextHandle(1),
Brian Stackf2aca3b2018-11-05 09:37:15 -080041 mOutstandingWakeUpEvents(0),
42 mReadWakeLockQueueRun(false),
43 mAutoReleaseWakeLockTime(0),
44 mHasWakeLock(false) {
Brian Stacka2d16bb2019-01-25 11:22:04 -080045 AddSensor<AccelSensor>();
46 AddSensor<GyroSensor>();
47 AddSensor<AmbientTempSensor>();
48 AddSensor<DeviceTempSensor>();
49 AddSensor<PressureSensor>();
50 AddSensor<MagnetometerSensor>();
51 AddSensor<LightSensor>();
52 AddSensor<ProximitySensor>();
53 AddSensor<RelativeHumiditySensor>();
Brian Stack2927ab72018-10-23 11:22:55 -070054}
Brian Stack475d4d42018-10-19 15:58:09 -070055
56Sensors::~Sensors() {
57 deleteEventFlag();
Brian Stackf2aca3b2018-11-05 09:37:15 -080058 mReadWakeLockQueueRun = false;
59 mWakeLockThread.join();
Brian Stack475d4d42018-10-19 15:58:09 -070060}
61
Brian Stack60fcdcf2018-10-16 14:51:34 -070062// Methods from ::android::hardware::sensors::V2_0::ISensors follow.
Brian Stack897528d2018-10-23 10:38:03 -070063Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
64 std::vector<SensorInfo> sensors;
65 for (const auto& sensor : mSensors) {
66 sensors.push_back(sensor.second->getSensorInfo());
67 }
68
69 // Call the HIDL callback with the SensorInfo
70 _hidl_cb(sensors);
71
Brian Stack60fcdcf2018-10-16 14:51:34 -070072 return Void();
73}
74
Brian Stackd23f2002018-11-05 13:48:16 -080075Return<Result> Sensors::setOperationMode(OperationMode mode) {
76 for (auto sensor : mSensors) {
77 sensor.second->setOperationMode(mode);
78 }
79 return Result::OK;
Brian Stack60fcdcf2018-10-16 14:51:34 -070080}
81
Brian Stack897528d2018-10-23 10:38:03 -070082Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
83 auto sensor = mSensors.find(sensorHandle);
84 if (sensor != mSensors.end()) {
85 sensor->second->activate(enabled);
86 return Result::OK;
87 }
88 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -070089}
90
91Return<Result> Sensors::initialize(
Brian Stack475d4d42018-10-19 15:58:09 -070092 const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor,
93 const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor,
94 const sp<ISensorsCallback>& sensorsCallback) {
95 Result result = Result::OK;
96
Brian Stackd3849e12019-01-07 12:57:12 -080097 // Ensure that all sensors are disabled
98 for (auto sensor : mSensors) {
99 sensor.second->activate(false /* enable */);
100 }
101
102 // Stop the Wake Lock thread if it is currently running
103 if (mReadWakeLockQueueRun.load()) {
104 mReadWakeLockQueueRun = false;
105 mWakeLockThread.join();
106 }
107
Brian Stack475d4d42018-10-19 15:58:09 -0700108 // Save a reference to the callback
109 mCallback = sensorsCallback;
110
111 // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions.
112 mEventQueue =
113 std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */);
114
115 // Ensure that any existing EventFlag is properly deleted
116 deleteEventFlag();
117
118 // Create the EventFlag that is used to signal to the framework that sensor events have been
119 // written to the Event FMQ
120 if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) {
121 result = Result::BAD_VALUE;
122 }
123
124 // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP
125 // events have been successfully read and handled by the framework.
126 mWakeLockQueue =
127 std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */);
128
129 if (!mCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) {
130 result = Result::BAD_VALUE;
131 }
132
Brian Stackf2aca3b2018-11-05 09:37:15 -0800133 // Start the thread to read events from the Wake Lock FMQ
134 mReadWakeLockQueueRun = true;
135 mWakeLockThread = std::thread(startReadWakeLockThread, this);
136
Brian Stack475d4d42018-10-19 15:58:09 -0700137 return result;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700138}
139
Brian Stack897528d2018-10-23 10:38:03 -0700140Return<Result> Sensors::batch(int32_t sensorHandle, int64_t samplingPeriodNs,
Brian Stack60fcdcf2018-10-16 14:51:34 -0700141 int64_t /* maxReportLatencyNs */) {
Brian Stack897528d2018-10-23 10:38:03 -0700142 auto sensor = mSensors.find(sensorHandle);
Brian Stack237abc62018-10-23 11:09:59 -0700143 if (sensor != mSensors.end()) {
144 sensor->second->batch(samplingPeriodNs);
145 return Result::OK;
Brian Stack897528d2018-10-23 10:38:03 -0700146 }
Brian Stack237abc62018-10-23 11:09:59 -0700147 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700148}
149
Brian Stacke4f74c72018-10-29 11:51:46 -0700150Return<Result> Sensors::flush(int32_t sensorHandle) {
151 auto sensor = mSensors.find(sensorHandle);
152 if (sensor != mSensors.end()) {
153 return sensor->second->flush();
154 }
155 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700156}
157
Brian Stackd23f2002018-11-05 13:48:16 -0800158Return<Result> Sensors::injectSensorData(const Event& event) {
159 auto sensor = mSensors.find(event.sensorHandle);
160 if (sensor != mSensors.end()) {
161 return sensor->second->injectEvent(event);
162 }
163
164 return Result::BAD_VALUE;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700165}
166
167Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */,
Brian Stack9a407f22018-10-19 16:04:11 -0700168 registerDirectChannel_cb _hidl_cb) {
Brian Stack56358ca2018-10-29 17:20:43 -0700169 _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */);
Brian Stack9a407f22018-10-19 16:04:11 -0700170 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700171}
172
173Return<Result> Sensors::unregisterDirectChannel(int32_t /* channelHandle */) {
Brian Stack9a407f22018-10-19 16:04:11 -0700174 return Result::INVALID_OPERATION;
Brian Stack60fcdcf2018-10-16 14:51:34 -0700175}
176
177Return<void> Sensors::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */,
Brian Stack9a407f22018-10-19 16:04:11 -0700178 RateLevel /* rate */, configDirectReport_cb _hidl_cb) {
179 _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */);
180 return Return<void>();
Brian Stack60fcdcf2018-10-16 14:51:34 -0700181}
182
Brian Stackf2aca3b2018-11-05 09:37:15 -0800183void Sensors::postEvents(const std::vector<Event>& events, bool wakeup) {
184 std::lock_guard<std::mutex> lock(mWriteLock);
185 if (mEventQueue->write(events.data(), events.size())) {
186 mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS));
Brian Stack237abc62018-10-23 11:09:59 -0700187
Brian Stackf2aca3b2018-11-05 09:37:15 -0800188 if (wakeup) {
189 // Keep track of the number of outstanding WAKE_UP events in order to properly hold
190 // a wake lock until the framework has secured a wake lock
191 updateWakeLock(events.size(), 0 /* eventsHandled */);
192 }
193 }
194}
Brian Stack237abc62018-10-23 11:09:59 -0700195
Brian Stackf2aca3b2018-11-05 09:37:15 -0800196void Sensors::updateWakeLock(int32_t eventsWritten, int32_t eventsHandled) {
197 std::lock_guard<std::mutex> lock(mWakeLockLock);
198 int32_t newVal = mOutstandingWakeUpEvents + eventsWritten - eventsHandled;
199 if (newVal < 0) {
200 mOutstandingWakeUpEvents = 0;
201 } else {
202 mOutstandingWakeUpEvents = newVal;
203 }
204
205 if (eventsWritten > 0) {
206 // Update the time at which the last WAKE_UP event was sent
207 mAutoReleaseWakeLockTime = ::android::uptimeMillis() +
208 static_cast<uint32_t>(SensorTimeout::WAKE_LOCK_SECONDS) * 1000;
209 }
210
211 if (!mHasWakeLock && mOutstandingWakeUpEvents > 0 &&
212 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLockName) == 0) {
213 mHasWakeLock = true;
214 } else if (mHasWakeLock) {
215 // Check if the wake lock should be released automatically if
216 // SensorTimeout::WAKE_LOCK_SECONDS has elapsed since the last WAKE_UP event was written to
217 // the Wake Lock FMQ.
218 if (::android::uptimeMillis() > mAutoReleaseWakeLockTime) {
219 ALOGD("No events read from wake lock FMQ for %d seconds, auto releasing wake lock",
220 SensorTimeout::WAKE_LOCK_SECONDS);
221 mOutstandingWakeUpEvents = 0;
222 }
223
224 if (mOutstandingWakeUpEvents == 0 && release_wake_lock(kWakeLockName) == 0) {
225 mHasWakeLock = false;
226 }
227 }
228}
229
230void Sensors::readWakeLockFMQ() {
231 while (mReadWakeLockQueueRun.load()) {
232 constexpr int64_t kReadTimeoutNs = 500 * 1000 * 1000; // 500 ms
233 uint32_t eventsHandled = 0;
234
235 // Read events from the Wake Lock FMQ. Timeout after a reasonable amount of time to ensure
236 // that any held wake lock is able to be released if it is held for too long.
Brian Stack2c313362019-01-08 13:09:49 -0800237 mWakeLockQueue->readBlocking(&eventsHandled, 1 /* count */, 0 /* readNotification */,
238 static_cast<uint32_t>(WakeLockQueueFlagBits::DATA_WRITTEN),
239 kReadTimeoutNs);
Brian Stackf2aca3b2018-11-05 09:37:15 -0800240 updateWakeLock(0 /* eventsWritten */, eventsHandled);
241 }
242}
243
244void Sensors::startReadWakeLockThread(Sensors* sensors) {
245 sensors->readWakeLockFMQ();
Brian Stack237abc62018-10-23 11:09:59 -0700246}
247
Brian Stack475d4d42018-10-19 15:58:09 -0700248void Sensors::deleteEventFlag() {
249 status_t status = EventFlag::deleteEventFlag(&mEventQueueFlag);
250 if (status != OK) {
251 ALOGI("Failed to delete event flag: %d", status);
252 }
253}
254
Brian Stack60fcdcf2018-10-16 14:51:34 -0700255} // namespace implementation
256} // namespace V2_0
257} // namespace sensors
258} // namespace hardware
259} // namespace android