Fix unintended unregister issue for gatt server instance 06/215806/3 submit/tizen/20191018.015644
authorWootak Jung <wootak.jung@samsung.com>
Wed, 16 Oct 2019 06:13:41 +0000 (15:13 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Thu, 17 Oct 2019 00:58:04 +0000 (09:58 +0900)
In case client_id and instance_id are same, gatt server is
unregistered although we need to unregister gatt client.

Change name of function __bt_do_unregister_server_instance()
for readability.

Change-Id: Ib0f1e4a66e5cfabf52c3dc5a02b84fb031db3e7e

bt-service-adaptation/services/gatt/bt-service-gatt.c

index 8a9ffc2..a0413c0 100644 (file)
@@ -251,7 +251,6 @@ static void __bt_handle_client_characteristic_read_data(event_gattc_read_data *e
 static void __bt_handle_client_descriptor_read_data(event_gattc_read_data *event_data);
 static void __bt_handle_client_characteristic_write_data(event_gattc_write_data *event_data);
 static void __bt_handle_client_descriptor_write_data(event_gattc_write_data *event_data);
-static int __bt_do_unregister_gatt_instance(int instance_id);
 static void __bt_hanlde_le_device_disconnection(event_dev_conn_status_t *event_data);
 static void __bt_handle_client_notification_registered(event_gattc_regdereg_notify_t *event_data,
                gboolean is_registered);
@@ -259,7 +258,7 @@ static void __bt_handle_client_notification_data(event_gattc_notify_data *event_
 
 #endif
 
-static int __bt_do_unregister_server_instance(int server_instance);
+static int __bt_unregister_gatt_server_instance(int server_instance);
 
 /* Linked List of GATT requests from Remote GATT Clients */
 static GSList *gatt_server_requests = NULL;
@@ -319,7 +318,7 @@ void _bt_check_adv_app_termination(const char *name)
                if (apps[k] == 1) {
                        BT_INFO("Unregister app[%d]", k);
                        /* Unregister server instance */
-                       __bt_do_unregister_server_instance(k);
+                       __bt_unregister_gatt_server_instance(k);
                }
        }
 }
@@ -596,58 +595,7 @@ void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len,
        }
 }
 
-#ifdef TIZEN_GATT_CLIENT
-static int __bt_do_unregister_gatt_instance(int instance_id)
-{
-       int ret = OAL_STATUS_SUCCESS;
-       int k;
-
-       BT_INFO("DeAllocate server or client instance ID [%d]", instance_id);
-
-       /* Reset data: instance_id parameter could be either for GATT Server or for GATT client  */
-       for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
-               if (numapps[k].instance_id == instance_id) {
-                       BT_INFO("This is a GATT server app, unregister: Slot [%d] vacant", k);
-                       numapps[k].is_initialized = FALSE;
-                       numapps[k].instance_id = -1;
-                       numapps[k].adv_handle = 0;
-                       numapps[k].adv_instance = -1;
-                       memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
-                       memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
-                       memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
-                       memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
-                       numapps[k].adv_data_len = 0;
-                       numapps[k].scan_rsp_len = 0;
-
-                       /* Its a GATT Server Instance */
-                       ret = gatts_unregister(instance_id);
-                       if (ret != OAL_STATUS_SUCCESS) {
-                               BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
-                               return BLUETOOTH_ERROR_INTERNAL;
-                       }
-                       break;
-               } else if (numapps[k].client_id == instance_id) {
-                       BT_INFO("This is a GATT client app, unregister: Slot [%d] vacant", k);
-                       numapps[k].client_id = -1;
-                       numapps[k].is_initialized = FALSE;
-                       memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
-                       memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
-                       memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
-
-                       /* Its a GATT Client Instance */
-                       ret = gattc_deregister(instance_id);
-                       if (ret != OAL_STATUS_SUCCESS) {
-                               BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
-                               return BLUETOOTH_ERROR_INTERNAL;
-                       }
-                       break;
-               }
-       }
-       return BLUETOOTH_ERROR_NONE;
-}
-#endif
-
-static int __bt_do_unregister_server_instance(int server_instance)
+static int __bt_unregister_gatt_server_instance(int server_instance)
 {
        int ret = OAL_STATUS_SUCCESS;
        int k;
@@ -758,7 +706,7 @@ int _bt_unregister_server_instance(const char *sender, int adv_handle)
                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_do_unregister_server_instance(server_instance);
+                       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);
@@ -774,7 +722,7 @@ int _bt_unregister_server_instance(const char *sender, int adv_handle)
                if (apps[k] == 1) {
                        BT_INFO("Unregister app[%d]", k);
                        /* Unregister server instance */
-                       __bt_do_unregister_server_instance(k);
+                       __bt_unregister_gatt_server_instance(k);
                }
        }
 
@@ -4806,29 +4754,37 @@ int _bt_gatt_watch_service_changed_indication(const char *sender,
 
 int _bt_unregister_gatt_client_instance(const char *sender, int client_id)
 {
-       BT_INFO("Unregister Allocated GATT CLient instance [%s] Client ID [%d]",
-                       sender, client_id);
-       int result = BLUETOOTH_ERROR_NONE;
-       int k;
+       bt_service_app_info_t *info = NULL;
+       int k, ret;
+
+       BT_INFO("Unregister Allocated GATT Client instance [%s] Client ID [%d]", sender, client_id);
 
        /* Unregister CLient instance associated with address X. It is possible that another app still
           has client_id valid for same remote address */
-       bt_service_app_info_t *info = NULL;
-
        for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
                info = &numapps[k];
 
                /* Exact matching of sender */
                if (!g_strcmp0(info->sender, sender) && info->client_id == client_id) {  /* Check for only valid GATT client Instance */
                        BT_INFO("Unregister GATT client instance [%d]", info->client_id);
-                       result = __bt_do_unregister_gatt_instance(info->client_id);
-                       if (result != BLUETOOTH_ERROR_NONE)
-                               BT_ERR("Error in unregistering GATT Client Interface");
+                       numapps[k].client_id = -1;
+                       numapps[k].is_initialized = FALSE;
+                       memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
+                       memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
+                       memset(&numapps[k].address.addr, 0x00, sizeof(bluetooth_device_address_t));
 
-                       break;
+                       /* Its a GATT Client Instance */
+                       ret = gattc_deregister(client_id);
+                       if (ret != OAL_STATUS_SUCCESS) {
+                               BT_ERR("DeAllocate GATT Client instance with stack Fail ret: %d", ret);
+                               return BLUETOOTH_ERROR_INTERNAL;
+                       } else {
+                               return BLUETOOTH_ERROR_NONE;
+                       }
                }
        }
-       return result;
+
+       return BLUETOOTH_ERROR_NOT_FOUND;
 }
 
 #endif