blob: 1d7ad12294fb27632d7db95b72d38e912acd52d3 [file] [log] [blame]
Chris Ye1a5a8882020-01-15 10:51:47 -08001/*
2 * Copyright (C) 2020 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/**
18 * @addtogroup Thermal
19 * @{
20 */
21
22/**
23 * @file thermal.h
24 */
25
26#ifndef _ANDROID_THERMAL_H
27#define _ANDROID_THERMAL_H
28
29#include <sys/cdefs.h>
30
31/******************************************************************
32 *
33 * IMPORTANT NOTICE:
34 *
35 * This file is part of Android's set of stable system headers
36 * exposed by the Android NDK (Native Development Kit).
37 *
38 * Third-party source AND binary code relies on the definitions
39 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
40 *
41 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
42 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
43 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
44 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
45 */
46
47/*
48 * Structures and functions to access thermal status and register/unregister
49 * thermal status listener in native code.
50 */
51
52#include <stdint.h>
53#include <sys/types.h>
54
55#if !defined(__INTRODUCED_IN)
Chris Forbes56e925a2020-09-15 09:34:59 -070056#define __INTRODUCED_IN(__api_level) /* nothing */
Chris Ye1a5a8882020-01-15 10:51:47 -080057#endif
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
gfan5d5faa42021-04-12 15:14:29 -070063/**
64 * Thermal status used in function {@link AThermal_getCurrentThermalStatus} and
65 * {@link AThermal_StatusCallback}.
66 */
Chris Ye1a5a8882020-01-15 10:51:47 -080067enum AThermalStatus {
68 /** Error in thermal status. */
69 ATHERMAL_STATUS_ERROR = -1,
70 /** Not under throttling. */
71 ATHERMAL_STATUS_NONE = 0,
72 /** Light throttling where UX is not impacted. */
73 ATHERMAL_STATUS_LIGHT = 1,
74 /** Moderate throttling where UX is not largely impacted. */
75 ATHERMAL_STATUS_MODERATE = 2,
76 /** Severe throttling where UX is largely impacted. */
77 ATHERMAL_STATUS_SEVERE = 3,
78 /** Platform has done everything to reduce power. */
79 ATHERMAL_STATUS_CRITICAL = 4,
80 /**
81 * Key components in platform are shutting down due to thermal condition.
82 * Device functionalities will be limited.
83 */
84 ATHERMAL_STATUS_EMERGENCY = 5,
85 /** Need shutdown immediately. */
86 ATHERMAL_STATUS_SHUTDOWN = 6,
87};
Dan Albertc796b902024-08-01 22:36:07 +000088typedef enum AThermalStatus AThermalStatus;
Chris Ye1a5a8882020-01-15 10:51:47 -080089
90/**
91 * An opaque type representing a handle to a thermal manager.
92 * An instance of thermal manager must be acquired prior to
93 * using thermal status APIs and must be released after use.
94 *
95 * <p>To use:<ul>
96 * <li>Create a new thermal manager instance by calling the
97 * {@link AThermal_acquireManager} function.</li>
98 * <li>Get current thermal status with
99 * {@link AThermal_getCurrentThermalStatus}.</li>
100 * <li>Register a thermal status listener with
101 * {@link AThermal_registerThermalStatusListener}.</li>
102 * <li>Unregister a thermal status listener with
103 * {@link AThermal_unregisterThermalStatusListener}.</li>
104 * <li>Release the thermal manager instance with
105 * {@link AThermal_releaseManager}.</li></ul></p>
106 *
107 */
108typedef struct AThermalManager AThermalManager;
109
110/**
111 * Prototype of the function that is called when thermal status changes.
112 * It's passed the updated thermal status as parameter, as well as the
113 * pointer provided by the client that registered a callback.
114 */
Xiang Wang800af342024-01-09 13:24:41 -0800115typedef void (*AThermal_StatusCallback)(void* _Nullable data, AThermalStatus status);
Chris Ye1a5a8882020-01-15 10:51:47 -0800116
117/**
118 * Acquire an instance of the thermal manager. This must be freed using
119 * {@link AThermal_releaseManager}.
120 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700121 * Available since API level 30.
122 *
Chris Ye1a5a8882020-01-15 10:51:47 -0800123 * @return manager instance on success, nullptr on failure.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700124 */
Xiang Wang800af342024-01-09 13:24:41 -0800125AThermalManager* _Nonnull AThermal_acquireManager() __INTRODUCED_IN(30);
Chris Ye1a5a8882020-01-15 10:51:47 -0800126
127/**
128 * Release the thermal manager pointer acquired via
129 * {@link AThermal_acquireManager}.
130 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700131 * Available since API level 30.
Chris Ye1a5a8882020-01-15 10:51:47 -0800132 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700133 * @param manager The manager to be released.
Chris Ye1a5a8882020-01-15 10:51:47 -0800134 */
Xiang Wang800af342024-01-09 13:24:41 -0800135void AThermal_releaseManager(AThermalManager* _Nonnull manager) __INTRODUCED_IN(30);
Chris Ye1a5a8882020-01-15 10:51:47 -0800136
137/**
138 * Gets the current thermal status.
139 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700140 * Available since API level 30.
141 *
Chris Ye1a5a8882020-01-15 10:51:47 -0800142 * @param manager The manager instance to use to query the thermal status.
143 * Acquired via {@link AThermal_acquireManager}.
144 *
145 * @return current thermal status, ATHERMAL_STATUS_ERROR on failure.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700146 */
Xiang Wang800af342024-01-09 13:24:41 -0800147AThermalStatus
148AThermal_getCurrentThermalStatus(AThermalManager* _Nonnull manager) __INTRODUCED_IN(30);
Chris Ye1a5a8882020-01-15 10:51:47 -0800149
150/**
151 * Register the thermal status listener for thermal status change.
152 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700153 * Available since API level 30.
154 *
Chris Ye1a5a8882020-01-15 10:51:47 -0800155 * @param manager The manager instance to use to register.
156 * Acquired via {@link AThermal_acquireManager}.
157 * @param callback The callback function to be called when thermal status updated.
158 * @param data The data pointer to be passed when callback is called.
159 *
160 * @return 0 on success
161 * EINVAL if the listener and data pointer were previously added and not removed.
162 * EPERM if the required permission is not held.
163 * EPIPE if communication with the system service has failed.
164 */
Xiang Wang800af342024-01-09 13:24:41 -0800165int AThermal_registerThermalStatusListener(AThermalManager* _Nonnull manager,
166 AThermal_StatusCallback _Nullable callback,
167 void* _Nullable data) __INTRODUCED_IN(30);
Chris Ye1a5a8882020-01-15 10:51:47 -0800168
169/**
170 * Unregister the thermal status listener previously resgistered.
171 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700172 * Available since API level 30.
173 *
Chris Ye1a5a8882020-01-15 10:51:47 -0800174 * @param manager The manager instance to use to unregister.
175 * Acquired via {@link AThermal_acquireManager}.
176 * @param callback The callback function to be called when thermal status updated.
177 * @param data The data pointer to be passed when callback is called.
178 *
179 * @return 0 on success
180 * EINVAL if the listener and data pointer were not previously added.
181 * EPERM if the required permission is not held.
182 * EPIPE if communication with the system service has failed.
183 */
Xiang Wang800af342024-01-09 13:24:41 -0800184int AThermal_unregisterThermalStatusListener(AThermalManager* _Nonnull manager,
185 AThermal_StatusCallback _Nullable callback,
186 void* _Nullable data) __INTRODUCED_IN(30);
Chris Ye1a5a8882020-01-15 10:51:47 -0800187
Chris Forbes56e925a2020-09-15 09:34:59 -0700188/**
189 * Provides an estimate of how much thermal headroom the device currently has before
190 * hitting severe throttling.
191 *
192 * Note that this only attempts to track the headroom of slow-moving sensors, such as
193 * the skin temperature sensor. This means that there is no benefit to calling this function
194 * more frequently than about once per second, and attempted to call significantly
Elliott Hughesd4b452c2023-09-15 22:52:03 +0000195 * more frequently may result in the function returning `NaN`.
Chris Forbes56e925a2020-09-15 09:34:59 -0700196 *
197 * In addition, in order to be able to provide an accurate forecast, the system does
198 * not attempt to forecast until it has multiple temperature samples from which to
199 * extrapolate. This should only take a few seconds from the time of the first call,
200 * but during this time, no forecasting will occur, and the current headroom will be
Elliott Hughesd4b452c2023-09-15 22:52:03 +0000201 * returned regardless of the value of `forecastSeconds`.
Chris Forbes56e925a2020-09-15 09:34:59 -0700202 *
203 * The value returned is a non-negative float that represents how much of the thermal envelope
204 * is in use (or is forecasted to be in use). A value of 1.0 indicates that the device is
gfan5d5faa42021-04-12 15:14:29 -0700205 * (or will be) throttled at {@link #ATHERMAL_STATUS_SEVERE}. Such throttling can affect the
Chris Forbes56e925a2020-09-15 09:34:59 -0700206 * CPU, GPU, and other subsystems. Values may exceed 1.0, but there is no implied mapping
207 * to specific thermal levels beyond that point. This means that values greater than 1.0
gfan5d5faa42021-04-12 15:14:29 -0700208 * may correspond to {@link #ATHERMAL_STATUS_SEVERE}, but may also represent heavier throttling.
Chris Forbes56e925a2020-09-15 09:34:59 -0700209 *
210 * A value of 0.0 corresponds to a fixed distance from 1.0, but does not correspond to any
211 * particular thermal status or temperature. Values on (0.0, 1.0] may be expected to scale
212 * linearly with temperature, though temperature changes over time are typically not linear.
213 * Negative values will be clamped to 0.0 before returning.
214 *
215 * Available since API level 31.
216 *
217 * @param manager The manager instance to use.
218 * Acquired via {@link AThermal_acquireManager}.
219 * @param forecastSeconds how many seconds into the future to forecast. Given that device
220 * conditions may change at any time, forecasts from further in the
221 * future will likely be less accurate than forecasts in the near future.
222 * @return a value greater than equal to 0.0, where 1.0 indicates the SEVERE throttling threshold,
223 * as described above. Returns NaN if the device does not support this functionality or
224 * if this function is called significantly faster than once per second.
225 */
Xiang Wang800af342024-01-09 13:24:41 -0800226float AThermal_getThermalHeadroom(AThermalManager* _Nonnull manager,
227 int forecastSeconds) __INTRODUCED_IN(31);
Chris Forbes56e925a2020-09-15 09:34:59 -0700228
Xiang Wange6c65b32023-10-09 13:39:37 -0700229/**
230 * This struct defines an instance of headroom threshold value and its status.
231 * <p>
232 * The value should be monotonically non-decreasing as the thermal status increases.
233 * For {@link ATHERMAL_STATUS_SEVERE}, its headroom threshold is guaranteed to
234 * be 1.0f. For status below severe status, the value should be lower or equal
235 * to 1.0f, and for status above severe, the value should be larger or equal to 1.0f.
236 * <p>
237 * Also see {@link AThermal_getThermalHeadroom} for explanation on headroom, and
238 * {@link AThermal_getThermalHeadroomThresholds} for how to use this.
239 */
240struct AThermalHeadroomThreshold {
241 float headroom;
242 AThermalStatus thermalStatus;
243};
Dan Albertc796b902024-08-01 22:36:07 +0000244typedef struct AThermalHeadroomThreshold AThermalHeadroomThreshold;
Xiang Wange6c65b32023-10-09 13:39:37 -0700245
246/**
247 * Gets the thermal headroom thresholds for all available thermal status.
248 *
249 * A thermal status will only exist in output if the device manufacturer has the
250 * corresponding threshold defined for at least one of its slow-moving skin temperature
251 * sensors. If it's set, one should also expect to get it from
252 * {@link #AThermal_getCurrentThermalStatus} or {@link AThermal_StatusCallback}.
253 * <p>
254 * The headroom threshold is used to interpret the possible thermal throttling status based on
255 * the headroom prediction. For example, if the headroom threshold for
256 * {@link ATHERMAL_STATUS_LIGHT} is 0.7, and a headroom prediction in 10s returns 0.75
Xiang Wang3c6ebed2024-11-22 23:05:41 +0000257 * (or `AThermal_getThermalHeadroom(10)=0.75`), one can expect that in 10 seconds the system
Xiang Wange6c65b32023-10-09 13:39:37 -0700258 * could be in lightly throttled state if the workload remains the same. The app can consider
259 * taking actions according to the nearest throttling status the difference between the headroom and
260 * the threshold.
261 * <p>
262 * For new devices it's guaranteed to have a single sensor, but for older devices with multiple
263 * sensors reporting different threshold values, the minimum threshold is taken to be conservative
264 * on predictions. Thus, when reading real-time headroom, it's not guaranteed that a real-time value
Xiang Wang3c6ebed2024-11-22 23:05:41 +0000265 * of 0.75 (or `AThermal_getThermalHeadroom(0)`=0.75) exceeding the threshold of 0.7 above
Xiang Wange6c65b32023-10-09 13:39:37 -0700266 * will always come with lightly throttled state
Xiang Wang3c6ebed2024-11-22 23:05:41 +0000267 * (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT`) but it can be lower
268 * (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE`).
Xiang Wange6c65b32023-10-09 13:39:37 -0700269 * While it's always guaranteed that the device won't be throttled heavier than the unmet
270 * threshold's state, so a real-time headroom of 0.75 will never come with
271 * {@link #ATHERMAL_STATUS_MODERATE} but always lower, and 0.65 will never come with
272 * {@link ATHERMAL_STATUS_LIGHT} but {@link #ATHERMAL_STATUS_NONE}.
273 * <p>
274 * The returned list of thresholds is cached on first successful query and owned by the thermal
275 * manager, which will not change between calls to this function. The caller should only need to
276 * free the manager with {@link AThermal_releaseManager}.
277 *
278 * @param manager The manager instance to use.
279 * Acquired via {@link AThermal_acquireManager}.
280 * @param outThresholds non-null output pointer to null AThermalHeadroomThreshold pointer, which
281 * will be set to the cached array of thresholds if thermal thresholds are supported
282 * by the system or device, otherwise nullptr or unmodified.
283 * @param size non-null output pointer whose value will be set to the size of the threshold array
284 * or 0 if it's not supported.
285 * @return 0 on success
286 * EINVAL if outThresholds or size_t is nullptr, or *outThresholds is not nullptr.
287 * EPIPE if communication with the system service has failed.
288 * ENOSYS if the feature is disabled by the current system.
289 */
Xiang Wang800af342024-01-09 13:24:41 -0800290int AThermal_getThermalHeadroomThresholds(AThermalManager* _Nonnull manager,
291 const AThermalHeadroomThreshold* _Nonnull
292 * _Nullable outThresholds,
293 size_t* _Nonnull size) __INTRODUCED_IN(35);
Xiang Wange6c65b32023-10-09 13:39:37 -0700294
Chris Ye1a5a8882020-01-15 10:51:47 -0800295#ifdef __cplusplus
296}
297#endif
298
299#endif // _ANDROID_THERMAL_H
300
301/** @} */