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