Mesh: Implement bluetooth service interface for model message
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hdp.c
index 42a1913..f8f0fad 100755 (executable)
 
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <dbus/dbus.h>
+#include <gio/gio.h>
+#include <glib.h>
 #include <string.h>
+#include <gio/gunixfdlist.h>
 
 #include "bluetooth-api.h"
 #include "bt-common.h"
 #include "bt-internal-types.h"
+#include "bt-request-sender.h"
 
 #define HDP_BUFFER_SIZE 1024
 #define BLUEZ_HDP_MANAGER_INTERFACE  "org.bluez.HealthManager1"
 #define BLUEZ_HDP_DEVICE_INTERFACE  "org.bluez.HealthDevice1"
 #define BLUEZ_HDP_CHANNEL_INTERFACE  "org.bluez.HealthChannel1"
 
-gboolean interface_exist = FALSE;
-
-typedef struct {
-       char *obj_channel_path;
-       int fd;
-       guint watch_id;
-} hdp_obj_info_t;
-
-typedef struct {
-       void *app_handle;
-       GSList *obj_info;
-} hdp_app_list_t;
-
-/* Variable for privilege, only for write API,
-  before we should reduce time to bt-service dbus calling
-  -1 : Don't have a permission to access API
-  0 : Initial value, not yet check
-  1 : Have a permission to access API
-*/
-static int privilege_token;
-
-
-/**********************************************************************
-*              Static Functions declaration                            *
-***********************************************************************/
-static int __bt_hdp_internal_create_application(unsigned int data_type,
-                                               int role,
-                                               bt_hdp_qos_type_t channel_type,
-                                               char **app_handle);
-
-static DBusHandlerResult __bt_hdp_internal_event_filter(DBusConnection *sys_conn,
-                                                       DBusMessage *msg,
-                                                       void *data);
-
-static void __bt_hdp_internal_handle_connect(DBusMessage *msg);
-
-static void __bt_hdp_internal_handle_disconnect(DBusMessage *msg);
-
-static void __bt_hdp_internal_handle_property_changed(DBusMessage *msg);
-
-static int __bt_hdp_internal_add_filter(void);
-
-static int __bt_hdp_internal_acquire_fd(const char *path);
-
-static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path);
-
-static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
-                                               GIOCondition cond,
-                                               gpointer data);
-
-static int __bt_hdp_internal_destroy_application(const char *app_handle);
-
-static void __bt_hdp_internal_remove_filter(void);
-
-static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle);
-
-static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd);
-
-static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path);
-
-/*Global Variables*/
-static DBusConnection *g_hdp_dus_conn;
-
-static GSList *g_app_list = NULL;
-
-/**********************************************************************
-*                      Health device APIs (HDP)                        *
-***********************************************************************/
-
-BT_EXPORT_API int bluetooth_hdp_activate(unsigned short data_type,
-                                       bt_hdp_role_type_t role,
-                                       bt_hdp_qos_type_t channel_type,
-                                       char **app_handle)
-{
-       int result = BLUETOOTH_ERROR_NONE;
-
-       BT_DBG("+");
-
-       BT_CHECK_ENABLED(return);
-
-       /*For source role is mandatory */
-       if (role == HDP_ROLE_SOURCE && channel_type == HDP_QOS_ANY) {
-               BT_ERR("For source, type is mandatory - Reliable/Streaming");
-               return BLUETOOTH_ERROR_INVALID_PARAM;
-       }
-
-       result = __bt_hdp_internal_create_application(data_type, role,
-                                               channel_type, app_handle);
-
-       return result;
-}
-
-static void __bt_hdp_obj_info_free(hdp_obj_info_t *info)
-{
-       if (info) {
-               g_source_remove(info->watch_id);
-               close(info->fd);
-               g_free(info->obj_channel_path);
-               g_free(info);
-       }
-}
-
-static int __bt_hdp_internal_create_application(unsigned int data_type,
-                                       int role,
-                                       bt_hdp_qos_type_t channel_type,
-                                       char **app_handle)
-{
-       DBusMessage *msg;
-       DBusMessage *reply;
-       const char *svalue;
-       const char *key_type;
-       char *app_path;
-       hdp_app_list_t *list;
-       DBusError err;
-       DBusMessageIter iter;
-       DBusMessageIter array_iter;
-       DBusMessageIter entry;
-       DBusMessageIter variant;
-       guint16 value;
-       DBusConnection *conn;
-       int ret = BLUETOOTH_ERROR_NONE;
-
-       BT_DBG("+");
-
-       conn = _bt_get_system_conn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       msg = dbus_message_new_method_call(BT_BLUEZ_NAME, "/org/bluez",
-                                         BLUEZ_HDP_MANAGER_INTERFACE,
-                                         "CreateApplication");
-
-       retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_message_iter_init_append(msg, &iter);
-       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
-               DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING
-               DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
-               &array_iter);
-
-       key_type = "DataType";
-       value = (guint16) data_type;
-       dbus_message_iter_open_container(&array_iter, DBUS_TYPE_DICT_ENTRY,
-                                       NULL, &entry);
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_type);
-       dbus_message_iter_open_container(&entry,
-               DBUS_TYPE_VARIANT, DBUS_TYPE_UINT16_AS_STRING, &variant);
-       dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT16, &value);
-       dbus_message_iter_close_container(&entry, &variant);
-       dbus_message_iter_close_container(&array_iter, &entry);
-
-       key_type = "Role";
-
-       /*0-Source,1-Sink*/
-       svalue = (role == HDP_ROLE_SINK) ? "Sink" : "Source";
-       dbus_message_iter_open_container(&array_iter, DBUS_TYPE_DICT_ENTRY,
-                                       NULL, &entry);
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_type);
-       dbus_message_iter_open_container(&entry,
-               DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &variant);
-       dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &svalue);
-       dbus_message_iter_close_container(&entry, &variant);
-       dbus_message_iter_close_container(&array_iter, &entry);
-
-       key_type = "Description";
-       svalue = "Health Device";
-       dbus_message_iter_open_container(&array_iter, DBUS_TYPE_DICT_ENTRY,
-                                       NULL, &entry);
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_type);
-       dbus_message_iter_open_container(&entry,
-               DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &variant);
-       dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &svalue);
-       dbus_message_iter_close_container(&entry, &variant);
-       dbus_message_iter_close_container(&array_iter, &entry);
-
-       if (role == HDP_ROLE_SOURCE) {
-               key_type = "ChannelType";
-               if (channel_type == HDP_QOS_RELIABLE)
-                       svalue = "reliable";
-               else if (channel_type == HDP_QOS_STREAMING)
-                       svalue = "streaming";
-
-               dbus_message_iter_open_container(&array_iter,
-                       DBUS_TYPE_DICT_ENTRY, NULL, &entry);
-               dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
-                                               &key_type);
-               dbus_message_iter_open_container(&entry,
-                       DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING,
-                       &variant);
-               dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING,
-                                               &svalue);
-               dbus_message_iter_close_container(&entry, &variant);
-               dbus_message_iter_close_container(&array_iter, &entry);
-       }
-
-       dbus_message_iter_close_container(&iter, &array_iter);
-
-       dbus_error_init(&err);
-
-       reply = dbus_connection_send_with_reply_and_block(
-                                       conn, msg,
-                                       -1, &err);
-       dbus_message_unref(msg);
-
-       if (!reply) {
-               BT_ERR(" HDP:dbus Can't create application");
-
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("%s", err.message);
-
-                       if (g_strrstr(err.message, BT_ACCESS_DENIED_MSG))
-                               ret = BLUETOOTH_ERROR_ACCESS_DENIED;
-                       else
-                               ret = BLUETOOTH_ERROR_INTERNAL;
-
-                       dbus_error_free(&err);
-               }
-               return ret;
-       }
-
-       if (!dbus_message_get_args(reply, &err, DBUS_TYPE_OBJECT_PATH,
-                               &app_path, DBUS_TYPE_INVALID)) {
-
-               BT_ERR(" HDP: Can't get reply arguments from Dbus");
-
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("Error: %s", err.message);
-                       dbus_error_free(&err);
-               }
-
-               dbus_message_unref(reply);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       dbus_message_unref(reply);
-
-       BT_DBG("Created health application: %s", (char *)app_path);
-
-       list = g_new0(hdp_app_list_t, 1);
-       list->app_handle = (void *)g_strdup(app_path);
-       *app_handle = (char *)list->app_handle;
-       g_app_list = g_slist_append(g_app_list, list);
-
-       BT_DBG("app_handle: %s", (char *)list->app_handle);
-
-       ret = __bt_hdp_internal_add_filter();
-
-       if (ret != BLUETOOTH_ERROR_NONE) {
-               BT_ERR("Funtion failed");
-               return ret;
-       }
-
-       return BLUETOOTH_ERROR_NONE;
-}
-
-static int __bt_hdp_internal_add_filter(void)
-{
-       DBusError dbus_error;
-
-       BT_DBG("+");
-
-       /*Single process only one signal registration is required */
-       if (g_hdp_dus_conn) {
-               BT_ERR("g_hdp_dus_conn already exist");
-               goto done;
-       }
-
-       /* Add the filter for HDP client functions */
-       dbus_error_init(&dbus_error);
-
-       g_hdp_dus_conn = _bt_get_system_conn();
-       retv_if(g_hdp_dus_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_connection_add_filter(g_hdp_dus_conn,
-                               __bt_hdp_internal_event_filter, NULL, NULL);
-
-       dbus_bus_add_match(g_hdp_dus_conn,
-                       "type='signal',interface=" BLUEZ_HDP_DEVICE_INTERFACE,
-                       &dbus_error);
-
-       dbus_bus_add_match(g_hdp_dus_conn,
-                       "type='signal',interface=" BT_MANAGER_INTERFACE,
-                       &dbus_error);
-
-       dbus_bus_add_match(g_hdp_dus_conn,
-                       "type='signal',interface=" BT_PROPERTIES_INTERFACE,
-                       &dbus_error);
-
-       if (dbus_error_is_set(&dbus_error)) {
-               BT_ERR("Fail to add dbus filter signal\n");
-               dbus_error_free(&dbus_error);
-               g_hdp_dus_conn = NULL;
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-done:
-       BT_DBG("-\n");
-       return BLUETOOTH_ERROR_NONE;
-
-}
-
-static void __bt_hdp_internal_handle_connected(DBusMessage *msg);
-
-
-static DBusHandlerResult __bt_hdp_internal_event_filter(DBusConnection *sys_conn,
-                                       DBusMessage *msg, void *data)
-{
-       const char *path = dbus_message_get_path(msg);
-
-       if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-       BT_DBG("Path = %s\n", path);
-       if (path == NULL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
-       if (dbus_message_is_signal(msg, BLUEZ_HDP_DEVICE_INTERFACE,
-                                       "ChannelConnected"))
-               __bt_hdp_internal_handle_connect(msg);
-       else if (dbus_message_is_signal(msg, BLUEZ_HDP_DEVICE_INTERFACE,
-                                       "ChannelDeleted"))
-               __bt_hdp_internal_handle_disconnect(msg);
-       else if (dbus_message_is_signal(msg, BLUEZ_HDP_DEVICE_INTERFACE,
-                                       "PropertyChanged"))
-               __bt_hdp_internal_handle_property_changed(msg);
-       else if (dbus_message_is_signal(msg, BT_MANAGER_INTERFACE,
-                                       "InterfacesAdded")) {
-               interface_exist = TRUE;
-               BT_DBG("InterfaceAdded");
-       } else if (dbus_message_is_signal(msg, BT_MANAGER_INTERFACE,
-                                       "InterfacesRemoved")) {
-               interface_exist = FALSE;
-               __bt_hdp_internal_handle_disconnect(msg);
-               BT_DBG("InterfaceRemoved");
-       } else if (dbus_message_is_signal(msg, BT_PROPERTIES_INTERFACE,
-                                       "PropertiesChanged")) {
-               BT_DBG("PropertyChanged");
-               if(interface_exist)
-                       __bt_hdp_internal_handle_connected(msg);
-       }
-
-       return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-static void __bt_hdp_internal_handle_connected(DBusMessage *msg)
-{
-       DBusMessageIter iter, dict, entry, var;
-       const char *path = NULL;
-       const char *obj_channel_path = NULL;
-       bt_user_info_t *user_info;
-       int ret;
-
-       if (dbus_message_iter_init(msg, &iter) == FALSE)
-               return;
-
-       dbus_message_iter_get_basic(&iter, &path);
-
-       BT_DBG("object path: %s", path);
-
-       if(!g_strcmp0(path, "org.bluez.HealthDevice1")) {
-               dbus_message_iter_next(&iter);
-
-               if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
-                       return;
-
-               dbus_message_iter_recurse(&iter, &dict);
-               while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
-                       const char *interface;
-
-                       dbus_message_iter_recurse(&dict, &entry);
-
-                       if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
-                               break;
-
-                       dbus_message_iter_get_basic(&entry, &interface);
-                       dbus_message_iter_next(&entry);
-                       dbus_message_iter_recurse(&entry, &var);
-
-                       if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_OBJECT_PATH)
-                               break;
-
-                       dbus_message_iter_get_basic(&var, &obj_channel_path);
-
-                       BT_DBG("interface: %s", interface);
-                       BT_DBG("object_path: %s", obj_channel_path);
-
-                       dbus_message_iter_next(&dict);
-               }
-
-               BT_INFO("Channel connected, Path = %s", obj_channel_path);
-
-               user_info = _bt_get_user_data(BT_COMMON);
-               if (user_info == NULL || user_info->cb == NULL)
-                       return;
-
-               ret = __bt_hdp_internal_acquire_fd(obj_channel_path);
-               if (ret != BLUETOOTH_ERROR_NONE) {
-                       _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                                       BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
-                                       user_info->cb, user_info->user_data);
-               }
-       }
-}
-
-static void __bt_hdp_internal_handle_connect(DBusMessage *msg)
-{
-       const char *path = dbus_message_get_path(msg);
-       const char *obj_channel_path;
-       bt_user_info_t *user_info;
-       int ret;
-
-       BT_INFO("+********Signal - ChannelConnected******\n\n");
-       BT_DBG("Path = %s", path);
-
-       if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH,
-                               &obj_channel_path, DBUS_TYPE_INVALID)) {
-               BT_ERR("Unexpected parameters in ChannelConnected signal");
-               return;
-       }
-
-       BT_INFO("Channel connected, Path = %s", obj_channel_path);
-
-       user_info = _bt_get_user_data(BT_COMMON);
-       if (user_info == NULL || user_info->cb == NULL)
-               return;
-
-       ret = __bt_hdp_internal_acquire_fd(obj_channel_path);
-       if (ret != BLUETOOTH_ERROR_NONE) {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                               BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
-                               user_info->cb, user_info->user_data);
-       } else {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                               BLUETOOTH_ERROR_NONE, NULL,
-                               user_info->cb, user_info->user_data);
-       }
-
-       BT_DBG("-");
-}
-
-static void __bt_hdp_internal_handle_disconnect(DBusMessage *msg)
-{
-       const char *path = dbus_message_get_path(msg);
-       const char *obj_channel_path;
-       char address[BT_ADDRESS_STRING_SIZE] = { 0, };
-       bluetooth_device_address_t device_addr = { {0} };
-       bt_hdp_disconnected_t dis_ind;
-       hdp_obj_info_t *info;
-       bt_user_info_t *user_info;
-
-       BT_INFO("+********Signal - ChannelDeleted ******\n\n");
-       BT_DBG("Path = %s", path);
-
-       if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH,
-                               &obj_channel_path, DBUS_TYPE_INVALID)) {
-               BT_ERR("Unexpected parameters in ChannelDeleted signal");
-               return;
-       }
-
-       BT_INFO("Channel Deleted, Path = %s", obj_channel_path);
-
-       info = __bt_hdp_internal_gslist_obj_find_using_path(obj_channel_path);
-       if (!info) {
-               BT_ERR("No obj info for ob_channel_path [%s]\n", obj_channel_path);
-               return;
-       }
-
-       /*Since bluetoothd is not sending the ChannelDeleted signal */
-       _bt_device_path_to_address(path, address);
-
-       _bt_convert_addr_string_to_type(device_addr.addr, address);
-
-       dis_ind.channel_id = info->fd;
-       dis_ind.device_address = device_addr;
-
-       user_info = _bt_get_user_data(BT_COMMON);
-
-       if (user_info->cb) {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
-                               BLUETOOTH_ERROR_NONE, &dis_ind,
-                               user_info->cb, user_info->user_data);
-       }
-
-       BT_DBG(" Removed connection from list\n");
-
-       __bt_hdp_obj_info_free(info);
-
-}
-
-static void __bt_hdp_internal_handle_property_changed(DBusMessage *msg)
-{
-       const char *path = dbus_message_get_path(msg);
-       DBusMessageIter item_iter;
-       DBusMessageIter value_iter;
-       const char *property;
-       const char *obj_main_channel_path;
-
-       BT_DBG("+*******Signal - PropertyChanged*******\n");
-       BT_DBG("Path = %s", path);
-
-       dbus_message_iter_init(msg, &item_iter);
-
-       if (dbus_message_iter_get_arg_type(&item_iter) != DBUS_TYPE_STRING) {
-               BT_ERR("This is bad format dbus");
-               return;
-       }
-
-       dbus_message_iter_get_basic(&item_iter, &property);
-
-       ret_if(property == NULL);
-
-       BT_DBG("Property (%s)\n", property);
-
-       if (0 == g_strcmp0(property, "MainChannel")) {
-               BT_INFO("Property MainChannel received");
-
-               dbus_message_iter_next(&item_iter);
-
-               dbus_message_iter_recurse(&item_iter, &value_iter);
-
-               dbus_message_iter_get_basic(&value_iter,
-                                               &obj_main_channel_path);
-               BT_DBG("Path = %s", path);
-
-               BT_DBG("Main Channel  Path = %s", obj_main_channel_path);
-       }
-       BT_DBG("-*************\n");
-}
-
-static int __bt_hdp_internal_acquire_fd(const char *path)
-{
-       char address[BT_ADDRESS_STRING_SIZE] = { 0, };
-       bluetooth_device_address_t device_addr = { {0} };
-       DBusMessageIter reply_iter;
-       DBusMessageIter reply_iter_entry;
-       const char *property;
-       char *type_qos = NULL;
-       char *device = NULL;;
-       char *app_handle = NULL;;
-       hdp_app_list_t *list = NULL;;
-       DBusMessage *msg;
-       DBusMessage *reply;
-       DBusConnection *conn;
-       bt_hdp_connected_t conn_ind;
-       DBusError err;
-       int fd;
-       bt_user_info_t *user_info;
-       char *dev_path;
-
-       BT_DBG("+");
-
-       conn = _bt_get_system_conn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       msg = dbus_message_new_method_call(BT_BLUEZ_NAME, path,
-                                         BLUEZ_HDP_CHANNEL_INTERFACE,
-                                         "Acquire");
-
-       retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_error_init(&err);
-
-       reply = dbus_connection_send_with_reply_and_block(conn, msg,
-                                                       -1, &err);
-
-       dbus_message_unref(msg);
-
-       if (!reply) {
-               BT_ERR(" HDP:****** dbus Can't create application ****");
-
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("%s", err.message);
-                       dbus_error_free(&err);
-               }
-
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       if (!dbus_message_get_args(reply, &err, DBUS_TYPE_UNIX_FD, &fd,
-                               DBUS_TYPE_INVALID)) {
-               BT_ERR(" HDP:dbus Can't get reply arguments");
-
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("%s", err.message);
-                       dbus_error_free(&err);
-               }
-               goto error;
-       }
-
-       dbus_message_unref(reply);
-
-       BT_DBG("File Descriptor = %d, Dev_path = %s \n", fd, path);
-
-       msg = dbus_message_new_method_call(BT_BLUEZ_NAME, path,
-                       BT_PROPERTIES_INTERFACE, "GetAll");
-       dev_path = g_strdup(BLUEZ_HDP_CHANNEL_INTERFACE);
-       dbus_message_append_args(msg, DBUS_TYPE_STRING, &dev_path,
-                                       DBUS_TYPE_INVALID);
-       g_free(dev_path);
-
-       retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
-
-       dbus_error_init(&err);
-
-       reply = dbus_connection_send_with_reply_and_block(conn, msg,
-                                                       -1, &err);
-
-       dbus_message_unref(msg);
-
-       if (!reply) {
-               BT_ERR(" HDP:dbus Can't get the reply");
-
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("%s", err.message);
-                       dbus_error_free(&err);
-               }
-
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-       dbus_message_iter_init(reply, &reply_iter);
-
-       if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_ARRAY) {
-               BT_ERR("Can't get reply arguments - DBUS_TYPE_ARRAY\n");
-               goto error;
-       }
-
-       dbus_message_iter_recurse(&reply_iter, &reply_iter_entry);
-
-       /*Parse the dict */
-       while (dbus_message_iter_get_arg_type(&reply_iter_entry) ==
-                                               DBUS_TYPE_DICT_ENTRY) {
-               DBusMessageIter dict_entry, dict_entry_val;
-               dbus_message_iter_recurse(&reply_iter_entry, &dict_entry);
-               dbus_message_iter_get_basic(&dict_entry, &property);
-               BT_DBG("String received = %s\n", property);
-
-               if (g_strcmp0("Type", property) == 0) {
-                       dbus_message_iter_next(&dict_entry);
-                       dbus_message_iter_recurse(&dict_entry, &dict_entry_val);
-                       if (dbus_message_iter_get_arg_type(&dict_entry_val) !=
-                                               DBUS_TYPE_STRING)
-                               continue;
-
-                       dbus_message_iter_get_basic(&dict_entry_val, &type_qos);
-
-               } else if (g_strcmp0("Device", property) == 0) {
-                       dbus_message_iter_next(&dict_entry);
-                       dbus_message_iter_recurse(&dict_entry, &dict_entry_val);
-                       if (dbus_message_iter_get_arg_type(&dict_entry_val) !=
-                                               DBUS_TYPE_OBJECT_PATH)
-                               continue;
-
-                       dbus_message_iter_get_basic(&dict_entry_val, &device);
-
-               } else if (g_strcmp0("Application", property) == 0) {
-                       dbus_message_iter_next(&dict_entry);
-                       dbus_message_iter_recurse(&dict_entry, &dict_entry_val);
-                       if (dbus_message_iter_get_arg_type(&dict_entry_val) !=
-                                               DBUS_TYPE_OBJECT_PATH)
-                               continue;
-
-                       dbus_message_iter_get_basic(&dict_entry_val,
-                                                       &app_handle);
-               }
-               dbus_message_iter_next(&reply_iter_entry);
-       }
-
-       BT_DBG("QOS = %s, Device = %s, Apphandler = %s",
-                       type_qos, device, app_handle);
-
-       if (NULL == type_qos || NULL == app_handle) {
-               BT_ERR("Pasing failed\n");
-               goto error;
-       }
-
-       list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
-
-       /*Only process register with app handle receive the Connected event */
-       if (NULL == list) {
-               BT_ERR("**** Could not locate the list for %s*****\n", app_handle);
-               goto error;
-       }
-
-       hdp_obj_info_t *info = g_new0(hdp_obj_info_t, 1);
-       info->fd = fd;
-       info->obj_channel_path = g_strdup(path);
-       info->watch_id = __bt_hdp_internal_watch_fd(fd, info->obj_channel_path);
-       list->obj_info = g_slist_append(list->obj_info, info);
-
-       _bt_device_path_to_address(path, address);
-
-       _bt_convert_addr_string_to_type(device_addr.addr, address);
-
-       conn_ind.app_handle = app_handle;
-       conn_ind.channel_id = fd;
-       conn_ind.device_address = device_addr;
-       conn_ind.type = (g_strcmp0(type_qos, "Reliable") == 0) ?
-                       HDP_QOS_RELIABLE : HDP_QOS_STREAMING;
-
-       BT_DBG("Going to give callback\n");
-
-       user_info = _bt_get_user_data(BT_COMMON);
-
-       if (user_info->cb) {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                               BLUETOOTH_ERROR_NONE, &conn_ind,
-                               user_info->cb, user_info->user_data);
-       }
-
-       dbus_message_unref(reply);
-
-       BT_DBG("Updated fd in the list*\n");
-       BT_DBG("-\n");
-
-       return BLUETOOTH_ERROR_NONE;
- error:
-       dbus_message_unref(reply);
-       return BLUETOOTH_ERROR_INTERNAL;
-}
-
-static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path)
-{
-       GIOChannel *gio;
-       guint id;
-
-       BT_DBG("+");
-
-       gio = g_io_channel_unix_new(file_desc);
-
-       g_io_channel_set_close_on_unref(gio, TRUE);
-
-       id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
-                       __bt_hdp_internal_data_received, (void *)path);
-       BT_DBG("-");
-       return id;
-}
-
-
-static void __bt_hdp_internal_handle_disconnect_cb(int sk, const char *path)
-{
-       char address[BT_ADDRESS_STRING_SIZE] = { 0, };
-       bluetooth_device_address_t device_addr = { {0} };
-       bt_hdp_disconnected_t dis_ind;
-       hdp_obj_info_t *info;
-       bt_user_info_t *user_info;
-
-       BT_INFO("******** Socket Error  ******\n");
-
-       info = __bt_hdp_internal_gslist_obj_find_using_path(path);
-       ret_if(info == NULL);
-
-       /*Since bluetoothd is not sending the ChannelDeleted signal */
-       _bt_device_path_to_address(path, address);
-
-       _bt_convert_addr_string_to_type(device_addr.addr, address);
-
-       dis_ind.channel_id = sk;
-       dis_ind.device_address = device_addr;
-
-       user_info = _bt_get_user_data(BT_COMMON);
-
-       if (user_info->cb) {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
-                               BLUETOOTH_ERROR_NONE, &dis_ind,
-                               user_info->cb, user_info->user_data);
-       }
-
-       BT_DBG(" Removed connection from list\n");
-
-       __bt_hdp_obj_info_free(info);
-}
-
-static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
-                                       GIOCondition cond, gpointer data)
-{
-       char buff[HDP_BUFFER_SIZE] = { 0, };
-       int sk;
-       int act_read;
-       bt_hdp_data_ind_t data_ind = { 0, };
-       const char *path = (const char *)data;
-       bt_user_info_t *user_info;
-
-       BT_DBG("+");
-
-       sk = g_io_channel_unix_get_fd(gio);
-
-       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
-               BT_DBG("GIOCondition %d.............path = %s\n", cond, path);
-                __bt_hdp_internal_handle_disconnect_cb(sk, path);
-               return FALSE;
-       }
-
-       act_read = recv(sk, (void *)buff, sizeof(buff), 0);
-
-       if (act_read > 0) {
-               BT_DBG("Received data of %d\n", act_read);
-       } else {
-               BT_ERR("Read failed.....\n");
-               __bt_hdp_internal_handle_disconnect_cb(sk, path);
-               return FALSE;
-       }
-
-       data_ind.channel_id = sk;
-       data_ind.buffer = buff;
-       data_ind.size = act_read;
-
-       user_info = _bt_get_user_data(BT_COMMON);
+typedef struct {
+       int fd;
+       unsigned int channel_id;
+       guint watch_id;
+       void *app_handle;
+} hdp_obj_info_t;
 
