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.content.Context;
20import android.os.SystemProperties;
21import android.util.Log;
22
23import java.io.IOException;
24import java.io.File;
25
26/* importing static variables */
27import static com.intel.thermal.ThermalManager.*;
28
29/**
30 * The ThermalUtils class contains all common utility functionality
31 * implementations
32 *
33 * @hide
34 */
35public class ThermalUtils {
36    private static final String TAG = "ThermalUtils";
37
38    /* Native methods to access Sysfs Interfaces */
39    private native static String native_readSysfs(String path);
40    private native static int native_readSysfsTemp(String path);
41    private native static int native_writeSysfs(String path, int val);
42    private native static int native_getThermalZoneIndex(String name);
43    private native static int native_getThermalZoneIndexContains(String name);
44    private native static int native_getCoolingDeviceIndex(String name);
45    private native static int native_getCoolingDeviceIndexContains(String name);
46    private native static boolean native_isFileExists(String name);
47
48    // native methods to access kernel sysfs layer
49    public static String readSysfs(String path) {
50        try {
51            return native_readSysfs(path);
52        } catch (UnsatisfiedLinkError e) {
53            Log.w(TAG, "caught UnsatisfiedLinkError in readSysfs");
54            return null;
55        }
56    }
57
58    public static int readSysfsTemp(String path) {
59        try {
60            return native_readSysfsTemp(path);
61        } catch (UnsatisfiedLinkError e) {
62            Log.i(TAG, "caught UnsatisfiedLinkError in readSysfsTemp");
63            return INVALID_TEMP;
64        }
65    }
66
67    public static int writeSysfs(String path, int val) {
68        try {
69            return native_writeSysfs(path, val);
70        } catch (UnsatisfiedLinkError e) {
71            Log.w(TAG, "caught UnsatisfiedLinkError in writeSysfs");
72            return -1;
73        }
74    }
75
76    public static int getThermalZoneIndex(String name) {
77        try {
78            return native_getThermalZoneIndex(name);
79        } catch (UnsatisfiedLinkError e) {
80            Log.w(TAG, "caught UnsatisfiedLinkError in getThermalZoneIndex");
81            return -1;
82        }
83    }
84
85    public static int getThermalZoneIndexContains(String name) {
86        try {
87            return native_getThermalZoneIndexContains(name);
88        } catch (UnsatisfiedLinkError e) {
89            Log.w(TAG, "caught UnsatisfiedLinkError in getThermalZoneIndexContains");
90            return -1;
91        }
92    }
93
94    public static int getCoolingDeviceIndex(String name) {
95        try {
96            return native_getCoolingDeviceIndex(name);
97        } catch (UnsatisfiedLinkError e) {
98            Log.w(TAG, "caught UnsatisfiedLinkError in getCoolingDeviceIndex");
99            return -1;
100        }
101    }
102
103    public static int getCoolingDeviceIndexContains(String name) {
104        try {
105            return native_getCoolingDeviceIndexContains(name);
106        } catch (UnsatisfiedLinkError e) {
107            Log.w(TAG, "caught UnsatisfiedLinkError in getCoolingDeviceIndexContains");
108            return -1;
109        }
110    }
111
112    public static boolean isFileExists(String path) {
113        try {
114            return native_isFileExists(path);
115        } catch (UnsatisfiedLinkError e) {
116            Log.w(TAG, "caught UnsatisfiedLinkError in isFileExists");
117            return false;
118        }
119    }
120
121
122    public static int calculateThermalState(int temp, Integer thresholds[]) {
123        if (thresholds == null) return THERMAL_STATE_OFF;
124        // Return OFF state if temperature less than starting of thresholds
125        if (temp < thresholds[0])
126            return THERMAL_STATE_OFF;
127
128        if (temp >= thresholds[thresholds.length - 2])
129            return (thresholds.length - 2);
130
131        for (int i = 0; i < thresholds.length - 1; i++) {
132            if (temp >= thresholds[i] && temp < thresholds[i + 1]) {
133                return i;
134            }
135        }
136
137        // should never come here
138        return THERMAL_STATE_OFF;
139    }
140
141    public static int getLowerThresholdTemp(int index, Integer thresholds[]) {
142        if (thresholds == null) return INVALID_TEMP;
143        if (index < 0 || index >= thresholds.length)
144            return INVALID_TEMP;
145        return thresholds[index];
146    }
147
148    public static int getUpperThresholdTemp(int index, Integer thresholds[]) {
149        if (thresholds == null) return INVALID_TEMP;
150        if (index < 0 || index >= thresholds.length)
151            return INVALID_TEMP;
152        return thresholds[index + 1];
153    }
154
155    public static void getTjMax() {
156        ThermalManager.sTjMaxTemp = readSysfsTemp(ThermalManager.TJMAX_PATH);
157        if (ThermalManager.sTjMaxTemp <= ThermalManager.ABS_ZERO) {
158            Log.i(TAG, "TjMax temp read failed, Default TjMax value =" +
159                    ThermalManager.sTjMaxTemp);
160            ThermalManager.sTjMaxTemp = ThermalManager.sDefaultTjMax;
161        }
162    }
163
164    public static void initialiseConfigFiles(Context context) {
165        if ("1".equals(SystemProperties.get("persist.thermal.debug.xml", "0"))) {
166            if (isFileExists(DEBUG_DIR_PATH + SENSOR_FILE_NAME) &&
167                    isFileExists(DEBUG_DIR_PATH + THROTTLE_FILE_NAME)) {
168                sSensorFilePath = DEBUG_DIR_PATH + SENSOR_FILE_NAME;
169                sThrottleFilePath = DEBUG_DIR_PATH + THROTTLE_FILE_NAME;
170                Log.i(TAG, "Reading thermal config files from /data/");
171                sIsConfigFiles = true;
172            } else {
173                Log.i(TAG, "systemProperty set to read config files from /data/, but files absent");
174            }
175        } else if (isFileExists(DEFAULT_DIR_PATH + SENSOR_FILE_NAME) &&
176                isFileExists(DEFAULT_DIR_PATH + THROTTLE_FILE_NAME)) {
177            sSensorFilePath = DEFAULT_DIR_PATH + SENSOR_FILE_NAME;
178            sThrottleFilePath = DEFAULT_DIR_PATH + THROTTLE_FILE_NAME;
179            Log.i(TAG, "Reading thermal config files from /system/etc/");
180            sIsConfigFiles = true;
181        } else {
182            sSensorFileXmlId = context.getResources().
183                getIdentifier("thermal_sensor_config", "xml", "com.intel.thermal");
184            sThrottleFileXmlId = context.getResources().
185                getIdentifier("thermal_throttle_config", "xml", "com.intel.thermal");
186            if (sSensorFileXmlId != 0 && sThrottleFileXmlId != 0) {
187                Log.i(TAG, "Reading thermal config files from overlays");
188                sIsOverlays = true;
189            } else {
190                Log.i(TAG, "Unable to retrieve config files from overlays");
191            }
192        }
193    }
194}
195