Fix the vconfkey related with TV out
[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         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                 value->b = vconf_value;
145
146         return ret;
147 }
148
149 int runtime_info_tvout_connected_set_event_cb(void)
150 {
151         return runtime_info_vconf_set_event_cb(VCONF_TVOUT_CONNECTED, RUNTIME_INFO_KEY_TV_OUT_CONNECTED, 0);
152 }
153
154 void runtime_info_tvout_connected_unset_event_cb(void)
155 {
156         runtime_info_vconf_unset_event_cb(VCONF_TVOUT_CONNECTED, 0);
157 }
158
159
160 int runtime_info_audio_jack_status_get_value(runtime_info_value_h value)
161 {
162         int vconf_value;
163         int ret;
164
165         ret = runtime_info_vconf_get_value_int(VCONF_AUDIO_JACK_STATUS, &vconf_value);
166         if (ret != RUNTIME_INFO_ERROR_NONE)
167                 return ret;
168
169         switch (vconf_value) {
170         case VCONFKEY_SYSMAN_EARJACK_3WIRE:
171                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE;
172                 break;
173
174         case VCONFKEY_SYSMAN_EARJACK_4WIRE:
175                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE;
176                 break;
177
178         default:
179                 value->i = RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED;
180                 break;
181         }
182
183         return ret;
184 }
185
186 int runtime_info_audio_jack_status_set_event_cb(void)
187 {
188         return runtime_info_vconf_set_event_cb(VCONF_AUDIO_JACK_STATUS, RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, 2);
189 }
190
191 void runtime_info_audio_jack_status_unset_event_cb(void)
192 {
193         runtime_info_vconf_unset_event_cb(VCONF_AUDIO_JACK_STATUS, 2);
194 }
195
196 int runtime_info_usb_connected_get_value(runtime_info_value_h value)
197 {
198         int vconf_value;
199         int ret;
200
201         ret = runtime_info_vconf_get_value_int(VCONF_USB_CONNECTED, &vconf_value);
202         if (ret != RUNTIME_INFO_ERROR_NONE)
203                 return ret;
204
205         switch (vconf_value) {
206         case VCONFKEY_SYSMAN_USB_DISCONNECTED:
207                 value->b = false;
208                 break;
209
210         case VCONFKEY_SYSMAN_USB_CONNECTED:
211                 value->b = false;
212                 break;
213
214         case VCONFKEY_SYSMAN_USB_AVAILABLE:
215                 value->b = true;
216                 break;
217
218         default:
219                 return RUNTIME_INFO_ERROR_IO_ERROR;
220         }
221
222         return ret;
223 }
224
225 int runtime_info_usb_connected_set_event_cb(void)
226 {
227         return runtime_info_vconf_set_event_cb(VCONF_USB_CONNECTED, RUNTIME_INFO_KEY_USB_CONNECTED, 0);
228 }
229
230 void runtime_info_usb_connected_unset_event_cb(void)
231 {
232         runtime_info_vconf_unset_event_cb(VCONF_USB_CONNECTED, 0);
233 }
234
235 int runtime_info_charger_connected_get_value(runtime_info_value_h value)
236 {
237         int vconf_value;
238         int ret;
239
240         ret = runtime_info_vconf_get_value_int(VCONF_CHARGER_CONNECTED, &vconf_value);
241         if (ret != RUNTIME_INFO_ERROR_NONE)
242                 return ret;
243
244         switch (vconf_value) {
245         case VCONFKEY_SYSMAN_CHARGER_DISCONNECTED:
246                 value->b = false;
247                 break;
248
249         case VCONFKEY_SYSMAN_CHARGER_CONNECTED:
250                 value->b = true;
251                 break;
252
253         default:
254                 return RUNTIME_INFO_ERROR_IO_ERROR;
255         }
256
257         return ret;
258 }
259
260 int runtime_info_charger_connected_set_event_cb(void)
261 {
262         return runtime_info_vconf_set_event_cb(VCONF_CHARGER_CONNECTED, RUNTIME_INFO_KEY_CHARGER_CONNECTED, 0);
263 }
264
265 void runtime_info_charger_connected_unset_event_cb(void)
266 {
267         runtime_info_vconf_unset_event_cb(VCONF_CHARGER_CONNECTED, 0);
268 }
269
270 int runtime_info_get_frequency_cpufreq(int core_idx, char *type, int *cpu_freq)
271 {
272         char path[256];
273         FILE *cpufreq_fp;
274         int result;
275
276         if (core_idx < 0)
277                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
278
279         if (!type || !cpu_freq)
280                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
281
282         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
283                         core_idx, type);
284         cpufreq_fp = fopen(path, "r");
285
286         //LCOV_EXCL_START : fallback routine
287         if (cpufreq_fp == NULL) {
288                 if (core_idx > 0) {
289                         _I("Fail to get the information about core%d. Get the core0's instead",
290                                         core_idx);
291                         snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cpufreq/scaling_%s_freq",
292                                         type);
293                         cpufreq_fp = fopen(path, "r");
294                 }
295
296                 if (cpufreq_fp == NULL) {
297                         _E("IO_ERROR(0x%08x) : failed to open cpufreq file",
298                                         RUNTIME_INFO_ERROR_IO_ERROR);
299                         return RUNTIME_INFO_ERROR_IO_ERROR;
300                 }
301         }
302         //LCOV_EXCL_STOP
303
304         if (!fscanf(cpufreq_fp, "%d", &result)) {
305                 //LCOV_EXCL_START : system error
306                 _E("IO_ERROR(0x%08x) : there is no information in the cpuinfo file",
307                                 RUNTIME_INFO_ERROR_IO_ERROR);
308                 fclose(cpufreq_fp);
309                 return RUNTIME_INFO_ERROR_IO_ERROR;
310                 //LCOV_EXCL_STOP
311         }
312
313         *cpu_freq = result / 1000;
314         fclose(cpufreq_fp);
315         return RUNTIME_INFO_ERROR_NONE;
316 }
317
318 //LCOV_EXCL_START : fallback routine
319 int runtime_info_get_frequency_cpuinfo(int core_idx, int *cpu_freq)
320 {
321         FILE *cpuinfo_fp;
322         char line[128];
323         int cur_core = 0;
324         char *start;
325         int acc_freq;
326
327         if (core_idx < 0)
328                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
329
330         if (!cpu_freq)
331                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
332
333         cpuinfo_fp = fopen("/proc/cpuinfo", "r");
334         if (cpuinfo_fp == NULL) {
335                 _E("Fail to open cpuinfo");
336                 return RUNTIME_INFO_ERROR_IO_ERROR;
337         }
338
339         while (fgets(line, sizeof(line), cpuinfo_fp) != NULL) {
340                 if (strncmp(line, "cpu MHz", 7))
341                         continue;
342
343                 if (cur_core == core_idx) {
344                         /* String format in the cpuinfo : "cpu MHz : 1234" */
345                         start = strchr(line, ':') + 2;
346                         acc_freq = 0;
347                         while (*start >= '0' && *start <= '9') {
348                                 acc_freq = (acc_freq * 10) + (*start - '0');
349                                 ++start;
350                         }
351
352                         *cpu_freq = acc_freq;
353                         fclose(cpuinfo_fp);
354                         return RUNTIME_INFO_ERROR_NONE;
355                 }
356                 ++cur_core;
357         }
358
359         fclose(cpuinfo_fp);
360         return RUNTIME_INFO_ERROR_NO_DATA;
361 }
362 //LCOV_EXCL_STOP