Skip to content

Commit 684eaa7

Browse files
committed
Add NDK support for thermal headroom callback API
Update the thermal headroom thresholds polling API doc on removal of cache update for Android 16. Bug: 360486877 Flag: EXEMPT NDK Test: atest NativeThermalTest NativeThermalUnitTest Change-Id: If5456fdc009b1154101cdae95d8d2943b030fbad
1 parent d3ca472 commit 684eaa7

1 file changed

Lines changed: 94 additions & 21 deletions

File tree

include/android/thermal.h

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,45 +140,47 @@ void AThermal_releaseManager(AThermalManager* _Nonnull manager) __INTRODUCED_IN(
140140
* Available since API level 30.
141141
*
142142
* @param manager The manager instance to use to query the thermal status.
143-
* Acquired via {@link AThermal_acquireManager}.
143+
* Acquired via {@link AThermal_acquireManager}.
144144
*
145145
* @return current thermal status, ATHERMAL_STATUS_ERROR on failure.
146146
*/
147147
AThermalStatus
148-
AThermal_getCurrentThermalStatus(AThermalManager* _Nonnull manager) __INTRODUCED_IN(30);
148+
AThermal_getCurrentThermalStatus(AThermalManager *_Nonnull manager) __INTRODUCED_IN(30);
149149

150150
/**
151-
* Register the thermal status listener for thermal status change.
151+
* Register a thermal status listener for thermal status change.
152152
*
153153
* Available since API level 30.
154154
*
155155
* @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.
156+
* Acquired via {@link AThermal_acquireManager}.
157+
* @param callback The callback function to be called on system binder thread pool when thermal
158+
* status updated.
158159
* @param data The data pointer to be passed when callback is called.
159160
*
160161
* @return 0 on success
161162
* 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.
163+
* EPIPE if communication with the system service has failed, the listener will not get
164+
* removed and this call should be retried
164165
*/
165-
int AThermal_registerThermalStatusListener(AThermalManager* _Nonnull manager,
166+
int AThermal_registerThermalStatusListener(AThermalManager *_Nonnull manager,
166167
AThermal_StatusCallback _Nullable callback,
167168
void* _Nullable data) __INTRODUCED_IN(30);
168169

169170
/**
170-
* Unregister the thermal status listener previously resgistered.
171+
* Unregister a thermal status listener previously registered.
172+
*
173+
* No subsequent invocations of the callback will occur after this function returns successfully.
171174
*
172175
* Available since API level 30.
173176
*
174177
* @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.
178+
* Acquired via {@link AThermal_acquireManager}.
179+
* @param callback The callback function that was previously registered.
177180
* @param data The data pointer to be passed when callback is called.
178181
*
179182
* @return 0 on success
180183
* EINVAL if the listener and data pointer were not previously added.
181-
* EPERM if the required permission is not held.
182184
* EPIPE if communication with the system service has failed.
183185
*/
184186
int AThermal_unregisterThermalStatusListener(AThermalManager* _Nonnull manager,
@@ -254,32 +256,38 @@ typedef struct AThermalHeadroomThreshold AThermalHeadroomThreshold;
254256
* The headroom threshold is used to interpret the possible thermal throttling status based on
255257
* the headroom prediction. For example, if the headroom threshold for
256258
* {@link ATHERMAL_STATUS_LIGHT} is 0.7, and a headroom prediction in 10s returns 0.75
257-
* (or {@code AThermal_getThermalHeadroom(10)=0.75}), one can expect that in 10 seconds the system
259+
* (or `AThermal_getThermalHeadroom(10)=0.75}`, one can expect that in 10 seconds the system
258260
* could be in lightly throttled state if the workload remains the same. The app can consider
259261
* taking actions according to the nearest throttling status the difference between the headroom and
260262
* the threshold.
261263
* <p>
262264
* For new devices it's guaranteed to have a single sensor, but for older devices with multiple
263265
* sensors reporting different threshold values, the minimum threshold is taken to be conservative
264266
* on predictions. Thus, when reading real-time headroom, it's not guaranteed that a real-time value
265-
* of 0.75 (or {@code AThermal_getThermalHeadroom(0)}=0.75) exceeding the threshold of 0.7 above
267+
* of 0.75 (or `AThermal_getThermalHeadroom(0)=0.75`) exceeding the threshold of 0.7 above
266268
* will always come with lightly throttled state
267-
* (or {@code AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT}) but it can be lower
268-
* (or {@code AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE}).
269+
* (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_LIGHT`) but it can be lower
270+
* (or `AThermal_getCurrentThermalStatus()=ATHERMAL_STATUS_NONE`).
269271
* While it's always guaranteed that the device won't be throttled heavier than the unmet
270272
* threshold's state, so a real-time headroom of 0.75 will never come with
271273
* {@link #ATHERMAL_STATUS_MODERATE} but always lower, and 0.65 will never come with
272274
* {@link ATHERMAL_STATUS_LIGHT} but {@link #ATHERMAL_STATUS_NONE}.
273275
* <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}.
276+
* Starting in Android 16, this polling API may return different results when called depending on
277+
* the device. The new headroom listener API {@link #AThermal_HeadroomCallback} can be used to
278+
* detect headroom thresholds changes.
279+
* <p>
280+
* Before API level 36 the returned list of thresholds is cached on first successful query and owned
281+
* by the thermal manager, which will not change between calls to this function. The caller should
282+
* only need to free the manager with {@link AThermal_releaseManager}.
283+
* <p>
277284
*
278285
* @param manager The manager instance to use.
279286
* Acquired via {@link AThermal_acquireManager}.
280287
* @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.
288+
* will be set to a new array of thresholds if thermal thresholds are supported
289+
* by the system or device, otherwise nullptr or unmodified. The client should
290+
* clean up the thresholds by array-deleting the threshold pointer.
283291
* @param size non-null output pointer whose value will be set to the size of the threshold array
284292
* or 0 if it's not supported.
285293
* @return 0 on success
@@ -292,6 +300,71 @@ int AThermal_getThermalHeadroomThresholds(AThermalManager* _Nonnull manager,
292300
* _Nullable outThresholds,
293301
size_t* _Nonnull size) __INTRODUCED_IN(35);
294302

303+
/**
304+
* Prototype of the function that is called when thermal headroom or thresholds changes.
305+
* It's passed the updated thermal headroom and thresholds as parameters, as well as the
306+
* pointer provided by the client that registered a callback.
307+
*
308+
* @param data The data pointer to be passed when callback is called.
309+
* @param headroom The current non-negative normalized headroom value, also see
310+
* {@link AThermal_getThermalHeadroom}.
311+
* @param forecastHeadroom The forecasted non-negative normalized headroom value, also see
312+
* {@link AThermal_getThermalHeadroom}.
313+
* @param forecastSeconds The seconds used for the forecast by the system.
314+
* @param thresholds The current headroom thresholds. The thresholds pointer will be a constant
315+
* shared across all callbacks registered from the same process, and it will be
316+
* destroyed after all the callbacks are finished. If the client intents to
317+
* persist the values, it should make a copy of it during the callback.
318+
* @param thresholdsCount The count of thresholds.
319+
*/
320+
typedef void (*AThermal_HeadroomCallback)(void *_Nullable data,
321+
float headroom,
322+
float forecastHeadroom,
323+
int forecastSeconds,
324+
const AThermalHeadroomThreshold* _Nullable thresholds,
325+
size_t thresholdsCount);
326+
327+
/**
328+
* Register a thermal headroom listener for thermal headroom or thresholds change.
329+
*
330+
* Available since API level 36.
331+
*
332+
* @param manager The manager instance to use to register.
333+
* Acquired via {@link AThermal_acquireManager}.
334+
* @param callback The callback function to be called on system binder thread pool when thermal
335+
* headroom or thresholds update.
336+
* @param data The data pointer to be passed when callback is called.
337+
*
338+
* @return 0 on success
339+
* EINVAL if the listener and data pointer were previously added and not removed.
340+
* EPIPE if communication with the system service has failed.
341+
*/
342+
int AThermal_registerThermalHeadroomListener(AThermalManager* _Nonnull manager,
343+
AThermal_HeadroomCallback _Nullable callback,
344+
void* _Nullable data) __INTRODUCED_IN(36);
345+
346+
/**
347+
* Unregister a thermal headroom listener previously registered.
348+
*
349+
* No subsequent invocations of the callback will occur after this function returns successfully.
350+
*
351+
* Available since API level 36.
352+
*
353+
* @param manager The manager instance to use to unregister.
354+
* Acquired via {@link AThermal_acquireManager}.
355+
* @param callback The callback function that was previously registered.
356+
* @param data The data pointer that was previously registered.
357+
*
358+
* @return 0 on success
359+
* EINVAL if the listener and data pointer were not previously added.
360+
* EPIPE if communication with the system service has failed, the listener will not get
361+
* removed and this call should be retried
362+
*/
363+
364+
int AThermal_unregisterThermalHeadroomListener(AThermalManager* _Nonnull manager,
365+
AThermal_HeadroomCallback _Nullable callback,
366+
void* _Nullable data) __INTRODUCED_IN(36);
367+
295368
#ifdef __cplusplus
296369
}
297370
#endif

0 commit comments

Comments
 (0)