27613055443a5937f3a502dac8abd5507a63338b
[platform/core/api/runtime-info.git] / src / runtime_info_system.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd 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
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <vconf.h>
22 #include <dlog.h>
23
24 #include <runtime_info.h>
25 #include <runtime_info_private.h>
26
27 static const char *VCONF_AUDIO_JACK = VCONFKEY_SYSMAN_EARJACK;
28 static const char *VCONF_VIBRATION_ENABLED = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
29 static const char *VCONF_ROTATION_LOCK_ENABLED = VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL;
30 static const char *VCONF_BATTERY_CHARGING = VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW;
31 static const char *VCONF_TVOUT_CONNECTED = VCONFKEY_SYSMAN_EARJACK;
32 static const char *VCONF_AUDIO_JACK_STATUS = VCONFKEY_SYSMAN_EARJACK;
33 static const char *VCONF_USB_CONNECTED = VCONFKEY_SYSMAN_USB_STATUS;
34 static const char *VCONF_CHARGER_CONNECTED = VCONFKEY_SYSMAN_CHARGER_STATUS;
35
36
37 int runtime_info_audiojack_get_value(runtime_info_value_h value)
38 {
39         int vconf_value;
40         int ret;
41
42         ret = runtime_info_vconf_get_value_int(VCONF_AUDIO_JACK, &vconf_value);
43         if (ret != RUNTIME_INFO_ERROR_NONE)
44                 return ret;
45
46         switch (vconf_value) {
47         case VCONFKEY_SYSMAN_EARJACK_3WIRE:
48         case VCONFKEY_SYSMAN_EARJACK_4WIRE:
49                 value->b = true;
50                 break;
51
52         default:
53                 value->b = false;
54                 break;
55         }
56
57         return ret;
58 }
59
60 int runtime_info_audiojack_set_event_cb(void)
61 {
62         return runtime_info_vconf_set_event_cb(VCONF_AUDIO_JACK, RUNTIME_INFO_KEY_AUDIO_JACK_CONNECTED, 0);
63 }
64
65 void runtime_info_audiojack_unset_event_cb(void)
66 {
67         runtime_info_vconf_unset_event_cb(VCONF_AUDIO_JACK, 0);
68 }
69
70 int runtime_info_vibration_enabled_get_value(runtime_info_value_h value)
71 {
72         int vconf_value;
73         int ret;
74
75         ret = runtime_info_vconf_get_value_bool(VCONF_VIBRATION_ENABLED, &vconf_value);
76         if (ret == RUNTIME_INFO_ERROR_NONE)
77                 value->b = (bool)vconf_value;
78
79         return ret;
80 }
81
82 int runtime_info_vibration_enabled_set_event_cb(void)
83 {
84         return runtime_info_vconf_set_event_cb(VCONF_VIBRATION_ENABLED, RUNTIME_INFO_KEY_VIBRATION_ENABLED, 0);
85 }
86
87 void runtime_info_vibration_enabled_unset_event_cb(void)
88 {
89         runtime_info_vconf_unset_event_cb(VCONF_VIBRATION_ENABLED, 0);
90 }
91
92 int runtime_info_auto_rotation_enabled_get_value(runtime_info_value_h value)
93 {
94         int vconf_value;
95         int ret;
96
97         ret = runtime_info_vconf_get_value_bool(VCONF_ROTATION_LOCK_ENABLED, &vconf_value);
98         if (ret == RUNTIME_INFO_ERROR_NONE)
99                 value->b = (bool)vconf_value;
100
101         return ret;
102 }
103
104 int runtime_info_auto_rotation_enabled_set_event_cb(void)
105 {
106         return runtime_info_vconf_set_event_cb(VCONF_ROTATION_LOCK_ENABLED, RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, 0);
107 }
108
109 void runtime_info_auto_rotation_enabled_unset_event_cb(void)
110 {
111         runtime_info_vconf_unset_event_cb(VCONF_ROTATION_LOCK_ENABLED, 0);
112 }
113
114 int runtime_info_battery_charging_get_value(runtime_info_value_h value)
115 {
116         int vconf_value;
117         int ret;
118
119         ret = runtime_info_vconf_get_value_int(VCONF_BATTERY_CHARGING, &vconf_value);
120         if (ret == RUNTIME_INFO_ERROR_NONE)
121                 value->b = vconf_value;
122
123         return ret;
124 }
125
126 int runtime_info_battery_charging_set_event_cb(void)
127 {
128         return runtime_info_vconf_set_event_cb(VCONF_BATTERY_CHARGING, RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, 0);
129 }
130
131 void runtime_info_battery_charging_unset_event_cb(void)
132 {
133         runtime_info_vconf_unset_event_cb(VCONF_BATTERY_CHARGING, 0);
134 }
135
136
137 int runtime_info_tvout_connected_get_value(runtime_info_value_h value)
138 {
139         int vconf_value;
140         int ret;
141
142         ret = runtime_info_vconf_get_value_int(VCONF_TVOUT_CONNECTED, &vconf_value);
143         if (ret != RUNTIME_INFO_ERROR_NONE)
144                 return ret;
145
146         switch (vconf_value) {
147         case VCONFKEY_SYSMAN_EARJACK_TVOUT:
148                 value->b = true;
149                 break;
150
151         default:
152                 value->b = false;
153                 break;
154         }
155
156         return ret;
157 }
158
159 int runtime_info_tvout_connected_set_event_cb(void)
160 {
161         return runtime_info_vconf_set_event_cb(VCONF_TVOUT_CONNECTED, RUNTIME_INFO_KEY_TV_OUT_CONNECTED, 1);
162 }
163
164 void runtime_info_tvout_connected_unset_event_cb(void)
165 {
166         runtime_info_vconf_unset_event_cb(VCONF_TVOUT_CONNECTED, 1);
167 }
168
169
170 int runtime_info_audio_jack_status_get_value(runtime_info_value_h value)
171 {
172         int vconf_value;
173         int ret;
174
175         ret = runtime_info_vconf_get_value_int(VCONF_AUDIO_JACK_STATUS, &vconf_value);
176         if (ret != RUNTIME_INFO_ERROR_NONE)
177                 return ret;
178
179         switch (vconf_value) {
180         case VCONFKEY_SYSMAN_EARJACK_3WIRE:
181                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE;
182                 break;
183
184         case VCONFKEY_SYSMAN_EARJACK_4WIRE:
185                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE;
186                 break;
187
188         default:
189                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED;
190                 break;
191         }
192
193         return ret;
194 }
195
196 int runtime_info_audio_jack_status_set_event_cb(void)
197 {
198         return runtime_info_vconf_set_event_cb(VCONF_AUDIO_JACK_STATUS, RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, 2);
199 }
200
201 void runtime_info_audio_jack_status_unset_event_cb(void)
202 {
203         runtime_info_vconf_unset_event_cb(VCONF_AUDIO_JACK_STATUS, 2);
204 }
205
206 int runtime_info_usb_connected_get_value(runtime_info_value_h value)
207 {
208         int vconf_value;
209         int ret;
210
211         ret = runtime_info_vconf_get_value_int(VCONF_USB_CONNECTED, &vconf_value);
212         if (ret != RUNTIME_INFO_ERROR_NONE)
213                 return ret;
214
215         switch (vconf_value) {
216         case VCONFKEY_SYSMAN_USB_DISCONNECTED:
217                 value->b = false;
218                 break;
219
220         case VCONFKEY_SYSMAN_USB_CONNECTED:
221                 value->b = false;
222                 break;
223
224         case VCONFKEY_SYSMAN_USB_AVAILABLE:
225                 value->b = true;
226                 break;
227
228         default:
229                 return RUNTIME_INFO_ERROR_IO_ERROR;
230         }
231
232         return ret;
233 }
234
235 int runtime_info_usb_connected_set_event_cb(void)
236 {
237         return runtime_info_vconf_set_event_cb(VCONF_USB_CONNECTED, RUNTIME_INFO_KEY_USB_CONNECTED, 0);
238 }
239
240 void runtime_info_usb_connected_unset_event_cb(void)
241 {
242         runtime_info_vconf_unset_event_cb(VCONF_USB_CONNECTED, 0);
243 }
244
245 int runtime_info_charger_connected_get_value(runtime_info_value_h value)
246 {
247         int vconf_value;
248         int ret;
249
250         ret = runtime_info_vconf_get_value_int(VCONF_CHARGER_CONNECTED, &vconf_value);
251         if (ret != RUNTIME_INFO_ERROR_NONE)
252                 return ret;
253
254         switch (vconf_value) {
255         case VCONFKEY_SYSMAN_CHARGER_DISCONNECTED:
256                 value->b = false;
257                 break;
258
259         case VCONFKEY_SYSMAN_CHARGER_CONNECTED:
260                 value->b = true;
261                 break;
262
263         default:
264                 return RUNTIME_INFO_ERROR_IO_ERROR;
265         }
266
267         return ret;
268 }
269
270 int runtime_info_charger_connected_set_event_cb(void)
271 {
272         return runtime_info_vconf_set_event_cb(VCONF_CHARGER_CONNECTED, RUNTIME_INFO_KEY_CHARGER_CONNECTED, 0);
273 }
274
275 void runtime_info_charger_connected_unset_event_cb(void)
276 {
277         runtime_info_vconf_unset_event_cb(VCONF_CHARGER_CONNECTED, 0);
278 }
279
280 int runtime_info_get_frequency_cpufreq(int core_idx, char *type, int *cpu_freq)
281 {
282         char path[256];
283         FILE *cpufreq_fp;
284         int result;
285
286         if (core_idx < 0)
287                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
288
289         if (!type || !cpu_freq)
290                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
291
292         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
293                         core_idx, type);
294         cpufreq_fp = fopen(path, "r");
295         if (cpufreq_fp == NULL) {
296                 if (core_idx > 0) {
297                         _I("Fail to get the information about core%d. Get the core0's instead",
298                                         core_idx);
299                         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cpufreq/scaling_%s_freq",
300                                         type);
301                         cpufreq_fp = fopen(path, "r");
302                 }
303
304                 if (cpufreq_fp == NULL) {
305                         _E("IO_ERROR(0x%08x) : failed to open cpufreq file",
306                                         RUNTIME_INFO_ERROR_IO_ERROR);
307                         return RUNTIME_INFO_ERROR_IO_ERROR;
308                 }
309         }
310
311         if (!fscanf(cpufreq_fp, "%d", &result)) {
312                 _E("IO_ERROR(0x%08x) : there is no information in the cpuinfo file",
313                                 RUNTIME_INFO_ERROR_IO_ERROR);
314                 fclose(cpufreq_fp);
315                 return RUNTIME_INFO_ERROR_IO_ERROR;
316         }
317
318         *cpu_freq = result / 1000;
319         fclose(cpufreq_fp);
320         return RUNTIME_INFO_ERROR_NONE;
321 }
322
323 int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq)
324 {
325         FILE *cpuinfo_fp;
326         char line[128];
327         int cur_core = 0;
328         char *start;
329         int acc_freq;
330
331         if (core_idx < 0)
332                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
333
334         if (!cpu_freq)
335                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
336
337         cpuinfo_fp = fopen("/proc/cpuinfo", "r");
338         if (cpuinfo_fp == NULL) {
339                 _E("Fail to open cpuinfo");
340                 return RUNTIME_INFO_ERROR_IO_ERROR;
341         }
342
343         while (fgets(line, sizeof(line), cpuinfo_fp) != NULL) {
344                 if (strncmp(line, "cpu MHz", 7))
345                         continue;
346
347                 if (cur_core == core_idx) {
348                         /* String format in the cpuinfo : "cpu MHz : 1234" */
349                         start = strchr(line, ':') + 2;
350                         acc_freq = 0;
351                         while (*start >= '0' && *start <= '9') {
352                                 acc_freq = (acc_freq * 10) + (*start - '0');
353                                 ++start;
354                         }
355
356                         *cpu_freq = acc_freq;
357                         fclose(cpuinfo_fp);
358                         return RUNTIME_INFO_ERROR_NONE;
359                 }
360                 ++cur_core;
361         }
362
363         fclose(cpuinfo_fp);
364         return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
365 }