1 /*
2  * Copyright (C) 2022 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 <android-base/logging.h>
21 #include <utils/Looper.h>
22 
23 namespace android {
24 namespace frameworks {
25 namespace sensorservice {
26 namespace implementation {
27 
28 using ::aidl::android::frameworks::sensorservice::IEventQueueCallback;
29 using ::aidl::android::hardware::sensors::Event;
30 
31 class EventQueueLooperCallback : public ::android::LooperCallback {
32 public:
EventQueueLooperCallback(sp<::android::SensorEventQueue> queue,std::shared_ptr<IEventQueueCallback> callback)33     EventQueueLooperCallback(sp<::android::SensorEventQueue> queue,
34                              std::shared_ptr<IEventQueueCallback> callback)
35           : mQueue(queue), mCallback(callback) {}
36 
handleEvent(int,int,void *)37     int handleEvent(int /* fd */, int /* events */, void* /* data */) {
38         ASensorEvent event;
39         ssize_t actual;
40 
41         auto internalQueue = mQueue.promote();
42         if (internalQueue == nullptr) {
43             return 1;
44         }
45 
46         while ((actual = internalQueue->read(&event, 1)) > 0) {
47             internalQueue->sendAck(&event, actual);
48             ndk::ScopedAStatus ret = mCallback->onEvent(convertEvent(event));
49             if (!ret.isOk()) {
50                 LOG(ERROR) << "Failed to envoke EventQueueCallback: " << ret;
51             }
52         }
53 
54         return 1; // continue to receive callbacks
55     }
56 
57 private:
58     wp<::android::SensorEventQueue> mQueue;
59     std::shared_ptr<IEventQueueCallback> mCallback;
60 };
61 
EventQueue(std::shared_ptr<IEventQueueCallback> callback,sp<::android::Looper> looper,sp<::android::SensorEventQueue> internalQueue)62 EventQueue::EventQueue(std::shared_ptr<IEventQueueCallback> callback, sp<::android::Looper> looper,
63                        sp<::android::SensorEventQueue> internalQueue)
64       : mLooper(looper), mInternalQueue(internalQueue) {
65     mLooper->addFd(internalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT,
66                    new EventQueueLooperCallback(internalQueue, callback), nullptr);
67 }
68 
~EventQueue()69 EventQueue::~EventQueue() {
70     mLooper->removeFd(mInternalQueue->getFd());
71 }
72 
enableSensor(int32_t in_sensorHandle,int32_t in_samplingPeriodUs,int64_t in_maxBatchReportLatencyUs)73 ndk::ScopedAStatus EventQueue::enableSensor(int32_t in_sensorHandle, int32_t in_samplingPeriodUs,
74                                             int64_t in_maxBatchReportLatencyUs) {
75     return convertResult(mInternalQueue->enableSensor(in_sensorHandle, in_samplingPeriodUs,
76                                                       in_maxBatchReportLatencyUs, 0));
77 }
78 
disableSensor(int32_t in_sensorHandle)79 ndk::ScopedAStatus EventQueue::disableSensor(int32_t in_sensorHandle) {
80     return convertResult(mInternalQueue->disableSensor(in_sensorHandle));
81 }
82 
83 } // namespace implementation
84 } // namespace sensorservice
85 } // namespace frameworks
86 } // namespace android
87