-       if (user_info->cb) {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
-                               BLUETOOTH_ERROR_NONE, &data_ind,
-                               user_info->cb, user_info->user_data);
-       }
+typedef struct {
+       void *app_handle;
+       GSList *obj_info;
+} hdp_app_list_t;
 
-       BT_DBG("-\n");
+static GSList *g_app_list = NULL;
 
-       return TRUE;
-}
+/* Variable for privilege, only for write API,
+ * before we should reduce time to bt-service dbus calling
+ * -1 : Don't have a permission to access API
+ * 0 : Initial value, not yet check
+ * 1 : Have a permission to access API
+ */
+static int privilege_token;
 
-BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
+static void __bt_hdp_obj_info_free(hdp_obj_info_t *info)
 {
        BT_DBG("+");
 
-       BT_CHECK_ENABLED(return);
-       BT_CHECK_PARAMETER(app_handle, return);
+       ret_if(NULL == info);
+
+       g_source_remove(info->watch_id);
+       close(info->fd);
+       g_free(info);
 
-       return __bt_hdp_internal_destroy_application(app_handle);
+       BT_DBG("-");
 }
 
 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle)
@@ -869,16 +79,16 @@ static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handl
                hdp_app_list_t *list = l->data;
 
                if (list) {
-                       BT_DBG("found app_handle=%s\n", (char *)list->app_handle);
                        if (0 == g_strcmp0((char *)list->app_handle,
                                                (char *)app_handle))
                                return list;
                }
        }
