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