Resolve memory leak in HAL
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-device-dbus-handler.c
index 1c86b8a..bff47c4 100644 (file)
@@ -231,7 +231,6 @@ int _bt_hal_device_remove_bond(const bt_bdaddr_t *bd_addr)
                        (GAsyncReadyCallback)__bt_hal_unbond_device_cb,
                        (gpointer)device_path);
 
-       DBG("-");
        return BT_STATUS_SUCCESS;
 }
 
@@ -566,7 +565,7 @@ done:
        ev->num_props = 0;
        size = sizeof(*ev);
        ERR("Error: Failed to get Remote device properties after SDP,"
-                       " Num Prop [%d] total size [%d]", ev->num_props, size);
+                       " Num Prop [%d] total size [%zd]", ev->num_props, size);
        event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
 
 cleanup:
@@ -633,10 +632,11 @@ static void __bt_device_parse_services(GVariant *result)
        }
 
        DBG("Send Remote Device services to HAL,"
-                       " Num Prop [%d] total size [%d]", ev->num_props, size);
+                       " Num Prop [%d] total size [%zd]", ev->num_props, size);
        event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
 
        g_variant_unref(result);
+       g_variant_iter_free(property_iter);
 }
 
 int __bt_hal_dbus_enquire_remote_device_services(char *address)
@@ -711,7 +711,6 @@ static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        struct hal_ev_bond_state_changed ev;
        memset(&ev, 0, sizeof(ev));
        char dev_address[18];
-       DBG("+");
 
 #if TODO_40
        /* Terminate ALL system popup */
@@ -721,9 +720,7 @@ static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
 
        g_dbus_proxy_call_finish(proxy, res, &err);
        device_path = g_dbus_proxy_get_object_path(proxy);
-       DBG("Device path: %s", device_path);
        _bt_hal_convert_device_path_to_address(device_path, dev_address);
-       DBG("Remote Device address [%s]", dev_address);
 
        if (err != NULL) {
                g_dbus_error_strip_remote_error(err);
@@ -785,10 +782,9 @@ static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                        event_cb(HAL_EV_BOND_STATE_CHANGED, (void*)&ev, sizeof(ev));
                }
        } else {
-               DBG("Bonding Success!!");
+               DBG("Bonding Success!! [%s]", dev_address);
        }
 
-       DBG("-");
 }
 
 static void __bt_hal_unbond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
@@ -800,7 +796,6 @@ static void __bt_hal_unbond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
        int result = BT_STATUS_SUCCESS;
        struct hal_ev_bond_state_changed ev;
        memset(&ev, 0, sizeof(ev));
-       DBG("+");
 
        g_dbus_proxy_call_finish(proxy, res, &err);
        if (err != NULL) {
@@ -826,8 +821,6 @@ static void __bt_hal_unbond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
                }
        }
        g_free(device_path);
-
-       DBG("-");
 }
 
 static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
@@ -926,7 +919,7 @@ static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
                                        uint8_t uuid[BT_HAL_STACK_UUID_SIZE];
                                        memset(uuid, 0x00, BT_HAL_STACK_UUID_SIZE);
                                        uuid_str = g_strdup(uuid_value[i]);
-                                       DBG("UUID string [%s]\n", uuid_str);
+                                       DBG("[%d] [%s]", i, uuid_str);
                                        _bt_hal_convert_uuid_string_to_type(uuid, uuid_str);
                                        memcpy(uuids + i * BT_HAL_STACK_UUID_SIZE, uuid, BT_HAL_STACK_UUID_SIZE);
                                        g_free(uuid_str);
@@ -945,6 +938,8 @@ static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
                        while (g_variant_iter_loop(char_value_iter, "y", &char_value))
                                g_byte_array_append(manufacturer_data, &char_value, 1);
 
+                       g_variant_iter_free(char_value_iter);
+
                        if (manufacturer_data) {
                                if (manufacturer_data->len > 0) {
                                        size += __bt_insert_hal_properties(
@@ -961,11 +956,10 @@ static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
                                        HAL_PROP_DEVICE_IS_ALIAS_SET, sizeof(uint8_t), &is_alias_set);
                        ev->num_props++;
                } else {
-                       ERR("Unhandled Property:[%s]", key);
+                       DBG("Unhandled Property:[%s]", key);
                }
        }
 
-       DBG("trust: %d, paired: %d", trust, paired);
        if (!event_cb)
                event_cb = _bt_hal_get_stack_message_handler();
        if (!event_cb) {
@@ -978,18 +972,19 @@ static gboolean __bt_device_bonded_device_info_cb(gpointer user_data)
                ev->num_props = 0;
                size = sizeof(*ev);
                DBG("Send Remote Device properties event to HAL,"
-                               " Num Prop [%d] total size [%d]", ev->num_props, size);
+                               " Num Prop [%d] total size [%zd]", ev->num_props, size);
                event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
        } else {
                if (size > 2) {
                        DBG("Send Remote Device properties event to HAL,"
-                               " Num Prop [%d] total size [%d]", ev->num_props, size);
+                               " Num Prop [%d] total size [%zd]", ev->num_props, size);
                        event_cb(HAL_EV_REMOTE_DEVICE_PROPS, (void*) buf, size);
                }
        }
 
 done:
        g_variant_unref(result);
+       g_variant_iter_free(property_iter);
        return FALSE;
 }
 
@@ -1323,7 +1318,7 @@ int _bt_hal_device_get_service_connection_state(
        return 1;
 }
 
