Fix uuid duplication issue for registering server instance
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / gatt / bt-service-gatt.c
index e6460af..6e8fb60 100644 (file)
@@ -212,7 +212,6 @@ typedef struct {
 } bt_gatt_included_service_info_t;
 
 static GSList *list_gatt_info = NULL;
-
 #endif
 
 /* App Information structure */
@@ -780,13 +779,13 @@ int _bt_unregister_server_instance(const char *sender, int adv_handle)
        int apps[MAX_APPS_SUPPORTED];
        int server_instance;
        int k;
+       bt_service_app_info_t *info = NULL;
 
        memset(&apps, -1, sizeof(apps));
 
        if (adv_handle == 0) {
                BT_DBG("Its a direct GATT Server app request to unregister");
                /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
-               bt_service_app_info_t *info = NULL;
 
                for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                        info = &numapps[k];
@@ -814,18 +813,24 @@ int _bt_unregister_server_instance(const char *sender, int adv_handle)
                        BT_ERR("No allocated server instance to be removed");
                        return BLUETOOTH_ERROR_INVALID_PARAM;
                }
-               if (!numapps[server_instance].service_handles) {
-                       BT_INFO("There are no Service handles with this app, safe to unregister");
-                       /* Unregister server instance only if this sender does not have any gatt services in it */
-                       result = __bt_unregister_gatt_server_instance(server_instance);
-               } else {
-                       numapps[server_instance].adv_handle = 0;
-                       memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
-                       numapps[server_instance].adv_data_len = 0;
-                       memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
-                       numapps[server_instance].scan_rsp_len = 0;
+
+               for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+                       info = &numapps[k];
+                       if (info->instance_id == server_instance) {
+                               if (info->service_handles == NULL) {
+                                       BT_INFO("There are no Service handles with this app, safe to unregister");
+                                       /* Unregister server instance only if this sender does not have any gatt services in it */
+                                       result = __bt_unregister_gatt_server_instance(server_instance);
+                               } else {
+                                       info->adv_handle = 0;
+                                       memset(info->adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+                                       info->adv_data_len = 0;
+                                       memset(info->scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
+                                       info->scan_rsp_len = 0;
+                               }
+                               return result;
+                       }
                }
-               return result;
        }
 
        /* Unregister all apps one by one */
@@ -850,7 +855,7 @@ int _bt_register_server_instance(const char *sender, int adv_handle)
 
        BT_INFO("Check on which instance Server instance can be initialized....");
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
-               if (numapps[k].is_initialized == 1) {
+               if (numapps[k].is_initialized == 1 || strlen(numapps[k].uuid) > 0) {
                        BT_DBG("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
                } else {
                        slot = k;
@@ -892,6 +897,24 @@ int _bt_register_server_instance(const char *sender, int adv_handle)
 }
 
 /* Event handlers */
+static void __bt_gatt_get_pending_request_info(int service_function,
+               char **sender)
+{
+       GSList *l;
+       invocation_info_t *req_info = NULL;
+
+       for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
+               req_info = l->data;
+               if (req_info == NULL)
+                       continue;
+
+               if (req_info->service_function == service_function) {
+                       *sender = req_info->sender;
+                       break;
+               }
+       }
+}
+
 static void __bt_gatt_handle_pending_request_info(int result,
                int service_function, void *data, unsigned int size)
 {
@@ -2353,6 +2376,8 @@ int _bt_gatt_server_delete_service(char *sender, int service_handle, int instanc
        int ret = OAL_STATUS_SUCCESS;
        GSList *l;
        int *handle = NULL;
+       int k;
+       bt_service_app_info_t *info = NULL;
 
        ret = gatts_delete_service(instance_id, service_handle);
        if (ret != OAL_STATUS_SUCCESS) {
@@ -2361,14 +2386,20 @@ int _bt_gatt_server_delete_service(char *sender, int service_handle, int instanc
        }
 
        /* Remove the Service Handle */
-       for (l = numapps[instance_id].service_handles; l != NULL;) {
-               handle = l->data;
-               l = g_slist_next(l);
-               if (handle && *handle == service_handle) {
-                       BT_INFO("Remove Service handle [%d]", *handle);
-                       numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
-                       g_free(handle);
-                       handle = NULL;
+       for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
+               info = &numapps[k];
+               if (info->instance_id == instance_id) {
+                       for (l = info->service_handles; l != NULL; ) {
+                               handle = l->data;
+                               l = g_slist_next(l);
+                               if (handle && *handle == service_handle) {
+                                       BT_INFO("Remove Service handle [%d]", *handle);
+                                       info->service_handles = g_slist_remove(info->service_handles, handle);
+                                       g_free(handle);
+                                       handle = NULL;
+                               }
+                       }
+                       break;
                }
        }
 
@@ -2577,7 +2608,7 @@ int _bt_get_att_mtu(bluetooth_device_address_t *address,
 {
        BT_CHECK_PARAMETER(address, return);
        BT_CHECK_PARAMETER(mtu, return);
-       struct gatt_client_info_t *conn_info = NULL;
+       struct gatt_client_info_t *client_info = NULL;
        char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
        int ret = OAL_STATUS_SUCCESS;
        int stack_mtu;
@@ -2586,26 +2617,32 @@ int _bt_get_att_mtu(bluetooth_device_address_t *address,
 
        BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
 
-       conn_info = _bt_find_remote_gatt_client_info(addr);
-       if (conn_info) {
+       client_info = _bt_find_remote_gatt_client_info(addr);
+       if (client_info) {
                BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
-                               conn_info->addr, conn_info->connection_id, conn_info->instance_id);
-       } else {
-               BT_ERR("GATT Client [%s] is not yet connected..", addr);
-               return BLUETOOTH_ERROR_NOT_CONNECTED;
-       }
+                               client_info->addr, client_info->connection_id, client_info->instance_id);
 
-       if (conn_info->instance_id == -1) {
-               ret = gattc_get_att_mtu(conn_info->connection_id, &stack_mtu);
+               ret = gatts_get_att_mtu(client_info->connection_id, &stack_mtu);
                if (ret != OAL_STATUS_SUCCESS) {
                        BT_ERR("ret: %d", ret);
                        return _bt_convert_oal_status_to_bt_error(ret);
                }
        } else {
-               ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
-               if (ret != OAL_STATUS_SUCCESS) {
-                       BT_ERR("ret: %d", ret);
-                       return _bt_convert_oal_status_to_bt_error(ret);
+               struct gatt_server_info_t *server_info = NULL;
+               BT_ERR("GATT Client [%s] is not yet connected..", addr);
+               server_info = _bt_find_remote_gatt_server_info(addr);
+               if (server_info) {
+                       BT_INFO("GATT Server [%s] is connected, conn Id [%d] Client ID [%d]",
+                                       server_info->addr, server_info->connection_id, server_info->client_id);
+
+                       ret = gattc_get_att_mtu(server_info->connection_id, &stack_mtu);
+                       if (ret != OAL_STATUS_SUCCESS) {
+                               BT_ERR("ret: %d", ret);
+                               return _bt_convert_oal_status_to_bt_error(ret);
+                       }
+               } else {
+                       BT_ERR("GATT Server [%s] is not yet connected..", addr);
+                       return BLUETOOTH_ERROR_NOT_CONNECTED;
                }
        }
 
@@ -3629,7 +3666,9 @@ Note: Even in case of failure, address, handles and result code should be return
                                data);
 
                /* Send Event */
-               _bt_send_event(BT_GATT_CLIENT_EVENT,
+               char *sender = NULL;
+               __bt_gatt_get_pending_request_info(BT_GATT_READ_CHARACTERISTIC, &sender);
+               _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
                                BLUETOOTH_EVENT_GATT_READ_CHAR,
                                param);
        }
@@ -3674,7 +3713,7 @@ static void __bt_handle_client_descriptor_read_data(
        conn_info = __bt_find_remote_gatt_server_info_from_conn_id(
                        event_data->uuid_status.conn_status.conn_id);
 
-       BT_INFO("Descriptor Read result from addr [%s] status [%d]",
+       BT_DBG("Descriptor Read result from addr [%s] status [%d]",
                        conn_info->addr, event_data->uuid_status.conn_status.status);
 
        /* Fill descriptor informations in buffer */
@@ -3699,7 +3738,6 @@ static void __bt_handle_client_descriptor_read_data(
        if (event_data->uuid_status.conn_status.status != OAL_STATUS_SUCCESS)
                result = BLUETOOTH_ERROR_INTERNAL;
        else {
-               BT_INFO("desc data len:%d", event_data->data_len);
                if (event_data->data_len > 0) {
                        /* DEBUG */
                        for (i = 0; i < event_data->data_len; i++)
@@ -3764,11 +3802,13 @@ static void __bt_handle_client_descriptor_read_data(
                                data);
 
                /* Send Event */
-               _bt_send_event(BT_GATT_CLIENT_EVENT,
+               char *sender = NULL;
+               __bt_gatt_get_pending_request_info(BT_GATT_READ_DESCRIPTOR_VALUE, &sender);
+               _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
                                        BLUETOOTH_EVENT_GATT_READ_DESC,
                                        param);
        }
-       BT_INFO("Send DBUS rpely for GATT Read Descriptor");
+
        /* Send DBUS return */
        __bt_gatt_handle_pending_request_info(result,
                        BT_GATT_READ_DESCRIPTOR_VALUE,
@@ -3855,7 +3895,9 @@ static void __bt_handle_client_characteristic_write_data(
                        event_data->char_id.inst_id);
 
        /* Send Event */
-       _bt_send_event(BT_GATT_CLIENT_EVENT,
+       char *sender = NULL;
+       __bt_gatt_get_pending_request_info(BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE, &sender);
+       _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
                        BLUETOOTH_EVENT_GATT_WRITE_CHAR,
                        param);
 
@@ -3864,6 +3906,7 @@ static void __bt_handle_client_characteristic_write_data(
                g_free(svc_uuid);
        if (char_uuid)
                g_free(char_uuid);
+
 done:
        /* Send DBUS return */
        __bt_gatt_handle_pending_request_info(result,
@@ -3901,7 +3944,7 @@ static void __bt_handle_client_descriptor_write_data(
                return;
        }
 
-       BT_INFO("Descriptor Write callback from addr [%s] status [%d]",
+       BT_DBG("Descriptor Write callback from addr [%s] status [%d]",
                        conn_info->addr, event_data->conn_status.status);
 
        /* Fill descriptor informations in buffer */
@@ -3968,7 +4011,9 @@ static void __bt_handle_client_descriptor_write_data(
                        event_data->descr_id.inst_id);
 
        /* Send Event */
-       _bt_send_event(BT_GATT_CLIENT_EVENT,
+       char *sender = NULL;
+       __bt_gatt_get_pending_request_info(BT_GATT_WRITE_DESCRIPTOR_VALUE, &sender);
+       _bt_send_event_to_dest(sender, BT_GATT_CLIENT_EVENT,
                        BLUETOOTH_EVENT_GATT_WRITE_DESC,
                        param);