Add lcov comments for coverage measurement
[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
296         //LCOV_EXCL_START : fallback routine
297         if (cpufreq_fp == NULL) {
298                 if (core_idx > 0) {
299                         _I("Fail to get the information about core%d. Get the core0's instead",
300                                         core_idx);
301                         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cpufreq/scaling_%s_freq",
302                                         type);
303                         cpufreq_fp = fopen(path, "r");
304                 }
305
306                 if (cpufreq_fp == NULL) {
307                         _E("IO_ERROR(0x%08x) : failed to open cpufreq file",
308                                         RUNTIME_INFO_ERROR_IO_ERROR);
309                         return RUNTIME_INFO_ERROR_IO_ERROR;
310                 }
311         }
312         //LCOV_EXCL_STOP
313
314         if (!fscanf(cpufreq_fp, "%d", &result)) {
315                 //LCOV_EXCL_START : system error
316                 _E("IO_ERROR(0x%08x) : there is no information in the cpuinfo file",
317                                 RUNTIME_INFO_ERROR_IO_ERROR);
318                 fclose(cpufreq_fp);
319                 return RUNTIME_INFO_ERROR_IO_ERROR;
320                 //LCOV_EXCL_STOP
321         }
322
323         *cpu_freq = result / 1000;
324         fclose(cpufreq_fp);
325         return RUNTIME_INFO_ERROR_NONE;
326 }
327
328 //LCOV_EXCL_START : fallback routine
329 int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq)
330 {
331         FILE *cpuinfo_fp;
332         char line[128];
333         int cur_core = 0;
334         char *start;
335         int acc_freq;
336
337         if (core_idx < 0)
338                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
339
340         if (!cpu_freq)
341                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
342
343         cpuinfo_fp = fopen("/proc/cpuinfo", "r");
344         if (cpuinfo_fp == NULL) {
345                 _E("Fail to open cpuinfo");
346                 return RUNTIME_INFO_ERROR_IO_ERROR;
347         }
348
349         while (fgets(line, sizeof(line), cpuinfo_fp) != NULL) {
350                 if (strncmp(line, "cpu MHz", 7))
351                         continue;
352
353                 if (cur_core == core_idx) {
354                         /* String format in the cpuinfo : "cpu MHz : 1234" */
355                         start = strchr(line, ':') + 2;
356                         acc_freq = 0;
357                         while (*start >= '0' && *start <= '9') {
358                                 acc_freq = (acc_freq * 10) + (*start - '0');
359                                 ++start;
360                         }
361
362                         *cpu_freq = acc_freq;
363                         fclose(cpuinfo_fp);
364                         return RUNTIME_INFO_ERROR_NONE;
365                 }
366                 ++cur_core;
367         }
368
369         fclose(cpuinfo_fp);
370         return RUNTIME_INFO_ERROR_NOT_SUPPORTED;
371 }
372 //LCOV_EXCL_STOP