1 /*
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 
17 package android.os;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.hardware.thermal.TemperatureType;
23 import android.hardware.thermal.ThrottlingSeverity;
24 
25 import com.android.internal.util.Preconditions;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * Temperature values used by IThermalService.
32  *
33  * @hide
34  */
35 public final class Temperature implements Parcelable {
36     /** Temperature value */
37     private final float mValue;
38     /** A Temperature type from ThermalHAL */
39     private final int mType;
40     /** Name of this Temperature */
41     private final String mName;
42     /** The level of the sensor is currently in throttling */
43     private final int mStatus;
44 
45     @IntDef(prefix = { "THROTTLING_" }, value = {
46             THROTTLING_NONE,
47             THROTTLING_LIGHT,
48             THROTTLING_MODERATE,
49             THROTTLING_SEVERE,
50             THROTTLING_CRITICAL,
51             THROTTLING_EMERGENCY,
52             THROTTLING_SHUTDOWN,
53     })
54     @Retention(RetentionPolicy.SOURCE)
55     public @interface ThrottlingStatus {}
56 
57     /** Keep in sync with hardware/interfaces/thermal/aidl/android/hardware/thermal
58      * /ThrottlingSeverity.aidl */
59     public static final int THROTTLING_NONE = ThrottlingSeverity.NONE;
60     public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT;
61     public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE;
62     public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE;
63     public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL;
64     public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY;
65     public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN;
66 
67     @IntDef(prefix = { "TYPE_" }, value = {
68             TYPE_UNKNOWN,
69             TYPE_CPU,
70             TYPE_GPU,
71             TYPE_BATTERY,
72             TYPE_SKIN,
73             TYPE_USB_PORT,
74             TYPE_POWER_AMPLIFIER,
75             TYPE_BCL_VOLTAGE,
76             TYPE_BCL_CURRENT,
77             TYPE_BCL_PERCENTAGE,
78             TYPE_NPU,
79             TYPE_TPU,
80             TYPE_DISPLAY,
81             TYPE_MODEM,
82             TYPE_SOC
83     })
84     @Retention(RetentionPolicy.SOURCE)
85     public @interface Type {}
86 
87     /** Keep in sync with hardware/interfaces/thermal/aidl/android/hardware/thermal
88      * /TemperatureType.aidl */
89     public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN;
90     public static final int TYPE_CPU = TemperatureType.CPU;
91     public static final int TYPE_GPU = TemperatureType.GPU;
92     public static final int TYPE_BATTERY = TemperatureType.BATTERY;
93     public static final int TYPE_SKIN = TemperatureType.SKIN;
94     public static final int TYPE_USB_PORT = TemperatureType.USB_PORT;
95     public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER;
96     public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE;
97     public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT;
98     public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE;
99     public static final int TYPE_NPU = TemperatureType.NPU;
100     public static final int TYPE_TPU = TemperatureType.TPU;
101     public static final int TYPE_DISPLAY = TemperatureType.DISPLAY;
102     public static final int TYPE_MODEM = TemperatureType.MODEM;
103     public static final int TYPE_SOC = TemperatureType.SOC;
104 
105     /**
106      * Verify a valid Temperature type.
107      *
108      * @return true if a Temperature type is valid otherwise false.
109      */
isValidType(@ype int type)110     public static boolean isValidType(@Type int type) {
111         return type >= TYPE_UNKNOWN && type <= TYPE_SOC;
112     }
113 
114     /**
115      * Verify a valid throttling status.
116      *
117      * @return true if a status is valid otherwise false.
118      */
isValidStatus(@hrottlingStatus int status)119     public static boolean isValidStatus(@ThrottlingStatus int status) {
120         return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN;
121     }
122 
Temperature(float value, @Type int type, @NonNull String name, @ThrottlingStatus int status)123     public Temperature(float value, @Type int type,
124             @NonNull String name, @ThrottlingStatus int status) {
125         Preconditions.checkArgument(isValidType(type), "Invalid Type");
126         Preconditions.checkArgument(isValidStatus(status) , "Invalid Status");
127         mValue = value;
128         mType = type;
129         mName = Preconditions.checkStringNotEmpty(name);
130         mStatus = status;
131     }
132 
133     /**
134      * Return the Temperature value.
135      *
136      * @return a Temperature value in floating point could be NaN.
137      */
getValue()138     public float getValue() {
139         return mValue;
140     }
141 
142     /**
143      * Return the Temperature type.
144      *
145      * @return a Temperature type: TYPE_*
146      */
getType()147     public @Type int getType() {
148         return mType;
149     }
150 
151     /**
152      * Return the Temperature name.
153      *
154      * @return a Temperature name as String.
155      */
getName()156     public String getName() {
157         return mName;
158     }
159 
160     /**
161      * Return the Temperature throttling status.
162      *
163      * @return a Temperature throttling status: THROTTLING_*
164      */
getStatus()165     public @ThrottlingStatus int getStatus() {
166         return mStatus;
167     }
168 
169     @Override
toString()170     public String toString() {
171         return "Temperature{mValue=" + mValue + ", mType=" + mType
172                 + ", mName=" + mName + ", mStatus=" + mStatus + "}";
173     }
174 
175     @Override
hashCode()176     public int hashCode() {
177         int hash = mName.hashCode();
178         hash = 31 * hash + Float.hashCode(mValue);
179         hash = 31 * hash + mType;
180         hash = 31 * hash + mStatus;
181         return hash;
182     }
183 
184     @Override
equals(@ullable Object o)185     public boolean equals(@Nullable Object o) {
186         if (!(o instanceof Temperature)) {
187             return false;
188         }
189         Temperature other = (Temperature) o;
190         return other.mValue == mValue && other.mType == mType
191                 && other.mName.equals(mName) && other.mStatus == mStatus;
192     }
193 
194     @Override
writeToParcel(Parcel p, int flags)195     public void writeToParcel(Parcel p, int flags) {
196         p.writeFloat(mValue);
197         p.writeInt(mType);
198         p.writeString(mName);
199         p.writeInt(mStatus);
200     }
201 
202     public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR =
203             new Parcelable.Creator<Temperature>() {
204                 @Override
205                 public Temperature createFromParcel(Parcel p) {
206                     float value = p.readFloat();
207                     int type = p.readInt();
208                     String name = p.readString();
209                     int status = p.readInt();
210                     return new Temperature(value, type, name, status);
211                 }
212 
213                 @Override
214                 public Temperature[] newArray(int size) {
215                     return new Temperature[size];
216                 }
217 
218             };
219 
220     @Override
describeContents()221     public int describeContents() {
222         return 0;
223     }
224 }
225