]> git.saurik.com Git - apple/libc.git/blame - sys/OSThermalNotification.c
Libc-1081.1.3.tar.gz
[apple/libc.git] / sys / OSThermalNotification.c
CommitLineData
b5d655f7
A
1/*
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
1f2f436a 24#include <dispatch/dispatch.h>
b5d655f7
A
25#include <libkern/OSThermalNotification.h>
26#include <notify.h>
27
23e20b00
A
28#include <TargetConditionals.h>
29
30#define OSThermalPressureLevelName "com.apple.system.thermalpressurelevel"
31const char * const kOSThermalNotificationPressureLevelName = OSThermalPressureLevelName;
32
33#if TARGET_OS_IPHONE
6465356a
A
34#define OSThermalAlert "com.apple.system.thermalalert"
35#define OSThermalDecision "com.apple.system.thermaldecision"
1f2f436a 36#define OSThermalStatusName "com.apple.system.thermalstatus"
b5d655f7 37
6465356a
A
38const char * const kOSThermalNotificationAlert = OSThermalAlert;
39const char * const kOSThermalNotificationDecision = OSThermalDecision;
40const char * const kOSThermalNotificationName = OSThermalStatusName;
b5d655f7 41
1f2f436a
A
42static const char * const kOSThermalMitigationNames[kOSThermalMitigationCount] = {
43 OSThermalStatusName,
44 "com.apple.system.thermalmitigation.70percenttorch",
45 "com.apple.system.thermalmitigation.70percentbacklight",
46 "com.apple.system.thermalmitigation.50percenttorch",
47 "com.apple.system.thermalmitigation.50percentbacklight",
48 "com.apple.system.thermalmitigation.disabletorch",
49 "com.apple.system.thermalmitigation.25percentbacklight",
50 "com.apple.system.thermalmitigation.disablemapshalo",
51 "com.apple.system.thermalmitigation.appterminate",
ad3c9f2a
A
52 "com.apple.system.thermalmitigation.devicerestart",
53 "com.apple.system.thermalmitigation.thermaltableready"
1f2f436a 54};
b5d655f7 55
1f2f436a
A
56static int tokens[kOSThermalMitigationCount];
57static dispatch_once_t predicates[kOSThermalMitigationCount];
ad3c9f2a 58static bool thermalLevelsReady = false;
b5d655f7 59
1f2f436a
A
60OSThermalNotificationLevel _OSThermalNotificationLevelForBehavior(int behavior)
61{
62 uint64_t val = OSThermalNotificationLevelAny;
63 if (behavior >= 0 && behavior < kOSThermalMitigationCount) {
64 dispatch_once(&predicates[behavior], ^{
ad3c9f2a 65 (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]);
1f2f436a
A
66 });
67 (void)notify_get_state(tokens[behavior], &val);
68 }
b5d655f7
A
69 return (OSThermalNotificationLevel)val;
70}
1f2f436a
A
71
72void _OSThermalNotificationSetLevelForBehavior(int level, int behavior)
73{
74 uint64_t val = (uint64_t)level;
75 if (behavior >= 0 && behavior < kOSThermalMitigationCount) {
76 dispatch_once(&predicates[behavior], ^{
ad3c9f2a 77 (void)notify_register_check(kOSThermalMitigationNames[behavior], &tokens[behavior]);
1f2f436a
A
78 });
79 (void)notify_set_state(tokens[behavior], val);
ad3c9f2a
A
80
81 // Note:
82 // - We are ready when we program in the appterminate value.
83 // - Assumes that user programs kOSThermalMitigationNone level less than
84 // kOSThermalMitigationAppTerminate & kOSThermalMitigationDeviceRestart
85 if (behavior == kOSThermalMitigationAppTerminate) {
86 dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{
87 (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]);
88 });
89 (void)notify_set_state(tokens[kOSThermalMitigationThermalTableReady], kOSThermalMitigationCount);
90 }
1f2f436a
A
91 }
92}
93
94
95OSThermalNotificationLevel OSThermalNotificationCurrentLevel(void)
96{
ad3c9f2a
A
97 if (thermalLevelsReady) {
98 return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone);
99 }
100
101 uint64_t tableReady = 0;
102
103 dispatch_once(&predicates[kOSThermalMitigationThermalTableReady], ^{
104 (void)notify_register_check(kOSThermalMitigationNames[kOSThermalMitigationThermalTableReady], &tokens[kOSThermalMitigationThermalTableReady]);
105 });
106 (void)notify_get_state(tokens[kOSThermalMitigationThermalTableReady], &tableReady);
107
108 // If we are ready then optimize this so we don't call dispatch everytime.
109 if (tableReady == kOSThermalMitigationCount) {
110 thermalLevelsReady = true;
111 return _OSThermalNotificationLevelForBehavior(kOSThermalMitigationNone);
112 }
113 else {
114 // Allow reset so we can dynamically change the table without thermal trap screen appearing.
115 thermalLevelsReady = false;
116 }
117
118 // Not ready returns -1, which should not be equal or greater than any other thermal state.
119 return OSThermalNotificationLevelAny;
1f2f436a 120}
23e20b00
A
121
122#endif // TARGET_OS_IPHONE