+
        return NULL;
 }
 
-static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
+static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_ch_id(unsigned int channel_id)
 {
        GSList *l;
        GSList *iter;
@@ -897,154 +107,312 @@ static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
                        if (!info)
                                return NULL;
 
-                       if (fd == info->fd)
+                       if (channel_id == info->channel_id)
                                return info;
                }
        }
+
        return NULL;
 }
 
-static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path)
+static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(unsigned int sock_fd)
 {
        GSList *l;
        GSList *iter;
-       hdp_obj_info_t *info = NULL;
 
        retv_if(g_app_list == NULL, NULL);
 
        BT_DBG("List length = %d\n", g_slist_length(g_app_list));
+
        for (l = g_app_list; l != NULL; l = l->next) {
                hdp_app_list_t *list = l->data;
                if (!list)
                        return NULL;
 
                for (iter = list->obj_info; iter != NULL; iter = iter->next) {
-                        info = iter->data;
+                       hdp_obj_info_t *info = iter->data;
                        if (!info)
                                return NULL;
 
-                       if (0 == g_strcmp0(info->obj_channel_path, obj_channel_path)) {
-                               list->obj_info = g_slist_remove(list->obj_info, info);
+                       if ((int)sock_fd == info->fd)
                                return info;
-                       }
                }
        }
+
        return NULL;
 }
 
