xref: /aosp_15_r20/hardware/libhardware/modules/sensors/SensorEventQueue.h (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #ifndef SENSOREVENTQUEUE_H_
18*e01b6f76SAndroid Build Coastguard Worker #define SENSOREVENTQUEUE_H_
19*e01b6f76SAndroid Build Coastguard Worker 
20*e01b6f76SAndroid Build Coastguard Worker #include <hardware/sensors.h>
21*e01b6f76SAndroid Build Coastguard Worker #include <pthread.h>
22*e01b6f76SAndroid Build Coastguard Worker 
23*e01b6f76SAndroid Build Coastguard Worker /*
24*e01b6f76SAndroid Build Coastguard Worker  * Fixed-size circular queue, with an API developed around the sensor HAL poll() method.
25*e01b6f76SAndroid Build Coastguard Worker  * Poll() takes a pointer to a buffer, which is written by poll() before it returns.
26*e01b6f76SAndroid Build Coastguard Worker  * This class can provide a pointer to a spot in its internal buffer for poll() to
27*e01b6f76SAndroid Build Coastguard Worker  * write to, instead of using an intermediate buffer and a memcpy.
28*e01b6f76SAndroid Build Coastguard Worker  *
29*e01b6f76SAndroid Build Coastguard Worker  * Thread safety:
30*e01b6f76SAndroid Build Coastguard Worker  * Reading can be done safely after grabbing the mutex lock, while poll() writing in a separate
31*e01b6f76SAndroid Build Coastguard Worker  * thread without a mutex lock. But there can only be one writer at a time.
32*e01b6f76SAndroid Build Coastguard Worker  */
33*e01b6f76SAndroid Build Coastguard Worker class SensorEventQueue {
34*e01b6f76SAndroid Build Coastguard Worker     int mCapacity;
35*e01b6f76SAndroid Build Coastguard Worker     int mStart; // start of readable region
36*e01b6f76SAndroid Build Coastguard Worker     int mSize; // number of readable items
37*e01b6f76SAndroid Build Coastguard Worker     sensors_event_t* mData;
38*e01b6f76SAndroid Build Coastguard Worker     pthread_cond_t mSpaceAvailableCondition;
39*e01b6f76SAndroid Build Coastguard Worker 
40*e01b6f76SAndroid Build Coastguard Worker public:
41*e01b6f76SAndroid Build Coastguard Worker     explicit SensorEventQueue(int capacity);
42*e01b6f76SAndroid Build Coastguard Worker     ~SensorEventQueue();
43*e01b6f76SAndroid Build Coastguard Worker 
44*e01b6f76SAndroid Build Coastguard Worker     // Returns length of region, between zero and min(capacity, requestedLength). If there is any
45*e01b6f76SAndroid Build Coastguard Worker     // writable space, it will return a region of at least one. Because it must return
46*e01b6f76SAndroid Build Coastguard Worker     // a pointer to a contiguous region, it may return smaller regions as we approach the end of
47*e01b6f76SAndroid Build Coastguard Worker     // the data array.
48*e01b6f76SAndroid Build Coastguard Worker     // Only call while holding the lock.
49*e01b6f76SAndroid Build Coastguard Worker     // The region is not marked internally in any way. Subsequent calls may return overlapping
50*e01b6f76SAndroid Build Coastguard Worker     // regions. This class expects there to be exactly one writer at a time.
51*e01b6f76SAndroid Build Coastguard Worker     int getWritableRegion(int requestedLength, sensors_event_t** out);
52*e01b6f76SAndroid Build Coastguard Worker 
53*e01b6f76SAndroid Build Coastguard Worker     // After writing to the region returned by getWritableRegion(), call this to indicate how
54*e01b6f76SAndroid Build Coastguard Worker     // many records were actually written.
55*e01b6f76SAndroid Build Coastguard Worker     // This increases size() by count.
56*e01b6f76SAndroid Build Coastguard Worker     // Only call while holding the lock.
57*e01b6f76SAndroid Build Coastguard Worker     void markAsWritten(int count);
58*e01b6f76SAndroid Build Coastguard Worker 
59*e01b6f76SAndroid Build Coastguard Worker     // Gets the number of readable records.
60*e01b6f76SAndroid Build Coastguard Worker     // Only call while holding the lock.
61*e01b6f76SAndroid Build Coastguard Worker     int getSize();
62*e01b6f76SAndroid Build Coastguard Worker 
63*e01b6f76SAndroid Build Coastguard Worker     // Returns pointer to the first readable record, or NULL if size() is zero.
64*e01b6f76SAndroid Build Coastguard Worker     // Only call this while holding the lock.
65*e01b6f76SAndroid Build Coastguard Worker     sensors_event_t* peek();
66*e01b6f76SAndroid Build Coastguard Worker 
67*e01b6f76SAndroid Build Coastguard Worker     // This will decrease the size by one, freeing up the oldest readable event's slot for writing.
68*e01b6f76SAndroid Build Coastguard Worker     // Only call while holding the lock.
69*e01b6f76SAndroid Build Coastguard Worker     void dequeue();
70*e01b6f76SAndroid Build Coastguard Worker 
71*e01b6f76SAndroid Build Coastguard Worker     // Blocks until space is available. No-op if there is already space.
72*e01b6f76SAndroid Build Coastguard Worker     // Returns true if it had to wait.
73*e01b6f76SAndroid Build Coastguard Worker     bool waitForSpace(pthread_mutex_t* mutex);
74*e01b6f76SAndroid Build Coastguard Worker };
75*e01b6f76SAndroid Build Coastguard Worker 
76*e01b6f76SAndroid Build Coastguard Worker #endif // SENSOREVENTQUEUE_H_
77