Devin Moore | 0b59781 | 2022-11-04 20:43:35 +0000 | [diff] [blame] | 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: |
| 33 | EventQueueLooperCallback(sp<::android::SensorEventQueue> queue, |
| 34 | std::shared_ptr<IEventQueueCallback> callback) |
| 35 | : mQueue(queue), mCallback(callback) {} |
| 36 | |
| 37 | int handleEvent(__unused int fd, __unused int events, __unused 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 | |
| 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 | |
| 69 | // FIXME why was this on onLastStrongRef instead of dtor? |
| 70 | EventQueue::~EventQueue() { |
| 71 | mLooper->removeFd(mInternalQueue->getFd()); |
| 72 | } |
| 73 | |
| 74 | ndk::ScopedAStatus EventQueue::enableSensor(int32_t in_sensorHandle, int32_t in_samplingPeriodUs, |
| 75 | int64_t in_maxBatchReportLatencyUs) { |
| 76 | return convertResult(mInternalQueue->enableSensor(in_sensorHandle, in_samplingPeriodUs, |
| 77 | in_maxBatchReportLatencyUs, 0)); |
| 78 | } |
| 79 | |
| 80 | ndk::ScopedAStatus EventQueue::disableSensor(int32_t in_sensorHandle) { |
| 81 | return convertResult(mInternalQueue->disableSensor(in_sensorHandle)); |
| 82 | } |
| 83 | |
| 84 | } // namespace implementation |
| 85 | } // namespace sensorservice |
| 86 | } // namespace frameworks |
| 87 | } // namespace android |