-static gboolean  __bt_hdp_internal_destroy_application_cb(gpointer data)
+static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
+                                       GIOCondition cond, gpointer data)
 {
-       const char *app_handle;
-       hdp_app_list_t *list = NULL;
-       app_handle = (const char *)data;
+       int fd;
+       gsize len = 0;
+       bt_user_info_t *user_info;
+       char buff[HDP_BUFFER_SIZE] = { 0, };
+       GError *err = NULL;
+       GIOStatus status = G_IO_STATUS_NORMAL;
+       bluetooth_device_address_t *address = data;
+       hdp_obj_info_t *info;
 
        BT_DBG("+");
 
-       list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
-       if (NULL == list) {
-               BT_ERR("**** list not found for %s ******\n", app_handle);
+       fd = g_io_channel_unix_get_fd(gio);
+       BT_DBG("fd: %d", fd);
+
+       info = __bt_hdp_internal_gslist_obj_find_using_fd(fd);
+       if (!info) {
+               BT_INFO("No obj info found for fd: %d", fd);
+               g_free(address);
                return FALSE;
        }
 
-       g_app_list = g_slist_remove(g_app_list, list);
+       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+               bt_hdp_disconnected_t disconn_info;
+               hdp_app_list_t *list;
 
-       g_free(list->app_handle);
-       g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
-       g_free(list);
+               BT_DBG("GIOCondition %d", cond);
+               disconn_info.channel_id = info->channel_id;
+               memcpy(&disconn_info.device_address, address, sizeof(bluetooth_device_address_t));
 
-       BT_DBG("List length = %d\n", g_slist_length(g_app_list));
+               list = __bt_hdp_internal_gslist_find_app_handler(info->app_handle);
+               if (list) {
+                       list->obj_info = g_slist_remove(list->obj_info, info);
+                       __bt_hdp_obj_info_free(info);
+               }
+
+               user_info = _bt_get_user_data(BT_COMMON);
+               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
+                               BLUETOOTH_ERROR_NONE, &disconn_info,
+                               user_info->cb, user_info->user_data);
+               g_free(address);
+               return FALSE;
+       }
+
+       status = g_io_channel_read_chars(gio, buff, HDP_BUFFER_SIZE, &len, &err);
+       if (status != G_IO_STATUS_NORMAL) {
+               BT_ERR("IO Channel read is failed with %d", status);
+               if (err) {
+                       BT_ERR("IO Channel read error [%s]", err->message);
+                       g_error_free(err);
+               }
+               g_free(address);
+               return FALSE;
+       }
+
+       BT_DBG("fd: %d, len: %zd, buffer: %s", fd, len, buff);
+
+       user_info = _bt_get_user_data(BT_COMMON);
+       if (user_info->cb) {
+               bt_hdp_data_ind_t data_ind = { 0, };
+
+               data_ind.channel_id = info->channel_id;
+               data_ind.buffer = buff;
+               data_ind.size = len;
+               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
+                               BLUETOOTH_ERROR_NONE, &data_ind,
+                               user_info->cb, user_info->user_data);
+       }
 
