xref: /aosp_15_r20/frameworks/native/include/android/thermal.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker /**
18*38e8c45fSAndroid Build Coastguard Worker  * @addtogroup Thermal
19*38e8c45fSAndroid Build Coastguard Worker  * @{
20*38e8c45fSAndroid Build Coastguard Worker  */
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker /**
23*38e8c45fSAndroid Build Coastguard Worker  * @file thermal.h
24*38e8c45fSAndroid Build Coastguard Worker  */
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker #ifndef _ANDROID_THERMAL_H
27*38e8c45fSAndroid Build Coastguard Worker #define _ANDROID_THERMAL_H
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #include <sys/cdefs.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker /******************************************************************
32*38e8c45fSAndroid Build Coastguard Worker  *
33*38e8c45fSAndroid Build Coastguard Worker  * IMPORTANT NOTICE:
34*38e8c45fSAndroid Build Coastguard Worker  *
35*38e8c45fSAndroid Build Coastguard Worker  *   This file is part of Android's set of stable system headers
36*38e8c45fSAndroid Build Coastguard Worker  *   exposed by the Android NDK (Native Development Kit).
37*38e8c45fSAndroid Build Coastguard Worker  *
38*38e8c45fSAndroid Build Coastguard Worker  *   Third-party source AND binary code relies on the definitions
39*38e8c45fSAndroid Build Coastguard Worker  *   here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
40*38e8c45fSAndroid Build Coastguard Worker  *
41*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
42*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
43*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
44*38e8c45fSAndroid Build Coastguard Worker  *   - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
45*38e8c45fSAndroid Build Coastguard Worker  */
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker /*
48*38e8c45fSAndroid Build Coastguard Worker  * Structures and functions to access thermal status and register/unregister
49*38e8c45fSAndroid Build Coastguard Worker  * thermal status listener in native code.
50*38e8c45fSAndroid Build Coastguard Worker  */
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker #include <stdint.h>
53*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker #if !defined(__INTRODUCED_IN)
56*38e8c45fSAndroid Build Coastguard Worker #define __INTRODUCED_IN(__api_level) /* nothing */
57*38e8c45fSAndroid Build Coastguard Worker #endif
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
60*38e8c45fSAndroid Build Coastguard Worker extern "C" {
61*38e8c45fSAndroid Build Coastguard Worker #endif
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker /**
64*38e8c45fSAndroid Build Coastguard Worker  * Thermal status used in function {@link AThermal_getCurrentThermalStatus} and
65*38e8c45fSAndroid Build Coastguard Worker  * {@link AThermal_StatusCallback}.
66*38e8c45fSAndroid Build Coastguard Worker  */
67*38e8c45fSAndroid Build Coastguard Worker enum AThermalStatus {
68*38e8c45fSAndroid Build Coastguard Worker     /** Error in thermal status. */
69*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_ERROR = -1,
70*38e8c45fSAndroid Build Coastguard Worker     /** Not under throttling. */
71*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_NONE = 0,
72*38e8c45fSAndroid Build Coastguard Worker     /** Light throttling where UX is not impacted. */
73*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_LIGHT = 1,
74*38e8c45fSAndroid Build Coastguard Worker     /** Moderate throttling where UX is not largely impacted. */
75*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_MODERATE = 2,
76*38e8c45fSAndroid Build Coastguard Worker     /** Severe throttling where UX is largely impacted. */
77*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_SEVERE = 3,
78*38e8c45fSAndroid Build Coastguard Worker     /** Platform has done everything to reduce power. */
79*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_CRITICAL = 4,
80*38e8c45fSAndroid Build Coastguard Worker     /**
81*38e8c45fSAndroid Build Coastguard Worker      * Key components in platform are shutting down due to thermal condition.
82*38e8c45fSAndroid Build Coastguard Worker      * Device functionalities will be limited.
83*38e8c45fSAndroid Build Coastguard Worker      */
84*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_EMERGENCY = 5,
85*38e8c45fSAndroid Build Coastguard Worker     /** Need shutdown immediately. */
86*38e8c45fSAndroid Build Coastguard Worker     ATHERMAL_STATUS_SHUTDOWN = 6,
87*38e8c45fSAndroid Build Coastguard Worker };
88*38e8c45fSAndroid Build Coastguard Worker typedef enum AThermalStatus AThermalStatus;
89*38e8c45fSAndroid Build Coastguard Worker 
90*38e8c45fSAndroid Build Coastguard Worker /**
91*38e8c45fSAndroid Build Coastguard Worker  * An opaque type representing a handle to a thermal manager.
92*38e8c45fSAndroid Build Coastguard Worker  * An instance of thermal manager must be acquired prior to
93*38e8c45fSAndroid Build Coastguard Worker  * using thermal status APIs and must be released after use.
94*38e8c45fSAndroid Build Coastguard Worker  *
95*38e8c45fSAndroid Build Coastguard Worker  * <p>To use:<ul>
96*38e8c45fSAndroid Build Coastguard Worker  *    <li>Create a new thermal manager instance by calling the
97*38e8c45fSAndroid Build Coastguard Worker  *        {@link AThermal_acquireManager} function.</li>
98*38e8c45fSAndroid Build Coastguard Worker  *    <li>Get current thermal status with
99*38e8c45fSAndroid Build Coastguard Worker  *        {@link AThermal_getCurrentThermalStatus}.</li>
100*38e8c45fSAndroid Build Coastguard Worker  *    <li>Register a thermal status listener with
101*38e8c45fSAndroid Build Coastguard Worker  *        {@link AThermal_registerThermalStatusListener}.</li>
102*38e8c45fSAndroid Build Coastguard Worker  *    <li>Unregister a thermal status listener with
103*38e8c45fSAndroid Build Coastguard Worker  *        {@link AThermal_unregisterThermalStatusListener}.</li>
104*38e8c45fSAndroid Build Coastguard Worker  *    <li>Release the thermal manager instance with
105*38e8c45fSAndroid Build Coastguard Worker  *        {@link AThermal_releaseManager}.</li></ul></p>
106*38e8c45fSAndroid Build Coastguard Worker  *
107*38e8c45fSAndroid Build Coastguard Worker  */
108*38e8c45fSAndroid Build Coastguard Worker typedef struct AThermalManager AThermalManager;
109*38e8c45fSAndroid Build Coastguard Worker 
110*38e8c45fSAndroid Build Coastguard Worker /**
111*38e8c45fSAndroid Build Coastguard Worker  * Prototype of the function that is called when thermal status changes.
112*38e8c45fSAndroid Build Coastguard Worker  * It's passed the updated thermal status as parameter, as well as the
113*38e8c45fSAndroid Build Coastguard Worker  * pointer provided by the client that registered a callback.
114*38e8c45fSAndroid Build Coastguard Worker  */
115*38e8c45fSAndroid Build Coastguard Worker typedef void (*AThermal_StatusCallback)(void* _Nullable data, AThermalStatus status);
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker /**
118*38e8c45fSAndroid Build Coastguard Worker   * Acquire an instance of the thermal manager. This must be freed using
119*38e8c45fSAndroid Build Coastguard Worker   * {@link AThermal_releaseManager}.
120*38e8c45fSAndroid Build Coastguard Worker   *
121*38e8c45fSAndroid Build Coastguard Worker   * Available since API level 30.
122*38e8c45fSAndroid Build Coastguard Worker   *
123*38e8c45fSAndroid Build Coastguard Worker   * @return manager instance on success, nullptr on failure.
124*38e8c45fSAndroid Build Coastguard Worker   */
125*38e8c45fSAndroid Build Coastguard Worker AThermalManager* _Nonnull AThermal_acquireManager() __INTRODUCED_IN(30);
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker /**
128*38e8c45fSAndroid Build Coastguard Worker  * Release the thermal manager pointer acquired via
129*38e8c45fSAndroid Build Coastguard Worker  * {@link AThermal_acquireManager}.
130*38e8c45fSAndroid Build Coastguard Worker  *
131*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 30.
132*38e8c45fSAndroid Build Coastguard Worker  *
133*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager to be released.
134*38e8c45fSAndroid Build Coastguard Worker  */
135*38e8c45fSAndroid Build Coastguard Worker void AThermal_releaseManager(AThermalManager* _Nonnull manager) __INTRODUCED_IN(30);
136*38e8c45fSAndroid Build Coastguard Worker 
137*38e8c45fSAndroid Build Coastguard Worker /**
138*38e8c45fSAndroid Build Coastguard Worker   * Gets the current thermal status.
139*38e8c45fSAndroid Build Coastguard Worker   *
140*38e8c45fSAndroid Build Coastguard Worker   * Available since API level 30.
141*38e8c45fSAndroid Build Coastguard Worker   *
142*38e8c45fSAndroid Build Coastguard Worker   * @param manager The manager instance to use to query the thermal status.
143*38e8c45fSAndroid Build Coastguard Worker   *                Acquired via {@link AThermal_acquireManager}.
144*38e8c45fSAndroid Build Coastguard Worker   *
145*38e8c45fSAndroid Build Coastguard Worker   * @return current thermal status, ATHERMAL_STATUS_ERROR on failure.
146*38e8c45fSAndroid Build Coastguard Worker   */
147*38e8c45fSAndroid Build Coastguard Worker AThermalStatus
148*38e8c45fSAndroid Build Coastguard Worker AThermal_getCurrentThermalStatus(AThermalManager *_Nonnull manager) __INTRODUCED_IN(30);
149*38e8c45fSAndroid Build Coastguard Worker 
150*38e8c45fSAndroid Build Coastguard Worker /**
151*38e8c45fSAndroid Build Coastguard Worker  * Register a thermal status listener for thermal status change.
152*38e8c45fSAndroid Build Coastguard Worker  *
153*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 30.
154*38e8c45fSAndroid Build Coastguard Worker  *
155*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use to register.
156*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
157*38e8c45fSAndroid Build Coastguard Worker  * @param callback The callback function to be called on system binder thread pool when thermal
158*38e8c45fSAndroid Build Coastguard Worker  *                 status updated.
159*38e8c45fSAndroid Build Coastguard Worker  * @param data The data pointer to be passed when callback is called.
160*38e8c45fSAndroid Build Coastguard Worker  *
161*38e8c45fSAndroid Build Coastguard Worker  * @return 0 on success
162*38e8c45fSAndroid Build Coastguard Worker  *         EINVAL if the listener and data pointer were previously added and not removed.
163*38e8c45fSAndroid Build Coastguard Worker  *         EPIPE if communication with the system service has failed, the listener will not get
164*38e8c45fSAndroid Build Coastguard Worker  *               removed and this call should be retried
165*38e8c45fSAndroid Build Coastguard Worker  */
166*38e8c45fSAndroid Build Coastguard Worker int AThermal_registerThermalStatusListener(AThermalManager *_Nonnull manager,
167*38e8c45fSAndroid Build Coastguard Worker                                            AThermal_StatusCallback _Nullable callback,
168*38e8c45fSAndroid Build Coastguard Worker                                            void* _Nullable data) __INTRODUCED_IN(30);
169*38e8c45fSAndroid Build Coastguard Worker 
170*38e8c45fSAndroid Build Coastguard Worker /**
171*38e8c45fSAndroid Build Coastguard Worker  * Unregister a thermal status listener previously registered.
172*38e8c45fSAndroid Build Coastguard Worker  *
173*38e8c45fSAndroid Build Coastguard Worker  * No subsequent invocations of the callback will occur after this function returns successfully.
174*38e8c45fSAndroid Build Coastguard Worker  *
175*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 30.
176*38e8c45fSAndroid Build Coastguard Worker  *
177*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use to unregister.
178*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
179*38e8c45fSAndroid Build Coastguard Worker  * @param callback The callback function that was previously registered.
180*38e8c45fSAndroid Build Coastguard Worker  * @param data The data pointer to be passed when callback is called.
181*38e8c45fSAndroid Build Coastguard Worker  *
182*38e8c45fSAndroid Build Coastguard Worker  * @return 0 on success
183*38e8c45fSAndroid Build Coastguard Worker  *         EINVAL if the listener and data pointer were not previously added.
184*38e8c45fSAndroid Build Coastguard Worker  *         EPIPE if communication with the system service has failed.
185*38e8c45fSAndroid Build Coastguard Worker  */
186*38e8c45fSAndroid Build Coastguard Worker int AThermal_unregisterThermalStatusListener(AThermalManager* _Nonnull manager,
187*38e8c45fSAndroid Build Coastguard Worker                                              AThermal_StatusCallback _Nullable callback,
188*38e8c45fSAndroid Build Coastguard Worker                                              void* _Nullable data) __INTRODUCED_IN(30);
189*38e8c45fSAndroid Build Coastguard Worker 
190*38e8c45fSAndroid Build Coastguard Worker /**
191*38e8c45fSAndroid Build Coastguard Worker  * Provides an estimate of how much thermal headroom the device currently has before
192*38e8c45fSAndroid Build Coastguard Worker  * hitting severe throttling.
193*38e8c45fSAndroid Build Coastguard Worker  *
194*38e8c45fSAndroid Build Coastguard Worker  * Note that this only attempts to track the headroom of slow-moving sensors, such as
195*38e8c45fSAndroid Build Coastguard Worker  * the skin temperature sensor. This means that there is no benefit to calling this function
196*38e8c45fSAndroid Build Coastguard Worker  * more frequently than about once per second, and attempted to call significantly
197*38e8c45fSAndroid Build Coastguard Worker  * more frequently may result in the function returning `NaN`.
198*38e8c45fSAndroid Build Coastguard Worker  *
199*38e8c45fSAndroid Build Coastguard Worker  * In addition, in order to be able to provide an accurate forecast, the system does
200*38e8c45fSAndroid Build Coastguard Worker  * not attempt to forecast until it has multiple temperature samples from which to
201*38e8c45fSAndroid Build Coastguard Worker  * extrapolate. This should only take a few seconds from the time of the first call,
202*38e8c45fSAndroid Build Coastguard Worker  * but during this time, no forecasting will occur, and the current headroom will be
203*38e8c45fSAndroid Build Coastguard Worker  * returned regardless of the value of `forecastSeconds`.
204*38e8c45fSAndroid Build Coastguard Worker  *
205*38e8c45fSAndroid Build Coastguard Worker  * The value returned is a non-negative float that represents how much of the thermal envelope
206*38e8c45fSAndroid Build Coastguard Worker  * is in use (or is forecasted to be in use). A value of 1.0 indicates that the device is
207*38e8c45fSAndroid Build Coastguard Worker  * (or will be) throttled at {@link #ATHERMAL_STATUS_SEVERE}. Such throttling can affect the
208*38e8c45fSAndroid Build Coastguard Worker  * CPU, GPU, and other subsystems. Values may exceed 1.0, but there is no implied mapping
209*38e8c45fSAndroid Build Coastguard Worker  * to specific thermal levels beyond that point. This means that values greater than 1.0
210*38e8c45fSAndroid Build Coastguard Worker  * may correspond to {@link #ATHERMAL_STATUS_SEVERE}, but may also represent heavier throttling.
211*38e8c45fSAndroid Build Coastguard Worker  *
212*38e8c45fSAndroid Build Coastguard Worker  * A value of 0.0 corresponds to a fixed distance from 1.0, but does not correspond to any
213*38e8c45fSAndroid Build Coastguard Worker  * particular thermal status or temperature. Values on (0.0, 1.0] may be expected to scale
214*38e8c45fSAndroid Build Coastguard Worker  * linearly with temperature, though temperature changes over time are typically not linear.
215*38e8c45fSAndroid Build Coastguard Worker  * Negative values will be clamped to 0.0 before returning.
216*38e8c45fSAndroid Build Coastguard Worker  *
217*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 31.
218*38e8c45fSAndroid Build Coastguard Worker  *
219*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use.
220*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
221*38e8c45fSAndroid Build Coastguard Worker  * @param forecastSeconds how many seconds into the future to forecast. Given that device
222*38e8c45fSAndroid Build Coastguard Worker  *                        conditions may change at any time, forecasts from further in the
223*38e8c45fSAndroid Build Coastguard Worker  *                        future will likely be less accurate than forecasts in the near future.
224*38e8c45fSAndroid Build Coastguard Worker  * @return a value greater than equal to 0.0, where 1.0 indicates the SEVERE throttling threshold,
225*38e8c45fSAndroid Build Coastguard Worker  *         as described above. Returns NaN if the device does not support this functionality or
226*38e8c45fSAndroid Build Coastguard Worker  *         if this function is called significantly faster than once per second.
227*38e8c45fSAndroid Build Coastguard Worker   */
228*38e8c45fSAndroid Build Coastguard Worker float AThermal_getThermalHeadroom(AThermalManager* _Nonnull manager,
229*38e8c45fSAndroid Build Coastguard Worker                                   int forecastSeconds) __INTRODUCED_IN(31);
230*38e8c45fSAndroid Build Coastguard Worker 
231*38e8c45fSAndroid Build Coastguard Worker /**
232*38e8c45fSAndroid Build Coastguard Worker  * This struct defines an instance of headroom threshold value and its status.
233*38e8c45fSAndroid Build Coastguard Worker  * <p>
234*38e8c45fSAndroid Build Coastguard Worker  * The value should be monotonically non-decreasing as the thermal status increases.
235*38e8c45fSAndroid Build Coastguard Worker  * For {@link ATHERMAL_STATUS_SEVERE}, its headroom threshold is guaranteed to
236*38e8c45fSAndroid Build Coastguard Worker  * be 1.0f. For status below severe status, the value should be lower or equal
237*38e8c45fSAndroid Build Coastguard Worker  * to 1.0f, and for status above severe, the value should be larger or equal to 1.0f.
238*38e8c45fSAndroid Build Coastguard Worker  * <p>
239*38e8c45fSAndroid Build Coastguard Worker  * Also see {@link AThermal_getThermalHeadroom} for explanation on headroom, and
240*38e8c45fSAndroid Build Coastguard Worker  * {@link AThermal_getThermalHeadroomThresholds} for how to use this.
241*38e8c45fSAndroid Build Coastguard Worker  */
242*38e8c45fSAndroid Build Coastguard Worker struct AThermalHeadroomThreshold {
243*38e8c45fSAndroid Build Coastguard Worker     float headroom;
244*38e8c45fSAndroid Build Coastguard Worker     AThermalStatus thermalStatus;
245*38e8c45fSAndroid Build Coastguard Worker };
246*38e8c45fSAndroid Build Coastguard Worker typedef struct AThermalHeadroomThreshold AThermalHeadroomThreshold;
247*38e8c45fSAndroid Build Coastguard Worker 
248*38e8c45fSAndroid Build Coastguard Worker /**
249*38e8c45fSAndroid Build Coastguard Worker  * Gets the thermal headroom thresholds for all available thermal status.
250*38e8c45fSAndroid Build Coastguard Worker  *
251*38e8c45fSAndroid Build Coastguard Worker  * A thermal status will only exist in output if the device manufacturer has the
252*38e8c45fSAndroid Build Coastguard Worker  * corresponding threshold defined for at least one of its slow-moving skin temperature
253*38e8c45fSAndroid Build Coastguard Worker  * sensors. If it's set, one should also expect to get it from
254*38e8c45fSAndroid Build Coastguard Worker  * {@link #AThermal_getCurrentThermalStatus} or {@link AThermal_StatusCallback}.
255*38e8c45fSAndroid Build Coastguard Worker  * <p>
256*38e8c45fSAndroid Build Coastguard Worker  * The headroom threshold is used to interpret the possible thermal throttling status based on
257*38e8c45fSAndroid Build Coastguard Worker  * the headroom prediction. For example, if the headroom threshold for
258*38e8c45fSAndroid Build Coastguard Worker  * {@link ATHERMAL_STATUS_LIGHT} is 0.7, and a headroom prediction in 10s returns 0.75
259*38e8c45fSAndroid Build Coastguard Worker  * (or `AThermal_getThermalHeadroom(10)=0.75}`, one can expect that in 10 seconds the system
260*38e8c45fSAndroid Build Coastguard Worker  * could be in lightly throttled state if the workload remains the same. The app can consider
261*38e8c45fSAndroid Build Coastguard Worker  * taking actions according to the nearest throttling status the difference between the headroom and
262*38e8c45fSAndroid Build Coastguard Worker  * the threshold.
263*38e8c45fSAndroid Build Coastguard Worker  * <p>
264*38e8c45fSAndroid Build Coastguard Worker  * For new devices it's guaranteed to have a single sensor, but for older devices with multiple
265*38e8c45fSAndroid Build Coastguard Worker  * sensors reporting different threshold values, the minimum threshold is taken to be conservative
266*38e8c45fSAndroid Build Coastguard Worker  * on predictions. Thus, when reading real-time headroom, it's not guaranteed that a real-time value
267*38e8c45fSAndroid Build Coastguard Worker  * of 0.75 (or `AThermal_getThermalHeadroom(0)=0.75`) exceeding the threshold of 0.7 above
268*38e8c45fSAndroid Build Coastguard Worker  * will always come with lightly throttled state
269*38e8c45fSAndroid Build Coastguard Worker  * (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT`) but it can be lower
270*38e8c45fSAndroid Build Coastguard Worker  * (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE`).
271*38e8c45fSAndroid Build Coastguard Worker  * While it's always guaranteed that the device won't be throttled heavier than the unmet
272*38e8c45fSAndroid Build Coastguard Worker  * threshold's state, so a real-time headroom of 0.75 will never come with
273*38e8c45fSAndroid Build Coastguard Worker  * {@link #ATHERMAL_STATUS_MODERATE} but always lower, and 0.65 will never come with
274*38e8c45fSAndroid Build Coastguard Worker  * {@link ATHERMAL_STATUS_LIGHT} but {@link #ATHERMAL_STATUS_NONE}.
275*38e8c45fSAndroid Build Coastguard Worker  * <p>
276*38e8c45fSAndroid Build Coastguard Worker  * Starting in Android 16, this polling API may return different results when called depending on
277*38e8c45fSAndroid Build Coastguard Worker  * the device. The new headroom listener API {@link #AThermal_HeadroomCallback} can be used to
278*38e8c45fSAndroid Build Coastguard Worker  * detect headroom thresholds changes.
279*38e8c45fSAndroid Build Coastguard Worker  * <p>
280*38e8c45fSAndroid Build Coastguard Worker  * Before API level 36 the returned list of thresholds is cached on first successful query and owned
281*38e8c45fSAndroid Build Coastguard Worker  * by the thermal manager, which will not change between calls to this function. The caller should
282*38e8c45fSAndroid Build Coastguard Worker  * only need to free the manager with {@link AThermal_releaseManager}.
283*38e8c45fSAndroid Build Coastguard Worker  * <p>
284*38e8c45fSAndroid Build Coastguard Worker  *
285*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use.
286*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
287*38e8c45fSAndroid Build Coastguard Worker  * @param outThresholds non-null output pointer to null AThermalHeadroomThreshold pointer, which
288*38e8c45fSAndroid Build Coastguard Worker  *                will be set to a new array of thresholds if thermal thresholds are supported
289*38e8c45fSAndroid Build Coastguard Worker  *                by the system or device, otherwise nullptr or unmodified. The client should
290*38e8c45fSAndroid Build Coastguard Worker  *                clean up the thresholds by array-deleting the threshold pointer.
291*38e8c45fSAndroid Build Coastguard Worker  * @param size non-null output pointer whose value will be set to the size of the threshold array
292*38e8c45fSAndroid Build Coastguard Worker  *             or 0 if it's not supported.
293*38e8c45fSAndroid Build Coastguard Worker  * @return 0 on success
294*38e8c45fSAndroid Build Coastguard Worker  *         EINVAL if outThresholds or size_t is nullptr, or *outThresholds is not nullptr.
295*38e8c45fSAndroid Build Coastguard Worker  *         EPIPE if communication with the system service has failed.
296*38e8c45fSAndroid Build Coastguard Worker  *         ENOSYS if the feature is disabled by the current system.
297*38e8c45fSAndroid Build Coastguard Worker  */
298*38e8c45fSAndroid Build Coastguard Worker int AThermal_getThermalHeadroomThresholds(AThermalManager* _Nonnull manager,
299*38e8c45fSAndroid Build Coastguard Worker                                           const AThermalHeadroomThreshold* _Nonnull
300*38e8c45fSAndroid Build Coastguard Worker                                           * _Nullable outThresholds,
301*38e8c45fSAndroid Build Coastguard Worker                                           size_t* _Nonnull size) __INTRODUCED_IN(35);
302*38e8c45fSAndroid Build Coastguard Worker 
303*38e8c45fSAndroid Build Coastguard Worker /**
304*38e8c45fSAndroid Build Coastguard Worker  * Prototype of the function that is called when thermal headroom or thresholds changes.
305*38e8c45fSAndroid Build Coastguard Worker  * It's passed the updated thermal headroom and thresholds as parameters, as well as the
306*38e8c45fSAndroid Build Coastguard Worker  * pointer provided by the client that registered a callback.
307*38e8c45fSAndroid Build Coastguard Worker  *
308*38e8c45fSAndroid Build Coastguard Worker  * @param data The data pointer to be passed when callback is called.
309*38e8c45fSAndroid Build Coastguard Worker  * @param headroom The current non-negative normalized headroom value, also see
310*38e8c45fSAndroid Build Coastguard Worker  *                 {@link AThermal_getThermalHeadroom}.
311*38e8c45fSAndroid Build Coastguard Worker  * @param forecastHeadroom The forecasted non-negative normalized headroom value, also see
312*38e8c45fSAndroid Build Coastguard Worker  *                         {@link AThermal_getThermalHeadroom}.
313*38e8c45fSAndroid Build Coastguard Worker  * @param forecastSeconds The seconds used for the forecast by the system.
314*38e8c45fSAndroid Build Coastguard Worker  * @param thresholds The current headroom thresholds. The thresholds pointer will be a constant
315*38e8c45fSAndroid Build Coastguard Worker  *                   shared across all callbacks registered from the same process, and it will be
316*38e8c45fSAndroid Build Coastguard Worker  *                   destroyed after all the callbacks are finished. If the client intents to
317*38e8c45fSAndroid Build Coastguard Worker  *                   persist the values, it should make a copy of it during the callback.
318*38e8c45fSAndroid Build Coastguard Worker  * @param thresholdsCount The count of thresholds.
319*38e8c45fSAndroid Build Coastguard Worker  */
320*38e8c45fSAndroid Build Coastguard Worker typedef void (*AThermal_HeadroomCallback)(void *_Nullable data,
321*38e8c45fSAndroid Build Coastguard Worker                                           float headroom,
322*38e8c45fSAndroid Build Coastguard Worker                                           float forecastHeadroom,
323*38e8c45fSAndroid Build Coastguard Worker                                           int forecastSeconds,
324*38e8c45fSAndroid Build Coastguard Worker                                           const AThermalHeadroomThreshold* _Nullable thresholds,
325*38e8c45fSAndroid Build Coastguard Worker                                           size_t thresholdsCount);
326*38e8c45fSAndroid Build Coastguard Worker 
327*38e8c45fSAndroid Build Coastguard Worker /**
328*38e8c45fSAndroid Build Coastguard Worker  * Register a thermal headroom listener for thermal headroom or thresholds change.
329*38e8c45fSAndroid Build Coastguard Worker  *
330*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 36.
331*38e8c45fSAndroid Build Coastguard Worker  *
332*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use to register.
333*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
334*38e8c45fSAndroid Build Coastguard Worker  * @param callback The callback function to be called on system binder thread pool when thermal
335*38e8c45fSAndroid Build Coastguard Worker  *                 headroom or thresholds update.
336*38e8c45fSAndroid Build Coastguard Worker  * @param data The data pointer to be passed when callback is called.
337*38e8c45fSAndroid Build Coastguard Worker  *
338*38e8c45fSAndroid Build Coastguard Worker  * @return 0 on success
339*38e8c45fSAndroid Build Coastguard Worker  *         EINVAL if the listener and data pointer were previously added and not removed.
340*38e8c45fSAndroid Build Coastguard Worker  *         EPIPE if communication with the system service has failed.
341*38e8c45fSAndroid Build Coastguard Worker  */
342*38e8c45fSAndroid Build Coastguard Worker int AThermal_registerThermalHeadroomListener(AThermalManager* _Nonnull manager,
343*38e8c45fSAndroid Build Coastguard Worker                                              AThermal_HeadroomCallback _Nullable callback,
344*38e8c45fSAndroid Build Coastguard Worker                                              void* _Nullable data) __INTRODUCED_IN(36);
345*38e8c45fSAndroid Build Coastguard Worker 
346*38e8c45fSAndroid Build Coastguard Worker /**
347*38e8c45fSAndroid Build Coastguard Worker  * Unregister a thermal headroom listener previously registered.
348*38e8c45fSAndroid Build Coastguard Worker  *
349*38e8c45fSAndroid Build Coastguard Worker  * No subsequent invocations of the callback will occur after this function returns successfully.
350*38e8c45fSAndroid Build Coastguard Worker  *
351*38e8c45fSAndroid Build Coastguard Worker  * Available since API level 36.
352*38e8c45fSAndroid Build Coastguard Worker  *
353*38e8c45fSAndroid Build Coastguard Worker  * @param manager The manager instance to use to unregister.
354*38e8c45fSAndroid Build Coastguard Worker  *                Acquired via {@link AThermal_acquireManager}.
355*38e8c45fSAndroid Build Coastguard Worker  * @param callback The callback function that was previously registered.
356*38e8c45fSAndroid Build Coastguard Worker  * @param data The data pointer that was previously registered.
357*38e8c45fSAndroid Build Coastguard Worker  *
358*38e8c45fSAndroid Build Coastguard Worker  * @return 0 on success
359*38e8c45fSAndroid Build Coastguard Worker  *         EINVAL if the listener and data pointer were not previously added.
360*38e8c45fSAndroid Build Coastguard Worker  *         EPIPE if communication with the system service has failed, the listener will not get
361*38e8c45fSAndroid Build Coastguard Worker  *               removed and this call should be retried
362*38e8c45fSAndroid Build Coastguard Worker  */
363*38e8c45fSAndroid Build Coastguard Worker 
364*38e8c45fSAndroid Build Coastguard Worker int AThermal_unregisterThermalHeadroomListener(AThermalManager* _Nonnull manager,
365*38e8c45fSAndroid Build Coastguard Worker                                                AThermal_HeadroomCallback _Nullable callback,
366*38e8c45fSAndroid Build Coastguard Worker                                                void* _Nullable data) __INTRODUCED_IN(36);
367*38e8c45fSAndroid Build Coastguard Worker 
368*38e8c45fSAndroid Build Coastguard Worker #ifdef __cplusplus
369*38e8c45fSAndroid Build Coastguard Worker }
370*38e8c45fSAndroid Build Coastguard Worker #endif
371*38e8c45fSAndroid Build Coastguard Worker 
372*38e8c45fSAndroid Build Coastguard Worker #endif // _ANDROID_THERMAL_H
373*38e8c45fSAndroid Build Coastguard Worker 
374*38e8c45fSAndroid Build Coastguard Worker /** @} */
375