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