Fix the coverity issues
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-gatt-server.c
index 90550f0..26d15c8 100644 (file)
@@ -40,6 +40,7 @@
 #include "bt-hal-utils.h"
 #include "bt-hal-dbus-common-utils.h"
 
+#include "bt-hal-gatt.h"
 #include "bt-hal-adapter-le.h"
 #include "bt-hal-event-receiver.h"
 
@@ -58,11 +59,10 @@ static GDBusProxy *manager_gproxy = NULL;
  **  Static variables
  ************************************************************************************/
 extern const btgatt_callbacks_t *bt_gatt_callbacks;
-guint owner_id;
-GDBusConnection *g_conn = NULL;
-//GDBusConnection *conn = NULL;
-GDBusNodeInfo *manager_node_info = NULL;
-guint manager_id;
+static guint owner_id;
+static GDBusConnection *g_conn = NULL;
+static GDBusNodeInfo *manager_node_info = NULL;
+static guint manager_id;
 
 /* Global handles which needs to be incremented during each addition */
 static int gatt_service_handle = 10;
@@ -135,7 +135,7 @@ static GSList *gatt_server_apps = NULL;
 
 static GSList *gatt_services = NULL;
 
-static int conn_id = 0;
+extern int hal_gatt_conn_id;
 
 /* Linked List of connected Remote GATT clients */
 static GSList *gatt_client_info_list = NULL;
@@ -195,9 +195,16 @@ struct hal_gatts_server_register_info_t {
 
 static GSList * hal_gatts_server_register_list;
 
-static int g_server_inst_id = 0;
 
-#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
+/* Should match the range with bt-service-gatt.c's MAX_APPS_SUPPORTED */
+/* TODO: Adjust MAX Server after modifying MAX app handle logic */
+#define BT_GATTS_MAX 11
+
+static int assigned_id = 0;
+
+static gboolean instance_id_used[BT_GATTS_MAX];
+
+#define CHECK_BTGATT_INIT() if (_bt_hal_gatt_interface_ready() == false)\
 {\
        ERR("%s: BTGATT not initialized", __FUNCTION__);\
        return BT_STATUS_NOT_READY;\
@@ -335,6 +342,73 @@ static const gchar manager_introspection_xml[] =
 "  </interface>"
 "</node>";
 
+static void __bt_free_gatt_server_app(struct gatt_server_app *app)
+{
+       g_free(app->app_path);
+       g_free(app);
+}
+
+static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
+{
+       if (!svc_info)
+               return;
+
+       g_free(svc_info->serv_path);
+       g_free(svc_info->service_uuid);
+       g_free(svc_info);
+}
+
+void _bt_hal_gatt_server_init(void)
+{
+       assigned_id = 0;
+       memset(instance_id_used, 0x00, sizeof(instance_id_used));
+
+       g_slist_free_full(gatt_server_apps, (GDestroyNotify)__bt_free_gatt_server_app);
+       gatt_server_apps = NULL;
+
+       g_slist_free_full(gatt_services, (GDestroyNotify)__bt_hal_gatt_free_service_info);
+       gatt_services = NULL;
+
+       g_slist_free_full(hal_gatts_server_register_list, g_free);
+       hal_gatts_server_register_list = NULL;
+}
+
+static int __bt_hal_gatt_assign_id(void)
+{
+       int index;
+
+       index = assigned_id + 1;
+
+       if (index >= BT_GATTS_MAX)
+               index = 1;
+
+       while (instance_id_used[index] == TRUE) {
+               if (index == assigned_id) {
+                       /* No available ID */
+                       ERR("All interface ID is used");
+                       return -1;
+               }
+
+               index++;
+
+               if (index >= BT_GATTS_MAX)
+                       index = 1;
+       }
+
+       assigned_id = index;
+       instance_id_used[index] = TRUE;
+
+       return assigned_id;
+}
+
+static void __bt_hal_gatt_delete_id(int instance_id)
+{
+       if (instance_id >= BT_GATTS_MAX || instance_id < 0)
+               return;
+
+       instance_id_used[instance_id] = FALSE;
+}
+
 static GSList *_bt_get_service_list_from_server(int instance)
 {
        GSList *l;
@@ -353,6 +427,24 @@ static GSList *_bt_get_service_list_from_server(int instance)
        return NULL;
 }
 
+static char *__bt_get_server_app_path_from_server(int instance)
+{
+       GSList *l;
+       INFO("Number of GATT Server apps [%d]", g_slist_length(gatt_server_apps));
+       INFO("Find App with slot [%d]", instance);
+
+       for (l = gatt_server_apps; l; l = g_slist_next(l)) {
+               struct gatt_server_app *app = (struct gatt_server_app *)l->data;
+
+               if (app->slot == instance) {
+                       INFO("App slot [%d] Found, App path [%s]",
+                                       app->slot, app->app_path);
+                       return app->app_path;
+               }
+       }
+       return NULL;
+}
+
 static void _bt_remote_service_from_gatt_server(int instance, int service_handle)
 {
        GSList *l;
@@ -415,10 +507,8 @@ static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *addres
                if (info == NULL)
                        continue;
 
-               if (!g_strcmp0(info->addr, address)) {
-                       INFO("Remote GATT client found addr[%s]", info->addr);
+               if (!g_strcmp0(info->addr, address))
                        return info;
-               }
        }
        return NULL;
 }
@@ -433,10 +523,8 @@ static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn(in
                if (info == NULL)
                        continue;
 
-               if (info->connection_id == conn_id) {
-                       INFO("Remote GATT client found addr[%s]", info->addr);
+               if (info->connection_id == conn_id)
                        return info;
-               }
        }
        return NULL;
 }
