blob: be7e824c49fba3fb20812123cbca5027509accc7 [file] [log] [blame]
Todd Poynordd055822017-05-25 17:53:21 -07001/*
2 * Copyright (c) 2017 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
17package android.os;
18
Wei Wangbad7c202018-11-01 11:57:39 -070019import android.annotation.IntDef;
20import android.hardware.thermal.V2_0.TemperatureType;
21import android.hardware.thermal.V2_0.ThrottlingSeverity;
22
23import java.lang.annotation.Retention;
24import java.lang.annotation.RetentionPolicy;
25
Todd Poynordd055822017-05-25 17:53:21 -070026/**
27 * Temperature values used by IThermalService.
Wei Wang37b17542018-11-12 11:02:09 -080028 *
Todd Poynordd055822017-05-25 17:53:21 -070029 * @hide
30 */
Wei Wangcc913612018-11-28 23:34:43 -080031public final class Temperature implements Parcelable {
Wei Wangbad7c202018-11-01 11:57:39 -070032 /** Temperature value */
Todd Poynordd055822017-05-25 17:53:21 -070033 private float mValue;
Wei Wangbad7c202018-11-01 11:57:39 -070034 /** A temperature type from ThermalHAL */
Todd Poynordd055822017-05-25 17:53:21 -070035 private int mType;
Wei Wangbad7c202018-11-01 11:57:39 -070036 /** Name of this temperature */
37 private String mName;
38 /** The level of the sensor is currently in throttling */
39 private int mStatus;
Todd Poynordd055822017-05-25 17:53:21 -070040
Wei Wangbad7c202018-11-01 11:57:39 -070041 @IntDef(prefix = { "THROTTLING_" }, value = {
42 THROTTLING_NONE,
43 THROTTLING_LIGHT,
44 THROTTLING_MODERATE,
45 THROTTLING_SEVERE,
46 THROTTLING_CRITICAL,
Wei Wanged2c9022018-12-04 14:44:36 -080047 THROTTLING_EMERGENCY,
Wei Wangbad7c202018-11-01 11:57:39 -070048 THROTTLING_SHUTDOWN,
49 })
50 @Retention(RetentionPolicy.SOURCE)
51 public @interface ThrottlingStatus {}
52
53 /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
54 public static final int THROTTLING_NONE = ThrottlingSeverity.NONE;
55 public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT;
56 public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE;
57 public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE;
58 public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL;
Wei Wanged2c9022018-12-04 14:44:36 -080059 public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY;
Wei Wangbad7c202018-11-01 11:57:39 -070060 public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN;
61
Wei Wangbad7c202018-11-01 11:57:39 -070062 @IntDef(prefix = { "TYPE_" }, value = {
63 TYPE_UNKNOWN,
64 TYPE_CPU,
65 TYPE_GPU,
66 TYPE_BATTERY,
67 TYPE_SKIN,
68 TYPE_USB_PORT,
69 TYPE_POWER_AMPLIFIER,
70 TYPE_BCL_VOLTAGE,
71 TYPE_BCL_CURRENT,
72 TYPE_BCL_PERCENTAGE,
Wei Wangbc79c4f2018-12-10 11:40:40 -080073 TYPE_NPU,
Wei Wangbad7c202018-11-01 11:57:39 -070074 })
75 @Retention(RetentionPolicy.SOURCE)
76 public @interface Type {}
77
78 /* Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
79 public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN;
80 public static final int TYPE_CPU = TemperatureType.CPU;
81 public static final int TYPE_GPU = TemperatureType.GPU;
82 public static final int TYPE_BATTERY = TemperatureType.BATTERY;
83 public static final int TYPE_SKIN = TemperatureType.SKIN;
84 public static final int TYPE_USB_PORT = TemperatureType.USB_PORT;
85 public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER;
86 public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE;
87 public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT;
88 public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE;
Wei Wangbc79c4f2018-12-10 11:40:40 -080089 public static final int TYPE_NPU = TemperatureType.NPU;
Wei Wangbad7c202018-11-01 11:57:39 -070090
91 /**
92 * Verify a valid temperature type.
93 *
94 * @return true if a temperature type is valid otherwise false.
95 */
Wei Wang37b17542018-11-12 11:02:09 -080096 public static boolean isValidType(@Type int type) {
Wei Wangbc79c4f2018-12-10 11:40:40 -080097 return type >= TYPE_UNKNOWN && type <= TYPE_NPU;
Todd Poynordd055822017-05-25 17:53:21 -070098 }
99
Wei Wang37b17542018-11-12 11:02:09 -0800100 /**
101 * Verify a valid throttling status.
102 *
103 * @return true if a status is valid otherwise false.
104 */
105 public static boolean isValidStatus(@ThrottlingStatus int status) {
106 return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN;
107 }
108
Wei Wangbad7c202018-11-01 11:57:39 -0700109 public Temperature() {
110 this(Float.NaN, TYPE_UNKNOWN, "", THROTTLING_NONE);
111 }
112
Wei Wang37b17542018-11-12 11:02:09 -0800113 public Temperature(float value, @Type int type, String name, @ThrottlingStatus int status) {
Todd Poynordd055822017-05-25 17:53:21 -0700114 mValue = value;
Wei Wangbad7c202018-11-01 11:57:39 -0700115 mType = isValidType(type) ? type : TYPE_UNKNOWN;
116 mName = name;
Wei Wang37b17542018-11-12 11:02:09 -0800117 mStatus = isValidStatus(status) ? status : THROTTLING_NONE;
Todd Poynordd055822017-05-25 17:53:21 -0700118 }
119
120 /**
121 * Return the temperature value.
Wei Wangbad7c202018-11-01 11:57:39 -0700122 *
123 * @return a temperature value in floating point could be NaN.
Todd Poynordd055822017-05-25 17:53:21 -0700124 */
125 public float getValue() {
126 return mValue;
127 }
128
129 /**
130 * Return the temperature type.
Wei Wangbad7c202018-11-01 11:57:39 -0700131 *
132 * @return a temperature type: TYPE_*
Todd Poynordd055822017-05-25 17:53:21 -0700133 */
Wei Wangbad7c202018-11-01 11:57:39 -0700134 public @Type int getType() {
Todd Poynordd055822017-05-25 17:53:21 -0700135 return mType;
136 }
137
Wei Wangbad7c202018-11-01 11:57:39 -0700138 /**
139 * Return the temperature name.
140 *
141 * @return a temperature name as String.
Todd Poynordd055822017-05-25 17:53:21 -0700142 */
Wei Wangbad7c202018-11-01 11:57:39 -0700143 public String getName() {
144 return mName;
145 }
146
147 /**
148 * Return the temperature throttling status.
149 *
150 * @return a temperature throttling status: THROTTLING_*
151 */
152 public @ThrottlingStatus int getStatus() {
153 return mStatus;
154 }
Todd Poynordd055822017-05-25 17:53:21 -0700155
156 private Temperature(Parcel p) {
157 readFromParcel(p);
158 }
159
160 /**
161 * Fill in Temperature members from a Parcel.
Wei Wangbad7c202018-11-01 11:57:39 -0700162 *
Todd Poynordd055822017-05-25 17:53:21 -0700163 * @param p the parceled Temperature object.
164 */
165 public void readFromParcel(Parcel p) {
166 mValue = p.readFloat();
167 mType = p.readInt();
Wei Wangbad7c202018-11-01 11:57:39 -0700168 mName = p.readString();
169 mStatus = p.readInt();
Todd Poynordd055822017-05-25 17:53:21 -0700170 }
171
172 @Override
173 public void writeToParcel(Parcel p, int flags) {
174 p.writeFloat(mValue);
175 p.writeInt(mType);
Wei Wangbad7c202018-11-01 11:57:39 -0700176 p.writeString(mName);
177 p.writeInt(mStatus);
Todd Poynordd055822017-05-25 17:53:21 -0700178 }
179
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700180 public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR =
Todd Poynordd055822017-05-25 17:53:21 -0700181 new Parcelable.Creator<Temperature>() {
Wei Wangbad7c202018-11-01 11:57:39 -0700182 @Override
183 public Temperature createFromParcel(Parcel p) {
184 return new Temperature(p);
185 }
Todd Poynordd055822017-05-25 17:53:21 -0700186
Wei Wangbad7c202018-11-01 11:57:39 -0700187 @Override
188 public Temperature[] newArray(int size) {
189 return new Temperature[size];
190 }
191 };
Todd Poynordd055822017-05-25 17:53:21 -0700192
193 @Override
194 public int describeContents() {
195 return 0;
196 }
197}