Fix bug in dlog message format
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-util.c
index 10b67c1..5b80486 100644 (file)
@@ -3,7 +3,7 @@
 *
 * Copyright 2012  Samsung Electronics Co., Ltd
 
-* Licensed under the Flora License, Version 1.0 (the "License");
+* Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
@@ -61,7 +61,7 @@ char *wfd_app_trim_path(const char *filewithpath)
        const char *space = "                                        ";
        int len = strlen(filewithpath);
 
-       if (len > 20) {
+       if (len >= 20) {
                strptr = (char *) filewithpath + (len - 20);
                start = 0;
        } else if (len < 20) {
@@ -70,7 +70,8 @@ char *wfd_app_trim_path(const char *filewithpath)
        }
 
        strncpy((char *) filename, space, strlen(space));
-       strncpy((char *) filename + start, strptr, 50);
+       if(strptr)
+               strncpy((char *) filename + start, strptr, 50);
 
        return (char *) filename;
 #endif
@@ -91,7 +92,7 @@ static void __launch_app_result_cb(app_control_h request, app_control_h reply, a
 {
        __WFD_APP_FUNC_ENTER__;
 
-       if(result == APP_CONTROL_RESULT_FAILED) {
+       if (result == APP_CONTROL_RESULT_FAILED) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "User cancel to reconnect screen mirroring\n");
 #ifdef WFD_SCREEN_MIRRORING_ENABLED
                ad->screen_mirroring_state = WFD_POP_SCREEN_MIRROR_NONE;
@@ -112,14 +113,18 @@ static void _move_data_to_app_control(const char *key, const int type,
        size_t size = 0;
 
 
-       WFD_RET_IF(data == NULL || key == NULL || type == 0, "Invialid parameter!");
+       WFD_RET_IF(data == NULL || key == NULL || type == 0, "Invialid parameter!");
 
        app_control_h control = data;
 
-       if (type == BUNDLE_TYPE_STR)
-       {
+       if (type == BUNDLE_TYPE_STR) {
                bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
-               buff = malloc(sizeof(char)* size + 1);
+               buff = malloc(sizeof(char) * size + 1);
+               if (buff == NULL) {
+                       WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to allocate memory");
+                       __WFD_APP_FUNC_EXIT__;
+                       return;
+               }
                snprintf(buff, size + 1, "%s", ((char*) ptr));
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
 
@@ -127,7 +132,12 @@ static void _move_data_to_app_control(const char *key, const int type,
                free(buff);
        } else if (type == BUNDLE_TYPE_BYTE) {
                bundle_keyval_get_basic_val((bundle_keyval_t *) kv, &ptr, &size);
-               buff = malloc(sizeof(char)* size + 1);
+               buff = malloc(sizeof(char) * size + 1);
+               if (buff == NULL) {
+                       WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to allocate memory");
+                       __WFD_APP_FUNC_EXIT__;
+                       return;
+               }
                snprintf(buff, size + 1, "%s", ((char*) ptr));
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Found STR -KEY: %s, VAL: %s, SIZE: %d", key, buff, size);
 
@@ -171,11 +181,10 @@ static void _launch_app(char *app_id, void *data)
 
        char *launch_type = NULL;
        bundle_get_str(data, "-t", &launch_type);
-       if (!strcmp(launch_type, "reconnect_by_connecting_wifi_ap")) {
+       if (!strcmp(launch_type, "reconnect_by_connecting_wifi_ap"))
                ret = app_control_send_launch_request(control, __launch_app_result_cb, NULL);
-       } else {
+       else
                ret = app_control_send_launch_request(control, NULL, NULL);
-       }
 
        if (ret != APP_CONTROL_ERROR_NONE) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "app_control_send_launch_request() is failed : %d", ret);
@@ -202,7 +211,7 @@ void _add_screen_mirroring_activated_indicator(void *user_data)
                ad->noti_screen_mirroring_play = NULL;
        }
 
-       if(ad->noti_screen_mirroring_on != NULL) {
+       if (ad->noti_screen_mirroring_on != NULL) {
                noti_err = notification_free(ad->noti_screen_mirroring_on);
                WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
        }
@@ -251,7 +260,7 @@ void _add_wfd_peers_connected_notification(void *user_data, char* package_name)
                ad->noti_screen_mirroring_on = NULL;
        }
 
-       if(ad->noti_screen_mirroring_play != NULL) {
+       if (ad->noti_screen_mirroring_play != NULL) {
                noti_err = notification_free(ad->noti_screen_mirroring_play);
                WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
        }
@@ -366,16 +375,14 @@ static void _wfd_flight_mode_changed(keynode_t *node, void *user_data)
 #ifdef WFD_SCREEN_MIRRORING_ENABLED
        /* checking Screen Mirroring */
        if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
-       {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
-       }
+
        WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
 
-       if(screen_mirroring_status > VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) {
+       if (screen_mirroring_status > VCONFKEY_SCREEN_MIRRORING_DEACTIVATED) {
                res = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
-               if (res < 0) {
+               if (res < 0)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
-               }
        }
 #endif
 
@@ -401,13 +408,12 @@ static void _wfd_cpu_limit_mode_changed(keynode_t *node, void *user_data)
                return;
        }
 
-       if(vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
+       if (vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
                return;
        }
 
-       if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0)
-       {
+       if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
                return;
        }
@@ -444,8 +450,7 @@ static void _wfd_power_saving_mode_changed(keynode_t *node, void *user_data)
                return;
        }
 
-       if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0)
-       {
+       if (vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &power_mode) < 0) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PSMODE\n");
                return;
        }
@@ -461,7 +466,7 @@ static void _wfd_power_saving_mode_changed(keynode_t *node, void *user_data)
                bundle_free(b);
        } else if (power_mode == SETTING_PSMODE_POWERFUL ||
                        power_mode == SETTING_PSMODE_ADVISOR){
-                       if(vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
+                       if (vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, &cup_limit_mode) < 0) {
                                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU\n");
                                return;
                        }
@@ -499,8 +504,7 @@ static void _wfd_wifi_status_changed(keynode_t *node, void *user_data)
        }
 #endif
 
-       if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_status) < 0)
-       {
+       if (vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_status) < 0) {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_WIFI_STATE\n");
                return;
        }
