Remove executable flag from non-executable files
[platform/adaptation/tw3/device-manager-plugin-tw3.git] / hw / thermal / thermal.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <glib.h>
25
26 #include <hw/thermal.h>
27 #include <hw/shared.h>
28
29 #define THERMAL_PATH "/sys/class/sec/temperature/ap_therm"
30
31 static struct event_data {
32         ThermalUpdated updated_cb;
33         void *data;
34 } edata = { 0, };
35
36 static guint timer;
37
38 static int thermal_get_info(struct thermal_info *info)
39 {
40         FILE *fp;
41         char buf[32];
42         size_t len;
43         char *ret;
44
45         if (!info)
46                 return -EINVAL;
47
48         fp = fopen(THERMAL_PATH, "r");
49         if (!fp) {
50                 _E("Failed to open %s(%d)", THERMAL_PATH, errno);
51                 return -errno;
52         }
53
54         len = fread(buf, 1, sizeof(buf) - 1, fp);
55         fclose(fp);
56         if (len == 0) {
57                 _E("Failed to read %s(%d)", THERMAL_PATH, errno);
58                 return -errno;
59         }
60         buf[len] = '\0';
61
62         ret = strstr(buf, "temp:");
63         if (!ret) {
64                 _E("Invalid temp value (%s)", buf);
65                 return -EINVAL;
66         }
67         info->temp = atoi(ret + 5); /* 5 == strlen("temp:") */
68
69         ret = strstr(buf, "adc:");
70         if (!ret) {
71                 _E("Invalid adc value (%s)", buf);
72                 return -EINVAL;
73         }
74         info->adc = atoi(ret + 4); /* 4 == strlen("adc:") */
75
76         _I("AP temp(%d) adc(%d)", info->temp, info->adc);
77
78         return 0;
79 }
80
81 static gboolean thermal_timeout(gpointer data)
82 {
83         struct thermal_info info;
84         int ret;
85
86         ret = thermal_get_info(&info);
87         if (ret < 0) {
88                 _E("Failed to read thermal state (%d)", ret);
89                 return G_SOURCE_CONTINUE;
90         }
91
92         if (edata.updated_cb)
93                 edata.updated_cb(&info, edata.data);
94
95         return G_SOURCE_CONTINUE;
96 }
97
98 static int thermal_register_changed_event(ThermalUpdated updated_cb, void *data)
99 {
100         if (timer)
101                 g_source_remove(timer);
102
103         timer = g_timeout_add(10000, thermal_timeout, NULL);
104         if (timer == 0) {
105                 _E("Failed to add timer for thermal");
106                 return -ENOENT;
107         }
108
109         edata.updated_cb = updated_cb;
110         edata.data = data;
111
112         return 0;
113 }
114
115 static int thermal_unregister_changed_event(ThermalUpdated updated_cb)
116 {
117         if (timer) {
118                 g_source_remove(timer);
119                 timer = 0;
120         }
121
122         edata.updated_cb = NULL;
123         edata.data = NULL;
124
125         return 0;
126 }
127
128 static int thermal_open(struct hw_info *info,
129                 const char *id, struct hw_common **common)
130 {
131         struct thermal_device *thermal_dev;
132
133         if (!info || !common)
134                 return -EINVAL;
135
136         thermal_dev = calloc(1, sizeof(struct thermal_device));
137         if (!thermal_dev)
138                 return -ENOMEM;
139
140         thermal_dev->common.info = info;
141         thermal_dev->register_changed_event
142                 = thermal_register_changed_event;
143         thermal_dev->unregister_changed_event
144                 = thermal_unregister_changed_event;
145         thermal_dev->get_info
146                 = thermal_get_info;
147
148         *common = (struct hw_common *)thermal_dev;
149         return 0;
150 }
151
152 static int thermal_close(struct hw_common *common)
153 {
154         if (!common)
155                 return -EINVAL;
156
157         free(common);
158         return 0;
159 }
160
161 HARDWARE_MODULE_STRUCTURE = {
162         .magic = HARDWARE_INFO_TAG,
163         .hal_version = HARDWARE_INFO_VERSION,
164         .device_version = THERMAL_HARDWARE_DEVICE_VERSION,
165         .id = THERMAL_HARDWARE_DEVICE_ID,
166         .name = "thermal",
167         .open = thermal_open,
168         .close = thermal_close,
169 };