| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Forbes | 56e925a | 2020-09-15 09:34:59 -0700 | [diff] [blame] | 56 | #define __INTRODUCED_IN(__api_level) /* nothing */ | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 57 | #endif | 
|  | 58 |  | 
|  | 59 | #ifdef __cplusplus | 
|  | 60 | extern "C" { | 
|  | 61 | #endif | 
|  | 62 |  | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 63 | enum AThermalStatus { | 
|  | 64 | /** Error in thermal status. */ | 
|  | 65 | ATHERMAL_STATUS_ERROR = -1, | 
|  | 66 | /** Not under throttling. */ | 
|  | 67 | ATHERMAL_STATUS_NONE = 0, | 
|  | 68 | /** Light throttling where UX is not impacted. */ | 
|  | 69 | ATHERMAL_STATUS_LIGHT = 1, | 
|  | 70 | /** Moderate throttling where UX is not largely impacted. */ | 
|  | 71 | ATHERMAL_STATUS_MODERATE = 2, | 
|  | 72 | /** Severe throttling where UX is largely impacted. */ | 
|  | 73 | ATHERMAL_STATUS_SEVERE = 3, | 
|  | 74 | /** Platform has done everything to reduce power. */ | 
|  | 75 | ATHERMAL_STATUS_CRITICAL = 4, | 
|  | 76 | /** | 
|  | 77 | * Key components in platform are shutting down due to thermal condition. | 
|  | 78 | * Device functionalities will be limited. | 
|  | 79 | */ | 
|  | 80 | ATHERMAL_STATUS_EMERGENCY = 5, | 
|  | 81 | /** Need shutdown immediately. */ | 
|  | 82 | ATHERMAL_STATUS_SHUTDOWN = 6, | 
|  | 83 | }; | 
|  | 84 |  | 
|  | 85 | /** | 
|  | 86 | * An opaque type representing a handle to a thermal manager. | 
|  | 87 | * An instance of thermal manager must be acquired prior to | 
|  | 88 | * using thermal status APIs and must be released after use. | 
|  | 89 | * | 
|  | 90 | * <p>To use:<ul> | 
|  | 91 | *    <li>Create a new thermal manager instance by calling the | 
|  | 92 | *        {@link AThermal_acquireManager} function.</li> | 
|  | 93 | *    <li>Get current thermal status with | 
|  | 94 | *        {@link AThermal_getCurrentThermalStatus}.</li> | 
|  | 95 | *    <li>Register a thermal status listener with | 
|  | 96 | *        {@link AThermal_registerThermalStatusListener}.</li> | 
|  | 97 | *    <li>Unregister a thermal status listener with | 
|  | 98 | *        {@link AThermal_unregisterThermalStatusListener}.</li> | 
|  | 99 | *    <li>Release the thermal manager instance with | 
|  | 100 | *        {@link AThermal_releaseManager}.</li></ul></p> | 
|  | 101 | * | 
|  | 102 | */ | 
|  | 103 | typedef struct AThermalManager AThermalManager; | 
|  | 104 |  | 
|  | 105 | /** | 
|  | 106 | * Prototype of the function that is called when thermal status changes. | 
|  | 107 | * It's passed the updated thermal status as parameter, as well as the | 
|  | 108 | * pointer provided by the client that registered a callback. | 
|  | 109 | */ | 
| Chris Ye | 8e8cec4 | 2020-03-26 14:14:58 -0700 | [diff] [blame] | 110 | typedef void (*AThermal_StatusCallback)(void *data, AThermalStatus status); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 111 |  | 
|  | 112 | /** | 
|  | 113 | * Acquire an instance of the thermal manager. This must be freed using | 
|  | 114 | * {@link AThermal_releaseManager}. | 
|  | 115 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 116 | * Available since API level 30. | 
|  | 117 | * | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 118 | * @return manager instance on success, nullptr on failure. | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 119 | */ | 
|  | 120 | AThermalManager* AThermal_acquireManager() __INTRODUCED_IN(30); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 121 |  | 
|  | 122 | /** | 
|  | 123 | * Release the thermal manager pointer acquired via | 
|  | 124 | * {@link AThermal_acquireManager}. | 
|  | 125 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 126 | * Available since API level 30. | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 127 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 128 | * @param manager The manager to be released. | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 129 | */ | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 130 | void AThermal_releaseManager(AThermalManager *manager) __INTRODUCED_IN(30); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 131 |  | 
|  | 132 | /** | 
|  | 133 | * Gets the current thermal status. | 
|  | 134 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 135 | * Available since API level 30. | 
|  | 136 | * | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 137 | * @param manager The manager instance to use to query the thermal status. | 
|  | 138 | * Acquired via {@link AThermal_acquireManager}. | 
|  | 139 | * | 
|  | 140 | * @return current thermal status, ATHERMAL_STATUS_ERROR on failure. | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 141 | */ | 
|  | 142 | AThermalStatus AThermal_getCurrentThermalStatus(AThermalManager *manager) __INTRODUCED_IN(30); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 143 |  | 
|  | 144 | /** | 
|  | 145 | * Register the thermal status listener for thermal status change. | 
|  | 146 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 147 | * Available since API level 30. | 
|  | 148 | * | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 149 | * @param manager The manager instance to use to register. | 
|  | 150 | * Acquired via {@link AThermal_acquireManager}. | 
|  | 151 | * @param callback The callback function to be called when thermal status updated. | 
|  | 152 | * @param data The data pointer to be passed when callback is called. | 
|  | 153 | * | 
|  | 154 | * @return 0 on success | 
|  | 155 | *         EINVAL if the listener and data pointer were previously added and not removed. | 
|  | 156 | *         EPERM if the required permission is not held. | 
|  | 157 | *         EPIPE if communication with the system service has failed. | 
|  | 158 | */ | 
|  | 159 | int AThermal_registerThermalStatusListener(AThermalManager *manager, | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 160 | AThermal_StatusCallback callback, void *data) __INTRODUCED_IN(30); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 161 |  | 
|  | 162 | /** | 
|  | 163 | * Unregister the thermal status listener previously resgistered. | 
|  | 164 | * | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 165 | * Available since API level 30. | 
|  | 166 | * | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 167 | * @param manager The manager instance to use to unregister. | 
|  | 168 | * Acquired via {@link AThermal_acquireManager}. | 
|  | 169 | * @param callback The callback function to be called when thermal status updated. | 
|  | 170 | * @param data The data pointer to be passed when callback is called. | 
|  | 171 | * | 
|  | 172 | * @return 0 on success | 
|  | 173 | *         EINVAL if the listener and data pointer were not previously added. | 
|  | 174 | *         EPERM if the required permission is not held. | 
|  | 175 | *         EPIPE if communication with the system service has failed. | 
|  | 176 | */ | 
|  | 177 | int AThermal_unregisterThermalStatusListener(AThermalManager *manager, | 
| Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 178 | AThermal_StatusCallback callback, void *data) __INTRODUCED_IN(30); | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 179 |  | 
| Chris Forbes | 56e925a | 2020-09-15 09:34:59 -0700 | [diff] [blame] | 180 | /** | 
|  | 181 | * Provides an estimate of how much thermal headroom the device currently has before | 
|  | 182 | * hitting severe throttling. | 
|  | 183 | * | 
|  | 184 | * Note that this only attempts to track the headroom of slow-moving sensors, such as | 
|  | 185 | * the skin temperature sensor. This means that there is no benefit to calling this function | 
|  | 186 | * more frequently than about once per second, and attempted to call significantly | 
|  | 187 | * more frequently may result in the function returning {@code NaN}. | 
|  | 188 | * | 
|  | 189 | * In addition, in order to be able to provide an accurate forecast, the system does | 
|  | 190 | * not attempt to forecast until it has multiple temperature samples from which to | 
|  | 191 | * extrapolate. This should only take a few seconds from the time of the first call, | 
|  | 192 | * but during this time, no forecasting will occur, and the current headroom will be | 
|  | 193 | * returned regardless of the value of {@code forecastSeconds}. | 
|  | 194 | * | 
|  | 195 | * The value returned is a non-negative float that represents how much of the thermal envelope | 
|  | 196 | * is in use (or is forecasted to be in use). A value of 1.0 indicates that the device is | 
|  | 197 | * (or will be) throttled at {@link #THERMAL_STATUS_SEVERE}. Such throttling can affect the | 
|  | 198 | * CPU, GPU, and other subsystems. Values may exceed 1.0, but there is no implied mapping | 
|  | 199 | * to specific thermal levels beyond that point. This means that values greater than 1.0 | 
|  | 200 | * may correspond to {@link #THERMAL_STATUS_SEVERE}, but may also represent heavier throttling. | 
|  | 201 | * | 
|  | 202 | * A value of 0.0 corresponds to a fixed distance from 1.0, but does not correspond to any | 
|  | 203 | * particular thermal status or temperature. Values on (0.0, 1.0] may be expected to scale | 
|  | 204 | * linearly with temperature, though temperature changes over time are typically not linear. | 
|  | 205 | * Negative values will be clamped to 0.0 before returning. | 
|  | 206 | * | 
|  | 207 | * Available since API level 31. | 
|  | 208 | * | 
|  | 209 | * @param manager The manager instance to use. | 
|  | 210 | *                Acquired via {@link AThermal_acquireManager}. | 
|  | 211 | * @param forecastSeconds how many seconds into the future to forecast. Given that device | 
|  | 212 | *                        conditions may change at any time, forecasts from further in the | 
|  | 213 | *                        future will likely be less accurate than forecasts in the near future. | 
|  | 214 | * @return a value greater than equal to 0.0, where 1.0 indicates the SEVERE throttling threshold, | 
|  | 215 | *         as described above. Returns NaN if the device does not support this functionality or | 
|  | 216 | *         if this function is called significantly faster than once per second. | 
|  | 217 | */ | 
|  | 218 | float AThermal_getThermalHeadroom(AThermalManager *manager, | 
|  | 219 | int forecastSeconds) __INTRODUCED_IN(31); | 
|  | 220 |  | 
| Chris Ye | 1a5a888 | 2020-01-15 10:51:47 -0800 | [diff] [blame] | 221 | #ifdef __cplusplus | 
|  | 222 | } | 
|  | 223 | #endif | 
|  | 224 |  | 
|  | 225 | #endif // _ANDROID_THERMAL_H | 
|  | 226 |  | 
|  | 227 | /** @} */ |