Support gpu, gem, swap memory usage information
[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_HDMI;
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         RETURN_ERROR_IF_NOT_SUPPORTED("http://tizen.org/feature/feedback.vibration");
76
77         ret = runtime_info_vconf_get_value_bool(VCONF_VIBRATION_ENABLED, &vconf_value);
78         if (ret == RUNTIME_INFO_ERROR_NONE)
79                 value->b = (bool)vconf_value;
80
81         return ret;
82 }
83
84 int runtime_info_vibration_enabled_set_event_cb(void)
85 {
86         return runtime_info_vconf_set_event_cb(VCONF_VIBRATION_ENABLED, RUNTIME_INFO_KEY_VIBRATION_ENABLED, 0);
87 }
88
89 void runtime_info_vibration_enabled_unset_event_cb(void)
90 {
91         runtime_info_vconf_unset_event_cb(VCONF_VIBRATION_ENABLED, 0);
92 }
93
94 int runtime_info_auto_rotation_enabled_get_value(runtime_info_value_h value)
95 {
96         int vconf_value;
97         int ret;
98
99         RETURN_ERROR_IF_NOT_SUPPORTED("http://tizen.org/feature/screen.auto_rotation");
100
101         ret = runtime_info_vconf_get_value_bool(VCONF_ROTATION_LOCK_ENABLED, &vconf_value);
102         if (ret == RUNTIME_INFO_ERROR_NONE)
103                 value->b = (bool)vconf_value;
104
105         return ret;
106 }
107
108 int runtime_info_auto_rotation_enabled_set_event_cb(void)
109 {
110         return runtime_info_vconf_set_event_cb(VCONF_ROTATION_LOCK_ENABLED, RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, 0);
111 }
112
113 void runtime_info_auto_rotation_enabled_unset_event_cb(void)
114 {
115         runtime_info_vconf_unset_event_cb(VCONF_ROTATION_LOCK_ENABLED, 0);
116 }
117
118 int runtime_info_battery_charging_get_value(runtime_info_value_h value)
119 {
120         int vconf_value;
121         int ret;
122
123         RETURN_ERROR_IF_NOT_SUPPORTED("http://tizen.org/feature/battery");
124
125         ret = runtime_info_vconf_get_value_int(VCONF_BATTERY_CHARGING, &vconf_value);
126         if (ret == RUNTIME_INFO_ERROR_NONE)
127                 value->b = vconf_value;
128
129         return ret;
130 }
131
132 int runtime_info_battery_charging_set_event_cb(void)
133 {
134         return runtime_info_vconf_set_event_cb(VCONF_BATTERY_CHARGING, RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, 0);
135 }
136
137 void runtime_info_battery_charging_unset_event_cb(void)
138 {
139         runtime_info_vconf_unset_event_cb(VCONF_BATTERY_CHARGING, 0);
140 }
141
142
143 int runtime_info_tvout_connected_get_value(runtime_info_value_h value)
144 {
145         int vconf_value;
146         int ret;
147
148         bool supported;
149         ret = system_info_get_platform_bool("http://tizen.org/feature/screen.output.hdmi", &supported);
150         if (ret != SYSTEM_INFO_ERROR_NONE)
151                 return RUNTIME_INFO_ERROR_IO_ERROR;
152         if (!supported) {
153                 ret = system_info_get_platform_bool("http://tizen.org/feature/screen.output.rca", &supported);
154                 if (ret != SYSTEM_INFO_ERROR_NONE)
155                         return RUNTIME_INFO_ERROR_IO_ERROR;
156                 if (!supported)
157                         return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
158         }
159
160         ret = runtime_info_vconf_get_value_int(VCONF_TVOUT_CONNECTED, &vconf_value);
161         if (ret == RUNTIME_INFO_ERROR_NONE)
162                 value->b = vconf_value;
163
164         return ret;
165 }
166
167 int runtime_info_tvout_connected_set_event_cb(void)
168 {
169         return runtime_info_vconf_set_event_cb(VCONF_TVOUT_CONNECTED, RUNTIME_INFO_KEY_TV_OUT_CONNECTED, 0);
170 }
171
172 void runtime_info_tvout_connected_unset_event_cb(void)
173 {
174         runtime_info_vconf_unset_event_cb(VCONF_TVOUT_CONNECTED, 0);
175 }
176
177
178 int runtime_info_audio_jack_status_get_value(runtime_info_value_h value)
179 {
180         int vconf_value;
181         int ret;
182
183         ret = runtime_info_vconf_get_value_int(VCONF_AUDIO_JACK_STATUS, &vconf_value);
184         if (ret != RUNTIME_INFO_ERROR_NONE)
185                 return ret;
186
187         switch (vconf_value) {
188         case VCONFKEY_SYSMAN_EARJACK_3WIRE:
189                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE;
190                 break;
191
192         case VCONFKEY_SYSMAN_EARJACK_4WIRE:
193                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE;
194                 break;
195
196         default:
197                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED;
198                 break;
199         }
200
201         return ret;
202 }
203
204 int runtime_info_audio_jack_status_set_event_cb(void)
205 {
206         return runtime_info_vconf_set_event_cb(VCONF_AUDIO_JACK_STATUS, RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, 2);
207 }
208
209 void runtime_info_audio_jack_status_unset_event_cb(void)
210 {
211         runtime_info_vconf_unset_event_cb(VCONF_AUDIO_JACK_STATUS, 2);
212 }
213
214 int runtime_info_usb_connected_get_value(runtime_info_value_h value)
215 {
216         int vconf_value;
217         int ret;
218
219         ret = runtime_info_vconf_get_value_int(VCONF_USB_CONNECTED, &vconf_value);
220         if (ret != RUNTIME_INFO_ERROR_NONE)
221                 return ret;
222
223         switch (vconf_value) {
224         case VCONFKEY_SYSMAN_USB_DISCONNECTED:
225                 value->b = false;
226                 break;
227
228         case VCONFKEY_SYSMAN_USB_CONNECTED:
229                 value->b = false;
230                 break;
231
232         case VCONFKEY_SYSMAN_USB_AVAILABLE:
233                 value->b = true;
234                 break;
235
236         default:
237                 return RUNTIME_INFO_ERROR_IO_ERROR;
238         }
239
240         return ret;
241 }
242
243 int runtime_info_usb_connected_set_event_cb(void)
244 {
245         return runtime_info_vconf_set_event_cb(VCONF_USB_CONNECTED, RUNTIME_INFO_KEY_USB_CONNECTED, 0);
246 }
247
248 void runtime_info_usb_connected_unset_event_cb(void)
249 {
250         runtime_info_vconf_unset_event_cb(VCONF_USB_CONNECTED, 0);
251 }
252
253 int runtime_info_charger_connected_get_value(runtime_info_value_h value)
254 {
255         int vconf_value;
256         int ret;
257
258         RETURN_ERROR_IF_NOT_SUPPORTED("http://tizen.org/feature/battery");
259
260         ret = runtime_info_vconf_get_value_int(VCONF_CHARGER_CONNECTED, &vconf_value);
261         if (ret != RUNTIME_INFO_ERROR_NONE)
262                 return ret;
263
264         switch (vconf_value) {
265         case VCONFKEY_SYSMAN_CHARGER_DISCONNECTED:
266                 value->b = false;
267                 break;
268
269         case VCONFKEY_SYSMAN_CHARGER_CONNECTED:
270                 value->b = true;
271                 break;
272
273         default:
274                 return RUNTIME_INFO_ERROR_IO_ERROR;
275         }
276
277         return ret;
278 }
279
280 int runtime_info_charger_connected_set_event_cb(void)
281 {
282         return runtime_info_vconf_set_event_cb(VCONF_CHARGER_CONNECTED, RUNTIME_INFO_KEY_CHARGER_CONNECTED, 0);
283 }
284
285 void runtime_info_charger_connected_unset_event_cb(void)
286 {
287         runtime_info_vconf_unset_event_cb(VCONF_CHARGER_CONNECTED, 0);
288 }
289
290 int runtime_info_get_frequency_cpufreq(int core_idx, char *type, int *cpu_freq)
291 {
292         char path[256];
293         FILE *cpufreq_fp;
294         int result;
295
296         if (core_idx < 0)
297                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
298
299         if (!type || !cpu_freq)
300                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
301
302         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
303                         core_idx, type);
304         cpufreq_fp = fopen(path, "r");
305
306         //LCOV_EXCL_START : fallback routine
307         if (cpufreq_fp == NULL) {
308                 if (core_idx > 0) {
309                         _I("Fail to get the information about core%d. Get the core0's instead",
310                                         core_idx);
311                         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cpufreq/scaling_%s_freq",
312                                         type);
313                         cpufreq_fp = fopen(path, "r");
314                 }
315
316                 if (cpufreq_fp == NULL) {
317                         _E("IO_ERROR(0x%08x) : failed to open cpufreq file",
318                                         RUNTIME_INFO_ERROR_IO_ERROR);
319                         return RUNTIME_INFO_ERROR_IO_ERROR;
320                 }
321         }
322         //LCOV_EXCL_STOP
323
324         if (!fscanf(cpufreq_fp, "%d", &result)) {
325                 //LCOV_EXCL_START : system error
326                 _E("IO_ERROR(0x%08x) : there is no information in the cpuinfo file",
327                                 RUNTIME_INFO_ERROR_IO_ERROR);
328                 fclose(cpufreq_fp);
329                 return RUNTIME_INFO_ERROR_IO_ERROR;
330                 //LCOV_EXCL_STOP
331         }
332
333         *cpu_freq = result / 1000;
334         fclose(cpufreq_fp);
335         return RUNTIME_INFO_ERROR_NONE;
336 }
337
338 //LCOV_EXCL_START : fallback routine
339 int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq)
340 {
341         FILE *cpuinfo_fp;
342         char line[128];
343         int cur_core = 0;
344         char *start;
345         int acc_freq;
346
347         if (core_idx < 0)
348                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
349
350         if (!cpu_freq)
351                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
352
353         cpuinfo_fp = fopen("/proc/cpuinfo", "r");
354         if (cpuinfo_fp == NULL) {
355                 _E("Fail to open cpuinfo");
356                 return RUNTIME_INFO_ERROR_IO_ERROR;
357         }
358
359         while (fgets(line, sizeof(line), cpuinfo_fp) != NULL) {
360                 if (strncmp(line, "cpu MHz", 7))
361                         continue;
362
363                 if (cur_core == core_idx) {
364                         /* String format in the cpuinfo : "cpu MHz : 1234" */
365                         start = strchr(line, ':') + 2;
366                         acc_freq = 0;
367                         while (*start >= '0' && *start <= '9') {
368                                 acc_freq = (acc_freq * 10) + (*start - '0');
369                                 ++start;
370                         }
371
372                         *cpu_freq = acc_freq;
373                         fclose(cpuinfo_fp);
374                         return RUNTIME_INFO_ERROR_NONE;
375                 }
376                 ++cur_core;
377         }
378
379         fclose(cpuinfo_fp);
380         return RUNTIME_INFO_ERROR_NO_DATA;
381 }
382 //LCOV_EXCL_STOP