• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "EventQueue.h"
18 #include "utils.h"
19 
20 #include <utils/Looper.h>
21 
22 namespace android {
23 namespace frameworks {
24 namespace sensorservice {
25 namespace V1_0 {
26 namespace implementation {
27 
28 class EventQueueLooperCallback : public ::android::LooperCallback {
29 public:
EventQueueLooperCallback(sp<::android::SensorEventQueue> queue,sp<IEventQueueCallback> callback)30     EventQueueLooperCallback(sp<::android::SensorEventQueue> queue,
31                              sp<IEventQueueCallback> callback)
32             : mQueue(queue), mCallback(callback) {
33     }
34 
handleEvent(__unused int fd,__unused int events,__unused void * data)35     int handleEvent(__unused int fd, __unused int events, __unused void* data) {
36 
37         ASensorEvent event;
38         ssize_t actual;
39 
40         auto internalQueue = mQueue.promote();
41         if (internalQueue == nullptr) {
42             return 1;
43         }
44 
45         while ((actual = internalQueue->read(&event, 1 /* count */)) > 0) {
46             internalQueue->sendAck(&event, actual);
47             Return<void> ret = mCallback->onEvent(convertEvent(event));
48             (void)ret.isOk(); // ignored
49         }
50 
51         return 1; // continue to receive callbacks
52     }
53 
54 private:
55     wp<::android::SensorEventQueue> mQueue;
56     sp<IEventQueueCallback> mCallback;
57 };
58 
EventQueue(sp<IEventQueueCallback> callback,sp<::android::Looper> looper,sp<::android::SensorEventQueue> internalQueue)59 EventQueue::EventQueue(
60         sp<IEventQueueCallback> callback,
61         sp<::android::Looper> looper,
62         sp<::android::SensorEventQueue> internalQueue)
63             : mLooper(looper),
64               mInternalQueue(internalQueue) {
65 
66     mLooper->addFd(internalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT,
67             new EventQueueLooperCallback(internalQueue, callback), NULL /* data */);
68 }
69 
onLastStrongRef(const void * id)70 void EventQueue::onLastStrongRef(const void *id) {
71     IEventQueue::onLastStrongRef(id);
72     mLooper->removeFd(mInternalQueue->getFd());
73 }
74 
75 // Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow.
enableSensor(int32_t sensorHandle,int32_t samplingPeriodUs,int64_t maxBatchReportLatencyUs)76 Return<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs,
77         int64_t maxBatchReportLatencyUs) {
78     return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs,
79             maxBatchReportLatencyUs, 0 /* reserved flags */));
80 }
81 
disableSensor(int32_t sensorHandle)82 Return<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
91