Enhance debug message
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-event-receiver.c
index 2058084..c8a0731 100644 (file)
@@ -39,6 +39,7 @@
 #include "bt-service-headset-connection.h"
 #include "bt-service-avrcp-controller.h"
 #include "bt-service-hid.h"
+#include "bt-service-proximity.h"
 
 #include "bt-service-opp-client.h"
 
@@ -216,7 +217,7 @@ static gboolean __bt_parse_device_properties(GVariant *item,
                                }
                                g_free(uuid_value);
                        }
-               } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
                        g_variant_get(val, "q", &dev_info->manufacturer_data_len);
                        if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
                                BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
@@ -225,7 +226,7 @@ static gboolean __bt_parse_device_properties(GVariant *item,
 
                        if (dev_info->manufacturer_data_len == 0)
                                dev_info->manufacturer_data = g_strdup("");
-               } else if (strcasecmp(key, "ManufacturerData") == 0) {
+               } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
                        int i = 0;
                        int len = 0;
                        GVariantIter *value_iter;
@@ -293,7 +294,7 @@ static gboolean __bt_parse_interface(GVariant *msg,
 static int __bt_get_owner_info(GVariant *msg, char **name,
                                char **previous, char **current)
 {
-       g_variant_get(msg, "(sss)", name, previous, current);
+       g_variant_get(msg, "(&s&s&s)", name, previous, current);
        return BLUETOOTH_ERROR_NONE;
 }
 
@@ -467,8 +468,6 @@ void _bt_handle_adapter_event(GVariant *msg, const char *member)
 
                /* Bonding from remote device */
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               if (address == NULL)
-                       return;
 
                g_variant_get(msg, "(&o)", &object_path);
 
@@ -576,13 +575,13 @@ void _bt_handle_adapter_event(GVariant *msg, const char *member)
                                param);
                g_free(address);
        } else if (strcasecmp(member, BT_HARDWARE_ERROR) == 0) {
-               BT_ERR_C("Hardware error received from BLUEZ");
+               BT_ERR_C("### Hardware error received from BLUEZ");
 /* Don't add the recovery logic into platform */
 #if 0
                _bt_recover_adapter();
 #endif
        } else if (strcasecmp(member, BT_TX_TIMEOUT_ERROR) == 0) {
-               BT_ERR_C("Tx timeout error received from BLUEZ");
+               BT_ERR_C("### Tx timeout error received from BLUEZ");
 /* Don't add the recovery logic into platform */
 #if 0
                _bt_recover_adapter();
@@ -602,15 +601,21 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
        GError *err = NULL;
        char *property = NULL;
        GVariant *param = NULL;
+
        g_variant_iter_init(&value_iter, msg);
        while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
                BT_INFO("Property %s", property);
 
                if (strcasecmp(property, "Discovering") == 0) {
                        gboolean discovering = FALSE;
+
+                       if (_bt_adapter_get_status() != BT_ACTIVATED)
+                               continue;
+
                        g_variant_get(val, "b", &discovering);
-                       /* Send event to application */
                        BT_DBG("Discovering %d", discovering);
+
+                       /* Send event to application */
                        if (discovering == TRUE) {
                                _bt_set_discovery_status(TRUE);
                                param = g_variant_new("(i)", result);
@@ -618,10 +623,12 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                        BLUETOOTH_EVENT_DISCOVERY_STARTED,
                                        param);
                        } else {
-                               ret_if(event_id > 0);
+                               if (event_id > 0)
+                                       continue;
 
                                adapter_proxy = _bt_get_adapter_proxy();
-                               ret_if(adapter_proxy == NULL);
+                               if (adapter_proxy == NULL)
+                                       continue;
 
                                /* Need to stop searching */
                                ret = g_dbus_proxy_call_sync(adapter_proxy, "StopDiscovery",
@@ -642,7 +649,13 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                } else if (strcasecmp(property, "LEDiscovering") == 0) {
                        gboolean le_discovering = FALSE;
 
+                       if (_bt_adapter_get_status() != BT_ACTIVATED ||
+                           _bt_adapter_get_le_status() != BT_LE_ACTIVATED)
+                               continue;
+
                        g_variant_get(val, "b", &le_discovering);
+                       BT_DBG("LEDiscovering %d", le_discovering);
+
                        /* Send event to application */
                        if (le_discovering == TRUE) {
                                _bt_set_le_scan_status(TRUE);
@@ -651,10 +664,12 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                BLUETOOTH_EVENT_LE_DISCOVERY_STARTED,
                                param);
                        } else {
-                               ret_if(event_id > 0);
+                               if (event_id > 0)
+                                       continue;
 
                                adapter_proxy = _bt_get_adapter_proxy();
-                               ret_if(adapter_proxy == NULL);
+                               if (adapter_proxy == NULL)
+                                       continue;
 
                                /* Need to stop searching */
                                ret = g_dbus_proxy_call_sync(adapter_proxy, "StopLEDiscovery",
@@ -674,22 +689,20 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                }
                } else if (strcasecmp(property, "Name") == 0) {
                        char *name = NULL;
-                       g_variant_get(val, "s", &name);
+                       g_variant_get(val, "&s", &name);
                        param = g_variant_new("(is)", result, name);
                        /* Send event to application */
                        _bt_send_event(BT_ADAPTER_EVENT,
                                BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
                                param);
-                       g_free(name);
                } else if (strcasecmp(property, "Alias") == 0) {
                        char *alias = NULL;
-                       g_variant_get(val, "s", &alias);
+                       g_variant_get(val, "&s", &alias);
                        param = g_variant_new("(is)", result, alias);
                        /* Send event to application */
                        _bt_send_event(BT_ADAPTER_EVENT,
                                BLUETOOTH_EVENT_LOCAL_NAME_CHANGED,
                                param);
-                       g_free(alias);
                } else if (strcasecmp(property, "Discoverable") == 0) {
                        gboolean discoverable = FALSE;
 
@@ -698,20 +711,23 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
 
                        if (discoverable == FALSE) {
                                if (_bt_get_discoverable_timeout_property() > 0) {
-                                       int time = 0;
+                                       unsigned int time = 0;
+
                                        adapter_proxy = _bt_get_adapter_properties_proxy();
-                                       ret_if(adapter_proxy == NULL);
+                                       if (adapter_proxy == NULL)
+                                               continue;
+
                                        ret = g_dbus_proxy_call_sync(adapter_proxy, "Set",
                                                g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
                                                "DiscoverableTimeout",
-                                               g_variant_new("i", time)),
+                                               g_variant_new("u", time)),
                                                G_DBUS_CALL_FLAGS_NONE,
                                                DBUS_TIMEOUT, NULL,
                                                &err);
 
                                        if (err != NULL) {
-                                               BT_ERR("StopLEDiscovery Failed: %s", err->message);
-                                               g_error_free(err);
+                                               BT_ERR("Set DiscoverableTimeout Failed: %s", err->message);
+                                               g_clear_error(&err);
                                        }
                                        if (ret)
                                                g_variant_unref(ret);
@@ -720,7 +736,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
 
                                /* Send event to application */
-                               BT_INFO("[Connectable]");
+                               BT_INFO("[Non Discoverable]");
                                param = g_variant_new("(in)", result, mode);
                                _bt_send_event(BT_ADAPTER_EVENT,
                                        BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
@@ -747,11 +763,8 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
 
                        /* Event was already sent by "Discoverable" signal */
                        if (mode == BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE ||
-                               mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE) {
-                               g_free(property);
-                               g_variant_unref(val);
-                               return;
-                       }
+                           mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
+                               continue;
 
                        /* Send event to application */
                        BT_INFO("[Limited Discoverable (Timeout %u secs)]",
@@ -761,30 +774,36 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                BLUETOOTH_EVENT_DISCOVERABLE_MODE_CHANGED,
                                param);
                } else if (strcasecmp(property, "Powered") == 0) {
-                       /* TODO: Need to check this operation!! */
                        gboolean powered = FALSE;
-                       int bt_state;
+                       int bt_state = VCONFKEY_BT_STATUS_OFF;
+                       int le_state = VCONFKEY_BT_LE_STATUS_OFF;
 
                        g_variant_get(val, "b", &powered);
                        BT_DBG("Powered = %d", powered);
-                       if (powered == FALSE) {
-#ifdef USB_BLUETOOTH
-                               _bt_handle_adapter_removed();
-#else
-                               if (vconf_get_int(VCONFKEY_BT_STATUS, &bt_state) == 0 &&
-                               bt_state != VCONFKEY_BT_STATUS_OFF) {
-                                       _bt_disable_adapter();
-                               }
-#endif
-                               if (vconf_get_int(VCONFKEY_BT_LE_STATUS, &bt_state) == 0 &&
-                                       bt_state != VCONFKEY_BT_LE_STATUS_OFF) {
-                                       _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
-                               }
-                       } else {
+
 #ifdef USB_BLUETOOTH
+                       if (powered) {
                                _bt_handle_adapter_added();
-#endif
+                               continue;
                        }
+
+                       _bt_handle_adapter_removed();
+
+                       vconf_get_int(VCONFKEY_BT_LE_STATUS, &le_state);
+                       if (le_state != VCONFKEY_BT_LE_STATUS_OFF)
+                               _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
+#else
+                       if (powered)
+                               continue;
+
+                       vconf_get_int(VCONFKEY_BT_STATUS, &bt_state);
+                       if (bt_state != VCONFKEY_BT_STATUS_OFF)
+                               _bt_disable_adapter();
+
+                       vconf_get_int(VCONFKEY_BT_LE_STATUS, &le_state);
+                       if (le_state != VCONFKEY_BT_LE_STATUS_OFF)
+                               _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
+#endif
                } else if (strcasecmp(property, "Connectable") == 0) {
                        gboolean connectable = FALSE;
 
@@ -805,16 +824,18 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                        char *name = NULL;
                        char *value = NULL;
                        GVariantIter *iter = NULL;
+
                        g_variant_get(val, "as", &iter);
-                       while (g_variant_iter_loop(iter, "s", &name)) {
-                               BT_DBG("name = %s", name);
-                               g_variant_iter_loop(iter, "s", &value);
-                               BT_DBG("Value = %s", value);
-                               if (FALSE == _bt_update_le_feature_support(name, value))
-                                       BT_INFO("Fail to update LE feature info");
+                       if (iter == NULL)
+                               continue;
+
+                       while (g_variant_iter_next(iter, "&s", &name) &&
+                              g_variant_iter_next(iter, "&s", &value)) {
+                               BT_DBG("name = %s, Value = %s", name, value);
+                               if (!_bt_update_le_feature_support(name, value))
+                                       BT_ERR("Failed to update LE feature (name = %s, value = %s)", name, value);
                        }
-                       if (iter)
-                               g_variant_iter_free(iter);
+                       g_variant_iter_free(iter);
                } else if (strcasecmp(property, "IpspInitStateChanged") == 0) {
                        gboolean ipsp_initialized = FALSE;
 
@@ -827,7 +848,7 @@ static void __bt_adapter_property_changed_event(GVariant *msg, const char *path)
                                        BLUETOOTH_EVENT_IPSP_INIT_STATE_CHANGED,
                                        param);
                } else {
-                       BT_DBG("property : [%s]", property);
+                       BT_DBG("Unhandled property : [%s]", property);
                }
        }
 }
@@ -1063,17 +1084,9 @@ static void __bt_device_property_changed_event(GVariant *msg, const char *path)
                                char secure_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
 
                                _bt_convert_addr_string_to_secure_string(secure_addr, address);
-                               BT_INFO("### Paired: %s", secure_addr);
+                               BT_INFO(" ### Paired: %s", secure_addr);
                                __bt_update_remote_cache_devinfo(address, TRUE);
 
-                               if (_bt_is_device_creating() == TRUE) {
-                                       BT_DBG("Try to Pair by me");
-                                       _bt_free_device_info(remote_dev_info);
-                                       g_free(address);
-                                       g_free(property);
-                                       g_variant_unref(val);
-                                       return;
-                               }
                                GVariant *uuids = NULL;
                                GVariantBuilder *builder = NULL;
                                int i = 0;
@@ -1280,6 +1293,70 @@ static void __bt_media_control_changed_event(GVariant *msg, const char *path)
        BT_DBG("-");
 }
 
+int get_alert_level_enum(const char *alert)
+{
+       int lvl = -1;
+
+       if (strcmp(alert, "none") == 0)
+               lvl = BT_PXP_ALERT_NONE;
+       else if (strcmp(alert, "mild") == 0)
+               lvl = BT_PXP_ALERT_MILD;
+       else if (strcmp(alert, "high") == 0)
+               lvl = BT_PXP_ALERT_HIGH;
+
+       return lvl;
+}
+
+static void _bt_handle_pxp_property_changed_event(GVariant *msg, const char *path, int role)
+{
+       int result = BLUETOOTH_ERROR_NONE;
+       int service_type;
+       int alert_lvl;
+       GVariantIter value_iter;
+       char *property = NULL;
+       char *address;
+       GVariant *val = NULL;
+       GVariant *child = NULL;
+       GVariant *param = NULL;
+       g_variant_iter_init(&value_iter, msg);
+
+       BT_DBG("+");
+       while ((child = g_variant_iter_next_value(&value_iter))) {
+               g_variant_get(child, "{sv}", &property, &val);
+               BT_INFO("Property %s", property);
+
+               if ((strcasecmp(property, "LinkLossAlertLevel") == 0) ||
+                       (strcasecmp(property, "ImmediateAlertLevel") == 0)) {
+                       char *alert_str = NULL;
+
+                       address = g_malloc0(BT_ADDRESS_STRING_SIZE);
+                       _bt_convert_device_path_to_address(path, address);
+
+                       if (strcasecmp(property, "LinkLossAlertLevel") == 0)
+                               service_type = BT_PXP_PROPERTY_LLS;
+                       else
+                               service_type = BT_PXP_PROPERTY_IAS;
+
+                       g_variant_get(val, "s", &alert_str);
+                       alert_lvl = get_alert_level_enum(alert_str);
+
+                       param = g_variant_new("(isiii)", result, address,
+                                                               role, service_type, alert_lvl);
+
+                       /* Send event to application */
+                       _bt_send_event(BT_DEVICE_EVENT,
+                                       BLUETOOTH_EVENT_PXP_PROPERTY_CHANGED,
+                                       param);
+                       g_free(address);
+                       g_free(alert_str);
+               }
+               g_free(property);
+               g_variant_unref(child);
+               g_variant_unref(val);
+       }
+       BT_DBG("-");
+}
+
 void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
 {
        char *interface_name = NULL;
@@ -1309,6 +1386,14 @@ void _bt_handle_property_changed_event(GVariant *msg, const char *object_path)
        } else if (strcasecmp(interface_name, BT_GATT_CHAR_INTERFACE) == 0) {
                __bt_gatt_char_property_changed_event(val,
                                        object_path);
+       } else if (strcasecmp(interface_name, BT_PROXIMITY_REPORTER_INTERFACE) == 0) {
+               BT_DBG("BT_PROXIMITY_REPORTER_INTERFACE");
+               _bt_handle_pxp_property_changed_event(val,
+                                       object_path, BT_PXP_REPORTER_ROLE);
+       } else if (strcasecmp(interface_name, BT_PROXIMITY_MONITOR_INTERFACE) == 0) {
+               BT_DBG("BT_PROXIMITY_MONITOR_INTERFACE");
+               _bt_handle_pxp_property_changed_event(val,
+                                       object_path, BT_PXP_MONITOR_ROLE);
        }
        g_variant_unref(val);
 }
