4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <hw/thermal.h>
27 #include "../shared.h"
29 #define THERMAL_PATH "/sys/class/sec/temperature/ap_therm"
31 static struct event_data {
32 ThermalUpdated updated_cb;
38 static int thermal_get_state(struct thermal_info *info)
48 fp = fopen(THERMAL_PATH, "r");
50 _E("Failed to open %s(%d)", THERMAL_PATH, errno);
54 len = fread(buf, 1, sizeof(buf) - 1, fp);
57 _E("Failed to read %s(%d)", THERMAL_PATH, errno);
62 if (!strstr(buf, "temp:")) {
63 _E("Invalid temparature value (%s)", buf);
67 value = atoi(buf + 5); /* 5 == strlen("temp:") */
69 _I("AP temparature(%d)", value);
72 info->state = THERMAL_STATE_LOW;
73 info->level = THERMAL_LEVEL_NORMAL;
78 info->state = THERMAL_STATE_NORMAL;
79 info->level = THERMAL_LEVEL_NORMAL;
83 info->state = THERMAL_STATE_HIGH;
86 info->level = THERMAL_LEVEL_WARNING;
88 info->level = THERMAL_LEVEL_CRITICAL;
90 info->level = THERMAL_LEVEL_POWEROFF;
95 static gboolean thermal_timeout(gpointer data)
97 struct thermal_info info;
100 ret = thermal_get_state(&info);
102 _E("Failed to read thermal state (%d)", ret);
103 return G_SOURCE_CONTINUE;
106 if (edata.updated_cb)
107 edata.updated_cb(&info, edata.data);
109 return G_SOURCE_CONTINUE;
112 static int thermal_register_changed_event(ThermalUpdated updated_cb, void *data)
115 g_source_remove(timer);
117 timer = g_timeout_add(10000, thermal_timeout, NULL);
119 _E("Failed to add timer for thermal");
123 edata.updated_cb = updated_cb;
129 static int thermal_unregister_changed_event(ThermalUpdated updated_cb)
132 g_source_remove(timer);
136 edata.updated_cb = NULL;
142 static int thermal_open(struct hw_info *info,
143 const char *id, struct hw_common **common)
145 struct thermal_device *thermal_dev;
147 if (!info || !common)
150 thermal_dev = calloc(1, sizeof(struct thermal_device));
154 thermal_dev->common.info = info;
155 thermal_dev->register_changed_event
156 = thermal_register_changed_event;
157 thermal_dev->unregister_changed_event
158 = thermal_unregister_changed_event;
159 thermal_dev->get_state
162 *common = (struct hw_common *)thermal_dev;
166 static int thermal_close(struct hw_common *common)
175 HARDWARE_MODULE_STRUCTURE = {
176 .magic = HARDWARE_INFO_TAG,
177 .hal_version = HARDWARE_INFO_VERSION,
178 .device_version = THERMAL_HARDWARE_DEVICE_VERSION,
179 .id = THERMAL_HARDWARE_DEVICE_ID,
181 .open = thermal_open,
182 .close = thermal_close,