blob: b781744553ea324231c06e45fc5ac2548ec34e71 [file] [log] [blame]
Yifan Hong95c7a062017-03-28 19:07:17 -07001/*
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 "EventQueue.h"
18#include "utils.h"
19
20#include <utils/Looper.h>
21
22namespace android {
23namespace frameworks {
24namespace sensorservice {
25namespace V1_0 {
26namespace implementation {
27
28class EventQueueLooperCallback : public ::android::LooperCallback {
29public:
Yifan Hong06d46fd2017-04-19 18:45:30 -070030 EventQueueLooperCallback(sp<::android::SensorEventQueue> queue,
31 sp<IEventQueueCallback> callback)
Yifan Hong95c7a062017-03-28 19:07:17 -070032 : mQueue(queue), mCallback(callback) {
33 }
34
35 int handleEvent(__unused int fd, __unused int events, __unused void* data) {
36
37 ASensorEvent event;
38 ssize_t actual;
Yifan Hong06d46fd2017-04-19 18:45:30 -070039
40 auto internalQueue = mQueue.promote();
41 if (internalQueue == nullptr) {
42 return 1;
43 }
Yifan Hong95c7a062017-03-28 19:07:17 -070044
45 while ((actual = internalQueue->read(&event, 1 /* count */)) > 0) {
46 internalQueue->sendAck(&event, actual);
Andreas Huber957aa852017-04-07 12:52:17 -070047 Return<void> ret = mCallback->onEvent(convertEvent(event));
48 (void)ret.isOk(); // ignored
Yifan Hong95c7a062017-03-28 19:07:17 -070049 }
50
51 return 1; // continue to receive callbacks
52 }
53
54private:
Yifan Hong06d46fd2017-04-19 18:45:30 -070055 wp<::android::SensorEventQueue> mQueue;
Yifan Hong95c7a062017-03-28 19:07:17 -070056 sp<IEventQueueCallback> mCallback;
57};
58
59EventQueue::EventQueue(
60 sp<IEventQueueCallback> callback,
61 sp<::android::Looper> looper,
62 sp<::android::SensorEventQueue> internalQueue)
63 : mLooper(looper),
64 mInternalQueue(internalQueue) {
65
Yifan Hong06d46fd2017-04-19 18:45:30 -070066 mLooper->addFd(internalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT,
Yi Kong8f313e32018-07-17 14:13:29 -070067 new EventQueueLooperCallback(internalQueue, callback), nullptr /* data */);
Yifan Hong95c7a062017-03-28 19:07:17 -070068}
69
Yifan Hong06d46fd2017-04-19 18:45:30 -070070void EventQueue::onLastStrongRef(const void *id) {
71 IEventQueue::onLastStrongRef(id);
Yifan Hong95c7a062017-03-28 19:07:17 -070072 mLooper->removeFd(mInternalQueue->getFd());
73}
74
75// Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow.
76Return<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs,
77 int64_t maxBatchReportLatencyUs) {
Yifan Hong95c7a062017-03-28 19:07:17 -070078 return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs,
79 maxBatchReportLatencyUs, 0 /* reserved flags */));
80}
81
82Return<Result> EventQueue::disableSensor(int32_t sensorHandle) {
83 return convertResult(mInternalQueue->disableSensor(sensorHandle));
84}
85
86} // namespace implementation
87} // namespace V1_0
88} // namespace sensorservice
89} // namespace frameworks
90} // namespace android