blob: 7caffcd2ddfd35ea68b40f5466ff3d229aac7304 [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;
Wei Wang38e5bd72019-04-03 14:58:42 -070020import android.annotation.NonNull;
Wei Wangbad7c202018-11-01 11:57:39 -070021import android.hardware.thermal.V2_0.TemperatureType;
22import android.hardware.thermal.V2_0.ThrottlingSeverity;
23
Wei Wang38e5bd72019-04-03 14:58:42 -070024import com.android.internal.util.Preconditions;
25
Wei Wangbad7c202018-11-01 11:57:39 -070026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28
Todd Poynordd055822017-05-25 17:53:21 -070029/**
30 * Temperature values used by IThermalService.
Wei Wang37b17542018-11-12 11:02:09 -080031 *
Todd Poynordd055822017-05-25 17:53:21 -070032 * @hide
33 */
Wei Wangcc913612018-11-28 23:34:43 -080034public final class Temperature implements Parcelable {
Wei Wangbad7c202018-11-01 11:57:39 -070035 /** Temperature value */
Wei Wang38e5bd72019-04-03 14:58:42 -070036 private final float mValue;
37 /** A Temperature type from ThermalHAL */
38 private final int mType;
39 /** Name of this Temperature */
40 private final String mName;
Wei Wangbad7c202018-11-01 11:57:39 -070041 /** The level of the sensor is currently in throttling */
Wei Wang38e5bd72019-04-03 14:58:42 -070042 private final int mStatus;
Todd Poynordd055822017-05-25 17:53:21 -070043
Wei Wangbad7c202018-11-01 11:57:39 -070044 @IntDef(prefix = { "THROTTLING_" }, value = {
45 THROTTLING_NONE,
46 THROTTLING_LIGHT,
47 THROTTLING_MODERATE,
48 THROTTLING_SEVERE,
49 THROTTLING_CRITICAL,
Wei Wanged2c9022018-12-04 14:44:36 -080050 THROTTLING_EMERGENCY,
Wei Wangbad7c202018-11-01 11:57:39 -070051 THROTTLING_SHUTDOWN,
52 })
53 @Retention(RetentionPolicy.SOURCE)
54 public @interface ThrottlingStatus {}
55
56 /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
57 public static final int THROTTLING_NONE = ThrottlingSeverity.NONE;
58 public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT;
59 public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE;
60 public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE;
61 public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL;
Wei Wanged2c9022018-12-04 14:44:36 -080062 public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY;
Wei Wangbad7c202018-11-01 11:57:39 -070063 public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN;
64
Wei Wangbad7c202018-11-01 11:57:39 -070065 @IntDef(prefix = { "TYPE_" }, value = {
66 TYPE_UNKNOWN,
67 TYPE_CPU,
68 TYPE_GPU,
69 TYPE_BATTERY,
70 TYPE_SKIN,
71 TYPE_USB_PORT,
72 TYPE_POWER_AMPLIFIER,
73 TYPE_BCL_VOLTAGE,
74 TYPE_BCL_CURRENT,
75 TYPE_BCL_PERCENTAGE,
Wei Wangbc79c4f2018-12-10 11:40:40 -080076 TYPE_NPU,
Wei Wangbad7c202018-11-01 11:57:39 -070077 })
78 @Retention(RetentionPolicy.SOURCE)
79 public @interface Type {}
80
Wei Wang38e5bd72019-04-03 14:58:42 -070081 /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
Wei Wangbad7c202018-11-01 11:57:39 -070082 public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN;
83 public static final int TYPE_CPU = TemperatureType.CPU;
84 public static final int TYPE_GPU = TemperatureType.GPU;
85 public static final int TYPE_BATTERY = TemperatureType.BATTERY;
86 public static final int TYPE_SKIN = TemperatureType.SKIN;
87 public static final int TYPE_USB_PORT = TemperatureType.USB_PORT;
88 public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER;
89 public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE;
90 public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT;
91 public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE;
Wei Wangbc79c4f2018-12-10 11:40:40 -080092 public static final int TYPE_NPU = TemperatureType.NPU;
Wei Wangbad7c202018-11-01 11:57:39 -070093
94 /**
Wei Wang38e5bd72019-04-03 14:58:42 -070095 * Verify a valid Temperature type.
Wei Wangbad7c202018-11-01 11:57:39 -070096 *
Wei Wang38e5bd72019-04-03 14:58:42 -070097 * @return true if a Temperature type is valid otherwise false.
Wei Wangbad7c202018-11-01 11:57:39 -070098 */
Wei Wang37b17542018-11-12 11:02:09 -080099 public static boolean isValidType(@Type int type) {
Wei Wangbc79c4f2018-12-10 11:40:40 -0800100 return type >= TYPE_UNKNOWN && type <= TYPE_NPU;
Todd Poynordd055822017-05-25 17:53:21 -0700101 }
102
Wei Wang37b17542018-11-12 11:02:09 -0800103 /**
104 * Verify a valid throttling status.
105 *
106 * @return true if a status is valid otherwise false.
107 */
108 public static boolean isValidStatus(@ThrottlingStatus int status) {
109 return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN;
110 }
111
Wei Wang38e5bd72019-04-03 14:58:42 -0700112 public Temperature(float value, @Type int type,
113 @NonNull String name, @ThrottlingStatus int status) {
114 Preconditions.checkArgument(isValidType(type), "Invalid Type");
115 Preconditions.checkArgument(isValidStatus(status) , "Invalid Status");
Todd Poynordd055822017-05-25 17:53:21 -0700116 mValue = value;
Wei Wang38e5bd72019-04-03 14:58:42 -0700117 mType = type;
118 mName = Preconditions.checkStringNotEmpty(name);
119 mStatus = status;
Todd Poynordd055822017-05-25 17:53:21 -0700120 }
121
122 /**
Wei Wang38e5bd72019-04-03 14:58:42 -0700123 * Return the Temperature value.
Wei Wangbad7c202018-11-01 11:57:39 -0700124 *
Wei Wang38e5bd72019-04-03 14:58:42 -0700125 * @return a Temperature value in floating point could be NaN.
Todd Poynordd055822017-05-25 17:53:21 -0700126 */
127 public float getValue() {
128 return mValue;
129 }
130
131 /**
Wei Wang38e5bd72019-04-03 14:58:42 -0700132 * Return the Temperature type.
Wei Wangbad7c202018-11-01 11:57:39 -0700133 *
Wei Wang38e5bd72019-04-03 14:58:42 -0700134 * @return a Temperature type: TYPE_*
Todd Poynordd055822017-05-25 17:53:21 -0700135 */
Wei Wangbad7c202018-11-01 11:57:39 -0700136 public @Type int getType() {
Todd Poynordd055822017-05-25 17:53:21 -0700137 return mType;
138 }
139
Wei Wangbad7c202018-11-01 11:57:39 -0700140 /**
Wei Wang38e5bd72019-04-03 14:58:42 -0700141 * Return the Temperature name.
Wei Wangbad7c202018-11-01 11:57:39 -0700142 *
Wei Wang38e5bd72019-04-03 14:58:42 -0700143 * @return a Temperature name as String.
Todd Poynordd055822017-05-25 17:53:21 -0700144 */
Wei Wangbad7c202018-11-01 11:57:39 -0700145 public String getName() {
146 return mName;
147 }
148
149 /**
Wei Wang38e5bd72019-04-03 14:58:42 -0700150 * Return the Temperature throttling status.
Wei Wangbad7c202018-11-01 11:57:39 -0700151 *
Wei Wang38e5bd72019-04-03 14:58:42 -0700152 * @return a Temperature throttling status: THROTTLING_*
Wei Wangbad7c202018-11-01 11:57:39 -0700153 */
154 public @ThrottlingStatus int getStatus() {
155 return mStatus;
156 }
Todd Poynordd055822017-05-25 17:53:21 -0700157
Wei Wang38e5bd72019-04-03 14:58:42 -0700158 @Override
159 public String toString() {
160 return "Temperature{mValue=" + mValue + ", mType=" + mType
161 + ", mName=" + mName + ", mStatus=" + mStatus + "}";
Todd Poynordd055822017-05-25 17:53:21 -0700162 }
163
Wei Wang38e5bd72019-04-03 14:58:42 -0700164 @Override
165 public int hashCode() {
166 int hash = mName.hashCode();
167 hash = 31 * hash + Float.hashCode(mValue);
168 hash = 31 * hash + mType;
169 hash = 31 * hash + mStatus;
170 return hash;
171 }
172
173 @Override
174 public boolean equals(Object o) {
175 if (!(o instanceof Temperature)) {
176 return false;
177 }
178 Temperature other = (Temperature) o;
179 return other.mValue == mValue && other.mType == mType
180 && other.mName.equals(mName) && other.mStatus == mStatus;
Todd Poynordd055822017-05-25 17:53:21 -0700181 }
182
183 @Override
184 public void writeToParcel(Parcel p, int flags) {
185 p.writeFloat(mValue);
186 p.writeInt(mType);
Wei Wangbad7c202018-11-01 11:57:39 -0700187 p.writeString(mName);
188 p.writeInt(mStatus);
Todd Poynordd055822017-05-25 17:53:21 -0700189 }
190
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700191 public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR =
Todd Poynordd055822017-05-25 17:53:21 -0700192 new Parcelable.Creator<Temperature>() {
Wei Wangbad7c202018-11-01 11:57:39 -0700193 @Override
194 public Temperature createFromParcel(Parcel p) {
Wei Wang38e5bd72019-04-03 14:58:42 -0700195 float value = p.readFloat();
196 int type = p.readInt();
197 String name = p.readString();
198 int status = p.readInt();
199 return new Temperature(value, type, name, status);
Wei Wangbad7c202018-11-01 11:57:39 -0700200 }
Todd Poynordd055822017-05-25 17:53:21 -0700201
Wei Wangbad7c202018-11-01 11:57:39 -0700202 @Override
203 public Temperature[] newArray(int size) {
204 return new Temperature[size];
205 }
Wei Wang38e5bd72019-04-03 14:58:42 -0700206
Wei Wangbad7c202018-11-01 11:57:39 -0700207 };
Todd Poynordd055822017-05-25 17:53:21 -0700208
209 @Override
210 public int describeContents() {
211 return 0;
212 }
213}