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;
328 if (runtime_info_get_item(key, &runtime_info_item)) {
329 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
330 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
333 if (runtime_info_item->data_type != data_type) {
334 LOGE("INVALID_PARAMETER(0x%08x) : invalid data type", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
335 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
338 get_value = runtime_info_item->get_value;
340 if (get_value == NULL) {
341 LOGE("IO_ERROR(0x%08x) : failed to call getter for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
342 return RUNTIME_INFO_ERROR_IO_ERROR;
345 if (get_value(value) != RUNTIME_INFO_ERROR_NONE) {
346 LOGE("IO_ERROR(0x%08x) : failed to get the runtime informaion / key(%d)", RUNTIME_INFO_ERROR_IO_ERROR, key);
347 return RUNTIME_INFO_ERROR_IO_ERROR;
350 return RUNTIME_INFO_ERROR_NONE;
353 API int runtime_info_get_value_int(runtime_info_key_e key, int *value)
356 runtime_info_value_u runtime_info_value;
359 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
360 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
363 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_INT, &runtime_info_value);
365 if (retcode == RUNTIME_INFO_ERROR_NONE)
366 *value = runtime_info_value.i;
371 API int runtime_info_get_value_bool(runtime_info_key_e key, bool *value)
374 runtime_info_value_u runtime_info_value;
377 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
378 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
381 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_BOOL, &runtime_info_value);
383 if (retcode == RUNTIME_INFO_ERROR_NONE)
384 *value = runtime_info_value.b;
389 API int runtime_info_get_value_double(runtime_info_key_e key, double *value)
392 runtime_info_value_u runtime_info_value;
395 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
396 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
399 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_DOUBLE, &runtime_info_value);
401 if (retcode == RUNTIME_INFO_ERROR_NONE)
402 *value = runtime_info_value.d;
407 API int runtime_info_get_value_string(runtime_info_key_e key, char **value)
410 runtime_info_value_u runtime_info_value;
413 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
414 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
417 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_STRING, &runtime_info_value);
419 if (retcode == RUNTIME_INFO_ERROR_NONE)
420 *value = runtime_info_value.s;
425 API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb callback, void *user_data)
427 runtime_info_item_h runtime_info_item;
428 runtime_info_func_set_event_cb set_event_cb;
429 bool subscribe_event = false;
431 if (callback == NULL) {
432 LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
433 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
436 if (runtime_info_get_item(key, &runtime_info_item)) {
437 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
438 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
441 set_event_cb = runtime_info_item->set_event_cb;
443 if (set_event_cb == NULL) {
444 LOGE("IO_ERROR(0x%08x) : failed to set callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
445 return RUNTIME_INFO_ERROR_IO_ERROR;
448 if (runtime_info_item->event_subscription == NULL) {
449 subscribe_event = true;
451 runtime_info_event_subscription_h event_subscription;
452 event_subscription = malloc(sizeof(runtime_info_event_subscription_s));
454 if (event_subscription == NULL) {
455 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
456 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
459 runtime_info_item->event_subscription = event_subscription;
460 runtime_info_item->event_subscription->most_recent_value = NULL;
463 runtime_info_item->event_subscription->changed_cb = callback;
464 runtime_info_item->event_subscription->user_data = user_data;
466 if (runtime_info_item->event_subscription->most_recent_value != NULL)
467 free(runtime_info_item->event_subscription->most_recent_value);
469 runtime_info_item->event_subscription->most_recent_value = NULL;
471 if (subscribe_event == true)
472 return set_event_cb();
474 return RUNTIME_INFO_ERROR_NONE;
477 API int runtime_info_unset_changed_cb(runtime_info_key_e key)
479 runtime_info_item_h runtime_info_item;
480 runtime_info_func_unset_event_cb unset_event_cb;
482 if (runtime_info_get_item(key, &runtime_info_item)) {
483 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
484 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
487 if (runtime_info_item->event_subscription != NULL) {
488 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
489 free(runtime_info_item->event_subscription->most_recent_value);
490 runtime_info_item->event_subscription->most_recent_value = NULL;
493 free(runtime_info_item->event_subscription);
494 runtime_info_item->event_subscription = NULL;
497 unset_event_cb = runtime_info_item->unset_event_cb;
499 if (unset_event_cb == NULL) {
500 LOGE("IO_ERROR(0x%08x) : failed to unset callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
501 return RUNTIME_INFO_ERROR_IO_ERROR;
506 return RUNTIME_INFO_ERROR_NONE;
509 void runtime_info_updated(runtime_info_key_e key)
511 runtime_info_item_h runtime_info_item;
512 runtime_info_value_u current_value;
513 bool dispatch_event = true;
516 if (runtime_info_get_item(key, &runtime_info_item)) {
517 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
521 if (runtime_info_item->event_subscription == NULL) {
522 LOGE("IO_ERROR(0x%08x) : invalid event subscription", RUNTIME_INFO_ERROR_IO_ERROR);
526 memset(¤t_value, 0, sizeof(runtime_info_value_u));
528 retcode = runtime_info_get_value(key, runtime_info_item->data_type, ¤t_value);
530 if (retcode != RUNTIME_INFO_ERROR_NONE) {
531 LOGE("IO_ERROR(0x%08x) : failed to get the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
535 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
536 if (!memcmp(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u)))
537 dispatch_event = false;
539 runtime_info_item->event_subscription->most_recent_value = calloc(1, sizeof(runtime_info_value_u));
541 if (runtime_info_item->event_subscription->most_recent_value == NULL) {
542 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
547 if (dispatch_event == true && runtime_info_item->event_subscription->changed_cb != NULL) {
548 memcpy(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u));
549 runtime_info_item->event_subscription->changed_cb(key, runtime_info_item->event_subscription->user_data);