@@ -552,9 +556,7 @@ static void _wfd_allshare_cast_status_changed(keynode_t *node, void *user_data)
        WFD_RET_IF(NULL == ad, "NULL parameters.\n");
 
        if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
-       {
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
-       }
 
        WFD_APP_LOG(WFD_APP_LOG_ERROR, "VCONFKEY_SCREEN_MIRRORING_STATE:%d\n", screen_mirroring_status);
 
@@ -608,9 +610,8 @@ static Eina_Bool _wfd_hard_key_down_cb(void *data, int type, void *event)
 
                if (strnlen(ad->mac_addr_connecting, MACSTR_LENGTH) > 0) {
                        res = wifi_direct_reject_connection(ad->mac_addr_connecting);
-                       if (res != WIFI_DIRECT_ERROR_NONE) {
+                       if (res != WIFI_DIRECT_ERROR_NONE)
                                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to reject connection(%d)", res);
-                       }
                }
                memset(ad->mac_addr_connecting, 0x00, MACSTR_LENGTH);
                wfd_destroy_popup();
@@ -716,35 +717,30 @@ int wfd_app_util_deregister_vconf_callbacks(void *data)
 
        /* remove callback for flight mode */
        ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE, _wfd_flight_mode_changed);
-       if (ret == -1) {
+       if (ret == -1)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for flight mode\n");
-       }
 
 #ifdef WFD_SCREEN_MIRRORING_ENABLED
        /* remove callback for allshare cast */
        ret = vconf_ignore_key_changed(VCONFKEY_SCREEN_MIRRORING_STATE, _wfd_allshare_cast_status_changed);
-       if (ret == -1) {
+       if (ret == -1)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for allshare cast\n");
-       }
 #endif
 
        /* remove callback for wifi */
        ret = vconf_ignore_key_changed(VCONFKEY_WIFI_STATE, _wfd_wifi_status_changed);
-       if (ret == -1) {
+       if (ret == -1)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for wifi\n");
-       }
 
        /* remove callback for power mode */
        ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, _wfd_power_saving_mode_changed);
-       if (ret == -1) {
+       if (ret == -1)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for power mode\n");
-       }
 
        /* remove callback for cpu limit mode */
        ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU, _wfd_cpu_limit_mode_changed);
-       if (ret == -1) {
+       if (ret == -1)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to ignore vconf key callback for cpu limit mode\n");
-       }
 
        return 0;
 }