@@ -462,10 +550,8 @@ static struct gatt_req_info *__bt_find_remote_gatt_client_request_info(int conn_
                                if (info1 == NULL)
                                        continue;
 
-                               if (info1->request_id == trans_id) {
-                                       INFO("Remote GATT client found addr[%s]", info->addr);
+                               if (info1->request_id == trans_id)
                                        return info1;
-                               }
                        }
                }
        }
@@ -476,18 +562,8 @@ void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
 {
        struct hal_ev_gatt_server_connected ev;
        struct gatt_client_info_t *conn_info = NULL;
-       int instance = -1;
        memset(&ev, 0, sizeof(ev));
 
-       /* Find Server Instance */
-       instance = bt_hal_gatts_get_gatt_server_instance();
-       if (instance == -1) {
-               ERR("Not even a single GATT server is registered");
-               return;
-       }
-
-       DBG("server instance [%d]", instance);
-
        /* Convert address to hex */
        _bt_hal_convert_addr_string_to_type(ev.bdaddr, address);
 
@@ -495,7 +571,6 @@ void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
        /* Check if device is already in connected list */
        conn_info = __bt_find_remote_gatt_client_info(address);
 
-
        /* If disconnected, and conn info found, then remove conn info */
        if (is_connected == FALSE) {
                DBG("GATT Disconnected");
@@ -521,12 +596,26 @@ void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
                }
                /* If connected, and conn info NOT found, then add conn info */
        } else {
+               int instance = -1;
+
+               /* Find Server Instance */
+               instance = bt_hal_gatts_get_gatt_server_instance();
+               if (instance == -1) {
+                       ERR("Not even a single GATT server is registered");
+                       ev.conn_id = ++hal_gatt_conn_id;
+                       ev.server_instance = -1;
+                       ev.connected = is_connected;
+                       event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
+                       return;
+               }
+               DBG("server instance [%d]", instance);
+
                if (!conn_info) {
                        /* Save Connection info */
                        conn_info = g_new0(struct gatt_client_info_t, 1);
                        conn_info->addr = g_strdup(address);
-                       INFO("Added GATT client addr[%s]", conn_info->addr);
-                       conn_info->connection_id = ++conn_id;
+                       DBG("Added GATT client addr[%s]", conn_info->addr);
+                       conn_info->connection_id = ++hal_gatt_conn_id;
                        conn_info->instance_id = instance;
                        gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
                        INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
@@ -542,6 +631,8 @@ void _bt_hal_gatt_connected_state_event(gboolean is_connected, char *address)
 
                                event_cb(HAL_EV_GATT_SERVER_CONNECTED, (void *)&ev, sizeof(ev));
                        }
+               } else {
+                       ERR("Abnormal case. conn_info should be NULL");
                }
        }
        /* Send GATT connected or disconnected event */
@@ -1118,10 +1209,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                g_variant_get(parameters, "(&suq)",
                                        &addr, &req_id, &offset);
 
-               DBG("Request id = %u, Offset = %u", req_id, offset);
-               DBG("Application path = %s", object_path);
-               DBG("Sender = %s", sender);
-               DBG("Address = %s", addr);
+               DBG("ReadValue : req id  %u, offset %u, path %s, sender %s, %s", req_id, offset, object_path, sender, addr);
 
                /* Check if device is already in connected list */
                conn_info = __bt_find_remote_gatt_client_info(addr);
