1 /*
2 * Copyright (C) 2017 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 "ASensorEventQueue.h"
18
19 #include "ALooper.h"
20
21 #define LOG_TAG "libsensorndkbridge"
22 #include <android-base/logging.h>
23
24 using android::sp;
25 using android::frameworks::sensorservice::V1_0::Result;
26 using android::hardware::sensors::V1_0::SensorInfo;
27 using android::OK;
28 using android::BAD_VALUE;
29 using android::Mutex;
30 using android::hardware::Return;
31
ASensorEventQueue(ALooper * looper,ALooper_callbackFunc callback,void * data)32 ASensorEventQueue::ASensorEventQueue(
33 ALooper *looper, ALooper_callbackFunc callback, void *data)
34 : mLooper(looper),
35 mCallback(callback),
36 mData(data) {
37 }
38
setImpl(const sp<IEventQueue> & queueImpl)39 void ASensorEventQueue::setImpl(const sp<IEventQueue> &queueImpl) {
40 mQueueImpl = queueImpl;
41 }
42
registerSensor(ASensorRef sensor,int32_t samplingPeriodUs,int64_t maxBatchReportLatencyUs)43 int ASensorEventQueue::registerSensor(
44 ASensorRef sensor,
45 int32_t samplingPeriodUs,
46 int64_t maxBatchReportLatencyUs) {
47 Return<Result> ret = mQueueImpl->enableSensor(
48 reinterpret_cast<const SensorInfo *>(sensor)->sensorHandle,
49 samplingPeriodUs,
50 maxBatchReportLatencyUs);
51
52 if (!ret.isOk()) {
53 return BAD_VALUE;
54 }
55
56 return OK;
57 }
58
enableSensor(ASensorRef sensor)59 int ASensorEventQueue::enableSensor(ASensorRef sensor) {
60 static constexpr int32_t SENSOR_DELAY_NORMAL = 200000;
61
62 return registerSensor(
63 sensor, SENSOR_DELAY_NORMAL, 0 /* maxBatchReportLatencyUs */);
64 }
65
setEventRate(ASensorRef sensor,int32_t samplingPeriodUs)66 int ASensorEventQueue::setEventRate(
67 ASensorRef sensor, int32_t samplingPeriodUs) {
68 // Technically this is not supposed to enable the sensor but using this
69 // API without enabling the sensor first is a no-op, so...
70 return registerSensor(
71 sensor, samplingPeriodUs, 0 /* maxBatchReportLatencyUs */);
72 }
73
disableSensor(ASensorRef sensor)74 int ASensorEventQueue::disableSensor(ASensorRef sensor) {
75 Return<Result> ret = mQueueImpl->disableSensor(
76 reinterpret_cast<const SensorInfo *>(sensor)->sensorHandle);
77
78 return ret.isOk() ? OK : BAD_VALUE;
79 }
80
getEvents(ASensorEvent * events,size_t count)81 ssize_t ASensorEventQueue::getEvents(ASensorEvent *events, size_t count) {
82 // XXX Should this block if there aren't any events in the queue?
83
84 Mutex::Autolock autoLock(mLock);
85
86 static_assert(
87 sizeof(ASensorEvent) == sizeof(sensors_event_t), "mismatched size");
88
89 size_t copy = std::min(count, mQueue.size());
90 for (size_t i = 0; i < copy; ++i) {
91 reinterpret_cast<sensors_event_t *>(events)[i] = mQueue[i];
92 }
93 mQueue.erase(mQueue.begin(), mQueue.begin() + copy);
94
95 LOG(VERBOSE) << "ASensorEventQueue::getEvents() returned " << copy << " events.";
96
97 return copy;
98 }
99
hasEvents() const100 int ASensorEventQueue::hasEvents() const {
101 return !mQueue.empty();
102 }
103
onEvent(const Event & event)104 Return<void> ASensorEventQueue::onEvent(const Event &event) {
105 LOG(VERBOSE) << "ASensorEventQueue::onEvent";
106
107 {
108 Mutex::Autolock autoLock(mLock);
109
110 mQueue.emplace_back();
111 sensors_event_t *sensorEvent = &mQueue[mQueue.size() - 1];
112 android::hardware::sensors::V1_0::implementation::convertToSensorEvent(
113 event, sensorEvent);
114 }
115
116 mLooper->signalSensorEvents(this);
117
118 return android::hardware::Void();
119 }
120
dispatchCallback()121 void ASensorEventQueue::dispatchCallback() {
122 if (mCallback != NULL) {
123 int res = (*mCallback)(-1 /* fd */, ALOOPER_EVENT_INPUT, mData);
124
125 if (res == 0) {
126 mCallback = NULL;
127 mData = NULL;
128 }
129 }
130 }
131
invalidate()132 void ASensorEventQueue::invalidate() {
133 mLooper->invalidateSensorQueue(this);
134 setImpl(nullptr);
135 }
136
137