@@ -771,17 +767,15 @@ void wfd_app_util_del_notification(wfd_appdata_t *ad)
        if (ad->noti_screen_mirroring_on) {
                noti_err = notification_free(ad->noti_screen_mirroring_on);
                ad->noti_screen_mirroring_on = NULL;
-               if (noti_err != NOTIFICATION_ERROR_NONE) {
+               if (noti_err != NOTIFICATION_ERROR_NONE)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
-               }
        }
 
        if (ad->noti_screen_mirroring_play) {
                noti_err = notification_free(ad->noti_screen_mirroring_play);
                ad->noti_screen_mirroring_play = NULL;
-               if (noti_err != NOTIFICATION_ERROR_NONE) {
+               if (noti_err != NOTIFICATION_ERROR_NONE)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
-               }
        }
 #endif
 
@@ -789,18 +783,16 @@ void wfd_app_util_del_notification(wfd_appdata_t *ad)
        if (ad->noti_wifi_direct_on) {
                noti_err = notification_free(ad->noti_wifi_direct_on);
                ad->noti_wifi_direct_on = NULL;
-               if (noti_err != NOTIFICATION_ERROR_NONE) {
+               if (noti_err != NOTIFICATION_ERROR_NONE)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
-               }
        }
 #endif
 
        if (ad->noti_wifi_direct_connected) {
                noti_err = notification_free(ad->noti_wifi_direct_connected);
                ad->noti_wifi_direct_connected = NULL;
-               if (noti_err != NOTIFICATION_ERROR_NONE) {
+               if (noti_err != NOTIFICATION_ERROR_NONE)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
-               }
        }
 
        __WFD_APP_FUNC_EXIT__;
@@ -820,7 +812,7 @@ void wfd_app_util_add_indicator_icon(void *user_data)
        notification_error_e noti_err = NOTIFICATION_ERROR_NONE;
        WFD_RET_IF(ad->noti_wifi_direct_on, "Indicator already exists");
 
-       if(ad->noti_wifi_direct_on != NULL) {
+       if (ad->noti_wifi_direct_on != NULL) {
                noti_err = notification_free(ad->noti_wifi_direct_on);
                WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
        }
@@ -864,18 +856,17 @@ void wfd_app_util_set_screen_mirroring_deactivated(wfd_appdata_t *ad)
                return;
        }
 
-       if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0) {
+       if (vconf_get_int(VCONFKEY_SCREEN_MIRRORING_STATE, &screen_mirroring_status) < 0)
                WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to get vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
-       }
 
        WFD_APP_LOG(WFD_APP_LOG_LOW, "screen_mirroring_status: %d\n", screen_mirroring_status);
        /* Set the vconf value to DEACTIVATED only when the previous vconf value is CONNECTED.
        If the previous vconf value is ACTIVATED, it means that the Screen Mirroring UG changed that key already. So no need to change it. */
-       if(screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
+       if (screen_mirroring_status == VCONFKEY_SCREEN_MIRRORING_CONNECTED) {
                result = vconf_set_int(VCONFKEY_SCREEN_MIRRORING_STATE, VCONFKEY_SCREEN_MIRRORING_DEACTIVATED);
-               if (result < 0) {
+               if (result < 0)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to set vconf VCONFKEY_SCREEN_MIRRORING_STATE\n");
-               }
+
                notification_status_message_post(D_("IDS_SMR_POP_SCREEN_MIRRORING_HAS_BEEN_DISABLED"));
        }
 
@@ -900,7 +891,7 @@ void wfd_app_util_add_wfd_turn_off_notification(void *user_data)
        /* delete all notifications */
        wfd_app_util_del_notification(ad);
 
-       if(ad->noti_wifi_direct_connected!= NULL) {
+       if (ad->noti_wifi_direct_connected != NULL) {
                noti_err = notification_free(ad->noti_wifi_direct_connected);
                WFD_RET_IF(noti_err != NOTIFICATION_ERROR_NONE, "Failed to notification_free. [%d]", noti_err);
        }
@@ -974,9 +965,8 @@ void wfd_app_util_del_wfd_connected_notification(wfd_appdata_t *ad)
        if (ad->noti_wifi_direct_connected) {
                noti_err = notification_free(ad->noti_wifi_direct_connected);
                ad->noti_wifi_direct_connected = NULL;
-               if (noti_err != NOTIFICATION_ERROR_NONE) {
+               if (noti_err != NOTIFICATION_ERROR_NONE)
                        WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to notification_free.(%d)\n", noti_err);
-               }
        }
 
        __WFD_APP_FUNC_EXIT__;