@@ -1129,6 +1217,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
 
                if (svc_info == NULL || conn_info == NULL) {
+                       ERR("Invalid information %p, %p", svc_info, conn_info);
                        g_dbus_method_invocation_return_value(invocation, NULL);
                        return;
                }
@@ -1179,9 +1268,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
 
                memset(&ev, 0, sizeof(ev));
 
-               DBG("WriteValue");
-               DBG("Application path = %s", object_path);
-               DBG("Sender = %s", sender);
+               DBG("WriteValue : Application path %s, sender %s", object_path, sender);
 
                g_variant_get(parameters, "(&suqb@ay)",
                                &addr, &req_id, &offset, &response_needed, &var);
@@ -1300,9 +1387,7 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
 
                struct hal_ev_gatt_server_indicate_cfm ev;
 
-               DBG("IndicateConfirm");
-               DBG("Application path = %s", object_path);
-               DBG("Sender = %s", sender);
+               DBG("IndicateConfirm : Application path %s, sender %s", object_path, sender);
 
                g_variant_get(parameters, "(&sb)", &addr, &complete);
                DBG("Remote Device address number = %s", addr);
@@ -1330,149 +1415,142 @@ static void __bt_gatt_char_method_call(GDBusConnection *connection,
                event_cb(HAL_EV_GATT_INDICATE_CFM, (void *)&ev, sizeof(ev));
        } else if (g_strcmp0(method_name, "AcquireWrite") == 0) {
 
-                               uint16_t mtu = 512;
-                               int char_hdl = -1;
-                               struct hal_ev_gatt_server_acquire_write_res  ev;
-                               struct gatt_service_info *svc_info = NULL;
-                               struct gatt_req_info *req_info = NULL;
-                               struct gatt_client_info_t *conn_info = NULL;
-                               char * dev_path = NULL;
-                               char * link = 0;
-                               char  addr[BT_HAL_ADDRESS_STRING_SIZE];
-
-                               DBG("AcquireWrite");
-                               DBG("Application path = %s", object_path);
-                               DBG("Sender = %s", sender);
-
-                               GVariantIter* iter;
-                               g_variant_get(parameters, "(a{sv})", &iter);
-                               char* key = NULL;
-                               GVariant* value = NULL;
-                               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
-                                               if (g_strcmp0(key, "MTU") == 0)
-                                                       g_variant_get(value, "i", &mtu);
-                                               else if (g_strcmp0(key, "link") == 0)
-                                                       g_variant_get(value, "s", &link);
-                                               else if (g_strcmp0(key, "device") == 0)
-                                                       g_variant_get(value, "o", &dev_path);
-                               }
+               uint16_t mtu = 512;
+               int char_hdl = -1;
+               struct hal_ev_gatt_server_acquire_write_res  ev;
+               struct gatt_service_info *svc_info = NULL;
+               struct gatt_req_info *req_info = NULL;
+               struct gatt_client_info_t *conn_info = NULL;
+               char *dev_path = NULL;
+               char *link = NULL;
+               char  addr[BT_HAL_ADDRESS_STRING_SIZE];
+
+               DBG("AcquireWrite : Application path %s, sender %s", object_path, sender);
+
+               GVariantIter* iter;
+               g_variant_get(parameters, "(a{sv})", &iter);
+               char *key = NULL;
+               GVariant *value = NULL;
+               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
+                               if (g_strcmp0(key, "MTU") == 0)
+                                       g_variant_get(value, "q", &mtu);
+                               else if (g_strcmp0(key, "link") == 0)
+                                       g_variant_get(value, "s", &link);
+                               else if (g_strcmp0(key, "device") == 0)
+                                       g_variant_get(value, "o", &dev_path);
+               }
 
-                               DBG("AcquireWrite   values retived");
-                               DBG(" path  %s LINK = %s, MTU = %u", addr, link, mtu);
+               DBG(" path  %s LINK = %s, MTU = %u", dev_path, link, mtu);
 
-                               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
+               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
+
+               _bt_hal_convert_device_path_to_address(dev_path, addr);
 
-                               _bt_hal_convert_device_path_to_address(dev_path, addr);
+               DBG("remote address %s", addr);
+               g_free(link);
+               g_free(dev_path);
 
-                               DBG("remote address %s", addr);
+               /* Check if device is already in connected list */
+               conn_info = __bt_find_remote_gatt_client_info(addr);
+               if (conn_info == NULL) {
+                       ERR("Cleint info not found\n");
+                       g_variant_iter_free(iter);
+                       goto done;
+               }
 
