2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <runtime_info.h>
25 #include <runtime_info_private.h>
31 #define LOG_TAG "CAPI_SYSTEM_RUNTIME_INFO"
33 #define RUNTIME_INFO_MAX -1
36 runtime_info_changed_cb changed_cb;
38 runtime_info_value_h most_recent_value;
39 } runtime_info_event_subscription_s;
41 typedef runtime_info_event_subscription_s * runtime_info_event_subscription_h;
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;
52 typedef runtime_info_item_s * runtime_info_item_h;
54 runtime_info_item_s runtime_info_item_table[] = {
57 RUNTIME_INFO_KEY_FLIGHT_MODE_ENABLED, /**<Indicates whether the device is in flight mode. */
58 RUNTIME_INFO_DATA_TYPE_BOOL,
59 runtime_info_flightmode_get_value,
60 runtime_info_flightmode_set_event_cb,
61 runtime_info_flightmode_unset_event_cb,
67 RUNTIME_INFO_KEY_WIFI_STATUS, /**<Indicates the current status of Wi-Fi. */
68 RUNTIME_INFO_DATA_TYPE_INT,
69 runtime_info_wifi_status_get_value,
70 runtime_info_wifi_status_set_event_cb,
71 runtime_info_wifi_status_unset_event_cb,
77 RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, /**<Indicates whether Bluetooth is enabled. */
78 RUNTIME_INFO_DATA_TYPE_BOOL,
79 runtime_info_bt_enabled_get_value,
80 runtime_info_bt_enabled_set_event_cb,
81 runtime_info_bt_enabled_unset_event_cb,
86 RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, /**<Indicates whether Wi-Fi hotspot is enabled. */
87 RUNTIME_INFO_DATA_TYPE_BOOL,
88 runtime_info_wifi_hotspot_get_value,
89 runtime_info_wifi_hotspot_set_event_cb,
90 runtime_info_wifi_hotspot_unset_event_cb,
95 RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, /**<Indicates whether Bluetooth tethering is enabled. */
96 RUNTIME_INFO_DATA_TYPE_BOOL,
97 runtime_info_bt_hotspot_get_value,
98 runtime_info_bt_hotspot_set_event_cb,
99 runtime_info_bt_hotspot_unset_event_cb,
104 RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, /**<Indicates whether USB tethering is enabled. */
105 RUNTIME_INFO_DATA_TYPE_BOOL,
106 runtime_info_usb_hotspot_get_value,
107 runtime_info_usb_hotspot_set_event_cb,
108 runtime_info_usb_hotspot_unset_event_cb,
113 RUNTIME_INFO_KEY_LOCATION_SERVICE_ENABLED, /**<Indicates whether the location service is allowed to use location data from GPS satellites. */
114 RUNTIME_INFO_DATA_TYPE_BOOL,
115 runtime_info_location_service_get_value,
116 runtime_info_location_service_set_event_cb,
117 runtime_info_location_service_unset_event_cb,
122 RUNTIME_INFO_KEY_LOCATION_ADVANCED_GPS_ENABLED, /**<Indicates whether the location service is allowed to download location data for GPS operation. */
123 RUNTIME_INFO_DATA_TYPE_BOOL,
124 runtime_info_location_agps_get_value,
125 runtime_info_location_agps_set_event_cb,
126 runtime_info_location_agps_unset_event_cb,
131 RUNTIME_INFO_KEY_LOCATION_NETWORK_POSITION_ENABLED, /**<Indicates whether the location service is allowed to use location data from cellular and Wi-Fi. */
132 RUNTIME_INFO_DATA_TYPE_BOOL,
133 runtime_info_location_network_get_value,
134 runtime_info_location_network_set_event_cb,
135 runtime_info_location_network_unset_event_cb,
140 RUNTIME_INFO_KEY_PACKET_DATA_ENABLED, /**<Indicates Whether the packet data through 3G network is enabled. */
141 RUNTIME_INFO_DATA_TYPE_BOOL,
142 runtime_info_packet_data_get_value,
143 runtime_info_packet_data_set_event_cb,
144 runtime_info_packet_data_unset_event_cb,
149 RUNTIME_INFO_KEY_DATA_ROAMING_ENABLED, /**<Indicates whether data roaming is enabled. */
150 RUNTIME_INFO_DATA_TYPE_BOOL,
151 runtime_info_data_roaming_get_value,
152 runtime_info_data_roaming_set_event_cb,
153 runtime_info_data_roaming_unset_event_cb,
158 RUNTIME_INFO_KEY_SILENT_MODE_ENABLED, /**<Indicates whether the device is in silent mode. */
159 RUNTIME_INFO_DATA_TYPE_BOOL,
160 runtime_info_silent_mode_get_value,
161 runtime_info_silent_mode_set_event_cb,
162 runtime_info_silent_mode_unset_event_cb,
167 RUNTIME_INFO_KEY_VIBRATION_ENABLED, /**<Indicates whether vibration is enabled. */
168 RUNTIME_INFO_DATA_TYPE_BOOL,
169 runtime_info_vibration_enabled_get_value,
170 runtime_info_vibration_enabled_set_event_cb,
171 runtime_info_vibration_enabled_unset_event_cb,
176 RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, /**<Indicates the current time format. */
177 RUNTIME_INFO_DATA_TYPE_BOOL,
178 runtime_info_24hour_format_get_value,
179 runtime_info_24hour_format_set_event_cb,
180 runtime_info_24hour_format_unset_event_cb,
185 RUNTIME_INFO_KEY_FIRST_DAY_OF_WEEK, /**<Indicates the first day of week. */
186 RUNTIME_INFO_DATA_TYPE_INT,
187 runtime_info_first_day_of_week_get_value,
188 runtime_info_first_day_of_week_set_event_cb,
189 runtime_info_first_day_of_week_unset_event_cb,
194 RUNTIME_INFO_KEY_LANGUAGE, /**<Indicates the current language setting. */
195 RUNTIME_INFO_DATA_TYPE_STRING,
196 runtime_info_language_get_value,
197 runtime_info_language_set_event_cb,
198 runtime_info_language_unset_event_cb,
203 RUNTIME_INFO_KEY_REGION, /**<Indicates the current region setting. */
204 RUNTIME_INFO_DATA_TYPE_STRING,
205 runtime_info_region_get_value,
206 runtime_info_region_set_event_cb,
207 runtime_info_region_unset_event_cb,
212 RUNTIME_INFO_KEY_AUDIO_JACK_CONNECTED, /**<Indicates whether audio jack is connected. */
213 RUNTIME_INFO_DATA_TYPE_BOOL,
214 runtime_info_audiojack_get_value,
215 runtime_info_audiojack_set_event_cb,
216 runtime_info_audiojack_unset_event_cb,
221 RUNTIME_INFO_KEY_GPS_STATUS, /**<Indicates the current status of GPS. */
222 RUNTIME_INFO_DATA_TYPE_INT,
223 runtime_info_gps_status_get_value,
224 runtime_info_gps_status_set_event_cb,
225 runtime_info_gps_status_unset_event_cb,
230 RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, /**<Indicates the battery is currently charging. */
231 RUNTIME_INFO_DATA_TYPE_BOOL,
232 runtime_info_battery_charging_get_value,
233 runtime_info_battery_charging_set_event_cb,
234 runtime_info_battery_charging_unset_event_cb,
239 RUNTIME_INFO_KEY_TV_OUT_CONNECTED, /**<Indicates whether TV out is connected. */
240 RUNTIME_INFO_DATA_TYPE_BOOL,
241 runtime_info_tvout_connected_get_value,
242 runtime_info_tvout_connected_set_event_cb,
243 runtime_info_tvout_connected_unset_event_cb,
248 RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, /**<Indicates the current status of audio jack. */
249 RUNTIME_INFO_DATA_TYPE_INT,
250 runtime_info_audio_jack_status_get_value,
251 runtime_info_audio_jack_status_set_event_cb,
252 runtime_info_audio_jack_status_unset_event_cb,
257 RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, /**<Indicates whether sliding keyboard is opened. */
258 RUNTIME_INFO_DATA_TYPE_BOOL,
259 runtime_info_sliding_keyboard_opened_get_value,
260 runtime_info_sliding_keyboard_opened_set_event_cb,
261 runtime_info_sliding_keyboard_opened_unset_event_cb,
266 RUNTIME_INFO_KEY_USB_CONNECTED, /**<Indicates whether usb is connected. */
267 RUNTIME_INFO_DATA_TYPE_BOOL,
268 runtime_info_usb_connected_get_value,
269 runtime_info_usb_connected_set_event_cb,
270 runtime_info_usb_connected_unset_event_cb,
275 RUNTIME_INFO_KEY_CHARGER_CONNECTED, /**<Indicates whether charger is connected. */
276 RUNTIME_INFO_DATA_TYPE_BOOL,
277 runtime_info_charger_connected_get_value,
278 runtime_info_charger_connected_set_event_cb,
279 runtime_info_charger_connected_unset_event_cb,
284 RUNTIME_INFO_KEY_VIBRATION_LEVEL_HAPTIC_FEEDBACK, /**<Indicates the current vibration level of haptic feedback. */
285 RUNTIME_INFO_DATA_TYPE_INT,
286 runtime_info_vibration_level_haptic_feedback_get_value,
287 runtime_info_vibration_level_haptic_feedback_set_event_cb,
288 runtime_info_vibration_level_haptic_feedback_unset_event_cb,
293 RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, /**<Indicates whether auto rotation is enabled. */
294 RUNTIME_INFO_DATA_TYPE_BOOL,
295 runtime_info_auto_rotation_enabled_get_value,
296 runtime_info_auto_rotation_enabled_set_event_cb,
297 runtime_info_auto_rotation_enabled_unset_event_cb,
302 RUNTIME_INFO_MAX, RUNTIME_INFO_DATA_TYPE_INT, NULL, NULL, NULL, NULL
307 static int runtime_info_get_item(runtime_info_key_e key, runtime_info_item_h *runtime_info_item)
311 while (runtime_info_item_table[index].key != RUNTIME_INFO_MAX) {
312 if (runtime_info_item_table[index].key == key) {
313 *runtime_info_item = &runtime_info_item_table[index];
323 int runtime_info_get_value(runtime_info_key_e key, runtime_info_data_type_e data_type, runtime_info_value_h value)
325 runtime_info_item_h runtime_info_item;
326 runtime_info_func_get_value get_value;
329 if (runtime_info_get_item(key, &runtime_info_item)) {
330 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
331 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
334 if (runtime_info_item->data_type != data_type) {
335 LOGE("INVALID_PARAMETER(0x%08x) : invalid data type", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
336 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
339 get_value = runtime_info_item->get_value;
341 if (get_value == NULL) {
342 LOGE("IO_ERROR(0x%08x) : failed to call getter for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
343 return RUNTIME_INFO_ERROR_IO_ERROR;
346 ret = get_value(value);
347 if (ret != RUNTIME_INFO_ERROR_NONE) {
348 LOGE("ERROR(0x%08x) : fail to get the runtime information / key(%d)", ret, key);
352 return RUNTIME_INFO_ERROR_NONE;
355 API int runtime_info_get_value_int(runtime_info_key_e key, int *value)
358 runtime_info_value_u runtime_info_value;
361 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
362 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
365 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_INT, &runtime_info_value);
367 if (retcode == RUNTIME_INFO_ERROR_NONE)
368 *value = runtime_info_value.i;
373 API int runtime_info_get_value_bool(runtime_info_key_e key, bool *value)
376 runtime_info_value_u runtime_info_value;
379 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
380 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
383 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_BOOL, &runtime_info_value);
385 if (retcode == RUNTIME_INFO_ERROR_NONE)
386 *value = runtime_info_value.b;
391 API int runtime_info_get_value_double(runtime_info_key_e key, double *value)
394 runtime_info_value_u runtime_info_value;
397 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
398 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
401 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_DOUBLE, &runtime_info_value);
403 if (retcode == RUNTIME_INFO_ERROR_NONE)
404 *value = runtime_info_value.d;
409 API int runtime_info_get_value_string(runtime_info_key_e key, char **value)
412 runtime_info_value_u runtime_info_value;
415 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
416 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
419 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_STRING, &runtime_info_value);
421 if (retcode == RUNTIME_INFO_ERROR_NONE)
422 *value = runtime_info_value.s;
427 API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb callback, void *user_data)
429 runtime_info_item_h runtime_info_item;
430 runtime_info_func_set_event_cb set_event_cb;
431 bool subscribe_event = false;
433 if (callback == NULL) {
434 LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
435 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
438 if (runtime_info_get_item(key, &runtime_info_item)) {
439 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
440 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
443 set_event_cb = runtime_info_item->set_event_cb;
445 if (set_event_cb == NULL) {
446 LOGE("IO_ERROR(0x%08x) : failed to set callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
447 return RUNTIME_INFO_ERROR_IO_ERROR;
450 if (runtime_info_item->event_subscription == NULL) {
451 subscribe_event = true;
453 runtime_info_event_subscription_h event_subscription;
454 event_subscription = malloc(sizeof(runtime_info_event_subscription_s));
456 if (event_subscription == NULL) {
457 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
458 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
461 runtime_info_item->event_subscription = event_subscription;
462 runtime_info_item->event_subscription->most_recent_value = NULL;
465 runtime_info_item->event_subscription->changed_cb = callback;
466 runtime_info_item->event_subscription->user_data = user_data;
468 if (runtime_info_item->event_subscription->most_recent_value != NULL)
469 free(runtime_info_item->event_subscription->most_recent_value);
471 runtime_info_item->event_subscription->most_recent_value = NULL;
473 if (subscribe_event == true)
474 return set_event_cb();
476 return RUNTIME_INFO_ERROR_NONE;
479 API int runtime_info_unset_changed_cb(runtime_info_key_e key)
481 runtime_info_item_h runtime_info_item;
482 runtime_info_func_unset_event_cb unset_event_cb;
484 if (runtime_info_get_item(key, &runtime_info_item)) {
485 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
486 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
489 if (runtime_info_item->event_subscription != NULL) {
490 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
491 free(runtime_info_item->event_subscription->most_recent_value);
492 runtime_info_item->event_subscription->most_recent_value = NULL;
495 free(runtime_info_item->event_subscription);
496 runtime_info_item->event_subscription = NULL;
499 unset_event_cb = runtime_info_item->unset_event_cb;
501 if (unset_event_cb == NULL) {
502 LOGE("IO_ERROR(0x%08x) : failed to unset callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
503 return RUNTIME_INFO_ERROR_IO_ERROR;
508 return RUNTIME_INFO_ERROR_NONE;
511 void runtime_info_updated(runtime_info_key_e key)
513 runtime_info_item_h runtime_info_item;
514 runtime_info_value_u current_value;
515 bool dispatch_event = true;
518 if (runtime_info_get_item(key, &runtime_info_item)) {
519 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
523 if (runtime_info_item->event_subscription == NULL) {
524 LOGE("IO_ERROR(0x%08x) : invalid event subscription", RUNTIME_INFO_ERROR_IO_ERROR);
528 memset(¤t_value, 0, sizeof(runtime_info_value_u));
530 retcode = runtime_info_get_value(key, runtime_info_item->data_type, ¤t_value);
532 if (retcode != RUNTIME_INFO_ERROR_NONE) {
533 LOGE("IO_ERROR(0x%08x) : failed to get the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
537 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
538 if (!memcmp(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u)))
539 dispatch_event = false;
541 runtime_info_item->event_subscription->most_recent_value = calloc(1, sizeof(runtime_info_value_u));
543 if (runtime_info_item->event_subscription->most_recent_value == NULL) {
544 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
549 if (dispatch_event == true && runtime_info_item->event_subscription->changed_cb != NULL) {
550 memcpy(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u));
551 runtime_info_item->event_subscription->changed_cb(key, runtime_info_item->event_subscription->user_data);