Fix bt-service crash issue in testhub
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-device.c
index 3b33125..83c358a 100644 (file)
@@ -122,9 +122,6 @@ static guint pending_le_conn_timer_id = 0;
 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data);
 
-static int __bt_retry_bond(void);
-
-
 /*static void __bt_decline_pair_request()
 {
        GVariant *out_param1;
@@ -495,6 +492,8 @@ bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
                                g_variant_get(tmp_value, "ay", &value_iter);
                                while (g_variant_iter_loop(value_iter, "y", &m_value))
                                        dev_info->manufacturer_data[i++] = m_value;
+
+                               g_variant_iter_free(value_iter);
                        }
                        g_variant_unref(tmp_value);
                } else {
@@ -679,93 +678,23 @@ static int __bt_cancel_bonding(void)
        /* First Cancel the ongoing pairing in bluez otherwise if we send
         * pair request bluez will send inprogress and we again retry bond so
         * this cycle continues */
-        GError *err = NULL;
-       g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
+       GError *err = NULL;
+       GVariant *result;
+       result = g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
                        NULL, G_DBUS_CALL_FLAGS_NONE,
                        10000, NULL, &err);
-       if (err) {
-               BT_ERR("Cancelling bonding request error msg (%s)", err->message);
-               g_clear_error(&err);
-               return BLUETOOTH_ERROR_PARING_FAILED;
-       }
-       return BLUETOOTH_ERROR_NONE;
-}
-
-static int __bt_retry_bond(void)
-{
-       BT_CHECK_PARAMETER(bonding_info, return);
-       BT_CHECK_PARAMETER(bonding_info->addr, return);
-
-       g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
-                               g_variant_new("(y)", bonding_info->conn_type),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               BT_MAX_DBUS_TIMEOUT,
-                               NULL,
-                               (GAsyncReadyCallback)__bt_bond_device_cb,
-                               NULL);
-
-       return BLUETOOTH_ERROR_NONE;
-}
-
-
-static int __bt_remove_and_bond(void)
-{
-       GDBusProxy *adapter_proxy;
-       GVariant *result = NULL;
-       GError *err = NULL;
-       char *device_path = NULL;
-
-       BT_CHECK_PARAMETER(bonding_info, return);
-       BT_CHECK_PARAMETER(bonding_info->addr, return);
-
-       adapter_proxy = _bt_get_adapter_proxy();
-       retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
-                               g_variant_new("(s)", bonding_info->addr),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1,
-                               NULL,
-                               NULL);
-       if (result == NULL)
-               return BLUETOOTH_ERROR_INTERNAL;
-
-       g_variant_get(result , "(o)", &device_path);
-       g_variant_unref(result);
-
-       retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
-                               g_variant_new("(o)", device_path),
-                               G_DBUS_CALL_FLAGS_NONE,
-                               -1,
-                               NULL,
-                               &err);
-       g_free(device_path);
-       if (result == NULL) {
-               if (err != NULL) {
-                       BT_ERR("UnpairDevice Fail: %s", err->message);
-                       g_error_free(err);
+       if (!result) {
+               if (err) {
+                       BT_ERR("Cancelling bonding request error msg (%s)", err->message);
+                       g_clear_error(&err);
+                       return BLUETOOTH_ERROR_PARING_FAILED;
                }
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       return __bt_retry_bond();
-}
-
-static int __bt_cancel_and_bond(void)
-{
-       int ret = BLUETOOTH_ERROR_NONE;
-
-       ret = _bt_agent_reply_cancellation();
-       if (ret != BLUETOOTH_ERROR_NONE) {
-               BT_ERR("Fail to call reply cancellation");
-               return ret;
+       } else {
+               g_variant_unref(result);
        }
-       return __bt_retry_bond();
+       return BLUETOOTH_ERROR_NONE;
 }
 
-
 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                                        gpointer user_data)
 {
@@ -775,9 +704,6 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        GVariant *out_param1;
        request_info_t *req_info;
        bluetooth_device_info_t dev_info;
-       bt_remote_dev_info_t *remote_dev_info;
-       GVariant *manufacture_data;
-       GVariant *param;
 
        /* Terminate ALL system popup */
        syspopup_destroy_all();
@@ -807,10 +733,8 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                BT_ERR("Error occured in CreateBonding [%s]", err->message);
 
                if (g_strrstr(err->message, "Already Exists")) {
-                       BT_INFO("Existing Bond, remove and retry");
-                       ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
-
-                       result = BLUETOOTH_ERROR_PARING_FAILED;
+                       BT_INFO("Existing Bond");
+                       result = BLUETOOTH_ERROR_ALREADY_CONNECT;
                } else if (_bt_agent_is_canceled() ||
                        g_strrstr(err->message, "Authentication Canceled")) {
                        BT_INFO("Cancelled by USER");
@@ -819,10 +743,8 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                        BT_INFO("REJECTED");
                        result = BLUETOOTH_ERROR_ACCESS_DENIED;
                } else if (g_strrstr(err->message, "In Progress")) {
-                       BT_INFO("Bond in progress, cancel and retry");
-                       ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
-
-                       result = BLUETOOTH_ERROR_PARING_FAILED;
+                       BT_INFO("Bond in progress");
+                       result = BLUETOOTH_ERROR_IN_PROGRESS;
                } else if (g_strrstr(err->message, "Authentication Failed")) {
                        BT_INFO("Authentication Failed");
                        if (bonding_info->is_autopair == TRUE) {
@@ -918,45 +840,6 @@ static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                goto dbus_return;
        }
 
-       remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
-       if (!remote_dev_info)
-               goto dbus_return;
-
-       GVariant *uuids = NULL;
-       GVariantBuilder *builder = NULL;
-       int i = 0;
-       builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
-       for (i = 0; i < remote_dev_info->uuid_count; i++) {
-               g_variant_builder_add(builder, "s",
-                       remote_dev_info->uuids[i]);
-       }
-       uuids = g_variant_new("as", builder);
-       g_variant_builder_unref(builder);
-       manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
-                                               remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
-                                               TRUE, NULL, NULL);
-
-       param = g_variant_new("(isunsbub@asn@ay)",
-                       result,
-                       bonding_info->addr,
-                       remote_dev_info->class,
-                       remote_dev_info->rssi,
-                       remote_dev_info->name,
-                       remote_dev_info->paired,
-                       remote_dev_info->connected,
-                       remote_dev_info->trust,
-                       uuids,
-                       remote_dev_info->manufacturer_data_len,
-                       manufacture_data);
-
-
-       /* Send the event to application */
-       _bt_send_event(BT_ADAPTER_EVENT,
-               BLUETOOTH_EVENT_BONDING_FINISHED,
-               param);
-
-       _bt_free_device_info(remote_dev_info);
-
 dbus_return:
        if (req_info->context == NULL)
                goto done;
@@ -2119,6 +2002,7 @@ int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        GDBusProxy *adapter_proxy;
        GDBusConnection *conn;
        int ret = BLUETOOTH_ERROR_NONE;
+       GVariant *result;
 
        BT_CHECK_PARAMETER(bd_addr, return);
 
@@ -2146,17 +2030,21 @@ int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        g_free(device_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
+       result = g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
                                NULL,
                                G_DBUS_CALL_FLAGS_NONE,
                                -1,
                                NULL,
                                &error);
-       if (error) {
-               BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
-               g_error_free(error);
-               g_object_unref(device_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
+       if (!result) {
+               if (error) {
+                       BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
+                       g_error_free(error);
+                       g_object_unref(device_proxy);
+                       return BLUETOOTH_ERROR_INTERNAL;
+               }
+       } else {
+               g_variant_unref(result);
        }
 
        g_object_unref(device_proxy);
@@ -2173,6 +2061,7 @@ int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        GDBusProxy *adapter_proxy;
        GDBusConnection *conn;
        int ret = BLUETOOTH_ERROR_NONE;
+       GVariant *result;
 
        BT_CHECK_PARAMETER(bd_addr, return);
 
@@ -2200,17 +2089,21 @@ int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
        g_free(device_path);
        retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
+       result = g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
                                NULL,
                                G_DBUS_CALL_FLAGS_NONE,
                                -1,
                                NULL,
                                &error);
-       if (error) {
-               BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
-               g_error_free(error);
-               g_object_unref(device_proxy);
-               return BLUETOOTH_ERROR_INTERNAL;
+       if (!result) {
+               if (error) {
+                       BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
+                       g_error_free(error);
+                       g_object_unref(device_proxy);
+                       return BLUETOOTH_ERROR_INTERNAL;
+               }
+       } else {
+               g_variant_unref(result);
        }
 
        g_object_unref(device_proxy);
@@ -2719,7 +2612,7 @@ int _bt_le_conn_update(const char *sender,
 
        BT_CHECK_PARAMETER(device_address, return);
 
-       BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
+       BT_DBG("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
                        sender, interval_min, interval_max, latency, time_out);
 
        if (interval_min > interval_max ||
@@ -3457,6 +3350,11 @@ int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
                BT_DBG("ATT MTU : %d", *mtu);
                g_variant_unref(value);
                g_variant_unref(result);
+
+               if (*mtu == 0) {
+                       g_object_unref(device_proxy);
+                       return BLUETOOTH_ERROR_NOT_CONNECTED;
+               }
        }
 
        g_object_unref(device_proxy);