-                               /* Check if device is already in connected list */
-                               conn_info = __bt_find_remote_gatt_client_info(addr);
+               if (svc_info == NULL) {
+                       ERR("svc_info  info not found\n");
+                       g_variant_iter_free(iter);
+                       goto done;
+               }
 
-                               if (conn_info == NULL) {
-                                       ERR("Cleint info not found\n");
-                                       goto done;
-                               }
+               /* Store requets information */
+               req_info = g_new0(struct gatt_req_info, 1);
+               req_info->attr_path = g_strdup(object_path);
+               req_info->svc_path = g_strdup(svc_info->serv_path);
+               req_info->request_id = conn_info->connection_id;
+               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
+               req_info->offset = mtu;
+               req_info->context = invocation;
 
-                               if (svc_info == NULL) {
-                                       ERR("svc_info  info not found\n");
-                                       goto done;
-                               }
+               /* Append request info in list of requests for the particular connection */
+               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
 
-                               /* Store requets information */
-                               req_info = g_new0(struct gatt_req_info, 1);
-                               req_info->attr_path = g_strdup(object_path);
-                               req_info->svc_path = g_strdup(svc_info->serv_path);
-                               req_info->request_id = conn_info->connection_id;
-                               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_WRITE;
-                               req_info->offset = mtu;
-                               req_info->context = invocation;
+               /* Send HAL event */
+               memset(&ev, 0, sizeof(ev));
+               ev.conn_id = conn_info->connection_id;
+               ev.char_handl = char_hdl;
+               ev.mtu = mtu;
+               ev.trans_id = conn_info->connection_id;
+               _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
+               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
+               g_variant_iter_free(iter);
+               return;
+       } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
 
-                               /* Append request info in list of requests for the particular connection */
-                               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
+               uint16_t mtu = 512;
+               int char_hdl = -1;
+               struct hal_ev_gatt_server_acquire_notify  ev;
+               struct gatt_service_info *svc_info = NULL;
+               struct gatt_client_info_t *conn_info = NULL;
+               struct gatt_req_info *req_info = NULL;
 
-                               /* Send HAL event */
-                               memset(&ev, 0, sizeof(ev));
-                               ev.conn_id = conn_info->connection_id;
-                               ev.char_handl = char_hdl;
-                               ev.mtu = mtu;
-                               ev.trans_id = conn_info->connection_id;
-                               _bt_hal_convert_addr_string_to_type(ev.bdaddr, addr);
-                               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_WRITE_RES, (void *)&ev, sizeof(ev));
+               DBG("AcquireNotify : Application path %s, sender %s", object_path, sender);
 
-                               DBG("HAL_EV_GATT_ACQUIRE_WRITE_RES   called");
+               GVariantIter* iter;
+               g_variant_get(parameters, "(a{sv})", &iter);
+               char* key = NULL;
+               GVariant* value = NULL;
+               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
+                               if (g_strcmp0(key, "MTU") == 0)
+                                       g_variant_get(value, "q", &mtu);
+               }
 
+               DBG("MTU = %u",  mtu);
 
-                       return;
-               } else if (g_strcmp0(method_name, "AcquireNotify") == 0) {
-
-                               uint16_t mtu = 512;
-                               int char_hdl = -1;
-                               struct hal_ev_gatt_server_acquire_notify  ev;
-                               struct gatt_service_info *svc_info = NULL;
-                               struct gatt_client_info_t *conn_info = NULL;
-                               struct gatt_req_info *req_info = NULL;
-
-                               DBG("AcquireNotify");
-                               DBG("Application path = %s", object_path);
-                               DBG("Sender = %s", sender);
-
-                               GVariantIter* iter;
-                               g_variant_get(parameters, "(a{sv})", &iter);
-                               char* key = NULL;
-                               GVariant* value = NULL;
-                               while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
-                                               if (g_strcmp0(key, "MTU") == 0)
-                                                       g_variant_get(value, "q", &mtu);
-                               }
+               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
 
-                               DBG("AcquireNotify  values retived");
-                               DBG(" MTU = %u",  mtu);
+               if (svc_info == NULL) {
+                       ERR("svc_info  info not found\n");
+                       g_variant_iter_free(iter);
+                       goto done;
+               }
 
-                               svc_info = __bt_gatt_find_gatt_service_from_char(object_path, &char_hdl);
+               /* Store requets information */
+               req_info = g_new0(struct gatt_req_info, 1);
+               req_info->attr_path = g_strdup(object_path);
+               req_info->svc_path = g_strdup(svc_info->serv_path);
+               req_info->request_id = 33;
+               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
+               req_info->offset = mtu;
+               req_info->context = invocation;
 