-       if (0 == g_slist_length(g_app_list))
-               __bt_hdp_internal_remove_filter();
        BT_DBG("-");
-       return FALSE;
+       return TRUE;
 }
 
-static int __bt_hdp_internal_destroy_application(const char *app_handle)
+static void __hdp_handle_new_connection(bt_hdp_connected_t *conn_info, int fd)
 {
-       DBusMessage *msg;
-       DBusMessage *reply;
-       DBusError err;
-       DBusConnection *conn;
-       int result = BLUETOOTH_ERROR_NONE;
+       hdp_obj_info_t *info;
+       hdp_app_list_t *list;
+       bluetooth_device_address_t *address;
+       GIOChannel *gio;
+
+       BT_DBG("+");
+
+       list = __bt_hdp_internal_gslist_find_app_handler((void *)conn_info->app_handle);
+       if (NULL == list) {
+               BT_ERR("**** Could not locate the list for %s*****\n", conn_info->app_handle);
+               return;
+       }
+
+       info = g_new0(hdp_obj_info_t, 1);
+       info->channel_id = conn_info->channel_id;
+       info->app_handle = list->app_handle;
+       info->fd = fd;
+       address = g_memdup(&(conn_info->device_address), sizeof(bluetooth_device_address_t));
 
-       conn = _bt_get_system_conn();
-       retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
+       gio = g_io_channel_unix_new(fd);
+       g_io_channel_set_close_on_unref(gio, TRUE);
+       info->watch_id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+                       __bt_hdp_internal_data_received, (void *)address);
+       g_io_channel_unref(gio);
+       list->obj_info = g_slist_append(list->obj_info, info);
 
