blob: c0365e503b4f1ba191cb4bc24ce6edc268ed6f3b [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:
30 EventQueueLooperCallback(sp<EventQueue> queue, sp<IEventQueueCallback> callback)
31 : mQueue(queue), mCallback(callback) {
32 }
33
34 int handleEvent(__unused int fd, __unused int events, __unused void* data) {
35
36 ASensorEvent event;
37 ssize_t actual;
38 const sp<::android::SensorEventQueue>& internalQueue = mQueue->mInternalQueue;
39
40 while ((actual = internalQueue->read(&event, 1 /* count */)) > 0) {
41 internalQueue->sendAck(&event, actual);
Andreas Huber957aa852017-04-07 12:52:17 -070042 Return<void> ret = mCallback->onEvent(convertEvent(event));
43 (void)ret.isOk(); // ignored
Yifan Hong95c7a062017-03-28 19:07:17 -070044 }
45
46 return 1; // continue to receive callbacks
47 }
48
49private:
50 sp<EventQueue> mQueue;
51 sp<IEventQueueCallback> mCallback;
52};
53
54EventQueue::EventQueue(
55 sp<IEventQueueCallback> callback,
56 sp<::android::Looper> looper,
57 sp<::android::SensorEventQueue> internalQueue)
58 : mLooper(looper),
59 mInternalQueue(internalQueue) {
60
61 mLooper->addFd(mInternalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT,
62 new EventQueueLooperCallback(this, callback), NULL /* data */);
63}
64
65EventQueue::~EventQueue() {
66 mLooper->removeFd(mInternalQueue->getFd());
67}
68
69// Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow.
70Return<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs,
71 int64_t maxBatchReportLatencyUs) {
72 // TODO implement
73 return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs,
74 maxBatchReportLatencyUs, 0 /* reserved flags */));
75}
76
77Return<Result> EventQueue::disableSensor(int32_t sensorHandle) {
78 return convertResult(mInternalQueue->disableSensor(sensorHandle));
79}
80
81} // namespace implementation
82} // namespace V1_0
83} // namespace sensorservice
84} // namespace frameworks
85} // namespace android