xref: /aosp_15_r20/frameworks/native/libs/sensor/include/sensor/SensorEventQueue.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2010 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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/Mutex.h>
24 #include <utils/RefBase.h>
25 #include <utils/String8.h>
26 #include <utils/Timers.h>
27 
28 #include <sensor/BitTube.h>
29 
30 // ----------------------------------------------------------------------------
31 #define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1U << 31)
32 struct ALooper;
33 struct ASensorEvent;
34 
35 // Concrete types for the NDK
36 struct ASensorEventQueue {
37     ALooper* looper;
38     bool requestAdditionalInfo;
39 };
40 
41 // ----------------------------------------------------------------------------
42 namespace android {
43 // ----------------------------------------------------------------------------
44 
45 class ISensorEventConnection;
46 class SensorManager;
47 class Sensor;
48 class Looper;
49 
50 // ----------------------------------------------------------------------------
51 
52 class SensorEventQueue : public ASensorEventQueue, public RefBase
53 {
54 public:
55 
56     enum { MAX_RECEIVE_BUFFER_EVENT_COUNT = 256 };
57 
58     /**
59      * Typical sensor delay (sample period) in microseconds.
60      */
61     // Fastest sampling, system will bound it to minDelay
62     static constexpr int32_t SENSOR_DELAY_FASTEST = 0;
63     // Typical sample period for game, 50Hz;
64     static constexpr int32_t SENSOR_DELAY_GAME = 20000;
65     // Typical sample period for UI, 15Hz
66     static constexpr int32_t SENSOR_DELAY_UI = 66667;
67     // Default sensor sample period
68     static constexpr int32_t SENSOR_DELAY_NORMAL = 200000;
69 
70     explicit SensorEventQueue(const sp<ISensorEventConnection>& connection,
71                               SensorManager& sensorManager, String8 packageName);
72     virtual ~SensorEventQueue();
73     virtual void onFirstRef();
74 
75     int getFd() const;
76 
77     static ssize_t write(const sp<BitTube>& tube,
78             ASensorEvent const* events, size_t numEvents);
79 
80     ssize_t read(ASensorEvent* events, size_t numEvents);
81 
82     status_t waitForEvent() const;
83     status_t wake() const;
84 
85     status_t enableSensor(Sensor const* sensor) const;
86     status_t enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const;
87     status_t disableSensor(Sensor const* sensor) const;
88     status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
89 
90     // these are here only to support SensorManager.java and HIDL Frameworks SensorManager.
91     status_t enableSensor(int32_t handle, int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs,
92                           int reservedFlags) const;
93     status_t disableSensor(int32_t handle) const;
94     status_t flush() const;
95     // Send an ack for every wake_up sensor event that is set to WAKE_UP_SENSOR_EVENT_NEEDS_ACK.
96     void sendAck(const ASensorEvent* events, int count);
97 
98     status_t injectSensorEvent(const ASensorEvent& event);
99 
100     // Filters the given sensor events in place and returns the new number of events.
101     //
102     // The filtering is controlled by ASensorEventQueue.requestAdditionalInfo, and if this value is
103     // false, then all SENSOR_TYPE_ADDITIONAL_INFO sensor events will be removed.
104     ssize_t filterEvents(ASensorEvent* events, size_t count) const;
105 
106 private:
107     sp<Looper> getLooper() const;
108     sp<ISensorEventConnection> mSensorEventConnection;
109     sp<BitTube> mSensorChannel;
110     mutable Mutex mLock;
111     mutable sp<Looper> mLooper;
112     ASensorEvent* mRecBuffer;
113     SensorManager& mSensorManager;
114     String8 mPackageName;
115     size_t mAvailable;
116     size_t mConsumed;
117     uint32_t mNumAcksToSend;
118 };
119 
120 // ----------------------------------------------------------------------------
121 }; // namespace android
122