-       msg = dbus_message_new_method_call(BT_BLUEZ_NAME, "/org/bluez",
-                       BLUEZ_HDP_MANAGER_INTERFACE, "DestroyApplication");
+       BT_DBG("-");
+}
 
-       retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
+void _bt_hdp_app_remove_obj_info(unsigned int channel_id)
+{
+       hdp_app_list_t *list;
+       hdp_obj_info_t *info;
 
-       dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH, &app_handle,
-                                       DBUS_TYPE_INVALID);
+       BT_DBG("+");
 
-       dbus_error_init(&err);
+       info = __bt_hdp_internal_gslist_obj_find_using_ch_id(channel_id);
+       ret_if(NULL == info);
 
-       reply = dbus_connection_send_with_reply_and_block(conn, msg,
-                                                       -1, &err);
-       dbus_message_unref(msg);
-       if (!reply) {
-               BT_ERR(" HDP:dbus Can't Destroy application");
+       list = __bt_hdp_internal_gslist_find_app_handler(info->app_handle);
+       ret_if(NULL == list);
 
-               if (dbus_error_is_set(&err)) {
-                       BT_ERR("%s", err.message);
+       list->obj_info = g_slist_remove(list->obj_info, info);
+       BT_DBG("obj_info length = %d\n", g_slist_length(list->obj_info));
+       __bt_hdp_obj_info_free(info);
 
-                       if (g_strrstr(err.message, BT_ACCESS_DENIED_MSG))
-                               result  = BLUETOOTH_ERROR_ACCESS_DENIED;
-                       else
-                               result  = BLUETOOTH_ERROR_INTERNAL;
+       BT_DBG("-");
+}
 
-                       dbus_error_free(&err);
-               }
+int _bt_hdp_app_acquire_fd(bt_hdp_connected_t *conn_info)
+{
+       int result;
+       GUnixFDList *out_fd_list = NULL;
 
-               return result ;
-       }
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
-       dbus_message_unref(reply);
+       g_array_append_vals(in_param1, &conn_info->channel_id, sizeof(int));
+       result = _bt_send_request_with_unix_fd_list(BT_BLUEZ_SERVICE, BT_HDP_GET_FD,
+                       in_param1, in_param2, in_param3, in_param4, NULL, &out_param, &out_fd_list);
 
-       BT_DBG("Destroyed health application: %s", (char *)app_handle);
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
-       g_idle_add(__bt_hdp_internal_destroy_application_cb,
-                       (gpointer)app_handle);
+       BT_DBG("result: %x", result);
+       if (result != BLUETOOTH_ERROR_NONE || NULL == out_fd_list) {
+               BT_ERR("out_fd_list is NULL");
+               bluetooth_hdp_disconnect(conn_info->channel_id, &conn_info->device_address);
+               return BLUETOOTH_ERROR_INTERNAL;
+       } else {
+               int *fd_list_array;
+               int len = 0;
+
+               fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+               BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+               __hdp_handle_new_connection(conn_info, fd_list_array[0]);
+               g_free(fd_list_array);
+               g_object_unref(out_fd_list);
+       }
 
+       BT_DBG("-");
        return BLUETOOTH_ERROR_NONE;
 }
 