-                       if (svc_info == NULL) {
-                                       ERR("svc_info  info not found\n");
-                                       goto done;
-                               }
+               conn_info = g_new0(struct gatt_client_info_t, 1);
+               conn_info->addr = g_strdup(object_path);
+               INFO("AcquireNotify : Added GATT client path[%s]", conn_info->addr);
+               conn_info->connection_id = 33;
+               conn_info->instance_id = 33;
+               /* Append request info in list of requests for the particular connection */
+               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
+               gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
 
-                               /* Store requets information */
-                               req_info = g_new0(struct gatt_req_info, 1);
-                               req_info->attr_path = g_strdup(object_path);
-                               req_info->svc_path = g_strdup(svc_info->serv_path);
-                               req_info->request_id = 33;
-                               req_info->request_type = BT_HAL_GATT_REQUEST_TYPE_ACQUIRE_NOTIFY;
-                               req_info->offset = mtu;
-                               req_info->context = invocation;
-
-                               conn_info = g_new0(struct gatt_client_info_t, 1);
-                               conn_info->addr = g_strdup(object_path);
-                               INFO("Added GATT client path[%s]", conn_info->addr);
-                               conn_info->connection_id = 33;
-                               conn_info->instance_id = 33;
-                               /* Append request info in list of requests for the particular connection */
-                               conn_info->gatt_req_info_list = g_slist_append(conn_info->gatt_req_info_list, req_info);
-                               gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
-
-                               /* Send HAL event */
-                               memset(&ev, 0, sizeof(ev));
-                               ev.conn_id = 33;
-                               ev.char_handl = char_hdl;
-                               ev.mtu = mtu;
-                               ev.trans_id = 33;
-
-                               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
-
-                               DBG("HAL_EV_GATT_ACQUIRE_NOTIFY_RES   called");
+               /* Send HAL event */
+               memset(&ev, 0, sizeof(ev));
+               ev.conn_id = 33;
+               ev.char_handl = char_hdl;
+               ev.mtu = mtu;
+               ev.trans_id = 33;
 
-                               return;
+               event_cb(HAL_EV_GATT_SERVER_ACQUIRE_NOTIFY_RES, (void *)&ev, sizeof(ev));
+               g_variant_iter_free(iter);
+               return;
        }
 
 done:
        g_dbus_method_invocation_return_value(invocation, NULL);
 }
 
