1/*
2 * Copyright 2014 Intel Corporation All Rights Reserved.
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 com.intel.thermal;
18
19import android.util.Log;
20
21import java.lang.reflect.Method;
22import java.util.ArrayList;
23
24/**
25 * This class contains the cooling device specific information. It also contains
26 * a reference to the actual throttle function.
27 *
28 * @hide
29 */
30public class ThermalCoolingDevice {
31
32    private static final String TAG = "ThermalCoolingDevice";
33
34    private String mDeviceName;
35
36    private String mClassPath;
37
38    private String mThrottlePath;
39
40    private int mCurrentThermalState;
41
42    private int mDeviceId;
43
44    private Class mDeviceClass;
45
46    private Method mThrottleMethod;
47
48    /* Maintains list of zoneid's under which this cooling device falls. */
49    private ArrayList<Integer> mZoneIdList = new ArrayList<Integer>();
50
51    /* Maintains corresponding state of zone present in mZoneidList */
52    private ArrayList<Integer> mZoneStateList = new ArrayList<Integer>();
53
54    /* List of values used to throttle this cooling device */
55    private ArrayList<Integer> mThrottleValues = null;
56
57    private int mNumThrottleValues = ThermalManager.DEFAULT_NUM_THROTTLE_VALUES;
58
59    public ThermalCoolingDevice() {
60        mCurrentThermalState = -1;
61        mThrottlePath = "auto";
62        mClassPath = "auto";
63    }
64
65    public void setNumThrottleValues(int num) {
66        mNumThrottleValues = num;
67    }
68
69    public int getNumThrottleValues() {
70        return mNumThrottleValues;
71    }
72
73    public void setDeviceName(String Name) {
74        mDeviceName = Name;
75    }
76
77    public String getDeviceName() {
78        return mDeviceName;
79    }
80
81    public void setDeviceId(int deviceId) {
82        mDeviceId = deviceId;
83    }
84
85    public int getDeviceId() {
86        return mDeviceId;
87    }
88
89    public String getClassPath() {
90        return mClassPath;
91    }
92
93    public void setClassPath(String Path) {
94        mClassPath = Path;
95    }
96
97    public Class getDeviceClass() {
98        return mDeviceClass;
99    }
100
101    public void setDeviceClass(Class cls) {
102        mDeviceClass = cls;
103    }
104
105    public Method getThrottleMethod() {
106        return mThrottleMethod;
107    }
108
109    public void setThrottleMethod(Method method) {
110        mThrottleMethod = method;
111    }
112
113    public String getThrottlePath() {
114        return mThrottlePath;
115    }
116
117    public void setThrottlePath(String path) {
118        if (path.equalsIgnoreCase("auto") && !mDeviceName.equalsIgnoreCase("battery")) {
119            //construct the throttle path
120            int indx = ThermalUtils.getCoolingDeviceIndexContains(mDeviceName);
121            if (indx != -1) {
122                mThrottlePath = ThermalManager.sCoolingDeviceBasePath + indx +
123                        ThermalManager.sCoolingDeviceState;
124            } else {
125                mThrottlePath = "invalid";
126            }
127        } else {
128            mThrottlePath = path;
129        }
130    }
131
132    public ArrayList<Integer> getZoneIdList() {
133        return mZoneIdList;
134    }
135
136    public ArrayList<Integer> getZoneStateList() {
137        return mZoneStateList;
138    }
139
140    public ArrayList<Integer> getThrottleValuesList() {
141        return mThrottleValues;
142    }
143
144    public void setThrottleValuesList(ArrayList<Integer> list) {
145        mThrottleValues = list;
146        mNumThrottleValues = list.size();
147    }
148    /**
149     * Sets the current thermal state of cooling device which will be maximum of
150     * all states of zones under which this cooling device falls.
151     */
152    private void updateCurrentThermalState() {
153        int state = 0;
154        for (Integer coolingDevState : mZoneStateList) {
155            state = Math.max(state, coolingDevState);
156        }
157        mCurrentThermalState = state;
158    }
159
160    /**
161     * Adds zoneID and its thermal state to mListOfZoneIDs and
162     * mListOfTStatesOfZones array. If zoneId exists then its thermal state is
163     * updated else zoneId and its state will be added to array.
164     */
165    public void updateZoneState(int zoneId, int state) {
166        int index = -1;
167
168        if (!mZoneIdList.isEmpty()) {
169            index = mZoneIdList.indexOf(zoneId);
170        }
171
172        // Entry does not exist
173        if (index == -1) {
174            mZoneIdList.add(zoneId);
175            mZoneStateList.add(state);
176        } else {
177            mZoneStateList.set(index, state);
178        }
179
180        updateCurrentThermalState();
181    }
182
183    public int getThermalState() {
184        return mCurrentThermalState;
185    }
186
187}
188