| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 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: | 
| Yifan Hong | 06d46fd | 2017-04-19 18:45:30 -0700 | [diff] [blame] | 30 | EventQueueLooperCallback(sp<::android::SensorEventQueue> queue, | 
|  | 31 | sp<IEventQueueCallback> callback) | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 32 | : mQueue(queue), mCallback(callback) { | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | int handleEvent(__unused int fd, __unused int events, __unused void* data) { | 
|  | 36 |  | 
|  | 37 | ASensorEvent event; | 
|  | 38 | ssize_t actual; | 
| Yifan Hong | 06d46fd | 2017-04-19 18:45:30 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | auto internalQueue = mQueue.promote(); | 
|  | 41 | if (internalQueue == nullptr) { | 
|  | 42 | return 1; | 
|  | 43 | } | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | while ((actual = internalQueue->read(&event, 1 /* count */)) > 0) { | 
|  | 46 | internalQueue->sendAck(&event, actual); | 
| Andreas Huber | 957aa85 | 2017-04-07 12:52:17 -0700 | [diff] [blame] | 47 | Return<void> ret = mCallback->onEvent(convertEvent(event)); | 
|  | 48 | (void)ret.isOk(); // ignored | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | return 1; // continue to receive callbacks | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | private: | 
| Yifan Hong | 06d46fd | 2017-04-19 18:45:30 -0700 | [diff] [blame] | 55 | wp<::android::SensorEventQueue> mQueue; | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 56 | sp<IEventQueueCallback> mCallback; | 
|  | 57 | }; | 
|  | 58 |  | 
|  | 59 | EventQueue::EventQueue( | 
|  | 60 | sp<IEventQueueCallback> callback, | 
|  | 61 | sp<::android::Looper> looper, | 
|  | 62 | sp<::android::SensorEventQueue> internalQueue) | 
|  | 63 | : mLooper(looper), | 
|  | 64 | mInternalQueue(internalQueue) { | 
|  | 65 |  | 
| Yifan Hong | 06d46fd | 2017-04-19 18:45:30 -0700 | [diff] [blame] | 66 | mLooper->addFd(internalQueue->getFd(), ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, | 
|  | 67 | new EventQueueLooperCallback(internalQueue, callback), NULL /* data */); | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| Yifan Hong | 06d46fd | 2017-04-19 18:45:30 -0700 | [diff] [blame] | 70 | void EventQueue::onLastStrongRef(const void *id) { | 
|  | 71 | IEventQueue::onLastStrongRef(id); | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 72 | mLooper->removeFd(mInternalQueue->getFd()); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | // Methods from ::android::frameworks::sensorservice::V1_0::IEventQueue follow. | 
|  | 76 | Return<Result> EventQueue::enableSensor(int32_t sensorHandle, int32_t samplingPeriodUs, | 
|  | 77 | int64_t maxBatchReportLatencyUs) { | 
| Yifan Hong | 95c7a06 | 2017-03-28 19:07:17 -0700 | [diff] [blame] | 78 | return convertResult(mInternalQueue->enableSensor(sensorHandle, samplingPeriodUs, | 
|  | 79 | maxBatchReportLatencyUs, 0 /* reserved flags */)); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 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 |