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#ifndef A_SENSOR_EVENT_QUEUE_H_ 18 19#define A_SENSOR_EVENT_QUEUE_H_ 20 21#include <android/frameworks/sensorservice/1.0/IEventQueue.h> 22#include <android/frameworks/sensorservice/1.0/IEventQueueCallback.h> 23#include <android/looper.h> 24#include <android/sensor.h> 25#include <sensors/convert.h> 26#include <utils/Mutex.h> 27 28struct ALooper; 29 30struct ASensorEventQueue 31 : public android::frameworks::sensorservice::V1_0::IEventQueueCallback { 32 using Event = android::hardware::sensors::V1_0::Event; 33 using IEventQueue = android::frameworks::sensorservice::V1_0::IEventQueue; 34 35 ASensorEventQueue( 36 ALooper *looper, 37 int ident, 38 ALooper_callbackFunc callback, 39 void *data); 40 41 android::hardware::Return<void> onEvent(const Event &event) override; 42 43 void setImpl(const android::sp<IEventQueue> &queueImpl); 44 45 int registerSensor( 46 ASensorRef sensor, 47 int32_t samplingPeriodUs, 48 int64_t maxBatchReportLatencyUs); 49 50 int enableSensor(ASensorRef sensor); 51 int disableSensor(ASensorRef sensor); 52 53 int setEventRate(ASensorRef sensor, int32_t samplingPeriodUs); 54 55 ssize_t getEvents(ASensorEvent *events, size_t count); 56 int hasEvents() const; 57 58 void dispatchCallback(); 59 60 void invalidate(); 61 62private: 63 ALooper *mLooper; 64 int mIdent; 65 ALooper_callbackFunc mCallback; 66 void *mData; 67 android::sp<IEventQueue> mQueueImpl; 68 69 android::Mutex mLock; 70 std::vector<sensors_event_t> mQueue; 71 72 DISALLOW_COPY_AND_ASSIGN(ASensorEventQueue); 73}; 74 75#endif // A_SENSOR_EVENT_QUEUE_H_ 76 77