-int _bt_hal_device_set_osp_server(uint32_t type, gboolean enable)
+int _bt_hal_device_register_osp_server(uint32_t type, char *uuid, char *path, int fd)
 {
        GapAgentPrivate *agent = _bt_hal_get_adapter_agent();
        gboolean result = FALSE;
@@ -1332,11 +1327,24 @@ int _bt_hal_device_set_osp_server(uint32_t type, gboolean enable)
        if (!agent)
                return BT_STATUS_FAIL;
 
-       if (enable)
-               result = _bt_hal_gap_agent_register_osp_server(agent, type);
-       else
-               result = _bt_hal_gap_agent_unregister_osp_server(agent, type);
+       result = _bt_hal_gap_agent_register_osp_server(agent, type, uuid, path, fd);
+       if (!result)
+               return BT_STATUS_FAIL;
+
+       DBG("-");
+       return BT_STATUS_SUCCESS;
+}
 
+int _bt_hal_device_unregister_osp_server(uint32_t type, char *uuid)
+{
+       GapAgentPrivate *agent = _bt_hal_get_adapter_agent();
+       gboolean result = FALSE;
+
+       DBG("+");
+       if (!agent)
+               return BT_STATUS_FAIL;
+
+       result = _bt_hal_gap_agent_unregister_osp_server(agent, type, uuid);
        if (!result)
                return BT_STATUS_FAIL;
 
@@ -1592,6 +1600,75 @@ int _bt_hal_device_get_trusted_profile(const bt_bdaddr_t *bd_addr,
                        trusted_flag, trusted);
 }
 
+int _bt_hal_device_get_ida(bt_bdaddr_t *bd_addr, bt_bdaddr_t *id_addr)
+{
+       char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
+       gchar *device_path = NULL;
+       const gchar *idaddress = NULL;
+       GDBusProxy *device_proxy;
+       GError *error = NULL;
+       GVariant *result = NULL;
+       GDBusConnection *conn;
+       int ret = BT_STATUS_SUCCESS;
+
+       if (bd_addr == NULL)
+               return BT_STATUS_PARM_INVALID;
+
+       conn = _bt_hal_get_system_gconn();
+
+       if (!conn) {
+               ERR("_bt_hal_get_system_gconn failed");
+               return BT_STATUS_FAIL;
+       }
+
+       _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
+
+       DBG("The device_address is: [%s]", address);
+
+       device_path = _bt_hal_get_device_object_path(address);
+
+       if (!device_path) {
+               ERR("_bt_hal_get_device_object_path failed");
+               return BT_STATUS_FAIL;
+       }
+
+       device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_HAL_BLUEZ_NAME, device_path,
+                       BT_HAL_DEVICE_INTERFACE,  NULL, NULL);
+       g_free(device_path);
+       if (!device_proxy) {
+               ERR("Unable to get proxy");
+               return BT_STATUS_FAIL;
+       }
+
+       result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
+                       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+       if (result == NULL) {
+               ERR("Failed to get device ID address");
+
+               ret = BT_STATUS_FAIL;
+               if (error != NULL) {
+                       g_dbus_error_strip_remote_error(error);
+                       ERR("Error occured in Proxy call [%s]", error->message);
+                       if (g_strcmp0(error->message, "Does Not Exist") == 0)
+                               ERR("Device does not exist");
+                       g_error_free(error);
+               }
+               g_object_unref(device_proxy);
+               return ret;
+       }
+
+       g_variant_get(result , "(&s)", &idaddress);
+
+       DBG_SECURE("ID Address : %s", idaddress);
+       _bt_hal_convert_addr_string_to_type(id_addr->address, idaddress);
+
+       g_variant_unref(result);
+       g_object_unref(device_proxy);
+
+       return ret;
+}
+
 int _bt_hal_device_enable_rssi_monitoring(bt_bdaddr_t *bd_addr, uint32_t link_type,
                    int low_threshold, int in_range_threshold, int high_threshold)
 {
@@ -1675,4 +1752,59 @@ int _bt_hal_device_get_connected_link_rssi_strength(const bt_bdaddr_t *bd_addr,
        DBG("-");
        return BT_STATUS_SUCCESS;
 }
+
+
+int _bt_hal_device_disconnect(const bt_bdaddr_t *bd_addr)
+{
+       GDBusProxy *proxy;
+       char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
+       GError *error = NULL;
+       GVariant *result = NULL;
+       GDBusConnection *conn;
+       char *device_path = NULL;
+       DBG("+");
+
+       conn = _bt_hal_get_system_gconn();
+       if (!conn) {
+               DBG("Could not get DBUS connection!");
+               return BT_STATUS_FAIL;
+       }
+
+       _bt_hal_convert_addr_type_to_string(address, bd_addr->address);
+       device_path = _bt_hal_get_device_object_path(address);
+
+       if (device_path == NULL) {
+               ERR("No created device with address:[%s] in statck", address);
+               return BT_STATUS_FAIL;
+       }
+       proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, BT_HAL_BLUEZ_NAME,
+                       device_path, BT_HAL_DEVICE_INTERFACE, NULL, NULL);
+
+       g_free(device_path);
+       if (proxy == NULL) {
+               ERR("Could not get Device Proxy");
+               return BT_STATUS_FAIL;
+       }
+
+       result = g_dbus_proxy_call_sync(proxy, "Disconnect",
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       -1, NULL,
+                       &error);
+
+       if (error != NULL) {
+               ERR("Dbus Call Error:[%s]", error->message);
+               g_clear_error(&error);
+               g_object_unref(proxy);
+               return BT_STATUS_FAIL;
+       }
+
+       g_object_unref(proxy);
+       if (result)
+               g_variant_unref(result);
+
+       DBG("-");
+       return BT_STATUS_SUCCESS;
+}
 #endif