-static void __bt_hdp_internal_remove_filter(void)
+/**********************************************************************
+*                      Health device APIs (HDP)                        *
+***********************************************************************/
+BT_EXPORT_API int bluetooth_hdp_activate(unsigned short data_type,
+                                       bt_hdp_role_type_t role,
+                                       bt_hdp_qos_type_t channel_type,
+                                       char **app_handle)
 {
+       int result = BLUETOOTH_ERROR_NONE;
+
        BT_DBG("+");
 
-       ret_if(g_hdp_dus_conn == NULL);
+       BT_CHECK_ENABLED(return);
+
+       /*For source role is mandatory */
+       if (role == HDP_ROLE_SOURCE && channel_type == HDP_QOS_ANY) {
+               BT_ERR("For source, type is mandatory - Reliable/Streaming");
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
+
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       g_array_append_vals(in_param1, &data_type, sizeof(short));
+       g_array_append_vals(in_param2, &role, sizeof(bt_hdp_role_type_t));
+       g_array_append_vals(in_param3, &channel_type, sizeof(bt_hdp_qos_type_t));
+
+       result = _bt_send_request(BT_BLUEZ_SERVICE, BT_HDP_REGISTER_SINK_APP,
+               in_param1, in_param2, in_param3, in_param4, &out_param);
 
-       dbus_connection_remove_filter(g_hdp_dus_conn,
-                                       __bt_hdp_internal_event_filter, NULL);
+       if (result == BLUETOOTH_ERROR_NONE) {
+               char *buf;
+               hdp_app_list_t *list;
 
-       g_hdp_dus_conn = NULL;  /*should not unref here, bcz no ++reff */
+               buf = &g_array_index(out_param, char, 0);
+               BT_DBG("Created app: %s", buf);
+
+               list = g_new0(hdp_app_list_t, 1);
+               list->app_handle = (void *)g_strdup(buf);
+               *app_handle = list->app_handle;
+               g_app_list = g_slist_append(g_app_list, list);
+       }
+
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
        BT_DBG("-");
+       return result;
+}
+
+BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
+{
+       int result;
+       hdp_app_list_t *list;
+
+       BT_DBG("+");
+
+       BT_CHECK_ENABLED(return);
+       BT_CHECK_PARAMETER(app_handle, return);
+
+       list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
+       if (NULL == list) {
+               BT_ERR("**** list not found for %s ******\n", app_handle);
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
+
+       BT_DBG("app_handle: %s", app_handle);
+
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       g_array_append_vals(in_param1, app_handle, (strlen(app_handle) + 1));
+
+       result = _bt_send_request(BT_BLUEZ_SERVICE, BT_HDP_UNREGISTER_SINK_APP,
+               in_param1, in_param2, in_param3, in_param4, &out_param);
+       if (result == BLUETOOTH_ERROR_NONE) {
+               g_app_list = g_slist_remove(g_app_list, list);
+               g_free(list->app_handle);
+               g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
+               g_free(list);
+       }
+
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       return result;
 }
 
 BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
-                                           const char *buffer,
-                                           unsigned int size)
+               const char *buffer,
+               unsigned int size)
 {
-       int wbytes = 0;
+       unsigned int wbytes = 0;
        int written = 0;
+       hdp_obj_info_t *info;
        int result;
 
        BT_DBG("+");
 
        BT_CHECK_ENABLED(return);
 
-       if ((channel_id == 0) || (NULL == buffer) || (size == 0)) {
-               BT_ERR("Invalid arguments..\n");
+       retv_if(NULL == buffer, BLUETOOTH_ERROR_INVALID_PARAM);
+       retv_if(0 == size, BLUETOOTH_ERROR_INVALID_PARAM);
+
+       info = __bt_hdp_internal_gslist_obj_find_using_ch_id(channel_id);
+       if (NULL == info) {
+               BT_ERR("*** Could not locate the info for %d*****", channel_id);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
        switch (privilege_token) {
        case 0:
-               result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_SEND_DATA);
+               result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HDP_SEND_DATA);
 
                if (result == BLUETOOTH_ERROR_NONE) {
                        privilege_token = 1; /* Have a permission */
@@ -1067,7 +435,7 @@ BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
        }
 
        while (wbytes < size) {
-               written = write(channel_id, buffer + wbytes, size - wbytes);
+               written = write(info->fd, (buffer + wbytes), (size - wbytes));
                if (written <= 0) {
                        BT_ERR("write failed..\n");
                        return BLUETOOTH_ERROR_NOT_IN_OPERATION;
@@ -1078,285 +446,80 @@ BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
        return BLUETOOTH_ERROR_NONE;
 }
 
-static void __bt_hdp_connect_request_cb(GDBusProxy *hdp_proxy,
-                               GAsyncResult *res, gpointer user_data)
-{
-       GError *err = NULL;
-       char *obj_connect_path = NULL;
-       bt_hdp_connected_t *conn_ind = user_data;
-       bt_user_info_t *user_info;
-       GVariant *reply = NULL;
-       int ret = BLUETOOTH_ERROR_NONE;
-
-       reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
-
-       g_object_unref(hdp_proxy);
-
-       if (!reply) {
-               if (err) {
-                       BT_ERR("HDP connection  Dbus Call Error: %s\n", err->message);
-                       g_clear_error(&err);
-               }
-
-               user_info = _bt_get_user_data(BT_COMMON);
-
-               if (user_info->cb) {
-                       _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                                       BLUETOOTH_ERROR_CONNECTION_ERROR, conn_ind,
-                                       user_info->cb, user_info->user_data);
-               }
-       } else {
-               g_variant_get(reply, "(&o)", &obj_connect_path);
-
-               BT_DBG("Obj Path returned = %s\n", obj_connect_path);
-               user_info = _bt_get_user_data(BT_COMMON);
-
-               ret = __bt_hdp_internal_acquire_fd(obj_connect_path);
-               if (ret != BLUETOOTH_ERROR_NONE) {
-                       user_info = _bt_get_user_data(BT_COMMON);
-                       if (user_info->cb) {
-                               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
-                                               BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
-                                                       user_info->cb, user_info->user_data);
-                       }
-               }
-               g_variant_unref(reply);
-       }
-       g_free((void *)conn_ind->app_handle);
-       g_free(conn_ind);
-}
-
-
 BT_EXPORT_API int bluetooth_hdp_connect(const char *app_handle,
                        bt_hdp_qos_type_t channel_type,
                        const bluetooth_device_address_t *device_address)
 {
-       GError *err = NULL;
-       GDBusConnection *conn = NULL;
-       GDBusProxy *hdp_proxy = NULL;
-       bt_hdp_connected_t *param;
-       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
-       char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
-       char *dev_path = NULL;
-       char *role;
-
-       BT_DBG("+");
+       int result;
+       hdp_app_list_t *list;
+       bt_user_info_t *user_info;
 
        BT_CHECK_ENABLED(return);
        BT_CHECK_PARAMETER(app_handle, return);
        BT_CHECK_PARAMETER(device_address, return);
 
-       if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_CONNECT)
-            == BLUETOOTH_ERROR_PERMISSION_DEINED) {
-               BT_ERR("Don't have a privilege to use this API");
-               return BLUETOOTH_ERROR_PERMISSION_DEINED;
-       }
-
-       if (channel_type == HDP_QOS_RELIABLE) {
-               role = "Reliable";
-       } else if (channel_type == HDP_QOS_STREAMING) {
-               role = "Streaming";
-       } else if (channel_type == HDP_QOS_ANY) {
-               role = "Any";
-       } else {
-               BT_ERR("Invalid channel_type %d", channel_type);
-               return BLUETOOTH_ERROR_ACCESS_DENIED;
-       }
-
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-
-       if (err) {
-               BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
-               g_clear_error(&err);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       /* If the adapter path is wrong, we can think the BT is not enabled. */
-       if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
-                                       default_adapter_path) < 0) {
-               BT_ERR("Could not get adapter path\n");
-               g_object_unref(conn);
-               return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
-       }
-
-       _bt_convert_addr_type_to_string(address,
-                               (unsigned char *)device_address->addr);
-
-       BT_DBG("create conection to %s", address);
-
-       dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
-
-       if (dev_path == NULL) {
-               g_object_unref(conn);
-               return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-       }
-
-       g_strdelimit(dev_path, ":", '_');
-
-       BT_DBG("path: %s", dev_path);
-
-       hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
-                                               NULL, BT_BLUEZ_NAME,
-                                               dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
-                                               NULL, NULL);
-       g_object_unref(conn);
+       user_info = _bt_get_user_data(BT_COMMON);
+       retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       if (hdp_proxy == NULL) {
-               BT_ERR("Failed to get the HDP server proxy\n");
-               g_free(dev_path);
-               return BLUETOOTH_ERROR_NOT_PAIRED;
+       list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
+       if (NULL == list) {
+               BT_ERR("**** list not found for %s ******\n", app_handle);
+               return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
-       BT_DBG("app path %s\n", app_handle);
-
-       param = g_new0(bt_hdp_connected_t, 1);
-       param->app_handle = g_strdup(app_handle);
-       memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
-       param->type = channel_type;
-
-       g_dbus_proxy_call(hdp_proxy, "CreateChannel",
-                               g_variant_new("(os)", app_handle, role),
-                               G_DBUS_CALL_FLAGS_NONE, -1, NULL,
-                               (GAsyncReadyCallback)__bt_hdp_connect_request_cb,
-                               param);
-
-       g_free(dev_path);
-
-       return BLUETOOTH_ERROR_NONE;
-
-}
-
-static void __bt_hdp_disconnect_request_cb(GDBusProxy *hdp_proxy,
-                       GAsyncResult *res, gpointer user_data)
-{
-       GError *err = NULL;
-       bt_hdp_disconnected_t *disconn_ind = user_data;
-       bt_user_info_t *user_info;
-       GVariant *reply = NULL;
+       BT_DBG("app_handle: %s", app_handle);
 
-       reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
-       g_object_unref(hdp_proxy);
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
-       user_info = _bt_get_user_data(BT_COMMON);
-       if (user_info == NULL || user_info->cb == NULL) {
-               g_free(disconn_ind);
-               if (err) {
-                               g_clear_error(&err);
-                       return;
-               }
-               g_variant_unref(reply);
-               return;
-       }
+       g_array_append_vals(in_param1, app_handle, (strlen(app_handle) + 1));
+       g_array_append_vals(in_param2, &channel_type, sizeof(bt_hdp_qos_type_t));
+       g_array_append_vals(in_param3, device_address, sizeof(bluetooth_device_address_t));
 
-       if (!reply) {
-               if (err) {
-                       BT_ERR("HDP disconnection Dbus Call Error: %s\n",
-                                                       err->message);
-                       g_clear_error(&err);
-               }
+       result = _bt_send_request_async(BT_BLUEZ_SERVICE,
+                       BT_HDP_CONNECT, in_param1, in_param2, in_param3, in_param4,
+                       user_info->cb, user_info->user_data);
 
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
-                               BLUETOOTH_ERROR_CONNECTION_ERROR, disconn_ind,
-                               user_info->cb, user_info->user_data);
-       } else {
-               _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
-                               BLUETOOTH_ERROR_NONE, disconn_ind,
-                               user_info->cb, user_info->user_data);
-               BT_INFO("HDP disconnection Dbus Call is done\n");
-               g_variant_unref(reply);
-       }
+       if (result != BLUETOOTH_ERROR_NONE)
+               BT_ERR("BT_HDP_CONNECT failed");
 
