removed log messages from process usage info APIs
[platform/core/api/runtime-info.git] / src / runtime_info.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 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30
31 #define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
32
33 #define RUNTIME_INFO_MAX -1
34
35 typedef struct {
36         runtime_info_changed_cb changed_cb;
37         void *user_data;
38         runtime_info_value_h most_recent_value;
39 } runtime_info_event_subscription_s;
40
41 typedef runtime_info_event_subscription_s * runtime_info_event_subscription_h;
42
43 typedef struct {
44         runtime_info_key_e key;
45         runtime_info_data_type_e data_type;
46         runtime_info_func_get_value get_value;
47         runtime_info_func_set_event_cb set_event_cb;
48         runtime_info_func_unset_event_cb unset_event_cb;
49         runtime_info_event_subscription_h event_subscription;
50 } runtime_info_item_s;
51
52 typedef runtime_info_item_s * runtime_info_item_h;
53
54 runtime_info_item_s runtime_info_item_table[] = {
55
56 {
57         RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, /**<Indicates whether Bluetooth is enabled. */
58         RUNTIME_INFO_DATA_TYPE_BOOL,
59         runtime_info_bt_enabled_get_value,
60         runtime_info_bt_enabled_set_event_cb,
61         runtime_info_bt_enabled_unset_event_cb,
62         NULL
63 },
64
65 {
66         RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, /**<Indicates whether Wi-Fi hotspot is enabled. */
67         RUNTIME_INFO_DATA_TYPE_BOOL,
68         runtime_info_wifi_hotspot_get_value,
69         runtime_info_wifi_hotspot_set_event_cb,
70         runtime_info_wifi_hotspot_unset_event_cb,
71         NULL
72 },
73
74 {
75         RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, /**<Indicates whether Bluetooth tethering is enabled. */
76         RUNTIME_INFO_DATA_TYPE_BOOL,
77         runtime_info_bt_hotspot_get_value,
78         runtime_info_bt_hotspot_set_event_cb,
79         runtime_info_bt_hotspot_unset_event_cb,
80         NULL
81 },
82
83 {
84         RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, /**<Indicates whether USB tethering is enabled. */
85         RUNTIME_INFO_DATA_TYPE_BOOL,
86         runtime_info_usb_hotspot_get_value,
87         runtime_info_usb_hotspot_set_event_cb,
88         runtime_info_usb_hotspot_unset_event_cb,
89         NULL
90 },
91
92 {
93         RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED, /**<Indicates whether the location service is allowed to use location data from GPS satellites. */
94         RUNTIME_INFO_DATA_TYPE_BOOL,
95         runtime_info_location_service_get_value,
96         runtime_info_location_service_set_event_cb,
97         runtime_info_location_service_unset_event_cb,
98         NULL
99 },
100
101 {
102         RUNTIME_INFO_KEY_LOCATION_NETWORK_POSITION_ENABLED, /**<Indicates whether the location service is allowed to use location data from cellular and Wi-Fi. */
103         RUNTIME_INFO_DATA_TYPE_BOOL,
104         runtime_info_location_network_get_value,
105         runtime_info_location_network_set_event_cb,
106         runtime_info_location_network_unset_event_cb,
107         NULL
108 },
109
110 {
111         RUNTIME_INFO_KEY_PACKET_DATA_ENABLED, /**<Indicates Whether the packet data through 3G network is enabled. */
112         RUNTIME_INFO_DATA_TYPE_BOOL,
113         runtime_info_packet_data_get_value,
114         runtime_info_packet_data_set_event_cb,
115         runtime_info_packet_data_unset_event_cb,
116         NULL
117 },
118
119 {
120         RUNTIME_INFO_KEY_DATA_ROAMING_ENABLED, /**<Indicates whether data roaming is enabled. */
121         RUNTIME_INFO_DATA_TYPE_BOOL,
122         runtime_info_data_roaming_get_value,
123         runtime_info_data_roaming_set_event_cb,
124         runtime_info_data_roaming_unset_event_cb,
125         NULL
126 },
127
128 {
129         RUNTIME_INFO_KEY_VIBRATION_ENABLED, /**<Indicates whether vibration is enabled. */
130         RUNTIME_INFO_DATA_TYPE_BOOL,
131         runtime_info_vibration_enabled_get_value,
132         runtime_info_vibration_enabled_set_event_cb,
133         runtime_info_vibration_enabled_unset_event_cb,
134         NULL
135 },
136
137 {
138         RUNTIME_INFO_KEY_AUDIO_JACK_CONNECTED, /**<Indicates whether audio jack is connected. */
139         RUNTIME_INFO_DATA_TYPE_BOOL,
140         runtime_info_audiojack_get_value,
141         runtime_info_audiojack_set_event_cb,
142         runtime_info_audiojack_unset_event_cb,
143         NULL
144 },
145
146 {
147         RUNTIME_INFO_KEY_GPS_STATUS, /**<Indicates the current status of GPS. */
148         RUNTIME_INFO_DATA_TYPE_INT,
149         runtime_info_gps_status_get_value,
150         runtime_info_gps_status_set_event_cb,
151         runtime_info_gps_status_unset_event_cb,
152         NULL
153 },
154
155 {
156         RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, /**<Indicates the battery is currently charging. */
157         RUNTIME_INFO_DATA_TYPE_BOOL,
158         runtime_info_battery_charging_get_value,
159         runtime_info_battery_charging_set_event_cb,
160         runtime_info_battery_charging_unset_event_cb,
161         NULL
162 },
163
164 {
165         RUNTIME_INFO_KEY_TV_OUT_CONNECTED, /**<Indicates whether TV out is connected. */
166         RUNTIME_INFO_DATA_TYPE_BOOL,
167         runtime_info_tvout_connected_get_value,
168         runtime_info_tvout_connected_set_event_cb,
169         runtime_info_tvout_connected_unset_event_cb,
170         NULL
171 },
172
173 {
174         RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, /**<Indicates the current status of audio jack. */
175         RUNTIME_INFO_DATA_TYPE_INT,
176         runtime_info_audio_jack_status_get_value,
177         runtime_info_audio_jack_status_set_event_cb,
178         runtime_info_audio_jack_status_unset_event_cb,
179         NULL
180 },
181
182 {
183         RUNTIME_INFO_KEY_USB_CONNECTED, /**<Indicates whether usb is connected. */
184         RUNTIME_INFO_DATA_TYPE_BOOL,
185         runtime_info_usb_connected_get_value,
186         runtime_info_usb_connected_set_event_cb,
187         runtime_info_usb_connected_unset_event_cb,
188         NULL
189 },
190
191 {
192         RUNTIME_INFO_KEY_CHARGER_CONNECTED, /**<Indicates whether charger is connected. */
193         RUNTIME_INFO_DATA_TYPE_BOOL,
194         runtime_info_charger_connected_get_value,
195         runtime_info_charger_connected_set_event_cb,
196         runtime_info_charger_connected_unset_event_cb,
197         NULL
198 },
199
200 {
201         RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, /**<Indicates whether auto rotation is enabled. */
202         RUNTIME_INFO_DATA_TYPE_BOOL,
203         runtime_info_auto_rotation_enabled_get_value,
204         runtime_info_auto_rotation_enabled_set_event_cb,
205         runtime_info_auto_rotation_enabled_unset_event_cb,
206         NULL
207 },
208
209 {
210         RUNTIME_INFO_MAX, RUNTIME_INFO_DATA_TYPE_INT, NULL, NULL, NULL, NULL
211 }
212
213 };
214
215 static int runtime_info_get_item(runtime_info_key_e key, runtime_info_item_h *runtime_info_item)
216 {
217         int index = 0;
218
219         while (runtime_info_item_table[index].key != RUNTIME_INFO_MAX) {
220                 if (runtime_info_item_table[index].key == key) {
221                         *runtime_info_item = &runtime_info_item_table[index];
222                         return 0;
223                 }
224
225                 index++;
226         }
227
228         return -1;
229 }
230
231 int runtime_info_get_value(runtime_info_key_e key, runtime_info_data_type_e data_type, runtime_info_value_h value)
232 {
233         runtime_info_item_h runtime_info_item;
234         runtime_info_func_get_value get_value;
235         int ret;
236
237         if (runtime_info_get_item(key, &runtime_info_item))     {
238                 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
239                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
240         }
241
242         if (runtime_info_item->data_type != data_type) {
243                 LOGE("INVALID_PARAMETER(0x%08x) : invalid data type", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
244                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
245         }
246
247         get_value = runtime_info_item->get_value;
248
249         if (get_value == NULL) {
250                 LOGE("IO_ERROR(0x%08x) : failed to call getter for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
251                 return RUNTIME_INFO_ERROR_IO_ERROR;
252         }
253
254         ret = get_value(value);
255         if (ret != RUNTIME_INFO_ERROR_NONE)
256                 LOGE("IO_ERROR(0x%08x) : failed to get the runtime informaion / key(%d)", ret, key);
257
258         return ret;
259 }
260
261 API int runtime_info_get_value_int(runtime_info_key_e key, int *value)
262 {
263         int retcode;
264         runtime_info_value_u runtime_info_value;
265
266         if (value == NULL) {
267                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
268                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
269         }
270
271         retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_INT, &runtime_info_value);
272
273         if (retcode == RUNTIME_INFO_ERROR_NONE)
274                 *value = runtime_info_value.i;
275
276         return retcode;
277 }
278
279 API int runtime_info_get_value_bool(runtime_info_key_e key, bool *value)
280 {
281         int retcode;
282         runtime_info_value_u runtime_info_value;
283
284         if (value == NULL) {
285                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
286                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
287         }
288
289         retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_BOOL, &runtime_info_value);
290
291         if (retcode == RUNTIME_INFO_ERROR_NONE)
292                 *value = runtime_info_value.b;
293
294         return retcode;
295 }
296
297 API int runtime_info_get_value_double(runtime_info_key_e key, double *value)
298 {
299         int retcode;
300         runtime_info_value_u runtime_info_value;
301
302         if (value == NULL) {
303                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
304                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
305         }
306
307         retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_DOUBLE, &runtime_info_value);
308
309         if (retcode == RUNTIME_INFO_ERROR_NONE)
310                 *value = runtime_info_value.d;
311
312         return retcode;
313 }
314
315 API int runtime_info_get_value_string(runtime_info_key_e key, char **value)
316 {
317         int retcode;
318         runtime_info_value_u runtime_info_value;
319
320         if (value == NULL) {
321                 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
322                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
323         }
324
325         retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_STRING, &runtime_info_value);
326
327         if (retcode == RUNTIME_INFO_ERROR_NONE)
328                 *value = runtime_info_value.s;
329
330         return retcode;
331 }
332
333 API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb callback, void *user_data)
334 {
335         runtime_info_item_h runtime_info_item;
336         runtime_info_func_set_event_cb set_event_cb;
337         bool subscribe_event = false;
338
339         if (callback == NULL) {
340                 LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
341                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
342         }
343
344         if (runtime_info_get_item(key, &runtime_info_item))     {
345                 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
346                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
347         }
348
349         set_event_cb = runtime_info_item->set_event_cb;
350
351         if (set_event_cb == NULL) {
352                 LOGE("IO_ERROR(0x%08x) : failed to set callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
353                 return RUNTIME_INFO_ERROR_IO_ERROR;
354         }
355
356         if (runtime_info_item->event_subscription == NULL) {
357                 subscribe_event = true;
358
359                 runtime_info_event_subscription_h event_subscription;
360                 event_subscription = malloc(sizeof(runtime_info_event_subscription_s));
361
362                 if (event_subscription == NULL) {
363                         LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
364                         return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
365                 }
366
367                 runtime_info_item->event_subscription = event_subscription;
368                 runtime_info_item->event_subscription->most_recent_value = NULL;
369         }
370
371         runtime_info_item->event_subscription->changed_cb = callback;
372         runtime_info_item->event_subscription->user_data = user_data;
373
374         if (runtime_info_item->event_subscription->most_recent_value != NULL)
375                 free(runtime_info_item->event_subscription->most_recent_value);
376
377         runtime_info_item->event_subscription->most_recent_value = NULL;
378
379         if (subscribe_event == true)
380                 return set_event_cb();
381         else
382                 return RUNTIME_INFO_ERROR_NONE;
383 }
384
385 API int runtime_info_unset_changed_cb(runtime_info_key_e key)
386 {
387         runtime_info_item_h runtime_info_item;
388         runtime_info_func_unset_event_cb unset_event_cb;
389
390         if (runtime_info_get_item(key, &runtime_info_item))     {
391                 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
392                 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
393         }
394
395         if (runtime_info_item->event_subscription != NULL) {
396                 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
397                         free(runtime_info_item->event_subscription->most_recent_value);
398                         runtime_info_item->event_subscription->most_recent_value = NULL;
399                 }
400
401                 free(runtime_info_item->event_subscription);
402                 runtime_info_item->event_subscription = NULL;
403         }
404
405         unset_event_cb = runtime_info_item->unset_event_cb;
406
407         if (unset_event_cb == NULL)     {
408                 LOGE("IO_ERROR(0x%08x) : failed to unset callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
409                 return RUNTIME_INFO_ERROR_IO_ERROR;
410         }
411
412         unset_event_cb();
413
414         return RUNTIME_INFO_ERROR_NONE;
415 }
416
417 void runtime_info_updated(runtime_info_key_e key)
418 {
419         runtime_info_item_h runtime_info_item;
420         runtime_info_value_u current_value;
421         bool dispatch_event = true;
422         int retcode;
423
424         if (runtime_info_get_item(key, &runtime_info_item))     {
425                 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
426                 return;
427         }
428
429         if (runtime_info_item->event_subscription == NULL) {
430                 LOGE("IO_ERROR(0x%08x) : invalid event subscription", RUNTIME_INFO_ERROR_IO_ERROR);
431                 return;
432         }
433
434         memset(&current_value, 0, sizeof(runtime_info_value_u));
435
436         retcode = runtime_info_get_value(key, runtime_info_item->data_type, &current_value);
437
438         if (retcode != RUNTIME_INFO_ERROR_NONE) {
439                 LOGE("IO_ERROR(0x%08x) : failed to get the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
440                 return;
441         }
442
443         if (runtime_info_item->event_subscription->most_recent_value != NULL) {
444                 if (!memcmp(runtime_info_item->event_subscription->most_recent_value, &current_value, sizeof(runtime_info_value_u)))
445                         dispatch_event = false;
446         } else {
447                 runtime_info_item->event_subscription->most_recent_value = calloc(1, sizeof(runtime_info_value_u));
448
449                 if (runtime_info_item->event_subscription->most_recent_value == NULL) {
450                         LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
451                         return;
452                 }
453         }
454
455         if (dispatch_event == true && runtime_info_item->event_subscription->changed_cb != NULL) {
456                 memcpy(runtime_info_item->event_subscription->most_recent_value, &current_value, sizeof(runtime_info_value_u));
457                 runtime_info_item->event_subscription->changed_cb(key, runtime_info_item->event_subscription->user_data);
458         }
459 }