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_INTERNAL_KEY_WIFI_STATUS, /**<Indicates the current status of Wi-Fi. */
58 RUNTIME_INFO_DATA_TYPE_INT,
59 runtime_info_wifi_status_get_value,
60 runtime_info_wifi_status_set_event_cb,
61 runtime_info_wifi_status_unset_event_cb,
66 RUNTIME_INFO_KEY_BLUETOOTH_ENABLED, /**<Indicates whether Bluetooth is enabled. */
67 RUNTIME_INFO_DATA_TYPE_BOOL,
68 runtime_info_bt_enabled_get_value,
69 runtime_info_bt_enabled_set_event_cb,
70 runtime_info_bt_enabled_unset_event_cb,
75 RUNTIME_INFO_KEY_WIFI_HOTSPOT_ENABLED, /**<Indicates whether Wi-Fi hotspot is enabled. */
76 RUNTIME_INFO_DATA_TYPE_BOOL,
77 runtime_info_wifi_hotspot_get_value,
78 runtime_info_wifi_hotspot_set_event_cb,
79 runtime_info_wifi_hotspot_unset_event_cb,
84 RUNTIME_INFO_KEY_BLUETOOTH_TETHERING_ENABLED, /**<Indicates whether Bluetooth tethering is enabled. */
85 RUNTIME_INFO_DATA_TYPE_BOOL,
86 runtime_info_bt_hotspot_get_value,
87 runtime_info_bt_hotspot_set_event_cb,
88 runtime_info_bt_hotspot_unset_event_cb,
93 RUNTIME_INFO_KEY_USB_TETHERING_ENABLED, /**<Indicates whether USB tethering is enabled. */
94 RUNTIME_INFO_DATA_TYPE_BOOL,
95 runtime_info_usb_hotspot_get_value,
96 runtime_info_usb_hotspot_set_event_cb,
97 runtime_info_usb_hotspot_unset_event_cb,
102 RUNTIME_INFO_KEY_PACKET_DATA_ENABLED, /**<Indicates Whether the packet data through 3G network is enabled. */
103 RUNTIME_INFO_DATA_TYPE_BOOL,
104 runtime_info_packet_data_get_value,
105 runtime_info_packet_data_set_event_cb,
106 runtime_info_packet_data_unset_event_cb,
111 RUNTIME_INFO_KEY_DATA_ROAMING_ENABLED, /**<Indicates whether data roaming is enabled. */
112 RUNTIME_INFO_DATA_TYPE_BOOL,
113 runtime_info_data_roaming_get_value,
114 runtime_info_data_roaming_set_event_cb,
115 runtime_info_data_roaming_unset_event_cb,
120 RUNTIME_INFO_KEY_VIBRATION_ENABLED, /**<Indicates whether vibration is enabled. */
121 RUNTIME_INFO_DATA_TYPE_BOOL,
122 runtime_info_vibration_enabled_get_value,
123 runtime_info_vibration_enabled_set_event_cb,
124 runtime_info_vibration_enabled_unset_event_cb,
129 RUNTIME_INFO_KEY_AUDIO_JACK_CONNECTED, /**<Indicates whether audio jack is connected. */
130 RUNTIME_INFO_DATA_TYPE_BOOL,
131 runtime_info_audiojack_get_value,
132 runtime_info_audiojack_set_event_cb,
133 runtime_info_audiojack_unset_event_cb,
138 RUNTIME_INFO_KEY_GPS_STATUS, /**<Indicates the current status of GPS. */
139 RUNTIME_INFO_DATA_TYPE_INT,
140 runtime_info_gps_status_get_value,
141 runtime_info_gps_status_set_event_cb,
142 runtime_info_gps_status_unset_event_cb,
147 RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, /**<Indicates the battery is currently charging. */
148 RUNTIME_INFO_DATA_TYPE_BOOL,
149 runtime_info_battery_charging_get_value,
150 runtime_info_battery_charging_set_event_cb,
151 runtime_info_battery_charging_unset_event_cb,
156 RUNTIME_INFO_KEY_TV_OUT_CONNECTED, /**<Indicates whether TV out is connected. */
157 RUNTIME_INFO_DATA_TYPE_BOOL,
158 runtime_info_tvout_connected_get_value,
159 runtime_info_tvout_connected_set_event_cb,
160 runtime_info_tvout_connected_unset_event_cb,
165 RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, /**<Indicates the current status of audio jack. */
166 RUNTIME_INFO_DATA_TYPE_INT,
167 runtime_info_audio_jack_status_get_value,
168 runtime_info_audio_jack_status_set_event_cb,
169 runtime_info_audio_jack_status_unset_event_cb,
174 RUNTIME_INFO_KEY_USB_CONNECTED, /**<Indicates whether usb is connected. */
175 RUNTIME_INFO_DATA_TYPE_BOOL,
176 runtime_info_usb_connected_get_value,
177 runtime_info_usb_connected_set_event_cb,
178 runtime_info_usb_connected_unset_event_cb,
183 RUNTIME_INFO_KEY_CHARGER_CONNECTED, /**<Indicates whether charger is connected. */
184 RUNTIME_INFO_DATA_TYPE_BOOL,
185 runtime_info_charger_connected_get_value,
186 runtime_info_charger_connected_set_event_cb,
187 runtime_info_charger_connected_unset_event_cb,
192 RUNTIME_INFO_KEY_AUTO_ROTATION_ENABLED, /**<Indicates whether auto rotation is enabled. */
193 RUNTIME_INFO_DATA_TYPE_BOOL,
194 runtime_info_auto_rotation_enabled_get_value,
195 runtime_info_auto_rotation_enabled_set_event_cb,
196 runtime_info_auto_rotation_enabled_unset_event_cb,
201 RUNTIME_INFO_MAX, RUNTIME_INFO_DATA_TYPE_INT, NULL, NULL, NULL, NULL
206 static int runtime_info_get_item(runtime_info_key_e key, runtime_info_item_h *runtime_info_item)
210 while (runtime_info_item_table[index].key != RUNTIME_INFO_MAX) {
211 if (runtime_info_item_table[index].key == key) {
212 *runtime_info_item = &runtime_info_item_table[index];
222 int runtime_info_get_value(runtime_info_key_e key, runtime_info_data_type_e data_type, runtime_info_value_h value)
224 runtime_info_item_h runtime_info_item;
225 runtime_info_func_get_value get_value;
228 if (runtime_info_get_item(key, &runtime_info_item)) {
229 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
230 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
233 if (runtime_info_item->data_type != data_type) {
234 LOGE("INVALID_PARAMETER(0x%08x) : invalid data type", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
235 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
238 get_value = runtime_info_item->get_value;
240 if (get_value == NULL) {
241 LOGE("IO_ERROR(0x%08x) : failed to call getter for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
242 return RUNTIME_INFO_ERROR_IO_ERROR;
245 ret = get_value(value);
246 if (ret != RUNTIME_INFO_ERROR_NONE)
247 LOGE("IO_ERROR(0x%08x) : failed to get the runtime informaion / key(%d)", ret, key);
252 API int runtime_info_get_value_int(runtime_info_key_e key, int *value)
255 runtime_info_value_u runtime_info_value;
258 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
259 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
262 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_INT, &runtime_info_value);
264 if (retcode == RUNTIME_INFO_ERROR_NONE)
265 *value = runtime_info_value.i;
270 API int runtime_info_get_value_bool(runtime_info_key_e key, bool *value)
273 runtime_info_value_u runtime_info_value;
276 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
277 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
280 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_BOOL, &runtime_info_value);
282 if (retcode == RUNTIME_INFO_ERROR_NONE)
283 *value = runtime_info_value.b;
288 API int runtime_info_get_value_double(runtime_info_key_e key, double *value)
291 runtime_info_value_u runtime_info_value;
294 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
295 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
298 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_DOUBLE, &runtime_info_value);
300 if (retcode == RUNTIME_INFO_ERROR_NONE)
301 *value = runtime_info_value.d;
306 API int runtime_info_get_value_string(runtime_info_key_e key, char **value)
309 runtime_info_value_u runtime_info_value;
312 LOGE("INVALID_PARAMETER(0x%08x) : invalid output param", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
313 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
316 retcode = runtime_info_get_value(key, RUNTIME_INFO_DATA_TYPE_STRING, &runtime_info_value);
318 if (retcode == RUNTIME_INFO_ERROR_NONE)
319 *value = runtime_info_value.s;
324 API int runtime_info_set_changed_cb(runtime_info_key_e key, runtime_info_changed_cb callback, void *user_data)
326 runtime_info_item_h runtime_info_item;
327 runtime_info_func_set_event_cb set_event_cb;
328 bool subscribe_event = false;
331 if (callback == NULL) {
332 LOGE("INVALID_PARAMETER(0x%08x)", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
333 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
336 if (runtime_info_get_item(key, &runtime_info_item)) {
337 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
338 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
341 set_event_cb = runtime_info_item->set_event_cb;
343 if (set_event_cb == NULL) {
344 LOGE("IO_ERROR(0x%08x) : failed to set callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
345 return RUNTIME_INFO_ERROR_IO_ERROR;
348 if (runtime_info_item->event_subscription == NULL) {
349 subscribe_event = true;
351 runtime_info_event_subscription_h event_subscription;
352 event_subscription = calloc(1, sizeof(runtime_info_event_subscription_s));
354 if (event_subscription == NULL) {
355 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
356 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
359 runtime_info_item->event_subscription = event_subscription;
362 runtime_info_item->event_subscription->changed_cb = callback;
363 runtime_info_item->event_subscription->user_data = user_data;
365 if (runtime_info_item->event_subscription->most_recent_value != NULL)
366 free(runtime_info_item->event_subscription->most_recent_value);
367 runtime_info_item->event_subscription->most_recent_value = NULL;
369 if (!subscribe_event)
370 return RUNTIME_INFO_ERROR_NONE;
372 ret = set_event_cb();
373 if (ret != RUNTIME_INFO_ERROR_NONE) {
374 _E("Failed to set event hadler (%d)", ret);
378 runtime_info_item->event_subscription->most_recent_value =
379 calloc(1, sizeof(runtime_info_value_u));
380 if (!runtime_info_item->event_subscription->most_recent_value) {
381 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
382 runtime_info_item->unset_event_cb();
383 return RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
386 ret = runtime_info_item->get_value(runtime_info_item->event_subscription->most_recent_value);
387 if (ret != RUNTIME_INFO_ERROR_NONE) {
388 LOGE("Failed to get current value (%d)", ret);
389 runtime_info_item->unset_event_cb();
390 free(runtime_info_item->event_subscription->most_recent_value);
391 runtime_info_item->event_subscription->most_recent_value = NULL;
395 return RUNTIME_INFO_ERROR_NONE;
398 API int runtime_info_unset_changed_cb(runtime_info_key_e key)
400 runtime_info_item_h runtime_info_item;
401 runtime_info_func_unset_event_cb unset_event_cb;
403 if (runtime_info_get_item(key, &runtime_info_item)) {
404 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
405 return RUNTIME_INFO_ERROR_INVALID_PARAMETER;
408 if (runtime_info_item->event_subscription != NULL) {
409 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
410 free(runtime_info_item->event_subscription->most_recent_value);
411 runtime_info_item->event_subscription->most_recent_value = NULL;
414 free(runtime_info_item->event_subscription);
415 runtime_info_item->event_subscription = NULL;
418 unset_event_cb = runtime_info_item->unset_event_cb;
420 if (unset_event_cb == NULL) {
421 LOGE("IO_ERROR(0x%08x) : failed to unset callback for the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
422 return RUNTIME_INFO_ERROR_IO_ERROR;
427 return RUNTIME_INFO_ERROR_NONE;
430 //LCOV_EXCL_START : Not called callback
431 void runtime_info_updated(runtime_info_key_e key)
433 runtime_info_item_h runtime_info_item;
434 runtime_info_value_u current_value;
435 bool dispatch_event = true;
438 if (runtime_info_get_item(key, &runtime_info_item)) {
439 LOGE("INVALID_PARAMETER(0x%08x) : invalid key", RUNTIME_INFO_ERROR_INVALID_PARAMETER);
443 if (runtime_info_item->event_subscription == NULL) {
444 LOGE("IO_ERROR(0x%08x) : invalid event subscription", RUNTIME_INFO_ERROR_IO_ERROR);
448 memset(¤t_value, 0, sizeof(runtime_info_value_u));
450 retcode = runtime_info_get_value(key, runtime_info_item->data_type, ¤t_value);
452 if (retcode != RUNTIME_INFO_ERROR_NONE) {
453 LOGE("IO_ERROR(0x%08x) : failed to get the runtime information", RUNTIME_INFO_ERROR_IO_ERROR);
457 if (runtime_info_item->event_subscription->most_recent_value != NULL) {
458 if (!memcmp(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u)))
459 dispatch_event = false;
461 runtime_info_item->event_subscription->most_recent_value = calloc(1, sizeof(runtime_info_value_u));
463 if (runtime_info_item->event_subscription->most_recent_value == NULL) {
464 LOGE("OUT_OF_MEMORY(0x%08x)", RUNTIME_INFO_ERROR_OUT_OF_MEMORY);
469 if (dispatch_event == true && runtime_info_item->event_subscription->changed_cb != NULL) {
470 memcpy(runtime_info_item->event_subscription->most_recent_value, ¤t_value, sizeof(runtime_info_value_u));
471 runtime_info_item->event_subscription->changed_cb(key, runtime_info_item->event_subscription->user_data);