-       g_free(disconn_ind);
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
+       return result;
 }
 
 BT_EXPORT_API int bluetooth_hdp_disconnect(unsigned int channel_id,
                        const bluetooth_device_address_t *device_address)
 {
-       GError *err = NULL;
-       GDBusConnection *conn = NULL;
-       GDBusProxy *hdp_proxy = NULL;
-       char address[BT_ADDRESS_STRING_SIZE] = { 0 };
-       char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
-       char *dev_path = NULL;
-       bt_hdp_disconnected_t *param;
-
-       BT_DBG("+\n");
+       int result;
+       hdp_obj_info_t *info;
+       bt_user_info_t *user_info;
 
        BT_CHECK_ENABLED(return);
        BT_CHECK_PARAMETER(device_address, return);
 
-       if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT)
-            == BLUETOOTH_ERROR_PERMISSION_DEINED) {
-               BT_ERR("Don't have a privilege to use this API");
-               return BLUETOOTH_ERROR_PERMISSION_DEINED;
-       }
+       user_info = _bt_get_user_data(BT_COMMON);
+       retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
 
-       hdp_obj_info_t *info =
-               __bt_hdp_internal_gslist_obj_find_using_fd(channel_id);
+       BT_DBG("channel_id: %d", channel_id);
+       info = __bt_hdp_internal_gslist_obj_find_using_ch_id(channel_id);
        if (NULL == info) {
-               BT_ERR("*** Could not locate the list for %d*****\n",
-                                                       channel_id);
+               BT_ERR("*** Could not locate the info for %d*****", channel_id);
                return BLUETOOTH_ERROR_INVALID_PARAM;
        }
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-
-       if (err) {
-               BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
-               g_clear_error(&err);
-               return BLUETOOTH_ERROR_INTERNAL;
-       }
-
-       /* If the adapter path is wrong, we can think the BT is not enabled. */
-       if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
-                                       default_adapter_path) < 0) {
-               BT_ERR("Could not get adapter path\n");
-               g_object_unref(conn);
-               return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
-       }
-
-       _bt_convert_addr_type_to_string(address,
-                               (unsigned char *)device_address->addr);
-
-       BT_DBG("create conection to  %s\n", address);
-
-       dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
-
-       if (dev_path == NULL) {
-               g_object_unref(conn);
-               return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
-       }
-
-       g_strdelimit(dev_path, ":", '_');
-
-       BT_DBG("path  %s\n", dev_path);
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
 
-       hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
-                                               NULL, BT_BLUEZ_NAME,
-                                               dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
-                                               NULL, NULL);
+       g_array_append_vals(in_param1, &channel_id, sizeof(int));
+       g_array_append_vals(in_param2, device_address, sizeof(bluetooth_device_address_t));
 
-       g_object_unref(conn);
-
-       if (hdp_proxy == NULL) {
-               BT_ERR("Failed to get the HDP proxy\n");
-               g_free(dev_path);
-               return BLUETOOTH_ERROR_NOT_PAIRED;
-       }
-
-       param = g_new0(bt_hdp_disconnected_t, 1);
-       param->channel_id = channel_id;
-       memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
-
-       g_dbus_proxy_call(hdp_proxy, "DestroyChannel",
-                               g_variant_new("o", info->obj_channel_path),
-                               G_DBUS_CALL_FLAGS_NONE, -1, NULL,
-                               (GAsyncReadyCallback)__bt_hdp_disconnect_request_cb,
-                               param);
-
-       g_free(dev_path);
-
-       return BLUETOOTH_ERROR_NONE;
+       result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT,
+                       in_param1, in_param2, in_param3, in_param4,
+                       user_info->cb, user_info->user_data);
+       if (result != BLUETOOTH_ERROR_NONE)
+               BT_ERR("BT_HDP_DISCONNECT failed");
 
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+       return result;
 }