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_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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
210 RUNTIME_INFO_MAX, RUNTIME_INFO_DATA_TYPE_INT, NULL, NULL, NULL, NULL
215 static int runtime_info_get_item(runtime_info_key_e key, runtime_info_item_h *runtime_info_item)
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];
231 int runtime_info_get_value(runtime_info_key_e key, runtime_info_data_type_e data_type, runtime_info_value_h value)
233 runtime_info_item_h runtime_info_item;
234 runtime_info_func_get_value get_value;
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;
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;
247 get_value = runtime_info_item->get_value;
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;
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);
261 API int runtime_info_get_value_int(runtime_info_key_e key, int *value)
264 runtime_info_value_u runtime_info_value;
267 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
268 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
271 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_INT, &runtime_info_value);
273 if (retcode == RUNTIME_INFO_ERROR_NONE)
274 *value = runtime_info_value.i;
279 API int runtime_info_get_value_bool(runtime_info_key_e key, bool *value)
282 runtime_info_value_u runtime_info_value;
285 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
286 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
289 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_BOOL, &runtime_info_value);
291 if (retcode == RUNTIME_INFO_ERROR_NONE)
292 *value = runtime_info_value.b;
297 API int runtime_info_get_value_double(runtime_info_key_e key, double *value)
300 runtime_info_value_u runtime_info_value;
303 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
304 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
307 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_DOUBLE, &runtime_info_value);
309 if (retcode == RUNTIME_INFO_ERROR_NONE)
310 *value = runtime_info_value.d;
315 API int runtime_info_get_value_string(runtime_info_key_e key, char **value)
318 runtime_info_value_u runtime_info_value;
321 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
322 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
325 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_STRING, &runtime_info_value);
327 if (retcode == RUNTIME_INFO_ERROR_NONE)
328 *value = runtime_info_value.s;
333 API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb callback, void *user_data)
335 runtime_info_item_h runtime_info_item;
336 runtime_info_func_set_event_cb set_event_cb;
337 bool subscribe_event = false;
339 if (callback == NULL) {
340 LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
341 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
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;
349 set_event_cb = runtime_info_item->set_event_cb;
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;
356 if (runtime_info_item->event_subscription == NULL) {
357 subscribe_event = true;
359 runtime_info_event_subscription_h event_subscription;
360 event_subscription = malloc(sizeof(runtime_info_event_subscription_s));
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;
367 runtime_info_item->event_subscription = event_subscription;
368 runtime_info_item->event_subscription->most_recent_value = NULL;
371 runtime_info_item->event_subscription->changed_cb = callback;
372 runtime_info_item->event_subscription->user_data = user_data;
374 if (runtime_info_item->event_subscription->most_recent_value != NULL)
375 free(runtime_info_item->event_subscription->most_recent_value);
377 runtime_info_item->event_subscription->most_recent_value = NULL;
379 if (subscribe_event == true)
380 return set_event_cb();
382 return RUNTIME_INFO_ERROR_NONE;
385 API int runtime_info_unset_changed_cb(runtime_info_key_e key)
387 runtime_info_item_h runtime_info_item;
388 runtime_info_func_unset_event_cb unset_event_cb;
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;
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;
401 free(runtime_info_item->event_subscription);
402 runtime_info_item->event_subscription = NULL;
405 unset_event_cb = runtime_info_item->unset_event_cb;
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;
414 return RUNTIME_INFO_ERROR_NONE;
417 void runtime_info_updated(runtime_info_key_e key)
419 runtime_info_item_h runtime_info_item;
420 runtime_info_value_u current_value;
421 bool dispatch_event = true;
424 if (runtime_info_get_item(key, &runtime_info_item)) {
425 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
429 if (runtime_info_item->event_subscription == NULL) {
430 LOGE("IO_ERROR(0x%08x) : invalid event subscription", RUNTIME_INFO_ERROR_IO_ERROR);
434 memset(¤t_value, 0, sizeof(runtime_info_value_u));
436 retcode = runtime_info_get_value(key, runtime_info_item->data_type, ¤t_value);
438 if (retcode != RUNTIME_INFO_ERROR_NONE) {
439 LOGE("IO_ERROR(0x%08x) : failed to get the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
443 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
444 if (!memcmp(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u)))
445 dispatch_event = false;
447 runtime_info_item->event_subscription->most_recent_value = calloc(1, sizeof(runtime_info_value_u));
449 if (runtime_info_item->event_subscription->most_recent_value == NULL) {
450 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
455 if (dispatch_event == true && runtime_info_item->event_subscription->changed_cb != NULL) {
456 memcpy(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u));
457 runtime_info_item->event_subscription->changed_cb(key, runtime_info_item->event_subscription->user_data);