EventQueue.cpp revision 957aa852bb9bd7a70cc4f7f553d4945f3f8da23c
195c7a0636763c0861d46425709befe90a3919c04Yifan Hong/* 295c7a0636763c0861d46425709befe90a3919c04Yifan Hong * Copyright (C) 2017 The Android Open Source Project 395c7a0636763c0861d46425709befe90a3919c04Yifan Hong * 495c7a0636763c0861d46425709befe90a3919c04Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License"); 595c7a0636763c0861d46425709befe90a3919c04Yifan Hong * you may not use this file except in compliance with the License. 695c7a0636763c0861d46425709befe90a3919c04Yifan Hong * You may obtain a copy of the License at 795c7a0636763c0861d46425709befe90a3919c04Yifan Hong * 895c7a0636763c0861d46425709befe90a3919c04Yifan Hong * http://www.apache.org/licenses/LICENSE-2.0 995c7a0636763c0861d46425709befe90a3919c04Yifan Hong * 1095c7a0636763c0861d46425709befe90a3919c04Yifan Hong * Unless required by applicable law or agreed to in writing, software 1195c7a0636763c0861d46425709befe90a3919c04Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS, 1295c7a0636763c0861d46425709befe90a3919c04Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1395c7a0636763c0861d46425709befe90a3919c04Yifan Hong * See the License for the specific language governing permissions and 1495c7a0636763c0861d46425709befe90a3919c04Yifan Hong * limitations under the License. 1595c7a0636763c0861d46425709befe90a3919c04Yifan Hong */ 1695c7a0636763c0861d46425709befe90a3919c04Yifan Hong 1795c7a0636763c0861d46425709befe90a3919c04Yifan Hong#include "EventQueue.h" 1895c7a0636763c0861d46425709befe90a3919c04Yifan Hong#include "utils.h" 1995c7a0636763c0861d46425709befe90a3919c04Yifan Hong 2095c7a0636763c0861d46425709befe90a3919c04Yifan Hong#include <utils/Looper.h> 2195c7a0636763c0861d46425709befe90a3919c04Yifan Hong 2295c7a0636763c0861d46425709befe90a3919c04Yifan Hongnamespace android { 2395c7a0636763c0861d46425709befe90a3919c04Yifan Hongnamespace frameworks { 2495c7a0636763c0861d46425709befe90a3919c04Yifan Hongnamespace sensorservice { 2595c7a0636763c0861d46425709befe90a3919c04Yifan Hongnamespace V1_0 { 2695c7a0636763c0861d46425709befe90a3919c04Yifan Hongnamespace implementation { 2795c7a0636763c0861d46425709befe90a3919c04Yifan Hong 2895c7a0636763c0861d46425709befe90a3919c04Yifan Hongclass EventQueueLooperCallback : public ::android::LooperCallback { 2995c7a0636763c0861d46425709befe90a3919c04Yifan Hongpublic: 3095c7a0636763c0861d46425709befe90a3919c04Yifan Hong EventQueueLooperCallback(sp<EventQueue> queue, sp<IEventQueueCallback> callback) 3195c7a0636763c0861d46425709befe90a3919c04Yifan Hong : mQueue(queue), mCallback(callback) { 3295c7a0636763c0861d46425709befe90a3919c04Yifan Hong } 3395c7a0636763c0861d46425709befe90a3919c04Yifan Hong 3495c7a0636763c0861d46425709befe90a3919c04Yifan Hong int handleEvent(__unused int fd, __unused int events, __unused void* data) { 3595c7a0636763c0861d46425709befe90a3919c04Yifan Hong 3695c7a0636763c0861d46425709befe90a3919c04Yifan Hong ASensorEvent event; 3795c7a0636763c0861d46425709befe90a3919c04Yifan Hong ssize_t actual; 3895c7a0636763c0861d46425709befe90a3919c04Yifan Hong const sp<::android::SensorEventQueue>& internalQueue = mQueue->mInternalQueue; 3995c7a0636763c0861d46425709befe90a3919c04Yifan Hong 4095c7a0636763c0861d46425709befe90a3919c04Yifan Hong while ((actual = internalQueue->read(&event, 1 /* count */)) > 0) { 4195c7a0636763c0861d46425709befe90a3919c04Yifan Hong internalQueue->sendAck(&event, actual); 42957aa852bb9bd7a70cc4f7f553d4945f3f8da23cAndreas Huber Return<void> ret = mCallback->onEvent(convertEvent(event)); 43957aa852bb9bd7a70cc4f7f553d4945f3f8da23cAndreas Huber (void)ret.isOk(); // ignored 4495c7a0636763c0861d46425709befe90a3919c04Yifan Hong } 4595c7a0636763c0861d46425709befe90a3919c04Yifan Hong 4695c7a0636763c0861d46425709befe90a3919c04Yifan Hong return 1; // continue to receive callbacks 4795c7a0636763c0861d46425709befe90a3919c04Yifan Hong } 4895c7a0636763c0861d46425709befe90a3919c04Yifan Hong 4995c7a0636763c0861d46425709befe90a3919c04Yifan Hongprivate: 5095c7a0636763c0861d46425709befe90a3919c04Yifan Hong sp<EventQueue> mQueue; 5195c7a0636763c0861d46425709befe90a3919c04Yifan Hong sp<IEventQueueCallback> mCallback; 5295c7a0636763c0861d46425709befe90a3919c04Yifan Hong}; 5395c7a0636763c0861d46425709befe90a3919c04Yifan Hong 5495c7a0636763c0861d46425709befe90a3919c04Yifan HongEventQueue::EventQueue( 5595c7a0636763c0861d46425709befe90a3919c04Yifan Hong sp<IEventQueueCallback> callback, 5695c7a0636763c0861d46425709befe90a3919c04Yifan Hong sp<::android::Looper> looper, 5795c7a0636763c0861d46425709befe90a3919c04Yifan Hong sp<::android::SensorEventQueue> internalQueue) 5895c7a0636763c0861d46425709befe90a3919c04Yifan Hong : mLooper(looper), 5995c7a0636763c0861d46425709befe90a3919c04Yifan Hong mInternalQueue(internalQueue) { 6095c7a0636763c0861d46425709befe90a3919c04Yifan Hong 6195c7a0636763c0861d46425709befe90a3919c04Yifan Hong mLooper->addFd(mInternalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, 6295c7a0636763c0861d46425709befe90a3919c04Yifan Hong new EventQueueLooperCallback(this, callback), NULL /* data */); 6395c7a0636763c0861d46425709befe90a3919c04Yifan Hong} 6495c7a0636763c0861d46425709befe90a3919c04Yifan Hong 6595c7a0636763c0861d46425709befe90a3919c04Yifan HongEventQueue::~EventQueue() { 6695c7a0636763c0861d46425709befe90a3919c04Yifan Hong mLooper->removeFd(mInternalQueue->getFd()); 6795c7a0636763c0861d46425709befe90a3919c04Yifan Hong} 6895c7a0636763c0861d46425709befe90a3919c04Yifan Hong 6995c7a0636763c0861d46425709befe90a3919c04Yifan Hong// Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow. 7095c7a0636763c0861d46425709befe90a3919c04Yifan HongReturn<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs, 7195c7a0636763c0861d46425709befe90a3919c04Yifan Hong int64_t maxBatchReportLatencyUs) { 7295c7a0636763c0861d46425709befe90a3919c04Yifan Hong // TODO implement 7395c7a0636763c0861d46425709befe90a3919c04Yifan Hong return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs, 7495c7a0636763c0861d46425709befe90a3919c04Yifan Hong maxBatchReportLatencyUs, 0 /* reserved flags */)); 7595c7a0636763c0861d46425709befe90a3919c04Yifan Hong} 7695c7a0636763c0861d46425709befe90a3919c04Yifan Hong 7795c7a0636763c0861d46425709befe90a3919c04Yifan HongReturn<Result> EventQueue::disableSensor(int32_t sensorHandle) { 7895c7a0636763c0861d46425709befe90a3919c04Yifan Hong return convertResult(mInternalQueue->disableSensor(sensorHandle)); 7995c7a0636763c0861d46425709befe90a3919c04Yifan Hong} 8095c7a0636763c0861d46425709befe90a3919c04Yifan Hong 8195c7a0636763c0861d46425709befe90a3919c04Yifan Hong} // namespace implementation 8295c7a0636763c0861d46425709befe90a3919c04Yifan Hong} // namespace V1_0 8395c7a0636763c0861d46425709befe90a3919c04Yifan Hong} // namespace sensorservice 8495c7a0636763c0861d46425709befe90a3919c04Yifan Hong} // namespace frameworks 8595c7a0636763c0861d46425709befe90a3919c04Yifan Hong} // namespace android 86