blob: 86d365c0c22182578bbe5ceda21e7642d558031e [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);
42 mCallback->onEvent(convertEvent(event));
43 }
44
45 return 1; // continue to receive callbacks
46 }
47
48private:
49 sp<EventQueue> mQueue;
50 sp<IEventQueueCallback> mCallback;
51};
52
53EventQueue::EventQueue(
54 sp<IEventQueueCallback> callback,
55 sp<::android::Looper> looper,
56 sp<::android::SensorEventQueue> internalQueue)
57 : mLooper(looper),
58 mInternalQueue(internalQueue) {
59
60 mLooper->addFd(mInternalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT,
61 new EventQueueLooperCallback(this, callback), NULL /* data */);
62}
63
64EventQueue::~EventQueue() {
65 mLooper->removeFd(mInternalQueue->getFd());
66}
67
68// Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow.
69Return<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs,
70 int64_t maxBatchReportLatencyUs) {
71 // TODO implement
72 return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs,
73 maxBatchReportLatencyUs, 0 /* reserved flags */));
74}
75
76Return<Result> EventQueue::disableSensor(int32_t sensorHandle) {
77 return convertResult(mInternalQueue->disableSensor(sensorHandle));
78}
79
80} // namespace implementation
81} // namespace V1_0
82} // namespace sensorservice
83} // namespace frameworks
84} // namespace android