-static gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *interface)
+static gboolean __bt_hal_gatt_emit_interface_removed(const char *app_path, gchar *object_path, gchar *interface)
 {
        gboolean ret;
        GError *error = NULL;
@@ -1482,8 +1560,8 @@ static gboolean __bt_hal_gatt_emit_interface_removed(gchar *object_path, gchar *
        g_variant_builder_init(array_builder, G_VARIANT_TYPE("as"));
        g_variant_builder_add(array_builder, "s", interface);
 
-       ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
-                       "org.freedesktop.Dbus.Objectmanager",
+       ret = g_dbus_connection_emit_signal(g_conn, NULL, app_path,
+                       "org.freedesktop.DBus.ObjectManager",
                        "InterfacesRemoved",
                        g_variant_new("(oas)",
                                object_path, array_builder),
@@ -1536,17 +1614,6 @@ static void __bt_hal_gatt_free_characteristic_info(struct gatt_char_info *char_i
        g_free(char_info);
 }
 
-
-static void __bt_hal_gatt_free_service_info(struct gatt_service_info *svc_info)
-{
-       if (!svc_info)
-               return;
-
-       g_free(svc_info->serv_path);
-       g_free(svc_info->service_uuid);
-       g_free(svc_info);
-}
-
 static const GDBusInterfaceVTable desc_interface_vtable = {
        __bt_gatt_desc_method_call,
        NULL,
@@ -1770,11 +1837,6 @@ static int bt_hal_gatts_get_gatt_server_instance()
        return -1;
 }
 
-int bt_hal_gatts_generate_server_id()
-{
-       return ++g_server_inst_id;
-}
-
 static struct hal_gatts_server_register_info_t * bt_hal_gatts_find_server_register_info_from_uuid(bt_uuid_t *app_uuid)
 {
        DBG("+");
@@ -1799,8 +1861,6 @@ static struct hal_gatts_server_register_info_t * bt_hal_gatts_find_server_regist
 
 static struct hal_gatts_server_register_info_t * bt_hal_gatts_find_server_register_info(int server_if)
 {
-       DBG("+");
-
        GSList *l;
        struct hal_gatts_server_register_info_t *info = NULL;
 
@@ -1811,7 +1871,6 @@ static struct hal_gatts_server_register_info_t * bt_hal_gatts_find_server_regist
                }
 
                if (info->server_if == server_if) {
-                       DBG("gatt server register found");
                        return info;
                }
        }
@@ -1843,9 +1902,8 @@ static struct hal_gatts_server_register_info_t * bt_hal_gatts_find_server_regist
 
 static struct hal_gatts_server_register_info_t * bt_hal_gatts_add_server_app(bt_uuid_t *app_uuid)
 {
-       DBG("+");
-
        struct hal_gatts_server_register_info_t * server_register_info = NULL;
+       int instance_id = 0;
 
        server_register_info = bt_hal_gatts_find_server_register_info_from_uuid(app_uuid);
        if (server_register_info != NULL) {
@@ -1853,12 +1911,16 @@ static struct hal_gatts_server_register_info_t * bt_hal_gatts_add_server_app(bt_
                return server_register_info;
        }
 
-       DBG("adding the gatt server app");
+       instance_id = __bt_hal_gatt_assign_id();
+       if (instance_id == -1) {
+               ERR("Fail to allocate the server if");
+               return NULL;
+       }
 
        server_register_info = g_malloc0(sizeof(struct hal_gatts_server_register_info_t));
-       server_register_info->server_if = bt_hal_gatts_generate_server_id();
+       server_register_info->server_if = instance_id;
 
-       DBG("amit server_if:[%d]", server_register_info->server_if);
+       DBG("Adding the gatt server app. server_if:[%d]", server_register_info->server_if);
 
        server_register_info->adv_slot_id = -1;
        memcpy(&server_register_info->app_uuid, app_uuid, sizeof(bt_uuid_t));
@@ -1873,9 +1935,13 @@ static bt_status_t gatt_server_register_app(bt_uuid_t *uuid)
        struct hal_gatts_server_register_info_t *server_register_info = NULL;
 
        DBG("Register server instance request");
-       hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
-
        server_register_info = bt_hal_gatts_add_server_app(uuid);
+       if (server_register_info == NULL) {
+               ERR("Fail to register the server app");
+               return BT_STATUS_FAIL;
+       }
+
+       hal_register_server_data *user_data = g_malloc0(sizeof(hal_register_server_data));
        user_data->instance_data = server_register_info->server_if;
 
        /*
@@ -1943,6 +2009,8 @@ static bt_status_t gatt_server_unregister_app(int server_if)
        hal_gatts_server_register_list = g_slist_remove(hal_gatts_server_register_list, server_register_info);
        g_free(server_register_info);
 
+       __bt_hal_gatt_delete_id(server_if);
+
        /* If server_if belongs to a GATT Server, then delete the GATT server from List */
        _bt_hal_remove_gatt_server_from_list(server_if);
        return BT_STATUS_SUCCESS;
@@ -2133,7 +2201,7 @@ static int __bt_hal_add_service_to_dbus(char *app_path, int slot, btgatt_srvc_id
                        GATT_SERV_INTERFACE, inner_builder);
 
        ret = g_dbus_connection_emit_signal(g_conn, NULL, "/",
-                       "org.freedesktop.Dbus.ObjectManager",
+                       "org.freedesktop.Dbus.ObjectManager", // TODO: need to check Dbus is working or not
                        "InterfacesAdded",
                        g_variant_new("(oa{sa{sv}})",
                                path, builder),
@@ -2259,6 +2327,8 @@ static int __bt_hal_gatt_init(void)
                owner_id = g_bus_own_name_on_connection(g_conn, name,
                                        G_BUS_NAME_OWNER_FLAGS_NONE,
                                        NULL, NULL, NULL, NULL);
+
+               g_free(name);
        }
        INFO("Owner ID [%d]", owner_id);
 
@@ -2305,10 +2375,8 @@ static void _bt_hal_update_gatt_server_path(int slot, char *app_path)
        app->slot = slot;
        gatt_server_apps = g_slist_append(gatt_server_apps, app);
        INFO("GATT Server: Path [%s] Slot [%d]-> Updated", app_path, slot);
-
 }
 
-
 static bt_status_t gatt_server_add_service(int server_if, btgatt_srvc_id_t *srvc_id,
                int num_handles)
 {
@@ -2598,14 +2666,8 @@ static bt_status_t gatt_server_add_descriptor(int slot, int service_handle, bt_u
        char *desc_flags[NUMBER_OF_FLAGS];
        int flag_count = 0;
        int *app_id;
-
        hal_gatt_desc_added *user_data = NULL;
-#if 0
-       if (new_char) {
-               desc_id = 1;
-               new_char = FALSE;
-       }
-#endif
+
        /* Fetch service data for the GATT server */
        serv_info = __bt_gatt_find_gatt_service_info(slot, service_handle);
        if (serv_info == NULL)
@@ -2870,6 +2932,7 @@ static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
        GSList *l1 = NULL;
        int err = BT_STATUS_SUCCESS;
        int ret = BT_STATUS_SUCCESS;
+       char *app_path = NULL;
        INFO("Slot [%d] service handle [%d]", server_if, service_handle);
 
        /* Fetch service data for the GATT server */
@@ -2880,6 +2943,13 @@ static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
                return BT_STATUS_FAIL;
        }
 
+       app_path = __bt_get_server_app_path_from_server(server_if);
+       if (app_path == NULL) {
+               ERR("Could not find service info svc handle [%d] server slot [%d]",
+                               service_handle, server_if);
+               return BT_STATUS_FAIL;
+       }
+
        if (serv_info->is_svc_registered == FALSE) {
                ERR("service Not registered path [%s] handle [%d]",
                        serv_info->serv_path, service_handle);
@@ -2901,6 +2971,7 @@ static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
                                        desc_info->desc_id);
                        if (ret) {
                                __bt_hal_gatt_emit_interface_removed(
+                                               app_path,
                                                desc_info->desc_path,
                                                GATT_DESC_INTERFACE);
                        } else {
@@ -2918,7 +2989,7 @@ static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
                ret = g_dbus_connection_unregister_object(g_conn,
                                char_info->char_id);
                if (ret) {
-                       __bt_hal_gatt_emit_interface_removed(char_info->char_path,
+                       __bt_hal_gatt_emit_interface_removed(app_path, char_info->char_path,
                                        GATT_CHAR_INTERFACE);
                } else {
                        INFO("Err");
@@ -2935,7 +3006,7 @@ static bt_status_t gatt_server_delete_service(int server_if, int service_handle)
 
        ret = g_dbus_connection_unregister_object(g_conn, serv_info->serv_id);
        if (ret) {
-               __bt_hal_gatt_emit_interface_removed(serv_info->serv_path,
+               __bt_hal_gatt_emit_interface_removed(app_path, serv_info->serv_path,
                                GATT_SERV_INTERFACE);
        } else {
                INFO("Failed!!");
@@ -2978,12 +3049,8 @@ static gboolean __bt_gatt_get_service_state(const char *service_path)
 
                svc_info = (struct gatt_service_info *)l->data;
 
-
-               if (!g_strcmp0(svc_info->serv_path, service_path)) {
-                       DBG("Return the state of the gatt service %d",
-                                       svc_info->is_svc_registered);
+               if (!g_strcmp0(svc_info->serv_path, service_path))
                        return svc_info->is_svc_registered;
-               }
        }
 
        DBG("gatt service info is NULL");
@@ -3150,22 +3217,20 @@ static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
        struct gatt_client_info_t *conn_info = NULL;
        int i;
 
-       DBG("GATT Server Send Response Conn ID [%d]", conn_id);
-
        conn_info = __bt_find_remote_gatt_client_info_from_conn(conn_id);
        if (conn_info == NULL) {
-               ERR("No Connection Inforamtion!!!");
+               ERR("No Connection Inforamtion. conn_id %d", conn_id);
                return BT_STATUS_FAIL;
        }
 
        req_info =  __bt_find_remote_gatt_client_request_info(conn_id, trans_id);
        if (req_info == NULL) {
-               ERR("No Request Inforamtion!!!");
+               ERR("No Request Inforamtion. conn_id %d", conn_id);
                return BT_STATUS_FAIL;
        }
 
        if (status != BT_STATUS_SUCCESS) {
-               ERR("resp_state is 0x%X", status);
+               ERR("resp_state is 0x%X, conn_id %d", status, conn_id);
 
                g_dbus_method_invocation_return_dbus_error(req_info->context,
                                "org.bluez.Error.Failed", "Application Error");
@@ -3182,13 +3247,6 @@ static bt_status_t gatt_server_send_response(int conn_id, int trans_id,
                return BT_STATUS_SUCCESS;
        }
 
-       DBG("Reponse Value length [%d]", response->attr_value.len);
-       DBG("Request type: [%d]", req_info->request_type);
-
-       /* DEBUG */
-       for (i = 0; i < response->attr_value.len; i++)
-               DBG("Resonse [%d] = [0x%x]", i, response->attr_value.value[i]);
-
        if (req_info->request_type == BT_HAL_GATT_REQUEST_TYPE_READ) {
                GVariantBuilder *inner_builder = NULL;
                inner_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
@@ -3320,6 +3378,8 @@ static bt_status_t gatt_server_update_att_value(int server_if, int attribute_han
        }
 
        g_free(serv_path);
+       g_strfreev(line_argv);
+
        line_argv = g_strsplit_set(char_path, "/", 0);
        serv_path = g_strdup_printf("/%s/%s/%s", line_argv[1], line_argv[2], line_argv[3]);
 
@@ -3432,20 +3492,18 @@ static int bt_hal_gatts_allocate_adv_slot(struct hal_gatts_server_register_info_
 {
        int adv_slot_id = -1;
 
-       DBG("gatt server:[%d], adv_slot:[%d]", server_register_info->server_if, server_register_info->adv_slot_id);
-
        /* allocate adv slot for gatt server if not assigned */
        if (server_register_info->adv_slot_id < 0 ) {
                adv_slot_id = _bt_hal_get_available_adv_slot_id(&server_register_info->app_uuid, FALSE);
                if (adv_slot_id < 0) {
-                       DBG("failed to get the adv_slot \n");
+                       ERR("failed to get the adv_slot");
                        return -1;
                }
 
                server_register_info->adv_slot_id = adv_slot_id;
        }
 
-       DBG("gatt server is assigned with adv_slot [%d]", server_register_info->adv_slot_id);
+       DBG("gatt server:[%d] Assigned adv_slot [%d]", server_register_info->server_if, server_register_info->adv_slot_id);
 
        return 0;
 }
@@ -3487,14 +3545,12 @@ void bt_hal_gatts_release_adv_slot(int server_if)
 
        server_register_info =  bt_hal_gatts_find_server_register_info(server_if);
        if (server_register_info == NULL) {
-               DBG("failed to get server_register_info");
+               ERR("failed to get server_register_info");
                return;
        }
 
-       DBG("gatt server:[%d], adv_slot:[%d]", server_register_info->server_if, server_register_info->adv_slot_id);
-
        if (server_register_info->adv_slot_id < 0) {
-               DBG("adv_slot is not allocated");
+               ERR("adv_slot is not allocated");
                return;
        }
 
@@ -3539,22 +3595,20 @@ static bt_status_t gatt_server_multi_adv_enable(int server_if)
 
        struct hal_gatts_server_register_info_t *server_register_info = NULL;
 
-       DBG("server_if: [%d]", server_if);
-
        server_register_info =  bt_hal_gatts_find_server_register_info(server_if);
        if (server_register_info == NULL) {
-               DBG("gatt server is not registered");
+               ERR("gatt server is not registered");
                return BT_STATUS_FAIL;
        }
 
        DBG("gatt server:[%d], adv_slot:[%d]", server_register_info->server_if, server_register_info->adv_slot_id);
 
        if (server_register_info->adv_slot_id < 0 ) {
-               DBG("adv_slot is not assigned to server:[%d] \n", server_if);
+               DBG("adv_slot is not assigned to server:[%d]", server_if);
 
                ret = bt_hal_gatts_allocate_adv_slot(server_register_info);
                if (ret < 0) {
-                       DBG("failed to get adv_slot");
+                       ERR("failed to get adv_slot");
                        return BT_STATUS_FAIL;
                }
        }
@@ -3567,7 +3621,7 @@ static bt_status_t gatt_server_multi_adv_update(int server_if, int min_interval,
                int chnl_map, int tx_power, int timeout_s)
 {
        CHECK_BTGATT_INIT();
-       DBG("+");
+
        /* Send Advertising parameters to LE Module */
        return _bt_hal_set_advertising_params(server_if, min_interval, max_interval, adv_type,
                        chnl_map, tx_power, timeout_s);
@@ -3585,7 +3639,7 @@ static bt_status_t gatt_server_set_filter_policy(int filter_policy)
 static bt_status_t gatt_server_multi_adv_set_inst_data(btgatt_adv_param_setup_t adv_param_setup)
 {
        CHECK_BTGATT_INIT();
-       DBG("+");
+
        /* Send Data to LE Module */
        return _bt_hal_set_advertising_data(adv_param_setup);
 }