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