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 
29 struct ALooper;
30 
31 struct 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             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 
62 private:
63     ALooper *mLooper;
64     ALooper_callbackFunc mCallback;
65     void *mData;
66     android::sp<IEventQueue> mQueueImpl;
67 
68     android::Mutex mLock;
69     std::vector<sensors_event_t> mQueue;
70 
71     DISALLOW_COPY_AND_ASSIGN(ASensorEventQueue);
72 };
73 
74 #endif  // A_SENSOR_EVENT_QUEUE_H_
75 
76