@@ -1526,17 +1611,14 @@ void __bt_gatt_char_property_changed_event(GVariant *msg,
        g_variant_iter_init(&value_iter, msg);
        char_handle = g_strdup(path);
        while ((g_variant_iter_loop(&value_iter, "{sv}", &property, &val))) {
-               BT_INFO("Property %s", property);
+               BT_DBG("Property %s", property);
 
                ret_if(property == NULL);
 
                if (strcasecmp(property, "Notifying") == 0) {
                        gboolean property_flag = FALSE;
                        g_variant_get(val, "b", &property_flag);
-                       if (property_flag == TRUE)
-                               BT_DBG("notifying is enabled");
-                       else
-                               BT_DBG("notifying is disabled");
+                       BT_INFO("Notifying is %s", property_flag ? "enabled" : "disabled");
                } else if (strcasecmp(property, "ChangedValue") == 0) {
                        int len = 0;
                        GByteArray *gp_byte_array = NULL;
@@ -1625,7 +1707,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        gboolean connected = FALSE;
                        char *address;
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
                        g_variant_get(msg, "(b)", &connected);
@@ -1658,7 +1739,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        }
 
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
 
@@ -1709,7 +1789,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                        ret_if(_bt_is_device_creating() == TRUE);
 
                        address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-                       ret_if(address == NULL);
 
                        _bt_convert_device_path_to_address(path, address);
 
@@ -1759,10 +1838,9 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                g_variant_get(msg, "(y)", &addr_type);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
-               dev_name = _bt_get_bonded_device_name(address);
+               dev_name = _bt_get_device_name(address);
 
                _bt_convert_addr_string_to_secure_string(secure_address, address);
                BT_INFO("Address : %s Type : %d", secure_address, addr_type);
@@ -1770,6 +1848,9 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                                !addr_type ? dev_name : secure_address);
                g_free(dev_name);
 
+               if (addr_type)
+                       _bt_add_le_connected_dev_info(address);
+
                _bt_logging_connection(TRUE, addr_type);
                param = g_variant_new("(isy)", result, address, addr_type);
                /*Send event to application*/
@@ -1788,7 +1869,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                result = disc_reason;
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
 
@@ -1797,15 +1877,17 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                      0x02 BDADDR_LE_RANDOM */
                _bt_convert_addr_string_to_secure_string(secure_address, address);
                BT_INFO("Address : %s Type : %d", secure_address, addr_type);
-               BT_ERR_C("### Disconnected [%s] [%d : %s] [%s]", !addr_type ? "BREDR" : "LE",
-                               disc_reason, _bt_convert_disc_reason_to_string(disc_reason),
-                               !addr_type ? dev_name : secure_address);
+               BT_ERR_C("### Disconnected [%s] [%s] [%d : %s]", !addr_type ? "BREDR" : "LE",
+                               !addr_type ? dev_name : secure_address,
+                               disc_reason, _bt_convert_disc_reason_to_string(disc_reason));
                g_free(dev_name);
 
                _bt_headset_set_local_connection(FALSE);
                _bt_logging_connection(FALSE, addr_type);
 
-               if (!addr_type) {
+               if (addr_type)
+                       _bt_remove_le_connected_dev_info(address);
+               else {
 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
                        {
                                int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
@@ -1854,105 +1936,104 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                g_variant_get(msg, "(si)", &profile_uuid, &state);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               ret_if(address == NULL);
 
                _bt_convert_device_path_to_address(path, address);
                _bt_convert_addr_string_to_type(bd_addr.addr, address);
 
                _bt_convert_addr_string_to_secure_string(secure_address, address);
-               BT_DBG("Address: %s", secure_address);
-               BT_DBG("Profile UUID: %s", profile_uuid);
-               BT_DBG("State: %d", state);
+               BT_INFO_C("[%s] state: %s, addr: %s",
+                       _bt_convert_uuid_to_string(profile_uuid),
+                       _bt_convert_profile_state_to_string(state), secure_address);
 
-               if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
-                       (state == BT_PROFILE_STATE_CONNECTED)) {
+               if (strcmp(profile_uuid, A2DP_SINK_UUID) == 0) {
+                       if (state == BT_PROFILE_STATE_CONNECTED) {
 
-                       int event = BLUETOOTH_EVENT_AV_CONNECTED;
-                       char connected_address[BT_ADDRESS_STRING_SIZE + 1];
-                       bluetooth_device_address_t device_address;
-                       gboolean connected;
-                       bt_headset_wait_t *wait_list;
-                       guint restricted = 0x0;
+                               int event = BLUETOOTH_EVENT_AV_CONNECTED;
+                               char connected_address[BT_ADDRESS_STRING_SIZE + 1];
+                               bluetooth_device_address_t device_address;
+                               gboolean connected;
+                               bt_headset_wait_t *wait_list;
+                               guint restricted = 0x0;
 
-                       __bt_set_device_values(TRUE,
-                               VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
+                               __bt_set_device_values(TRUE,
+                                       VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
 
-                       __bt_connection_manager_set_state(address, event);
+                               __bt_connection_manager_set_state(address, event);
 
-                       _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
+                               _bt_get_restrict_profile(&bd_addr, RESTRICTED_PROFILE_HFP_HS, &restricted);
 
-                       if (_bt_headset_get_local_connection() == FALSE) {
-                               if (restricted == 0x0) /* not restricted*/
-                                       _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
-                       } else {
-                               /* Connection Started from local device therefore no need to
-                                * intiate connection for pending profile */
-                               _bt_headset_set_local_connection(FALSE);
-                       }
-                       param = g_variant_new("(is)", result, address);
-                       _bt_send_event(BT_HEADSET_EVENT, event,
-                               param);
-                       connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
-                                               connected_address);
-                       if (connected) {
-                               if (g_strcmp0(connected_address, address) != 0) {
-                                       _bt_convert_addr_string_to_type(
-                                               device_address.addr,
-                                               connected_address);
-                                       _bt_audio_disconnect(0, BT_AUDIO_A2DP,
-                                               &device_address, NULL);
+                               if (_bt_headset_get_local_connection() == FALSE) {
+                                       if (restricted == 0x0) /* not restricted*/
+                                               _bt_start_timer_for_connection(address, BT_AUDIO_HSP);
+                               } else {
+                                       /* Connection Started from local device therefore no need to
+                                        * intiate connection for pending profile */
+                                       _bt_headset_set_local_connection(FALSE);
+                               }
+                               param = g_variant_new("(is)", result, address);
+                               _bt_send_event(BT_HEADSET_EVENT, event,
+                                       param);
+                               connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP,
+                                                       connected_address);
+                               if (connected) {
+                                       if (g_strcmp0(connected_address, address) != 0) {
+                                               _bt_convert_addr_string_to_type(
+                                                       device_address.addr,
+                                                       connected_address);
+                                               _bt_audio_disconnect(0, BT_AUDIO_A2DP,
+                                                       &device_address, NULL);
+                                       }
                                }
-                       }
 
-                       _bt_add_headset_to_list(BT_AUDIO_A2DP,
-                                               BT_STATE_CONNECTED, address);
+                               _bt_add_headset_to_list(BT_AUDIO_A2DP,
+                                                       BT_STATE_CONNECTED, address);
 
-                       wait_list = _bt_get_audio_wait_data();
-                       if (wait_list != NULL &&
-                               (g_strcmp0(wait_list->address, address) == 0))
-                               _bt_rel_wait_data();
+                               wait_list = _bt_get_audio_wait_data();
+                               if (wait_list != NULL &&
+                                       (g_strcmp0(wait_list->address, address) == 0))
+                                       _bt_rel_wait_data();
 
-               } else if ((strcmp(profile_uuid, A2DP_SINK_UUID) == 0)  &&
-                       (state == BT_PROFILE_STATE_DISCONNECTED)) {
+                       } else if (state == BT_PROFILE_STATE_DISCONNECTED) {
 
-                       int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
+                               int event = BLUETOOTH_EVENT_AV_DISCONNECTED;
 
-                       if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
-                               g_free(address);
-                               g_free(profile_uuid);
-                               return;
-                       }
+                               if (!_bt_is_service_connected(address, BT_AUDIO_A2DP)) {
+                                       g_free(address);
+                                       g_free(profile_uuid);
+                                       return;
+                               }
 
-                       __bt_set_device_values(FALSE,
-                               VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
+                               __bt_set_device_values(FALSE,
+                                       VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED);
 
-                       __bt_connection_manager_set_state(address, event);
-                       param = g_variant_new("(is)", result, address);
-                       _bt_send_event(BT_HEADSET_EVENT, event,
-                               param);
-                       /* Remove data from the connected list */
-                       _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
-                       wait_list = _bt_get_audio_wait_data();
+                               __bt_connection_manager_set_state(address, event);
+                               param = g_variant_new("(is)", result, address);
+                               _bt_send_event(BT_HEADSET_EVENT, event,
+                                       param);
+                               /* Remove data from the connected list */
+                               _bt_remove_headset_from_list(BT_AUDIO_A2DP, address);
+                               wait_list = _bt_get_audio_wait_data();
 
-                       if (wait_list == NULL) {
-                               g_free(address);
-                               g_free(profile_uuid);
-                               return;
-                       }
+                               if (wait_list == NULL) {
+                                       g_free(address);
+                                       g_free(profile_uuid);
+                                       return;
+                               }
 
-                       if (((wait_list->type == BT_AUDIO_ALL) &&
-                               (wait_list->ag_flag == TRUE)) ||
-                               (wait_list->type == BT_AUDIO_A2DP) ||
-                               (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
-                               bluetooth_device_address_t device_address;
-                               _bt_convert_addr_string_to_type(
-                                                       device_address.addr,
-                                                       wait_list->address);
+                               if (((wait_list->type == BT_AUDIO_ALL) &&
+                                       (wait_list->ag_flag == TRUE)) ||
+                                       (wait_list->type == BT_AUDIO_A2DP) ||
+                                       (wait_list->disconnection_type == BT_AUDIO_A2DP)) {
+                                       bluetooth_device_address_t device_address;
+                                       _bt_convert_addr_string_to_type(
+                                                               device_address.addr,
+                                                               wait_list->address);
 
-                               _bt_audio_connect(wait_list->req_id,
-                                                       wait_list->type,
-                                                       &device_address,
-                                                       NULL);
+                                       _bt_audio_connect(wait_list->req_id,
+                                                               wait_list->type,
+                                                               &device_address,
+                                                               NULL);
+                               }
                        }
                } else if (strcmp(profile_uuid, AVRCP_TARGET_UUID) == 0) {
 
@@ -2069,15 +2150,18 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                ret_if(_bt_is_le_scanning() == FALSE);
 
                le_dev_info = g_malloc0(sizeof(bt_remote_le_dev_info_t));
-               if (le_dev_info == NULL)
-                       return;
 
-               g_variant_get(msg, "(syyii@ay)", &le_dev_info->address,
+               g_variant_get(msg, "(&syyii@ay)", &le_dev_info->address,
                                                &le_dev_info->addr_type,
                                                &le_dev_info->adv_type,
                                                &le_dev_info->rssi,
                                                &le_dev_info->adv_data_len,
                                                &value);
+               if (value == NULL) {
+                       _bt_free_le_device_info(le_dev_info);
+                       return;
+               }
+
                _bt_convert_device_path_to_address(path, le_dev_info->address);
 
                buffer_len = g_variant_get_size(value);
@@ -2101,21 +2185,13 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
 
                if (le_dev_info->adv_type != BT_LE_ADV_SCAN_RSP) {       /* ADV_IND */
                        adv_info = g_malloc0(sizeof(bt_le_adv_info_t));
-                       if (adv_info == NULL) {
-                               _bt_free_le_device_info(le_dev_info);
-                               g_variant_unref(value);
-                               return;
-                       }
-
                        adv_info->addr = g_strdup(le_dev_info->address);
                        adv_info->addr_type = le_dev_info->addr_type;
                        adv_info->rssi = le_dev_info->rssi;
                        adv_info->data_len = le_dev_info->adv_data_len;
                        adv_info->data = g_malloc0(le_dev_info->adv_data_len);
-                       if (adv_info->data) {
-                               memcpy(adv_info->data, le_dev_info->adv_data,
-                                               le_dev_info->adv_data_len);
-                       }
+                       memcpy(adv_info->data, le_dev_info->adv_data,
+                                       le_dev_info->adv_data_len);
 
                        if (__bt_add_adv_ind_info(adv_info) == 0) {
                                adv_info->timer_id = g_timeout_add(1000,
@@ -2175,8 +2251,6 @@ void _bt_handle_device_event(GVariant *msg, const char *member, const char *path
                ret_if(_bt_is_le_scanning() == FALSE);
 
                ibeacon_dev_info = g_malloc0(sizeof(bt_remote_ibeacon_dev_info_t));
-               if (ibeacon_dev_info == NULL)
-                       return;
 
                g_variant_get(msg, "(syuusuuy)", &ibeacon_dev_info->address,
                                                &ibeacon_dev_info->addr_type,
@@ -2198,7 +2272,7 @@ void __bt_set_audio_values(gboolean connected, char *address)
 
        /*  Set the headset name */
        if (connected == TRUE)
-               name = _bt_get_bonded_device_name(address);
+               name = _bt_get_device_name(address);
        else
                name = g_strdup("");
 
@@ -2254,8 +2328,6 @@ void _bt_handle_headset_event(GVariant *msg, const char *path)
                g_variant_get(value, "b", &property_flag);
 
                address = g_malloc0(BT_ADDRESS_STRING_SIZE);
-               if (address == NULL)
-                       return;
 
                _bt_convert_device_path_to_address(path, address);
                _bt_convert_addr_string_to_type(bd_addr.addr, address);
@@ -2464,7 +2536,7 @@ void _bt_handle_sink_event(GVariant *msg, const char *path)
                                if (connected) {
                                        /* Match connected device address */
                                        if (g_strcmp0(connected_address, address) != 0) {
-                                               /* Convert BD adress from string type */
+                                               /* Convert BD address from string type */
                                                _bt_convert_addr_string_to_type(
                                                                device_address.addr,
                                                                connected_address);
@@ -2498,12 +2570,10 @@ void _bt_handle_agent_event(GVariant *msg, const char *member)
 
        if (strcasecmp(member, "ObexAuthorize") == 0) {
                __bt_get_agent_signal_info(msg, &address, &name, &uuid);
-               param = g_variant_new("(iss)", result, address, name);
+               param = g_variant_new("(i&s&s)", result, address, name);
                _bt_send_event(BT_OPP_SERVER_EVENT,
                        BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE,
                        param);
-               g_free(address);
-               g_free(name);
        } else if (strcasecmp(member, "RfcommAuthorize") == 0) {
                bt_rfcomm_server_info_t *server_info;
 
@@ -2512,14 +2582,11 @@ void _bt_handle_agent_event(GVariant *msg, const char *member)
                server_info = _bt_rfcomm_get_server_info_using_uuid(uuid);
                ret_if(server_info == NULL);
                ret_if(server_info->server_type != BT_CUSTOM_SERVER);
-               param = g_variant_new("(isssn)", result, address, uuid, name,
+               param = g_variant_new("(i&s&s&sn)", result, address, uuid, name,
                                        server_info->control_fd);
                _bt_send_event(BT_RFCOMM_SERVER_EVENT,
                        BLUETOOTH_EVENT_RFCOMM_AUTHORIZE,
                        param);
-               g_free(address);
-               g_free(uuid);
-               g_free(name);
        }
 }
 
@@ -2613,17 +2680,15 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                                bt_cache_info_t *cache_info;
                                bt_remote_dev_info_t *dev_info;
 
-                               ret_if(_bt_is_discovering() == FALSE &&
-                                               _bt_is_le_scanning() == FALSE);
+                               if (_bt_is_discovering() == FALSE &&
+                                   _bt_is_le_scanning() == FALSE) {
+                                       g_variant_unref(value);
+                                       return;
+                               }
 
                                cache_info = g_malloc0(sizeof(bt_cache_info_t));
-                               ret_if(cache_info == NULL);
 
                                dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
-                               if (dev_info == NULL) {
-                                       __bt_free_cache_info(cache_info);
-                                       return;
-                               }
 
                                cache_info->dev_info = dev_info;
 
@@ -2727,27 +2792,30 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                char *previous = NULL;
                char *current = NULL;
 
+               if (g_strcmp0(g_variant_get_type_string(parameters), "(sss)") != 0) {
+                       BT_ERR("Invalid variant format");
+                       return;
+               }
+
                if (__bt_get_owner_info(parameters, &name, &previous, &current)) {
                        BT_ERR("Fail to get the owner info");
                        return;
                }
 
-               if (*current != '\0') {
-                       g_free(current);
-                       if (name)
-                               g_free(name);
-                       if (previous)
-                               g_free(previous);
+               if (*current != '\0')
+                       return;
+
+               if (name == NULL)
                        return;
-               }
 
                if (strcasecmp(name, BT_BLUEZ_NAME) == 0) {
-                       BT_DBG("Bluetoothd is terminated");
+                       BT_INFO_C("### Bluetoothd is terminated");
                        if (_bt_adapter_get_status() == BT_ACTIVATED)
                                 _bt_disable_cb();
 
                        _bt_handle_adapter_removed();
                        __bt_devices_list_free();
+                       _bt_service_adapter_le_deinit();
                }
 
                _bt_obex_server_check_allocation(&value);
@@ -2764,13 +2832,14 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
                        _bt_rfcomm_server_check_termination(name);
                }
 
+               /* Reset connection interval */
+               _bt_remove_all_le_conn_param_info(name);
+
                /* Stop advertising started by terminated process */
                _bt_stop_advertising_by_terminated_process(name);
+
                /* Stop LE Scan */
                _bt_stop_le_scan(name);
-               g_free(name);
-               g_free(previous);
-               g_free(current);
        } else if (g_strcmp0(interface_name, BT_PROPERTIES_INTERFACE) == 0) {
                const char *path = object_path;
 
@@ -2786,7 +2855,9 @@ static  void __bt_manager_event_filter(GDBusConnection *connection,
        } else if (g_strcmp0(interface_name, BT_NETWORK_SERVER_INTERFACE) == 0) {
                _bt_handle_network_server_event(parameters, signal_name);
        } else if (g_strcmp0(interface_name, BT_HEADSET_INTERFACE) == 0) {
-               _bt_handle_headset_event(parameters, object_path);
+               if (g_strcmp0(signal_name, "PropertyChanged") == 0) {
+                       _bt_handle_headset_event(parameters, object_path);
+               }
        } else if (g_strcmp0(interface_name, BT_SINK_INTERFACE) == 0) {
                _bt_handle_sink_event(parameters, object_path);
        } else if (g_strcmp0(interface_name, BT_AGENT_INTERFACE) == 0) {
@@ -3194,7 +3265,7 @@ int _bt_register_audio_subscribe_signal(GDBusConnection *conn,
                if (subs_headset_id == -1) {
                        subs_headset_id = g_dbus_connection_signal_subscribe(conn,
                                NULL, BT_HEADSET_INTERFACE,
-                               NULL, NULL, NULL, 0,
+                               "PropertyChanged", NULL, NULL, 0,
                                __bt_manager_event_filter,
                                NULL, NULL);
                }
@@ -3631,4 +3702,3 @@ void _bt_deinit_service_event_receiver(void)
 
        BT_DBG("-");
 }
-